1 //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PowerPC-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // PPCGenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/PPCPredicates.h"
18 #include "PPCCCState.h"
19 #include "PPCCallingConv.h"
20 #include "PPCISelLowering.h"
21 #include "PPCMachineFunctionInfo.h"
22 #include "PPCSubtarget.h"
23 #include "PPCTargetMachine.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/CodeGen/CallingConvLower.h"
26 #include "llvm/CodeGen/FastISel.h"
27 #include "llvm/CodeGen/FunctionLoweringInfo.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/TargetLowering.h"
33 #include "llvm/IR/CallingConv.h"
34 #include "llvm/IR/GetElementPtrTypeIterator.h"
35 #include "llvm/IR/GlobalAlias.h"
36 #include "llvm/IR/GlobalVariable.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Target/TargetMachine.h"
42 //===----------------------------------------------------------------------===//
45 // fastLowerArguments: Handle simple cases.
46 // PPCMaterializeGV: Handle TLS.
47 // SelectCall: Handle function pointers.
48 // SelectCall: Handle multi-register return values.
49 // SelectCall: Optimize away nops for local calls.
50 // processCallArgs: Handle bit-converted arguments.
51 // finishCall: Handle multi-register return values.
52 // PPCComputeAddress: Handle parameter references as FrameIndex's.
53 // PPCEmitCmp: Handle immediate as operand 1.
54 // SelectCall: Handle small byval arguments.
55 // SelectIntrinsicCall: Implement.
56 // SelectSelect: Implement.
57 // Consider factoring isTypeLegal into the base class.
58 // Implement switches and jump tables.
60 //===----------------------------------------------------------------------===//
63 #define DEBUG_TYPE "ppcfastisel"
67 typedef struct Address {
80 // Innocuous defaults for our address.
82 : BaseType(RegBase), Offset(0) {
87 class PPCFastISel final : public FastISel {
89 const TargetMachine &TM;
90 const PPCSubtarget *PPCSubTarget;
91 PPCFunctionInfo *PPCFuncInfo;
92 const TargetInstrInfo &TII;
93 const TargetLowering &TLI;
97 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
98 const TargetLibraryInfo *LibInfo)
99 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
100 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
101 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
102 TII(*PPCSubTarget->getInstrInfo()),
103 TLI(*PPCSubTarget->getTargetLowering()),
104 Context(&FuncInfo.Fn->getContext()) {}
106 // Backend specific FastISel code.
108 bool fastSelectInstruction(const Instruction *I) override;
109 unsigned fastMaterializeConstant(const Constant *C) override;
110 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
111 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
112 const LoadInst *LI) override;
113 bool fastLowerArguments() override;
114 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
115 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill,
119 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
120 const TargetRegisterClass *RC,
121 unsigned Op0, bool Op0IsKill);
122 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 unsigned Op1, bool Op1IsKill);
127 bool fastLowerCall(CallLoweringInfo &CLI) override;
129 // Instruction selection routines.
131 bool SelectLoad(const Instruction *I);
132 bool SelectStore(const Instruction *I);
133 bool SelectBranch(const Instruction *I);
134 bool SelectIndirectBr(const Instruction *I);
135 bool SelectFPExt(const Instruction *I);
136 bool SelectFPTrunc(const Instruction *I);
137 bool SelectIToFP(const Instruction *I, bool IsSigned);
138 bool SelectFPToI(const Instruction *I, bool IsSigned);
139 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
140 bool SelectRet(const Instruction *I);
141 bool SelectTrunc(const Instruction *I);
142 bool SelectIntExt(const Instruction *I);
146 bool isTypeLegal(Type *Ty, MVT &VT);
147 bool isLoadTypeLegal(Type *Ty, MVT &VT);
148 bool isValueAvailable(const Value *V) const;
149 bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
150 return RC->getID() == PPC::VSFRCRegClassID;
152 bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
153 return RC->getID() == PPC::VSSRCRegClassID;
155 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
156 bool isZExt, unsigned DestReg,
157 const PPC::Predicate Pred);
158 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
159 const TargetRegisterClass *RC, bool IsZExt = true,
160 unsigned FP64LoadOpc = PPC::LFD);
161 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
162 bool PPCComputeAddress(const Value *Obj, Address &Addr);
163 void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
165 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
166 unsigned DestReg, bool IsZExt);
167 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
168 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
169 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
170 bool UseSExt = true);
171 unsigned PPCMaterialize32BitInt(int64_t Imm,
172 const TargetRegisterClass *RC);
173 unsigned PPCMaterialize64BitInt(int64_t Imm,
174 const TargetRegisterClass *RC);
175 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
176 unsigned SrcReg, bool IsSigned);
177 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
179 // Call handling routines.
181 bool processCallArgs(SmallVectorImpl<Value*> &Args,
182 SmallVectorImpl<unsigned> &ArgRegs,
183 SmallVectorImpl<MVT> &ArgVTs,
184 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
185 SmallVectorImpl<unsigned> &RegArgs,
189 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
190 LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
193 #include "PPCGenFastISel.inc"
197 } // end anonymous namespace
199 #include "PPCGenCallingConv.inc"
201 // Function whose sole purpose is to kill compiler warnings
202 // stemming from unused functions included from PPCGenCallingConv.inc.
203 CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
205 return CC_PPC32_SVR4;
207 return CC_PPC32_SVR4_ByVal;
209 return CC_PPC32_SVR4_VarArg;
211 return RetCC_PPC_Cold;
216 static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
218 // These are not representable with any single compare.
219 case CmpInst::FCMP_FALSE:
220 case CmpInst::FCMP_TRUE:
221 // Major concern about the following 6 cases is NaN result. The comparison
222 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
223 // only one of which will be set. The result is generated by fcmpu
224 // instruction. However, bc instruction only inspects one of the first 3
225 // bits, so when un is set, bc instruction may jump to an undesired
228 // More specifically, if we expect an unordered comparison and un is set, we
229 // expect to always go to true branch; in such case UEQ, UGT and ULT still
230 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
231 // since they are tested by inspecting !eq, !lt, !gt, respectively.
233 // Similarly, for ordered comparison, when un is set, we always expect the
234 // result to be false. In such case OGT, OLT and OEQ is good, since they are
235 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
236 // and ONE are tested through !lt, !gt and !eq, and these are true.
237 case CmpInst::FCMP_UEQ:
238 case CmpInst::FCMP_UGT:
239 case CmpInst::FCMP_ULT:
240 case CmpInst::FCMP_OGE:
241 case CmpInst::FCMP_OLE:
242 case CmpInst::FCMP_ONE:
244 return Optional<PPC::Predicate>();
246 case CmpInst::FCMP_OEQ:
247 case CmpInst::ICMP_EQ:
250 case CmpInst::FCMP_OGT:
251 case CmpInst::ICMP_UGT:
252 case CmpInst::ICMP_SGT:
255 case CmpInst::FCMP_UGE:
256 case CmpInst::ICMP_UGE:
257 case CmpInst::ICMP_SGE:
260 case CmpInst::FCMP_OLT:
261 case CmpInst::ICMP_ULT:
262 case CmpInst::ICMP_SLT:
265 case CmpInst::FCMP_ULE:
266 case CmpInst::ICMP_ULE:
267 case CmpInst::ICMP_SLE:
270 case CmpInst::FCMP_UNE:
271 case CmpInst::ICMP_NE:
274 case CmpInst::FCMP_ORD:
277 case CmpInst::FCMP_UNO:
282 // Determine whether the type Ty is simple enough to be handled by
283 // fast-isel, and return its equivalent machine type in VT.
284 // FIXME: Copied directly from ARM -- factor into base class?
285 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
286 EVT Evt = TLI.getValueType(DL, Ty, true);
288 // Only handle simple types.
289 if (Evt == MVT::Other || !Evt.isSimple()) return false;
290 VT = Evt.getSimpleVT();
292 // Handle all legal types, i.e. a register that will directly hold this
294 return TLI.isTypeLegal(VT);
297 // Determine whether the type Ty is simple enough to be handled by
298 // fast-isel as a load target, and return its equivalent machine type in VT.
299 bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
300 if (isTypeLegal(Ty, VT)) return true;
302 // If this is a type than can be sign or zero-extended to a basic operation
303 // go ahead and accept it now.
304 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
311 bool PPCFastISel::isValueAvailable(const Value *V) const {
312 if (!isa<Instruction>(V))
315 const auto *I = cast<Instruction>(V);
316 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
319 // Given a value Obj, create an Address object Addr that represents its
320 // address. Return false if we can't handle it.
321 bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
322 const User *U = nullptr;
323 unsigned Opcode = Instruction::UserOp1;
324 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
325 // Don't walk into other basic blocks unless the object is an alloca from
326 // another block, otherwise it may not have a virtual register assigned.
327 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
328 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
329 Opcode = I->getOpcode();
332 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
333 Opcode = C->getOpcode();
340 case Instruction::BitCast:
341 // Look through bitcasts.
342 return PPCComputeAddress(U->getOperand(0), Addr);
343 case Instruction::IntToPtr:
344 // Look past no-op inttoptrs.
345 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
346 TLI.getPointerTy(DL))
347 return PPCComputeAddress(U->getOperand(0), Addr);
349 case Instruction::PtrToInt:
350 // Look past no-op ptrtoints.
351 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
352 return PPCComputeAddress(U->getOperand(0), Addr);
354 case Instruction::GetElementPtr: {
355 Address SavedAddr = Addr;
356 long TmpOffset = Addr.Offset;
358 // Iterate through the GEP folding the constants into offsets where
360 gep_type_iterator GTI = gep_type_begin(U);
361 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
362 II != IE; ++II, ++GTI) {
363 const Value *Op = *II;
364 if (StructType *STy = GTI.getStructTypeOrNull()) {
365 const StructLayout *SL = DL.getStructLayout(STy);
366 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
367 TmpOffset += SL->getElementOffset(Idx);
369 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
371 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
372 // Constant-offset addressing.
373 TmpOffset += CI->getSExtValue() * S;
376 if (canFoldAddIntoGEP(U, Op)) {
377 // A compatible add with a constant operand. Fold the constant.
379 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
380 TmpOffset += CI->getSExtValue() * S;
381 // Iterate on the other operand.
382 Op = cast<AddOperator>(Op)->getOperand(0);
386 goto unsupported_gep;
391 // Try to grab the base operand now.
392 Addr.Offset = TmpOffset;
393 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
395 // We failed, restore everything and try the other options.
401 case Instruction::Alloca: {
402 const AllocaInst *AI = cast<AllocaInst>(Obj);
403 DenseMap<const AllocaInst*, int>::iterator SI =
404 FuncInfo.StaticAllocaMap.find(AI);
405 if (SI != FuncInfo.StaticAllocaMap.end()) {
406 Addr.BaseType = Address::FrameIndexBase;
407 Addr.Base.FI = SI->second;
414 // FIXME: References to parameters fall through to the behavior
415 // below. They should be able to reference a frame index since
416 // they are stored to the stack, so we can get "ld rx, offset(r1)"
417 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
418 // just contain the parameter. Try to handle this with a FI.
420 // Try to get this in a register if nothing else has worked.
421 if (Addr.Base.Reg == 0)
422 Addr.Base.Reg = getRegForValue(Obj);
424 // Prevent assignment of base register to X0, which is inappropriate
425 // for loads and stores alike.
426 if (Addr.Base.Reg != 0)
427 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
429 return Addr.Base.Reg != 0;
432 // Fix up some addresses that can't be used directly. For example, if
433 // an offset won't fit in an instruction field, we may need to move it
434 // into an index register.
435 void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
436 unsigned &IndexReg) {
438 // Check whether the offset fits in the instruction field.
439 if (!isInt<16>(Addr.Offset))
442 // If this is a stack pointer and the offset needs to be simplified then
443 // put the alloca address into a register, set the base type back to
444 // register and continue. This should almost never happen.
445 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
446 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
448 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
449 Addr.Base.Reg = ResultReg;
450 Addr.BaseType = Address::RegBase;
454 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
455 const ConstantInt *Offset =
456 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
457 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
458 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
462 // Emit a load instruction if possible, returning true if we succeeded,
463 // otherwise false. See commentary below for how the register class of
464 // the load is determined.
465 bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
466 const TargetRegisterClass *RC,
467 bool IsZExt, unsigned FP64LoadOpc) {
469 bool UseOffset = true;
470 bool HasSPE = PPCSubTarget->hasSPE();
472 // If ResultReg is given, it determines the register class of the load.
473 // Otherwise, RC is the register class to use. If the result of the
474 // load isn't anticipated in this block, both may be zero, in which
475 // case we must make a conservative guess. In particular, don't assign
476 // R0 or X0 to the result register, as the result may be used in a load,
477 // store, add-immediate, or isel that won't permit this. (Though
478 // perhaps the spill and reload of live-exit values would handle this?)
479 const TargetRegisterClass *UseRC =
480 (ResultReg ? MRI.getRegClass(ResultReg) :
482 (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
483 (VT == MVT::f32 ? (HasSPE ? &PPC::SPE4RCRegClass : &PPC::F4RCRegClass) :
484 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
485 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
487 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
489 switch (VT.SimpleTy) {
490 default: // e.g., vector types not handled
493 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
496 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
497 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
500 Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
501 : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
502 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
507 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
508 "64-bit load with 32-bit target??");
509 UseOffset = ((Addr.Offset & 3) == 0);
512 Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
519 // If necessary, materialize the offset into a register and use
520 // the indexed form. Also handle stack pointers with special needs.
521 unsigned IndexReg = 0;
522 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
524 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
526 bool IsVSSRC = isVSSRCRegClass(UseRC);
527 bool IsVSFRC = isVSFRCRegClass(UseRC);
528 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
529 bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
530 if ((Is32VSXLoad || Is64VSXLoad) &&
531 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
532 (Addr.Offset == 0)) {
537 ResultReg = createResultReg(UseRC);
539 // Note: If we still have a frame index here, we know the offset is
540 // in range, as otherwise PPCSimplifyAddress would have converted it
542 if (Addr.BaseType == Address::FrameIndexBase) {
543 // VSX only provides an indexed load.
544 if (Is32VSXLoad || Is64VSXLoad) return false;
546 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
547 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
549 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
550 MFI.getObjectAlignment(Addr.Base.FI));
552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
553 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
555 // Base reg with offset in range.
556 } else if (UseOffset) {
557 // VSX only provides an indexed load.
558 if (Is32VSXLoad || Is64VSXLoad) return false;
560 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
561 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
565 // Get the RR opcode corresponding to the RI one. FIXME: It would be
566 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
567 // is hard to get at.
569 default: llvm_unreachable("Unexpected opcode!");
570 case PPC::LBZ: Opc = PPC::LBZX; break;
571 case PPC::LBZ8: Opc = PPC::LBZX8; break;
572 case PPC::LHZ: Opc = PPC::LHZX; break;
573 case PPC::LHZ8: Opc = PPC::LHZX8; break;
574 case PPC::LHA: Opc = PPC::LHAX; break;
575 case PPC::LHA8: Opc = PPC::LHAX8; break;
576 case PPC::LWZ: Opc = PPC::LWZX; break;
577 case PPC::LWZ8: Opc = PPC::LWZX8; break;
578 case PPC::LWA: Opc = PPC::LWAX; break;
579 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
580 case PPC::LD: Opc = PPC::LDX; break;
581 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
582 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
583 case PPC::EVLDD: Opc = PPC::EVLDDX; break;
584 case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
587 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
590 // If we have an index register defined we use it in the store inst,
591 // otherwise we use X0 as base as it makes the vector instructions to
592 // use zero in the computation of the effective address regardless the
593 // content of the register.
595 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
597 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
603 // Attempt to fast-select a load instruction.
604 bool PPCFastISel::SelectLoad(const Instruction *I) {
605 // FIXME: No atomic loads are supported.
606 if (cast<LoadInst>(I)->isAtomic())
609 // Verify we have a legal type before going any further.
611 if (!isLoadTypeLegal(I->getType(), VT))
614 // See if we can handle this address.
616 if (!PPCComputeAddress(I->getOperand(0), Addr))
619 // Look at the currently assigned register for this instruction
620 // to determine the required register class. This is necessary
621 // to constrain RA from using R0/X0 when this is not legal.
622 unsigned AssignedReg = FuncInfo.ValueMap[I];
623 const TargetRegisterClass *RC =
624 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
626 unsigned ResultReg = 0;
627 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
628 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
630 updateValueMap(I, ResultReg);
634 // Emit a store instruction to store SrcReg at Addr.
635 bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
636 assert(SrcReg && "Nothing to store!");
638 bool UseOffset = true;
640 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
641 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
643 switch (VT.SimpleTy) {
644 default: // e.g., vector types not handled
647 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
650 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
653 assert(Is32BitInt && "Not GPRC for i32??");
658 UseOffset = ((Addr.Offset & 3) == 0);
661 Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
664 Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
668 // If necessary, materialize the offset into a register and use
669 // the indexed form. Also handle stack pointers with special needs.
670 unsigned IndexReg = 0;
671 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
673 // If this is a potential VSX store with an offset of 0, a VSX indexed store
675 bool IsVSSRC = isVSSRCRegClass(RC);
676 bool IsVSFRC = isVSFRCRegClass(RC);
677 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
678 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
679 if ((Is32VSXStore || Is64VSXStore) &&
680 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
681 (Addr.Offset == 0)) {
685 // Note: If we still have a frame index here, we know the offset is
686 // in range, as otherwise PPCSimplifyAddress would have converted it
688 if (Addr.BaseType == Address::FrameIndexBase) {
689 // VSX only provides an indexed store.
690 if (Is32VSXStore || Is64VSXStore) return false;
692 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
693 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
695 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
696 MFI.getObjectAlignment(Addr.Base.FI));
698 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
701 .addFrameIndex(Addr.Base.FI)
704 // Base reg with offset in range.
705 } else if (UseOffset) {
706 // VSX only provides an indexed store.
707 if (Is32VSXStore || Is64VSXStore)
710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
711 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
715 // Get the RR opcode corresponding to the RI one. FIXME: It would be
716 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
717 // is hard to get at.
719 default: llvm_unreachable("Unexpected opcode!");
720 case PPC::STB: Opc = PPC::STBX; break;
721 case PPC::STH : Opc = PPC::STHX; break;
722 case PPC::STW : Opc = PPC::STWX; break;
723 case PPC::STB8: Opc = PPC::STBX8; break;
724 case PPC::STH8: Opc = PPC::STHX8; break;
725 case PPC::STW8: Opc = PPC::STWX8; break;
726 case PPC::STD: Opc = PPC::STDX; break;
727 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
728 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
729 case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
730 case PPC::SPESTW: Opc = PPC::SPESTWX; break;
733 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
736 // If we have an index register defined we use it in the store inst,
737 // otherwise we use X0 as base as it makes the vector instructions to
738 // use zero in the computation of the effective address regardless the
739 // content of the register.
741 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
743 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
749 // Attempt to fast-select a store instruction.
750 bool PPCFastISel::SelectStore(const Instruction *I) {
751 Value *Op0 = I->getOperand(0);
754 // FIXME: No atomics loads are supported.
755 if (cast<StoreInst>(I)->isAtomic())
758 // Verify we have a legal type before going any further.
760 if (!isLoadTypeLegal(Op0->getType(), VT))
763 // Get the value to be stored into a register.
764 SrcReg = getRegForValue(Op0);
768 // See if we can handle this address.
770 if (!PPCComputeAddress(I->getOperand(1), Addr))
773 if (!PPCEmitStore(VT, SrcReg, Addr))
779 // Attempt to fast-select a branch instruction.
780 bool PPCFastISel::SelectBranch(const Instruction *I) {
781 const BranchInst *BI = cast<BranchInst>(I);
782 MachineBasicBlock *BrBB = FuncInfo.MBB;
783 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
784 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
786 // For now, just try the simplest case where it's fed by a compare.
787 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
788 if (isValueAvailable(CI)) {
789 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
793 PPC::Predicate PPCPred = OptPPCPred.getValue();
795 // Take advantage of fall-through opportunities.
796 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
798 PPCPred = PPC::InvertPredicate(PPCPred);
801 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
803 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
807 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
808 .addImm(PPCSubTarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
809 .addReg(CondReg).addMBB(TBB);
810 finishCondBranch(BI->getParent(), TBB, FBB);
813 } else if (const ConstantInt *CI =
814 dyn_cast<ConstantInt>(BI->getCondition())) {
815 uint64_t Imm = CI->getZExtValue();
816 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
817 fastEmitBranch(Target, DbgLoc);
821 // FIXME: ARM looks for a case where the block containing the compare
822 // has been split from the block containing the branch. If this happens,
823 // there is a vreg available containing the result of the compare. I'm
824 // not sure we can do much, as we've lost the predicate information with
825 // the compare instruction -- we have a 4-bit CR but don't know which bit
830 // Attempt to emit a compare of the two source values. Signed and unsigned
831 // comparisons are supported. Return false if we can't handle it.
832 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
833 bool IsZExt, unsigned DestReg,
834 const PPC::Predicate Pred) {
835 Type *Ty = SrcValue1->getType();
836 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
837 if (!SrcEVT.isSimple())
839 MVT SrcVT = SrcEVT.getSimpleVT();
841 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
844 // See if operand 2 is an immediate encodeable in the compare.
845 // FIXME: Operands are not in canonical order at -O0, so an immediate
846 // operand in position 1 is a lost opportunity for now. We are
847 // similar to ARM in this regard.
850 const bool HasSPE = PPCSubTarget->hasSPE();
852 // Only 16-bit integer constants can be represented in compares for
853 // PowerPC. Others will be materialized into a register.
854 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
855 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
856 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
857 const APInt &CIVal = ConstInt->getValue();
858 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
859 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
864 unsigned SrcReg1 = getRegForValue(SrcValue1);
868 unsigned SrcReg2 = 0;
870 SrcReg2 = getRegForValue(SrcValue2);
876 bool NeedsExt = false;
877 auto RC = MRI.getRegClass(SrcReg1);
878 switch (SrcVT.SimpleTy) {
879 default: return false;
883 default: return false;
885 CmpOpc = PPC::EFSCMPEQ;
888 CmpOpc = PPC::EFSCMPLT;
891 CmpOpc = PPC::EFSCMPGT;
895 CmpOpc = PPC::FCMPUS;
896 if (isVSSRCRegClass(RC)) {
897 unsigned TmpReg = createResultReg(&PPC::F4RCRegClass);
898 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
899 TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg1);
907 default: return false;
909 CmpOpc = PPC::EFDCMPEQ;
912 CmpOpc = PPC::EFDCMPLT;
915 CmpOpc = PPC::EFDCMPGT;
918 } else if (isVSFRCRegClass(RC)) {
919 CmpOpc = PPC::XSCMPUDP;
921 CmpOpc = PPC::FCMPUD;
931 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
933 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
937 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
939 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
944 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
945 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
950 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
951 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
959 .addReg(SrcReg1).addReg(SrcReg2);
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
962 .addReg(SrcReg1).addImm(Imm);
967 // Attempt to fast-select a floating-point extend instruction.
968 bool PPCFastISel::SelectFPExt(const Instruction *I) {
969 Value *Src = I->getOperand(0);
970 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
971 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
973 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
976 unsigned SrcReg = getRegForValue(Src);
980 // No code is generated for a FP extend.
981 updateValueMap(I, SrcReg);
985 // Attempt to fast-select a floating-point truncate instruction.
986 bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
987 Value *Src = I->getOperand(0);
988 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
989 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
991 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
994 unsigned SrcReg = getRegForValue(Src);
998 // Round the result to single precision.
1001 if (PPCSubTarget->hasSPE()) {
1002 DestReg = createResultReg(&PPC::SPE4RCRegClass);
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1004 TII.get(PPC::EFSCFD), DestReg)
1007 DestReg = createResultReg(&PPC::F4RCRegClass);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1009 TII.get(PPC::FRSP), DestReg)
1013 updateValueMap(I, DestReg);
1017 // Move an i32 or i64 value in a GPR to an f64 value in an FPR.
1018 // FIXME: When direct register moves are implemented (see PowerISA 2.07),
1019 // those should be used instead of moving via a stack slot when the
1020 // subtarget permits.
1021 // FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1022 // stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1023 // case to 8 bytes which produces tighter code but wastes stack space.
1024 unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1027 // If necessary, extend 32-bit int to 64-bit.
1028 if (SrcVT == MVT::i32) {
1029 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1030 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1035 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1037 Addr.BaseType = Address::FrameIndexBase;
1038 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1040 // Store the value from the GPR.
1041 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1044 // Load the integer value into an FPR. The kind of load used depends
1045 // on a number of conditions.
1046 unsigned LoadOpc = PPC::LFD;
1048 if (SrcVT == MVT::i32) {
1050 LoadOpc = PPC::LFIWZX;
1051 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
1052 } else if (PPCSubTarget->hasLFIWAX()) {
1053 LoadOpc = PPC::LFIWAX;
1054 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
1058 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1059 unsigned ResultReg = 0;
1060 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1066 // Attempt to fast-select an integer-to-floating-point conversion.
1067 // FIXME: Once fast-isel has better support for VSX, conversions using
1068 // direct moves should be implemented.
1069 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1071 Type *DstTy = I->getType();
1072 if (!isTypeLegal(DstTy, DstVT))
1075 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1078 Value *Src = I->getOperand(0);
1079 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
1080 if (!SrcEVT.isSimple())
1083 MVT SrcVT = SrcEVT.getSimpleVT();
1085 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1086 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1089 unsigned SrcReg = getRegForValue(Src);
1093 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1094 if (PPCSubTarget->hasSPE()) {
1096 if (DstVT == MVT::f32)
1097 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1099 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1101 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1102 // Generate the convert.
1103 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1105 updateValueMap(I, DestReg);
1109 // We can only lower an unsigned convert if we have the newer
1110 // floating-point conversion operations.
1111 if (!IsSigned && !PPCSubTarget->hasFPCVT())
1114 // FIXME: For now we require the newer floating-point conversion operations
1115 // (which are present only on P7 and A2 server models) when converting
1116 // to single-precision float. Otherwise we have to generate a lot of
1117 // fiddly code to avoid double rounding. If necessary, the fiddly code
1118 // can be found in PPCTargetLowering::LowerINT_TO_FP().
1119 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
1122 // Extend the input if necessary.
1123 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1124 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1125 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1131 // Move the integer value to an FPR.
1132 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1136 // Determine the opcode for the conversion.
1137 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1138 unsigned DestReg = createResultReg(RC);
1141 if (DstVT == MVT::f32)
1142 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1144 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1146 // Generate the convert.
1147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1150 updateValueMap(I, DestReg);
1154 // Move the floating-point value in SrcReg into an integer destination
1155 // register, and return the register (or zero if we can't handle it).
1156 // FIXME: When direct register moves are implemented (see PowerISA 2.07),
1157 // those should be used instead of moving via a stack slot when the
1158 // subtarget permits.
1159 unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1160 unsigned SrcReg, bool IsSigned) {
1161 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1162 // Note that if have STFIWX available, we could use a 4-byte stack
1163 // slot for i32, but this being fast-isel we'll just go with the
1164 // easiest code gen possible.
1166 Addr.BaseType = Address::FrameIndexBase;
1167 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1169 // Store the value from the FPR.
1170 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1173 // Reload it into a GPR. If we want an i32 on big endian, modify the
1174 // address to have a 4-byte offset so we load from the right place.
1176 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
1178 // Look at the currently assigned register for this instruction
1179 // to determine the required register class.
1180 unsigned AssignedReg = FuncInfo.ValueMap[I];
1181 const TargetRegisterClass *RC =
1182 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
1184 unsigned ResultReg = 0;
1185 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1191 // Attempt to fast-select a floating-point-to-integer conversion.
1192 // FIXME: Once fast-isel has better support for VSX, conversions using
1193 // direct moves should be implemented.
1194 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1196 Type *DstTy = I->getType();
1197 if (!isTypeLegal(DstTy, DstVT))
1200 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1203 // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
1204 if (DstVT == MVT::i64 && !IsSigned &&
1205 !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
1208 Value *Src = I->getOperand(0);
1209 Type *SrcTy = Src->getType();
1210 if (!isTypeLegal(SrcTy, SrcVT))
1213 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1216 unsigned SrcReg = getRegForValue(Src);
1220 // Convert f32 to f64 if necessary. This is just a meaningless copy
1221 // to get the register class right.
1222 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1223 if (InRC == &PPC::F4RCRegClass) {
1224 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
1225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1226 TII.get(TargetOpcode::COPY), TmpReg)
1231 // Determine the opcode for the conversion, which takes place
1232 // entirely within FPRs.
1236 if (PPCSubTarget->hasSPE()) {
1237 DestReg = createResultReg(&PPC::GPRCRegClass);
1239 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1241 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
1243 DestReg = createResultReg(&PPC::F8RCRegClass);
1244 if (DstVT == MVT::i32)
1248 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
1250 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1253 // Generate the convert.
1254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1257 // Now move the integer value from a float register to an integer register.
1258 unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
1259 PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1264 updateValueMap(I, IntReg);
1268 // Attempt to fast-select a binary integer operation that isn't already
1269 // handled automatically.
1270 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1271 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1273 // We can get here in the case when we have a binary operation on a non-legal
1274 // type and the target independent selector doesn't know how to handle it.
1275 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1278 // Look at the currently assigned register for this instruction
1279 // to determine the required register class. If there is no register,
1280 // make a conservative choice (don't assign R0).
1281 unsigned AssignedReg = FuncInfo.ValueMap[I];
1282 const TargetRegisterClass *RC =
1283 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1284 &PPC::GPRC_and_GPRC_NOR0RegClass);
1285 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1288 switch (ISDOpcode) {
1289 default: return false;
1291 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1294 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1297 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1301 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1302 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1303 if (SrcReg1 == 0) return false;
1305 // Handle case of small immediate operand.
1306 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1307 const APInt &CIVal = ConstInt->getValue();
1308 int Imm = (int)CIVal.getSExtValue();
1310 if (isInt<16>(Imm)) {
1313 llvm_unreachable("Missing case!");
1316 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1320 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1333 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1342 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1353 updateValueMap(I, ResultReg);
1360 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1361 if (SrcReg2 == 0) return false;
1363 // Reverse operands for subtract-from.
1364 if (ISDOpcode == ISD::SUB)
1365 std::swap(SrcReg1, SrcReg2);
1367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1368 .addReg(SrcReg1).addReg(SrcReg2);
1369 updateValueMap(I, ResultReg);
1373 // Handle arguments to a call that we're attempting to fast-select.
1374 // Return false if the arguments are too complex for us at the moment.
1375 bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1376 SmallVectorImpl<unsigned> &ArgRegs,
1377 SmallVectorImpl<MVT> &ArgVTs,
1378 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1379 SmallVectorImpl<unsigned> &RegArgs,
1383 SmallVector<CCValAssign, 16> ArgLocs;
1384 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
1386 // Reserve space for the linkage area on the stack.
1387 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
1388 CCInfo.AllocateStack(LinkageSize, 8);
1390 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1392 // Bail out if we can't handle any of the arguments.
1393 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1394 CCValAssign &VA = ArgLocs[I];
1395 MVT ArgVT = ArgVTs[VA.getValNo()];
1397 // Skip vector arguments for now, as well as long double and
1398 // uint128_t, and anything that isn't passed in a register.
1399 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
1400 !VA.isRegLoc() || VA.needsCustom())
1403 // Skip bit-converted arguments for now.
1404 if (VA.getLocInfo() == CCValAssign::BCvt)
1408 // Get a count of how many bytes are to be pushed onto the stack.
1409 NumBytes = CCInfo.getNextStackOffset();
1411 // The prolog code of the callee may store up to 8 GPR argument registers to
1412 // the stack, allowing va_start to index over them in memory if its varargs.
1413 // Because we cannot tell if this is needed on the caller side, we have to
1414 // conservatively assume that it is needed. As such, make sure we have at
1415 // least enough stack space for the caller to store the 8 GPRs.
1416 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
1417 NumBytes = std::max(NumBytes, LinkageSize + 64);
1419 // Issue CALLSEQ_START.
1420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1421 TII.get(TII.getCallFrameSetupOpcode()))
1422 .addImm(NumBytes).addImm(0);
1424 // Prepare to assign register arguments. Every argument uses up a
1425 // GPR protocol register even if it's passed in a floating-point
1426 // register (unless we're using the fast calling convention).
1427 unsigned NextGPR = PPC::X3;
1428 unsigned NextFPR = PPC::F1;
1430 // Process arguments.
1431 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1432 CCValAssign &VA = ArgLocs[I];
1433 unsigned Arg = ArgRegs[VA.getValNo()];
1434 MVT ArgVT = ArgVTs[VA.getValNo()];
1436 // Handle argument promotion and bitcasts.
1437 switch (VA.getLocInfo()) {
1439 llvm_unreachable("Unknown loc info!");
1440 case CCValAssign::Full:
1442 case CCValAssign::SExt: {
1443 MVT DestVT = VA.getLocVT();
1444 const TargetRegisterClass *RC =
1445 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1446 unsigned TmpReg = createResultReg(RC);
1447 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1448 llvm_unreachable("Failed to emit a sext!");
1453 case CCValAssign::AExt:
1454 case CCValAssign::ZExt: {
1455 MVT DestVT = VA.getLocVT();
1456 const TargetRegisterClass *RC =
1457 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1458 unsigned TmpReg = createResultReg(RC);
1459 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1460 llvm_unreachable("Failed to emit a zext!");
1465 case CCValAssign::BCvt: {
1466 // FIXME: Not yet handled.
1467 llvm_unreachable("Should have bailed before getting here!");
1472 // Copy this argument to the appropriate register.
1474 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1476 if (CC != CallingConv::Fast)
1481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1482 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
1483 RegArgs.push_back(ArgReg);
1489 // For a call that we've determined we can fast-select, finish the
1490 // call sequence and generate a copy to obtain the return value (if any).
1491 bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1492 CallingConv::ID CC = CLI.CallConv;
1494 // Issue CallSEQ_END.
1495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1496 TII.get(TII.getCallFrameDestroyOpcode()))
1497 .addImm(NumBytes).addImm(0);
1499 // Next, generate a copy to obtain the return value.
1500 // FIXME: No multi-register return values yet, though I don't foresee
1501 // any real difficulties there.
1502 if (RetVT != MVT::isVoid) {
1503 SmallVector<CCValAssign, 16> RVLocs;
1504 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1505 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1506 CCValAssign &VA = RVLocs[0];
1507 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1508 assert(VA.isRegLoc() && "Can only return in registers!");
1510 MVT DestVT = VA.getValVT();
1511 MVT CopyVT = DestVT;
1513 // Ints smaller than a register still arrive in a full 64-bit
1514 // register, so make sure we recognize this.
1515 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1518 unsigned SourcePhysReg = VA.getLocReg();
1519 unsigned ResultReg = 0;
1521 if (RetVT == CopyVT) {
1522 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1523 ResultReg = createResultReg(CpyRC);
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1526 TII.get(TargetOpcode::COPY), ResultReg)
1527 .addReg(SourcePhysReg);
1529 // If necessary, round the floating result to single precision.
1530 } else if (CopyVT == MVT::f64) {
1531 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
1532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
1533 ResultReg).addReg(SourcePhysReg);
1535 // If only the low half of a general register is needed, generate
1536 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1537 // used along the fast-isel path (not lowered), and downstream logic
1538 // also doesn't like a direct subreg copy on a physical reg.)
1539 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1540 ResultReg = createResultReg(&PPC::GPRCRegClass);
1541 // Convert physical register from G8RC to GPRC.
1542 SourcePhysReg -= PPC::X0 - PPC::R0;
1543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1544 TII.get(TargetOpcode::COPY), ResultReg)
1545 .addReg(SourcePhysReg);
1548 assert(ResultReg && "ResultReg unset!");
1549 CLI.InRegs.push_back(SourcePhysReg);
1550 CLI.ResultReg = ResultReg;
1551 CLI.NumResultRegs = 1;
1557 bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1558 CallingConv::ID CC = CLI.CallConv;
1559 bool IsTailCall = CLI.IsTailCall;
1560 bool IsVarArg = CLI.IsVarArg;
1561 const Value *Callee = CLI.Callee;
1562 const MCSymbol *Symbol = CLI.Symbol;
1564 if (!Callee && !Symbol)
1567 // Allow SelectionDAG isel to handle tail calls.
1571 // Let SDISel handle vararg functions.
1575 // Handle simple calls for now, with legal return types and
1576 // those that can be extended.
1577 Type *RetTy = CLI.RetTy;
1579 if (RetTy->isVoidTy())
1580 RetVT = MVT::isVoid;
1581 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1584 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1585 // We can't handle boolean returns when CR bits are in use.
1588 // FIXME: No multi-register return values yet.
1589 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1590 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1591 RetVT != MVT::f64) {
1592 SmallVector<CCValAssign, 16> RVLocs;
1593 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
1594 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1595 if (RVLocs.size() > 1)
1599 // Bail early if more than 8 arguments, as we only currently
1600 // handle arguments passed in registers.
1601 unsigned NumArgs = CLI.OutVals.size();
1605 // Set up the argument vectors.
1606 SmallVector<Value*, 8> Args;
1607 SmallVector<unsigned, 8> ArgRegs;
1608 SmallVector<MVT, 8> ArgVTs;
1609 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1611 Args.reserve(NumArgs);
1612 ArgRegs.reserve(NumArgs);
1613 ArgVTs.reserve(NumArgs);
1614 ArgFlags.reserve(NumArgs);
1616 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
1617 // Only handle easy calls for now. It would be reasonably easy
1618 // to handle <= 8-byte structures passed ByVal in registers, but we
1619 // have to ensure they are right-justified in the register.
1620 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1621 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
1624 Value *ArgValue = CLI.OutVals[i];
1625 Type *ArgTy = ArgValue->getType();
1627 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1630 if (ArgVT.isVector())
1633 unsigned Arg = getRegForValue(ArgValue);
1637 Args.push_back(ArgValue);
1638 ArgRegs.push_back(Arg);
1639 ArgVTs.push_back(ArgVT);
1640 ArgFlags.push_back(Flags);
1643 // Process the arguments.
1644 SmallVector<unsigned, 8> RegArgs;
1647 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1648 RegArgs, CC, NumBytes, IsVarArg))
1651 MachineInstrBuilder MIB;
1652 // FIXME: No handling for function pointers yet. This requires
1653 // implementing the function descriptor (OPD) setup.
1654 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1656 // patchpoints are a special case; they always dispatch to a pointer value.
1657 // However, we don't actually want to generate the indirect call sequence
1658 // here (that will be generated, as necessary, during asm printing), and
1659 // the call we generate here will be erased by FastISel::selectPatchpoint,
1660 // so don't try very hard...
1661 if (CLI.IsPatchPoint)
1662 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1666 // Build direct call with NOP for TOC restore.
1667 // FIXME: We can and should optimize away the NOP for local calls.
1668 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1669 TII.get(PPC::BL8_NOP));
1671 MIB.addGlobalAddress(GV);
1674 // Add implicit physical register uses to the call.
1675 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1676 MIB.addReg(RegArgs[II], RegState::Implicit);
1678 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1680 PPCFuncInfo->setUsesTOCBasePtr();
1681 MIB.addReg(PPC::X2, RegState::Implicit);
1683 // Add a register mask with the call-preserved registers. Proper
1684 // defs for return values will be added by setPhysRegsDeadExcept().
1685 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1689 // Finish off the call including any return values.
1690 return finishCall(RetVT, CLI, NumBytes);
1693 // Attempt to fast-select a return instruction.
1694 bool PPCFastISel::SelectRet(const Instruction *I) {
1696 if (!FuncInfo.CanLowerReturn)
1699 if (TLI.supportSplitCSR(FuncInfo.MF))
1702 const ReturnInst *Ret = cast<ReturnInst>(I);
1703 const Function &F = *I->getParent()->getParent();
1705 // Build a list of return value registers.
1706 SmallVector<unsigned, 4> RetRegs;
1707 CallingConv::ID CC = F.getCallingConv();
1709 if (Ret->getNumOperands() > 0) {
1710 SmallVector<ISD::OutputArg, 4> Outs;
1711 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1713 // Analyze operands of the call, assigning locations to each operand.
1714 SmallVector<CCValAssign, 16> ValLocs;
1715 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
1716 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1717 const Value *RV = Ret->getOperand(0);
1719 // FIXME: Only one output register for now.
1720 if (ValLocs.size() > 1)
1723 // Special case for returning a constant integer of any size - materialize
1724 // the constant as an i64 and copy it to the return register.
1725 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
1726 CCValAssign &VA = ValLocs[0];
1728 unsigned RetReg = VA.getLocReg();
1729 // We still need to worry about properly extending the sign. For example,
1730 // we could have only a single bit or a constant that needs zero
1731 // extension rather than sign extension. Make sure we pass the return
1732 // value extension property to integer materialization.
1734 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
1736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1737 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1739 RetRegs.push_back(RetReg);
1742 unsigned Reg = getRegForValue(RV);
1747 // Copy the result values into the output registers.
1748 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1750 CCValAssign &VA = ValLocs[i];
1751 assert(VA.isRegLoc() && "Can only return in registers!");
1752 RetRegs.push_back(VA.getLocReg());
1753 unsigned SrcReg = Reg + VA.getValNo();
1755 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1756 if (!RVEVT.isSimple())
1758 MVT RVVT = RVEVT.getSimpleVT();
1759 MVT DestVT = VA.getLocVT();
1761 if (RVVT != DestVT && RVVT != MVT::i8 &&
1762 RVVT != MVT::i16 && RVVT != MVT::i32)
1765 if (RVVT != DestVT) {
1766 switch (VA.getLocInfo()) {
1768 llvm_unreachable("Unknown loc info!");
1769 case CCValAssign::Full:
1770 llvm_unreachable("Full value assign but types don't match?");
1771 case CCValAssign::AExt:
1772 case CCValAssign::ZExt: {
1773 const TargetRegisterClass *RC =
1774 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1775 unsigned TmpReg = createResultReg(RC);
1776 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1781 case CCValAssign::SExt: {
1782 const TargetRegisterClass *RC =
1783 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1784 unsigned TmpReg = createResultReg(RC);
1785 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1794 TII.get(TargetOpcode::COPY), RetRegs[i])
1800 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1801 TII.get(PPC::BLR8));
1803 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1804 MIB.addReg(RetRegs[i], RegState::Implicit);
1809 // Attempt to emit an integer extend of SrcReg into DestReg. Both
1810 // signed and zero extensions are supported. Return false if we
1812 bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1813 unsigned DestReg, bool IsZExt) {
1814 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1816 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1819 // Signed extensions use EXTSB, EXTSH, EXTSW.
1822 if (SrcVT == MVT::i8)
1823 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1824 else if (SrcVT == MVT::i16)
1825 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1827 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1828 Opc = PPC::EXTSW_32_64;
1830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1833 // Unsigned 32-bit extensions use RLWINM.
1834 } else if (DestVT == MVT::i32) {
1836 if (SrcVT == MVT::i8)
1839 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
1844 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1846 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1849 if (SrcVT == MVT::i8)
1851 else if (SrcVT == MVT::i16)
1855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1856 TII.get(PPC::RLDICL_32_64), DestReg)
1857 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1863 // Attempt to fast-select an indirect branch instruction.
1864 bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1865 unsigned AddrReg = getRegForValue(I->getOperand(0));
1869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
1871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
1873 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1874 for (const BasicBlock *SuccBB : IB->successors())
1875 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
1880 // Attempt to fast-select an integer truncate instruction.
1881 bool PPCFastISel::SelectTrunc(const Instruction *I) {
1882 Value *Src = I->getOperand(0);
1883 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1884 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1886 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1889 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1892 unsigned SrcReg = getRegForValue(Src);
1896 // The only interesting case is when we need to switch register classes.
1897 if (SrcVT == MVT::i64) {
1898 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
1899 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1900 TII.get(TargetOpcode::COPY),
1901 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1905 updateValueMap(I, SrcReg);
1909 // Attempt to fast-select an integer extend instruction.
1910 bool PPCFastISel::SelectIntExt(const Instruction *I) {
1911 Type *DestTy = I->getType();
1912 Value *Src = I->getOperand(0);
1913 Type *SrcTy = Src->getType();
1915 bool IsZExt = isa<ZExtInst>(I);
1916 unsigned SrcReg = getRegForValue(Src);
1917 if (!SrcReg) return false;
1919 EVT SrcEVT, DestEVT;
1920 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1921 DestEVT = TLI.getValueType(DL, DestTy, true);
1922 if (!SrcEVT.isSimple())
1924 if (!DestEVT.isSimple())
1927 MVT SrcVT = SrcEVT.getSimpleVT();
1928 MVT DestVT = DestEVT.getSimpleVT();
1930 // If we know the register class needed for the result of this
1931 // instruction, use it. Otherwise pick the register class of the
1932 // correct size that does not contain X0/R0, since we don't know
1933 // whether downstream uses permit that assignment.
1934 unsigned AssignedReg = FuncInfo.ValueMap[I];
1935 const TargetRegisterClass *RC =
1936 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1937 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1938 &PPC::GPRC_and_GPRC_NOR0RegClass));
1939 unsigned ResultReg = createResultReg(RC);
1941 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1944 updateValueMap(I, ResultReg);
1948 // Attempt to fast-select an instruction that wasn't handled by
1949 // the table-generated machinery.
1950 bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
1952 switch (I->getOpcode()) {
1953 case Instruction::Load:
1954 return SelectLoad(I);
1955 case Instruction::Store:
1956 return SelectStore(I);
1957 case Instruction::Br:
1958 return SelectBranch(I);
1959 case Instruction::IndirectBr:
1960 return SelectIndirectBr(I);
1961 case Instruction::FPExt:
1962 return SelectFPExt(I);
1963 case Instruction::FPTrunc:
1964 return SelectFPTrunc(I);
1965 case Instruction::SIToFP:
1966 return SelectIToFP(I, /*IsSigned*/ true);
1967 case Instruction::UIToFP:
1968 return SelectIToFP(I, /*IsSigned*/ false);
1969 case Instruction::FPToSI:
1970 return SelectFPToI(I, /*IsSigned*/ true);
1971 case Instruction::FPToUI:
1972 return SelectFPToI(I, /*IsSigned*/ false);
1973 case Instruction::Add:
1974 return SelectBinaryIntOp(I, ISD::ADD);
1975 case Instruction::Or:
1976 return SelectBinaryIntOp(I, ISD::OR);
1977 case Instruction::Sub:
1978 return SelectBinaryIntOp(I, ISD::SUB);
1979 case Instruction::Call:
1980 return selectCall(I);
1981 case Instruction::Ret:
1982 return SelectRet(I);
1983 case Instruction::Trunc:
1984 return SelectTrunc(I);
1985 case Instruction::ZExt:
1986 case Instruction::SExt:
1987 return SelectIntExt(I);
1988 // Here add other flavors of Instruction::XXX that automated
1989 // cases don't catch. For example, switches are terminators
1990 // that aren't yet handled.
1997 // Materialize a floating-point constant into a register, and return
1998 // the register number (or zero if we failed to handle it).
1999 unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
2000 // No plans to handle long double here.
2001 if (VT != MVT::f32 && VT != MVT::f64)
2004 // All FP constants are loaded from the constant pool.
2005 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
2006 assert(Align > 0 && "Unexpectedly missing alignment information!");
2007 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
2008 const bool HasSPE = PPCSubTarget->hasSPE();
2009 const TargetRegisterClass *RC;
2011 RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
2013 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2015 unsigned DestReg = createResultReg(RC);
2016 CodeModel::Model CModel = TM.getCodeModel();
2018 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2019 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2020 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
2025 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2027 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2029 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2031 PPCFuncInfo->setUsesTOCBasePtr();
2032 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
2033 if (CModel == CodeModel::Small) {
2034 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
2036 .addConstantPoolIndex(Idx).addReg(PPC::X2);
2037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2038 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2040 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
2041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
2042 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
2043 // But for large code model, we must generate a LDtocL followed
2045 if (CModel == CodeModel::Large) {
2046 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2048 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
2049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2053 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2054 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2056 .addMemOperand(MMO);
2062 // Materialize the address of a global value into a register, and return
2063 // the register number (or zero if we failed to handle it).
2064 unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2065 assert(VT == MVT::i64 && "Non-address!");
2066 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2067 unsigned DestReg = createResultReg(RC);
2069 // Global values may be plain old object addresses, TLS object
2070 // addresses, constant pool entries, or jump tables. How we generate
2071 // code for these may depend on small, medium, or large code model.
2072 CodeModel::Model CModel = TM.getCodeModel();
2074 // FIXME: Jump tables are not yet required because fast-isel doesn't
2075 // handle switches; if that changes, we need them as well. For now,
2076 // what follows assumes everything's a generic (or TLS) global address.
2078 // FIXME: We don't yet handle the complexity of TLS.
2079 if (GV->isThreadLocal())
2082 PPCFuncInfo->setUsesTOCBasePtr();
2083 // For small code model, generate a simple TOC load.
2084 if (CModel == CodeModel::Small)
2085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2087 .addGlobalAddress(GV)
2090 // If the address is an externally defined symbol, a symbol with common
2091 // or externally available linkage, a non-local function address, or a
2092 // jump table address (not yet needed), or if we are generating code
2093 // for large code model, we generate:
2094 // LDtocL(GV, ADDIStocHA(%x2, GV))
2095 // Otherwise we generate:
2096 // ADDItocL(ADDIStocHA(%x2, GV), GV)
2097 // Either way, start with the ADDIStocHA:
2098 unsigned HighPartReg = createResultReg(RC);
2099 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
2100 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2102 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2103 if (GVFlags & PPCII::MO_NLP_FLAG) {
2104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2105 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
2107 // Otherwise generate the ADDItocL.
2108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
2109 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
2116 // Materialize a 32-bit integer constant into a register, and return
2117 // the register number (or zero if we failed to handle it).
2118 unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2119 const TargetRegisterClass *RC) {
2120 unsigned Lo = Imm & 0xFFFF;
2121 unsigned Hi = (Imm >> 16) & 0xFFFF;
2123 unsigned ResultReg = createResultReg(RC);
2124 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2128 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2131 // Both Lo and Hi have nonzero bits.
2132 unsigned TmpReg = createResultReg(RC);
2133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2134 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2137 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2138 .addReg(TmpReg).addImm(Lo);
2141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2142 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2148 // Materialize a 64-bit integer constant into a register, and return
2149 // the register number (or zero if we failed to handle it).
2150 unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2151 const TargetRegisterClass *RC) {
2152 unsigned Remainder = 0;
2155 // If the value doesn't fit in 32 bits, see if we can shift it
2156 // so that it fits in 32 bits.
2157 if (!isInt<32>(Imm)) {
2158 Shift = countTrailingZeros<uint64_t>(Imm);
2159 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2161 if (isInt<32>(ImmSh))
2170 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2171 // (if not shifted).
2172 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2176 // If upper 32 bits were not zero, we've built them and need to shift
2180 TmpReg2 = createResultReg(RC);
2181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
2182 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2186 unsigned TmpReg3, Hi, Lo;
2187 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2188 TmpReg3 = createResultReg(RC);
2189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
2190 TmpReg3).addReg(TmpReg2).addImm(Hi);
2194 if ((Lo = Remainder & 0xFFFF)) {
2195 unsigned ResultReg = createResultReg(RC);
2196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
2197 ResultReg).addReg(TmpReg3).addImm(Lo);
2204 // Materialize an integer constant into a register, and return
2205 // the register number (or zero if we failed to handle it).
2206 unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2208 // If we're using CR bit registers for i1 values, handle that as a special
2210 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
2211 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2213 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2217 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2221 const TargetRegisterClass *RC =
2222 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
2223 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
2225 // If the constant is in range, use a load-immediate.
2226 // Since LI will sign extend the constant we need to make sure that for
2227 // our zeroext constants that the sign extended constant fits into 16-bits -
2228 // a range of 0..0x7fff.
2229 if (isInt<16>(Imm)) {
2230 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2231 unsigned ImmReg = createResultReg(RC);
2232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2237 // Construct the constant piecewise.
2239 return PPCMaterialize64BitInt(Imm, RC);
2240 else if (VT == MVT::i32)
2241 return PPCMaterialize32BitInt(Imm, RC);
2246 // Materialize a constant into a register, and return the register
2247 // number (or zero if we failed to handle it).
2248 unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
2249 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
2251 // Only handle simple types.
2252 if (!CEVT.isSimple()) return 0;
2253 MVT VT = CEVT.getSimpleVT();
2255 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2256 return PPCMaterializeFP(CFP, VT);
2257 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2258 return PPCMaterializeGV(GV, VT);
2259 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2260 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2261 // assumes that constant PHI operands will be zero extended, and failure to
2262 // match that assumption will cause problems if we sign extend here but
2263 // some user of a PHI is in a block for which we fall back to full SDAG
2264 // instruction selection.
2265 return PPCMaterializeInt(CI, VT, false);
2270 // Materialize the address created by an alloca into a register, and
2271 // return the register number (or zero if we failed to handle it).
2272 unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
2273 // Don't handle dynamic allocas.
2274 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2277 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2279 DenseMap<const AllocaInst*, int>::iterator SI =
2280 FuncInfo.StaticAllocaMap.find(AI);
2282 if (SI != FuncInfo.StaticAllocaMap.end()) {
2283 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
2285 ResultReg).addFrameIndex(SI->second).addImm(0);
2292 // Fold loads into extends when possible.
2293 // FIXME: We can have multiple redundant extend/trunc instructions
2294 // following a load. The folding only picks up one. Extend this
2295 // to check subsequent instructions for the same pattern and remove
2296 // them. Thus ResultReg should be the def reg for the last redundant
2297 // instruction in a chain, and all intervening instructions can be
2298 // removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2299 // to add ELF64-NOT: rldicl to the appropriate tests when this works.
2300 bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2301 const LoadInst *LI) {
2302 // Verify we have a legal type before going any further.
2304 if (!isLoadTypeLegal(LI->getType(), VT))
2307 // Combine load followed by zero- or sign-extend.
2308 bool IsZExt = false;
2309 switch(MI->getOpcode()) {
2314 case PPC::RLDICL_32_64: {
2316 unsigned MB = MI->getOperand(3).getImm();
2317 if ((VT == MVT::i8 && MB <= 56) ||
2318 (VT == MVT::i16 && MB <= 48) ||
2319 (VT == MVT::i32 && MB <= 32))
2325 case PPC::RLWINM8: {
2327 unsigned MB = MI->getOperand(3).getImm();
2328 if ((VT == MVT::i8 && MB <= 24) ||
2329 (VT == MVT::i16 && MB <= 16))
2336 case PPC::EXTSB8_32_64:
2337 /* There is no sign-extending load-byte instruction. */
2342 case PPC::EXTSH8_32_64: {
2343 if (VT != MVT::i16 && VT != MVT::i8)
2350 case PPC::EXTSW_32_64: {
2351 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2357 // See if we can handle this address.
2359 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2362 unsigned ResultReg = MI->getOperand(0).getReg();
2364 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2365 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
2368 MachineBasicBlock::iterator I(MI);
2369 removeDeadCode(I, std::next(I));
2373 // Attempt to lower call arguments in a faster way than done by
2374 // the selection DAG code.
2375 bool PPCFastISel::fastLowerArguments() {
2376 // Defer to normal argument lowering for now. It's reasonably
2377 // efficient. Consider doing something like ARM to handle the
2378 // case where all args fit in registers, no varargs, no float
2383 // Handle materializing integer constants into a register. This is not
2384 // automatically generated for PowerPC, so must be explicitly created here.
2385 unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2387 if (Opc != ISD::Constant)
2390 // If we're using CR bit registers for i1 values, handle that as a special
2392 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
2393 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2395 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2399 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2403 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2404 &PPC::GPRCRegClass);
2406 return PPCMaterialize64BitInt(Imm, RC);
2408 return PPCMaterialize32BitInt(Imm, RC);
2411 // Override for ADDI and ADDI8 to set the correct register class
2412 // on RHS operand 0. The automatic infrastructure naively assumes
2413 // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2414 // for these cases. At the moment, none of the other automatically
2415 // generated RI instructions require special treatment. However, once
2416 // SelectSelect is implemented, "isel" requires similar handling.
2418 // Also be conservative about the output register class. Avoid
2419 // assigning R0 or X0 to the output register for GPRC and G8RC
2420 // register classes, as any such result could be used in ADDI, etc.,
2421 // where those regs have another meaning.
2422 unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
2423 const TargetRegisterClass *RC,
2424 unsigned Op0, bool Op0IsKill,
2426 if (MachineInstOpcode == PPC::ADDI)
2427 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2428 else if (MachineInstOpcode == PPC::ADDI8)
2429 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2431 const TargetRegisterClass *UseRC =
2432 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2433 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2435 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
2436 Op0, Op0IsKill, Imm);
2439 // Override for instructions with one register operand to avoid use of
2440 // R0/X0. The automatic infrastructure isn't aware of the context so
2441 // we must be conservative.
2442 unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
2443 const TargetRegisterClass* RC,
2444 unsigned Op0, bool Op0IsKill) {
2445 const TargetRegisterClass *UseRC =
2446 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2447 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2449 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2452 // Override for instructions with two register operands to avoid use
2453 // of R0/X0. The automatic infrastructure isn't aware of the context
2454 // so we must be conservative.
2455 unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2456 const TargetRegisterClass* RC,
2457 unsigned Op0, bool Op0IsKill,
2458 unsigned Op1, bool Op1IsKill) {
2459 const TargetRegisterClass *UseRC =
2460 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2461 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2463 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2468 // Create the fast instruction selector for PowerPC64 ELF.
2469 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2470 const TargetLibraryInfo *LibInfo) {
2471 // Only available on 64-bit ELF for now.
2472 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
2473 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
2474 return new PPCFastISel(FuncInfo, LibInfo);