blob: a561d5b1190a540f508d2da484002d0f53fd73f8 [file] [log] [blame]
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001//===-- 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
Bill Schmidt0cf702f2013-07-30 00:50:39 +000016#include "PPC.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000018#include "PPCCallingConv.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000019#include "PPCISelLowering.h"
Hal Finkele6698d52015-02-01 15:03:28 +000020#include "PPCMachineFunctionInfo.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000021#include "PPCSubtarget.h"
22#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000023#include "llvm/ADT/Optional.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000032#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000033#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/IntrinsicInst.h"
36#include "llvm/IR/Operator.h"
37#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000038#include "llvm/Target/TargetLowering.h"
39#include "llvm/Target/TargetMachine.h"
40
Bill Schmidteb8d6f72013-08-31 02:33:40 +000041//===----------------------------------------------------------------------===//
42//
43// TBD:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +000044// fastLowerArguments: Handle simple cases.
Bill Schmidteb8d6f72013-08-31 02:33:40 +000045// PPCMaterializeGV: Handle TLS.
46// SelectCall: Handle function pointers.
47// SelectCall: Handle multi-register return values.
48// SelectCall: Optimize away nops for local calls.
49// processCallArgs: Handle bit-converted arguments.
50// finishCall: Handle multi-register return values.
51// PPCComputeAddress: Handle parameter references as FrameIndex's.
52// PPCEmitCmp: Handle immediate as operand 1.
53// SelectCall: Handle small byval arguments.
54// SelectIntrinsicCall: Implement.
55// SelectSelect: Implement.
56// Consider factoring isTypeLegal into the base class.
57// Implement switches and jump tables.
58//
59//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000060using namespace llvm;
61
Chandler Carruth84e68b22014-04-22 02:41:26 +000062#define DEBUG_TYPE "ppcfastisel"
63
Bill Schmidt0cf702f2013-07-30 00:50:39 +000064namespace {
65
66typedef struct Address {
67 enum {
68 RegBase,
69 FrameIndexBase
70 } BaseType;
71
72 union {
73 unsigned Reg;
74 int FI;
75 } Base;
76
Bill Schmidtccecf262013-08-30 02:29:45 +000077 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000078
79 // Innocuous defaults for our address.
80 Address()
81 : BaseType(RegBase), Offset(0) {
82 Base.Reg = 0;
83 }
84} Address;
85
Craig Topper26696312014-03-18 07:27:13 +000086class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000087
88 const TargetMachine &TM;
Eric Christopher85806142015-01-30 02:11:24 +000089 const PPCSubtarget *PPCSubTarget;
Hal Finkele6698d52015-02-01 15:03:28 +000090 PPCFunctionInfo *PPCFuncInfo;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000091 const TargetInstrInfo &TII;
92 const TargetLowering &TLI;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000093 LLVMContext *Context;
94
95 public:
96 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
97 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000098 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
Eric Christophercccae792015-01-30 22:02:31 +000099 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
Hal Finkele6698d52015-02-01 15:03:28 +0000100 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
Eric Christopher85806142015-01-30 02:11:24 +0000101 TII(*PPCSubTarget->getInstrInfo()),
102 TLI(*PPCSubTarget->getTargetLowering()),
Eric Christopherd9134482014-08-04 21:25:23 +0000103 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000104
105 // Backend specific FastISel code.
106 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000107 bool fastSelectInstruction(const Instruction *I) override;
108 unsigned fastMaterializeConstant(const Constant *C) override;
109 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000110 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
111 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000112 bool fastLowerArguments() override;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000113 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
114 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000118 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000121 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000125
Hal Finkel934361a2015-01-14 01:07:51 +0000126 bool fastLowerCall(CallLoweringInfo &CLI) override;
127
Bill Schmidt03008132013-08-25 22:33:42 +0000128 // Instruction selection routines.
129 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000130 bool SelectLoad(const Instruction *I);
131 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000132 bool SelectBranch(const Instruction *I);
133 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000134 bool SelectFPExt(const Instruction *I);
135 bool SelectFPTrunc(const Instruction *I);
136 bool SelectIToFP(const Instruction *I, bool IsSigned);
137 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000138 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000139 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000140 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000141 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000142
143 // Utility routines.
144 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000145 bool isTypeLegal(Type *Ty, MVT &VT);
146 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000147 bool isValueAvailable(const Value *V) const;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000148 bool isVSFRCRegister(unsigned Register) const {
149 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
150 }
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000151 bool isVSSRCRegister(unsigned Register) const {
152 return MRI.getRegClass(Register)->getID() == PPC::VSSRCRegClassID;
153 }
Bill Schmidt03008132013-08-25 22:33:42 +0000154 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
155 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000156 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
157 const TargetRegisterClass *RC, bool IsZExt = true,
158 unsigned FP64LoadOpc = PPC::LFD);
159 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
160 bool PPCComputeAddress(const Value *Obj, Address &Addr);
161 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
162 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000163 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
164 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000165 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000166 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Samuel Antao61570df2014-09-17 23:25:06 +0000167 unsigned PPCMaterializeInt(const Constant *C, MVT VT, bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000168 unsigned PPCMaterialize32BitInt(int64_t Imm,
169 const TargetRegisterClass *RC);
170 unsigned PPCMaterialize64BitInt(int64_t Imm,
171 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000172 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
173 unsigned SrcReg, bool IsSigned);
174 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000175
Bill Schmidtd89f6782013-08-26 19:42:51 +0000176 // Call handling routines.
177 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000178 bool processCallArgs(SmallVectorImpl<Value*> &Args,
179 SmallVectorImpl<unsigned> &ArgRegs,
180 SmallVectorImpl<MVT> &ArgVTs,
181 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
182 SmallVectorImpl<unsigned> &RegArgs,
183 CallingConv::ID CC,
184 unsigned &NumBytes,
185 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000186 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000187 CCAssignFn *usePPC32CCs(unsigned Flag);
188
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000189 private:
190 #include "PPCGenFastISel.inc"
191
192};
193
194} // end anonymous namespace
195
Bill Schmidtd89f6782013-08-26 19:42:51 +0000196#include "PPCGenCallingConv.inc"
197
198// Function whose sole purpose is to kill compiler warnings
199// stemming from unused functions included from PPCGenCallingConv.inc.
200CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
201 if (Flag == 1)
202 return CC_PPC32_SVR4;
203 else if (Flag == 2)
204 return CC_PPC32_SVR4_ByVal;
205 else if (Flag == 3)
206 return CC_PPC32_SVR4_VarArg;
207 else
208 return RetCC_PPC;
209}
210
Bill Schmidt03008132013-08-25 22:33:42 +0000211static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
212 switch (Pred) {
213 // These are not representable with any single compare.
214 case CmpInst::FCMP_FALSE:
215 case CmpInst::FCMP_UEQ:
216 case CmpInst::FCMP_UGT:
217 case CmpInst::FCMP_UGE:
218 case CmpInst::FCMP_ULT:
219 case CmpInst::FCMP_ULE:
220 case CmpInst::FCMP_UNE:
221 case CmpInst::FCMP_TRUE:
222 default:
223 return Optional<PPC::Predicate>();
224
225 case CmpInst::FCMP_OEQ:
226 case CmpInst::ICMP_EQ:
227 return PPC::PRED_EQ;
228
229 case CmpInst::FCMP_OGT:
230 case CmpInst::ICMP_UGT:
231 case CmpInst::ICMP_SGT:
232 return PPC::PRED_GT;
233
234 case CmpInst::FCMP_OGE:
235 case CmpInst::ICMP_UGE:
236 case CmpInst::ICMP_SGE:
237 return PPC::PRED_GE;
238
239 case CmpInst::FCMP_OLT:
240 case CmpInst::ICMP_ULT:
241 case CmpInst::ICMP_SLT:
242 return PPC::PRED_LT;
243
244 case CmpInst::FCMP_OLE:
245 case CmpInst::ICMP_ULE:
246 case CmpInst::ICMP_SLE:
247 return PPC::PRED_LE;
248
249 case CmpInst::FCMP_ONE:
250 case CmpInst::ICMP_NE:
251 return PPC::PRED_NE;
252
253 case CmpInst::FCMP_ORD:
254 return PPC::PRED_NU;
255
256 case CmpInst::FCMP_UNO:
257 return PPC::PRED_UN;
258 }
259}
260
Bill Schmidtccecf262013-08-30 02:29:45 +0000261// Determine whether the type Ty is simple enough to be handled by
262// fast-isel, and return its equivalent machine type in VT.
263// FIXME: Copied directly from ARM -- factor into base class?
264bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
265 EVT Evt = TLI.getValueType(Ty, true);
266
267 // Only handle simple types.
268 if (Evt == MVT::Other || !Evt.isSimple()) return false;
269 VT = Evt.getSimpleVT();
270
271 // Handle all legal types, i.e. a register that will directly hold this
272 // value.
273 return TLI.isTypeLegal(VT);
274}
275
276// Determine whether the type Ty is simple enough to be handled by
277// fast-isel as a load target, and return its equivalent machine type in VT.
278bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
279 if (isTypeLegal(Ty, VT)) return true;
280
281 // If this is a type than can be sign or zero-extended to a basic operation
282 // go ahead and accept it now.
283 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
284 return true;
285 }
286
287 return false;
288}
289
Hal Finkel5f2a1372015-05-23 12:18:10 +0000290bool PPCFastISel::isValueAvailable(const Value *V) const {
291 if (!isa<Instruction>(V))
292 return true;
293
294 const auto *I = cast<Instruction>(V);
295 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
296 return true;
297
298 return false;
299}
300
Bill Schmidtccecf262013-08-30 02:29:45 +0000301// Given a value Obj, create an Address object Addr that represents its
302// address. Return false if we can't handle it.
303bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000304 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000305 unsigned Opcode = Instruction::UserOp1;
306 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
307 // Don't walk into other basic blocks unless the object is an alloca from
308 // another block, otherwise it may not have a virtual register assigned.
309 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
310 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
311 Opcode = I->getOpcode();
312 U = I;
313 }
314 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
315 Opcode = C->getOpcode();
316 U = C;
317 }
318
319 switch (Opcode) {
320 default:
321 break;
322 case Instruction::BitCast:
323 // Look through bitcasts.
324 return PPCComputeAddress(U->getOperand(0), Addr);
325 case Instruction::IntToPtr:
326 // Look past no-op inttoptrs.
327 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
328 return PPCComputeAddress(U->getOperand(0), Addr);
329 break;
330 case Instruction::PtrToInt:
331 // Look past no-op ptrtoints.
332 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
333 return PPCComputeAddress(U->getOperand(0), Addr);
334 break;
335 case Instruction::GetElementPtr: {
336 Address SavedAddr = Addr;
337 long TmpOffset = Addr.Offset;
338
339 // Iterate through the GEP folding the constants into offsets where
340 // we can.
341 gep_type_iterator GTI = gep_type_begin(U);
342 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
343 II != IE; ++II, ++GTI) {
344 const Value *Op = *II;
345 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000346 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000347 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
348 TmpOffset += SL->getElementOffset(Idx);
349 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000350 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000351 for (;;) {
352 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
353 // Constant-offset addressing.
354 TmpOffset += CI->getSExtValue() * S;
355 break;
356 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000357 if (canFoldAddIntoGEP(U, Op)) {
358 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000359 ConstantInt *CI =
360 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
361 TmpOffset += CI->getSExtValue() * S;
362 // Iterate on the other operand.
363 Op = cast<AddOperator>(Op)->getOperand(0);
364 continue;
365 }
366 // Unsupported
367 goto unsupported_gep;
368 }
369 }
370 }
371
372 // Try to grab the base operand now.
373 Addr.Offset = TmpOffset;
374 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
375
376 // We failed, restore everything and try the other options.
377 Addr = SavedAddr;
378
379 unsupported_gep:
380 break;
381 }
382 case Instruction::Alloca: {
383 const AllocaInst *AI = cast<AllocaInst>(Obj);
384 DenseMap<const AllocaInst*, int>::iterator SI =
385 FuncInfo.StaticAllocaMap.find(AI);
386 if (SI != FuncInfo.StaticAllocaMap.end()) {
387 Addr.BaseType = Address::FrameIndexBase;
388 Addr.Base.FI = SI->second;
389 return true;
390 }
391 break;
392 }
393 }
394
395 // FIXME: References to parameters fall through to the behavior
396 // below. They should be able to reference a frame index since
397 // they are stored to the stack, so we can get "ld rx, offset(r1)"
398 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
399 // just contain the parameter. Try to handle this with a FI.
400
401 // Try to get this in a register if nothing else has worked.
402 if (Addr.Base.Reg == 0)
403 Addr.Base.Reg = getRegForValue(Obj);
404
405 // Prevent assignment of base register to X0, which is inappropriate
406 // for loads and stores alike.
407 if (Addr.Base.Reg != 0)
408 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
409
410 return Addr.Base.Reg != 0;
411}
412
413// Fix up some addresses that can't be used directly. For example, if
414// an offset won't fit in an instruction field, we may need to move it
415// into an index register.
416void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
417 unsigned &IndexReg) {
418
419 // Check whether the offset fits in the instruction field.
420 if (!isInt<16>(Addr.Offset))
421 UseOffset = false;
422
423 // If this is a stack pointer and the offset needs to be simplified then
424 // put the alloca address into a register, set the base type back to
425 // register and continue. This should almost never happen.
426 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
427 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000429 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
430 Addr.Base.Reg = ResultReg;
431 Addr.BaseType = Address::RegBase;
432 }
433
434 if (!UseOffset) {
435 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
436 : Type::getInt64Ty(*Context));
437 const ConstantInt *Offset =
438 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
439 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
440 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
441 }
442}
443
444// Emit a load instruction if possible, returning true if we succeeded,
445// otherwise false. See commentary below for how the register class of
446// the load is determined.
447bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
448 const TargetRegisterClass *RC,
449 bool IsZExt, unsigned FP64LoadOpc) {
450 unsigned Opc;
451 bool UseOffset = true;
452
453 // If ResultReg is given, it determines the register class of the load.
454 // Otherwise, RC is the register class to use. If the result of the
455 // load isn't anticipated in this block, both may be zero, in which
456 // case we must make a conservative guess. In particular, don't assign
457 // R0 or X0 to the result register, as the result may be used in a load,
458 // store, add-immediate, or isel that won't permit this. (Though
459 // perhaps the spill and reload of live-exit values would handle this?)
460 const TargetRegisterClass *UseRC =
461 (ResultReg ? MRI.getRegClass(ResultReg) :
462 (RC ? RC :
463 (VT == MVT::f64 ? &PPC::F8RCRegClass :
464 (VT == MVT::f32 ? &PPC::F4RCRegClass :
465 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
466 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
467
468 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
469
470 switch (VT.SimpleTy) {
471 default: // e.g., vector types not handled
472 return false;
473 case MVT::i8:
474 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
475 break;
476 case MVT::i16:
477 Opc = (IsZExt ?
478 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
479 (Is32BitInt ? PPC::LHA : PPC::LHA8));
480 break;
481 case MVT::i32:
482 Opc = (IsZExt ?
483 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
484 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
485 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
486 UseOffset = false;
487 break;
488 case MVT::i64:
489 Opc = PPC::LD;
490 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
491 "64-bit load with 32-bit target??");
492 UseOffset = ((Addr.Offset & 3) == 0);
493 break;
494 case MVT::f32:
495 Opc = PPC::LFS;
496 break;
497 case MVT::f64:
498 Opc = FP64LoadOpc;
499 break;
500 }
501
502 // If necessary, materialize the offset into a register and use
503 // the indexed form. Also handle stack pointers with special needs.
504 unsigned IndexReg = 0;
505 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000506
507 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
508 // be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000509 bool IsVSSRC = (ResultReg != 0) && isVSSRCRegister(ResultReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000510 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000511 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
512 bool Is64VSXLoad = IsVSSRC && Opc == PPC::LFD;
513 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000514 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
515 (Addr.Offset == 0)) {
516 UseOffset = false;
517 }
518
Bill Schmidtccecf262013-08-30 02:29:45 +0000519 if (ResultReg == 0)
520 ResultReg = createResultReg(UseRC);
521
522 // Note: If we still have a frame index here, we know the offset is
523 // in range, as otherwise PPCSimplifyAddress would have converted it
524 // into a RegBase.
525 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000526 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000527 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000528
529 MachineMemOperand *MMO =
530 FuncInfo.MF->getMachineMemOperand(
531 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
532 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
533 MFI.getObjectAlignment(Addr.Base.FI));
534
Rafael Espindolaea09c592014-02-18 22:05:46 +0000535 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000536 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
537
538 // Base reg with offset in range.
539 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000540 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000541 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000542
Rafael Espindolaea09c592014-02-18 22:05:46 +0000543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000544 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
545
546 // Indexed form.
547 } else {
548 // Get the RR opcode corresponding to the RI one. FIXME: It would be
549 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
550 // is hard to get at.
551 switch (Opc) {
552 default: llvm_unreachable("Unexpected opcode!");
553 case PPC::LBZ: Opc = PPC::LBZX; break;
554 case PPC::LBZ8: Opc = PPC::LBZX8; break;
555 case PPC::LHZ: Opc = PPC::LHZX; break;
556 case PPC::LHZ8: Opc = PPC::LHZX8; break;
557 case PPC::LHA: Opc = PPC::LHAX; break;
558 case PPC::LHA8: Opc = PPC::LHAX8; break;
559 case PPC::LWZ: Opc = PPC::LWZX; break;
560 case PPC::LWZ8: Opc = PPC::LWZX8; break;
561 case PPC::LWA: Opc = PPC::LWAX; break;
562 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
563 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000564 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000565 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000566 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000567 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000568 .addReg(Addr.Base.Reg).addReg(IndexReg);
569 }
570
571 return true;
572}
573
574// Attempt to fast-select a load instruction.
575bool PPCFastISel::SelectLoad(const Instruction *I) {
576 // FIXME: No atomic loads are supported.
577 if (cast<LoadInst>(I)->isAtomic())
578 return false;
579
580 // Verify we have a legal type before going any further.
581 MVT VT;
582 if (!isLoadTypeLegal(I->getType(), VT))
583 return false;
584
585 // See if we can handle this address.
586 Address Addr;
587 if (!PPCComputeAddress(I->getOperand(0), Addr))
588 return false;
589
590 // Look at the currently assigned register for this instruction
591 // to determine the required register class. This is necessary
592 // to constrain RA from using R0/X0 when this is not legal.
593 unsigned AssignedReg = FuncInfo.ValueMap[I];
594 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000595 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000596
597 unsigned ResultReg = 0;
598 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
599 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000600 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000601 return true;
602}
603
604// Emit a store instruction to store SrcReg at Addr.
605bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
606 assert(SrcReg && "Nothing to store!");
607 unsigned Opc;
608 bool UseOffset = true;
609
610 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
611 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
612
613 switch (VT.SimpleTy) {
614 default: // e.g., vector types not handled
615 return false;
616 case MVT::i8:
617 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
618 break;
619 case MVT::i16:
620 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
621 break;
622 case MVT::i32:
623 assert(Is32BitInt && "Not GPRC for i32??");
624 Opc = PPC::STW;
625 break;
626 case MVT::i64:
627 Opc = PPC::STD;
628 UseOffset = ((Addr.Offset & 3) == 0);
629 break;
630 case MVT::f32:
631 Opc = PPC::STFS;
632 break;
633 case MVT::f64:
634 Opc = PPC::STFD;
635 break;
636 }
637
638 // If necessary, materialize the offset into a register and use
639 // the indexed form. Also handle stack pointers with special needs.
640 unsigned IndexReg = 0;
641 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
642
Bill Seurer8c728ae2014-12-05 20:15:56 +0000643 // If this is a potential VSX store with an offset of 0, a VSX indexed store
644 // can be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000645 bool IsVSSRC = isVSSRCRegister(SrcReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000646 bool IsVSFRC = isVSFRCRegister(SrcReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000647 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
648 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
649 if ((Is32VSXStore || Is64VSXStore) &&
650 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000651 (Addr.Offset == 0)) {
652 UseOffset = false;
653 }
654
Bill Schmidtccecf262013-08-30 02:29:45 +0000655 // Note: If we still have a frame index here, we know the offset is
656 // in range, as otherwise PPCSimplifyAddress would have converted it
657 // into a RegBase.
658 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000659 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000660 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000661
Bill Schmidtccecf262013-08-30 02:29:45 +0000662 MachineMemOperand *MMO =
663 FuncInfo.MF->getMachineMemOperand(
664 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
665 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
666 MFI.getObjectAlignment(Addr.Base.FI));
667
Rafael Espindolaea09c592014-02-18 22:05:46 +0000668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
669 .addReg(SrcReg)
670 .addImm(Addr.Offset)
671 .addFrameIndex(Addr.Base.FI)
672 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000673
674 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000675 } else if (UseOffset) {
676 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000677 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000678
Rafael Espindolaea09c592014-02-18 22:05:46 +0000679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000680 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
681
682 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000683 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000684 // Get the RR opcode corresponding to the RI one. FIXME: It would be
685 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
686 // is hard to get at.
687 switch (Opc) {
688 default: llvm_unreachable("Unexpected opcode!");
689 case PPC::STB: Opc = PPC::STBX; break;
690 case PPC::STH : Opc = PPC::STHX; break;
691 case PPC::STW : Opc = PPC::STWX; break;
692 case PPC::STB8: Opc = PPC::STBX8; break;
693 case PPC::STH8: Opc = PPC::STHX8; break;
694 case PPC::STW8: Opc = PPC::STWX8; break;
695 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000696 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000697 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000698 }
Samuel Antaof6815602015-03-17 15:00:57 +0000699
700 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
701 .addReg(SrcReg);
702
703 // If we have an index register defined we use it in the store inst,
704 // otherwise we use X0 as base as it makes the vector instructions to
705 // use zero in the computation of the effective address regardless the
706 // content of the register.
707 if (IndexReg)
708 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
709 else
710 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000711 }
712
713 return true;
714}
715
716// Attempt to fast-select a store instruction.
717bool PPCFastISel::SelectStore(const Instruction *I) {
718 Value *Op0 = I->getOperand(0);
719 unsigned SrcReg = 0;
720
721 // FIXME: No atomics loads are supported.
722 if (cast<StoreInst>(I)->isAtomic())
723 return false;
724
725 // Verify we have a legal type before going any further.
726 MVT VT;
727 if (!isLoadTypeLegal(Op0->getType(), VT))
728 return false;
729
730 // Get the value to be stored into a register.
731 SrcReg = getRegForValue(Op0);
732 if (SrcReg == 0)
733 return false;
734
735 // See if we can handle this address.
736 Address Addr;
737 if (!PPCComputeAddress(I->getOperand(1), Addr))
738 return false;
739
740 if (!PPCEmitStore(VT, SrcReg, Addr))
741 return false;
742
743 return true;
744}
745
Bill Schmidt03008132013-08-25 22:33:42 +0000746// Attempt to fast-select a branch instruction.
747bool PPCFastISel::SelectBranch(const Instruction *I) {
748 const BranchInst *BI = cast<BranchInst>(I);
749 MachineBasicBlock *BrBB = FuncInfo.MBB;
750 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
751 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
752
753 // For now, just try the simplest case where it's fed by a compare.
754 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000755 if (isValueAvailable(CI)) {
756 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
757 if (!OptPPCPred)
758 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000759
Hal Finkel5f2a1372015-05-23 12:18:10 +0000760 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000761
Hal Finkel5f2a1372015-05-23 12:18:10 +0000762 // Take advantage of fall-through opportunities.
763 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
764 std::swap(TBB, FBB);
765 PPCPred = PPC::InvertPredicate(PPCPred);
766 }
767
768 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
769
770 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
771 CondReg))
772 return false;
773
774 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
775 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
776 fastEmitBranch(FBB, DbgLoc);
777 FuncInfo.MBB->addSuccessor(TBB);
778 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000779 }
Bill Schmidt03008132013-08-25 22:33:42 +0000780 } else if (const ConstantInt *CI =
781 dyn_cast<ConstantInt>(BI->getCondition())) {
782 uint64_t Imm = CI->getZExtValue();
783 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000784 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000785 return true;
786 }
787
788 // FIXME: ARM looks for a case where the block containing the compare
789 // has been split from the block containing the branch. If this happens,
790 // there is a vreg available containing the result of the compare. I'm
791 // not sure we can do much, as we've lost the predicate information with
792 // the compare instruction -- we have a 4-bit CR but don't know which bit
793 // to test here.
794 return false;
795}
796
797// Attempt to emit a compare of the two source values. Signed and unsigned
798// comparisons are supported. Return false if we can't handle it.
799bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
800 bool IsZExt, unsigned DestReg) {
801 Type *Ty = SrcValue1->getType();
802 EVT SrcEVT = TLI.getValueType(Ty, true);
803 if (!SrcEVT.isSimple())
804 return false;
805 MVT SrcVT = SrcEVT.getSimpleVT();
806
Eric Christopher1b8e7632014-05-22 01:07:24 +0000807 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000808 return false;
809
Bill Schmidt03008132013-08-25 22:33:42 +0000810 // See if operand 2 is an immediate encodeable in the compare.
811 // FIXME: Operands are not in canonical order at -O0, so an immediate
812 // operand in position 1 is a lost opportunity for now. We are
813 // similar to ARM in this regard.
814 long Imm = 0;
815 bool UseImm = false;
816
817 // Only 16-bit integer constants can be represented in compares for
818 // PowerPC. Others will be materialized into a register.
819 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
820 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
821 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
822 const APInt &CIVal = ConstInt->getValue();
823 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
824 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
825 UseImm = true;
826 }
827 }
828
829 unsigned CmpOpc;
830 bool NeedsExt = false;
831 switch (SrcVT.SimpleTy) {
832 default: return false;
833 case MVT::f32:
834 CmpOpc = PPC::FCMPUS;
835 break;
836 case MVT::f64:
837 CmpOpc = PPC::FCMPUD;
838 break;
839 case MVT::i1:
840 case MVT::i8:
841 case MVT::i16:
842 NeedsExt = true;
843 // Intentional fall-through.
844 case MVT::i32:
845 if (!UseImm)
846 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
847 else
848 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
849 break;
850 case MVT::i64:
851 if (!UseImm)
852 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
853 else
854 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
855 break;
856 }
857
858 unsigned SrcReg1 = getRegForValue(SrcValue1);
859 if (SrcReg1 == 0)
860 return false;
861
862 unsigned SrcReg2 = 0;
863 if (!UseImm) {
864 SrcReg2 = getRegForValue(SrcValue2);
865 if (SrcReg2 == 0)
866 return false;
867 }
868
869 if (NeedsExt) {
870 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
871 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
872 return false;
873 SrcReg1 = ExtReg;
874
875 if (!UseImm) {
876 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
877 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
878 return false;
879 SrcReg2 = ExtReg;
880 }
881 }
882
883 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000885 .addReg(SrcReg1).addReg(SrcReg2);
886 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000887 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000888 .addReg(SrcReg1).addImm(Imm);
889
890 return true;
891}
892
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000893// Attempt to fast-select a floating-point extend instruction.
894bool PPCFastISel::SelectFPExt(const Instruction *I) {
895 Value *Src = I->getOperand(0);
896 EVT SrcVT = TLI.getValueType(Src->getType(), true);
897 EVT DestVT = TLI.getValueType(I->getType(), true);
898
899 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
900 return false;
901
902 unsigned SrcReg = getRegForValue(Src);
903 if (!SrcReg)
904 return false;
905
906 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000907 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000908 return true;
909}
910
911// Attempt to fast-select a floating-point truncate instruction.
912bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
913 Value *Src = I->getOperand(0);
914 EVT SrcVT = TLI.getValueType(Src->getType(), true);
915 EVT DestVT = TLI.getValueType(I->getType(), true);
916
917 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
918 return false;
919
920 unsigned SrcReg = getRegForValue(Src);
921 if (!SrcReg)
922 return false;
923
924 // Round the result to single precision.
925 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000927 .addReg(SrcReg);
928
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000929 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000930 return true;
931}
932
933// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000934// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000935// those should be used instead of moving via a stack slot when the
936// subtarget permits.
937// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
938// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
939// case to 8 bytes which produces tighter code but wastes stack space.
940unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
941 bool IsSigned) {
942
943 // If necessary, extend 32-bit int to 64-bit.
944 if (SrcVT == MVT::i32) {
945 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
946 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
947 return 0;
948 SrcReg = TmpReg;
949 }
950
951 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
952 Address Addr;
953 Addr.BaseType = Address::FrameIndexBase;
954 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
955
956 // Store the value from the GPR.
957 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
958 return 0;
959
960 // Load the integer value into an FPR. The kind of load used depends
961 // on a number of conditions.
962 unsigned LoadOpc = PPC::LFD;
963
964 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000965 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000966 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000967 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000968 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000969 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000970 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000971 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000972 }
973
974 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
975 unsigned ResultReg = 0;
976 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
977 return 0;
978
979 return ResultReg;
980}
981
982// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000983// FIXME: Once fast-isel has better support for VSX, conversions using
984// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000985bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
986 MVT DstVT;
987 Type *DstTy = I->getType();
988 if (!isTypeLegal(DstTy, DstVT))
989 return false;
990
991 if (DstVT != MVT::f32 && DstVT != MVT::f64)
992 return false;
993
994 Value *Src = I->getOperand(0);
995 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
996 if (!SrcEVT.isSimple())
997 return false;
998
999 MVT SrcVT = SrcEVT.getSimpleVT();
1000
1001 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1002 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1003 return false;
1004
1005 unsigned SrcReg = getRegForValue(Src);
1006 if (SrcReg == 0)
1007 return false;
1008
1009 // We can only lower an unsigned convert if we have the newer
1010 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001011 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001012 return false;
1013
1014 // FIXME: For now we require the newer floating-point conversion operations
1015 // (which are present only on P7 and A2 server models) when converting
1016 // to single-precision float. Otherwise we have to generate a lot of
1017 // fiddly code to avoid double rounding. If necessary, the fiddly code
1018 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001019 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001020 return false;
1021
1022 // Extend the input if necessary.
1023 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1024 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1025 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1026 return false;
1027 SrcVT = MVT::i64;
1028 SrcReg = TmpReg;
1029 }
1030
1031 // Move the integer value to an FPR.
1032 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1033 if (FPReg == 0)
1034 return false;
1035
1036 // Determine the opcode for the conversion.
1037 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1038 unsigned DestReg = createResultReg(RC);
1039 unsigned Opc;
1040
1041 if (DstVT == MVT::f32)
1042 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1043 else
1044 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1045
1046 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001048 .addReg(FPReg);
1049
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001050 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001051 return true;
1052}
1053
1054// Move the floating-point value in SrcReg into an integer destination
1055// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001056// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001057// those should be used instead of moving via a stack slot when the
1058// subtarget permits.
1059unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1060 unsigned SrcReg, bool IsSigned) {
1061 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1062 // Note that if have STFIWX available, we could use a 4-byte stack
1063 // slot for i32, but this being fast-isel we'll just go with the
1064 // easiest code gen possible.
1065 Address Addr;
1066 Addr.BaseType = Address::FrameIndexBase;
1067 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1068
1069 // Store the value from the FPR.
1070 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1071 return 0;
1072
1073 // Reload it into a GPR. If we want an i32, modify the address
1074 // to have a 4-byte offset so we load from the right place.
1075 if (VT == MVT::i32)
1076 Addr.Offset = 4;
1077
1078 // Look at the currently assigned register for this instruction
1079 // to determine the required register class.
1080 unsigned AssignedReg = FuncInfo.ValueMap[I];
1081 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001082 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001083
1084 unsigned ResultReg = 0;
1085 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1086 return 0;
1087
1088 return ResultReg;
1089}
1090
1091// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001092// FIXME: Once fast-isel has better support for VSX, conversions using
1093// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001094bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1095 MVT DstVT, SrcVT;
1096 Type *DstTy = I->getType();
1097 if (!isTypeLegal(DstTy, DstVT))
1098 return false;
1099
1100 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1101 return false;
1102
Bill Schmidt83973ef2014-06-24 20:05:18 +00001103 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1104 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1105 return false;
1106
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001107 Value *Src = I->getOperand(0);
1108 Type *SrcTy = Src->getType();
1109 if (!isTypeLegal(SrcTy, SrcVT))
1110 return false;
1111
1112 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1113 return false;
1114
1115 unsigned SrcReg = getRegForValue(Src);
1116 if (SrcReg == 0)
1117 return false;
1118
1119 // Convert f32 to f64 if necessary. This is just a meaningless copy
1120 // to get the register class right. COPY_TO_REGCLASS is needed since
1121 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1122 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1123 if (InRC == &PPC::F4RCRegClass) {
1124 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001126 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1127 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1128 SrcReg = TmpReg;
1129 }
1130
1131 // Determine the opcode for the conversion, which takes place
1132 // entirely within FPRs.
1133 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1134 unsigned Opc;
1135
1136 if (DstVT == MVT::i32)
1137 if (IsSigned)
1138 Opc = PPC::FCTIWZ;
1139 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001140 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001141 else
1142 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1143
1144 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001146 .addReg(SrcReg);
1147
1148 // Now move the integer value from a float register to an integer register.
1149 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1150 if (IntReg == 0)
1151 return false;
1152
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001153 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001154 return true;
1155}
1156
Bill Schmidtccecf262013-08-30 02:29:45 +00001157// Attempt to fast-select a binary integer operation that isn't already
1158// handled automatically.
1159bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1160 EVT DestVT = TLI.getValueType(I->getType(), true);
1161
1162 // We can get here in the case when we have a binary operation on a non-legal
1163 // type and the target independent selector doesn't know how to handle it.
1164 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1165 return false;
1166
1167 // Look at the currently assigned register for this instruction
1168 // to determine the required register class. If there is no register,
1169 // make a conservative choice (don't assign R0).
1170 unsigned AssignedReg = FuncInfo.ValueMap[I];
1171 const TargetRegisterClass *RC =
1172 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1173 &PPC::GPRC_and_GPRC_NOR0RegClass);
1174 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1175
1176 unsigned Opc;
1177 switch (ISDOpcode) {
1178 default: return false;
1179 case ISD::ADD:
1180 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1181 break;
1182 case ISD::OR:
1183 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1184 break;
1185 case ISD::SUB:
1186 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1187 break;
1188 }
1189
1190 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1191 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1192 if (SrcReg1 == 0) return false;
1193
1194 // Handle case of small immediate operand.
1195 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1196 const APInt &CIVal = ConstInt->getValue();
1197 int Imm = (int)CIVal.getSExtValue();
1198 bool UseImm = true;
1199 if (isInt<16>(Imm)) {
1200 switch (Opc) {
1201 default:
1202 llvm_unreachable("Missing case!");
1203 case PPC::ADD4:
1204 Opc = PPC::ADDI;
1205 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1206 break;
1207 case PPC::ADD8:
1208 Opc = PPC::ADDI8;
1209 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1210 break;
1211 case PPC::OR:
1212 Opc = PPC::ORI;
1213 break;
1214 case PPC::OR8:
1215 Opc = PPC::ORI8;
1216 break;
1217 case PPC::SUBF:
1218 if (Imm == -32768)
1219 UseImm = false;
1220 else {
1221 Opc = PPC::ADDI;
1222 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1223 Imm = -Imm;
1224 }
1225 break;
1226 case PPC::SUBF8:
1227 if (Imm == -32768)
1228 UseImm = false;
1229 else {
1230 Opc = PPC::ADDI8;
1231 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1232 Imm = -Imm;
1233 }
1234 break;
1235 }
1236
1237 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1239 ResultReg)
1240 .addReg(SrcReg1)
1241 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001242 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001243 return true;
1244 }
1245 }
1246 }
1247
1248 // Reg-reg case.
1249 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1250 if (SrcReg2 == 0) return false;
1251
1252 // Reverse operands for subtract-from.
1253 if (ISDOpcode == ISD::SUB)
1254 std::swap(SrcReg1, SrcReg2);
1255
Rafael Espindolaea09c592014-02-18 22:05:46 +00001256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001257 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001258 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001259 return true;
1260}
1261
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001262// Handle arguments to a call that we're attempting to fast-select.
1263// Return false if the arguments are too complex for us at the moment.
1264bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1265 SmallVectorImpl<unsigned> &ArgRegs,
1266 SmallVectorImpl<MVT> &ArgVTs,
1267 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1268 SmallVectorImpl<unsigned> &RegArgs,
1269 CallingConv::ID CC,
1270 unsigned &NumBytes,
1271 bool IsVarArg) {
1272 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001273 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001274
1275 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001276 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001277 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001278
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001279 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1280
1281 // Bail out if we can't handle any of the arguments.
1282 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1283 CCValAssign &VA = ArgLocs[I];
1284 MVT ArgVT = ArgVTs[VA.getValNo()];
1285
1286 // Skip vector arguments for now, as well as long double and
1287 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001288 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001289 !VA.isRegLoc() || VA.needsCustom())
1290 return false;
1291
1292 // Skip bit-converted arguments for now.
1293 if (VA.getLocInfo() == CCValAssign::BCvt)
1294 return false;
1295 }
1296
1297 // Get a count of how many bytes are to be pushed onto the stack.
1298 NumBytes = CCInfo.getNextStackOffset();
1299
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001300 // The prolog code of the callee may store up to 8 GPR argument registers to
1301 // the stack, allowing va_start to index over them in memory if its varargs.
1302 // Because we cannot tell if this is needed on the caller side, we have to
1303 // conservatively assume that it is needed. As such, make sure we have at
1304 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001305 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001306 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001307
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001308 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001310 TII.get(TII.getCallFrameSetupOpcode()))
1311 .addImm(NumBytes);
1312
1313 // Prepare to assign register arguments. Every argument uses up a
1314 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001315 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001316 unsigned NextGPR = PPC::X3;
1317 unsigned NextFPR = PPC::F1;
1318
1319 // Process arguments.
1320 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1321 CCValAssign &VA = ArgLocs[I];
1322 unsigned Arg = ArgRegs[VA.getValNo()];
1323 MVT ArgVT = ArgVTs[VA.getValNo()];
1324
1325 // Handle argument promotion and bitcasts.
1326 switch (VA.getLocInfo()) {
1327 default:
1328 llvm_unreachable("Unknown loc info!");
1329 case CCValAssign::Full:
1330 break;
1331 case CCValAssign::SExt: {
1332 MVT DestVT = VA.getLocVT();
1333 const TargetRegisterClass *RC =
1334 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1335 unsigned TmpReg = createResultReg(RC);
1336 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1337 llvm_unreachable("Failed to emit a sext!");
1338 ArgVT = DestVT;
1339 Arg = TmpReg;
1340 break;
1341 }
1342 case CCValAssign::AExt:
1343 case CCValAssign::ZExt: {
1344 MVT DestVT = VA.getLocVT();
1345 const TargetRegisterClass *RC =
1346 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1347 unsigned TmpReg = createResultReg(RC);
1348 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1349 llvm_unreachable("Failed to emit a zext!");
1350 ArgVT = DestVT;
1351 Arg = TmpReg;
1352 break;
1353 }
1354 case CCValAssign::BCvt: {
1355 // FIXME: Not yet handled.
1356 llvm_unreachable("Should have bailed before getting here!");
1357 break;
1358 }
1359 }
1360
1361 // Copy this argument to the appropriate register.
1362 unsigned ArgReg;
1363 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1364 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001365 if (CC != CallingConv::Fast)
1366 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001367 } else
1368 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001369
1370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1371 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001372 RegArgs.push_back(ArgReg);
1373 }
1374
1375 return true;
1376}
1377
1378// For a call that we've determined we can fast-select, finish the
1379// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001380bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1381 CallingConv::ID CC = CLI.CallConv;
1382
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001383 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001385 TII.get(TII.getCallFrameDestroyOpcode()))
1386 .addImm(NumBytes).addImm(0);
1387
1388 // Next, generate a copy to obtain the return value.
1389 // FIXME: No multi-register return values yet, though I don't foresee
1390 // any real difficulties there.
1391 if (RetVT != MVT::isVoid) {
1392 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001393 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001394 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1395 CCValAssign &VA = RVLocs[0];
1396 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1397 assert(VA.isRegLoc() && "Can only return in registers!");
1398
1399 MVT DestVT = VA.getValVT();
1400 MVT CopyVT = DestVT;
1401
1402 // Ints smaller than a register still arrive in a full 64-bit
1403 // register, so make sure we recognize this.
1404 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1405 CopyVT = MVT::i64;
1406
1407 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001408 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001409
1410 if (RetVT == CopyVT) {
1411 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1412 ResultReg = createResultReg(CpyRC);
1413
Rafael Espindolaea09c592014-02-18 22:05:46 +00001414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001415 TII.get(TargetOpcode::COPY), ResultReg)
1416 .addReg(SourcePhysReg);
1417
1418 // If necessary, round the floating result to single precision.
1419 } else if (CopyVT == MVT::f64) {
1420 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001422 ResultReg).addReg(SourcePhysReg);
1423
1424 // If only the low half of a general register is needed, generate
1425 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1426 // used along the fast-isel path (not lowered), and downstream logic
1427 // also doesn't like a direct subreg copy on a physical reg.)
1428 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1429 ResultReg = createResultReg(&PPC::GPRCRegClass);
1430 // Convert physical register from G8RC to GPRC.
1431 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001433 TII.get(TargetOpcode::COPY), ResultReg)
1434 .addReg(SourcePhysReg);
1435 }
1436
Bill Schmidt0954ea12013-08-30 23:25:30 +00001437 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001438 CLI.InRegs.push_back(SourcePhysReg);
1439 CLI.ResultReg = ResultReg;
1440 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001441 }
Hal Finkel934361a2015-01-14 01:07:51 +00001442
1443 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001444}
1445
Hal Finkel934361a2015-01-14 01:07:51 +00001446bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1447 CallingConv::ID CC = CLI.CallConv;
1448 bool IsTailCall = CLI.IsTailCall;
1449 bool IsVarArg = CLI.IsVarArg;
1450 const Value *Callee = CLI.Callee;
1451 const char *SymName = CLI.SymName;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001452
Hal Finkel934361a2015-01-14 01:07:51 +00001453 if (!Callee && !SymName)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001454 return false;
1455
1456 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001457 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001458 return false;
1459
Hal Finkel934361a2015-01-14 01:07:51 +00001460 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001461 if (IsVarArg)
1462 return false;
1463
1464 // Handle simple calls for now, with legal return types and
1465 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001466 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001467 MVT RetVT;
1468 if (RetTy->isVoidTy())
1469 RetVT = MVT::isVoid;
1470 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1471 RetVT != MVT::i8)
1472 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001473 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1474 // We can't handle boolean returns when CR bits are in use.
1475 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001476
1477 // FIXME: No multi-register return values yet.
1478 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1479 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1480 RetVT != MVT::f64) {
1481 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001482 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001483 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1484 if (RVLocs.size() > 1)
1485 return false;
1486 }
1487
1488 // Bail early if more than 8 arguments, as we only currently
1489 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001490 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001491 if (NumArgs > 8)
1492 return false;
1493
1494 // Set up the argument vectors.
1495 SmallVector<Value*, 8> Args;
1496 SmallVector<unsigned, 8> ArgRegs;
1497 SmallVector<MVT, 8> ArgVTs;
1498 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1499
1500 Args.reserve(NumArgs);
1501 ArgRegs.reserve(NumArgs);
1502 ArgVTs.reserve(NumArgs);
1503 ArgFlags.reserve(NumArgs);
1504
Hal Finkel934361a2015-01-14 01:07:51 +00001505 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001506 // Only handle easy calls for now. It would be reasonably easy
1507 // to handle <= 8-byte structures passed ByVal in registers, but we
1508 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001509 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1510 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001511 return false;
1512
Hal Finkel934361a2015-01-14 01:07:51 +00001513 Value *ArgValue = CLI.OutVals[i];
1514 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001515 MVT ArgVT;
1516 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1517 return false;
1518
1519 if (ArgVT.isVector())
1520 return false;
1521
Hal Finkel934361a2015-01-14 01:07:51 +00001522 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001523 if (Arg == 0)
1524 return false;
1525
Hal Finkel934361a2015-01-14 01:07:51 +00001526 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001527 ArgRegs.push_back(Arg);
1528 ArgVTs.push_back(ArgVT);
1529 ArgFlags.push_back(Flags);
1530 }
1531
1532 // Process the arguments.
1533 SmallVector<unsigned, 8> RegArgs;
1534 unsigned NumBytes;
1535
1536 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1537 RegArgs, CC, NumBytes, IsVarArg))
1538 return false;
1539
Hal Finkel934361a2015-01-14 01:07:51 +00001540 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001541 // FIXME: No handling for function pointers yet. This requires
1542 // implementing the function descriptor (OPD) setup.
1543 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001544 if (!GV) {
1545 // patchpoints are a special case; they always dispatch to a pointer value.
1546 // However, we don't actually want to generate the indirect call sequence
1547 // here (that will be generated, as necessary, during asm printing), and
1548 // the call we generate here will be erased by FastISel::selectPatchpoint,
1549 // so don't try very hard...
1550 if (CLI.IsPatchPoint)
1551 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1552 else
1553 return false;
1554 } else {
1555 // Build direct call with NOP for TOC restore.
1556 // FIXME: We can and should optimize away the NOP for local calls.
1557 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1558 TII.get(PPC::BL8_NOP));
1559 // Add callee.
1560 MIB.addGlobalAddress(GV);
1561 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001562
1563 // Add implicit physical register uses to the call.
1564 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1565 MIB.addReg(RegArgs[II], RegState::Implicit);
1566
Hal Finkelaf519932015-01-19 07:20:27 +00001567 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1568 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001569 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001570 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001571
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001572 // Add a register mask with the call-preserved registers. Proper
1573 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001574 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001575
Hal Finkel934361a2015-01-14 01:07:51 +00001576 CLI.Call = MIB;
1577
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001578 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001579 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001580}
1581
Bill Schmidtd89f6782013-08-26 19:42:51 +00001582// Attempt to fast-select a return instruction.
1583bool PPCFastISel::SelectRet(const Instruction *I) {
1584
1585 if (!FuncInfo.CanLowerReturn)
1586 return false;
1587
1588 const ReturnInst *Ret = cast<ReturnInst>(I);
1589 const Function &F = *I->getParent()->getParent();
1590
1591 // Build a list of return value registers.
1592 SmallVector<unsigned, 4> RetRegs;
1593 CallingConv::ID CC = F.getCallingConv();
1594
1595 if (Ret->getNumOperands() > 0) {
1596 SmallVector<ISD::OutputArg, 4> Outs;
1597 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1598
1599 // Analyze operands of the call, assigning locations to each operand.
1600 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001601 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001602 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1603 const Value *RV = Ret->getOperand(0);
1604
1605 // FIXME: Only one output register for now.
1606 if (ValLocs.size() > 1)
1607 return false;
1608
1609 // Special case for returning a constant integer of any size.
1610 // Materialize the constant as an i64 and copy it to the return
Samuel Antao61570df2014-09-17 23:25:06 +00001611 // register. We still need to worry about properly extending the sign. E.g:
1612 // If the constant has only one bit, it means it is a boolean. Therefore
1613 // we can't use PPCMaterializeInt because it extends the sign which will
1614 // cause negations of the returned value to be incorrect as they are
1615 // implemented as the flip of the least significant bit.
Bill Schmidtd89f6782013-08-26 19:42:51 +00001616 if (isa<ConstantInt>(*RV)) {
1617 const Constant *C = cast<Constant>(RV);
Samuel Antao61570df2014-09-17 23:25:06 +00001618
1619 CCValAssign &VA = ValLocs[0];
1620
1621 unsigned RetReg = VA.getLocReg();
1622 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64,
1623 VA.getLocInfo() == CCValAssign::SExt);
1624
Rafael Espindolaea09c592014-02-18 22:05:46 +00001625 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001626 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1627
Bill Schmidtd89f6782013-08-26 19:42:51 +00001628 RetRegs.push_back(RetReg);
1629
1630 } else {
1631 unsigned Reg = getRegForValue(RV);
1632
1633 if (Reg == 0)
1634 return false;
1635
1636 // Copy the result values into the output registers.
1637 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1638
1639 CCValAssign &VA = ValLocs[i];
1640 assert(VA.isRegLoc() && "Can only return in registers!");
1641 RetRegs.push_back(VA.getLocReg());
1642 unsigned SrcReg = Reg + VA.getValNo();
1643
1644 EVT RVEVT = TLI.getValueType(RV->getType());
1645 if (!RVEVT.isSimple())
1646 return false;
1647 MVT RVVT = RVEVT.getSimpleVT();
1648 MVT DestVT = VA.getLocVT();
1649
1650 if (RVVT != DestVT && RVVT != MVT::i8 &&
1651 RVVT != MVT::i16 && RVVT != MVT::i32)
1652 return false;
1653
1654 if (RVVT != DestVT) {
1655 switch (VA.getLocInfo()) {
1656 default:
1657 llvm_unreachable("Unknown loc info!");
1658 case CCValAssign::Full:
1659 llvm_unreachable("Full value assign but types don't match?");
1660 case CCValAssign::AExt:
1661 case CCValAssign::ZExt: {
1662 const TargetRegisterClass *RC =
1663 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1664 unsigned TmpReg = createResultReg(RC);
1665 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1666 return false;
1667 SrcReg = TmpReg;
1668 break;
1669 }
1670 case CCValAssign::SExt: {
1671 const TargetRegisterClass *RC =
1672 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1673 unsigned TmpReg = createResultReg(RC);
1674 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1675 return false;
1676 SrcReg = TmpReg;
1677 break;
1678 }
1679 }
1680 }
1681
Rafael Espindolaea09c592014-02-18 22:05:46 +00001682 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001683 TII.get(TargetOpcode::COPY), RetRegs[i])
1684 .addReg(SrcReg);
1685 }
1686 }
1687 }
1688
Rafael Espindolaea09c592014-02-18 22:05:46 +00001689 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001690 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001691
1692 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1693 MIB.addReg(RetRegs[i], RegState::Implicit);
1694
1695 return true;
1696}
1697
Bill Schmidt03008132013-08-25 22:33:42 +00001698// Attempt to emit an integer extend of SrcReg into DestReg. Both
1699// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001700// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001701bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1702 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001703 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1704 return false;
1705 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1706 return false;
1707
1708 // Signed extensions use EXTSB, EXTSH, EXTSW.
1709 if (!IsZExt) {
1710 unsigned Opc;
1711 if (SrcVT == MVT::i8)
1712 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1713 else if (SrcVT == MVT::i16)
1714 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1715 else {
1716 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1717 Opc = PPC::EXTSW_32_64;
1718 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001719 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001720 .addReg(SrcReg);
1721
1722 // Unsigned 32-bit extensions use RLWINM.
1723 } else if (DestVT == MVT::i32) {
1724 unsigned MB;
1725 if (SrcVT == MVT::i8)
1726 MB = 24;
1727 else {
1728 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1729 MB = 16;
1730 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001731 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001732 DestReg)
1733 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1734
1735 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1736 } else {
1737 unsigned MB;
1738 if (SrcVT == MVT::i8)
1739 MB = 56;
1740 else if (SrcVT == MVT::i16)
1741 MB = 48;
1742 else
1743 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001744 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001745 TII.get(PPC::RLDICL_32_64), DestReg)
1746 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1747 }
1748
1749 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001750}
1751
1752// Attempt to fast-select an indirect branch instruction.
1753bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1754 unsigned AddrReg = getRegForValue(I->getOperand(0));
1755 if (AddrReg == 0)
1756 return false;
1757
Rafael Espindolaea09c592014-02-18 22:05:46 +00001758 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001759 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001760 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001761
1762 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1763 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1764 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1765
1766 return true;
1767}
1768
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001769// Attempt to fast-select an integer truncate instruction.
1770bool PPCFastISel::SelectTrunc(const Instruction *I) {
1771 Value *Src = I->getOperand(0);
1772 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1773 EVT DestVT = TLI.getValueType(I->getType(), true);
1774
1775 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1776 return false;
1777
1778 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1779 return false;
1780
1781 unsigned SrcReg = getRegForValue(Src);
1782 if (!SrcReg)
1783 return false;
1784
1785 // The only interesting case is when we need to switch register classes.
1786 if (SrcVT == MVT::i64) {
1787 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001788 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1789 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001790 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1791 SrcReg = ResultReg;
1792 }
1793
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001794 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001795 return true;
1796}
1797
Bill Schmidtd89f6782013-08-26 19:42:51 +00001798// Attempt to fast-select an integer extend instruction.
1799bool PPCFastISel::SelectIntExt(const Instruction *I) {
1800 Type *DestTy = I->getType();
1801 Value *Src = I->getOperand(0);
1802 Type *SrcTy = Src->getType();
1803
1804 bool IsZExt = isa<ZExtInst>(I);
1805 unsigned SrcReg = getRegForValue(Src);
1806 if (!SrcReg) return false;
1807
1808 EVT SrcEVT, DestEVT;
1809 SrcEVT = TLI.getValueType(SrcTy, true);
1810 DestEVT = TLI.getValueType(DestTy, true);
1811 if (!SrcEVT.isSimple())
1812 return false;
1813 if (!DestEVT.isSimple())
1814 return false;
1815
1816 MVT SrcVT = SrcEVT.getSimpleVT();
1817 MVT DestVT = DestEVT.getSimpleVT();
1818
1819 // If we know the register class needed for the result of this
1820 // instruction, use it. Otherwise pick the register class of the
1821 // correct size that does not contain X0/R0, since we don't know
1822 // whether downstream uses permit that assignment.
1823 unsigned AssignedReg = FuncInfo.ValueMap[I];
1824 const TargetRegisterClass *RC =
1825 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1826 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1827 &PPC::GPRC_and_GPRC_NOR0RegClass));
1828 unsigned ResultReg = createResultReg(RC);
1829
1830 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1831 return false;
1832
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001833 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001834 return true;
1835}
1836
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001837// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001838// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001839bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001840
1841 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001842 case Instruction::Load:
1843 return SelectLoad(I);
1844 case Instruction::Store:
1845 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001846 case Instruction::Br:
1847 return SelectBranch(I);
1848 case Instruction::IndirectBr:
1849 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001850 case Instruction::FPExt:
1851 return SelectFPExt(I);
1852 case Instruction::FPTrunc:
1853 return SelectFPTrunc(I);
1854 case Instruction::SIToFP:
1855 return SelectIToFP(I, /*IsSigned*/ true);
1856 case Instruction::UIToFP:
1857 return SelectIToFP(I, /*IsSigned*/ false);
1858 case Instruction::FPToSI:
1859 return SelectFPToI(I, /*IsSigned*/ true);
1860 case Instruction::FPToUI:
1861 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001862 case Instruction::Add:
1863 return SelectBinaryIntOp(I, ISD::ADD);
1864 case Instruction::Or:
1865 return SelectBinaryIntOp(I, ISD::OR);
1866 case Instruction::Sub:
1867 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001868 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001869 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001870 case Instruction::Ret:
1871 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001872 case Instruction::Trunc:
1873 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001874 case Instruction::ZExt:
1875 case Instruction::SExt:
1876 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001877 // Here add other flavors of Instruction::XXX that automated
1878 // cases don't catch. For example, switches are terminators
1879 // that aren't yet handled.
1880 default:
1881 break;
1882 }
1883 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001884}
1885
1886// Materialize a floating-point constant into a register, and return
1887// the register number (or zero if we failed to handle it).
1888unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1889 // No plans to handle long double here.
1890 if (VT != MVT::f32 && VT != MVT::f64)
1891 return 0;
1892
1893 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001894 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001895 assert(Align > 0 && "Unexpectedly missing alignment information!");
1896 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1897 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1898 CodeModel::Model CModel = TM.getCodeModel();
1899
1900 MachineMemOperand *MMO =
1901 FuncInfo.MF->getMachineMemOperand(
1902 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1903 (VT == MVT::f32) ? 4 : 8, Align);
1904
Bill Schmidt03008132013-08-25 22:33:42 +00001905 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1906 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1907
Hal Finkele6698d52015-02-01 15:03:28 +00001908 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001909 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1910 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001911 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001912 TmpReg)
1913 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001914 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001915 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1916 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001917 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001918 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001919 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001920 // But for large code model, we must generate a LDtocL followed
1921 // by the LF[SD].
1922 if (CModel == CodeModel::Large) {
1923 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001924 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001925 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001927 .addImm(0).addReg(TmpReg2);
1928 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001929 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001930 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1931 .addReg(TmpReg)
1932 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001933 }
1934
1935 return DestReg;
1936}
1937
Bill Schmidtccecf262013-08-30 02:29:45 +00001938// Materialize the address of a global value into a register, and return
1939// the register number (or zero if we failed to handle it).
1940unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1941 assert(VT == MVT::i64 && "Non-address!");
1942 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1943 unsigned DestReg = createResultReg(RC);
1944
1945 // Global values may be plain old object addresses, TLS object
1946 // addresses, constant pool entries, or jump tables. How we generate
1947 // code for these may depend on small, medium, or large code model.
1948 CodeModel::Model CModel = TM.getCodeModel();
1949
1950 // FIXME: Jump tables are not yet required because fast-isel doesn't
1951 // handle switches; if that changes, we need them as well. For now,
1952 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001953
1954 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001955 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001956 return 0;
1957
Hal Finkele6698d52015-02-01 15:03:28 +00001958 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001959 // For small code model, generate a simple TOC load.
1960 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1962 DestReg)
1963 .addGlobalAddress(GV)
1964 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001965 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001966 // If the address is an externally defined symbol, a symbol with common
1967 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001968 // jump table address (not yet needed), or if we are generating code
1969 // for large code model, we generate:
1970 // LDtocL(GV, ADDIStocHA(%X2, GV))
1971 // Otherwise we generate:
1972 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1973 // Either way, start with the ADDIStocHA:
1974 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001976 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1977
Bill Schmidtccecf262013-08-30 02:29:45 +00001978 // If/when switches are implemented, jump tables should be handled
1979 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001980 if (CModel == CodeModel::Large ||
1981 (GV->getType()->getElementType()->isFunctionTy() &&
1982 (GV->isDeclaration() || GV->isWeakForLinker())) ||
1983 GV->isDeclaration() || GV->hasCommonLinkage() ||
1984 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001985 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001986 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1987 else
1988 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001990 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1991 }
1992
1993 return DestReg;
1994}
1995
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001996// Materialize a 32-bit integer constant into a register, and return
1997// the register number (or zero if we failed to handle it).
1998unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1999 const TargetRegisterClass *RC) {
2000 unsigned Lo = Imm & 0xFFFF;
2001 unsigned Hi = (Imm >> 16) & 0xFFFF;
2002
2003 unsigned ResultReg = createResultReg(RC);
2004 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2005
2006 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002008 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2009 .addImm(Imm);
2010 else if (Lo) {
2011 // Both Lo and Hi have nonzero bits.
2012 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002014 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2015 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002016 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002017 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2018 .addReg(TmpReg).addImm(Lo);
2019 } else
2020 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002021 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002022 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2023 .addImm(Hi);
2024
2025 return ResultReg;
2026}
2027
2028// Materialize a 64-bit integer constant into a register, and return
2029// the register number (or zero if we failed to handle it).
2030unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2031 const TargetRegisterClass *RC) {
2032 unsigned Remainder = 0;
2033 unsigned Shift = 0;
2034
2035 // If the value doesn't fit in 32 bits, see if we can shift it
2036 // so that it fits in 32 bits.
2037 if (!isInt<32>(Imm)) {
2038 Shift = countTrailingZeros<uint64_t>(Imm);
2039 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2040
2041 if (isInt<32>(ImmSh))
2042 Imm = ImmSh;
2043 else {
2044 Remainder = Imm;
2045 Shift = 32;
2046 Imm >>= 32;
2047 }
2048 }
2049
2050 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2051 // (if not shifted).
2052 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2053 if (!Shift)
2054 return TmpReg1;
2055
2056 // If upper 32 bits were not zero, we've built them and need to shift
2057 // them into place.
2058 unsigned TmpReg2;
2059 if (Imm) {
2060 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002062 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2063 } else
2064 TmpReg2 = TmpReg1;
2065
2066 unsigned TmpReg3, Hi, Lo;
2067 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2068 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002069 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002070 TmpReg3).addReg(TmpReg2).addImm(Hi);
2071 } else
2072 TmpReg3 = TmpReg2;
2073
2074 if ((Lo = Remainder & 0xFFFF)) {
2075 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002077 ResultReg).addReg(TmpReg3).addImm(Lo);
2078 return ResultReg;
2079 }
2080
2081 return TmpReg3;
2082}
2083
2084
2085// Materialize an integer constant into a register, and return
2086// the register number (or zero if we failed to handle it).
Samuel Antao61570df2014-09-17 23:25:06 +00002087unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT,
2088 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002089 // If we're using CR bit registers for i1 values, handle that as a special
2090 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002091 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002092 const ConstantInt *CI = cast<ConstantInt>(C);
2093 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2094 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2095 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2096 return ImmReg;
2097 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002098
2099 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2100 VT != MVT::i8 && VT != MVT::i1)
2101 return 0;
2102
2103 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2104 &PPC::GPRCRegClass);
2105
2106 // If the constant is in range, use a load-immediate.
2107 const ConstantInt *CI = cast<ConstantInt>(C);
2108 if (isInt<16>(CI->getSExtValue())) {
2109 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2110 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Samuel Antao61570df2014-09-17 23:25:06 +00002112 .addImm( (UseSExt) ? CI->getSExtValue() : CI->getZExtValue() );
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002113 return ImmReg;
2114 }
2115
2116 // Construct the constant piecewise.
2117 int64_t Imm = CI->getZExtValue();
2118
2119 if (VT == MVT::i64)
2120 return PPCMaterialize64BitInt(Imm, RC);
2121 else if (VT == MVT::i32)
2122 return PPCMaterialize32BitInt(Imm, RC);
2123
2124 return 0;
2125}
2126
2127// Materialize a constant into a register, and return the register
2128// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002129unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002130 EVT CEVT = TLI.getValueType(C->getType(), true);
2131
2132 // Only handle simple types.
2133 if (!CEVT.isSimple()) return 0;
2134 MVT VT = CEVT.getSimpleVT();
2135
2136 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2137 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002138 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2139 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002140 else if (isa<ConstantInt>(C))
Hal Finkel0c505b02014-12-25 23:08:25 +00002141 return PPCMaterializeInt(C, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002142
2143 return 0;
2144}
2145
2146// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002147// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002148unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002149 // Don't handle dynamic allocas.
2150 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2151
2152 MVT VT;
2153 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2154
2155 DenseMap<const AllocaInst*, int>::iterator SI =
2156 FuncInfo.StaticAllocaMap.find(AI);
2157
2158 if (SI != FuncInfo.StaticAllocaMap.end()) {
2159 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002161 ResultReg).addFrameIndex(SI->second).addImm(0);
2162 return ResultReg;
2163 }
2164
2165 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002166}
2167
Bill Schmidtccecf262013-08-30 02:29:45 +00002168// Fold loads into extends when possible.
2169// FIXME: We can have multiple redundant extend/trunc instructions
2170// following a load. The folding only picks up one. Extend this
2171// to check subsequent instructions for the same pattern and remove
2172// them. Thus ResultReg should be the def reg for the last redundant
2173// instruction in a chain, and all intervening instructions can be
2174// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2175// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002176bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2177 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002178 // Verify we have a legal type before going any further.
2179 MVT VT;
2180 if (!isLoadTypeLegal(LI->getType(), VT))
2181 return false;
2182
2183 // Combine load followed by zero- or sign-extend.
2184 bool IsZExt = false;
2185 switch(MI->getOpcode()) {
2186 default:
2187 return false;
2188
2189 case PPC::RLDICL:
2190 case PPC::RLDICL_32_64: {
2191 IsZExt = true;
2192 unsigned MB = MI->getOperand(3).getImm();
2193 if ((VT == MVT::i8 && MB <= 56) ||
2194 (VT == MVT::i16 && MB <= 48) ||
2195 (VT == MVT::i32 && MB <= 32))
2196 break;
2197 return false;
2198 }
2199
2200 case PPC::RLWINM:
2201 case PPC::RLWINM8: {
2202 IsZExt = true;
2203 unsigned MB = MI->getOperand(3).getImm();
2204 if ((VT == MVT::i8 && MB <= 24) ||
2205 (VT == MVT::i16 && MB <= 16))
2206 break;
2207 return false;
2208 }
2209
2210 case PPC::EXTSB:
2211 case PPC::EXTSB8:
2212 case PPC::EXTSB8_32_64:
2213 /* There is no sign-extending load-byte instruction. */
2214 return false;
2215
2216 case PPC::EXTSH:
2217 case PPC::EXTSH8:
2218 case PPC::EXTSH8_32_64: {
2219 if (VT != MVT::i16 && VT != MVT::i8)
2220 return false;
2221 break;
2222 }
2223
2224 case PPC::EXTSW:
2225 case PPC::EXTSW_32_64: {
2226 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2227 return false;
2228 break;
2229 }
2230 }
2231
2232 // See if we can handle this address.
2233 Address Addr;
2234 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2235 return false;
2236
2237 unsigned ResultReg = MI->getOperand(0).getReg();
2238
Craig Topper062a2ba2014-04-25 05:30:21 +00002239 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002240 return false;
2241
2242 MI->eraseFromParent();
2243 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002244}
2245
2246// Attempt to lower call arguments in a faster way than done by
2247// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002248bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002249 // Defer to normal argument lowering for now. It's reasonably
2250 // efficient. Consider doing something like ARM to handle the
2251 // case where all args fit in registers, no varargs, no float
2252 // or vector args.
2253 return false;
2254}
2255
Bill Schmidt03008132013-08-25 22:33:42 +00002256// Handle materializing integer constants into a register. This is not
2257// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002258unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002259
2260 if (Opc != ISD::Constant)
2261 return 0;
2262
Hal Finkel940ab932014-02-28 00:27:01 +00002263 // If we're using CR bit registers for i1 values, handle that as a special
2264 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002265 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002266 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2268 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2269 return ImmReg;
2270 }
2271
Bill Schmidt03008132013-08-25 22:33:42 +00002272 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2273 VT != MVT::i8 && VT != MVT::i1)
2274 return 0;
2275
2276 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2277 &PPC::GPRCRegClass);
2278 if (VT == MVT::i64)
2279 return PPCMaterialize64BitInt(Imm, RC);
2280 else
2281 return PPCMaterialize32BitInt(Imm, RC);
2282}
2283
Bill Schmidtccecf262013-08-30 02:29:45 +00002284// Override for ADDI and ADDI8 to set the correct register class
2285// on RHS operand 0. The automatic infrastructure naively assumes
2286// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2287// for these cases. At the moment, none of the other automatically
2288// generated RI instructions require special treatment. However, once
2289// SelectSelect is implemented, "isel" requires similar handling.
2290//
2291// Also be conservative about the output register class. Avoid
2292// assigning R0 or X0 to the output register for GPRC and G8RC
2293// register classes, as any such result could be used in ADDI, etc.,
2294// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002295unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002296 const TargetRegisterClass *RC,
2297 unsigned Op0, bool Op0IsKill,
2298 uint64_t Imm) {
2299 if (MachineInstOpcode == PPC::ADDI)
2300 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2301 else if (MachineInstOpcode == PPC::ADDI8)
2302 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2303
2304 const TargetRegisterClass *UseRC =
2305 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2306 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2307
Juergen Ributzka88e32512014-09-03 20:56:59 +00002308 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002309 Op0, Op0IsKill, Imm);
2310}
2311
2312// Override for instructions with one register operand to avoid use of
2313// R0/X0. The automatic infrastructure isn't aware of the context so
2314// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002315unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002316 const TargetRegisterClass* RC,
2317 unsigned Op0, bool Op0IsKill) {
2318 const TargetRegisterClass *UseRC =
2319 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2320 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2321
Juergen Ributzka88e32512014-09-03 20:56:59 +00002322 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002323}
2324
2325// Override for instructions with two register operands to avoid use
2326// of R0/X0. The automatic infrastructure isn't aware of the context
2327// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002328unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002329 const TargetRegisterClass* RC,
2330 unsigned Op0, bool Op0IsKill,
2331 unsigned Op1, bool Op1IsKill) {
2332 const TargetRegisterClass *UseRC =
2333 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2334 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2335
Juergen Ributzka88e32512014-09-03 20:56:59 +00002336 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002337 Op1, Op1IsKill);
2338}
2339
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002340namespace llvm {
2341 // Create the fast instruction selector for PowerPC64 ELF.
2342 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2343 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002344 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002345 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002346 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002347 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002348 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002349 }
2350}