blob: fbd7b6d7f1ce775c6000eb3256d8cb23ddbb2e98 [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);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000147 bool isVSFRCRegister(unsigned Register) const {
148 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
149 }
Bill Schmidt03008132013-08-25 22:33:42 +0000150 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
151 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000152 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
153 const TargetRegisterClass *RC, bool IsZExt = true,
154 unsigned FP64LoadOpc = PPC::LFD);
155 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
156 bool PPCComputeAddress(const Value *Obj, Address &Addr);
157 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
158 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000159 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
160 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000161 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000162 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Samuel Antao61570df2014-09-17 23:25:06 +0000163 unsigned PPCMaterializeInt(const Constant *C, MVT VT, bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000164 unsigned PPCMaterialize32BitInt(int64_t Imm,
165 const TargetRegisterClass *RC);
166 unsigned PPCMaterialize64BitInt(int64_t Imm,
167 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000168 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
169 unsigned SrcReg, bool IsSigned);
170 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000171
Bill Schmidtd89f6782013-08-26 19:42:51 +0000172 // Call handling routines.
173 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000174 bool processCallArgs(SmallVectorImpl<Value*> &Args,
175 SmallVectorImpl<unsigned> &ArgRegs,
176 SmallVectorImpl<MVT> &ArgVTs,
177 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
178 SmallVectorImpl<unsigned> &RegArgs,
179 CallingConv::ID CC,
180 unsigned &NumBytes,
181 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000182 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000183 CCAssignFn *usePPC32CCs(unsigned Flag);
184
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000185 private:
186 #include "PPCGenFastISel.inc"
187
188};
189
190} // end anonymous namespace
191
Bill Schmidtd89f6782013-08-26 19:42:51 +0000192#include "PPCGenCallingConv.inc"
193
194// Function whose sole purpose is to kill compiler warnings
195// stemming from unused functions included from PPCGenCallingConv.inc.
196CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
197 if (Flag == 1)
198 return CC_PPC32_SVR4;
199 else if (Flag == 2)
200 return CC_PPC32_SVR4_ByVal;
201 else if (Flag == 3)
202 return CC_PPC32_SVR4_VarArg;
203 else
204 return RetCC_PPC;
205}
206
Bill Schmidt03008132013-08-25 22:33:42 +0000207static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
208 switch (Pred) {
209 // These are not representable with any single compare.
210 case CmpInst::FCMP_FALSE:
211 case CmpInst::FCMP_UEQ:
212 case CmpInst::FCMP_UGT:
213 case CmpInst::FCMP_UGE:
214 case CmpInst::FCMP_ULT:
215 case CmpInst::FCMP_ULE:
216 case CmpInst::FCMP_UNE:
217 case CmpInst::FCMP_TRUE:
218 default:
219 return Optional<PPC::Predicate>();
220
221 case CmpInst::FCMP_OEQ:
222 case CmpInst::ICMP_EQ:
223 return PPC::PRED_EQ;
224
225 case CmpInst::FCMP_OGT:
226 case CmpInst::ICMP_UGT:
227 case CmpInst::ICMP_SGT:
228 return PPC::PRED_GT;
229
230 case CmpInst::FCMP_OGE:
231 case CmpInst::ICMP_UGE:
232 case CmpInst::ICMP_SGE:
233 return PPC::PRED_GE;
234
235 case CmpInst::FCMP_OLT:
236 case CmpInst::ICMP_ULT:
237 case CmpInst::ICMP_SLT:
238 return PPC::PRED_LT;
239
240 case CmpInst::FCMP_OLE:
241 case CmpInst::ICMP_ULE:
242 case CmpInst::ICMP_SLE:
243 return PPC::PRED_LE;
244
245 case CmpInst::FCMP_ONE:
246 case CmpInst::ICMP_NE:
247 return PPC::PRED_NE;
248
249 case CmpInst::FCMP_ORD:
250 return PPC::PRED_NU;
251
252 case CmpInst::FCMP_UNO:
253 return PPC::PRED_UN;
254 }
255}
256
Bill Schmidtccecf262013-08-30 02:29:45 +0000257// Determine whether the type Ty is simple enough to be handled by
258// fast-isel, and return its equivalent machine type in VT.
259// FIXME: Copied directly from ARM -- factor into base class?
260bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
261 EVT Evt = TLI.getValueType(Ty, true);
262
263 // Only handle simple types.
264 if (Evt == MVT::Other || !Evt.isSimple()) return false;
265 VT = Evt.getSimpleVT();
266
267 // Handle all legal types, i.e. a register that will directly hold this
268 // value.
269 return TLI.isTypeLegal(VT);
270}
271
272// Determine whether the type Ty is simple enough to be handled by
273// fast-isel as a load target, and return its equivalent machine type in VT.
274bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
275 if (isTypeLegal(Ty, VT)) return true;
276
277 // If this is a type than can be sign or zero-extended to a basic operation
278 // go ahead and accept it now.
279 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
280 return true;
281 }
282
283 return false;
284}
285
286// Given a value Obj, create an Address object Addr that represents its
287// address. Return false if we can't handle it.
288bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000289 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000290 unsigned Opcode = Instruction::UserOp1;
291 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
292 // Don't walk into other basic blocks unless the object is an alloca from
293 // another block, otherwise it may not have a virtual register assigned.
294 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
295 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
296 Opcode = I->getOpcode();
297 U = I;
298 }
299 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
300 Opcode = C->getOpcode();
301 U = C;
302 }
303
304 switch (Opcode) {
305 default:
306 break;
307 case Instruction::BitCast:
308 // Look through bitcasts.
309 return PPCComputeAddress(U->getOperand(0), Addr);
310 case Instruction::IntToPtr:
311 // Look past no-op inttoptrs.
312 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
313 return PPCComputeAddress(U->getOperand(0), Addr);
314 break;
315 case Instruction::PtrToInt:
316 // Look past no-op ptrtoints.
317 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
318 return PPCComputeAddress(U->getOperand(0), Addr);
319 break;
320 case Instruction::GetElementPtr: {
321 Address SavedAddr = Addr;
322 long TmpOffset = Addr.Offset;
323
324 // Iterate through the GEP folding the constants into offsets where
325 // we can.
326 gep_type_iterator GTI = gep_type_begin(U);
327 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
328 II != IE; ++II, ++GTI) {
329 const Value *Op = *II;
330 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000331 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000332 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
333 TmpOffset += SL->getElementOffset(Idx);
334 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000335 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000336 for (;;) {
337 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
338 // Constant-offset addressing.
339 TmpOffset += CI->getSExtValue() * S;
340 break;
341 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000342 if (canFoldAddIntoGEP(U, Op)) {
343 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000344 ConstantInt *CI =
345 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
346 TmpOffset += CI->getSExtValue() * S;
347 // Iterate on the other operand.
348 Op = cast<AddOperator>(Op)->getOperand(0);
349 continue;
350 }
351 // Unsupported
352 goto unsupported_gep;
353 }
354 }
355 }
356
357 // Try to grab the base operand now.
358 Addr.Offset = TmpOffset;
359 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
360
361 // We failed, restore everything and try the other options.
362 Addr = SavedAddr;
363
364 unsupported_gep:
365 break;
366 }
367 case Instruction::Alloca: {
368 const AllocaInst *AI = cast<AllocaInst>(Obj);
369 DenseMap<const AllocaInst*, int>::iterator SI =
370 FuncInfo.StaticAllocaMap.find(AI);
371 if (SI != FuncInfo.StaticAllocaMap.end()) {
372 Addr.BaseType = Address::FrameIndexBase;
373 Addr.Base.FI = SI->second;
374 return true;
375 }
376 break;
377 }
378 }
379
380 // FIXME: References to parameters fall through to the behavior
381 // below. They should be able to reference a frame index since
382 // they are stored to the stack, so we can get "ld rx, offset(r1)"
383 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
384 // just contain the parameter. Try to handle this with a FI.
385
386 // Try to get this in a register if nothing else has worked.
387 if (Addr.Base.Reg == 0)
388 Addr.Base.Reg = getRegForValue(Obj);
389
390 // Prevent assignment of base register to X0, which is inappropriate
391 // for loads and stores alike.
392 if (Addr.Base.Reg != 0)
393 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
394
395 return Addr.Base.Reg != 0;
396}
397
398// Fix up some addresses that can't be used directly. For example, if
399// an offset won't fit in an instruction field, we may need to move it
400// into an index register.
401void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
402 unsigned &IndexReg) {
403
404 // Check whether the offset fits in the instruction field.
405 if (!isInt<16>(Addr.Offset))
406 UseOffset = false;
407
408 // If this is a stack pointer and the offset needs to be simplified then
409 // put the alloca address into a register, set the base type back to
410 // register and continue. This should almost never happen.
411 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
412 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000414 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
415 Addr.Base.Reg = ResultReg;
416 Addr.BaseType = Address::RegBase;
417 }
418
419 if (!UseOffset) {
420 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
421 : Type::getInt64Ty(*Context));
422 const ConstantInt *Offset =
423 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
424 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
425 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
426 }
427}
428
429// Emit a load instruction if possible, returning true if we succeeded,
430// otherwise false. See commentary below for how the register class of
431// the load is determined.
432bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
433 const TargetRegisterClass *RC,
434 bool IsZExt, unsigned FP64LoadOpc) {
435 unsigned Opc;
436 bool UseOffset = true;
437
438 // If ResultReg is given, it determines the register class of the load.
439 // Otherwise, RC is the register class to use. If the result of the
440 // load isn't anticipated in this block, both may be zero, in which
441 // case we must make a conservative guess. In particular, don't assign
442 // R0 or X0 to the result register, as the result may be used in a load,
443 // store, add-immediate, or isel that won't permit this. (Though
444 // perhaps the spill and reload of live-exit values would handle this?)
445 const TargetRegisterClass *UseRC =
446 (ResultReg ? MRI.getRegClass(ResultReg) :
447 (RC ? RC :
448 (VT == MVT::f64 ? &PPC::F8RCRegClass :
449 (VT == MVT::f32 ? &PPC::F4RCRegClass :
450 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
451 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
452
453 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
454
455 switch (VT.SimpleTy) {
456 default: // e.g., vector types not handled
457 return false;
458 case MVT::i8:
459 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
460 break;
461 case MVT::i16:
462 Opc = (IsZExt ?
463 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
464 (Is32BitInt ? PPC::LHA : PPC::LHA8));
465 break;
466 case MVT::i32:
467 Opc = (IsZExt ?
468 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
469 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
470 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
471 UseOffset = false;
472 break;
473 case MVT::i64:
474 Opc = PPC::LD;
475 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
476 "64-bit load with 32-bit target??");
477 UseOffset = ((Addr.Offset & 3) == 0);
478 break;
479 case MVT::f32:
480 Opc = PPC::LFS;
481 break;
482 case MVT::f64:
483 Opc = FP64LoadOpc;
484 break;
485 }
486
487 // If necessary, materialize the offset into a register and use
488 // the indexed form. Also handle stack pointers with special needs.
489 unsigned IndexReg = 0;
490 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000491
492 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
493 // be used.
494 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
495 if (IsVSFRC && (Opc == PPC::LFD) &&
496 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
497 (Addr.Offset == 0)) {
498 UseOffset = false;
499 }
500
Bill Schmidtccecf262013-08-30 02:29:45 +0000501 if (ResultReg == 0)
502 ResultReg = createResultReg(UseRC);
503
504 // Note: If we still have a frame index here, we know the offset is
505 // in range, as otherwise PPCSimplifyAddress would have converted it
506 // into a RegBase.
507 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000508 // VSX only provides an indexed load.
509 if (IsVSFRC && Opc == PPC::LFD) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000510
511 MachineMemOperand *MMO =
512 FuncInfo.MF->getMachineMemOperand(
513 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
514 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
515 MFI.getObjectAlignment(Addr.Base.FI));
516
Rafael Espindolaea09c592014-02-18 22:05:46 +0000517 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000518 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
519
520 // Base reg with offset in range.
521 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000522 // VSX only provides an indexed load.
523 if (IsVSFRC && Opc == PPC::LFD) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000524
Rafael Espindolaea09c592014-02-18 22:05:46 +0000525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000526 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
527
528 // Indexed form.
529 } else {
530 // Get the RR opcode corresponding to the RI one. FIXME: It would be
531 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
532 // is hard to get at.
533 switch (Opc) {
534 default: llvm_unreachable("Unexpected opcode!");
535 case PPC::LBZ: Opc = PPC::LBZX; break;
536 case PPC::LBZ8: Opc = PPC::LBZX8; break;
537 case PPC::LHZ: Opc = PPC::LHZX; break;
538 case PPC::LHZ8: Opc = PPC::LHZX8; break;
539 case PPC::LHA: Opc = PPC::LHAX; break;
540 case PPC::LHA8: Opc = PPC::LHAX8; break;
541 case PPC::LWZ: Opc = PPC::LWZX; break;
542 case PPC::LWZ8: Opc = PPC::LWZX8; break;
543 case PPC::LWA: Opc = PPC::LWAX; break;
544 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
545 case PPC::LD: Opc = PPC::LDX; break;
546 case PPC::LFS: Opc = PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000547 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000548 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000550 .addReg(Addr.Base.Reg).addReg(IndexReg);
551 }
552
553 return true;
554}
555
556// Attempt to fast-select a load instruction.
557bool PPCFastISel::SelectLoad(const Instruction *I) {
558 // FIXME: No atomic loads are supported.
559 if (cast<LoadInst>(I)->isAtomic())
560 return false;
561
562 // Verify we have a legal type before going any further.
563 MVT VT;
564 if (!isLoadTypeLegal(I->getType(), VT))
565 return false;
566
567 // See if we can handle this address.
568 Address Addr;
569 if (!PPCComputeAddress(I->getOperand(0), Addr))
570 return false;
571
572 // Look at the currently assigned register for this instruction
573 // to determine the required register class. This is necessary
574 // to constrain RA from using R0/X0 when this is not legal.
575 unsigned AssignedReg = FuncInfo.ValueMap[I];
576 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000577 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000578
579 unsigned ResultReg = 0;
580 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
581 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000582 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000583 return true;
584}
585
586// Emit a store instruction to store SrcReg at Addr.
587bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
588 assert(SrcReg && "Nothing to store!");
589 unsigned Opc;
590 bool UseOffset = true;
591
592 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
593 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
594
595 switch (VT.SimpleTy) {
596 default: // e.g., vector types not handled
597 return false;
598 case MVT::i8:
599 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
600 break;
601 case MVT::i16:
602 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
603 break;
604 case MVT::i32:
605 assert(Is32BitInt && "Not GPRC for i32??");
606 Opc = PPC::STW;
607 break;
608 case MVT::i64:
609 Opc = PPC::STD;
610 UseOffset = ((Addr.Offset & 3) == 0);
611 break;
612 case MVT::f32:
613 Opc = PPC::STFS;
614 break;
615 case MVT::f64:
616 Opc = PPC::STFD;
617 break;
618 }
619
620 // If necessary, materialize the offset into a register and use
621 // the indexed form. Also handle stack pointers with special needs.
622 unsigned IndexReg = 0;
623 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
624
Bill Seurer8c728ae2014-12-05 20:15:56 +0000625 // If this is a potential VSX store with an offset of 0, a VSX indexed store
626 // can be used.
627 bool IsVSFRC = isVSFRCRegister(SrcReg);
628 if (IsVSFRC && (Opc == PPC::STFD) &&
629 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
630 (Addr.Offset == 0)) {
631 UseOffset = false;
632 }
633
Bill Schmidtccecf262013-08-30 02:29:45 +0000634 // Note: If we still have a frame index here, we know the offset is
635 // in range, as otherwise PPCSimplifyAddress would have converted it
636 // into a RegBase.
637 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000638 // VSX only provides an indexed store.
639 if (IsVSFRC && Opc == PPC::STFD) return false;
640
Bill Schmidtccecf262013-08-30 02:29:45 +0000641 MachineMemOperand *MMO =
642 FuncInfo.MF->getMachineMemOperand(
643 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
644 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
645 MFI.getObjectAlignment(Addr.Base.FI));
646
Rafael Espindolaea09c592014-02-18 22:05:46 +0000647 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
648 .addReg(SrcReg)
649 .addImm(Addr.Offset)
650 .addFrameIndex(Addr.Base.FI)
651 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000652
653 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000654 } else if (UseOffset) {
655 // VSX only provides an indexed store.
656 if (IsVSFRC && Opc == PPC::STFD) return false;
657
Rafael Espindolaea09c592014-02-18 22:05:46 +0000658 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000659 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
660
661 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000662 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000663 // Get the RR opcode corresponding to the RI one. FIXME: It would be
664 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
665 // is hard to get at.
666 switch (Opc) {
667 default: llvm_unreachable("Unexpected opcode!");
668 case PPC::STB: Opc = PPC::STBX; break;
669 case PPC::STH : Opc = PPC::STHX; break;
670 case PPC::STW : Opc = PPC::STWX; break;
671 case PPC::STB8: Opc = PPC::STBX8; break;
672 case PPC::STH8: Opc = PPC::STHX8; break;
673 case PPC::STW8: Opc = PPC::STWX8; break;
674 case PPC::STD: Opc = PPC::STDX; break;
675 case PPC::STFS: Opc = PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000676 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000677 }
Samuel Antaof6815602015-03-17 15:00:57 +0000678
679 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
680 .addReg(SrcReg);
681
682 // If we have an index register defined we use it in the store inst,
683 // otherwise we use X0 as base as it makes the vector instructions to
684 // use zero in the computation of the effective address regardless the
685 // content of the register.
686 if (IndexReg)
687 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
688 else
689 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000690 }
691
692 return true;
693}
694
695// Attempt to fast-select a store instruction.
696bool PPCFastISel::SelectStore(const Instruction *I) {
697 Value *Op0 = I->getOperand(0);
698 unsigned SrcReg = 0;
699
700 // FIXME: No atomics loads are supported.
701 if (cast<StoreInst>(I)->isAtomic())
702 return false;
703
704 // Verify we have a legal type before going any further.
705 MVT VT;
706 if (!isLoadTypeLegal(Op0->getType(), VT))
707 return false;
708
709 // Get the value to be stored into a register.
710 SrcReg = getRegForValue(Op0);
711 if (SrcReg == 0)
712 return false;
713
714 // See if we can handle this address.
715 Address Addr;
716 if (!PPCComputeAddress(I->getOperand(1), Addr))
717 return false;
718
719 if (!PPCEmitStore(VT, SrcReg, Addr))
720 return false;
721
722 return true;
723}
724
Bill Schmidt03008132013-08-25 22:33:42 +0000725// Attempt to fast-select a branch instruction.
726bool PPCFastISel::SelectBranch(const Instruction *I) {
727 const BranchInst *BI = cast<BranchInst>(I);
728 MachineBasicBlock *BrBB = FuncInfo.MBB;
729 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
730 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
731
732 // For now, just try the simplest case where it's fed by a compare.
733 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
734 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
735 if (!OptPPCPred)
736 return false;
737
738 PPC::Predicate PPCPred = OptPPCPred.getValue();
739
740 // Take advantage of fall-through opportunities.
741 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
742 std::swap(TBB, FBB);
743 PPCPred = PPC::InvertPredicate(PPCPred);
744 }
745
746 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
747
748 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
749 CondReg))
750 return false;
751
Rafael Espindolaea09c592014-02-18 22:05:46 +0000752 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Bill Schmidt03008132013-08-25 22:33:42 +0000753 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000754 fastEmitBranch(FBB, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000755 FuncInfo.MBB->addSuccessor(TBB);
756 return true;
757
758 } else if (const ConstantInt *CI =
759 dyn_cast<ConstantInt>(BI->getCondition())) {
760 uint64_t Imm = CI->getZExtValue();
761 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000762 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000763 return true;
764 }
765
766 // FIXME: ARM looks for a case where the block containing the compare
767 // has been split from the block containing the branch. If this happens,
768 // there is a vreg available containing the result of the compare. I'm
769 // not sure we can do much, as we've lost the predicate information with
770 // the compare instruction -- we have a 4-bit CR but don't know which bit
771 // to test here.
772 return false;
773}
774
775// Attempt to emit a compare of the two source values. Signed and unsigned
776// comparisons are supported. Return false if we can't handle it.
777bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
778 bool IsZExt, unsigned DestReg) {
779 Type *Ty = SrcValue1->getType();
780 EVT SrcEVT = TLI.getValueType(Ty, true);
781 if (!SrcEVT.isSimple())
782 return false;
783 MVT SrcVT = SrcEVT.getSimpleVT();
784
Eric Christopher1b8e7632014-05-22 01:07:24 +0000785 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000786 return false;
787
Bill Schmidt03008132013-08-25 22:33:42 +0000788 // See if operand 2 is an immediate encodeable in the compare.
789 // FIXME: Operands are not in canonical order at -O0, so an immediate
790 // operand in position 1 is a lost opportunity for now. We are
791 // similar to ARM in this regard.
792 long Imm = 0;
793 bool UseImm = false;
794
795 // Only 16-bit integer constants can be represented in compares for
796 // PowerPC. Others will be materialized into a register.
797 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
798 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
799 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
800 const APInt &CIVal = ConstInt->getValue();
801 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
802 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
803 UseImm = true;
804 }
805 }
806
807 unsigned CmpOpc;
808 bool NeedsExt = false;
809 switch (SrcVT.SimpleTy) {
810 default: return false;
811 case MVT::f32:
812 CmpOpc = PPC::FCMPUS;
813 break;
814 case MVT::f64:
815 CmpOpc = PPC::FCMPUD;
816 break;
817 case MVT::i1:
818 case MVT::i8:
819 case MVT::i16:
820 NeedsExt = true;
821 // Intentional fall-through.
822 case MVT::i32:
823 if (!UseImm)
824 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
825 else
826 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
827 break;
828 case MVT::i64:
829 if (!UseImm)
830 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
831 else
832 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
833 break;
834 }
835
836 unsigned SrcReg1 = getRegForValue(SrcValue1);
837 if (SrcReg1 == 0)
838 return false;
839
840 unsigned SrcReg2 = 0;
841 if (!UseImm) {
842 SrcReg2 = getRegForValue(SrcValue2);
843 if (SrcReg2 == 0)
844 return false;
845 }
846
847 if (NeedsExt) {
848 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
849 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
850 return false;
851 SrcReg1 = ExtReg;
852
853 if (!UseImm) {
854 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
855 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
856 return false;
857 SrcReg2 = ExtReg;
858 }
859 }
860
861 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000862 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000863 .addReg(SrcReg1).addReg(SrcReg2);
864 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000866 .addReg(SrcReg1).addImm(Imm);
867
868 return true;
869}
870
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000871// Attempt to fast-select a floating-point extend instruction.
872bool PPCFastISel::SelectFPExt(const Instruction *I) {
873 Value *Src = I->getOperand(0);
874 EVT SrcVT = TLI.getValueType(Src->getType(), true);
875 EVT DestVT = TLI.getValueType(I->getType(), true);
876
877 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
878 return false;
879
880 unsigned SrcReg = getRegForValue(Src);
881 if (!SrcReg)
882 return false;
883
884 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000885 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000886 return true;
887}
888
889// Attempt to fast-select a floating-point truncate instruction.
890bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
891 Value *Src = I->getOperand(0);
892 EVT SrcVT = TLI.getValueType(Src->getType(), true);
893 EVT DestVT = TLI.getValueType(I->getType(), true);
894
895 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
896 return false;
897
898 unsigned SrcReg = getRegForValue(Src);
899 if (!SrcReg)
900 return false;
901
902 // Round the result to single precision.
903 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000904 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000905 .addReg(SrcReg);
906
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000907 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000908 return true;
909}
910
911// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000912// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000913// those should be used instead of moving via a stack slot when the
914// subtarget permits.
915// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
916// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
917// case to 8 bytes which produces tighter code but wastes stack space.
918unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
919 bool IsSigned) {
920
921 // If necessary, extend 32-bit int to 64-bit.
922 if (SrcVT == MVT::i32) {
923 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
924 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
925 return 0;
926 SrcReg = TmpReg;
927 }
928
929 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
930 Address Addr;
931 Addr.BaseType = Address::FrameIndexBase;
932 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
933
934 // Store the value from the GPR.
935 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
936 return 0;
937
938 // Load the integer value into an FPR. The kind of load used depends
939 // on a number of conditions.
940 unsigned LoadOpc = PPC::LFD;
941
942 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000943 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000944 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000945 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000946 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000947 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000948 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000949 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000950 }
951
952 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
953 unsigned ResultReg = 0;
954 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
955 return 0;
956
957 return ResultReg;
958}
959
960// Attempt to fast-select an integer-to-floating-point conversion.
961bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
962 MVT DstVT;
963 Type *DstTy = I->getType();
964 if (!isTypeLegal(DstTy, DstVT))
965 return false;
966
967 if (DstVT != MVT::f32 && DstVT != MVT::f64)
968 return false;
969
970 Value *Src = I->getOperand(0);
971 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
972 if (!SrcEVT.isSimple())
973 return false;
974
975 MVT SrcVT = SrcEVT.getSimpleVT();
976
977 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
978 SrcVT != MVT::i32 && SrcVT != MVT::i64)
979 return false;
980
981 unsigned SrcReg = getRegForValue(Src);
982 if (SrcReg == 0)
983 return false;
984
985 // We can only lower an unsigned convert if we have the newer
986 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +0000987 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000988 return false;
989
990 // FIXME: For now we require the newer floating-point conversion operations
991 // (which are present only on P7 and A2 server models) when converting
992 // to single-precision float. Otherwise we have to generate a lot of
993 // fiddly code to avoid double rounding. If necessary, the fiddly code
994 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +0000995 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000996 return false;
997
998 // Extend the input if necessary.
999 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1000 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1001 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1002 return false;
1003 SrcVT = MVT::i64;
1004 SrcReg = TmpReg;
1005 }
1006
1007 // Move the integer value to an FPR.
1008 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1009 if (FPReg == 0)
1010 return false;
1011
1012 // Determine the opcode for the conversion.
1013 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1014 unsigned DestReg = createResultReg(RC);
1015 unsigned Opc;
1016
1017 if (DstVT == MVT::f32)
1018 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1019 else
1020 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1021
1022 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001024 .addReg(FPReg);
1025
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001026 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001027 return true;
1028}
1029
1030// Move the floating-point value in SrcReg into an integer destination
1031// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001032// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001033// those should be used instead of moving via a stack slot when the
1034// subtarget permits.
1035unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1036 unsigned SrcReg, bool IsSigned) {
1037 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1038 // Note that if have STFIWX available, we could use a 4-byte stack
1039 // slot for i32, but this being fast-isel we'll just go with the
1040 // easiest code gen possible.
1041 Address Addr;
1042 Addr.BaseType = Address::FrameIndexBase;
1043 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1044
1045 // Store the value from the FPR.
1046 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1047 return 0;
1048
1049 // Reload it into a GPR. If we want an i32, modify the address
1050 // to have a 4-byte offset so we load from the right place.
1051 if (VT == MVT::i32)
1052 Addr.Offset = 4;
1053
1054 // Look at the currently assigned register for this instruction
1055 // to determine the required register class.
1056 unsigned AssignedReg = FuncInfo.ValueMap[I];
1057 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001058 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001059
1060 unsigned ResultReg = 0;
1061 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1062 return 0;
1063
1064 return ResultReg;
1065}
1066
1067// Attempt to fast-select a floating-point-to-integer conversion.
1068bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1069 MVT DstVT, SrcVT;
1070 Type *DstTy = I->getType();
1071 if (!isTypeLegal(DstTy, DstVT))
1072 return false;
1073
1074 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1075 return false;
1076
Bill Schmidt83973ef2014-06-24 20:05:18 +00001077 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1078 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1079 return false;
1080
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001081 Value *Src = I->getOperand(0);
1082 Type *SrcTy = Src->getType();
1083 if (!isTypeLegal(SrcTy, SrcVT))
1084 return false;
1085
1086 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1087 return false;
1088
1089 unsigned SrcReg = getRegForValue(Src);
1090 if (SrcReg == 0)
1091 return false;
1092
1093 // Convert f32 to f64 if necessary. This is just a meaningless copy
1094 // to get the register class right. COPY_TO_REGCLASS is needed since
1095 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1096 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1097 if (InRC == &PPC::F4RCRegClass) {
1098 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001099 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001100 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1101 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1102 SrcReg = TmpReg;
1103 }
1104
1105 // Determine the opcode for the conversion, which takes place
1106 // entirely within FPRs.
1107 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1108 unsigned Opc;
1109
1110 if (DstVT == MVT::i32)
1111 if (IsSigned)
1112 Opc = PPC::FCTIWZ;
1113 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001114 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001115 else
1116 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1117
1118 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001120 .addReg(SrcReg);
1121
1122 // Now move the integer value from a float register to an integer register.
1123 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1124 if (IntReg == 0)
1125 return false;
1126
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001127 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001128 return true;
1129}
1130
Bill Schmidtccecf262013-08-30 02:29:45 +00001131// Attempt to fast-select a binary integer operation that isn't already
1132// handled automatically.
1133bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1134 EVT DestVT = TLI.getValueType(I->getType(), true);
1135
1136 // We can get here in the case when we have a binary operation on a non-legal
1137 // type and the target independent selector doesn't know how to handle it.
1138 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1139 return false;
1140
1141 // Look at the currently assigned register for this instruction
1142 // to determine the required register class. If there is no register,
1143 // make a conservative choice (don't assign R0).
1144 unsigned AssignedReg = FuncInfo.ValueMap[I];
1145 const TargetRegisterClass *RC =
1146 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1147 &PPC::GPRC_and_GPRC_NOR0RegClass);
1148 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1149
1150 unsigned Opc;
1151 switch (ISDOpcode) {
1152 default: return false;
1153 case ISD::ADD:
1154 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1155 break;
1156 case ISD::OR:
1157 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1158 break;
1159 case ISD::SUB:
1160 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1161 break;
1162 }
1163
1164 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1165 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1166 if (SrcReg1 == 0) return false;
1167
1168 // Handle case of small immediate operand.
1169 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1170 const APInt &CIVal = ConstInt->getValue();
1171 int Imm = (int)CIVal.getSExtValue();
1172 bool UseImm = true;
1173 if (isInt<16>(Imm)) {
1174 switch (Opc) {
1175 default:
1176 llvm_unreachable("Missing case!");
1177 case PPC::ADD4:
1178 Opc = PPC::ADDI;
1179 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1180 break;
1181 case PPC::ADD8:
1182 Opc = PPC::ADDI8;
1183 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1184 break;
1185 case PPC::OR:
1186 Opc = PPC::ORI;
1187 break;
1188 case PPC::OR8:
1189 Opc = PPC::ORI8;
1190 break;
1191 case PPC::SUBF:
1192 if (Imm == -32768)
1193 UseImm = false;
1194 else {
1195 Opc = PPC::ADDI;
1196 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1197 Imm = -Imm;
1198 }
1199 break;
1200 case PPC::SUBF8:
1201 if (Imm == -32768)
1202 UseImm = false;
1203 else {
1204 Opc = PPC::ADDI8;
1205 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1206 Imm = -Imm;
1207 }
1208 break;
1209 }
1210
1211 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1213 ResultReg)
1214 .addReg(SrcReg1)
1215 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001216 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001217 return true;
1218 }
1219 }
1220 }
1221
1222 // Reg-reg case.
1223 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1224 if (SrcReg2 == 0) return false;
1225
1226 // Reverse operands for subtract-from.
1227 if (ISDOpcode == ISD::SUB)
1228 std::swap(SrcReg1, SrcReg2);
1229
Rafael Espindolaea09c592014-02-18 22:05:46 +00001230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001231 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001232 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001233 return true;
1234}
1235
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001236// Handle arguments to a call that we're attempting to fast-select.
1237// Return false if the arguments are too complex for us at the moment.
1238bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1239 SmallVectorImpl<unsigned> &ArgRegs,
1240 SmallVectorImpl<MVT> &ArgVTs,
1241 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1242 SmallVectorImpl<unsigned> &RegArgs,
1243 CallingConv::ID CC,
1244 unsigned &NumBytes,
1245 bool IsVarArg) {
1246 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001247 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001248
1249 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001250 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001251 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001252
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001253 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1254
1255 // Bail out if we can't handle any of the arguments.
1256 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1257 CCValAssign &VA = ArgLocs[I];
1258 MVT ArgVT = ArgVTs[VA.getValNo()];
1259
1260 // Skip vector arguments for now, as well as long double and
1261 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001262 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001263 !VA.isRegLoc() || VA.needsCustom())
1264 return false;
1265
1266 // Skip bit-converted arguments for now.
1267 if (VA.getLocInfo() == CCValAssign::BCvt)
1268 return false;
1269 }
1270
1271 // Get a count of how many bytes are to be pushed onto the stack.
1272 NumBytes = CCInfo.getNextStackOffset();
1273
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001274 // The prolog code of the callee may store up to 8 GPR argument registers to
1275 // the stack, allowing va_start to index over them in memory if its varargs.
1276 // Because we cannot tell if this is needed on the caller side, we have to
1277 // conservatively assume that it is needed. As such, make sure we have at
1278 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001279 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001280 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001281
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001282 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001284 TII.get(TII.getCallFrameSetupOpcode()))
1285 .addImm(NumBytes);
1286
1287 // Prepare to assign register arguments. Every argument uses up a
1288 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001289 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001290 unsigned NextGPR = PPC::X3;
1291 unsigned NextFPR = PPC::F1;
1292
1293 // Process arguments.
1294 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1295 CCValAssign &VA = ArgLocs[I];
1296 unsigned Arg = ArgRegs[VA.getValNo()];
1297 MVT ArgVT = ArgVTs[VA.getValNo()];
1298
1299 // Handle argument promotion and bitcasts.
1300 switch (VA.getLocInfo()) {
1301 default:
1302 llvm_unreachable("Unknown loc info!");
1303 case CCValAssign::Full:
1304 break;
1305 case CCValAssign::SExt: {
1306 MVT DestVT = VA.getLocVT();
1307 const TargetRegisterClass *RC =
1308 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1309 unsigned TmpReg = createResultReg(RC);
1310 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1311 llvm_unreachable("Failed to emit a sext!");
1312 ArgVT = DestVT;
1313 Arg = TmpReg;
1314 break;
1315 }
1316 case CCValAssign::AExt:
1317 case CCValAssign::ZExt: {
1318 MVT DestVT = VA.getLocVT();
1319 const TargetRegisterClass *RC =
1320 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1321 unsigned TmpReg = createResultReg(RC);
1322 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1323 llvm_unreachable("Failed to emit a zext!");
1324 ArgVT = DestVT;
1325 Arg = TmpReg;
1326 break;
1327 }
1328 case CCValAssign::BCvt: {
1329 // FIXME: Not yet handled.
1330 llvm_unreachable("Should have bailed before getting here!");
1331 break;
1332 }
1333 }
1334
1335 // Copy this argument to the appropriate register.
1336 unsigned ArgReg;
1337 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1338 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001339 if (CC != CallingConv::Fast)
1340 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001341 } else
1342 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001343
1344 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1345 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001346 RegArgs.push_back(ArgReg);
1347 }
1348
1349 return true;
1350}
1351
1352// For a call that we've determined we can fast-select, finish the
1353// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001354bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1355 CallingConv::ID CC = CLI.CallConv;
1356
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001357 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001358 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001359 TII.get(TII.getCallFrameDestroyOpcode()))
1360 .addImm(NumBytes).addImm(0);
1361
1362 // Next, generate a copy to obtain the return value.
1363 // FIXME: No multi-register return values yet, though I don't foresee
1364 // any real difficulties there.
1365 if (RetVT != MVT::isVoid) {
1366 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001367 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001368 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1369 CCValAssign &VA = RVLocs[0];
1370 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1371 assert(VA.isRegLoc() && "Can only return in registers!");
1372
1373 MVT DestVT = VA.getValVT();
1374 MVT CopyVT = DestVT;
1375
1376 // Ints smaller than a register still arrive in a full 64-bit
1377 // register, so make sure we recognize this.
1378 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1379 CopyVT = MVT::i64;
1380
1381 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001382 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001383
1384 if (RetVT == CopyVT) {
1385 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1386 ResultReg = createResultReg(CpyRC);
1387
Rafael Espindolaea09c592014-02-18 22:05:46 +00001388 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001389 TII.get(TargetOpcode::COPY), ResultReg)
1390 .addReg(SourcePhysReg);
1391
1392 // If necessary, round the floating result to single precision.
1393 } else if (CopyVT == MVT::f64) {
1394 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001395 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001396 ResultReg).addReg(SourcePhysReg);
1397
1398 // If only the low half of a general register is needed, generate
1399 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1400 // used along the fast-isel path (not lowered), and downstream logic
1401 // also doesn't like a direct subreg copy on a physical reg.)
1402 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1403 ResultReg = createResultReg(&PPC::GPRCRegClass);
1404 // Convert physical register from G8RC to GPRC.
1405 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001407 TII.get(TargetOpcode::COPY), ResultReg)
1408 .addReg(SourcePhysReg);
1409 }
1410
Bill Schmidt0954ea12013-08-30 23:25:30 +00001411 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001412 CLI.InRegs.push_back(SourcePhysReg);
1413 CLI.ResultReg = ResultReg;
1414 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001415 }
Hal Finkel934361a2015-01-14 01:07:51 +00001416
1417 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001418}
1419
Hal Finkel934361a2015-01-14 01:07:51 +00001420bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1421 CallingConv::ID CC = CLI.CallConv;
1422 bool IsTailCall = CLI.IsTailCall;
1423 bool IsVarArg = CLI.IsVarArg;
1424 const Value *Callee = CLI.Callee;
1425 const char *SymName = CLI.SymName;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001426
Hal Finkel934361a2015-01-14 01:07:51 +00001427 if (!Callee && !SymName)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001428 return false;
1429
1430 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001431 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001432 return false;
1433
Hal Finkel934361a2015-01-14 01:07:51 +00001434 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001435 if (IsVarArg)
1436 return false;
1437
1438 // Handle simple calls for now, with legal return types and
1439 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001440 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001441 MVT RetVT;
1442 if (RetTy->isVoidTy())
1443 RetVT = MVT::isVoid;
1444 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1445 RetVT != MVT::i8)
1446 return false;
1447
1448 // FIXME: No multi-register return values yet.
1449 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1450 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1451 RetVT != MVT::f64) {
1452 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001453 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001454 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1455 if (RVLocs.size() > 1)
1456 return false;
1457 }
1458
1459 // Bail early if more than 8 arguments, as we only currently
1460 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001461 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001462 if (NumArgs > 8)
1463 return false;
1464
1465 // Set up the argument vectors.
1466 SmallVector<Value*, 8> Args;
1467 SmallVector<unsigned, 8> ArgRegs;
1468 SmallVector<MVT, 8> ArgVTs;
1469 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1470
1471 Args.reserve(NumArgs);
1472 ArgRegs.reserve(NumArgs);
1473 ArgVTs.reserve(NumArgs);
1474 ArgFlags.reserve(NumArgs);
1475
Hal Finkel934361a2015-01-14 01:07:51 +00001476 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001477 // Only handle easy calls for now. It would be reasonably easy
1478 // to handle <= 8-byte structures passed ByVal in registers, but we
1479 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001480 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1481 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001482 return false;
1483
Hal Finkel934361a2015-01-14 01:07:51 +00001484 Value *ArgValue = CLI.OutVals[i];
1485 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001486 MVT ArgVT;
1487 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1488 return false;
1489
1490 if (ArgVT.isVector())
1491 return false;
1492
Hal Finkel934361a2015-01-14 01:07:51 +00001493 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001494 if (Arg == 0)
1495 return false;
1496
Hal Finkel934361a2015-01-14 01:07:51 +00001497 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001498 ArgRegs.push_back(Arg);
1499 ArgVTs.push_back(ArgVT);
1500 ArgFlags.push_back(Flags);
1501 }
1502
1503 // Process the arguments.
1504 SmallVector<unsigned, 8> RegArgs;
1505 unsigned NumBytes;
1506
1507 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1508 RegArgs, CC, NumBytes, IsVarArg))
1509 return false;
1510
Hal Finkel934361a2015-01-14 01:07:51 +00001511 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001512 // FIXME: No handling for function pointers yet. This requires
1513 // implementing the function descriptor (OPD) setup.
1514 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001515 if (!GV) {
1516 // patchpoints are a special case; they always dispatch to a pointer value.
1517 // However, we don't actually want to generate the indirect call sequence
1518 // here (that will be generated, as necessary, during asm printing), and
1519 // the call we generate here will be erased by FastISel::selectPatchpoint,
1520 // so don't try very hard...
1521 if (CLI.IsPatchPoint)
1522 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1523 else
1524 return false;
1525 } else {
1526 // Build direct call with NOP for TOC restore.
1527 // FIXME: We can and should optimize away the NOP for local calls.
1528 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1529 TII.get(PPC::BL8_NOP));
1530 // Add callee.
1531 MIB.addGlobalAddress(GV);
1532 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001533
1534 // Add implicit physical register uses to the call.
1535 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1536 MIB.addReg(RegArgs[II], RegState::Implicit);
1537
Hal Finkelaf519932015-01-19 07:20:27 +00001538 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1539 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001540 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001541 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001542
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001543 // Add a register mask with the call-preserved registers. Proper
1544 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001545 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001546
Hal Finkel934361a2015-01-14 01:07:51 +00001547 CLI.Call = MIB;
1548
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001549 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001550 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001551}
1552
Bill Schmidtd89f6782013-08-26 19:42:51 +00001553// Attempt to fast-select a return instruction.
1554bool PPCFastISel::SelectRet(const Instruction *I) {
1555
1556 if (!FuncInfo.CanLowerReturn)
1557 return false;
1558
1559 const ReturnInst *Ret = cast<ReturnInst>(I);
1560 const Function &F = *I->getParent()->getParent();
1561
1562 // Build a list of return value registers.
1563 SmallVector<unsigned, 4> RetRegs;
1564 CallingConv::ID CC = F.getCallingConv();
1565
1566 if (Ret->getNumOperands() > 0) {
1567 SmallVector<ISD::OutputArg, 4> Outs;
1568 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1569
1570 // Analyze operands of the call, assigning locations to each operand.
1571 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001572 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001573 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1574 const Value *RV = Ret->getOperand(0);
1575
1576 // FIXME: Only one output register for now.
1577 if (ValLocs.size() > 1)
1578 return false;
1579
1580 // Special case for returning a constant integer of any size.
1581 // Materialize the constant as an i64 and copy it to the return
Samuel Antao61570df2014-09-17 23:25:06 +00001582 // register. We still need to worry about properly extending the sign. E.g:
1583 // If the constant has only one bit, it means it is a boolean. Therefore
1584 // we can't use PPCMaterializeInt because it extends the sign which will
1585 // cause negations of the returned value to be incorrect as they are
1586 // implemented as the flip of the least significant bit.
Bill Schmidtd89f6782013-08-26 19:42:51 +00001587 if (isa<ConstantInt>(*RV)) {
1588 const Constant *C = cast<Constant>(RV);
Samuel Antao61570df2014-09-17 23:25:06 +00001589
1590 CCValAssign &VA = ValLocs[0];
1591
1592 unsigned RetReg = VA.getLocReg();
1593 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64,
1594 VA.getLocInfo() == CCValAssign::SExt);
1595
Rafael Espindolaea09c592014-02-18 22:05:46 +00001596 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001597 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1598
Bill Schmidtd89f6782013-08-26 19:42:51 +00001599 RetRegs.push_back(RetReg);
1600
1601 } else {
1602 unsigned Reg = getRegForValue(RV);
1603
1604 if (Reg == 0)
1605 return false;
1606
1607 // Copy the result values into the output registers.
1608 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1609
1610 CCValAssign &VA = ValLocs[i];
1611 assert(VA.isRegLoc() && "Can only return in registers!");
1612 RetRegs.push_back(VA.getLocReg());
1613 unsigned SrcReg = Reg + VA.getValNo();
1614
1615 EVT RVEVT = TLI.getValueType(RV->getType());
1616 if (!RVEVT.isSimple())
1617 return false;
1618 MVT RVVT = RVEVT.getSimpleVT();
1619 MVT DestVT = VA.getLocVT();
1620
1621 if (RVVT != DestVT && RVVT != MVT::i8 &&
1622 RVVT != MVT::i16 && RVVT != MVT::i32)
1623 return false;
1624
1625 if (RVVT != DestVT) {
1626 switch (VA.getLocInfo()) {
1627 default:
1628 llvm_unreachable("Unknown loc info!");
1629 case CCValAssign::Full:
1630 llvm_unreachable("Full value assign but types don't match?");
1631 case CCValAssign::AExt:
1632 case CCValAssign::ZExt: {
1633 const TargetRegisterClass *RC =
1634 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1635 unsigned TmpReg = createResultReg(RC);
1636 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1637 return false;
1638 SrcReg = TmpReg;
1639 break;
1640 }
1641 case CCValAssign::SExt: {
1642 const TargetRegisterClass *RC =
1643 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1644 unsigned TmpReg = createResultReg(RC);
1645 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1646 return false;
1647 SrcReg = TmpReg;
1648 break;
1649 }
1650 }
1651 }
1652
Rafael Espindolaea09c592014-02-18 22:05:46 +00001653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001654 TII.get(TargetOpcode::COPY), RetRegs[i])
1655 .addReg(SrcReg);
1656 }
1657 }
1658 }
1659
Rafael Espindolaea09c592014-02-18 22:05:46 +00001660 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001661 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001662
1663 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1664 MIB.addReg(RetRegs[i], RegState::Implicit);
1665
1666 return true;
1667}
1668
Bill Schmidt03008132013-08-25 22:33:42 +00001669// Attempt to emit an integer extend of SrcReg into DestReg. Both
1670// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001671// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001672bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1673 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001674 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1675 return false;
1676 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1677 return false;
1678
1679 // Signed extensions use EXTSB, EXTSH, EXTSW.
1680 if (!IsZExt) {
1681 unsigned Opc;
1682 if (SrcVT == MVT::i8)
1683 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1684 else if (SrcVT == MVT::i16)
1685 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1686 else {
1687 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1688 Opc = PPC::EXTSW_32_64;
1689 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001691 .addReg(SrcReg);
1692
1693 // Unsigned 32-bit extensions use RLWINM.
1694 } else if (DestVT == MVT::i32) {
1695 unsigned MB;
1696 if (SrcVT == MVT::i8)
1697 MB = 24;
1698 else {
1699 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1700 MB = 16;
1701 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001703 DestReg)
1704 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1705
1706 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1707 } else {
1708 unsigned MB;
1709 if (SrcVT == MVT::i8)
1710 MB = 56;
1711 else if (SrcVT == MVT::i16)
1712 MB = 48;
1713 else
1714 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001715 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001716 TII.get(PPC::RLDICL_32_64), DestReg)
1717 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1718 }
1719
1720 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001721}
1722
1723// Attempt to fast-select an indirect branch instruction.
1724bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1725 unsigned AddrReg = getRegForValue(I->getOperand(0));
1726 if (AddrReg == 0)
1727 return false;
1728
Rafael Espindolaea09c592014-02-18 22:05:46 +00001729 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001730 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001731 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001732
1733 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1734 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1735 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1736
1737 return true;
1738}
1739
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001740// Attempt to fast-select an integer truncate instruction.
1741bool PPCFastISel::SelectTrunc(const Instruction *I) {
1742 Value *Src = I->getOperand(0);
1743 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1744 EVT DestVT = TLI.getValueType(I->getType(), true);
1745
1746 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1747 return false;
1748
1749 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1750 return false;
1751
1752 unsigned SrcReg = getRegForValue(Src);
1753 if (!SrcReg)
1754 return false;
1755
1756 // The only interesting case is when we need to switch register classes.
1757 if (SrcVT == MVT::i64) {
1758 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001759 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1760 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001761 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1762 SrcReg = ResultReg;
1763 }
1764
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001765 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001766 return true;
1767}
1768
Bill Schmidtd89f6782013-08-26 19:42:51 +00001769// Attempt to fast-select an integer extend instruction.
1770bool PPCFastISel::SelectIntExt(const Instruction *I) {
1771 Type *DestTy = I->getType();
1772 Value *Src = I->getOperand(0);
1773 Type *SrcTy = Src->getType();
1774
1775 bool IsZExt = isa<ZExtInst>(I);
1776 unsigned SrcReg = getRegForValue(Src);
1777 if (!SrcReg) return false;
1778
1779 EVT SrcEVT, DestEVT;
1780 SrcEVT = TLI.getValueType(SrcTy, true);
1781 DestEVT = TLI.getValueType(DestTy, true);
1782 if (!SrcEVT.isSimple())
1783 return false;
1784 if (!DestEVT.isSimple())
1785 return false;
1786
1787 MVT SrcVT = SrcEVT.getSimpleVT();
1788 MVT DestVT = DestEVT.getSimpleVT();
1789
1790 // If we know the register class needed for the result of this
1791 // instruction, use it. Otherwise pick the register class of the
1792 // correct size that does not contain X0/R0, since we don't know
1793 // whether downstream uses permit that assignment.
1794 unsigned AssignedReg = FuncInfo.ValueMap[I];
1795 const TargetRegisterClass *RC =
1796 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1797 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1798 &PPC::GPRC_and_GPRC_NOR0RegClass));
1799 unsigned ResultReg = createResultReg(RC);
1800
1801 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1802 return false;
1803
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001804 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001805 return true;
1806}
1807
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001808// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001809// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001810bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001811
1812 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001813 case Instruction::Load:
1814 return SelectLoad(I);
1815 case Instruction::Store:
1816 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001817 case Instruction::Br:
1818 return SelectBranch(I);
1819 case Instruction::IndirectBr:
1820 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001821 case Instruction::FPExt:
1822 return SelectFPExt(I);
1823 case Instruction::FPTrunc:
1824 return SelectFPTrunc(I);
1825 case Instruction::SIToFP:
1826 return SelectIToFP(I, /*IsSigned*/ true);
1827 case Instruction::UIToFP:
1828 return SelectIToFP(I, /*IsSigned*/ false);
1829 case Instruction::FPToSI:
1830 return SelectFPToI(I, /*IsSigned*/ true);
1831 case Instruction::FPToUI:
1832 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001833 case Instruction::Add:
1834 return SelectBinaryIntOp(I, ISD::ADD);
1835 case Instruction::Or:
1836 return SelectBinaryIntOp(I, ISD::OR);
1837 case Instruction::Sub:
1838 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001839 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001840 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001841 case Instruction::Ret:
1842 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001843 case Instruction::Trunc:
1844 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001845 case Instruction::ZExt:
1846 case Instruction::SExt:
1847 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001848 // Here add other flavors of Instruction::XXX that automated
1849 // cases don't catch. For example, switches are terminators
1850 // that aren't yet handled.
1851 default:
1852 break;
1853 }
1854 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001855}
1856
1857// Materialize a floating-point constant into a register, and return
1858// the register number (or zero if we failed to handle it).
1859unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1860 // No plans to handle long double here.
1861 if (VT != MVT::f32 && VT != MVT::f64)
1862 return 0;
1863
1864 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001865 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001866 assert(Align > 0 && "Unexpectedly missing alignment information!");
1867 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1868 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1869 CodeModel::Model CModel = TM.getCodeModel();
1870
1871 MachineMemOperand *MMO =
1872 FuncInfo.MF->getMachineMemOperand(
1873 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1874 (VT == MVT::f32) ? 4 : 8, Align);
1875
Bill Schmidt03008132013-08-25 22:33:42 +00001876 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1877 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1878
Hal Finkele6698d52015-02-01 15:03:28 +00001879 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001880 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1881 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001882 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001883 TmpReg)
1884 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001885 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001886 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1887 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001888 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001890 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001891 // But for large code model, we must generate a LDtocL followed
1892 // by the LF[SD].
1893 if (CModel == CodeModel::Large) {
1894 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001896 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001897 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001898 .addImm(0).addReg(TmpReg2);
1899 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001900 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001901 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1902 .addReg(TmpReg)
1903 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001904 }
1905
1906 return DestReg;
1907}
1908
Bill Schmidtccecf262013-08-30 02:29:45 +00001909// Materialize the address of a global value into a register, and return
1910// the register number (or zero if we failed to handle it).
1911unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1912 assert(VT == MVT::i64 && "Non-address!");
1913 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1914 unsigned DestReg = createResultReg(RC);
1915
1916 // Global values may be plain old object addresses, TLS object
1917 // addresses, constant pool entries, or jump tables. How we generate
1918 // code for these may depend on small, medium, or large code model.
1919 CodeModel::Model CModel = TM.getCodeModel();
1920
1921 // FIXME: Jump tables are not yet required because fast-isel doesn't
1922 // handle switches; if that changes, we need them as well. For now,
1923 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001924
1925 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001926 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001927 return 0;
1928
Hal Finkele6698d52015-02-01 15:03:28 +00001929 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001930 // For small code model, generate a simple TOC load.
1931 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001932 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1933 DestReg)
1934 .addGlobalAddress(GV)
1935 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001936 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001937 // If the address is an externally defined symbol, a symbol with common
1938 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001939 // jump table address (not yet needed), or if we are generating code
1940 // for large code model, we generate:
1941 // LDtocL(GV, ADDIStocHA(%X2, GV))
1942 // Otherwise we generate:
1943 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1944 // Either way, start with the ADDIStocHA:
1945 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001947 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1948
Bill Schmidtccecf262013-08-30 02:29:45 +00001949 // If/when switches are implemented, jump tables should be handled
1950 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001951 if (CModel == CodeModel::Large ||
1952 (GV->getType()->getElementType()->isFunctionTy() &&
1953 (GV->isDeclaration() || GV->isWeakForLinker())) ||
1954 GV->isDeclaration() || GV->hasCommonLinkage() ||
1955 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001956 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001957 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1958 else
1959 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001961 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1962 }
1963
1964 return DestReg;
1965}
1966
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001967// Materialize a 32-bit integer constant into a register, and return
1968// the register number (or zero if we failed to handle it).
1969unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1970 const TargetRegisterClass *RC) {
1971 unsigned Lo = Imm & 0xFFFF;
1972 unsigned Hi = (Imm >> 16) & 0xFFFF;
1973
1974 unsigned ResultReg = createResultReg(RC);
1975 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1976
1977 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001979 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
1980 .addImm(Imm);
1981 else if (Lo) {
1982 // Both Lo and Hi have nonzero bits.
1983 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001985 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
1986 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001987 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001988 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
1989 .addReg(TmpReg).addImm(Lo);
1990 } else
1991 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001992 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001993 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
1994 .addImm(Hi);
1995
1996 return ResultReg;
1997}
1998
1999// Materialize a 64-bit integer constant into a register, and return
2000// the register number (or zero if we failed to handle it).
2001unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2002 const TargetRegisterClass *RC) {
2003 unsigned Remainder = 0;
2004 unsigned Shift = 0;
2005
2006 // If the value doesn't fit in 32 bits, see if we can shift it
2007 // so that it fits in 32 bits.
2008 if (!isInt<32>(Imm)) {
2009 Shift = countTrailingZeros<uint64_t>(Imm);
2010 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2011
2012 if (isInt<32>(ImmSh))
2013 Imm = ImmSh;
2014 else {
2015 Remainder = Imm;
2016 Shift = 32;
2017 Imm >>= 32;
2018 }
2019 }
2020
2021 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2022 // (if not shifted).
2023 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2024 if (!Shift)
2025 return TmpReg1;
2026
2027 // If upper 32 bits were not zero, we've built them and need to shift
2028 // them into place.
2029 unsigned TmpReg2;
2030 if (Imm) {
2031 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002032 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002033 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2034 } else
2035 TmpReg2 = TmpReg1;
2036
2037 unsigned TmpReg3, Hi, Lo;
2038 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2039 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002041 TmpReg3).addReg(TmpReg2).addImm(Hi);
2042 } else
2043 TmpReg3 = TmpReg2;
2044
2045 if ((Lo = Remainder & 0xFFFF)) {
2046 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002048 ResultReg).addReg(TmpReg3).addImm(Lo);
2049 return ResultReg;
2050 }
2051
2052 return TmpReg3;
2053}
2054
2055
2056// Materialize an integer constant into a register, and return
2057// the register number (or zero if we failed to handle it).
Samuel Antao61570df2014-09-17 23:25:06 +00002058unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT,
2059 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002060 // If we're using CR bit registers for i1 values, handle that as a special
2061 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002062 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002063 const ConstantInt *CI = cast<ConstantInt>(C);
2064 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2066 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2067 return ImmReg;
2068 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002069
2070 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2071 VT != MVT::i8 && VT != MVT::i1)
2072 return 0;
2073
2074 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2075 &PPC::GPRCRegClass);
2076
2077 // If the constant is in range, use a load-immediate.
2078 const ConstantInt *CI = cast<ConstantInt>(C);
2079 if (isInt<16>(CI->getSExtValue())) {
2080 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2081 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002082 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Samuel Antao61570df2014-09-17 23:25:06 +00002083 .addImm( (UseSExt) ? CI->getSExtValue() : CI->getZExtValue() );
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002084 return ImmReg;
2085 }
2086
2087 // Construct the constant piecewise.
2088 int64_t Imm = CI->getZExtValue();
2089
2090 if (VT == MVT::i64)
2091 return PPCMaterialize64BitInt(Imm, RC);
2092 else if (VT == MVT::i32)
2093 return PPCMaterialize32BitInt(Imm, RC);
2094
2095 return 0;
2096}
2097
2098// Materialize a constant into a register, and return the register
2099// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002100unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002101 EVT CEVT = TLI.getValueType(C->getType(), true);
2102
2103 // Only handle simple types.
2104 if (!CEVT.isSimple()) return 0;
2105 MVT VT = CEVT.getSimpleVT();
2106
2107 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2108 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002109 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2110 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002111 else if (isa<ConstantInt>(C))
Hal Finkel0c505b02014-12-25 23:08:25 +00002112 return PPCMaterializeInt(C, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002113
2114 return 0;
2115}
2116
2117// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002118// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002119unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002120 // Don't handle dynamic allocas.
2121 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2122
2123 MVT VT;
2124 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2125
2126 DenseMap<const AllocaInst*, int>::iterator SI =
2127 FuncInfo.StaticAllocaMap.find(AI);
2128
2129 if (SI != FuncInfo.StaticAllocaMap.end()) {
2130 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002131 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002132 ResultReg).addFrameIndex(SI->second).addImm(0);
2133 return ResultReg;
2134 }
2135
2136 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002137}
2138
Bill Schmidtccecf262013-08-30 02:29:45 +00002139// Fold loads into extends when possible.
2140// FIXME: We can have multiple redundant extend/trunc instructions
2141// following a load. The folding only picks up one. Extend this
2142// to check subsequent instructions for the same pattern and remove
2143// them. Thus ResultReg should be the def reg for the last redundant
2144// instruction in a chain, and all intervening instructions can be
2145// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2146// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002147bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2148 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002149 // Verify we have a legal type before going any further.
2150 MVT VT;
2151 if (!isLoadTypeLegal(LI->getType(), VT))
2152 return false;
2153
2154 // Combine load followed by zero- or sign-extend.
2155 bool IsZExt = false;
2156 switch(MI->getOpcode()) {
2157 default:
2158 return false;
2159
2160 case PPC::RLDICL:
2161 case PPC::RLDICL_32_64: {
2162 IsZExt = true;
2163 unsigned MB = MI->getOperand(3).getImm();
2164 if ((VT == MVT::i8 && MB <= 56) ||
2165 (VT == MVT::i16 && MB <= 48) ||
2166 (VT == MVT::i32 && MB <= 32))
2167 break;
2168 return false;
2169 }
2170
2171 case PPC::RLWINM:
2172 case PPC::RLWINM8: {
2173 IsZExt = true;
2174 unsigned MB = MI->getOperand(3).getImm();
2175 if ((VT == MVT::i8 && MB <= 24) ||
2176 (VT == MVT::i16 && MB <= 16))
2177 break;
2178 return false;
2179 }
2180
2181 case PPC::EXTSB:
2182 case PPC::EXTSB8:
2183 case PPC::EXTSB8_32_64:
2184 /* There is no sign-extending load-byte instruction. */
2185 return false;
2186
2187 case PPC::EXTSH:
2188 case PPC::EXTSH8:
2189 case PPC::EXTSH8_32_64: {
2190 if (VT != MVT::i16 && VT != MVT::i8)
2191 return false;
2192 break;
2193 }
2194
2195 case PPC::EXTSW:
2196 case PPC::EXTSW_32_64: {
2197 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2198 return false;
2199 break;
2200 }
2201 }
2202
2203 // See if we can handle this address.
2204 Address Addr;
2205 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2206 return false;
2207
2208 unsigned ResultReg = MI->getOperand(0).getReg();
2209
Craig Topper062a2ba2014-04-25 05:30:21 +00002210 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002211 return false;
2212
2213 MI->eraseFromParent();
2214 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002215}
2216
2217// Attempt to lower call arguments in a faster way than done by
2218// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002219bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002220 // Defer to normal argument lowering for now. It's reasonably
2221 // efficient. Consider doing something like ARM to handle the
2222 // case where all args fit in registers, no varargs, no float
2223 // or vector args.
2224 return false;
2225}
2226
Bill Schmidt03008132013-08-25 22:33:42 +00002227// Handle materializing integer constants into a register. This is not
2228// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002229unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002230
2231 if (Opc != ISD::Constant)
2232 return 0;
2233
Hal Finkel940ab932014-02-28 00:27:01 +00002234 // If we're using CR bit registers for i1 values, handle that as a special
2235 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002236 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002237 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2239 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2240 return ImmReg;
2241 }
2242
Bill Schmidt03008132013-08-25 22:33:42 +00002243 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2244 VT != MVT::i8 && VT != MVT::i1)
2245 return 0;
2246
2247 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2248 &PPC::GPRCRegClass);
2249 if (VT == MVT::i64)
2250 return PPCMaterialize64BitInt(Imm, RC);
2251 else
2252 return PPCMaterialize32BitInt(Imm, RC);
2253}
2254
Bill Schmidtccecf262013-08-30 02:29:45 +00002255// Override for ADDI and ADDI8 to set the correct register class
2256// on RHS operand 0. The automatic infrastructure naively assumes
2257// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2258// for these cases. At the moment, none of the other automatically
2259// generated RI instructions require special treatment. However, once
2260// SelectSelect is implemented, "isel" requires similar handling.
2261//
2262// Also be conservative about the output register class. Avoid
2263// assigning R0 or X0 to the output register for GPRC and G8RC
2264// register classes, as any such result could be used in ADDI, etc.,
2265// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002266unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002267 const TargetRegisterClass *RC,
2268 unsigned Op0, bool Op0IsKill,
2269 uint64_t Imm) {
2270 if (MachineInstOpcode == PPC::ADDI)
2271 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2272 else if (MachineInstOpcode == PPC::ADDI8)
2273 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2274
2275 const TargetRegisterClass *UseRC =
2276 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2277 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2278
Juergen Ributzka88e32512014-09-03 20:56:59 +00002279 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002280 Op0, Op0IsKill, Imm);
2281}
2282
2283// Override for instructions with one register operand to avoid use of
2284// R0/X0. The automatic infrastructure isn't aware of the context so
2285// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002286unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002287 const TargetRegisterClass* RC,
2288 unsigned Op0, bool Op0IsKill) {
2289 const TargetRegisterClass *UseRC =
2290 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2291 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2292
Juergen Ributzka88e32512014-09-03 20:56:59 +00002293 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002294}
2295
2296// Override for instructions with two register operands to avoid use
2297// of R0/X0. The automatic infrastructure isn't aware of the context
2298// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002299unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002300 const TargetRegisterClass* RC,
2301 unsigned Op0, bool Op0IsKill,
2302 unsigned Op1, bool Op1IsKill) {
2303 const TargetRegisterClass *UseRC =
2304 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2305 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2306
Juergen Ributzka88e32512014-09-03 20:56:59 +00002307 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002308 Op1, Op1IsKill);
2309}
2310
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002311namespace llvm {
2312 // Create the fast instruction selector for PowerPC64 ELF.
2313 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2314 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002315 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002316 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002317 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002318 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002319 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002320 }
2321}