blob: 0b8e23c4ebf8f3fbd59d937541fd510c80803b56 [file] [log] [blame]
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001//===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PowerPC-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// PPCGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
Bill Schmidt0cf702f2013-07-30 00:50:39 +000016#include "PPC.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000018#include "PPCCallingConv.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000019#include "PPCISelLowering.h"
Hal Finkele6698d52015-02-01 15:03:28 +000020#include "PPCMachineFunctionInfo.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000021#include "PPCSubtarget.h"
22#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000023#include "llvm/ADT/Optional.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000032#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000033#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/IntrinsicInst.h"
36#include "llvm/IR/Operator.h"
37#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000038#include "llvm/Target/TargetLowering.h"
39#include "llvm/Target/TargetMachine.h"
40
Bill Schmidteb8d6f72013-08-31 02:33:40 +000041//===----------------------------------------------------------------------===//
42//
43// TBD:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +000044// fastLowerArguments: Handle simple cases.
Bill Schmidteb8d6f72013-08-31 02:33:40 +000045// PPCMaterializeGV: Handle TLS.
46// SelectCall: Handle function pointers.
47// SelectCall: Handle multi-register return values.
48// SelectCall: Optimize away nops for local calls.
49// processCallArgs: Handle bit-converted arguments.
50// finishCall: Handle multi-register return values.
51// PPCComputeAddress: Handle parameter references as FrameIndex's.
52// PPCEmitCmp: Handle immediate as operand 1.
53// SelectCall: Handle small byval arguments.
54// SelectIntrinsicCall: Implement.
55// SelectSelect: Implement.
56// Consider factoring isTypeLegal into the base class.
57// Implement switches and jump tables.
58//
59//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000060using namespace llvm;
61
Chandler Carruth84e68b22014-04-22 02:41:26 +000062#define DEBUG_TYPE "ppcfastisel"
63
Bill Schmidt0cf702f2013-07-30 00:50:39 +000064namespace {
65
66typedef struct Address {
67 enum {
68 RegBase,
69 FrameIndexBase
70 } BaseType;
71
72 union {
73 unsigned Reg;
74 int FI;
75 } Base;
76
Bill Schmidtccecf262013-08-30 02:29:45 +000077 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000078
79 // Innocuous defaults for our address.
80 Address()
81 : BaseType(RegBase), Offset(0) {
82 Base.Reg = 0;
83 }
84} Address;
85
Craig Topper26696312014-03-18 07:27:13 +000086class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000087
88 const TargetMachine &TM;
Eric Christopher85806142015-01-30 02:11:24 +000089 const PPCSubtarget *PPCSubTarget;
Hal Finkele6698d52015-02-01 15:03:28 +000090 PPCFunctionInfo *PPCFuncInfo;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000091 const TargetInstrInfo &TII;
92 const TargetLowering &TLI;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000093 LLVMContext *Context;
94
95 public:
96 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
97 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000098 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
Eric Christophercccae792015-01-30 22:02:31 +000099 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
Hal Finkele6698d52015-02-01 15:03:28 +0000100 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
Eric Christopher85806142015-01-30 02:11:24 +0000101 TII(*PPCSubTarget->getInstrInfo()),
102 TLI(*PPCSubTarget->getTargetLowering()),
Eric Christopherd9134482014-08-04 21:25:23 +0000103 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000104
105 // Backend specific FastISel code.
106 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000107 bool fastSelectInstruction(const Instruction *I) override;
108 unsigned fastMaterializeConstant(const Constant *C) override;
109 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000110 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
111 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000112 bool fastLowerArguments() override;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000113 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
114 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000118 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000121 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000125
Hal Finkel934361a2015-01-14 01:07:51 +0000126 bool fastLowerCall(CallLoweringInfo &CLI) override;
127
Bill Schmidt03008132013-08-25 22:33:42 +0000128 // Instruction selection routines.
129 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000130 bool SelectLoad(const Instruction *I);
131 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000132 bool SelectBranch(const Instruction *I);
133 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000134 bool SelectFPExt(const Instruction *I);
135 bool SelectFPTrunc(const Instruction *I);
136 bool SelectIToFP(const Instruction *I, bool IsSigned);
137 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000138 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000139 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000140 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000141 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000142
143 // Utility routines.
144 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000145 bool isTypeLegal(Type *Ty, MVT &VT);
146 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000147 bool isValueAvailable(const Value *V) const;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000148 bool isVSFRCRegister(unsigned Register) const {
149 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
150 }
Bill Schmidt03008132013-08-25 22:33:42 +0000151 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
152 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000153 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
154 const TargetRegisterClass *RC, bool IsZExt = true,
155 unsigned FP64LoadOpc = PPC::LFD);
156 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
157 bool PPCComputeAddress(const Value *Obj, Address &Addr);
158 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
159 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000160 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
161 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000162 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000163 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Samuel Antao61570df2014-09-17 23:25:06 +0000164 unsigned PPCMaterializeInt(const Constant *C, MVT VT, bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000165 unsigned PPCMaterialize32BitInt(int64_t Imm,
166 const TargetRegisterClass *RC);
167 unsigned PPCMaterialize64BitInt(int64_t Imm,
168 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000169 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
170 unsigned SrcReg, bool IsSigned);
171 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000172
Bill Schmidtd89f6782013-08-26 19:42:51 +0000173 // Call handling routines.
174 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000175 bool processCallArgs(SmallVectorImpl<Value*> &Args,
176 SmallVectorImpl<unsigned> &ArgRegs,
177 SmallVectorImpl<MVT> &ArgVTs,
178 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
179 SmallVectorImpl<unsigned> &RegArgs,
180 CallingConv::ID CC,
181 unsigned &NumBytes,
182 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000183 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000184 CCAssignFn *usePPC32CCs(unsigned Flag);
185
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000186 private:
187 #include "PPCGenFastISel.inc"
188
189};
190
191} // end anonymous namespace
192
Bill Schmidtd89f6782013-08-26 19:42:51 +0000193#include "PPCGenCallingConv.inc"
194
195// Function whose sole purpose is to kill compiler warnings
196// stemming from unused functions included from PPCGenCallingConv.inc.
197CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
198 if (Flag == 1)
199 return CC_PPC32_SVR4;
200 else if (Flag == 2)
201 return CC_PPC32_SVR4_ByVal;
202 else if (Flag == 3)
203 return CC_PPC32_SVR4_VarArg;
204 else
205 return RetCC_PPC;
206}
207
Bill Schmidt03008132013-08-25 22:33:42 +0000208static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
209 switch (Pred) {
210 // These are not representable with any single compare.
211 case CmpInst::FCMP_FALSE:
212 case CmpInst::FCMP_UEQ:
213 case CmpInst::FCMP_UGT:
214 case CmpInst::FCMP_UGE:
215 case CmpInst::FCMP_ULT:
216 case CmpInst::FCMP_ULE:
217 case CmpInst::FCMP_UNE:
218 case CmpInst::FCMP_TRUE:
219 default:
220 return Optional<PPC::Predicate>();
221
222 case CmpInst::FCMP_OEQ:
223 case CmpInst::ICMP_EQ:
224 return PPC::PRED_EQ;
225
226 case CmpInst::FCMP_OGT:
227 case CmpInst::ICMP_UGT:
228 case CmpInst::ICMP_SGT:
229 return PPC::PRED_GT;
230
231 case CmpInst::FCMP_OGE:
232 case CmpInst::ICMP_UGE:
233 case CmpInst::ICMP_SGE:
234 return PPC::PRED_GE;
235
236 case CmpInst::FCMP_OLT:
237 case CmpInst::ICMP_ULT:
238 case CmpInst::ICMP_SLT:
239 return PPC::PRED_LT;
240
241 case CmpInst::FCMP_OLE:
242 case CmpInst::ICMP_ULE:
243 case CmpInst::ICMP_SLE:
244 return PPC::PRED_LE;
245
246 case CmpInst::FCMP_ONE:
247 case CmpInst::ICMP_NE:
248 return PPC::PRED_NE;
249
250 case CmpInst::FCMP_ORD:
251 return PPC::PRED_NU;
252
253 case CmpInst::FCMP_UNO:
254 return PPC::PRED_UN;
255 }
256}
257
Bill Schmidtccecf262013-08-30 02:29:45 +0000258// Determine whether the type Ty is simple enough to be handled by
259// fast-isel, and return its equivalent machine type in VT.
260// FIXME: Copied directly from ARM -- factor into base class?
261bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
262 EVT Evt = TLI.getValueType(Ty, true);
263
264 // Only handle simple types.
265 if (Evt == MVT::Other || !Evt.isSimple()) return false;
266 VT = Evt.getSimpleVT();
267
268 // Handle all legal types, i.e. a register that will directly hold this
269 // value.
270 return TLI.isTypeLegal(VT);
271}
272
273// Determine whether the type Ty is simple enough to be handled by
274// fast-isel as a load target, and return its equivalent machine type in VT.
275bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
276 if (isTypeLegal(Ty, VT)) return true;
277
278 // If this is a type than can be sign or zero-extended to a basic operation
279 // go ahead and accept it now.
280 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
281 return true;
282 }
283
284 return false;
285}
286
Hal Finkel5f2a1372015-05-23 12:18:10 +0000287bool PPCFastISel::isValueAvailable(const Value *V) const {
288 if (!isa<Instruction>(V))
289 return true;
290
291 const auto *I = cast<Instruction>(V);
292 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
293 return true;
294
295 return false;
296}
297
Bill Schmidtccecf262013-08-30 02:29:45 +0000298// Given a value Obj, create an Address object Addr that represents its
299// address. Return false if we can't handle it.
300bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000301 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000302 unsigned Opcode = Instruction::UserOp1;
303 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
304 // Don't walk into other basic blocks unless the object is an alloca from
305 // another block, otherwise it may not have a virtual register assigned.
306 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
307 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
308 Opcode = I->getOpcode();
309 U = I;
310 }
311 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
312 Opcode = C->getOpcode();
313 U = C;
314 }
315
316 switch (Opcode) {
317 default:
318 break;
319 case Instruction::BitCast:
320 // Look through bitcasts.
321 return PPCComputeAddress(U->getOperand(0), Addr);
322 case Instruction::IntToPtr:
323 // Look past no-op inttoptrs.
324 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
325 return PPCComputeAddress(U->getOperand(0), Addr);
326 break;
327 case Instruction::PtrToInt:
328 // Look past no-op ptrtoints.
329 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
330 return PPCComputeAddress(U->getOperand(0), Addr);
331 break;
332 case Instruction::GetElementPtr: {
333 Address SavedAddr = Addr;
334 long TmpOffset = Addr.Offset;
335
336 // Iterate through the GEP folding the constants into offsets where
337 // we can.
338 gep_type_iterator GTI = gep_type_begin(U);
339 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
340 II != IE; ++II, ++GTI) {
341 const Value *Op = *II;
342 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000343 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000344 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
345 TmpOffset += SL->getElementOffset(Idx);
346 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000347 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000348 for (;;) {
349 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
350 // Constant-offset addressing.
351 TmpOffset += CI->getSExtValue() * S;
352 break;
353 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000354 if (canFoldAddIntoGEP(U, Op)) {
355 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000356 ConstantInt *CI =
357 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
358 TmpOffset += CI->getSExtValue() * S;
359 // Iterate on the other operand.
360 Op = cast<AddOperator>(Op)->getOperand(0);
361 continue;
362 }
363 // Unsupported
364 goto unsupported_gep;
365 }
366 }
367 }
368
369 // Try to grab the base operand now.
370 Addr.Offset = TmpOffset;
371 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
372
373 // We failed, restore everything and try the other options.
374 Addr = SavedAddr;
375
376 unsupported_gep:
377 break;
378 }
379 case Instruction::Alloca: {
380 const AllocaInst *AI = cast<AllocaInst>(Obj);
381 DenseMap<const AllocaInst*, int>::iterator SI =
382 FuncInfo.StaticAllocaMap.find(AI);
383 if (SI != FuncInfo.StaticAllocaMap.end()) {
384 Addr.BaseType = Address::FrameIndexBase;
385 Addr.Base.FI = SI->second;
386 return true;
387 }
388 break;
389 }
390 }
391
392 // FIXME: References to parameters fall through to the behavior
393 // below. They should be able to reference a frame index since
394 // they are stored to the stack, so we can get "ld rx, offset(r1)"
395 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
396 // just contain the parameter. Try to handle this with a FI.
397
398 // Try to get this in a register if nothing else has worked.
399 if (Addr.Base.Reg == 0)
400 Addr.Base.Reg = getRegForValue(Obj);
401
402 // Prevent assignment of base register to X0, which is inappropriate
403 // for loads and stores alike.
404 if (Addr.Base.Reg != 0)
405 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
406
407 return Addr.Base.Reg != 0;
408}
409
410// Fix up some addresses that can't be used directly. For example, if
411// an offset won't fit in an instruction field, we may need to move it
412// into an index register.
413void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
414 unsigned &IndexReg) {
415
416 // Check whether the offset fits in the instruction field.
417 if (!isInt<16>(Addr.Offset))
418 UseOffset = false;
419
420 // If this is a stack pointer and the offset needs to be simplified then
421 // put the alloca address into a register, set the base type back to
422 // register and continue. This should almost never happen.
423 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
424 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000426 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
427 Addr.Base.Reg = ResultReg;
428 Addr.BaseType = Address::RegBase;
429 }
430
431 if (!UseOffset) {
432 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
433 : Type::getInt64Ty(*Context));
434 const ConstantInt *Offset =
435 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
436 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
437 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
438 }
439}
440
441// Emit a load instruction if possible, returning true if we succeeded,
442// otherwise false. See commentary below for how the register class of
443// the load is determined.
444bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
445 const TargetRegisterClass *RC,
446 bool IsZExt, unsigned FP64LoadOpc) {
447 unsigned Opc;
448 bool UseOffset = true;
449
450 // If ResultReg is given, it determines the register class of the load.
451 // Otherwise, RC is the register class to use. If the result of the
452 // load isn't anticipated in this block, both may be zero, in which
453 // case we must make a conservative guess. In particular, don't assign
454 // R0 or X0 to the result register, as the result may be used in a load,
455 // store, add-immediate, or isel that won't permit this. (Though
456 // perhaps the spill and reload of live-exit values would handle this?)
457 const TargetRegisterClass *UseRC =
458 (ResultReg ? MRI.getRegClass(ResultReg) :
459 (RC ? RC :
460 (VT == MVT::f64 ? &PPC::F8RCRegClass :
461 (VT == MVT::f32 ? &PPC::F4RCRegClass :
462 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
463 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
464
465 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
466
467 switch (VT.SimpleTy) {
468 default: // e.g., vector types not handled
469 return false;
470 case MVT::i8:
471 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
472 break;
473 case MVT::i16:
474 Opc = (IsZExt ?
475 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
476 (Is32BitInt ? PPC::LHA : PPC::LHA8));
477 break;
478 case MVT::i32:
479 Opc = (IsZExt ?
480 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
481 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
482 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
483 UseOffset = false;
484 break;
485 case MVT::i64:
486 Opc = PPC::LD;
487 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
488 "64-bit load with 32-bit target??");
489 UseOffset = ((Addr.Offset & 3) == 0);
490 break;
491 case MVT::f32:
492 Opc = PPC::LFS;
493 break;
494 case MVT::f64:
495 Opc = FP64LoadOpc;
496 break;
497 }
498
499 // If necessary, materialize the offset into a register and use
500 // the indexed form. Also handle stack pointers with special needs.
501 unsigned IndexReg = 0;
502 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000503
504 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
505 // be used.
506 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
507 if (IsVSFRC && (Opc == PPC::LFD) &&
508 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
509 (Addr.Offset == 0)) {
510 UseOffset = false;
511 }
512
Bill Schmidtccecf262013-08-30 02:29:45 +0000513 if (ResultReg == 0)
514 ResultReg = createResultReg(UseRC);
515
516 // Note: If we still have a frame index here, we know the offset is
517 // in range, as otherwise PPCSimplifyAddress would have converted it
518 // into a RegBase.
519 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000520 // VSX only provides an indexed load.
521 if (IsVSFRC && Opc == PPC::LFD) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000522
523 MachineMemOperand *MMO =
524 FuncInfo.MF->getMachineMemOperand(
525 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
526 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
527 MFI.getObjectAlignment(Addr.Base.FI));
528
Rafael Espindolaea09c592014-02-18 22:05:46 +0000529 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000530 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
531
532 // Base reg with offset in range.
533 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000534 // VSX only provides an indexed load.
535 if (IsVSFRC && Opc == PPC::LFD) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000536
Rafael Espindolaea09c592014-02-18 22:05:46 +0000537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000538 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
539
540 // Indexed form.
541 } else {
542 // Get the RR opcode corresponding to the RI one. FIXME: It would be
543 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
544 // is hard to get at.
545 switch (Opc) {
546 default: llvm_unreachable("Unexpected opcode!");
547 case PPC::LBZ: Opc = PPC::LBZX; break;
548 case PPC::LBZ8: Opc = PPC::LBZX8; break;
549 case PPC::LHZ: Opc = PPC::LHZX; break;
550 case PPC::LHZ8: Opc = PPC::LHZX8; break;
551 case PPC::LHA: Opc = PPC::LHAX; break;
552 case PPC::LHA8: Opc = PPC::LHAX8; break;
553 case PPC::LWZ: Opc = PPC::LWZX; break;
554 case PPC::LWZ8: Opc = PPC::LWZX8; break;
555 case PPC::LWA: Opc = PPC::LWAX; break;
556 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
557 case PPC::LD: Opc = PPC::LDX; break;
558 case PPC::LFS: Opc = PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000559 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000560 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000562 .addReg(Addr.Base.Reg).addReg(IndexReg);
563 }
564
565 return true;
566}
567
568// Attempt to fast-select a load instruction.
569bool PPCFastISel::SelectLoad(const Instruction *I) {
570 // FIXME: No atomic loads are supported.
571 if (cast<LoadInst>(I)->isAtomic())
572 return false;
573
574 // Verify we have a legal type before going any further.
575 MVT VT;
576 if (!isLoadTypeLegal(I->getType(), VT))
577 return false;
578
579 // See if we can handle this address.
580 Address Addr;
581 if (!PPCComputeAddress(I->getOperand(0), Addr))
582 return false;
583
584 // Look at the currently assigned register for this instruction
585 // to determine the required register class. This is necessary
586 // to constrain RA from using R0/X0 when this is not legal.
587 unsigned AssignedReg = FuncInfo.ValueMap[I];
588 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000589 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000590
591 unsigned ResultReg = 0;
592 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
593 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000594 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000595 return true;
596}
597
598// Emit a store instruction to store SrcReg at Addr.
599bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
600 assert(SrcReg && "Nothing to store!");
601 unsigned Opc;
602 bool UseOffset = true;
603
604 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
605 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
606
607 switch (VT.SimpleTy) {
608 default: // e.g., vector types not handled
609 return false;
610 case MVT::i8:
611 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
612 break;
613 case MVT::i16:
614 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
615 break;
616 case MVT::i32:
617 assert(Is32BitInt && "Not GPRC for i32??");
618 Opc = PPC::STW;
619 break;
620 case MVT::i64:
621 Opc = PPC::STD;
622 UseOffset = ((Addr.Offset & 3) == 0);
623 break;
624 case MVT::f32:
625 Opc = PPC::STFS;
626 break;
627 case MVT::f64:
628 Opc = PPC::STFD;
629 break;
630 }
631
632 // If necessary, materialize the offset into a register and use
633 // the indexed form. Also handle stack pointers with special needs.
634 unsigned IndexReg = 0;
635 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
636
Bill Seurer8c728ae2014-12-05 20:15:56 +0000637 // If this is a potential VSX store with an offset of 0, a VSX indexed store
638 // can be used.
639 bool IsVSFRC = isVSFRCRegister(SrcReg);
640 if (IsVSFRC && (Opc == PPC::STFD) &&
641 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
642 (Addr.Offset == 0)) {
643 UseOffset = false;
644 }
645
Bill Schmidtccecf262013-08-30 02:29:45 +0000646 // Note: If we still have a frame index here, we know the offset is
647 // in range, as otherwise PPCSimplifyAddress would have converted it
648 // into a RegBase.
649 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000650 // VSX only provides an indexed store.
651 if (IsVSFRC && Opc == PPC::STFD) return false;
652
Bill Schmidtccecf262013-08-30 02:29:45 +0000653 MachineMemOperand *MMO =
654 FuncInfo.MF->getMachineMemOperand(
655 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset),
656 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
657 MFI.getObjectAlignment(Addr.Base.FI));
658
Rafael Espindolaea09c592014-02-18 22:05:46 +0000659 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
660 .addReg(SrcReg)
661 .addImm(Addr.Offset)
662 .addFrameIndex(Addr.Base.FI)
663 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000664
665 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000666 } else if (UseOffset) {
667 // VSX only provides an indexed store.
668 if (IsVSFRC && Opc == PPC::STFD) return false;
669
Rafael Espindolaea09c592014-02-18 22:05:46 +0000670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000671 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
672
673 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000674 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000675 // Get the RR opcode corresponding to the RI one. FIXME: It would be
676 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
677 // is hard to get at.
678 switch (Opc) {
679 default: llvm_unreachable("Unexpected opcode!");
680 case PPC::STB: Opc = PPC::STBX; break;
681 case PPC::STH : Opc = PPC::STHX; break;
682 case PPC::STW : Opc = PPC::STWX; break;
683 case PPC::STB8: Opc = PPC::STBX8; break;
684 case PPC::STH8: Opc = PPC::STHX8; break;
685 case PPC::STW8: Opc = PPC::STWX8; break;
686 case PPC::STD: Opc = PPC::STDX; break;
687 case PPC::STFS: Opc = PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000688 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000689 }
Samuel Antaof6815602015-03-17 15:00:57 +0000690
691 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
692 .addReg(SrcReg);
693
694 // If we have an index register defined we use it in the store inst,
695 // otherwise we use X0 as base as it makes the vector instructions to
696 // use zero in the computation of the effective address regardless the
697 // content of the register.
698 if (IndexReg)
699 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
700 else
701 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000702 }
703
704 return true;
705}
706
707// Attempt to fast-select a store instruction.
708bool PPCFastISel::SelectStore(const Instruction *I) {
709 Value *Op0 = I->getOperand(0);
710 unsigned SrcReg = 0;
711
712 // FIXME: No atomics loads are supported.
713 if (cast<StoreInst>(I)->isAtomic())
714 return false;
715
716 // Verify we have a legal type before going any further.
717 MVT VT;
718 if (!isLoadTypeLegal(Op0->getType(), VT))
719 return false;
720
721 // Get the value to be stored into a register.
722 SrcReg = getRegForValue(Op0);
723 if (SrcReg == 0)
724 return false;
725
726 // See if we can handle this address.
727 Address Addr;
728 if (!PPCComputeAddress(I->getOperand(1), Addr))
729 return false;
730
731 if (!PPCEmitStore(VT, SrcReg, Addr))
732 return false;
733
734 return true;
735}
736
Bill Schmidt03008132013-08-25 22:33:42 +0000737// Attempt to fast-select a branch instruction.
738bool PPCFastISel::SelectBranch(const Instruction *I) {
739 const BranchInst *BI = cast<BranchInst>(I);
740 MachineBasicBlock *BrBB = FuncInfo.MBB;
741 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
742 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
743
744 // For now, just try the simplest case where it's fed by a compare.
745 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000746 if (isValueAvailable(CI)) {
747 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
748 if (!OptPPCPred)
749 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000750
Hal Finkel5f2a1372015-05-23 12:18:10 +0000751 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000752
Hal Finkel5f2a1372015-05-23 12:18:10 +0000753 // Take advantage of fall-through opportunities.
754 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
755 std::swap(TBB, FBB);
756 PPCPred = PPC::InvertPredicate(PPCPred);
757 }
758
759 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
760
761 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
762 CondReg))
763 return false;
764
765 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
766 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
767 fastEmitBranch(FBB, DbgLoc);
768 FuncInfo.MBB->addSuccessor(TBB);
769 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000770 }
Bill Schmidt03008132013-08-25 22:33:42 +0000771 } else if (const ConstantInt *CI =
772 dyn_cast<ConstantInt>(BI->getCondition())) {
773 uint64_t Imm = CI->getZExtValue();
774 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000775 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000776 return true;
777 }
778
779 // FIXME: ARM looks for a case where the block containing the compare
780 // has been split from the block containing the branch. If this happens,
781 // there is a vreg available containing the result of the compare. I'm
782 // not sure we can do much, as we've lost the predicate information with
783 // the compare instruction -- we have a 4-bit CR but don't know which bit
784 // to test here.
785 return false;
786}
787
788// Attempt to emit a compare of the two source values. Signed and unsigned
789// comparisons are supported. Return false if we can't handle it.
790bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
791 bool IsZExt, unsigned DestReg) {
792 Type *Ty = SrcValue1->getType();
793 EVT SrcEVT = TLI.getValueType(Ty, true);
794 if (!SrcEVT.isSimple())
795 return false;
796 MVT SrcVT = SrcEVT.getSimpleVT();
797
Eric Christopher1b8e7632014-05-22 01:07:24 +0000798 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000799 return false;
800
Bill Schmidt03008132013-08-25 22:33:42 +0000801 // See if operand 2 is an immediate encodeable in the compare.
802 // FIXME: Operands are not in canonical order at -O0, so an immediate
803 // operand in position 1 is a lost opportunity for now. We are
804 // similar to ARM in this regard.
805 long Imm = 0;
806 bool UseImm = false;
807
808 // Only 16-bit integer constants can be represented in compares for
809 // PowerPC. Others will be materialized into a register.
810 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
811 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
812 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
813 const APInt &CIVal = ConstInt->getValue();
814 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
815 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
816 UseImm = true;
817 }
818 }
819
820 unsigned CmpOpc;
821 bool NeedsExt = false;
822 switch (SrcVT.SimpleTy) {
823 default: return false;
824 case MVT::f32:
825 CmpOpc = PPC::FCMPUS;
826 break;
827 case MVT::f64:
828 CmpOpc = PPC::FCMPUD;
829 break;
830 case MVT::i1:
831 case MVT::i8:
832 case MVT::i16:
833 NeedsExt = true;
834 // Intentional fall-through.
835 case MVT::i32:
836 if (!UseImm)
837 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
838 else
839 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
840 break;
841 case MVT::i64:
842 if (!UseImm)
843 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
844 else
845 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
846 break;
847 }
848
849 unsigned SrcReg1 = getRegForValue(SrcValue1);
850 if (SrcReg1 == 0)
851 return false;
852
853 unsigned SrcReg2 = 0;
854 if (!UseImm) {
855 SrcReg2 = getRegForValue(SrcValue2);
856 if (SrcReg2 == 0)
857 return false;
858 }
859
860 if (NeedsExt) {
861 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
862 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
863 return false;
864 SrcReg1 = ExtReg;
865
866 if (!UseImm) {
867 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
868 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
869 return false;
870 SrcReg2 = ExtReg;
871 }
872 }
873
874 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000876 .addReg(SrcReg1).addReg(SrcReg2);
877 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000879 .addReg(SrcReg1).addImm(Imm);
880
881 return true;
882}
883
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000884// Attempt to fast-select a floating-point extend instruction.
885bool PPCFastISel::SelectFPExt(const Instruction *I) {
886 Value *Src = I->getOperand(0);
887 EVT SrcVT = TLI.getValueType(Src->getType(), true);
888 EVT DestVT = TLI.getValueType(I->getType(), true);
889
890 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
891 return false;
892
893 unsigned SrcReg = getRegForValue(Src);
894 if (!SrcReg)
895 return false;
896
897 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000898 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000899 return true;
900}
901
902// Attempt to fast-select a floating-point truncate instruction.
903bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
904 Value *Src = I->getOperand(0);
905 EVT SrcVT = TLI.getValueType(Src->getType(), true);
906 EVT DestVT = TLI.getValueType(I->getType(), true);
907
908 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
909 return false;
910
911 unsigned SrcReg = getRegForValue(Src);
912 if (!SrcReg)
913 return false;
914
915 // Round the result to single precision.
916 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000918 .addReg(SrcReg);
919
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000920 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000921 return true;
922}
923
924// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000925// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000926// those should be used instead of moving via a stack slot when the
927// subtarget permits.
928// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
929// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
930// case to 8 bytes which produces tighter code but wastes stack space.
931unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
932 bool IsSigned) {
933
934 // If necessary, extend 32-bit int to 64-bit.
935 if (SrcVT == MVT::i32) {
936 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
937 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
938 return 0;
939 SrcReg = TmpReg;
940 }
941
942 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
943 Address Addr;
944 Addr.BaseType = Address::FrameIndexBase;
945 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
946
947 // Store the value from the GPR.
948 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
949 return 0;
950
951 // Load the integer value into an FPR. The kind of load used depends
952 // on a number of conditions.
953 unsigned LoadOpc = PPC::LFD;
954
955 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000956 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000957 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000958 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000959 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000960 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000961 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000962 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000963 }
964
965 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
966 unsigned ResultReg = 0;
967 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
968 return 0;
969
970 return ResultReg;
971}
972
973// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000974// FIXME: Once fast-isel has better support for VSX, conversions using
975// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000976bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
977 MVT DstVT;
978 Type *DstTy = I->getType();
979 if (!isTypeLegal(DstTy, DstVT))
980 return false;
981
982 if (DstVT != MVT::f32 && DstVT != MVT::f64)
983 return false;
984
985 Value *Src = I->getOperand(0);
986 EVT SrcEVT = TLI.getValueType(Src->getType(), true);
987 if (!SrcEVT.isSimple())
988 return false;
989
990 MVT SrcVT = SrcEVT.getSimpleVT();
991
992 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
993 SrcVT != MVT::i32 && SrcVT != MVT::i64)
994 return false;
995
996 unsigned SrcReg = getRegForValue(Src);
997 if (SrcReg == 0)
998 return false;
999
1000 // We can only lower an unsigned convert if we have the newer
1001 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001002 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001003 return false;
1004
1005 // FIXME: For now we require the newer floating-point conversion operations
1006 // (which are present only on P7 and A2 server models) when converting
1007 // to single-precision float. Otherwise we have to generate a lot of
1008 // fiddly code to avoid double rounding. If necessary, the fiddly code
1009 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001010 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001011 return false;
1012
1013 // Extend the input if necessary.
1014 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1015 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1016 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1017 return false;
1018 SrcVT = MVT::i64;
1019 SrcReg = TmpReg;
1020 }
1021
1022 // Move the integer value to an FPR.
1023 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1024 if (FPReg == 0)
1025 return false;
1026
1027 // Determine the opcode for the conversion.
1028 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1029 unsigned DestReg = createResultReg(RC);
1030 unsigned Opc;
1031
1032 if (DstVT == MVT::f32)
1033 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1034 else
1035 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1036
1037 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001039 .addReg(FPReg);
1040
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001041 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001042 return true;
1043}
1044
1045// Move the floating-point value in SrcReg into an integer destination
1046// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001047// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001048// those should be used instead of moving via a stack slot when the
1049// subtarget permits.
1050unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1051 unsigned SrcReg, bool IsSigned) {
1052 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1053 // Note that if have STFIWX available, we could use a 4-byte stack
1054 // slot for i32, but this being fast-isel we'll just go with the
1055 // easiest code gen possible.
1056 Address Addr;
1057 Addr.BaseType = Address::FrameIndexBase;
1058 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1059
1060 // Store the value from the FPR.
1061 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1062 return 0;
1063
1064 // Reload it into a GPR. If we want an i32, modify the address
1065 // to have a 4-byte offset so we load from the right place.
1066 if (VT == MVT::i32)
1067 Addr.Offset = 4;
1068
1069 // Look at the currently assigned register for this instruction
1070 // to determine the required register class.
1071 unsigned AssignedReg = FuncInfo.ValueMap[I];
1072 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001073 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001074
1075 unsigned ResultReg = 0;
1076 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1077 return 0;
1078
1079 return ResultReg;
1080}
1081
1082// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001083// FIXME: Once fast-isel has better support for VSX, conversions using
1084// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001085bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1086 MVT DstVT, SrcVT;
1087 Type *DstTy = I->getType();
1088 if (!isTypeLegal(DstTy, DstVT))
1089 return false;
1090
1091 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1092 return false;
1093
Bill Schmidt83973ef2014-06-24 20:05:18 +00001094 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1095 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1096 return false;
1097
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001098 Value *Src = I->getOperand(0);
1099 Type *SrcTy = Src->getType();
1100 if (!isTypeLegal(SrcTy, SrcVT))
1101 return false;
1102
1103 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1104 return false;
1105
1106 unsigned SrcReg = getRegForValue(Src);
1107 if (SrcReg == 0)
1108 return false;
1109
1110 // Convert f32 to f64 if necessary. This is just a meaningless copy
1111 // to get the register class right. COPY_TO_REGCLASS is needed since
1112 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream.
1113 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1114 if (InRC == &PPC::F4RCRegClass) {
1115 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001117 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg)
1118 .addReg(SrcReg).addImm(PPC::F8RCRegClassID);
1119 SrcReg = TmpReg;
1120 }
1121
1122 // Determine the opcode for the conversion, which takes place
1123 // entirely within FPRs.
1124 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1125 unsigned Opc;
1126
1127 if (DstVT == MVT::i32)
1128 if (IsSigned)
1129 Opc = PPC::FCTIWZ;
1130 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001131 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001132 else
1133 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1134
1135 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001137 .addReg(SrcReg);
1138
1139 // Now move the integer value from a float register to an integer register.
1140 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1141 if (IntReg == 0)
1142 return false;
1143
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001144 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001145 return true;
1146}
1147
Bill Schmidtccecf262013-08-30 02:29:45 +00001148// Attempt to fast-select a binary integer operation that isn't already
1149// handled automatically.
1150bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1151 EVT DestVT = TLI.getValueType(I->getType(), true);
1152
1153 // We can get here in the case when we have a binary operation on a non-legal
1154 // type and the target independent selector doesn't know how to handle it.
1155 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1156 return false;
1157
1158 // Look at the currently assigned register for this instruction
1159 // to determine the required register class. If there is no register,
1160 // make a conservative choice (don't assign R0).
1161 unsigned AssignedReg = FuncInfo.ValueMap[I];
1162 const TargetRegisterClass *RC =
1163 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1164 &PPC::GPRC_and_GPRC_NOR0RegClass);
1165 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1166
1167 unsigned Opc;
1168 switch (ISDOpcode) {
1169 default: return false;
1170 case ISD::ADD:
1171 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1172 break;
1173 case ISD::OR:
1174 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1175 break;
1176 case ISD::SUB:
1177 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1178 break;
1179 }
1180
1181 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1182 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1183 if (SrcReg1 == 0) return false;
1184
1185 // Handle case of small immediate operand.
1186 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1187 const APInt &CIVal = ConstInt->getValue();
1188 int Imm = (int)CIVal.getSExtValue();
1189 bool UseImm = true;
1190 if (isInt<16>(Imm)) {
1191 switch (Opc) {
1192 default:
1193 llvm_unreachable("Missing case!");
1194 case PPC::ADD4:
1195 Opc = PPC::ADDI;
1196 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1197 break;
1198 case PPC::ADD8:
1199 Opc = PPC::ADDI8;
1200 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1201 break;
1202 case PPC::OR:
1203 Opc = PPC::ORI;
1204 break;
1205 case PPC::OR8:
1206 Opc = PPC::ORI8;
1207 break;
1208 case PPC::SUBF:
1209 if (Imm == -32768)
1210 UseImm = false;
1211 else {
1212 Opc = PPC::ADDI;
1213 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1214 Imm = -Imm;
1215 }
1216 break;
1217 case PPC::SUBF8:
1218 if (Imm == -32768)
1219 UseImm = false;
1220 else {
1221 Opc = PPC::ADDI8;
1222 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1223 Imm = -Imm;
1224 }
1225 break;
1226 }
1227
1228 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001229 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1230 ResultReg)
1231 .addReg(SrcReg1)
1232 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001233 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001234 return true;
1235 }
1236 }
1237 }
1238
1239 // Reg-reg case.
1240 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1241 if (SrcReg2 == 0) return false;
1242
1243 // Reverse operands for subtract-from.
1244 if (ISDOpcode == ISD::SUB)
1245 std::swap(SrcReg1, SrcReg2);
1246
Rafael Espindolaea09c592014-02-18 22:05:46 +00001247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001248 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001249 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001250 return true;
1251}
1252
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001253// Handle arguments to a call that we're attempting to fast-select.
1254// Return false if the arguments are too complex for us at the moment.
1255bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1256 SmallVectorImpl<unsigned> &ArgRegs,
1257 SmallVectorImpl<MVT> &ArgVTs,
1258 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1259 SmallVectorImpl<unsigned> &RegArgs,
1260 CallingConv::ID CC,
1261 unsigned &NumBytes,
1262 bool IsVarArg) {
1263 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001264 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001265
1266 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001267 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001268 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001269
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001270 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1271
1272 // Bail out if we can't handle any of the arguments.
1273 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1274 CCValAssign &VA = ArgLocs[I];
1275 MVT ArgVT = ArgVTs[VA.getValNo()];
1276
1277 // Skip vector arguments for now, as well as long double and
1278 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001279 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001280 !VA.isRegLoc() || VA.needsCustom())
1281 return false;
1282
1283 // Skip bit-converted arguments for now.
1284 if (VA.getLocInfo() == CCValAssign::BCvt)
1285 return false;
1286 }
1287
1288 // Get a count of how many bytes are to be pushed onto the stack.
1289 NumBytes = CCInfo.getNextStackOffset();
1290
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001291 // The prolog code of the callee may store up to 8 GPR argument registers to
1292 // the stack, allowing va_start to index over them in memory if its varargs.
1293 // Because we cannot tell if this is needed on the caller side, we have to
1294 // conservatively assume that it is needed. As such, make sure we have at
1295 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001296 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001297 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001298
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001299 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001301 TII.get(TII.getCallFrameSetupOpcode()))
1302 .addImm(NumBytes);
1303
1304 // Prepare to assign register arguments. Every argument uses up a
1305 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001306 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001307 unsigned NextGPR = PPC::X3;
1308 unsigned NextFPR = PPC::F1;
1309
1310 // Process arguments.
1311 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1312 CCValAssign &VA = ArgLocs[I];
1313 unsigned Arg = ArgRegs[VA.getValNo()];
1314 MVT ArgVT = ArgVTs[VA.getValNo()];
1315
1316 // Handle argument promotion and bitcasts.
1317 switch (VA.getLocInfo()) {
1318 default:
1319 llvm_unreachable("Unknown loc info!");
1320 case CCValAssign::Full:
1321 break;
1322 case CCValAssign::SExt: {
1323 MVT DestVT = VA.getLocVT();
1324 const TargetRegisterClass *RC =
1325 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1326 unsigned TmpReg = createResultReg(RC);
1327 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1328 llvm_unreachable("Failed to emit a sext!");
1329 ArgVT = DestVT;
1330 Arg = TmpReg;
1331 break;
1332 }
1333 case CCValAssign::AExt:
1334 case CCValAssign::ZExt: {
1335 MVT DestVT = VA.getLocVT();
1336 const TargetRegisterClass *RC =
1337 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1338 unsigned TmpReg = createResultReg(RC);
1339 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1340 llvm_unreachable("Failed to emit a zext!");
1341 ArgVT = DestVT;
1342 Arg = TmpReg;
1343 break;
1344 }
1345 case CCValAssign::BCvt: {
1346 // FIXME: Not yet handled.
1347 llvm_unreachable("Should have bailed before getting here!");
1348 break;
1349 }
1350 }
1351
1352 // Copy this argument to the appropriate register.
1353 unsigned ArgReg;
1354 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1355 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001356 if (CC != CallingConv::Fast)
1357 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001358 } else
1359 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001360
1361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1362 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001363 RegArgs.push_back(ArgReg);
1364 }
1365
1366 return true;
1367}
1368
1369// For a call that we've determined we can fast-select, finish the
1370// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001371bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1372 CallingConv::ID CC = CLI.CallConv;
1373
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001374 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001376 TII.get(TII.getCallFrameDestroyOpcode()))
1377 .addImm(NumBytes).addImm(0);
1378
1379 // Next, generate a copy to obtain the return value.
1380 // FIXME: No multi-register return values yet, though I don't foresee
1381 // any real difficulties there.
1382 if (RetVT != MVT::isVoid) {
1383 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001384 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001385 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1386 CCValAssign &VA = RVLocs[0];
1387 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1388 assert(VA.isRegLoc() && "Can only return in registers!");
1389
1390 MVT DestVT = VA.getValVT();
1391 MVT CopyVT = DestVT;
1392
1393 // Ints smaller than a register still arrive in a full 64-bit
1394 // register, so make sure we recognize this.
1395 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1396 CopyVT = MVT::i64;
1397
1398 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001399 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001400
1401 if (RetVT == CopyVT) {
1402 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1403 ResultReg = createResultReg(CpyRC);
1404
Rafael Espindolaea09c592014-02-18 22:05:46 +00001405 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001406 TII.get(TargetOpcode::COPY), ResultReg)
1407 .addReg(SourcePhysReg);
1408
1409 // If necessary, round the floating result to single precision.
1410 } else if (CopyVT == MVT::f64) {
1411 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001413 ResultReg).addReg(SourcePhysReg);
1414
1415 // If only the low half of a general register is needed, generate
1416 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1417 // used along the fast-isel path (not lowered), and downstream logic
1418 // also doesn't like a direct subreg copy on a physical reg.)
1419 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1420 ResultReg = createResultReg(&PPC::GPRCRegClass);
1421 // Convert physical register from G8RC to GPRC.
1422 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001424 TII.get(TargetOpcode::COPY), ResultReg)
1425 .addReg(SourcePhysReg);
1426 }
1427
Bill Schmidt0954ea12013-08-30 23:25:30 +00001428 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001429 CLI.InRegs.push_back(SourcePhysReg);
1430 CLI.ResultReg = ResultReg;
1431 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001432 }
Hal Finkel934361a2015-01-14 01:07:51 +00001433
1434 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001435}
1436
Hal Finkel934361a2015-01-14 01:07:51 +00001437bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1438 CallingConv::ID CC = CLI.CallConv;
1439 bool IsTailCall = CLI.IsTailCall;
1440 bool IsVarArg = CLI.IsVarArg;
1441 const Value *Callee = CLI.Callee;
1442 const char *SymName = CLI.SymName;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001443
Hal Finkel934361a2015-01-14 01:07:51 +00001444 if (!Callee && !SymName)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001445 return false;
1446
1447 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001448 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001449 return false;
1450
Hal Finkel934361a2015-01-14 01:07:51 +00001451 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001452 if (IsVarArg)
1453 return false;
1454
1455 // Handle simple calls for now, with legal return types and
1456 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001457 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001458 MVT RetVT;
1459 if (RetTy->isVoidTy())
1460 RetVT = MVT::isVoid;
1461 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1462 RetVT != MVT::i8)
1463 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001464 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1465 // We can't handle boolean returns when CR bits are in use.
1466 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001467
1468 // FIXME: No multi-register return values yet.
1469 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1470 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1471 RetVT != MVT::f64) {
1472 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001473 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001474 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1475 if (RVLocs.size() > 1)
1476 return false;
1477 }
1478
1479 // Bail early if more than 8 arguments, as we only currently
1480 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001481 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001482 if (NumArgs > 8)
1483 return false;
1484
1485 // Set up the argument vectors.
1486 SmallVector<Value*, 8> Args;
1487 SmallVector<unsigned, 8> ArgRegs;
1488 SmallVector<MVT, 8> ArgVTs;
1489 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1490
1491 Args.reserve(NumArgs);
1492 ArgRegs.reserve(NumArgs);
1493 ArgVTs.reserve(NumArgs);
1494 ArgFlags.reserve(NumArgs);
1495
Hal Finkel934361a2015-01-14 01:07:51 +00001496 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001497 // Only handle easy calls for now. It would be reasonably easy
1498 // to handle <= 8-byte structures passed ByVal in registers, but we
1499 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001500 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1501 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001502 return false;
1503
Hal Finkel934361a2015-01-14 01:07:51 +00001504 Value *ArgValue = CLI.OutVals[i];
1505 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001506 MVT ArgVT;
1507 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1508 return false;
1509
1510 if (ArgVT.isVector())
1511 return false;
1512
Hal Finkel934361a2015-01-14 01:07:51 +00001513 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001514 if (Arg == 0)
1515 return false;
1516
Hal Finkel934361a2015-01-14 01:07:51 +00001517 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001518 ArgRegs.push_back(Arg);
1519 ArgVTs.push_back(ArgVT);
1520 ArgFlags.push_back(Flags);
1521 }
1522
1523 // Process the arguments.
1524 SmallVector<unsigned, 8> RegArgs;
1525 unsigned NumBytes;
1526
1527 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1528 RegArgs, CC, NumBytes, IsVarArg))
1529 return false;
1530
Hal Finkel934361a2015-01-14 01:07:51 +00001531 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001532 // FIXME: No handling for function pointers yet. This requires
1533 // implementing the function descriptor (OPD) setup.
1534 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001535 if (!GV) {
1536 // patchpoints are a special case; they always dispatch to a pointer value.
1537 // However, we don't actually want to generate the indirect call sequence
1538 // here (that will be generated, as necessary, during asm printing), and
1539 // the call we generate here will be erased by FastISel::selectPatchpoint,
1540 // so don't try very hard...
1541 if (CLI.IsPatchPoint)
1542 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1543 else
1544 return false;
1545 } else {
1546 // Build direct call with NOP for TOC restore.
1547 // FIXME: We can and should optimize away the NOP for local calls.
1548 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1549 TII.get(PPC::BL8_NOP));
1550 // Add callee.
1551 MIB.addGlobalAddress(GV);
1552 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001553
1554 // Add implicit physical register uses to the call.
1555 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1556 MIB.addReg(RegArgs[II], RegState::Implicit);
1557
Hal Finkelaf519932015-01-19 07:20:27 +00001558 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1559 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001560 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001561 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001562
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001563 // Add a register mask with the call-preserved registers. Proper
1564 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001565 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001566
Hal Finkel934361a2015-01-14 01:07:51 +00001567 CLI.Call = MIB;
1568
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001569 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001570 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001571}
1572
Bill Schmidtd89f6782013-08-26 19:42:51 +00001573// Attempt to fast-select a return instruction.
1574bool PPCFastISel::SelectRet(const Instruction *I) {
1575
1576 if (!FuncInfo.CanLowerReturn)
1577 return false;
1578
1579 const ReturnInst *Ret = cast<ReturnInst>(I);
1580 const Function &F = *I->getParent()->getParent();
1581
1582 // Build a list of return value registers.
1583 SmallVector<unsigned, 4> RetRegs;
1584 CallingConv::ID CC = F.getCallingConv();
1585
1586 if (Ret->getNumOperands() > 0) {
1587 SmallVector<ISD::OutputArg, 4> Outs;
1588 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1589
1590 // Analyze operands of the call, assigning locations to each operand.
1591 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001592 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001593 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1594 const Value *RV = Ret->getOperand(0);
1595
1596 // FIXME: Only one output register for now.
1597 if (ValLocs.size() > 1)
1598 return false;
1599
1600 // Special case for returning a constant integer of any size.
1601 // Materialize the constant as an i64 and copy it to the return
Samuel Antao61570df2014-09-17 23:25:06 +00001602 // register. We still need to worry about properly extending the sign. E.g:
1603 // If the constant has only one bit, it means it is a boolean. Therefore
1604 // we can't use PPCMaterializeInt because it extends the sign which will
1605 // cause negations of the returned value to be incorrect as they are
1606 // implemented as the flip of the least significant bit.
Bill Schmidtd89f6782013-08-26 19:42:51 +00001607 if (isa<ConstantInt>(*RV)) {
1608 const Constant *C = cast<Constant>(RV);
Samuel Antao61570df2014-09-17 23:25:06 +00001609
1610 CCValAssign &VA = ValLocs[0];
1611
1612 unsigned RetReg = VA.getLocReg();
1613 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64,
1614 VA.getLocInfo() == CCValAssign::SExt);
1615
Rafael Espindolaea09c592014-02-18 22:05:46 +00001616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001617 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1618
Bill Schmidtd89f6782013-08-26 19:42:51 +00001619 RetRegs.push_back(RetReg);
1620
1621 } else {
1622 unsigned Reg = getRegForValue(RV);
1623
1624 if (Reg == 0)
1625 return false;
1626
1627 // Copy the result values into the output registers.
1628 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1629
1630 CCValAssign &VA = ValLocs[i];
1631 assert(VA.isRegLoc() && "Can only return in registers!");
1632 RetRegs.push_back(VA.getLocReg());
1633 unsigned SrcReg = Reg + VA.getValNo();
1634
1635 EVT RVEVT = TLI.getValueType(RV->getType());
1636 if (!RVEVT.isSimple())
1637 return false;
1638 MVT RVVT = RVEVT.getSimpleVT();
1639 MVT DestVT = VA.getLocVT();
1640
1641 if (RVVT != DestVT && RVVT != MVT::i8 &&
1642 RVVT != MVT::i16 && RVVT != MVT::i32)
1643 return false;
1644
1645 if (RVVT != DestVT) {
1646 switch (VA.getLocInfo()) {
1647 default:
1648 llvm_unreachable("Unknown loc info!");
1649 case CCValAssign::Full:
1650 llvm_unreachable("Full value assign but types don't match?");
1651 case CCValAssign::AExt:
1652 case CCValAssign::ZExt: {
1653 const TargetRegisterClass *RC =
1654 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1655 unsigned TmpReg = createResultReg(RC);
1656 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1657 return false;
1658 SrcReg = TmpReg;
1659 break;
1660 }
1661 case CCValAssign::SExt: {
1662 const TargetRegisterClass *RC =
1663 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1664 unsigned TmpReg = createResultReg(RC);
1665 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1666 return false;
1667 SrcReg = TmpReg;
1668 break;
1669 }
1670 }
1671 }
1672
Rafael Espindolaea09c592014-02-18 22:05:46 +00001673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001674 TII.get(TargetOpcode::COPY), RetRegs[i])
1675 .addReg(SrcReg);
1676 }
1677 }
1678 }
1679
Rafael Espindolaea09c592014-02-18 22:05:46 +00001680 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001681 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001682
1683 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1684 MIB.addReg(RetRegs[i], RegState::Implicit);
1685
1686 return true;
1687}
1688
Bill Schmidt03008132013-08-25 22:33:42 +00001689// Attempt to emit an integer extend of SrcReg into DestReg. Both
1690// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001691// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001692bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1693 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001694 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1695 return false;
1696 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1697 return false;
1698
1699 // Signed extensions use EXTSB, EXTSH, EXTSW.
1700 if (!IsZExt) {
1701 unsigned Opc;
1702 if (SrcVT == MVT::i8)
1703 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1704 else if (SrcVT == MVT::i16)
1705 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1706 else {
1707 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1708 Opc = PPC::EXTSW_32_64;
1709 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001711 .addReg(SrcReg);
1712
1713 // Unsigned 32-bit extensions use RLWINM.
1714 } else if (DestVT == MVT::i32) {
1715 unsigned MB;
1716 if (SrcVT == MVT::i8)
1717 MB = 24;
1718 else {
1719 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1720 MB = 16;
1721 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001722 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001723 DestReg)
1724 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1725
1726 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1727 } else {
1728 unsigned MB;
1729 if (SrcVT == MVT::i8)
1730 MB = 56;
1731 else if (SrcVT == MVT::i16)
1732 MB = 48;
1733 else
1734 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001735 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001736 TII.get(PPC::RLDICL_32_64), DestReg)
1737 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1738 }
1739
1740 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001741}
1742
1743// Attempt to fast-select an indirect branch instruction.
1744bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1745 unsigned AddrReg = getRegForValue(I->getOperand(0));
1746 if (AddrReg == 0)
1747 return false;
1748
Rafael Espindolaea09c592014-02-18 22:05:46 +00001749 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001750 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001751 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001752
1753 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1754 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i)
1755 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]);
1756
1757 return true;
1758}
1759
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001760// Attempt to fast-select an integer truncate instruction.
1761bool PPCFastISel::SelectTrunc(const Instruction *I) {
1762 Value *Src = I->getOperand(0);
1763 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1764 EVT DestVT = TLI.getValueType(I->getType(), true);
1765
1766 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1767 return false;
1768
1769 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1770 return false;
1771
1772 unsigned SrcReg = getRegForValue(Src);
1773 if (!SrcReg)
1774 return false;
1775
1776 // The only interesting case is when we need to switch register classes.
1777 if (SrcVT == MVT::i64) {
1778 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001779 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1780 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001781 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1782 SrcReg = ResultReg;
1783 }
1784
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001785 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001786 return true;
1787}
1788
Bill Schmidtd89f6782013-08-26 19:42:51 +00001789// Attempt to fast-select an integer extend instruction.
1790bool PPCFastISel::SelectIntExt(const Instruction *I) {
1791 Type *DestTy = I->getType();
1792 Value *Src = I->getOperand(0);
1793 Type *SrcTy = Src->getType();
1794
1795 bool IsZExt = isa<ZExtInst>(I);
1796 unsigned SrcReg = getRegForValue(Src);
1797 if (!SrcReg) return false;
1798
1799 EVT SrcEVT, DestEVT;
1800 SrcEVT = TLI.getValueType(SrcTy, true);
1801 DestEVT = TLI.getValueType(DestTy, true);
1802 if (!SrcEVT.isSimple())
1803 return false;
1804 if (!DestEVT.isSimple())
1805 return false;
1806
1807 MVT SrcVT = SrcEVT.getSimpleVT();
1808 MVT DestVT = DestEVT.getSimpleVT();
1809
1810 // If we know the register class needed for the result of this
1811 // instruction, use it. Otherwise pick the register class of the
1812 // correct size that does not contain X0/R0, since we don't know
1813 // whether downstream uses permit that assignment.
1814 unsigned AssignedReg = FuncInfo.ValueMap[I];
1815 const TargetRegisterClass *RC =
1816 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1817 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1818 &PPC::GPRC_and_GPRC_NOR0RegClass));
1819 unsigned ResultReg = createResultReg(RC);
1820
1821 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1822 return false;
1823
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001824 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001825 return true;
1826}
1827
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001828// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001829// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001830bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001831
1832 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001833 case Instruction::Load:
1834 return SelectLoad(I);
1835 case Instruction::Store:
1836 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001837 case Instruction::Br:
1838 return SelectBranch(I);
1839 case Instruction::IndirectBr:
1840 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001841 case Instruction::FPExt:
1842 return SelectFPExt(I);
1843 case Instruction::FPTrunc:
1844 return SelectFPTrunc(I);
1845 case Instruction::SIToFP:
1846 return SelectIToFP(I, /*IsSigned*/ true);
1847 case Instruction::UIToFP:
1848 return SelectIToFP(I, /*IsSigned*/ false);
1849 case Instruction::FPToSI:
1850 return SelectFPToI(I, /*IsSigned*/ true);
1851 case Instruction::FPToUI:
1852 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001853 case Instruction::Add:
1854 return SelectBinaryIntOp(I, ISD::ADD);
1855 case Instruction::Or:
1856 return SelectBinaryIntOp(I, ISD::OR);
1857 case Instruction::Sub:
1858 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001859 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001860 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001861 case Instruction::Ret:
1862 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001863 case Instruction::Trunc:
1864 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001865 case Instruction::ZExt:
1866 case Instruction::SExt:
1867 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001868 // Here add other flavors of Instruction::XXX that automated
1869 // cases don't catch. For example, switches are terminators
1870 // that aren't yet handled.
1871 default:
1872 break;
1873 }
1874 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001875}
1876
1877// Materialize a floating-point constant into a register, and return
1878// the register number (or zero if we failed to handle it).
1879unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1880 // No plans to handle long double here.
1881 if (VT != MVT::f32 && VT != MVT::f64)
1882 return 0;
1883
1884 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001885 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001886 assert(Align > 0 && "Unexpectedly missing alignment information!");
1887 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1888 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1889 CodeModel::Model CModel = TM.getCodeModel();
1890
1891 MachineMemOperand *MMO =
1892 FuncInfo.MF->getMachineMemOperand(
1893 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad,
1894 (VT == MVT::f32) ? 4 : 8, Align);
1895
Bill Schmidt03008132013-08-25 22:33:42 +00001896 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1897 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1898
Hal Finkele6698d52015-02-01 15:03:28 +00001899 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001900 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1901 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001902 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001903 TmpReg)
1904 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001906 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1907 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001908 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001910 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001911 // But for large code model, we must generate a LDtocL followed
1912 // by the LF[SD].
1913 if (CModel == CodeModel::Large) {
1914 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001915 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001916 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001918 .addImm(0).addReg(TmpReg2);
1919 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001921 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1922 .addReg(TmpReg)
1923 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001924 }
1925
1926 return DestReg;
1927}
1928
Bill Schmidtccecf262013-08-30 02:29:45 +00001929// Materialize the address of a global value into a register, and return
1930// the register number (or zero if we failed to handle it).
1931unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1932 assert(VT == MVT::i64 && "Non-address!");
1933 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1934 unsigned DestReg = createResultReg(RC);
1935
1936 // Global values may be plain old object addresses, TLS object
1937 // addresses, constant pool entries, or jump tables. How we generate
1938 // code for these may depend on small, medium, or large code model.
1939 CodeModel::Model CModel = TM.getCodeModel();
1940
1941 // FIXME: Jump tables are not yet required because fast-isel doesn't
1942 // handle switches; if that changes, we need them as well. For now,
1943 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001944
1945 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001946 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001947 return 0;
1948
Hal Finkele6698d52015-02-01 15:03:28 +00001949 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001950 // For small code model, generate a simple TOC load.
1951 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1953 DestReg)
1954 .addGlobalAddress(GV)
1955 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001956 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001957 // If the address is an externally defined symbol, a symbol with common
1958 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001959 // jump table address (not yet needed), or if we are generating code
1960 // for large code model, we generate:
1961 // LDtocL(GV, ADDIStocHA(%X2, GV))
1962 // Otherwise we generate:
1963 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1964 // Either way, start with the ADDIStocHA:
1965 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001966 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001967 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1968
Bill Schmidtccecf262013-08-30 02:29:45 +00001969 // If/when switches are implemented, jump tables should be handled
1970 // on the "if" path here.
Bill Schmidt5d82f092014-06-16 21:36:02 +00001971 if (CModel == CodeModel::Large ||
1972 (GV->getType()->getElementType()->isFunctionTy() &&
1973 (GV->isDeclaration() || GV->isWeakForLinker())) ||
1974 GV->isDeclaration() || GV->hasCommonLinkage() ||
1975 GV->hasAvailableExternallyLinkage())
Rafael Espindolaea09c592014-02-18 22:05:46 +00001976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001977 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1978 else
1979 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001980 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001981 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
1982 }
1983
1984 return DestReg;
1985}
1986
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001987// Materialize a 32-bit integer constant into a register, and return
1988// the register number (or zero if we failed to handle it).
1989unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1990 const TargetRegisterClass *RC) {
1991 unsigned Lo = Imm & 0xFFFF;
1992 unsigned Hi = (Imm >> 16) & 0xFFFF;
1993
1994 unsigned ResultReg = createResultReg(RC);
1995 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1996
1997 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001999 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2000 .addImm(Imm);
2001 else if (Lo) {
2002 // Both Lo and Hi have nonzero bits.
2003 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002004 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002005 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2006 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002008 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2009 .addReg(TmpReg).addImm(Lo);
2010 } else
2011 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002013 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2014 .addImm(Hi);
2015
2016 return ResultReg;
2017}
2018
2019// Materialize a 64-bit integer constant into a register, and return
2020// the register number (or zero if we failed to handle it).
2021unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2022 const TargetRegisterClass *RC) {
2023 unsigned Remainder = 0;
2024 unsigned Shift = 0;
2025
2026 // If the value doesn't fit in 32 bits, see if we can shift it
2027 // so that it fits in 32 bits.
2028 if (!isInt<32>(Imm)) {
2029 Shift = countTrailingZeros<uint64_t>(Imm);
2030 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2031
2032 if (isInt<32>(ImmSh))
2033 Imm = ImmSh;
2034 else {
2035 Remainder = Imm;
2036 Shift = 32;
2037 Imm >>= 32;
2038 }
2039 }
2040
2041 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2042 // (if not shifted).
2043 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2044 if (!Shift)
2045 return TmpReg1;
2046
2047 // If upper 32 bits were not zero, we've built them and need to shift
2048 // them into place.
2049 unsigned TmpReg2;
2050 if (Imm) {
2051 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002053 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2054 } else
2055 TmpReg2 = TmpReg1;
2056
2057 unsigned TmpReg3, Hi, Lo;
2058 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2059 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002061 TmpReg3).addReg(TmpReg2).addImm(Hi);
2062 } else
2063 TmpReg3 = TmpReg2;
2064
2065 if ((Lo = Remainder & 0xFFFF)) {
2066 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002068 ResultReg).addReg(TmpReg3).addImm(Lo);
2069 return ResultReg;
2070 }
2071
2072 return TmpReg3;
2073}
2074
2075
2076// Materialize an integer constant into a register, and return
2077// the register number (or zero if we failed to handle it).
Samuel Antao61570df2014-09-17 23:25:06 +00002078unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT,
2079 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002080 // If we're using CR bit registers for i1 values, handle that as a special
2081 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002082 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002083 const ConstantInt *CI = cast<ConstantInt>(C);
2084 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2086 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2087 return ImmReg;
2088 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002089
2090 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2091 VT != MVT::i8 && VT != MVT::i1)
2092 return 0;
2093
2094 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2095 &PPC::GPRCRegClass);
2096
2097 // If the constant is in range, use a load-immediate.
2098 const ConstantInt *CI = cast<ConstantInt>(C);
2099 if (isInt<16>(CI->getSExtValue())) {
2100 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2101 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002102 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Samuel Antao61570df2014-09-17 23:25:06 +00002103 .addImm( (UseSExt) ? CI->getSExtValue() : CI->getZExtValue() );
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002104 return ImmReg;
2105 }
2106
2107 // Construct the constant piecewise.
2108 int64_t Imm = CI->getZExtValue();
2109
2110 if (VT == MVT::i64)
2111 return PPCMaterialize64BitInt(Imm, RC);
2112 else if (VT == MVT::i32)
2113 return PPCMaterialize32BitInt(Imm, RC);
2114
2115 return 0;
2116}
2117
2118// Materialize a constant into a register, and return the register
2119// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002120unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002121 EVT CEVT = TLI.getValueType(C->getType(), true);
2122
2123 // Only handle simple types.
2124 if (!CEVT.isSimple()) return 0;
2125 MVT VT = CEVT.getSimpleVT();
2126
2127 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2128 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002129 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2130 return PPCMaterializeGV(GV, VT);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002131 else if (isa<ConstantInt>(C))
Hal Finkel0c505b02014-12-25 23:08:25 +00002132 return PPCMaterializeInt(C, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002133
2134 return 0;
2135}
2136
2137// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002138// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002139unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002140 // Don't handle dynamic allocas.
2141 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2142
2143 MVT VT;
2144 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2145
2146 DenseMap<const AllocaInst*, int>::iterator SI =
2147 FuncInfo.StaticAllocaMap.find(AI);
2148
2149 if (SI != FuncInfo.StaticAllocaMap.end()) {
2150 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002151 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002152 ResultReg).addFrameIndex(SI->second).addImm(0);
2153 return ResultReg;
2154 }
2155
2156 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002157}
2158
Bill Schmidtccecf262013-08-30 02:29:45 +00002159// Fold loads into extends when possible.
2160// FIXME: We can have multiple redundant extend/trunc instructions
2161// following a load. The folding only picks up one. Extend this
2162// to check subsequent instructions for the same pattern and remove
2163// them. Thus ResultReg should be the def reg for the last redundant
2164// instruction in a chain, and all intervening instructions can be
2165// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2166// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002167bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2168 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002169 // Verify we have a legal type before going any further.
2170 MVT VT;
2171 if (!isLoadTypeLegal(LI->getType(), VT))
2172 return false;
2173
2174 // Combine load followed by zero- or sign-extend.
2175 bool IsZExt = false;
2176 switch(MI->getOpcode()) {
2177 default:
2178 return false;
2179
2180 case PPC::RLDICL:
2181 case PPC::RLDICL_32_64: {
2182 IsZExt = true;
2183 unsigned MB = MI->getOperand(3).getImm();
2184 if ((VT == MVT::i8 && MB <= 56) ||
2185 (VT == MVT::i16 && MB <= 48) ||
2186 (VT == MVT::i32 && MB <= 32))
2187 break;
2188 return false;
2189 }
2190
2191 case PPC::RLWINM:
2192 case PPC::RLWINM8: {
2193 IsZExt = true;
2194 unsigned MB = MI->getOperand(3).getImm();
2195 if ((VT == MVT::i8 && MB <= 24) ||
2196 (VT == MVT::i16 && MB <= 16))
2197 break;
2198 return false;
2199 }
2200
2201 case PPC::EXTSB:
2202 case PPC::EXTSB8:
2203 case PPC::EXTSB8_32_64:
2204 /* There is no sign-extending load-byte instruction. */
2205 return false;
2206
2207 case PPC::EXTSH:
2208 case PPC::EXTSH8:
2209 case PPC::EXTSH8_32_64: {
2210 if (VT != MVT::i16 && VT != MVT::i8)
2211 return false;
2212 break;
2213 }
2214
2215 case PPC::EXTSW:
2216 case PPC::EXTSW_32_64: {
2217 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2218 return false;
2219 break;
2220 }
2221 }
2222
2223 // See if we can handle this address.
2224 Address Addr;
2225 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2226 return false;
2227
2228 unsigned ResultReg = MI->getOperand(0).getReg();
2229
Craig Topper062a2ba2014-04-25 05:30:21 +00002230 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002231 return false;
2232
2233 MI->eraseFromParent();
2234 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002235}
2236
2237// Attempt to lower call arguments in a faster way than done by
2238// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002239bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002240 // Defer to normal argument lowering for now. It's reasonably
2241 // efficient. Consider doing something like ARM to handle the
2242 // case where all args fit in registers, no varargs, no float
2243 // or vector args.
2244 return false;
2245}
2246
Bill Schmidt03008132013-08-25 22:33:42 +00002247// Handle materializing integer constants into a register. This is not
2248// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002249unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002250
2251 if (Opc != ISD::Constant)
2252 return 0;
2253
Hal Finkel940ab932014-02-28 00:27:01 +00002254 // If we're using CR bit registers for i1 values, handle that as a special
2255 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002256 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002257 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2258 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2259 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2260 return ImmReg;
2261 }
2262
Bill Schmidt03008132013-08-25 22:33:42 +00002263 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2264 VT != MVT::i8 && VT != MVT::i1)
2265 return 0;
2266
2267 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2268 &PPC::GPRCRegClass);
2269 if (VT == MVT::i64)
2270 return PPCMaterialize64BitInt(Imm, RC);
2271 else
2272 return PPCMaterialize32BitInt(Imm, RC);
2273}
2274
Bill Schmidtccecf262013-08-30 02:29:45 +00002275// Override for ADDI and ADDI8 to set the correct register class
2276// on RHS operand 0. The automatic infrastructure naively assumes
2277// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2278// for these cases. At the moment, none of the other automatically
2279// generated RI instructions require special treatment. However, once
2280// SelectSelect is implemented, "isel" requires similar handling.
2281//
2282// Also be conservative about the output register class. Avoid
2283// assigning R0 or X0 to the output register for GPRC and G8RC
2284// register classes, as any such result could be used in ADDI, etc.,
2285// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002286unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002287 const TargetRegisterClass *RC,
2288 unsigned Op0, bool Op0IsKill,
2289 uint64_t Imm) {
2290 if (MachineInstOpcode == PPC::ADDI)
2291 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2292 else if (MachineInstOpcode == PPC::ADDI8)
2293 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2294
2295 const TargetRegisterClass *UseRC =
2296 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2297 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2298
Juergen Ributzka88e32512014-09-03 20:56:59 +00002299 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002300 Op0, Op0IsKill, Imm);
2301}
2302
2303// Override for instructions with one register operand to avoid use of
2304// R0/X0. The automatic infrastructure isn't aware of the context so
2305// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002306unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002307 const TargetRegisterClass* RC,
2308 unsigned Op0, bool Op0IsKill) {
2309 const TargetRegisterClass *UseRC =
2310 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2311 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2312
Juergen Ributzka88e32512014-09-03 20:56:59 +00002313 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002314}
2315
2316// Override for instructions with two register operands to avoid use
2317// of R0/X0. The automatic infrastructure isn't aware of the context
2318// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002319unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002320 const TargetRegisterClass* RC,
2321 unsigned Op0, bool Op0IsKill,
2322 unsigned Op1, bool Op1IsKill) {
2323 const TargetRegisterClass *UseRC =
2324 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2325 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2326
Juergen Ributzka88e32512014-09-03 20:56:59 +00002327 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002328 Op1, Op1IsKill);
2329}
2330
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002331namespace llvm {
2332 // Create the fast instruction selector for PowerPC64 ELF.
2333 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2334 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002335 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002336 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002337 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002338 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002339 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002340 }
2341}