blob: 700df20f8aa07e9c3bfb4a8cad6f4e646fc0b23a [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);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000167 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
168 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000169 unsigned PPCMaterialize32BitInt(int64_t Imm,
170 const TargetRegisterClass *RC);
171 unsigned PPCMaterialize64BitInt(int64_t Imm,
172 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000173 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
174 unsigned SrcReg, bool IsSigned);
175 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000176
Bill Schmidtd89f6782013-08-26 19:42:51 +0000177 // Call handling routines.
178 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000179 bool processCallArgs(SmallVectorImpl<Value*> &Args,
180 SmallVectorImpl<unsigned> &ArgRegs,
181 SmallVectorImpl<MVT> &ArgVTs,
182 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
183 SmallVectorImpl<unsigned> &RegArgs,
184 CallingConv::ID CC,
185 unsigned &NumBytes,
186 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000187 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000188 CCAssignFn *usePPC32CCs(unsigned Flag);
189
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000190 private:
191 #include "PPCGenFastISel.inc"
192
193};
194
195} // end anonymous namespace
196
Bill Schmidtd89f6782013-08-26 19:42:51 +0000197#include "PPCGenCallingConv.inc"
198
199// Function whose sole purpose is to kill compiler warnings
200// stemming from unused functions included from PPCGenCallingConv.inc.
201CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
202 if (Flag == 1)
203 return CC_PPC32_SVR4;
204 else if (Flag == 2)
205 return CC_PPC32_SVR4_ByVal;
206 else if (Flag == 3)
207 return CC_PPC32_SVR4_VarArg;
208 else
209 return RetCC_PPC;
210}
211
Bill Schmidt03008132013-08-25 22:33:42 +0000212static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
213 switch (Pred) {
214 // These are not representable with any single compare.
215 case CmpInst::FCMP_FALSE:
216 case CmpInst::FCMP_UEQ:
217 case CmpInst::FCMP_UGT:
218 case CmpInst::FCMP_UGE:
219 case CmpInst::FCMP_ULT:
220 case CmpInst::FCMP_ULE:
221 case CmpInst::FCMP_UNE:
222 case CmpInst::FCMP_TRUE:
223 default:
224 return Optional<PPC::Predicate>();
225
226 case CmpInst::FCMP_OEQ:
227 case CmpInst::ICMP_EQ:
228 return PPC::PRED_EQ;
229
230 case CmpInst::FCMP_OGT:
231 case CmpInst::ICMP_UGT:
232 case CmpInst::ICMP_SGT:
233 return PPC::PRED_GT;
234
235 case CmpInst::FCMP_OGE:
236 case CmpInst::ICMP_UGE:
237 case CmpInst::ICMP_SGE:
238 return PPC::PRED_GE;
239
240 case CmpInst::FCMP_OLT:
241 case CmpInst::ICMP_ULT:
242 case CmpInst::ICMP_SLT:
243 return PPC::PRED_LT;
244
245 case CmpInst::FCMP_OLE:
246 case CmpInst::ICMP_ULE:
247 case CmpInst::ICMP_SLE:
248 return PPC::PRED_LE;
249
250 case CmpInst::FCMP_ONE:
251 case CmpInst::ICMP_NE:
252 return PPC::PRED_NE;
253
254 case CmpInst::FCMP_ORD:
255 return PPC::PRED_NU;
256
257 case CmpInst::FCMP_UNO:
258 return PPC::PRED_UN;
259 }
260}
261
Bill Schmidtccecf262013-08-30 02:29:45 +0000262// Determine whether the type Ty is simple enough to be handled by
263// fast-isel, and return its equivalent machine type in VT.
264// FIXME: Copied directly from ARM -- factor into base class?
265bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000266 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000267
268 // Only handle simple types.
269 if (Evt == MVT::Other || !Evt.isSimple()) return false;
270 VT = Evt.getSimpleVT();
271
272 // Handle all legal types, i.e. a register that will directly hold this
273 // value.
274 return TLI.isTypeLegal(VT);
275}
276
277// Determine whether the type Ty is simple enough to be handled by
278// fast-isel as a load target, and return its equivalent machine type in VT.
279bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
280 if (isTypeLegal(Ty, VT)) return true;
281
282 // If this is a type than can be sign or zero-extended to a basic operation
283 // go ahead and accept it now.
284 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
285 return true;
286 }
287
288 return false;
289}
290
Hal Finkel5f2a1372015-05-23 12:18:10 +0000291bool PPCFastISel::isValueAvailable(const Value *V) const {
292 if (!isa<Instruction>(V))
293 return true;
294
295 const auto *I = cast<Instruction>(V);
296 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
297 return true;
298
299 return false;
300}
301
Bill Schmidtccecf262013-08-30 02:29:45 +0000302// Given a value Obj, create an Address object Addr that represents its
303// address. Return false if we can't handle it.
304bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000305 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000306 unsigned Opcode = Instruction::UserOp1;
307 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
308 // Don't walk into other basic blocks unless the object is an alloca from
309 // another block, otherwise it may not have a virtual register assigned.
310 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
311 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
312 Opcode = I->getOpcode();
313 U = I;
314 }
315 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
316 Opcode = C->getOpcode();
317 U = C;
318 }
319
320 switch (Opcode) {
321 default:
322 break;
323 case Instruction::BitCast:
324 // Look through bitcasts.
325 return PPCComputeAddress(U->getOperand(0), Addr);
326 case Instruction::IntToPtr:
327 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000328 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
329 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000330 return PPCComputeAddress(U->getOperand(0), Addr);
331 break;
332 case Instruction::PtrToInt:
333 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000334 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000335 return PPCComputeAddress(U->getOperand(0), Addr);
336 break;
337 case Instruction::GetElementPtr: {
338 Address SavedAddr = Addr;
339 long TmpOffset = Addr.Offset;
340
341 // Iterate through the GEP folding the constants into offsets where
342 // we can.
343 gep_type_iterator GTI = gep_type_begin(U);
344 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
345 II != IE; ++II, ++GTI) {
346 const Value *Op = *II;
347 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000348 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000349 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
350 TmpOffset += SL->getElementOffset(Idx);
351 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000352 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000353 for (;;) {
354 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
355 // Constant-offset addressing.
356 TmpOffset += CI->getSExtValue() * S;
357 break;
358 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000359 if (canFoldAddIntoGEP(U, Op)) {
360 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000361 ConstantInt *CI =
362 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
363 TmpOffset += CI->getSExtValue() * S;
364 // Iterate on the other operand.
365 Op = cast<AddOperator>(Op)->getOperand(0);
366 continue;
367 }
368 // Unsupported
369 goto unsupported_gep;
370 }
371 }
372 }
373
374 // Try to grab the base operand now.
375 Addr.Offset = TmpOffset;
376 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
377
378 // We failed, restore everything and try the other options.
379 Addr = SavedAddr;
380
381 unsupported_gep:
382 break;
383 }
384 case Instruction::Alloca: {
385 const AllocaInst *AI = cast<AllocaInst>(Obj);
386 DenseMap<const AllocaInst*, int>::iterator SI =
387 FuncInfo.StaticAllocaMap.find(AI);
388 if (SI != FuncInfo.StaticAllocaMap.end()) {
389 Addr.BaseType = Address::FrameIndexBase;
390 Addr.Base.FI = SI->second;
391 return true;
392 }
393 break;
394 }
395 }
396
397 // FIXME: References to parameters fall through to the behavior
398 // below. They should be able to reference a frame index since
399 // they are stored to the stack, so we can get "ld rx, offset(r1)"
400 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
401 // just contain the parameter. Try to handle this with a FI.
402
403 // Try to get this in a register if nothing else has worked.
404 if (Addr.Base.Reg == 0)
405 Addr.Base.Reg = getRegForValue(Obj);
406
407 // Prevent assignment of base register to X0, which is inappropriate
408 // for loads and stores alike.
409 if (Addr.Base.Reg != 0)
410 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
411
412 return Addr.Base.Reg != 0;
413}
414
415// Fix up some addresses that can't be used directly. For example, if
416// an offset won't fit in an instruction field, we may need to move it
417// into an index register.
418void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
419 unsigned &IndexReg) {
420
421 // Check whether the offset fits in the instruction field.
422 if (!isInt<16>(Addr.Offset))
423 UseOffset = false;
424
425 // If this is a stack pointer and the offset needs to be simplified then
426 // put the alloca address into a register, set the base type back to
427 // register and continue. This should almost never happen.
428 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
429 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000431 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
432 Addr.Base.Reg = ResultReg;
433 Addr.BaseType = Address::RegBase;
434 }
435
436 if (!UseOffset) {
437 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
438 : Type::getInt64Ty(*Context));
439 const ConstantInt *Offset =
440 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
441 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
442 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
443 }
444}
445
446// Emit a load instruction if possible, returning true if we succeeded,
447// otherwise false. See commentary below for how the register class of
448// the load is determined.
449bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
450 const TargetRegisterClass *RC,
451 bool IsZExt, unsigned FP64LoadOpc) {
452 unsigned Opc;
453 bool UseOffset = true;
454
455 // If ResultReg is given, it determines the register class of the load.
456 // Otherwise, RC is the register class to use. If the result of the
457 // load isn't anticipated in this block, both may be zero, in which
458 // case we must make a conservative guess. In particular, don't assign
459 // R0 or X0 to the result register, as the result may be used in a load,
460 // store, add-immediate, or isel that won't permit this. (Though
461 // perhaps the spill and reload of live-exit values would handle this?)
462 const TargetRegisterClass *UseRC =
463 (ResultReg ? MRI.getRegClass(ResultReg) :
464 (RC ? RC :
465 (VT == MVT::f64 ? &PPC::F8RCRegClass :
466 (VT == MVT::f32 ? &PPC::F4RCRegClass :
467 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
468 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
469
470 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
471
472 switch (VT.SimpleTy) {
473 default: // e.g., vector types not handled
474 return false;
475 case MVT::i8:
476 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
477 break;
478 case MVT::i16:
479 Opc = (IsZExt ?
480 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
481 (Is32BitInt ? PPC::LHA : PPC::LHA8));
482 break;
483 case MVT::i32:
484 Opc = (IsZExt ?
485 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
486 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
487 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
488 UseOffset = false;
489 break;
490 case MVT::i64:
491 Opc = PPC::LD;
492 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
493 "64-bit load with 32-bit target??");
494 UseOffset = ((Addr.Offset & 3) == 0);
495 break;
496 case MVT::f32:
497 Opc = PPC::LFS;
498 break;
499 case MVT::f64:
500 Opc = FP64LoadOpc;
501 break;
502 }
503
504 // If necessary, materialize the offset into a register and use
505 // the indexed form. Also handle stack pointers with special needs.
506 unsigned IndexReg = 0;
507 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000508
509 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
510 // be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000511 bool IsVSSRC = (ResultReg != 0) && isVSSRCRegister(ResultReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000512 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000513 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
514 bool Is64VSXLoad = IsVSSRC && Opc == PPC::LFD;
515 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000516 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
517 (Addr.Offset == 0)) {
518 UseOffset = false;
519 }
520
Bill Schmidtccecf262013-08-30 02:29:45 +0000521 if (ResultReg == 0)
522 ResultReg = createResultReg(UseRC);
523
524 // Note: If we still have a frame index here, we know the offset is
525 // in range, as otherwise PPCSimplifyAddress would have converted it
526 // into a RegBase.
527 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000528 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000529 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000530
531 MachineMemOperand *MMO =
532 FuncInfo.MF->getMachineMemOperand(
533 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
534 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
535 MFI.getObjectAlignment(Addr.Base.FI));
536
Rafael Espindolaea09c592014-02-18 22:05:46 +0000537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000538 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
539
540 // Base reg with offset in range.
541 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000542 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000543 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000544
Rafael Espindolaea09c592014-02-18 22:05:46 +0000545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000546 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
547
548 // Indexed form.
549 } else {
550 // Get the RR opcode corresponding to the RI one. FIXME: It would be
551 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
552 // is hard to get at.
553 switch (Opc) {
554 default: llvm_unreachable("Unexpected opcode!");
555 case PPC::LBZ: Opc = PPC::LBZX; break;
556 case PPC::LBZ8: Opc = PPC::LBZX8; break;
557 case PPC::LHZ: Opc = PPC::LHZX; break;
558 case PPC::LHZ8: Opc = PPC::LHZX8; break;
559 case PPC::LHA: Opc = PPC::LHAX; break;
560 case PPC::LHA8: Opc = PPC::LHAX8; break;
561 case PPC::LWZ: Opc = PPC::LWZX; break;
562 case PPC::LWZ8: Opc = PPC::LWZX8; break;
563 case PPC::LWA: Opc = PPC::LWAX; break;
564 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
565 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000566 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000567 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000568 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000570 .addReg(Addr.Base.Reg).addReg(IndexReg);
571 }
572
573 return true;
574}
575
576// Attempt to fast-select a load instruction.
577bool PPCFastISel::SelectLoad(const Instruction *I) {
578 // FIXME: No atomic loads are supported.
579 if (cast<LoadInst>(I)->isAtomic())
580 return false;
581
582 // Verify we have a legal type before going any further.
583 MVT VT;
584 if (!isLoadTypeLegal(I->getType(), VT))
585 return false;
586
587 // See if we can handle this address.
588 Address Addr;
589 if (!PPCComputeAddress(I->getOperand(0), Addr))
590 return false;
591
592 // Look at the currently assigned register for this instruction
593 // to determine the required register class. This is necessary
594 // to constrain RA from using R0/X0 when this is not legal.
595 unsigned AssignedReg = FuncInfo.ValueMap[I];
596 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000597 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000598
599 unsigned ResultReg = 0;
600 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
601 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000602 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000603 return true;
604}
605
606// Emit a store instruction to store SrcReg at Addr.
607bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
608 assert(SrcReg && "Nothing to store!");
609 unsigned Opc;
610 bool UseOffset = true;
611
612 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
613 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
614
615 switch (VT.SimpleTy) {
616 default: // e.g., vector types not handled
617 return false;
618 case MVT::i8:
619 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
620 break;
621 case MVT::i16:
622 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
623 break;
624 case MVT::i32:
625 assert(Is32BitInt && "Not GPRC for i32??");
626 Opc = PPC::STW;
627 break;
628 case MVT::i64:
629 Opc = PPC::STD;
630 UseOffset = ((Addr.Offset & 3) == 0);
631 break;
632 case MVT::f32:
633 Opc = PPC::STFS;
634 break;
635 case MVT::f64:
636 Opc = PPC::STFD;
637 break;
638 }
639
640 // If necessary, materialize the offset into a register and use
641 // the indexed form. Also handle stack pointers with special needs.
642 unsigned IndexReg = 0;
643 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
644
Bill Seurer8c728ae2014-12-05 20:15:56 +0000645 // If this is a potential VSX store with an offset of 0, a VSX indexed store
646 // can be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000647 bool IsVSSRC = isVSSRCRegister(SrcReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000648 bool IsVSFRC = isVSFRCRegister(SrcReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000649 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
650 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
651 if ((Is32VSXStore || Is64VSXStore) &&
652 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000653 (Addr.Offset == 0)) {
654 UseOffset = false;
655 }
656
Bill Schmidtccecf262013-08-30 02:29:45 +0000657 // Note: If we still have a frame index here, we know the offset is
658 // in range, as otherwise PPCSimplifyAddress would have converted it
659 // into a RegBase.
660 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000661 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000662 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000663
Bill Schmidtccecf262013-08-30 02:29:45 +0000664 MachineMemOperand *MMO =
665 FuncInfo.MF->getMachineMemOperand(
666 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
667 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
668 MFI.getObjectAlignment(Addr.Base.FI));
669
Rafael Espindolaea09c592014-02-18 22:05:46 +0000670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
671 .addReg(SrcReg)
672 .addImm(Addr.Offset)
673 .addFrameIndex(Addr.Base.FI)
674 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000675
676 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000677 } else if (UseOffset) {
678 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000679 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000680
Rafael Espindolaea09c592014-02-18 22:05:46 +0000681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000682 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
683
684 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000685 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000686 // Get the RR opcode corresponding to the RI one. FIXME: It would be
687 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
688 // is hard to get at.
689 switch (Opc) {
690 default: llvm_unreachable("Unexpected opcode!");
691 case PPC::STB: Opc = PPC::STBX; break;
692 case PPC::STH : Opc = PPC::STHX; break;
693 case PPC::STW : Opc = PPC::STWX; break;
694 case PPC::STB8: Opc = PPC::STBX8; break;
695 case PPC::STH8: Opc = PPC::STHX8; break;
696 case PPC::STW8: Opc = PPC::STWX8; break;
697 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000698 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000699 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000700 }
Samuel Antaof6815602015-03-17 15:00:57 +0000701
702 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
703 .addReg(SrcReg);
704
705 // If we have an index register defined we use it in the store inst,
706 // otherwise we use X0 as base as it makes the vector instructions to
707 // use zero in the computation of the effective address regardless the
708 // content of the register.
709 if (IndexReg)
710 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
711 else
712 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000713 }
714
715 return true;
716}
717
718// Attempt to fast-select a store instruction.
719bool PPCFastISel::SelectStore(const Instruction *I) {
720 Value *Op0 = I->getOperand(0);
721 unsigned SrcReg = 0;
722
723 // FIXME: No atomics loads are supported.
724 if (cast<StoreInst>(I)->isAtomic())
725 return false;
726
727 // Verify we have a legal type before going any further.
728 MVT VT;
729 if (!isLoadTypeLegal(Op0->getType(), VT))
730 return false;
731
732 // Get the value to be stored into a register.
733 SrcReg = getRegForValue(Op0);
734 if (SrcReg == 0)
735 return false;
736
737 // See if we can handle this address.
738 Address Addr;
739 if (!PPCComputeAddress(I->getOperand(1), Addr))
740 return false;
741
742 if (!PPCEmitStore(VT, SrcReg, Addr))
743 return false;
744
745 return true;
746}
747
Bill Schmidt03008132013-08-25 22:33:42 +0000748// Attempt to fast-select a branch instruction.
749bool PPCFastISel::SelectBranch(const Instruction *I) {
750 const BranchInst *BI = cast<BranchInst>(I);
751 MachineBasicBlock *BrBB = FuncInfo.MBB;
752 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
753 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
754
755 // For now, just try the simplest case where it's fed by a compare.
756 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000757 if (isValueAvailable(CI)) {
758 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
759 if (!OptPPCPred)
760 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000761
Hal Finkel5f2a1372015-05-23 12:18:10 +0000762 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000763
Hal Finkel5f2a1372015-05-23 12:18:10 +0000764 // Take advantage of fall-through opportunities.
765 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
766 std::swap(TBB, FBB);
767 PPCPred = PPC::InvertPredicate(PPCPred);
768 }
769
770 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
771
772 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
773 CondReg))
774 return false;
775
776 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
777 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
778 fastEmitBranch(FBB, DbgLoc);
779 FuncInfo.MBB->addSuccessor(TBB);
780 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000781 }
Bill Schmidt03008132013-08-25 22:33:42 +0000782 } else if (const ConstantInt *CI =
783 dyn_cast<ConstantInt>(BI->getCondition())) {
784 uint64_t Imm = CI->getZExtValue();
785 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000786 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000787 return true;
788 }
789
790 // FIXME: ARM looks for a case where the block containing the compare
791 // has been split from the block containing the branch. If this happens,
792 // there is a vreg available containing the result of the compare. I'm
793 // not sure we can do much, as we've lost the predicate information with
794 // the compare instruction -- we have a 4-bit CR but don't know which bit
795 // to test here.
796 return false;
797}
798
799// Attempt to emit a compare of the two source values. Signed and unsigned
800// comparisons are supported. Return false if we can't handle it.
801bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
802 bool IsZExt, unsigned DestReg) {
803 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000804 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000805 if (!SrcEVT.isSimple())
806 return false;
807 MVT SrcVT = SrcEVT.getSimpleVT();
808
Eric Christopher1b8e7632014-05-22 01:07:24 +0000809 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000810 return false;
811
Bill Schmidt03008132013-08-25 22:33:42 +0000812 // See if operand 2 is an immediate encodeable in the compare.
813 // FIXME: Operands are not in canonical order at -O0, so an immediate
814 // operand in position 1 is a lost opportunity for now. We are
815 // similar to ARM in this regard.
816 long Imm = 0;
817 bool UseImm = false;
818
819 // Only 16-bit integer constants can be represented in compares for
820 // PowerPC. Others will be materialized into a register.
821 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
822 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
823 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
824 const APInt &CIVal = ConstInt->getValue();
825 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
826 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
827 UseImm = true;
828 }
829 }
830
831 unsigned CmpOpc;
832 bool NeedsExt = false;
833 switch (SrcVT.SimpleTy) {
834 default: return false;
835 case MVT::f32:
836 CmpOpc = PPC::FCMPUS;
837 break;
838 case MVT::f64:
839 CmpOpc = PPC::FCMPUD;
840 break;
841 case MVT::i1:
842 case MVT::i8:
843 case MVT::i16:
844 NeedsExt = true;
845 // Intentional fall-through.
846 case MVT::i32:
847 if (!UseImm)
848 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
849 else
850 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
851 break;
852 case MVT::i64:
853 if (!UseImm)
854 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
855 else
856 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
857 break;
858 }
859
860 unsigned SrcReg1 = getRegForValue(SrcValue1);
861 if (SrcReg1 == 0)
862 return false;
863
864 unsigned SrcReg2 = 0;
865 if (!UseImm) {
866 SrcReg2 = getRegForValue(SrcValue2);
867 if (SrcReg2 == 0)
868 return false;
869 }
870
871 if (NeedsExt) {
872 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
873 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
874 return false;
875 SrcReg1 = ExtReg;
876
877 if (!UseImm) {
878 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
879 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
880 return false;
881 SrcReg2 = ExtReg;
882 }
883 }
884
885 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000886 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000887 .addReg(SrcReg1).addReg(SrcReg2);
888 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000890 .addReg(SrcReg1).addImm(Imm);
891
892 return true;
893}
894
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000895// Attempt to fast-select a floating-point extend instruction.
896bool PPCFastISel::SelectFPExt(const Instruction *I) {
897 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000898 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
899 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000900
901 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
902 return false;
903
904 unsigned SrcReg = getRegForValue(Src);
905 if (!SrcReg)
906 return false;
907
908 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000909 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000910 return true;
911}
912
913// Attempt to fast-select a floating-point truncate instruction.
914bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
915 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000916 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
917 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000918
919 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
920 return false;
921
922 unsigned SrcReg = getRegForValue(Src);
923 if (!SrcReg)
924 return false;
925
926 // Round the result to single precision.
927 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000929 .addReg(SrcReg);
930
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000931 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000932 return true;
933}
934
935// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000936// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000937// those should be used instead of moving via a stack slot when the
938// subtarget permits.
939// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
940// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
941// case to 8 bytes which produces tighter code but wastes stack space.
942unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
943 bool IsSigned) {
944
945 // If necessary, extend 32-bit int to 64-bit.
946 if (SrcVT == MVT::i32) {
947 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
948 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
949 return 0;
950 SrcReg = TmpReg;
951 }
952
953 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
954 Address Addr;
955 Addr.BaseType = Address::FrameIndexBase;
956 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
957
958 // Store the value from the GPR.
959 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
960 return 0;
961
962 // Load the integer value into an FPR. The kind of load used depends
963 // on a number of conditions.
964 unsigned LoadOpc = PPC::LFD;
965
966 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000967 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000968 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000969 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000970 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000971 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000972 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000973 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000974 }
975
976 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
977 unsigned ResultReg = 0;
978 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
979 return 0;
980
981 return ResultReg;
982}
983
984// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000985// FIXME: Once fast-isel has better support for VSX, conversions using
986// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000987bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
988 MVT DstVT;
989 Type *DstTy = I->getType();
990 if (!isTypeLegal(DstTy, DstVT))
991 return false;
992
993 if (DstVT != MVT::f32 && DstVT != MVT::f64)
994 return false;
995
996 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000997 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000998 if (!SrcEVT.isSimple())
999 return false;
1000
1001 MVT SrcVT = SrcEVT.getSimpleVT();
1002
1003 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1004 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1005 return false;
1006
1007 unsigned SrcReg = getRegForValue(Src);
1008 if (SrcReg == 0)
1009 return false;
1010
1011 // We can only lower an unsigned convert if we have the newer
1012 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001013 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001014 return false;
1015
1016 // FIXME: For now we require the newer floating-point conversion operations
1017 // (which are present only on P7 and A2 server models) when converting
1018 // to single-precision float. Otherwise we have to generate a lot of
1019 // fiddly code to avoid double rounding. If necessary, the fiddly code
1020 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001021 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001022 return false;
1023
1024 // Extend the input if necessary.
1025 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1026 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1027 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1028 return false;
1029 SrcVT = MVT::i64;
1030 SrcReg = TmpReg;
1031 }
1032
1033 // Move the integer value to an FPR.
1034 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1035 if (FPReg == 0)
1036 return false;
1037
1038 // Determine the opcode for the conversion.
1039 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1040 unsigned DestReg = createResultReg(RC);
1041 unsigned Opc;
1042
1043 if (DstVT == MVT::f32)
1044 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1045 else
1046 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1047
1048 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001050 .addReg(FPReg);
1051
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001052 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001053 return true;
1054}
1055
1056// Move the floating-point value in SrcReg into an integer destination
1057// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001058// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001059// those should be used instead of moving via a stack slot when the
1060// subtarget permits.
1061unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1062 unsigned SrcReg, bool IsSigned) {
1063 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1064 // Note that if have STFIWX available, we could use a 4-byte stack
1065 // slot for i32, but this being fast-isel we'll just go with the
1066 // easiest code gen possible.
1067 Address Addr;
1068 Addr.BaseType = Address::FrameIndexBase;
1069 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1070
1071 // Store the value from the FPR.
1072 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1073 return 0;
1074
1075 // Reload it into a GPR. If we want an i32, modify the address
1076 // to have a 4-byte offset so we load from the right place.
1077 if (VT == MVT::i32)
1078 Addr.Offset = 4;
1079
1080 // Look at the currently assigned register for this instruction
1081 // to determine the required register class.
1082 unsigned AssignedReg = FuncInfo.ValueMap[I];
1083 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001084 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001085
1086 unsigned ResultReg = 0;
1087 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1088 return 0;
1089
1090 return ResultReg;
1091}
1092
1093// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001094// FIXME: Once fast-isel has better support for VSX, conversions using
1095// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001096bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1097 MVT DstVT, SrcVT;
1098 Type *DstTy = I->getType();
1099 if (!isTypeLegal(DstTy, DstVT))
1100 return false;
1101
1102 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1103 return false;
1104
Bill Schmidt83973ef2014-06-24 20:05:18 +00001105 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1106 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1107 return false;
1108
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001109 Value *Src = I->getOperand(0);
1110 Type *SrcTy = Src->getType();
1111 if (!isTypeLegal(SrcTy, SrcVT))
1112 return false;
1113
1114 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1115 return false;
1116
1117 unsigned SrcReg = getRegForValue(Src);
1118 if (SrcReg == 0)
1119 return false;
1120
1121 // Convert f32 to f64 if necessary. This is just a meaningless copy
1122 // to get the register class right. COPY_TO_REGCLASS is needed since
1123 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1124 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1125 if (InRC == &PPC::F4RCRegClass) {
1126 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001128 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1129 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1130 SrcReg = TmpReg;
1131 }
1132
1133 // Determine the opcode for the conversion, which takes place
1134 // entirely within FPRs.
1135 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1136 unsigned Opc;
1137
1138 if (DstVT == MVT::i32)
1139 if (IsSigned)
1140 Opc = PPC::FCTIWZ;
1141 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001142 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001143 else
1144 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1145
1146 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001148 .addReg(SrcReg);
1149
1150 // Now move the integer value from a float register to an integer register.
1151 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1152 if (IntReg == 0)
1153 return false;
1154
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001155 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001156 return true;
1157}
1158
Bill Schmidtccecf262013-08-30 02:29:45 +00001159// Attempt to fast-select a binary integer operation that isn't already
1160// handled automatically.
1161bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001162 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001163
1164 // We can get here in the case when we have a binary operation on a non-legal
1165 // type and the target independent selector doesn't know how to handle it.
1166 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1167 return false;
1168
1169 // Look at the currently assigned register for this instruction
1170 // to determine the required register class. If there is no register,
1171 // make a conservative choice (don't assign R0).
1172 unsigned AssignedReg = FuncInfo.ValueMap[I];
1173 const TargetRegisterClass *RC =
1174 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1175 &PPC::GPRC_and_GPRC_NOR0RegClass);
1176 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1177
1178 unsigned Opc;
1179 switch (ISDOpcode) {
1180 default: return false;
1181 case ISD::ADD:
1182 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1183 break;
1184 case ISD::OR:
1185 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1186 break;
1187 case ISD::SUB:
1188 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1189 break;
1190 }
1191
1192 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1193 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1194 if (SrcReg1 == 0) return false;
1195
1196 // Handle case of small immediate operand.
1197 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1198 const APInt &CIVal = ConstInt->getValue();
1199 int Imm = (int)CIVal.getSExtValue();
1200 bool UseImm = true;
1201 if (isInt<16>(Imm)) {
1202 switch (Opc) {
1203 default:
1204 llvm_unreachable("Missing case!");
1205 case PPC::ADD4:
1206 Opc = PPC::ADDI;
1207 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1208 break;
1209 case PPC::ADD8:
1210 Opc = PPC::ADDI8;
1211 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1212 break;
1213 case PPC::OR:
1214 Opc = PPC::ORI;
1215 break;
1216 case PPC::OR8:
1217 Opc = PPC::ORI8;
1218 break;
1219 case PPC::SUBF:
1220 if (Imm == -32768)
1221 UseImm = false;
1222 else {
1223 Opc = PPC::ADDI;
1224 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1225 Imm = -Imm;
1226 }
1227 break;
1228 case PPC::SUBF8:
1229 if (Imm == -32768)
1230 UseImm = false;
1231 else {
1232 Opc = PPC::ADDI8;
1233 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1234 Imm = -Imm;
1235 }
1236 break;
1237 }
1238
1239 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1241 ResultReg)
1242 .addReg(SrcReg1)
1243 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001244 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001245 return true;
1246 }
1247 }
1248 }
1249
1250 // Reg-reg case.
1251 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1252 if (SrcReg2 == 0) return false;
1253
1254 // Reverse operands for subtract-from.
1255 if (ISDOpcode == ISD::SUB)
1256 std::swap(SrcReg1, SrcReg2);
1257
Rafael Espindolaea09c592014-02-18 22:05:46 +00001258 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001259 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001260 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001261 return true;
1262}
1263
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001264// Handle arguments to a call that we're attempting to fast-select.
1265// Return false if the arguments are too complex for us at the moment.
1266bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1267 SmallVectorImpl<unsigned> &ArgRegs,
1268 SmallVectorImpl<MVT> &ArgVTs,
1269 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1270 SmallVectorImpl<unsigned> &RegArgs,
1271 CallingConv::ID CC,
1272 unsigned &NumBytes,
1273 bool IsVarArg) {
1274 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001275 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001276
1277 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001278 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001279 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001280
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001281 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1282
1283 // Bail out if we can't handle any of the arguments.
1284 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1285 CCValAssign &VA = ArgLocs[I];
1286 MVT ArgVT = ArgVTs[VA.getValNo()];
1287
1288 // Skip vector arguments for now, as well as long double and
1289 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001290 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001291 !VA.isRegLoc() || VA.needsCustom())
1292 return false;
1293
1294 // Skip bit-converted arguments for now.
1295 if (VA.getLocInfo() == CCValAssign::BCvt)
1296 return false;
1297 }
1298
1299 // Get a count of how many bytes are to be pushed onto the stack.
1300 NumBytes = CCInfo.getNextStackOffset();
1301
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001302 // The prolog code of the callee may store up to 8 GPR argument registers to
1303 // the stack, allowing va_start to index over them in memory if its varargs.
1304 // Because we cannot tell if this is needed on the caller side, we have to
1305 // conservatively assume that it is needed. As such, make sure we have at
1306 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001307 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001308 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001309
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001310 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001312 TII.get(TII.getCallFrameSetupOpcode()))
1313 .addImm(NumBytes);
1314
1315 // Prepare to assign register arguments. Every argument uses up a
1316 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001317 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001318 unsigned NextGPR = PPC::X3;
1319 unsigned NextFPR = PPC::F1;
1320
1321 // Process arguments.
1322 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1323 CCValAssign &VA = ArgLocs[I];
1324 unsigned Arg = ArgRegs[VA.getValNo()];
1325 MVT ArgVT = ArgVTs[VA.getValNo()];
1326
1327 // Handle argument promotion and bitcasts.
1328 switch (VA.getLocInfo()) {
1329 default:
1330 llvm_unreachable("Unknown loc info!");
1331 case CCValAssign::Full:
1332 break;
1333 case CCValAssign::SExt: {
1334 MVT DestVT = VA.getLocVT();
1335 const TargetRegisterClass *RC =
1336 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1337 unsigned TmpReg = createResultReg(RC);
1338 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1339 llvm_unreachable("Failed to emit a sext!");
1340 ArgVT = DestVT;
1341 Arg = TmpReg;
1342 break;
1343 }
1344 case CCValAssign::AExt:
1345 case CCValAssign::ZExt: {
1346 MVT DestVT = VA.getLocVT();
1347 const TargetRegisterClass *RC =
1348 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1349 unsigned TmpReg = createResultReg(RC);
1350 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1351 llvm_unreachable("Failed to emit a zext!");
1352 ArgVT = DestVT;
1353 Arg = TmpReg;
1354 break;
1355 }
1356 case CCValAssign::BCvt: {
1357 // FIXME: Not yet handled.
1358 llvm_unreachable("Should have bailed before getting here!");
1359 break;
1360 }
1361 }
1362
1363 // Copy this argument to the appropriate register.
1364 unsigned ArgReg;
1365 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1366 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001367 if (CC != CallingConv::Fast)
1368 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001369 } else
1370 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001371
1372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1373 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001374 RegArgs.push_back(ArgReg);
1375 }
1376
1377 return true;
1378}
1379
1380// For a call that we've determined we can fast-select, finish the
1381// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001382bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1383 CallingConv::ID CC = CLI.CallConv;
1384
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001385 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001386 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001387 TII.get(TII.getCallFrameDestroyOpcode()))
1388 .addImm(NumBytes).addImm(0);
1389
1390 // Next, generate a copy to obtain the return value.
1391 // FIXME: No multi-register return values yet, though I don't foresee
1392 // any real difficulties there.
1393 if (RetVT != MVT::isVoid) {
1394 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001395 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001396 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1397 CCValAssign &VA = RVLocs[0];
1398 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1399 assert(VA.isRegLoc() && "Can only return in registers!");
1400
1401 MVT DestVT = VA.getValVT();
1402 MVT CopyVT = DestVT;
1403
1404 // Ints smaller than a register still arrive in a full 64-bit
1405 // register, so make sure we recognize this.
1406 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1407 CopyVT = MVT::i64;
1408
1409 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001410 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001411
1412 if (RetVT == CopyVT) {
1413 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1414 ResultReg = createResultReg(CpyRC);
1415
Rafael Espindolaea09c592014-02-18 22:05:46 +00001416 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001417 TII.get(TargetOpcode::COPY), ResultReg)
1418 .addReg(SourcePhysReg);
1419
1420 // If necessary, round the floating result to single precision.
1421 } else if (CopyVT == MVT::f64) {
1422 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001424 ResultReg).addReg(SourcePhysReg);
1425
1426 // If only the low half of a general register is needed, generate
1427 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1428 // used along the fast-isel path (not lowered), and downstream logic
1429 // also doesn't like a direct subreg copy on a physical reg.)
1430 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1431 ResultReg = createResultReg(&PPC::GPRCRegClass);
1432 // Convert physical register from G8RC to GPRC.
1433 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001434 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001435 TII.get(TargetOpcode::COPY), ResultReg)
1436 .addReg(SourcePhysReg);
1437 }
1438
Bill Schmidt0954ea12013-08-30 23:25:30 +00001439 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001440 CLI.InRegs.push_back(SourcePhysReg);
1441 CLI.ResultReg = ResultReg;
1442 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001443 }
Hal Finkel934361a2015-01-14 01:07:51 +00001444
1445 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001446}
1447
Hal Finkel934361a2015-01-14 01:07:51 +00001448bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1449 CallingConv::ID CC = CLI.CallConv;
1450 bool IsTailCall = CLI.IsTailCall;
1451 bool IsVarArg = CLI.IsVarArg;
1452 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001453 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001454
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001455 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001456 return false;
1457
1458 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001459 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001460 return false;
1461
Hal Finkel934361a2015-01-14 01:07:51 +00001462 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001463 if (IsVarArg)
1464 return false;
1465
1466 // Handle simple calls for now, with legal return types and
1467 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001468 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001469 MVT RetVT;
1470 if (RetTy->isVoidTy())
1471 RetVT = MVT::isVoid;
1472 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1473 RetVT != MVT::i8)
1474 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001475 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1476 // We can't handle boolean returns when CR bits are in use.
1477 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001478
1479 // FIXME: No multi-register return values yet.
1480 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1481 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1482 RetVT != MVT::f64) {
1483 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001484 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001485 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1486 if (RVLocs.size() > 1)
1487 return false;
1488 }
1489
1490 // Bail early if more than 8 arguments, as we only currently
1491 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001492 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001493 if (NumArgs > 8)
1494 return false;
1495
1496 // Set up the argument vectors.
1497 SmallVector<Value*, 8> Args;
1498 SmallVector<unsigned, 8> ArgRegs;
1499 SmallVector<MVT, 8> ArgVTs;
1500 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1501
1502 Args.reserve(NumArgs);
1503 ArgRegs.reserve(NumArgs);
1504 ArgVTs.reserve(NumArgs);
1505 ArgFlags.reserve(NumArgs);
1506
Hal Finkel934361a2015-01-14 01:07:51 +00001507 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001508 // Only handle easy calls for now. It would be reasonably easy
1509 // to handle <= 8-byte structures passed ByVal in registers, but we
1510 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001511 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1512 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001513 return false;
1514
Hal Finkel934361a2015-01-14 01:07:51 +00001515 Value *ArgValue = CLI.OutVals[i];
1516 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001517 MVT ArgVT;
1518 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1519 return false;
1520
1521 if (ArgVT.isVector())
1522 return false;
1523
Hal Finkel934361a2015-01-14 01:07:51 +00001524 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001525 if (Arg == 0)
1526 return false;
1527
Hal Finkel934361a2015-01-14 01:07:51 +00001528 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001529 ArgRegs.push_back(Arg);
1530 ArgVTs.push_back(ArgVT);
1531 ArgFlags.push_back(Flags);
1532 }
1533
1534 // Process the arguments.
1535 SmallVector<unsigned, 8> RegArgs;
1536 unsigned NumBytes;
1537
1538 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1539 RegArgs, CC, NumBytes, IsVarArg))
1540 return false;
1541
Hal Finkel934361a2015-01-14 01:07:51 +00001542 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001543 // FIXME: No handling for function pointers yet. This requires
1544 // implementing the function descriptor (OPD) setup.
1545 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001546 if (!GV) {
1547 // patchpoints are a special case; they always dispatch to a pointer value.
1548 // However, we don't actually want to generate the indirect call sequence
1549 // here (that will be generated, as necessary, during asm printing), and
1550 // the call we generate here will be erased by FastISel::selectPatchpoint,
1551 // so don't try very hard...
1552 if (CLI.IsPatchPoint)
1553 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1554 else
1555 return false;
1556 } else {
1557 // Build direct call with NOP for TOC restore.
1558 // FIXME: We can and should optimize away the NOP for local calls.
1559 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1560 TII.get(PPC::BL8_NOP));
1561 // Add callee.
1562 MIB.addGlobalAddress(GV);
1563 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001564
1565 // Add implicit physical register uses to the call.
1566 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1567 MIB.addReg(RegArgs[II], RegState::Implicit);
1568
Hal Finkelaf519932015-01-19 07:20:27 +00001569 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1570 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001571 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001572 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001573
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001574 // Add a register mask with the call-preserved registers. Proper
1575 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001576 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001577
Hal Finkel934361a2015-01-14 01:07:51 +00001578 CLI.Call = MIB;
1579
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001580 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001581 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001582}
1583
Bill Schmidtd89f6782013-08-26 19:42:51 +00001584// Attempt to fast-select a return instruction.
1585bool PPCFastISel::SelectRet(const Instruction *I) {
1586
1587 if (!FuncInfo.CanLowerReturn)
1588 return false;
1589
1590 const ReturnInst *Ret = cast<ReturnInst>(I);
1591 const Function &F = *I->getParent()->getParent();
1592
1593 // Build a list of return value registers.
1594 SmallVector<unsigned, 4> RetRegs;
1595 CallingConv::ID CC = F.getCallingConv();
1596
1597 if (Ret->getNumOperands() > 0) {
1598 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001599 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001600
1601 // Analyze operands of the call, assigning locations to each operand.
1602 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001603 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001604 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1605 const Value *RV = Ret->getOperand(0);
1606
1607 // FIXME: Only one output register for now.
1608 if (ValLocs.size() > 1)
1609 return false;
1610
Eric Christopherf0024d12015-07-25 00:48:08 +00001611 // Special case for returning a constant integer of any size - materialize
1612 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001613 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001614 CCValAssign &VA = ValLocs[0];
1615
1616 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001617 // We still need to worry about properly extending the sign. For example,
1618 // we could have only a single bit or a constant that needs zero
1619 // extension rather than sign extension. Make sure we pass the return
1620 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001621 unsigned SrcReg =
1622 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() == CCValAssign::SExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001623
Rafael Espindolaea09c592014-02-18 22:05:46 +00001624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001625 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1626
Bill Schmidtd89f6782013-08-26 19:42:51 +00001627 RetRegs.push_back(RetReg);
1628
1629 } else {
1630 unsigned Reg = getRegForValue(RV);
1631
1632 if (Reg == 0)
1633 return false;
1634
1635 // Copy the result values into the output registers.
1636 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1637
1638 CCValAssign &VA = ValLocs[i];
1639 assert(VA.isRegLoc() && "Can only return in registers!");
1640 RetRegs.push_back(VA.getLocReg());
1641 unsigned SrcReg = Reg + VA.getValNo();
1642
Mehdi Amini44ede332015-07-09 02:09:04 +00001643 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001644 if (!RVEVT.isSimple())
1645 return false;
1646 MVT RVVT = RVEVT.getSimpleVT();
1647 MVT DestVT = VA.getLocVT();
1648
1649 if (RVVT != DestVT && RVVT != MVT::i8 &&
1650 RVVT != MVT::i16 && RVVT != MVT::i32)
1651 return false;
1652
1653 if (RVVT != DestVT) {
1654 switch (VA.getLocInfo()) {
1655 default:
1656 llvm_unreachable("Unknown loc info!");
1657 case CCValAssign::Full:
1658 llvm_unreachable("Full value assign but types don't match?");
1659 case CCValAssign::AExt:
1660 case CCValAssign::ZExt: {
1661 const TargetRegisterClass *RC =
1662 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1663 unsigned TmpReg = createResultReg(RC);
1664 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1665 return false;
1666 SrcReg = TmpReg;
1667 break;
1668 }
1669 case CCValAssign::SExt: {
1670 const TargetRegisterClass *RC =
1671 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1672 unsigned TmpReg = createResultReg(RC);
1673 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1674 return false;
1675 SrcReg = TmpReg;
1676 break;
1677 }
1678 }
1679 }
1680
Rafael Espindolaea09c592014-02-18 22:05:46 +00001681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001682 TII.get(TargetOpcode::COPY), RetRegs[i])
1683 .addReg(SrcReg);
1684 }
1685 }
1686 }
1687
Rafael Espindolaea09c592014-02-18 22:05:46 +00001688 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001689 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001690
1691 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1692 MIB.addReg(RetRegs[i], RegState::Implicit);
1693
1694 return true;
1695}
1696
Bill Schmidt03008132013-08-25 22:33:42 +00001697// Attempt to emit an integer extend of SrcReg into DestReg. Both
1698// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001699// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001700bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1701 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001702 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1703 return false;
1704 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1705 return false;
1706
1707 // Signed extensions use EXTSB, EXTSH, EXTSW.
1708 if (!IsZExt) {
1709 unsigned Opc;
1710 if (SrcVT == MVT::i8)
1711 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1712 else if (SrcVT == MVT::i16)
1713 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1714 else {
1715 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1716 Opc = PPC::EXTSW_32_64;
1717 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001719 .addReg(SrcReg);
1720
1721 // Unsigned 32-bit extensions use RLWINM.
1722 } else if (DestVT == MVT::i32) {
1723 unsigned MB;
1724 if (SrcVT == MVT::i8)
1725 MB = 24;
1726 else {
1727 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1728 MB = 16;
1729 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001730 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001731 DestReg)
1732 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1733
1734 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1735 } else {
1736 unsigned MB;
1737 if (SrcVT == MVT::i8)
1738 MB = 56;
1739 else if (SrcVT == MVT::i16)
1740 MB = 48;
1741 else
1742 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001744 TII.get(PPC::RLDICL_32_64), DestReg)
1745 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1746 }
1747
1748 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001749}
1750
1751// Attempt to fast-select an indirect branch instruction.
1752bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1753 unsigned AddrReg = getRegForValue(I->getOperand(0));
1754 if (AddrReg == 0)
1755 return false;
1756
Rafael Espindolaea09c592014-02-18 22:05:46 +00001757 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001758 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001759 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001760
1761 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1762 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1763 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1764
1765 return true;
1766}
1767
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001768// Attempt to fast-select an integer truncate instruction.
1769bool PPCFastISel::SelectTrunc(const Instruction *I) {
1770 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001771 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1772 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001773
1774 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1775 return false;
1776
1777 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1778 return false;
1779
1780 unsigned SrcReg = getRegForValue(Src);
1781 if (!SrcReg)
1782 return false;
1783
1784 // The only interesting case is when we need to switch register classes.
1785 if (SrcVT == MVT::i64) {
1786 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001787 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1788 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001789 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1790 SrcReg = ResultReg;
1791 }
1792
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001793 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001794 return true;
1795}
1796
Bill Schmidtd89f6782013-08-26 19:42:51 +00001797// Attempt to fast-select an integer extend instruction.
1798bool PPCFastISel::SelectIntExt(const Instruction *I) {
1799 Type *DestTy = I->getType();
1800 Value *Src = I->getOperand(0);
1801 Type *SrcTy = Src->getType();
1802
1803 bool IsZExt = isa<ZExtInst>(I);
1804 unsigned SrcReg = getRegForValue(Src);
1805 if (!SrcReg) return false;
1806
1807 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001808 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1809 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001810 if (!SrcEVT.isSimple())
1811 return false;
1812 if (!DestEVT.isSimple())
1813 return false;
1814
1815 MVT SrcVT = SrcEVT.getSimpleVT();
1816 MVT DestVT = DestEVT.getSimpleVT();
1817
1818 // If we know the register class needed for the result of this
1819 // instruction, use it. Otherwise pick the register class of the
1820 // correct size that does not contain X0/R0, since we don't know
1821 // whether downstream uses permit that assignment.
1822 unsigned AssignedReg = FuncInfo.ValueMap[I];
1823 const TargetRegisterClass *RC =
1824 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1825 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1826 &PPC::GPRC_and_GPRC_NOR0RegClass));
1827 unsigned ResultReg = createResultReg(RC);
1828
1829 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1830 return false;
1831
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001832 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001833 return true;
1834}
1835
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001836// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001837// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001838bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001839
1840 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001841 case Instruction::Load:
1842 return SelectLoad(I);
1843 case Instruction::Store:
1844 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001845 case Instruction::Br:
1846 return SelectBranch(I);
1847 case Instruction::IndirectBr:
1848 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001849 case Instruction::FPExt:
1850 return SelectFPExt(I);
1851 case Instruction::FPTrunc:
1852 return SelectFPTrunc(I);
1853 case Instruction::SIToFP:
1854 return SelectIToFP(I, /*IsSigned*/ true);
1855 case Instruction::UIToFP:
1856 return SelectIToFP(I, /*IsSigned*/ false);
1857 case Instruction::FPToSI:
1858 return SelectFPToI(I, /*IsSigned*/ true);
1859 case Instruction::FPToUI:
1860 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001861 case Instruction::Add:
1862 return SelectBinaryIntOp(I, ISD::ADD);
1863 case Instruction::Or:
1864 return SelectBinaryIntOp(I, ISD::OR);
1865 case Instruction::Sub:
1866 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001867 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001868 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001869 case Instruction::Ret:
1870 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001871 case Instruction::Trunc:
1872 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001873 case Instruction::ZExt:
1874 case Instruction::SExt:
1875 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001876 // Here add other flavors of Instruction::XXX that automated
1877 // cases don't catch. For example, switches are terminators
1878 // that aren't yet handled.
1879 default:
1880 break;
1881 }
1882 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001883}
1884
1885// Materialize a floating-point constant into a register, and return
1886// the register number (or zero if we failed to handle it).
1887unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1888 // No plans to handle long double here.
1889 if (VT != MVT::f32 && VT != MVT::f64)
1890 return 0;
1891
1892 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001893 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001894 assert(Align > 0 && "Unexpectedly missing alignment information!");
1895 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1896 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1897 CodeModel::Model CModel = TM.getCodeModel();
1898
1899 MachineMemOperand *MMO =
1900 FuncInfo.MF->getMachineMemOperand(
1901 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1902 (VT == MVT::f32) ? 4 : 8, Align);
1903
Bill Schmidt03008132013-08-25 22:33:42 +00001904 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1905 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1906
Hal Finkele6698d52015-02-01 15:03:28 +00001907 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001908 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1909 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001911 TmpReg)
1912 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001914 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1915 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001916 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001918 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001919 // But for large code model, we must generate a LDtocL followed
1920 // by the LF[SD].
1921 if (CModel == CodeModel::Large) {
1922 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001924 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001925 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001926 .addImm(0).addReg(TmpReg2);
1927 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001929 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1930 .addReg(TmpReg)
1931 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001932 }
1933
1934 return DestReg;
1935}
1936
Bill Schmidtccecf262013-08-30 02:29:45 +00001937// Materialize the address of a global value into a register, and return
1938// the register number (or zero if we failed to handle it).
1939unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1940 assert(VT == MVT::i64 && "Non-address!");
1941 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1942 unsigned DestReg = createResultReg(RC);
1943
1944 // Global values may be plain old object addresses, TLS object
1945 // addresses, constant pool entries, or jump tables. How we generate
1946 // code for these may depend on small, medium, or large code model.
1947 CodeModel::Model CModel = TM.getCodeModel();
1948
1949 // FIXME: Jump tables are not yet required because fast-isel doesn't
1950 // handle switches; if that changes, we need them as well. For now,
1951 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001952
1953 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001954 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001955 return 0;
1956
Hal Finkele6698d52015-02-01 15:03:28 +00001957 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001958 // For small code model, generate a simple TOC load.
1959 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1961 DestReg)
1962 .addGlobalAddress(GV)
1963 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001964 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001965 // If the address is an externally defined symbol, a symbol with common
1966 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001967 // jump table address (not yet needed), or if we are generating code
1968 // for large code model, we generate:
1969 // LDtocL(GV, ADDIStocHA(%X2, GV))
1970 // Otherwise we generate:
1971 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1972 // Either way, start with the ADDIStocHA:
1973 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001975 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1976
Bill Schmidtccecf262013-08-30 02:29:45 +00001977 // If/when switches are implemented, jump tables should be handled
1978 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001979 if (CModel == CodeModel::Large ||
1980 (GV->getType()->getElementType()->isFunctionTy() &&
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001981 !GV->isStrongDefinitionForLinker()) ||
Bill Schmidt5d82f092014-06-16 21:36:02 +00001982 GV->isDeclaration() || GV->hasCommonLinkage() ||
1983 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001985 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1986 else
1987 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001989 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1990 }
1991
1992 return DestReg;
1993}
1994
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001995// Materialize a 32-bit integer constant into a register, and return
1996// the register number (or zero if we failed to handle it).
1997unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1998 const TargetRegisterClass *RC) {
1999 unsigned Lo = Imm & 0xFFFF;
2000 unsigned Hi = (Imm >> 16) & 0xFFFF;
2001
2002 unsigned ResultReg = createResultReg(RC);
2003 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2004
2005 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002007 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2008 .addImm(Imm);
2009 else if (Lo) {
2010 // Both Lo and Hi have nonzero bits.
2011 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002013 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2014 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002016 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2017 .addReg(TmpReg).addImm(Lo);
2018 } else
2019 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002020 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002021 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2022 .addImm(Hi);
2023
2024 return ResultReg;
2025}
2026
2027// Materialize a 64-bit integer constant into a register, and return
2028// the register number (or zero if we failed to handle it).
2029unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2030 const TargetRegisterClass *RC) {
2031 unsigned Remainder = 0;
2032 unsigned Shift = 0;
2033
2034 // If the value doesn't fit in 32 bits, see if we can shift it
2035 // so that it fits in 32 bits.
2036 if (!isInt<32>(Imm)) {
2037 Shift = countTrailingZeros<uint64_t>(Imm);
2038 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2039
2040 if (isInt<32>(ImmSh))
2041 Imm = ImmSh;
2042 else {
2043 Remainder = Imm;
2044 Shift = 32;
2045 Imm >>= 32;
2046 }
2047 }
2048
2049 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2050 // (if not shifted).
2051 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2052 if (!Shift)
2053 return TmpReg1;
2054
2055 // If upper 32 bits were not zero, we've built them and need to shift
2056 // them into place.
2057 unsigned TmpReg2;
2058 if (Imm) {
2059 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002061 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2062 } else
2063 TmpReg2 = TmpReg1;
2064
2065 unsigned TmpReg3, Hi, Lo;
2066 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2067 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002069 TmpReg3).addReg(TmpReg2).addImm(Hi);
2070 } else
2071 TmpReg3 = TmpReg2;
2072
2073 if ((Lo = Remainder & 0xFFFF)) {
2074 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002076 ResultReg).addReg(TmpReg3).addImm(Lo);
2077 return ResultReg;
2078 }
2079
2080 return TmpReg3;
2081}
2082
2083
2084// Materialize an integer constant into a register, and return
2085// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002086unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2087 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002088 // If we're using CR bit registers for i1 values, handle that as a special
2089 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002090 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002091 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2093 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2094 return ImmReg;
2095 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002096
2097 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2098 VT != MVT::i8 && VT != MVT::i1)
2099 return 0;
2100
2101 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2102 &PPC::GPRCRegClass);
2103
2104 // If the constant is in range, use a load-immediate.
Eric Christopherf0024d12015-07-25 00:48:08 +00002105 if (UseSExt && isInt<16>(CI->getSExtValue())) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002106 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2107 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Eric Christopherf0024d12015-07-25 00:48:08 +00002109 .addImm(CI->getSExtValue());
2110 return ImmReg;
2111 } else if (!UseSExt && isUInt<16>(CI->getZExtValue())) {
2112 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2113 unsigned ImmReg = createResultReg(RC);
2114 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2115 .addImm(CI->getZExtValue());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002116 return ImmReg;
2117 }
2118
2119 // Construct the constant piecewise.
2120 int64_t Imm = CI->getZExtValue();
2121
2122 if (VT == MVT::i64)
2123 return PPCMaterialize64BitInt(Imm, RC);
2124 else if (VT == MVT::i32)
2125 return PPCMaterialize32BitInt(Imm, RC);
2126
2127 return 0;
2128}
2129
2130// Materialize a constant into a register, and return the register
2131// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002132unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002133 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002134
2135 // Only handle simple types.
2136 if (!CEVT.isSimple()) return 0;
2137 MVT VT = CEVT.getSimpleVT();
2138
2139 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2140 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002141 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2142 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002143 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2144 return PPCMaterializeInt(CI, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002145
2146 return 0;
2147}
2148
2149// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002150// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002151unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002152 // Don't handle dynamic allocas.
2153 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2154
2155 MVT VT;
2156 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2157
2158 DenseMap<const AllocaInst*, int>::iterator SI =
2159 FuncInfo.StaticAllocaMap.find(AI);
2160
2161 if (SI != FuncInfo.StaticAllocaMap.end()) {
2162 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002164 ResultReg).addFrameIndex(SI->second).addImm(0);
2165 return ResultReg;
2166 }
2167
2168 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002169}
2170
Bill Schmidtccecf262013-08-30 02:29:45 +00002171// Fold loads into extends when possible.
2172// FIXME: We can have multiple redundant extend/trunc instructions
2173// following a load. The folding only picks up one. Extend this
2174// to check subsequent instructions for the same pattern and remove
2175// them. Thus ResultReg should be the def reg for the last redundant
2176// instruction in a chain, and all intervening instructions can be
2177// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2178// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002179bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2180 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002181 // Verify we have a legal type before going any further.
2182 MVT VT;
2183 if (!isLoadTypeLegal(LI->getType(), VT))
2184 return false;
2185
2186 // Combine load followed by zero- or sign-extend.
2187 bool IsZExt = false;
2188 switch(MI->getOpcode()) {
2189 default:
2190 return false;
2191
2192 case PPC::RLDICL:
2193 case PPC::RLDICL_32_64: {
2194 IsZExt = true;
2195 unsigned MB = MI->getOperand(3).getImm();
2196 if ((VT == MVT::i8 && MB <= 56) ||
2197 (VT == MVT::i16 && MB <= 48) ||
2198 (VT == MVT::i32 && MB <= 32))
2199 break;
2200 return false;
2201 }
2202
2203 case PPC::RLWINM:
2204 case PPC::RLWINM8: {
2205 IsZExt = true;
2206 unsigned MB = MI->getOperand(3).getImm();
2207 if ((VT == MVT::i8 && MB <= 24) ||
2208 (VT == MVT::i16 && MB <= 16))
2209 break;
2210 return false;
2211 }
2212
2213 case PPC::EXTSB:
2214 case PPC::EXTSB8:
2215 case PPC::EXTSB8_32_64:
2216 /* There is no sign-extending load-byte instruction. */
2217 return false;
2218
2219 case PPC::EXTSH:
2220 case PPC::EXTSH8:
2221 case PPC::EXTSH8_32_64: {
2222 if (VT != MVT::i16 && VT != MVT::i8)
2223 return false;
2224 break;
2225 }
2226
2227 case PPC::EXTSW:
2228 case PPC::EXTSW_32_64: {
2229 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2230 return false;
2231 break;
2232 }
2233 }
2234
2235 // See if we can handle this address.
2236 Address Addr;
2237 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2238 return false;
2239
2240 unsigned ResultReg = MI->getOperand(0).getReg();
2241
Craig Topper062a2ba2014-04-25 05:30:21 +00002242 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002243 return false;
2244
2245 MI->eraseFromParent();
2246 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002247}
2248
2249// Attempt to lower call arguments in a faster way than done by
2250// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002251bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002252 // Defer to normal argument lowering for now. It's reasonably
2253 // efficient. Consider doing something like ARM to handle the
2254 // case where all args fit in registers, no varargs, no float
2255 // or vector args.
2256 return false;
2257}
2258
Bill Schmidt03008132013-08-25 22:33:42 +00002259// Handle materializing integer constants into a register. This is not
2260// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002261unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002262
2263 if (Opc != ISD::Constant)
2264 return 0;
2265
Hal Finkel940ab932014-02-28 00:27:01 +00002266 // If we're using CR bit registers for i1 values, handle that as a special
2267 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002268 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002269 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2270 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2271 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2272 return ImmReg;
2273 }
2274
Bill Schmidt03008132013-08-25 22:33:42 +00002275 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2276 VT != MVT::i8 && VT != MVT::i1)
2277 return 0;
2278
2279 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2280 &PPC::GPRCRegClass);
2281 if (VT == MVT::i64)
2282 return PPCMaterialize64BitInt(Imm, RC);
2283 else
2284 return PPCMaterialize32BitInt(Imm, RC);
2285}
2286
Bill Schmidtccecf262013-08-30 02:29:45 +00002287// Override for ADDI and ADDI8 to set the correct register class
2288// on RHS operand 0. The automatic infrastructure naively assumes
2289// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2290// for these cases. At the moment, none of the other automatically
2291// generated RI instructions require special treatment. However, once
2292// SelectSelect is implemented, "isel" requires similar handling.
2293//
2294// Also be conservative about the output register class. Avoid
2295// assigning R0 or X0 to the output register for GPRC and G8RC
2296// register classes, as any such result could be used in ADDI, etc.,
2297// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002298unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002299 const TargetRegisterClass *RC,
2300 unsigned Op0, bool Op0IsKill,
2301 uint64_t Imm) {
2302 if (MachineInstOpcode == PPC::ADDI)
2303 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2304 else if (MachineInstOpcode == PPC::ADDI8)
2305 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2306
2307 const TargetRegisterClass *UseRC =
2308 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2309 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2310
Juergen Ributzka88e32512014-09-03 20:56:59 +00002311 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002312 Op0, Op0IsKill, Imm);
2313}
2314
2315// Override for instructions with one register operand to avoid use of
2316// R0/X0. The automatic infrastructure isn't aware of the context so
2317// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002318unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002319 const TargetRegisterClass* RC,
2320 unsigned Op0, bool Op0IsKill) {
2321 const TargetRegisterClass *UseRC =
2322 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2323 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2324
Juergen Ributzka88e32512014-09-03 20:56:59 +00002325 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002326}
2327
2328// Override for instructions with two register operands to avoid use
2329// of R0/X0. The automatic infrastructure isn't aware of the context
2330// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002331unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002332 const TargetRegisterClass* RC,
2333 unsigned Op0, bool Op0IsKill,
2334 unsigned Op1, bool Op1IsKill) {
2335 const TargetRegisterClass *UseRC =
2336 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2337 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2338
Juergen Ributzka88e32512014-09-03 20:56:59 +00002339 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002340 Op1, Op1IsKill);
2341}
2342
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002343namespace llvm {
2344 // Create the fast instruction selector for PowerPC64 ELF.
2345 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2346 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002347 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002348 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002349 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002350 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002351 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002352 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002353}