1 //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "MCTargetDesc/PPCPredicates.h"
17 #include "PPC.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"
41
42 //===----------------------------------------------------------------------===//
43 //
44 // TBD:
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.
59 //
60 //===----------------------------------------------------------------------===//
61 using namespace llvm;
62
63 #define DEBUG_TYPE "ppcfastisel"
64
65 namespace {
66
67 typedef struct Address {
68 enum {
69 RegBase,
70 FrameIndexBase
71 } BaseType;
72
73 union {
74 unsigned Reg;
75 int FI;
76 } Base;
77
78 long Offset;
79
80 // Innocuous defaults for our address.
Address__anon08f32e980111::Address81 Address()
82 : BaseType(RegBase), Offset(0) {
83 Base.Reg = 0;
84 }
85 } Address;
86
87 class PPCFastISel final : public FastISel {
88
89 const TargetMachine &TM;
90 const PPCSubtarget *PPCSubTarget;
91 PPCFunctionInfo *PPCFuncInfo;
92 const TargetInstrInfo &TII;
93 const TargetLowering &TLI;
94 LLVMContext *Context;
95
96 public:
PPCFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)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()) {}
105
106 // Backend specific FastISel code.
107 private:
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,
118 uint64_t Imm);
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);
126
127 bool fastLowerCall(CallLoweringInfo &CLI) override;
128
129 // Instruction selection routines.
130 private:
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);
143
144 // Utility routines.
145 private:
146 bool isTypeLegal(Type *Ty, MVT &VT);
147 bool isLoadTypeLegal(Type *Ty, MVT &VT);
148 bool isValueAvailable(const Value *V) const;
isVSFRCRegClass(const TargetRegisterClass * RC) const149 bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
150 return RC->getID() == PPC::VSFRCRegClassID;
151 }
isVSSRCRegClass(const TargetRegisterClass * RC) const152 bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
153 return RC->getID() == PPC::VSSRCRegClassID;
154 }
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,
164 unsigned &IndexReg);
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);
178
179 // Call handling routines.
180 private:
181 bool processCallArgs(SmallVectorImpl<Value*> &Args,
182 SmallVectorImpl<unsigned> &ArgRegs,
183 SmallVectorImpl<MVT> &ArgVTs,
184 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
185 SmallVectorImpl<unsigned> &RegArgs,
186 CallingConv::ID CC,
187 unsigned &NumBytes,
188 bool IsVarArg);
189 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
190 LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
191
192 private:
193 #include "PPCGenFastISel.inc"
194
195 };
196
197 } // end anonymous namespace
198
199 #include "PPCGenCallingConv.inc"
200
201 // Function whose sole purpose is to kill compiler warnings
202 // stemming from unused functions included from PPCGenCallingConv.inc.
usePPC32CCs(unsigned Flag)203 CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
204 if (Flag == 1)
205 return CC_PPC32_SVR4;
206 else if (Flag == 2)
207 return CC_PPC32_SVR4_ByVal;
208 else if (Flag == 3)
209 return CC_PPC32_SVR4_VarArg;
210 else if (Flag == 4)
211 return RetCC_PPC_Cold;
212 else
213 return RetCC_PPC;
214 }
215
getComparePred(CmpInst::Predicate Pred)216 static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
217 switch (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
226 // place.
227 //
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.
232 //
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:
243 default:
244 return Optional<PPC::Predicate>();
245
246 case CmpInst::FCMP_OEQ:
247 case CmpInst::ICMP_EQ:
248 return PPC::PRED_EQ;
249
250 case CmpInst::FCMP_OGT:
251 case CmpInst::ICMP_UGT:
252 case CmpInst::ICMP_SGT:
253 return PPC::PRED_GT;
254
255 case CmpInst::FCMP_UGE:
256 case CmpInst::ICMP_UGE:
257 case CmpInst::ICMP_SGE:
258 return PPC::PRED_GE;
259
260 case CmpInst::FCMP_OLT:
261 case CmpInst::ICMP_ULT:
262 case CmpInst::ICMP_SLT:
263 return PPC::PRED_LT;
264
265 case CmpInst::FCMP_ULE:
266 case CmpInst::ICMP_ULE:
267 case CmpInst::ICMP_SLE:
268 return PPC::PRED_LE;
269
270 case CmpInst::FCMP_UNE:
271 case CmpInst::ICMP_NE:
272 return PPC::PRED_NE;
273
274 case CmpInst::FCMP_ORD:
275 return PPC::PRED_NU;
276
277 case CmpInst::FCMP_UNO:
278 return PPC::PRED_UN;
279 }
280 }
281
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?
isTypeLegal(Type * Ty,MVT & VT)285 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
286 EVT Evt = TLI.getValueType(DL, Ty, true);
287
288 // Only handle simple types.
289 if (Evt == MVT::Other || !Evt.isSimple()) return false;
290 VT = Evt.getSimpleVT();
291
292 // Handle all legal types, i.e. a register that will directly hold this
293 // value.
294 return TLI.isTypeLegal(VT);
295 }
296
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.
isLoadTypeLegal(Type * Ty,MVT & VT)299 bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
300 if (isTypeLegal(Ty, VT)) return true;
301
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) {
305 return true;
306 }
307
308 return false;
309 }
310
isValueAvailable(const Value * V) const311 bool PPCFastISel::isValueAvailable(const Value *V) const {
312 if (!isa<Instruction>(V))
313 return true;
314
315 const auto *I = cast<Instruction>(V);
316 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
317 }
318
319 // Given a value Obj, create an Address object Addr that represents its
320 // address. Return false if we can't handle it.
PPCComputeAddress(const Value * Obj,Address & Addr)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();
330 U = I;
331 }
332 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
333 Opcode = C->getOpcode();
334 U = C;
335 }
336
337 switch (Opcode) {
338 default:
339 break;
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);
348 break;
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);
353 break;
354 case Instruction::GetElementPtr: {
355 Address SavedAddr = Addr;
356 long TmpOffset = Addr.Offset;
357
358 // Iterate through the GEP folding the constants into offsets where
359 // we can.
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);
368 } else {
369 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
370 for (;;) {
371 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
372 // Constant-offset addressing.
373 TmpOffset += CI->getSExtValue() * S;
374 break;
375 }
376 if (canFoldAddIntoGEP(U, Op)) {
377 // A compatible add with a constant operand. Fold the constant.
378 ConstantInt *CI =
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);
383 continue;
384 }
385 // Unsupported
386 goto unsupported_gep;
387 }
388 }
389 }
390
391 // Try to grab the base operand now.
392 Addr.Offset = TmpOffset;
393 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
394
395 // We failed, restore everything and try the other options.
396 Addr = SavedAddr;
397
398 unsupported_gep:
399 break;
400 }
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;
408 return true;
409 }
410 break;
411 }
412 }
413
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.
419
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);
423
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);
428
429 return Addr.Base.Reg != 0;
430 }
431
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.
PPCSimplifyAddress(Address & Addr,bool & UseOffset,unsigned & IndexReg)435 void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
436 unsigned &IndexReg) {
437
438 // Check whether the offset fits in the instruction field.
439 if (!isInt<16>(Addr.Offset))
440 UseOffset = false;
441
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;
451 }
452
453 if (!UseOffset) {
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!");
459 }
460 }
461
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.
PPCEmitLoad(MVT VT,unsigned & ResultReg,Address & Addr,const TargetRegisterClass * RC,bool IsZExt,unsigned FP64LoadOpc)465 bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
466 const TargetRegisterClass *RC,
467 bool IsZExt, unsigned FP64LoadOpc) {
468 unsigned Opc;
469 bool UseOffset = true;
470 bool HasSPE = PPCSubTarget->hasSPE();
471
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) :
481 (RC ? RC :
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)))));
486
487 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
488
489 switch (VT.SimpleTy) {
490 default: // e.g., vector types not handled
491 return false;
492 case MVT::i8:
493 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
494 break;
495 case MVT::i16:
496 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
497 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
498 break;
499 case MVT::i32:
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))
503 UseOffset = false;
504 break;
505 case MVT::i64:
506 Opc = PPC::LD;
507 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
508 "64-bit load with 32-bit target??");
509 UseOffset = ((Addr.Offset & 3) == 0);
510 break;
511 case MVT::f32:
512 Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
513 break;
514 case MVT::f64:
515 Opc = FP64LoadOpc;
516 break;
517 }
518
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);
523
524 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
525 // be used.
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)) {
533 UseOffset = false;
534 }
535
536 if (ResultReg == 0)
537 ResultReg = createResultReg(UseRC);
538
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
541 // into a RegBase.
542 if (Addr.BaseType == Address::FrameIndexBase) {
543 // VSX only provides an indexed load.
544 if (Is32VSXLoad || Is64VSXLoad) return false;
545
546 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
547 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
548 Addr.Offset),
549 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
550 MFI.getObjectAlignment(Addr.Base.FI));
551
552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
553 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
554
555 // Base reg with offset in range.
556 } else if (UseOffset) {
557 // VSX only provides an indexed load.
558 if (Is32VSXLoad || Is64VSXLoad) return false;
559
560 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
561 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
562
563 // Indexed form.
564 } else {
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.
568 switch (Opc) {
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;
585 }
586
587 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
588 ResultReg);
589
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.
594 if (IndexReg)
595 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
596 else
597 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
598 }
599
600 return true;
601 }
602
603 // Attempt to fast-select a load instruction.
SelectLoad(const Instruction * I)604 bool PPCFastISel::SelectLoad(const Instruction *I) {
605 // FIXME: No atomic loads are supported.
606 if (cast<LoadInst>(I)->isAtomic())
607 return false;
608
609 // Verify we have a legal type before going any further.
610 MVT VT;
611 if (!isLoadTypeLegal(I->getType(), VT))
612 return false;
613
614 // See if we can handle this address.
615 Address Addr;
616 if (!PPCComputeAddress(I->getOperand(0), Addr))
617 return false;
618
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;
625
626 unsigned ResultReg = 0;
627 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
628 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
629 return false;
630 updateValueMap(I, ResultReg);
631 return true;
632 }
633
634 // Emit a store instruction to store SrcReg at Addr.
PPCEmitStore(MVT VT,unsigned SrcReg,Address & Addr)635 bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
636 assert(SrcReg && "Nothing to store!");
637 unsigned Opc;
638 bool UseOffset = true;
639
640 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
641 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
642
643 switch (VT.SimpleTy) {
644 default: // e.g., vector types not handled
645 return false;
646 case MVT::i8:
647 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
648 break;
649 case MVT::i16:
650 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
651 break;
652 case MVT::i32:
653 assert(Is32BitInt && "Not GPRC for i32??");
654 Opc = PPC::STW;
655 break;
656 case MVT::i64:
657 Opc = PPC::STD;
658 UseOffset = ((Addr.Offset & 3) == 0);
659 break;
660 case MVT::f32:
661 Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
662 break;
663 case MVT::f64:
664 Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
665 break;
666 }
667
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);
672
673 // If this is a potential VSX store with an offset of 0, a VSX indexed store
674 // can be used.
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)) {
682 UseOffset = false;
683 }
684
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
687 // into a RegBase.
688 if (Addr.BaseType == Address::FrameIndexBase) {
689 // VSX only provides an indexed store.
690 if (Is32VSXStore || Is64VSXStore) return false;
691
692 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
693 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
694 Addr.Offset),
695 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
696 MFI.getObjectAlignment(Addr.Base.FI));
697
698 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
699 .addReg(SrcReg)
700 .addImm(Addr.Offset)
701 .addFrameIndex(Addr.Base.FI)
702 .addMemOperand(MMO);
703
704 // Base reg with offset in range.
705 } else if (UseOffset) {
706 // VSX only provides an indexed store.
707 if (Is32VSXStore || Is64VSXStore)
708 return false;
709
710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
711 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
712
713 // Indexed form.
714 } else {
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.
718 switch (Opc) {
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;
731 }
732
733 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
734 .addReg(SrcReg);
735
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.
740 if (IndexReg)
741 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
742 else
743 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
744 }
745
746 return true;
747 }
748
749 // Attempt to fast-select a store instruction.
SelectStore(const Instruction * I)750 bool PPCFastISel::SelectStore(const Instruction *I) {
751 Value *Op0 = I->getOperand(0);
752 unsigned SrcReg = 0;
753
754 // FIXME: No atomics loads are supported.
755 if (cast<StoreInst>(I)->isAtomic())
756 return false;
757
758 // Verify we have a legal type before going any further.
759 MVT VT;
760 if (!isLoadTypeLegal(Op0->getType(), VT))
761 return false;
762
763 // Get the value to be stored into a register.
764 SrcReg = getRegForValue(Op0);
765 if (SrcReg == 0)
766 return false;
767
768 // See if we can handle this address.
769 Address Addr;
770 if (!PPCComputeAddress(I->getOperand(1), Addr))
771 return false;
772
773 if (!PPCEmitStore(VT, SrcReg, Addr))
774 return false;
775
776 return true;
777 }
778
779 // Attempt to fast-select a branch instruction.
SelectBranch(const Instruction * I)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)];
785
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());
790 if (!OptPPCPred)
791 return false;
792
793 PPC::Predicate PPCPred = OptPPCPred.getValue();
794
795 // Take advantage of fall-through opportunities.
796 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
797 std::swap(TBB, FBB);
798 PPCPred = PPC::InvertPredicate(PPCPred);
799 }
800
801 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
802
803 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
804 CondReg, PPCPred))
805 return false;
806
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);
811 return true;
812 }
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);
818 return true;
819 }
820
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
826 // to test here.
827 return false;
828 }
829
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.
PPCEmitCmp(const Value * SrcValue1,const Value * SrcValue2,bool IsZExt,unsigned DestReg,const PPC::Predicate Pred)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())
838 return false;
839 MVT SrcVT = SrcEVT.getSimpleVT();
840
841 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
842 return false;
843
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.
848 long Imm = 0;
849 bool UseImm = false;
850 const bool HasSPE = PPCSubTarget->hasSPE();
851
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)))
860 UseImm = true;
861 }
862 }
863
864 unsigned SrcReg1 = getRegForValue(SrcValue1);
865 if (SrcReg1 == 0)
866 return false;
867
868 unsigned SrcReg2 = 0;
869 if (!UseImm) {
870 SrcReg2 = getRegForValue(SrcValue2);
871 if (SrcReg2 == 0)
872 return false;
873 }
874
875 unsigned CmpOpc;
876 bool NeedsExt = false;
877 auto RC = MRI.getRegClass(SrcReg1);
878 switch (SrcVT.SimpleTy) {
879 default: return false;
880 case MVT::f32:
881 if (HasSPE) {
882 switch (Pred) {
883 default: return false;
884 case PPC::PRED_EQ:
885 CmpOpc = PPC::EFSCMPEQ;
886 break;
887 case PPC::PRED_LT:
888 CmpOpc = PPC::EFSCMPLT;
889 break;
890 case PPC::PRED_GT:
891 CmpOpc = PPC::EFSCMPGT;
892 break;
893 }
894 } else {
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);
900 SrcReg1 = TmpReg;
901 }
902 }
903 break;
904 case MVT::f64:
905 if (HasSPE) {
906 switch (Pred) {
907 default: return false;
908 case PPC::PRED_EQ:
909 CmpOpc = PPC::EFDCMPEQ;
910 break;
911 case PPC::PRED_LT:
912 CmpOpc = PPC::EFDCMPLT;
913 break;
914 case PPC::PRED_GT:
915 CmpOpc = PPC::EFDCMPGT;
916 break;
917 }
918 } else if (isVSFRCRegClass(RC)) {
919 CmpOpc = PPC::XSCMPUDP;
920 } else {
921 CmpOpc = PPC::FCMPUD;
922 }
923 break;
924 case MVT::i1:
925 case MVT::i8:
926 case MVT::i16:
927 NeedsExt = true;
928 LLVM_FALLTHROUGH;
929 case MVT::i32:
930 if (!UseImm)
931 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
932 else
933 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
934 break;
935 case MVT::i64:
936 if (!UseImm)
937 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
938 else
939 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
940 break;
941 }
942
943 if (NeedsExt) {
944 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
945 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
946 return false;
947 SrcReg1 = ExtReg;
948
949 if (!UseImm) {
950 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
951 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
952 return false;
953 SrcReg2 = ExtReg;
954 }
955 }
956
957 if (!UseImm)
958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
959 .addReg(SrcReg1).addReg(SrcReg2);
960 else
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
962 .addReg(SrcReg1).addImm(Imm);
963
964 return true;
965 }
966
967 // Attempt to fast-select a floating-point extend instruction.
SelectFPExt(const Instruction * I)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);
972
973 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
974 return false;
975
976 unsigned SrcReg = getRegForValue(Src);
977 if (!SrcReg)
978 return false;
979
980 // No code is generated for a FP extend.
981 updateValueMap(I, SrcReg);
982 return true;
983 }
984
985 // Attempt to fast-select a floating-point truncate instruction.
SelectFPTrunc(const Instruction * I)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);
990
991 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
992 return false;
993
994 unsigned SrcReg = getRegForValue(Src);
995 if (!SrcReg)
996 return false;
997
998 // Round the result to single precision.
999 unsigned DestReg;
1000
1001 if (PPCSubTarget->hasSPE()) {
1002 DestReg = createResultReg(&PPC::SPE4RCRegClass);
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1004 TII.get(PPC::EFSCFD), DestReg)
1005 .addReg(SrcReg);
1006 } else {
1007 DestReg = createResultReg(&PPC::F4RCRegClass);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1009 TII.get(PPC::FRSP), DestReg)
1010 .addReg(SrcReg);
1011 }
1012
1013 updateValueMap(I, DestReg);
1014 return true;
1015 }
1016
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.
PPCMoveToFPReg(MVT SrcVT,unsigned SrcReg,bool IsSigned)1024 unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1025 bool IsSigned) {
1026
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))
1031 return 0;
1032 SrcReg = TmpReg;
1033 }
1034
1035 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1036 Address Addr;
1037 Addr.BaseType = Address::FrameIndexBase;
1038 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1039
1040 // Store the value from the GPR.
1041 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1042 return 0;
1043
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;
1047
1048 if (SrcVT == MVT::i32) {
1049 if (!IsSigned) {
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;
1055 }
1056 }
1057
1058 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1059 unsigned ResultReg = 0;
1060 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1061 return 0;
1062
1063 return ResultReg;
1064 }
1065
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.
SelectIToFP(const Instruction * I,bool IsSigned)1069 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1070 MVT DstVT;
1071 Type *DstTy = I->getType();
1072 if (!isTypeLegal(DstTy, DstVT))
1073 return false;
1074
1075 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1076 return false;
1077
1078 Value *Src = I->getOperand(0);
1079 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
1080 if (!SrcEVT.isSimple())
1081 return false;
1082
1083 MVT SrcVT = SrcEVT.getSimpleVT();
1084
1085 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1086 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1087 return false;
1088
1089 unsigned SrcReg = getRegForValue(Src);
1090 if (SrcReg == 0)
1091 return false;
1092
1093 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1094 if (PPCSubTarget->hasSPE()) {
1095 unsigned Opc;
1096 if (DstVT == MVT::f32)
1097 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1098 else
1099 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1100
1101 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1102 // Generate the convert.
1103 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1104 .addReg(SrcReg);
1105 updateValueMap(I, DestReg);
1106 return true;
1107 }
1108
1109 // We can only lower an unsigned convert if we have the newer
1110 // floating-point conversion operations.
1111 if (!IsSigned && !PPCSubTarget->hasFPCVT())
1112 return false;
1113
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())
1120 return false;
1121
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))
1126 return false;
1127 SrcVT = MVT::i64;
1128 SrcReg = TmpReg;
1129 }
1130
1131 // Move the integer value to an FPR.
1132 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1133 if (FPReg == 0)
1134 return false;
1135
1136 // Determine the opcode for the conversion.
1137 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1138 unsigned DestReg = createResultReg(RC);
1139 unsigned Opc;
1140
1141 if (DstVT == MVT::f32)
1142 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1143 else
1144 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1145
1146 // Generate the convert.
1147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1148 .addReg(FPReg);
1149
1150 updateValueMap(I, DestReg);
1151 return true;
1152 }
1153
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.
PPCMoveToIntReg(const Instruction * I,MVT VT,unsigned SrcReg,bool IsSigned)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.
1165 Address Addr;
1166 Addr.BaseType = Address::FrameIndexBase;
1167 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1168
1169 // Store the value from the FPR.
1170 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1171 return 0;
1172
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.
1175 if (VT == MVT::i32)
1176 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
1177
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;
1183
1184 unsigned ResultReg = 0;
1185 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1186 return 0;
1187
1188 return ResultReg;
1189 }
1190
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.
SelectFPToI(const Instruction * I,bool IsSigned)1194 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1195 MVT DstVT, SrcVT;
1196 Type *DstTy = I->getType();
1197 if (!isTypeLegal(DstTy, DstVT))
1198 return false;
1199
1200 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1201 return false;
1202
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())
1206 return false;
1207
1208 Value *Src = I->getOperand(0);
1209 Type *SrcTy = Src->getType();
1210 if (!isTypeLegal(SrcTy, SrcVT))
1211 return false;
1212
1213 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1214 return false;
1215
1216 unsigned SrcReg = getRegForValue(Src);
1217 if (SrcReg == 0)
1218 return false;
1219
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)
1227 .addReg(SrcReg);
1228 SrcReg = TmpReg;
1229 }
1230
1231 // Determine the opcode for the conversion, which takes place
1232 // entirely within FPRs.
1233 unsigned DestReg;
1234 unsigned Opc;
1235
1236 if (PPCSubTarget->hasSPE()) {
1237 DestReg = createResultReg(&PPC::GPRCRegClass);
1238 if (IsSigned)
1239 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1240 else
1241 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
1242 } else {
1243 DestReg = createResultReg(&PPC::F8RCRegClass);
1244 if (DstVT == MVT::i32)
1245 if (IsSigned)
1246 Opc = PPC::FCTIWZ;
1247 else
1248 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
1249 else
1250 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1251 }
1252
1253 // Generate the convert.
1254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1255 .addReg(SrcReg);
1256
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);
1260
1261 if (IntReg == 0)
1262 return false;
1263
1264 updateValueMap(I, IntReg);
1265 return true;
1266 }
1267
1268 // Attempt to fast-select a binary integer operation that isn't already
1269 // handled automatically.
SelectBinaryIntOp(const Instruction * I,unsigned ISDOpcode)1270 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1271 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1272
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)
1276 return false;
1277
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);
1286
1287 unsigned Opc;
1288 switch (ISDOpcode) {
1289 default: return false;
1290 case ISD::ADD:
1291 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1292 break;
1293 case ISD::OR:
1294 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1295 break;
1296 case ISD::SUB:
1297 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1298 break;
1299 }
1300
1301 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1302 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1303 if (SrcReg1 == 0) return false;
1304
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();
1309 bool UseImm = true;
1310 if (isInt<16>(Imm)) {
1311 switch (Opc) {
1312 default:
1313 llvm_unreachable("Missing case!");
1314 case PPC::ADD4:
1315 Opc = PPC::ADDI;
1316 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1317 break;
1318 case PPC::ADD8:
1319 Opc = PPC::ADDI8;
1320 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1321 break;
1322 case PPC::OR:
1323 Opc = PPC::ORI;
1324 break;
1325 case PPC::OR8:
1326 Opc = PPC::ORI8;
1327 break;
1328 case PPC::SUBF:
1329 if (Imm == -32768)
1330 UseImm = false;
1331 else {
1332 Opc = PPC::ADDI;
1333 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1334 Imm = -Imm;
1335 }
1336 break;
1337 case PPC::SUBF8:
1338 if (Imm == -32768)
1339 UseImm = false;
1340 else {
1341 Opc = PPC::ADDI8;
1342 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1343 Imm = -Imm;
1344 }
1345 break;
1346 }
1347
1348 if (UseImm) {
1349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1350 ResultReg)
1351 .addReg(SrcReg1)
1352 .addImm(Imm);
1353 updateValueMap(I, ResultReg);
1354 return true;
1355 }
1356 }
1357 }
1358
1359 // Reg-reg case.
1360 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1361 if (SrcReg2 == 0) return false;
1362
1363 // Reverse operands for subtract-from.
1364 if (ISDOpcode == ISD::SUB)
1365 std::swap(SrcReg1, SrcReg2);
1366
1367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1368 .addReg(SrcReg1).addReg(SrcReg2);
1369 updateValueMap(I, ResultReg);
1370 return true;
1371 }
1372
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.
processCallArgs(SmallVectorImpl<Value * > & Args,SmallVectorImpl<unsigned> & ArgRegs,SmallVectorImpl<MVT> & ArgVTs,SmallVectorImpl<ISD::ArgFlagsTy> & ArgFlags,SmallVectorImpl<unsigned> & RegArgs,CallingConv::ID CC,unsigned & NumBytes,bool IsVarArg)1375 bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1376 SmallVectorImpl<unsigned> &ArgRegs,
1377 SmallVectorImpl<MVT> &ArgVTs,
1378 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1379 SmallVectorImpl<unsigned> &RegArgs,
1380 CallingConv::ID CC,
1381 unsigned &NumBytes,
1382 bool IsVarArg) {
1383 SmallVector<CCValAssign, 16> ArgLocs;
1384 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
1385
1386 // Reserve space for the linkage area on the stack.
1387 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
1388 CCInfo.AllocateStack(LinkageSize, 8);
1389
1390 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1391
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()];
1396
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())
1401 return false;
1402
1403 // Skip bit-converted arguments for now.
1404 if (VA.getLocInfo() == CCValAssign::BCvt)
1405 return false;
1406 }
1407
1408 // Get a count of how many bytes are to be pushed onto the stack.
1409 NumBytes = CCInfo.getNextStackOffset();
1410
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);
1418
1419 // Issue CALLSEQ_START.
1420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1421 TII.get(TII.getCallFrameSetupOpcode()))
1422 .addImm(NumBytes).addImm(0);
1423
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;
1429
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()];
1435
1436 // Handle argument promotion and bitcasts.
1437 switch (VA.getLocInfo()) {
1438 default:
1439 llvm_unreachable("Unknown loc info!");
1440 case CCValAssign::Full:
1441 break;
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!");
1449 ArgVT = DestVT;
1450 Arg = TmpReg;
1451 break;
1452 }
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!");
1461 ArgVT = DestVT;
1462 Arg = TmpReg;
1463 break;
1464 }
1465 case CCValAssign::BCvt: {
1466 // FIXME: Not yet handled.
1467 llvm_unreachable("Should have bailed before getting here!");
1468 break;
1469 }
1470 }
1471
1472 // Copy this argument to the appropriate register.
1473 unsigned ArgReg;
1474 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1475 ArgReg = NextFPR++;
1476 if (CC != CallingConv::Fast)
1477 ++NextGPR;
1478 } else
1479 ArgReg = NextGPR++;
1480
1481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1482 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
1483 RegArgs.push_back(ArgReg);
1484 }
1485
1486 return true;
1487 }
1488
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).
finishCall(MVT RetVT,CallLoweringInfo & CLI,unsigned & NumBytes)1491 bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1492 CallingConv::ID CC = CLI.CallConv;
1493
1494 // Issue CallSEQ_END.
1495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1496 TII.get(TII.getCallFrameDestroyOpcode()))
1497 .addImm(NumBytes).addImm(0);
1498
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!");
1509
1510 MVT DestVT = VA.getValVT();
1511 MVT CopyVT = DestVT;
1512
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)
1516 CopyVT = MVT::i64;
1517
1518 unsigned SourcePhysReg = VA.getLocReg();
1519 unsigned ResultReg = 0;
1520
1521 if (RetVT == CopyVT) {
1522 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1523 ResultReg = createResultReg(CpyRC);
1524
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1526 TII.get(TargetOpcode::COPY), ResultReg)
1527 .addReg(SourcePhysReg);
1528
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);
1534
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);
1546 }
1547
1548 assert(ResultReg && "ResultReg unset!");
1549 CLI.InRegs.push_back(SourcePhysReg);
1550 CLI.ResultReg = ResultReg;
1551 CLI.NumResultRegs = 1;
1552 }
1553
1554 return true;
1555 }
1556
fastLowerCall(CallLoweringInfo & CLI)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;
1563
1564 if (!Callee && !Symbol)
1565 return false;
1566
1567 // Allow SelectionDAG isel to handle tail calls.
1568 if (IsTailCall)
1569 return false;
1570
1571 // Let SDISel handle vararg functions.
1572 if (IsVarArg)
1573 return false;
1574
1575 // Handle simple calls for now, with legal return types and
1576 // those that can be extended.
1577 Type *RetTy = CLI.RetTy;
1578 MVT RetVT;
1579 if (RetTy->isVoidTy())
1580 RetVT = MVT::isVoid;
1581 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1582 RetVT != MVT::i8)
1583 return false;
1584 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1585 // We can't handle boolean returns when CR bits are in use.
1586 return false;
1587
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)
1596 return false;
1597 }
1598
1599 // Bail early if more than 8 arguments, as we only currently
1600 // handle arguments passed in registers.
1601 unsigned NumArgs = CLI.OutVals.size();
1602 if (NumArgs > 8)
1603 return false;
1604
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;
1610
1611 Args.reserve(NumArgs);
1612 ArgRegs.reserve(NumArgs);
1613 ArgVTs.reserve(NumArgs);
1614 ArgFlags.reserve(NumArgs);
1615
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())
1622 return false;
1623
1624 Value *ArgValue = CLI.OutVals[i];
1625 Type *ArgTy = ArgValue->getType();
1626 MVT ArgVT;
1627 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1628 return false;
1629
1630 if (ArgVT.isVector())
1631 return false;
1632
1633 unsigned Arg = getRegForValue(ArgValue);
1634 if (Arg == 0)
1635 return false;
1636
1637 Args.push_back(ArgValue);
1638 ArgRegs.push_back(Arg);
1639 ArgVTs.push_back(ArgVT);
1640 ArgFlags.push_back(Flags);
1641 }
1642
1643 // Process the arguments.
1644 SmallVector<unsigned, 8> RegArgs;
1645 unsigned NumBytes;
1646
1647 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1648 RegArgs, CC, NumBytes, IsVarArg))
1649 return false;
1650
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);
1655 if (!GV) {
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));
1663 else
1664 return false;
1665 } else {
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));
1670 // Add callee.
1671 MIB.addGlobalAddress(GV);
1672 }
1673
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);
1677
1678 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1679 // into the call.
1680 PPCFuncInfo->setUsesTOCBasePtr();
1681 MIB.addReg(PPC::X2, RegState::Implicit);
1682
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));
1686
1687 CLI.Call = MIB;
1688
1689 // Finish off the call including any return values.
1690 return finishCall(RetVT, CLI, NumBytes);
1691 }
1692
1693 // Attempt to fast-select a return instruction.
SelectRet(const Instruction * I)1694 bool PPCFastISel::SelectRet(const Instruction *I) {
1695
1696 if (!FuncInfo.CanLowerReturn)
1697 return false;
1698
1699 if (TLI.supportSplitCSR(FuncInfo.MF))
1700 return false;
1701
1702 const ReturnInst *Ret = cast<ReturnInst>(I);
1703 const Function &F = *I->getParent()->getParent();
1704
1705 // Build a list of return value registers.
1706 SmallVector<unsigned, 4> RetRegs;
1707 CallingConv::ID CC = F.getCallingConv();
1708
1709 if (Ret->getNumOperands() > 0) {
1710 SmallVector<ISD::OutputArg, 4> Outs;
1711 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1712
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);
1718
1719 // FIXME: Only one output register for now.
1720 if (ValLocs.size() > 1)
1721 return false;
1722
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];
1727
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.
1733 unsigned SrcReg =
1734 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
1735
1736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1737 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1738
1739 RetRegs.push_back(RetReg);
1740
1741 } else {
1742 unsigned Reg = getRegForValue(RV);
1743
1744 if (Reg == 0)
1745 return false;
1746
1747 // Copy the result values into the output registers.
1748 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1749
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();
1754
1755 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1756 if (!RVEVT.isSimple())
1757 return false;
1758 MVT RVVT = RVEVT.getSimpleVT();
1759 MVT DestVT = VA.getLocVT();
1760
1761 if (RVVT != DestVT && RVVT != MVT::i8 &&
1762 RVVT != MVT::i16 && RVVT != MVT::i32)
1763 return false;
1764
1765 if (RVVT != DestVT) {
1766 switch (VA.getLocInfo()) {
1767 default:
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))
1777 return false;
1778 SrcReg = TmpReg;
1779 break;
1780 }
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))
1786 return false;
1787 SrcReg = TmpReg;
1788 break;
1789 }
1790 }
1791 }
1792
1793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1794 TII.get(TargetOpcode::COPY), RetRegs[i])
1795 .addReg(SrcReg);
1796 }
1797 }
1798 }
1799
1800 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1801 TII.get(PPC::BLR8));
1802
1803 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1804 MIB.addReg(RetRegs[i], RegState::Implicit);
1805
1806 return true;
1807 }
1808
1809 // Attempt to emit an integer extend of SrcReg into DestReg. Both
1810 // signed and zero extensions are supported. Return false if we
1811 // can't handle it.
PPCEmitIntExt(MVT SrcVT,unsigned SrcReg,MVT DestVT,unsigned DestReg,bool IsZExt)1812 bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1813 unsigned DestReg, bool IsZExt) {
1814 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1815 return false;
1816 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1817 return false;
1818
1819 // Signed extensions use EXTSB, EXTSH, EXTSW.
1820 if (!IsZExt) {
1821 unsigned Opc;
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;
1826 else {
1827 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1828 Opc = PPC::EXTSW_32_64;
1829 }
1830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1831 .addReg(SrcReg);
1832
1833 // Unsigned 32-bit extensions use RLWINM.
1834 } else if (DestVT == MVT::i32) {
1835 unsigned MB;
1836 if (SrcVT == MVT::i8)
1837 MB = 24;
1838 else {
1839 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1840 MB = 16;
1841 }
1842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
1843 DestReg)
1844 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1845
1846 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1847 } else {
1848 unsigned MB;
1849 if (SrcVT == MVT::i8)
1850 MB = 56;
1851 else if (SrcVT == MVT::i16)
1852 MB = 48;
1853 else
1854 MB = 32;
1855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1856 TII.get(PPC::RLDICL_32_64), DestReg)
1857 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1858 }
1859
1860 return true;
1861 }
1862
1863 // Attempt to fast-select an indirect branch instruction.
SelectIndirectBr(const Instruction * I)1864 bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1865 unsigned AddrReg = getRegForValue(I->getOperand(0));
1866 if (AddrReg == 0)
1867 return false;
1868
1869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
1870 .addReg(AddrReg);
1871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
1872
1873 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1874 for (const BasicBlock *SuccBB : IB->successors())
1875 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
1876
1877 return true;
1878 }
1879
1880 // Attempt to fast-select an integer truncate instruction.
SelectTrunc(const Instruction * I)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);
1885
1886 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1887 return false;
1888
1889 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1890 return false;
1891
1892 unsigned SrcReg = getRegForValue(Src);
1893 if (!SrcReg)
1894 return false;
1895
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);
1902 SrcReg = ResultReg;
1903 }
1904
1905 updateValueMap(I, SrcReg);
1906 return true;
1907 }
1908
1909 // Attempt to fast-select an integer extend instruction.
SelectIntExt(const Instruction * I)1910 bool PPCFastISel::SelectIntExt(const Instruction *I) {
1911 Type *DestTy = I->getType();
1912 Value *Src = I->getOperand(0);
1913 Type *SrcTy = Src->getType();
1914
1915 bool IsZExt = isa<ZExtInst>(I);
1916 unsigned SrcReg = getRegForValue(Src);
1917 if (!SrcReg) return false;
1918
1919 EVT SrcEVT, DestEVT;
1920 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1921 DestEVT = TLI.getValueType(DL, DestTy, true);
1922 if (!SrcEVT.isSimple())
1923 return false;
1924 if (!DestEVT.isSimple())
1925 return false;
1926
1927 MVT SrcVT = SrcEVT.getSimpleVT();
1928 MVT DestVT = DestEVT.getSimpleVT();
1929
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);
1940
1941 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1942 return false;
1943
1944 updateValueMap(I, ResultReg);
1945 return true;
1946 }
1947
1948 // Attempt to fast-select an instruction that wasn't handled by
1949 // the table-generated machinery.
fastSelectInstruction(const Instruction * I)1950 bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
1951
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.
1991 default:
1992 break;
1993 }
1994 return false;
1995 }
1996
1997 // Materialize a floating-point constant into a register, and return
1998 // the register number (or zero if we failed to handle it).
PPCMaterializeFP(const ConstantFP * CFP,MVT VT)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)
2002 return 0;
2003
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;
2010 if (HasSPE)
2011 RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
2012 else
2013 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2014
2015 unsigned DestReg = createResultReg(RC);
2016 CodeModel::Model CModel = TM.getCodeModel();
2017
2018 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2019 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2020 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
2021
2022 unsigned Opc;
2023
2024 if (HasSPE)
2025 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2026 else
2027 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2028
2029 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2030
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),
2035 TmpReg)
2036 .addConstantPoolIndex(Idx).addReg(PPC::X2);
2037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2038 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2039 } else {
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
2044 // by the LF[SD].
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)
2050 .addImm(0)
2051 .addReg(TmpReg2);
2052 } else
2053 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2054 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2055 .addReg(TmpReg)
2056 .addMemOperand(MMO);
2057 }
2058
2059 return DestReg;
2060 }
2061
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).
PPCMaterializeGV(const GlobalValue * GV,MVT VT)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);
2068
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();
2073
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.
2077
2078 // FIXME: We don't yet handle the complexity of TLS.
2079 if (GV->isThreadLocal())
2080 return 0;
2081
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),
2086 DestReg)
2087 .addGlobalAddress(GV)
2088 .addReg(PPC::X2);
2089 else {
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);
2101
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);
2106 } else {
2107 // Otherwise generate the ADDItocL.
2108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
2109 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
2110 }
2111 }
2112
2113 return DestReg;
2114 }
2115
2116 // Materialize a 32-bit integer constant into a register, and return
2117 // the register number (or zero if we failed to handle it).
PPCMaterialize32BitInt(int64_t Imm,const TargetRegisterClass * RC)2118 unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2119 const TargetRegisterClass *RC) {
2120 unsigned Lo = Imm & 0xFFFF;
2121 unsigned Hi = (Imm >> 16) & 0xFFFF;
2122
2123 unsigned ResultReg = createResultReg(RC);
2124 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2125
2126 if (isInt<16>(Imm))
2127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2128 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2129 .addImm(Imm);
2130 else if (Lo) {
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)
2135 .addImm(Hi);
2136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2137 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2138 .addReg(TmpReg).addImm(Lo);
2139 } else
2140 // Just Hi bits.
2141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2142 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2143 .addImm(Hi);
2144
2145 return ResultReg;
2146 }
2147
2148 // Materialize a 64-bit integer constant into a register, and return
2149 // the register number (or zero if we failed to handle it).
PPCMaterialize64BitInt(int64_t Imm,const TargetRegisterClass * RC)2150 unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2151 const TargetRegisterClass *RC) {
2152 unsigned Remainder = 0;
2153 unsigned Shift = 0;
2154
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;
2160
2161 if (isInt<32>(ImmSh))
2162 Imm = ImmSh;
2163 else {
2164 Remainder = Imm;
2165 Shift = 32;
2166 Imm >>= 32;
2167 }
2168 }
2169
2170 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2171 // (if not shifted).
2172 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2173 if (!Shift)
2174 return TmpReg1;
2175
2176 // If upper 32 bits were not zero, we've built them and need to shift
2177 // them into place.
2178 unsigned TmpReg2;
2179 if (Imm) {
2180 TmpReg2 = createResultReg(RC);
2181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
2182 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2183 } else
2184 TmpReg2 = TmpReg1;
2185
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);
2191 } else
2192 TmpReg3 = TmpReg2;
2193
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);
2198 return ResultReg;
2199 }
2200
2201 return TmpReg3;
2202 }
2203
2204 // Materialize an integer constant into a register, and return
2205 // the register number (or zero if we failed to handle it).
PPCMaterializeInt(const ConstantInt * CI,MVT VT,bool UseSExt)2206 unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2207 bool UseSExt) {
2208 // If we're using CR bit registers for i1 values, handle that as a special
2209 // case first.
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);
2214 return ImmReg;
2215 }
2216
2217 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2218 VT != MVT::i1)
2219 return 0;
2220
2221 const TargetRegisterClass *RC =
2222 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
2223 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
2224
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)
2233 .addImm(Imm);
2234 return ImmReg;
2235 }
2236
2237 // Construct the constant piecewise.
2238 if (VT == MVT::i64)
2239 return PPCMaterialize64BitInt(Imm, RC);
2240 else if (VT == MVT::i32)
2241 return PPCMaterialize32BitInt(Imm, RC);
2242
2243 return 0;
2244 }
2245
2246 // Materialize a constant into a register, and return the register
2247 // number (or zero if we failed to handle it).
fastMaterializeConstant(const Constant * C)2248 unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
2249 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
2250
2251 // Only handle simple types.
2252 if (!CEVT.isSimple()) return 0;
2253 MVT VT = CEVT.getSimpleVT();
2254
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);
2266
2267 return 0;
2268 }
2269
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).
fastMaterializeAlloca(const AllocaInst * AI)2272 unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
2273 // Don't handle dynamic allocas.
2274 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2275
2276 MVT VT;
2277 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2278
2279 DenseMap<const AllocaInst*, int>::iterator SI =
2280 FuncInfo.StaticAllocaMap.find(AI);
2281
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);
2286 return ResultReg;
2287 }
2288
2289 return 0;
2290 }
2291
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.
tryToFoldLoadIntoMI(MachineInstr * MI,unsigned OpNo,const LoadInst * LI)2300 bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2301 const LoadInst *LI) {
2302 // Verify we have a legal type before going any further.
2303 MVT VT;
2304 if (!isLoadTypeLegal(LI->getType(), VT))
2305 return false;
2306
2307 // Combine load followed by zero- or sign-extend.
2308 bool IsZExt = false;
2309 switch(MI->getOpcode()) {
2310 default:
2311 return false;
2312
2313 case PPC::RLDICL:
2314 case PPC::RLDICL_32_64: {
2315 IsZExt = true;
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))
2320 break;
2321 return false;
2322 }
2323
2324 case PPC::RLWINM:
2325 case PPC::RLWINM8: {
2326 IsZExt = true;
2327 unsigned MB = MI->getOperand(3).getImm();
2328 if ((VT == MVT::i8 && MB <= 24) ||
2329 (VT == MVT::i16 && MB <= 16))
2330 break;
2331 return false;
2332 }
2333
2334 case PPC::EXTSB:
2335 case PPC::EXTSB8:
2336 case PPC::EXTSB8_32_64:
2337 /* There is no sign-extending load-byte instruction. */
2338 return false;
2339
2340 case PPC::EXTSH:
2341 case PPC::EXTSH8:
2342 case PPC::EXTSH8_32_64: {
2343 if (VT != MVT::i16 && VT != MVT::i8)
2344 return false;
2345 break;
2346 }
2347
2348 case PPC::EXTSW:
2349 case PPC::EXTSW_32:
2350 case PPC::EXTSW_32_64: {
2351 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2352 return false;
2353 break;
2354 }
2355 }
2356
2357 // See if we can handle this address.
2358 Address Addr;
2359 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2360 return false;
2361
2362 unsigned ResultReg = MI->getOperand(0).getReg();
2363
2364 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2365 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
2366 return false;
2367
2368 MachineBasicBlock::iterator I(MI);
2369 removeDeadCode(I, std::next(I));
2370 return true;
2371 }
2372
2373 // Attempt to lower call arguments in a faster way than done by
2374 // the selection DAG code.
fastLowerArguments()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
2379 // or vector args.
2380 return false;
2381 }
2382
2383 // Handle materializing integer constants into a register. This is not
2384 // automatically generated for PowerPC, so must be explicitly created here.
fastEmit_i(MVT Ty,MVT VT,unsigned Opc,uint64_t Imm)2385 unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2386
2387 if (Opc != ISD::Constant)
2388 return 0;
2389
2390 // If we're using CR bit registers for i1 values, handle that as a special
2391 // case first.
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);
2396 return ImmReg;
2397 }
2398
2399 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2400 VT != MVT::i1)
2401 return 0;
2402
2403 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2404 &PPC::GPRCRegClass);
2405 if (VT == MVT::i64)
2406 return PPCMaterialize64BitInt(Imm, RC);
2407 else
2408 return PPCMaterialize32BitInt(Imm, RC);
2409 }
2410
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.
2417 //
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.
fastEmitInst_ri(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill,uint64_t Imm)2422 unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
2423 const TargetRegisterClass *RC,
2424 unsigned Op0, bool Op0IsKill,
2425 uint64_t Imm) {
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);
2430
2431 const TargetRegisterClass *UseRC =
2432 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2433 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2434
2435 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
2436 Op0, Op0IsKill, Imm);
2437 }
2438
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.
fastEmitInst_r(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill)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));
2448
2449 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2450 }
2451
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.
fastEmitInst_rr(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill,unsigned Op1,bool Op1IsKill)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));
2462
2463 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2464 Op1, Op1IsKill);
2465 }
2466
2467 namespace llvm {
2468 // Create the fast instruction selector for PowerPC64 ELF.
createFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)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);
2475 return nullptr;
2476 }
2477 }
2478