blob: 901a7502cb2c466eb5d7efdaec2cf2f64e67d092 [file] [log] [blame]
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001//===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PowerPC-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// PPCGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
Bill Schmidt0cf702f2013-07-30 00:50:39 +000016#include "PPC.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel934361a2015-01-14 01:07:51 +000018#include "PPCCallingConv.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000019#include "PPCISelLowering.h"
Hal Finkele6698d52015-02-01 15:03:28 +000020#include "PPCMachineFunctionInfo.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000021#include "PPCSubtarget.h"
22#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000023#include "llvm/ADT/Optional.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
31#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000032#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000033#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/IntrinsicInst.h"
36#include "llvm/IR/Operator.h"
37#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000038#include "llvm/Target/TargetLowering.h"
39#include "llvm/Target/TargetMachine.h"
40
Bill Schmidteb8d6f72013-08-31 02:33:40 +000041//===----------------------------------------------------------------------===//
42//
43// TBD:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +000044// fastLowerArguments: Handle simple cases.
Bill Schmidteb8d6f72013-08-31 02:33:40 +000045// PPCMaterializeGV: Handle TLS.
46// SelectCall: Handle function pointers.
47// SelectCall: Handle multi-register return values.
48// SelectCall: Optimize away nops for local calls.
49// processCallArgs: Handle bit-converted arguments.
50// finishCall: Handle multi-register return values.
51// PPCComputeAddress: Handle parameter references as FrameIndex's.
52// PPCEmitCmp: Handle immediate as operand 1.
53// SelectCall: Handle small byval arguments.
54// SelectIntrinsicCall: Implement.
55// SelectSelect: Implement.
56// Consider factoring isTypeLegal into the base class.
57// Implement switches and jump tables.
58//
59//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000060using namespace llvm;
61
Chandler Carruth84e68b22014-04-22 02:41:26 +000062#define DEBUG_TYPE "ppcfastisel"
63
Bill Schmidt0cf702f2013-07-30 00:50:39 +000064namespace {
65
66typedef struct Address {
67 enum {
68 RegBase,
69 FrameIndexBase
70 } BaseType;
71
72 union {
73 unsigned Reg;
74 int FI;
75 } Base;
76
Bill Schmidtccecf262013-08-30 02:29:45 +000077 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000078
79 // Innocuous defaults for our address.
80 Address()
81 : BaseType(RegBase), Offset(0) {
82 Base.Reg = 0;
83 }
84} Address;
85
Craig Topper26696312014-03-18 07:27:13 +000086class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000087
88 const TargetMachine &TM;
Eric Christopher85806142015-01-30 02:11:24 +000089 const PPCSubtarget *PPCSubTarget;
Hal Finkele6698d52015-02-01 15:03:28 +000090 PPCFunctionInfo *PPCFuncInfo;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000091 const TargetInstrInfo &TII;
92 const TargetLowering &TLI;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000093 LLVMContext *Context;
94
95 public:
96 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
97 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000098 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
Eric Christophercccae792015-01-30 22:02:31 +000099 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
Hal Finkele6698d52015-02-01 15:03:28 +0000100 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
Eric Christopher85806142015-01-30 02:11:24 +0000101 TII(*PPCSubTarget->getInstrInfo()),
102 TLI(*PPCSubTarget->getTargetLowering()),
Eric Christopherd9134482014-08-04 21:25:23 +0000103 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000104
105 // Backend specific FastISel code.
106 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000107 bool fastSelectInstruction(const Instruction *I) override;
108 unsigned fastMaterializeConstant(const Constant *C) override;
109 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000110 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
111 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000112 bool fastLowerArguments() override;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000113 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
114 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000118 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000121 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000125
Hal Finkel934361a2015-01-14 01:07:51 +0000126 bool fastLowerCall(CallLoweringInfo &CLI) override;
127
Bill Schmidt03008132013-08-25 22:33:42 +0000128 // Instruction selection routines.
129 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000130 bool SelectLoad(const Instruction *I);
131 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000132 bool SelectBranch(const Instruction *I);
133 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000134 bool SelectFPExt(const Instruction *I);
135 bool SelectFPTrunc(const Instruction *I);
136 bool SelectIToFP(const Instruction *I, bool IsSigned);
137 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000138 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000139 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000140 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000141 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000142
143 // Utility routines.
144 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000145 bool isTypeLegal(Type *Ty, MVT &VT);
146 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000147 bool isValueAvailable(const Value *V) const;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000148 bool isVSFRCRegister(unsigned Register) const {
149 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
150 }
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000151 bool isVSSRCRegister(unsigned Register) const {
152 return MRI.getRegClass(Register)->getID() == PPC::VSSRCRegClassID;
153 }
Bill Schmidt03008132013-08-25 22:33:42 +0000154 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
155 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000156 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
157 const TargetRegisterClass *RC, bool IsZExt = true,
158 unsigned FP64LoadOpc = PPC::LFD);
159 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
160 bool PPCComputeAddress(const Value *Obj, Address &Addr);
161 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
162 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000163 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
164 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000165 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000166 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000167 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
168 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000169 unsigned PPCMaterialize32BitInt(int64_t Imm,
170 const TargetRegisterClass *RC);
171 unsigned PPCMaterialize64BitInt(int64_t Imm,
172 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000173 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
174 unsigned SrcReg, bool IsSigned);
175 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000176
Bill Schmidtd89f6782013-08-26 19:42:51 +0000177 // Call handling routines.
178 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000179 bool processCallArgs(SmallVectorImpl<Value*> &Args,
180 SmallVectorImpl<unsigned> &ArgRegs,
181 SmallVectorImpl<MVT> &ArgVTs,
182 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
183 SmallVectorImpl<unsigned> &RegArgs,
184 CallingConv::ID CC,
185 unsigned &NumBytes,
186 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000187 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000188 CCAssignFn *usePPC32CCs(unsigned Flag);
189
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000190 private:
191 #include "PPCGenFastISel.inc"
192
193};
194
195} // end anonymous namespace
196
Bill Schmidtd89f6782013-08-26 19:42:51 +0000197#include "PPCGenCallingConv.inc"
198
199// Function whose sole purpose is to kill compiler warnings
200// stemming from unused functions included from PPCGenCallingConv.inc.
201CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
202 if (Flag == 1)
203 return CC_PPC32_SVR4;
204 else if (Flag == 2)
205 return CC_PPC32_SVR4_ByVal;
206 else if (Flag == 3)
207 return CC_PPC32_SVR4_VarArg;
208 else
209 return RetCC_PPC;
210}
211
Bill Schmidt03008132013-08-25 22:33:42 +0000212static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
213 switch (Pred) {
214 // These are not representable with any single compare.
215 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000216 case CmpInst::FCMP_TRUE:
217 // Major concern about the following 6 cases is NaN result. The comparison
218 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
219 // only one of which will be set. The result is generated by fcmpu
220 // instruction. However, bc instruction only inspects one of the first 3
221 // bits, so when un is set, bc instruction may jump to to an undesired
222 // place.
223 //
224 // More specifically, if we expect an unordered comparison and un is set, we
225 // expect to always go to true branch; in such case UEQ, UGT and ULT still
226 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
227 // since they are tested by inspecting !eq, !lt, !gt, respectively.
228 //
229 // Similarly, for ordered comparison, when un is set, we always expect the
230 // result to be false. In such case OGT, OLT and OEQ is good, since they are
231 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
232 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000233 case CmpInst::FCMP_UEQ:
234 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000235 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000236 case CmpInst::FCMP_OGE:
237 case CmpInst::FCMP_OLE:
238 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000239 default:
240 return Optional<PPC::Predicate>();
241
242 case CmpInst::FCMP_OEQ:
243 case CmpInst::ICMP_EQ:
244 return PPC::PRED_EQ;
245
246 case CmpInst::FCMP_OGT:
247 case CmpInst::ICMP_UGT:
248 case CmpInst::ICMP_SGT:
249 return PPC::PRED_GT;
250
Tim Shen5cdf7502016-03-17 22:27:58 +0000251 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000252 case CmpInst::ICMP_UGE:
253 case CmpInst::ICMP_SGE:
254 return PPC::PRED_GE;
255
256 case CmpInst::FCMP_OLT:
257 case CmpInst::ICMP_ULT:
258 case CmpInst::ICMP_SLT:
259 return PPC::PRED_LT;
260
Tim Shen5cdf7502016-03-17 22:27:58 +0000261 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000262 case CmpInst::ICMP_ULE:
263 case CmpInst::ICMP_SLE:
264 return PPC::PRED_LE;
265
Tim Shen5cdf7502016-03-17 22:27:58 +0000266 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000267 case CmpInst::ICMP_NE:
268 return PPC::PRED_NE;
269
270 case CmpInst::FCMP_ORD:
271 return PPC::PRED_NU;
272
273 case CmpInst::FCMP_UNO:
274 return PPC::PRED_UN;
275 }
276}
277
Bill Schmidtccecf262013-08-30 02:29:45 +0000278// Determine whether the type Ty is simple enough to be handled by
279// fast-isel, and return its equivalent machine type in VT.
280// FIXME: Copied directly from ARM -- factor into base class?
281bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000282 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000283
284 // Only handle simple types.
285 if (Evt == MVT::Other || !Evt.isSimple()) return false;
286 VT = Evt.getSimpleVT();
287
288 // Handle all legal types, i.e. a register that will directly hold this
289 // value.
290 return TLI.isTypeLegal(VT);
291}
292
293// Determine whether the type Ty is simple enough to be handled by
294// fast-isel as a load target, and return its equivalent machine type in VT.
295bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
296 if (isTypeLegal(Ty, VT)) return true;
297
298 // If this is a type than can be sign or zero-extended to a basic operation
299 // go ahead and accept it now.
300 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
301 return true;
302 }
303
304 return false;
305}
306
Hal Finkel5f2a1372015-05-23 12:18:10 +0000307bool PPCFastISel::isValueAvailable(const Value *V) const {
308 if (!isa<Instruction>(V))
309 return true;
310
311 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000312 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000313}
314
Bill Schmidtccecf262013-08-30 02:29:45 +0000315// Given a value Obj, create an Address object Addr that represents its
316// address. Return false if we can't handle it.
317bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000318 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000319 unsigned Opcode = Instruction::UserOp1;
320 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
321 // Don't walk into other basic blocks unless the object is an alloca from
322 // another block, otherwise it may not have a virtual register assigned.
323 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
324 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
325 Opcode = I->getOpcode();
326 U = I;
327 }
328 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
329 Opcode = C->getOpcode();
330 U = C;
331 }
332
333 switch (Opcode) {
334 default:
335 break;
336 case Instruction::BitCast:
337 // Look through bitcasts.
338 return PPCComputeAddress(U->getOperand(0), Addr);
339 case Instruction::IntToPtr:
340 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000341 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
342 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000343 return PPCComputeAddress(U->getOperand(0), Addr);
344 break;
345 case Instruction::PtrToInt:
346 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000347 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000348 return PPCComputeAddress(U->getOperand(0), Addr);
349 break;
350 case Instruction::GetElementPtr: {
351 Address SavedAddr = Addr;
352 long TmpOffset = Addr.Offset;
353
354 // Iterate through the GEP folding the constants into offsets where
355 // we can.
356 gep_type_iterator GTI = gep_type_begin(U);
357 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
358 II != IE; ++II, ++GTI) {
359 const Value *Op = *II;
360 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000361 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000362 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
363 TmpOffset += SL->getElementOffset(Idx);
364 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000365 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000366 for (;;) {
367 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
368 // Constant-offset addressing.
369 TmpOffset += CI->getSExtValue() * S;
370 break;
371 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000372 if (canFoldAddIntoGEP(U, Op)) {
373 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000374 ConstantInt *CI =
375 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
376 TmpOffset += CI->getSExtValue() * S;
377 // Iterate on the other operand.
378 Op = cast<AddOperator>(Op)->getOperand(0);
379 continue;
380 }
381 // Unsupported
382 goto unsupported_gep;
383 }
384 }
385 }
386
387 // Try to grab the base operand now.
388 Addr.Offset = TmpOffset;
389 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
390
391 // We failed, restore everything and try the other options.
392 Addr = SavedAddr;
393
394 unsupported_gep:
395 break;
396 }
397 case Instruction::Alloca: {
398 const AllocaInst *AI = cast<AllocaInst>(Obj);
399 DenseMap<const AllocaInst*, int>::iterator SI =
400 FuncInfo.StaticAllocaMap.find(AI);
401 if (SI != FuncInfo.StaticAllocaMap.end()) {
402 Addr.BaseType = Address::FrameIndexBase;
403 Addr.Base.FI = SI->second;
404 return true;
405 }
406 break;
407 }
408 }
409
410 // FIXME: References to parameters fall through to the behavior
411 // below. They should be able to reference a frame index since
412 // they are stored to the stack, so we can get "ld rx, offset(r1)"
413 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
414 // just contain the parameter. Try to handle this with a FI.
415
416 // Try to get this in a register if nothing else has worked.
417 if (Addr.Base.Reg == 0)
418 Addr.Base.Reg = getRegForValue(Obj);
419
420 // Prevent assignment of base register to X0, which is inappropriate
421 // for loads and stores alike.
422 if (Addr.Base.Reg != 0)
423 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
424
425 return Addr.Base.Reg != 0;
426}
427
428// Fix up some addresses that can't be used directly. For example, if
429// an offset won't fit in an instruction field, we may need to move it
430// into an index register.
431void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset,
432 unsigned &IndexReg) {
433
434 // Check whether the offset fits in the instruction field.
435 if (!isInt<16>(Addr.Offset))
436 UseOffset = false;
437
438 // If this is a stack pointer and the offset needs to be simplified then
439 // put the alloca address into a register, set the base type back to
440 // register and continue. This should almost never happen.
441 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
442 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000443 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000444 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
445 Addr.Base.Reg = ResultReg;
446 Addr.BaseType = Address::RegBase;
447 }
448
449 if (!UseOffset) {
450 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context)
451 : Type::getInt64Ty(*Context));
452 const ConstantInt *Offset =
453 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
454 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
455 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
456 }
457}
458
459// Emit a load instruction if possible, returning true if we succeeded,
460// otherwise false. See commentary below for how the register class of
461// the load is determined.
462bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
463 const TargetRegisterClass *RC,
464 bool IsZExt, unsigned FP64LoadOpc) {
465 unsigned Opc;
466 bool UseOffset = true;
467
468 // If ResultReg is given, it determines the register class of the load.
469 // Otherwise, RC is the register class to use. If the result of the
470 // load isn't anticipated in this block, both may be zero, in which
471 // case we must make a conservative guess. In particular, don't assign
472 // R0 or X0 to the result register, as the result may be used in a load,
473 // store, add-immediate, or isel that won't permit this. (Though
474 // perhaps the spill and reload of live-exit values would handle this?)
475 const TargetRegisterClass *UseRC =
476 (ResultReg ? MRI.getRegClass(ResultReg) :
477 (RC ? RC :
478 (VT == MVT::f64 ? &PPC::F8RCRegClass :
479 (VT == MVT::f32 ? &PPC::F4RCRegClass :
480 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
481 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
482
483 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
484
485 switch (VT.SimpleTy) {
486 default: // e.g., vector types not handled
487 return false;
488 case MVT::i8:
489 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
490 break;
491 case MVT::i16:
492 Opc = (IsZExt ?
493 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
494 (Is32BitInt ? PPC::LHA : PPC::LHA8));
495 break;
496 case MVT::i32:
497 Opc = (IsZExt ?
498 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
499 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
500 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
501 UseOffset = false;
502 break;
503 case MVT::i64:
504 Opc = PPC::LD;
505 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
506 "64-bit load with 32-bit target??");
507 UseOffset = ((Addr.Offset & 3) == 0);
508 break;
509 case MVT::f32:
510 Opc = PPC::LFS;
511 break;
512 case MVT::f64:
513 Opc = FP64LoadOpc;
514 break;
515 }
516
517 // If necessary, materialize the offset into a register and use
518 // the indexed form. Also handle stack pointers with special needs.
519 unsigned IndexReg = 0;
520 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000521
522 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
523 // be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000524 bool IsVSSRC = (ResultReg != 0) && isVSSRCRegister(ResultReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000525 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000526 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
527 bool Is64VSXLoad = IsVSSRC && Opc == PPC::LFD;
528 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000529 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
530 (Addr.Offset == 0)) {
531 UseOffset = false;
532 }
533
Bill Schmidtccecf262013-08-30 02:29:45 +0000534 if (ResultReg == 0)
535 ResultReg = createResultReg(UseRC);
536
537 // Note: If we still have a frame index here, we know the offset is
538 // in range, as otherwise PPCSimplifyAddress would have converted it
539 // into a RegBase.
540 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000541 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000542 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000543
Alex Lorenze40c8a22015-08-11 23:09:45 +0000544 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
545 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
546 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000547 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
548 MFI.getObjectAlignment(Addr.Base.FI));
549
Rafael Espindolaea09c592014-02-18 22:05:46 +0000550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000551 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
552
553 // Base reg with offset in range.
554 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000555 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000556 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000557
Rafael Espindolaea09c592014-02-18 22:05:46 +0000558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000559 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
560
561 // Indexed form.
562 } else {
563 // Get the RR opcode corresponding to the RI one. FIXME: It would be
564 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
565 // is hard to get at.
566 switch (Opc) {
567 default: llvm_unreachable("Unexpected opcode!");
568 case PPC::LBZ: Opc = PPC::LBZX; break;
569 case PPC::LBZ8: Opc = PPC::LBZX8; break;
570 case PPC::LHZ: Opc = PPC::LHZX; break;
571 case PPC::LHZ8: Opc = PPC::LHZX8; break;
572 case PPC::LHA: Opc = PPC::LHAX; break;
573 case PPC::LHA8: Opc = PPC::LHAX8; break;
574 case PPC::LWZ: Opc = PPC::LWZX; break;
575 case PPC::LWZ8: Opc = PPC::LWZX8; break;
576 case PPC::LWA: Opc = PPC::LWAX; break;
577 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
578 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000579 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000580 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000581 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000582 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000583 .addReg(Addr.Base.Reg).addReg(IndexReg);
584 }
585
586 return true;
587}
588
589// Attempt to fast-select a load instruction.
590bool PPCFastISel::SelectLoad(const Instruction *I) {
591 // FIXME: No atomic loads are supported.
592 if (cast<LoadInst>(I)->isAtomic())
593 return false;
594
595 // Verify we have a legal type before going any further.
596 MVT VT;
597 if (!isLoadTypeLegal(I->getType(), VT))
598 return false;
599
600 // See if we can handle this address.
601 Address Addr;
602 if (!PPCComputeAddress(I->getOperand(0), Addr))
603 return false;
604
605 // Look at the currently assigned register for this instruction
606 // to determine the required register class. This is necessary
607 // to constrain RA from using R0/X0 when this is not legal.
608 unsigned AssignedReg = FuncInfo.ValueMap[I];
609 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000610 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000611
612 unsigned ResultReg = 0;
613 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
614 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000615 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000616 return true;
617}
618
619// Emit a store instruction to store SrcReg at Addr.
620bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
621 assert(SrcReg && "Nothing to store!");
622 unsigned Opc;
623 bool UseOffset = true;
624
625 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
626 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
627
628 switch (VT.SimpleTy) {
629 default: // e.g., vector types not handled
630 return false;
631 case MVT::i8:
632 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
633 break;
634 case MVT::i16:
635 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
636 break;
637 case MVT::i32:
638 assert(Is32BitInt && "Not GPRC for i32??");
639 Opc = PPC::STW;
640 break;
641 case MVT::i64:
642 Opc = PPC::STD;
643 UseOffset = ((Addr.Offset & 3) == 0);
644 break;
645 case MVT::f32:
646 Opc = PPC::STFS;
647 break;
648 case MVT::f64:
649 Opc = PPC::STFD;
650 break;
651 }
652
653 // If necessary, materialize the offset into a register and use
654 // the indexed form. Also handle stack pointers with special needs.
655 unsigned IndexReg = 0;
656 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
657
Bill Seurer8c728ae2014-12-05 20:15:56 +0000658 // If this is a potential VSX store with an offset of 0, a VSX indexed store
659 // can be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000660 bool IsVSSRC = isVSSRCRegister(SrcReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000661 bool IsVSFRC = isVSFRCRegister(SrcReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000662 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
663 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
664 if ((Is32VSXStore || Is64VSXStore) &&
665 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000666 (Addr.Offset == 0)) {
667 UseOffset = false;
668 }
669
Bill Schmidtccecf262013-08-30 02:29:45 +0000670 // Note: If we still have a frame index here, we know the offset is
671 // in range, as otherwise PPCSimplifyAddress would have converted it
672 // into a RegBase.
673 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000674 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000675 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000676
Alex Lorenze40c8a22015-08-11 23:09:45 +0000677 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
678 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
679 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000680 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
681 MFI.getObjectAlignment(Addr.Base.FI));
682
Rafael Espindolaea09c592014-02-18 22:05:46 +0000683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
684 .addReg(SrcReg)
685 .addImm(Addr.Offset)
686 .addFrameIndex(Addr.Base.FI)
687 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000688
689 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000690 } else if (UseOffset) {
691 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000692 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000693
Rafael Espindolaea09c592014-02-18 22:05:46 +0000694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000695 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
696
697 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000698 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000699 // Get the RR opcode corresponding to the RI one. FIXME: It would be
700 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
701 // is hard to get at.
702 switch (Opc) {
703 default: llvm_unreachable("Unexpected opcode!");
704 case PPC::STB: Opc = PPC::STBX; break;
705 case PPC::STH : Opc = PPC::STHX; break;
706 case PPC::STW : Opc = PPC::STWX; break;
707 case PPC::STB8: Opc = PPC::STBX8; break;
708 case PPC::STH8: Opc = PPC::STHX8; break;
709 case PPC::STW8: Opc = PPC::STWX8; break;
710 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000711 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000712 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000713 }
Samuel Antaof6815602015-03-17 15:00:57 +0000714
715 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
716 .addReg(SrcReg);
717
718 // If we have an index register defined we use it in the store inst,
719 // otherwise we use X0 as base as it makes the vector instructions to
720 // use zero in the computation of the effective address regardless the
721 // content of the register.
722 if (IndexReg)
723 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
724 else
725 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000726 }
727
728 return true;
729}
730
731// Attempt to fast-select a store instruction.
732bool PPCFastISel::SelectStore(const Instruction *I) {
733 Value *Op0 = I->getOperand(0);
734 unsigned SrcReg = 0;
735
736 // FIXME: No atomics loads are supported.
737 if (cast<StoreInst>(I)->isAtomic())
738 return false;
739
740 // Verify we have a legal type before going any further.
741 MVT VT;
742 if (!isLoadTypeLegal(Op0->getType(), VT))
743 return false;
744
745 // Get the value to be stored into a register.
746 SrcReg = getRegForValue(Op0);
747 if (SrcReg == 0)
748 return false;
749
750 // See if we can handle this address.
751 Address Addr;
752 if (!PPCComputeAddress(I->getOperand(1), Addr))
753 return false;
754
755 if (!PPCEmitStore(VT, SrcReg, Addr))
756 return false;
757
758 return true;
759}
760
Bill Schmidt03008132013-08-25 22:33:42 +0000761// Attempt to fast-select a branch instruction.
762bool PPCFastISel::SelectBranch(const Instruction *I) {
763 const BranchInst *BI = cast<BranchInst>(I);
764 MachineBasicBlock *BrBB = FuncInfo.MBB;
765 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
766 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
767
768 // For now, just try the simplest case where it's fed by a compare.
769 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000770 if (isValueAvailable(CI)) {
771 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
772 if (!OptPPCPred)
773 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000774
Hal Finkel5f2a1372015-05-23 12:18:10 +0000775 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000776
Hal Finkel5f2a1372015-05-23 12:18:10 +0000777 // Take advantage of fall-through opportunities.
778 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
779 std::swap(TBB, FBB);
780 PPCPred = PPC::InvertPredicate(PPCPred);
781 }
782
783 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
784
785 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
786 CondReg))
787 return false;
788
789 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
790 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000791 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000792 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000793 }
Bill Schmidt03008132013-08-25 22:33:42 +0000794 } else if (const ConstantInt *CI =
795 dyn_cast<ConstantInt>(BI->getCondition())) {
796 uint64_t Imm = CI->getZExtValue();
797 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000798 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000799 return true;
800 }
801
802 // FIXME: ARM looks for a case where the block containing the compare
803 // has been split from the block containing the branch. If this happens,
804 // there is a vreg available containing the result of the compare. I'm
805 // not sure we can do much, as we've lost the predicate information with
806 // the compare instruction -- we have a 4-bit CR but don't know which bit
807 // to test here.
808 return false;
809}
810
811// Attempt to emit a compare of the two source values. Signed and unsigned
812// comparisons are supported. Return false if we can't handle it.
813bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
814 bool IsZExt, unsigned DestReg) {
815 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000816 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000817 if (!SrcEVT.isSimple())
818 return false;
819 MVT SrcVT = SrcEVT.getSimpleVT();
820
Eric Christopher1b8e7632014-05-22 01:07:24 +0000821 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000822 return false;
823
Bill Schmidt03008132013-08-25 22:33:42 +0000824 // See if operand 2 is an immediate encodeable in the compare.
825 // FIXME: Operands are not in canonical order at -O0, so an immediate
826 // operand in position 1 is a lost opportunity for now. We are
827 // similar to ARM in this regard.
828 long Imm = 0;
829 bool UseImm = false;
830
831 // Only 16-bit integer constants can be represented in compares for
832 // PowerPC. Others will be materialized into a register.
833 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
834 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
835 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
836 const APInt &CIVal = ConstInt->getValue();
837 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
838 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
839 UseImm = true;
840 }
841 }
842
843 unsigned CmpOpc;
844 bool NeedsExt = false;
845 switch (SrcVT.SimpleTy) {
846 default: return false;
847 case MVT::f32:
848 CmpOpc = PPC::FCMPUS;
849 break;
850 case MVT::f64:
851 CmpOpc = PPC::FCMPUD;
852 break;
853 case MVT::i1:
854 case MVT::i8:
855 case MVT::i16:
856 NeedsExt = true;
857 // Intentional fall-through.
858 case MVT::i32:
859 if (!UseImm)
860 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
861 else
862 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
863 break;
864 case MVT::i64:
865 if (!UseImm)
866 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
867 else
868 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
869 break;
870 }
871
872 unsigned SrcReg1 = getRegForValue(SrcValue1);
873 if (SrcReg1 == 0)
874 return false;
875
876 unsigned SrcReg2 = 0;
877 if (!UseImm) {
878 SrcReg2 = getRegForValue(SrcValue2);
879 if (SrcReg2 == 0)
880 return false;
881 }
882
883 if (NeedsExt) {
884 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
885 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
886 return false;
887 SrcReg1 = ExtReg;
888
889 if (!UseImm) {
890 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
891 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
892 return false;
893 SrcReg2 = ExtReg;
894 }
895 }
896
897 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000898 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000899 .addReg(SrcReg1).addReg(SrcReg2);
900 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000901 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000902 .addReg(SrcReg1).addImm(Imm);
903
904 return true;
905}
906
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000907// Attempt to fast-select a floating-point extend instruction.
908bool PPCFastISel::SelectFPExt(const Instruction *I) {
909 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000910 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
911 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000912
913 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
914 return false;
915
916 unsigned SrcReg = getRegForValue(Src);
917 if (!SrcReg)
918 return false;
919
920 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000921 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000922 return true;
923}
924
925// Attempt to fast-select a floating-point truncate instruction.
926bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
927 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000928 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
929 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000930
931 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
932 return false;
933
934 unsigned SrcReg = getRegForValue(Src);
935 if (!SrcReg)
936 return false;
937
938 // Round the result to single precision.
939 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000940 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000941 .addReg(SrcReg);
942
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000943 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000944 return true;
945}
946
947// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000948// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000949// those should be used instead of moving via a stack slot when the
950// subtarget permits.
951// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
952// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
953// case to 8 bytes which produces tighter code but wastes stack space.
954unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
955 bool IsSigned) {
956
957 // If necessary, extend 32-bit int to 64-bit.
958 if (SrcVT == MVT::i32) {
959 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
960 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
961 return 0;
962 SrcReg = TmpReg;
963 }
964
965 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
966 Address Addr;
967 Addr.BaseType = Address::FrameIndexBase;
968 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
969
970 // Store the value from the GPR.
971 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
972 return 0;
973
974 // Load the integer value into an FPR. The kind of load used depends
975 // on a number of conditions.
976 unsigned LoadOpc = PPC::LFD;
977
978 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000979 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000980 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000981 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000982 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000983 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000984 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000985 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000986 }
987
988 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
989 unsigned ResultReg = 0;
990 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
991 return 0;
992
993 return ResultReg;
994}
995
996// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000997// FIXME: Once fast-isel has better support for VSX, conversions using
998// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000999bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1000 MVT DstVT;
1001 Type *DstTy = I->getType();
1002 if (!isTypeLegal(DstTy, DstVT))
1003 return false;
1004
1005 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1006 return false;
1007
1008 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001009 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001010 if (!SrcEVT.isSimple())
1011 return false;
1012
1013 MVT SrcVT = SrcEVT.getSimpleVT();
1014
1015 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1016 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1017 return false;
1018
1019 unsigned SrcReg = getRegForValue(Src);
1020 if (SrcReg == 0)
1021 return false;
1022
1023 // We can only lower an unsigned convert if we have the newer
1024 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001025 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001026 return false;
1027
1028 // FIXME: For now we require the newer floating-point conversion operations
1029 // (which are present only on P7 and A2 server models) when converting
1030 // to single-precision float. Otherwise we have to generate a lot of
1031 // fiddly code to avoid double rounding. If necessary, the fiddly code
1032 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001033 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001034 return false;
1035
1036 // Extend the input if necessary.
1037 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1038 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1039 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1040 return false;
1041 SrcVT = MVT::i64;
1042 SrcReg = TmpReg;
1043 }
1044
1045 // Move the integer value to an FPR.
1046 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1047 if (FPReg == 0)
1048 return false;
1049
1050 // Determine the opcode for the conversion.
1051 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1052 unsigned DestReg = createResultReg(RC);
1053 unsigned Opc;
1054
1055 if (DstVT == MVT::f32)
1056 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1057 else
1058 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1059
1060 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001062 .addReg(FPReg);
1063
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001064 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001065 return true;
1066}
1067
1068// Move the floating-point value in SrcReg into an integer destination
1069// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001070// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001071// those should be used instead of moving via a stack slot when the
1072// subtarget permits.
1073unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1074 unsigned SrcReg, bool IsSigned) {
1075 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1076 // Note that if have STFIWX available, we could use a 4-byte stack
1077 // slot for i32, but this being fast-isel we'll just go with the
1078 // easiest code gen possible.
1079 Address Addr;
1080 Addr.BaseType = Address::FrameIndexBase;
1081 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1082
1083 // Store the value from the FPR.
1084 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1085 return 0;
1086
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001087 // Reload it into a GPR. If we want an i32 on big endian, modify the
1088 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001089 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001090 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001091
1092 // Look at the currently assigned register for this instruction
1093 // to determine the required register class.
1094 unsigned AssignedReg = FuncInfo.ValueMap[I];
1095 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001096 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001097
1098 unsigned ResultReg = 0;
1099 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1100 return 0;
1101
1102 return ResultReg;
1103}
1104
1105// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001106// FIXME: Once fast-isel has better support for VSX, conversions using
1107// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001108bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1109 MVT DstVT, SrcVT;
1110 Type *DstTy = I->getType();
1111 if (!isTypeLegal(DstTy, DstVT))
1112 return false;
1113
1114 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1115 return false;
1116
Bill Schmidt83973ef2014-06-24 20:05:18 +00001117 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1118 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1119 return false;
1120
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001121 Value *Src = I->getOperand(0);
1122 Type *SrcTy = Src->getType();
1123 if (!isTypeLegal(SrcTy, SrcVT))
1124 return false;
1125
1126 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1127 return false;
1128
1129 unsigned SrcReg = getRegForValue(Src);
1130 if (SrcReg == 0)
1131 return false;
1132
1133 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001134 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001135 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1136 if (InRC == &PPC::F4RCRegClass) {
1137 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001139 TII.get(TargetOpcode::COPY), TmpReg)
1140 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001141 SrcReg = TmpReg;
1142 }
1143
1144 // Determine the opcode for the conversion, which takes place
1145 // entirely within FPRs.
1146 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1147 unsigned Opc;
1148
1149 if (DstVT == MVT::i32)
1150 if (IsSigned)
1151 Opc = PPC::FCTIWZ;
1152 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001153 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001154 else
1155 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1156
1157 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001159 .addReg(SrcReg);
1160
1161 // Now move the integer value from a float register to an integer register.
1162 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1163 if (IntReg == 0)
1164 return false;
1165
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001166 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001167 return true;
1168}
1169
Bill Schmidtccecf262013-08-30 02:29:45 +00001170// Attempt to fast-select a binary integer operation that isn't already
1171// handled automatically.
1172bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001173 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001174
1175 // We can get here in the case when we have a binary operation on a non-legal
1176 // type and the target independent selector doesn't know how to handle it.
1177 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1178 return false;
1179
1180 // Look at the currently assigned register for this instruction
1181 // to determine the required register class. If there is no register,
1182 // make a conservative choice (don't assign R0).
1183 unsigned AssignedReg = FuncInfo.ValueMap[I];
1184 const TargetRegisterClass *RC =
1185 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1186 &PPC::GPRC_and_GPRC_NOR0RegClass);
1187 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1188
1189 unsigned Opc;
1190 switch (ISDOpcode) {
1191 default: return false;
1192 case ISD::ADD:
1193 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1194 break;
1195 case ISD::OR:
1196 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1197 break;
1198 case ISD::SUB:
1199 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1200 break;
1201 }
1202
1203 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1204 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1205 if (SrcReg1 == 0) return false;
1206
1207 // Handle case of small immediate operand.
1208 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1209 const APInt &CIVal = ConstInt->getValue();
1210 int Imm = (int)CIVal.getSExtValue();
1211 bool UseImm = true;
1212 if (isInt<16>(Imm)) {
1213 switch (Opc) {
1214 default:
1215 llvm_unreachable("Missing case!");
1216 case PPC::ADD4:
1217 Opc = PPC::ADDI;
1218 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1219 break;
1220 case PPC::ADD8:
1221 Opc = PPC::ADDI8;
1222 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1223 break;
1224 case PPC::OR:
1225 Opc = PPC::ORI;
1226 break;
1227 case PPC::OR8:
1228 Opc = PPC::ORI8;
1229 break;
1230 case PPC::SUBF:
1231 if (Imm == -32768)
1232 UseImm = false;
1233 else {
1234 Opc = PPC::ADDI;
1235 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1236 Imm = -Imm;
1237 }
1238 break;
1239 case PPC::SUBF8:
1240 if (Imm == -32768)
1241 UseImm = false;
1242 else {
1243 Opc = PPC::ADDI8;
1244 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1245 Imm = -Imm;
1246 }
1247 break;
1248 }
1249
1250 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1252 ResultReg)
1253 .addReg(SrcReg1)
1254 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001255 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001256 return true;
1257 }
1258 }
1259 }
1260
1261 // Reg-reg case.
1262 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1263 if (SrcReg2 == 0) return false;
1264
1265 // Reverse operands for subtract-from.
1266 if (ISDOpcode == ISD::SUB)
1267 std::swap(SrcReg1, SrcReg2);
1268
Rafael Espindolaea09c592014-02-18 22:05:46 +00001269 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001270 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001271 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001272 return true;
1273}
1274
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001275// Handle arguments to a call that we're attempting to fast-select.
1276// Return false if the arguments are too complex for us at the moment.
1277bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1278 SmallVectorImpl<unsigned> &ArgRegs,
1279 SmallVectorImpl<MVT> &ArgVTs,
1280 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1281 SmallVectorImpl<unsigned> &RegArgs,
1282 CallingConv::ID CC,
1283 unsigned &NumBytes,
1284 bool IsVarArg) {
1285 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001286 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001287
1288 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001289 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001290 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001291
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001292 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1293
1294 // Bail out if we can't handle any of the arguments.
1295 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1296 CCValAssign &VA = ArgLocs[I];
1297 MVT ArgVT = ArgVTs[VA.getValNo()];
1298
1299 // Skip vector arguments for now, as well as long double and
1300 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001301 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001302 !VA.isRegLoc() || VA.needsCustom())
1303 return false;
1304
1305 // Skip bit-converted arguments for now.
1306 if (VA.getLocInfo() == CCValAssign::BCvt)
1307 return false;
1308 }
1309
1310 // Get a count of how many bytes are to be pushed onto the stack.
1311 NumBytes = CCInfo.getNextStackOffset();
1312
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001313 // The prolog code of the callee may store up to 8 GPR argument registers to
1314 // the stack, allowing va_start to index over them in memory if its varargs.
1315 // Because we cannot tell if this is needed on the caller side, we have to
1316 // conservatively assume that it is needed. As such, make sure we have at
1317 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001318 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001319 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001320
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001321 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001323 TII.get(TII.getCallFrameSetupOpcode()))
1324 .addImm(NumBytes);
1325
1326 // Prepare to assign register arguments. Every argument uses up a
1327 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001328 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001329 unsigned NextGPR = PPC::X3;
1330 unsigned NextFPR = PPC::F1;
1331
1332 // Process arguments.
1333 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1334 CCValAssign &VA = ArgLocs[I];
1335 unsigned Arg = ArgRegs[VA.getValNo()];
1336 MVT ArgVT = ArgVTs[VA.getValNo()];
1337
1338 // Handle argument promotion and bitcasts.
1339 switch (VA.getLocInfo()) {
1340 default:
1341 llvm_unreachable("Unknown loc info!");
1342 case CCValAssign::Full:
1343 break;
1344 case CCValAssign::SExt: {
1345 MVT DestVT = VA.getLocVT();
1346 const TargetRegisterClass *RC =
1347 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1348 unsigned TmpReg = createResultReg(RC);
1349 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1350 llvm_unreachable("Failed to emit a sext!");
1351 ArgVT = DestVT;
1352 Arg = TmpReg;
1353 break;
1354 }
1355 case CCValAssign::AExt:
1356 case CCValAssign::ZExt: {
1357 MVT DestVT = VA.getLocVT();
1358 const TargetRegisterClass *RC =
1359 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1360 unsigned TmpReg = createResultReg(RC);
1361 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1362 llvm_unreachable("Failed to emit a zext!");
1363 ArgVT = DestVT;
1364 Arg = TmpReg;
1365 break;
1366 }
1367 case CCValAssign::BCvt: {
1368 // FIXME: Not yet handled.
1369 llvm_unreachable("Should have bailed before getting here!");
1370 break;
1371 }
1372 }
1373
1374 // Copy this argument to the appropriate register.
1375 unsigned ArgReg;
1376 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1377 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001378 if (CC != CallingConv::Fast)
1379 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001380 } else
1381 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001382
1383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1384 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001385 RegArgs.push_back(ArgReg);
1386 }
1387
1388 return true;
1389}
1390
1391// For a call that we've determined we can fast-select, finish the
1392// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001393bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1394 CallingConv::ID CC = CLI.CallConv;
1395
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001396 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001397 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001398 TII.get(TII.getCallFrameDestroyOpcode()))
1399 .addImm(NumBytes).addImm(0);
1400
1401 // Next, generate a copy to obtain the return value.
1402 // FIXME: No multi-register return values yet, though I don't foresee
1403 // any real difficulties there.
1404 if (RetVT != MVT::isVoid) {
1405 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001406 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001407 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1408 CCValAssign &VA = RVLocs[0];
1409 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1410 assert(VA.isRegLoc() && "Can only return in registers!");
1411
1412 MVT DestVT = VA.getValVT();
1413 MVT CopyVT = DestVT;
1414
1415 // Ints smaller than a register still arrive in a full 64-bit
1416 // register, so make sure we recognize this.
1417 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1418 CopyVT = MVT::i64;
1419
1420 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001421 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001422
1423 if (RetVT == CopyVT) {
1424 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1425 ResultReg = createResultReg(CpyRC);
1426
Rafael Espindolaea09c592014-02-18 22:05:46 +00001427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001428 TII.get(TargetOpcode::COPY), ResultReg)
1429 .addReg(SourcePhysReg);
1430
1431 // If necessary, round the floating result to single precision.
1432 } else if (CopyVT == MVT::f64) {
1433 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001434 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001435 ResultReg).addReg(SourcePhysReg);
1436
1437 // If only the low half of a general register is needed, generate
1438 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1439 // used along the fast-isel path (not lowered), and downstream logic
1440 // also doesn't like a direct subreg copy on a physical reg.)
1441 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1442 ResultReg = createResultReg(&PPC::GPRCRegClass);
1443 // Convert physical register from G8RC to GPRC.
1444 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001446 TII.get(TargetOpcode::COPY), ResultReg)
1447 .addReg(SourcePhysReg);
1448 }
1449
Bill Schmidt0954ea12013-08-30 23:25:30 +00001450 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001451 CLI.InRegs.push_back(SourcePhysReg);
1452 CLI.ResultReg = ResultReg;
1453 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001454 }
Hal Finkel934361a2015-01-14 01:07:51 +00001455
1456 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001457}
1458
Hal Finkel934361a2015-01-14 01:07:51 +00001459bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1460 CallingConv::ID CC = CLI.CallConv;
1461 bool IsTailCall = CLI.IsTailCall;
1462 bool IsVarArg = CLI.IsVarArg;
1463 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001464 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001465
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001466 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001467 return false;
1468
1469 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001470 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001471 return false;
1472
Hal Finkel934361a2015-01-14 01:07:51 +00001473 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001474 if (IsVarArg)
1475 return false;
1476
1477 // Handle simple calls for now, with legal return types and
1478 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001479 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001480 MVT RetVT;
1481 if (RetTy->isVoidTy())
1482 RetVT = MVT::isVoid;
1483 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1484 RetVT != MVT::i8)
1485 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001486 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1487 // We can't handle boolean returns when CR bits are in use.
1488 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001489
1490 // FIXME: No multi-register return values yet.
1491 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1492 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1493 RetVT != MVT::f64) {
1494 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001495 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001496 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1497 if (RVLocs.size() > 1)
1498 return false;
1499 }
1500
1501 // Bail early if more than 8 arguments, as we only currently
1502 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001503 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001504 if (NumArgs > 8)
1505 return false;
1506
1507 // Set up the argument vectors.
1508 SmallVector<Value*, 8> Args;
1509 SmallVector<unsigned, 8> ArgRegs;
1510 SmallVector<MVT, 8> ArgVTs;
1511 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1512
1513 Args.reserve(NumArgs);
1514 ArgRegs.reserve(NumArgs);
1515 ArgVTs.reserve(NumArgs);
1516 ArgFlags.reserve(NumArgs);
1517
Hal Finkel934361a2015-01-14 01:07:51 +00001518 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001519 // Only handle easy calls for now. It would be reasonably easy
1520 // to handle <= 8-byte structures passed ByVal in registers, but we
1521 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001522 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1523 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001524 return false;
1525
Hal Finkel934361a2015-01-14 01:07:51 +00001526 Value *ArgValue = CLI.OutVals[i];
1527 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001528 MVT ArgVT;
1529 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1530 return false;
1531
1532 if (ArgVT.isVector())
1533 return false;
1534
Hal Finkel934361a2015-01-14 01:07:51 +00001535 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001536 if (Arg == 0)
1537 return false;
1538
Hal Finkel934361a2015-01-14 01:07:51 +00001539 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001540 ArgRegs.push_back(Arg);
1541 ArgVTs.push_back(ArgVT);
1542 ArgFlags.push_back(Flags);
1543 }
1544
1545 // Process the arguments.
1546 SmallVector<unsigned, 8> RegArgs;
1547 unsigned NumBytes;
1548
1549 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1550 RegArgs, CC, NumBytes, IsVarArg))
1551 return false;
1552
Hal Finkel934361a2015-01-14 01:07:51 +00001553 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001554 // FIXME: No handling for function pointers yet. This requires
1555 // implementing the function descriptor (OPD) setup.
1556 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001557 if (!GV) {
1558 // patchpoints are a special case; they always dispatch to a pointer value.
1559 // However, we don't actually want to generate the indirect call sequence
1560 // here (that will be generated, as necessary, during asm printing), and
1561 // the call we generate here will be erased by FastISel::selectPatchpoint,
1562 // so don't try very hard...
1563 if (CLI.IsPatchPoint)
1564 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1565 else
1566 return false;
1567 } else {
1568 // Build direct call with NOP for TOC restore.
1569 // FIXME: We can and should optimize away the NOP for local calls.
1570 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1571 TII.get(PPC::BL8_NOP));
1572 // Add callee.
1573 MIB.addGlobalAddress(GV);
1574 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001575
1576 // Add implicit physical register uses to the call.
1577 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1578 MIB.addReg(RegArgs[II], RegState::Implicit);
1579
Hal Finkelaf519932015-01-19 07:20:27 +00001580 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1581 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001582 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001583 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001584
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001585 // Add a register mask with the call-preserved registers. Proper
1586 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001587 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001588
Hal Finkel934361a2015-01-14 01:07:51 +00001589 CLI.Call = MIB;
1590
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001591 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001592 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001593}
1594
Bill Schmidtd89f6782013-08-26 19:42:51 +00001595// Attempt to fast-select a return instruction.
1596bool PPCFastISel::SelectRet(const Instruction *I) {
1597
1598 if (!FuncInfo.CanLowerReturn)
1599 return false;
1600
1601 const ReturnInst *Ret = cast<ReturnInst>(I);
1602 const Function &F = *I->getParent()->getParent();
1603
1604 // Build a list of return value registers.
1605 SmallVector<unsigned, 4> RetRegs;
1606 CallingConv::ID CC = F.getCallingConv();
1607
1608 if (Ret->getNumOperands() > 0) {
1609 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001610 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001611
1612 // Analyze operands of the call, assigning locations to each operand.
1613 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001614 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001615 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1616 const Value *RV = Ret->getOperand(0);
1617
1618 // FIXME: Only one output register for now.
1619 if (ValLocs.size() > 1)
1620 return false;
1621
Eric Christopherf0024d12015-07-25 00:48:08 +00001622 // Special case for returning a constant integer of any size - materialize
1623 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001624 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001625 CCValAssign &VA = ValLocs[0];
1626
1627 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001628 // We still need to worry about properly extending the sign. For example,
1629 // we could have only a single bit or a constant that needs zero
1630 // extension rather than sign extension. Make sure we pass the return
1631 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001632 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001633 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001634
Rafael Espindolaea09c592014-02-18 22:05:46 +00001635 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001636 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1637
Bill Schmidtd89f6782013-08-26 19:42:51 +00001638 RetRegs.push_back(RetReg);
1639
1640 } else {
1641 unsigned Reg = getRegForValue(RV);
1642
1643 if (Reg == 0)
1644 return false;
1645
1646 // Copy the result values into the output registers.
1647 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1648
1649 CCValAssign &VA = ValLocs[i];
1650 assert(VA.isRegLoc() && "Can only return in registers!");
1651 RetRegs.push_back(VA.getLocReg());
1652 unsigned SrcReg = Reg + VA.getValNo();
1653
Mehdi Amini44ede332015-07-09 02:09:04 +00001654 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001655 if (!RVEVT.isSimple())
1656 return false;
1657 MVT RVVT = RVEVT.getSimpleVT();
1658 MVT DestVT = VA.getLocVT();
1659
1660 if (RVVT != DestVT && RVVT != MVT::i8 &&
1661 RVVT != MVT::i16 && RVVT != MVT::i32)
1662 return false;
1663
1664 if (RVVT != DestVT) {
1665 switch (VA.getLocInfo()) {
1666 default:
1667 llvm_unreachable("Unknown loc info!");
1668 case CCValAssign::Full:
1669 llvm_unreachable("Full value assign but types don't match?");
1670 case CCValAssign::AExt:
1671 case CCValAssign::ZExt: {
1672 const TargetRegisterClass *RC =
1673 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1674 unsigned TmpReg = createResultReg(RC);
1675 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1676 return false;
1677 SrcReg = TmpReg;
1678 break;
1679 }
1680 case CCValAssign::SExt: {
1681 const TargetRegisterClass *RC =
1682 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1683 unsigned TmpReg = createResultReg(RC);
1684 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1685 return false;
1686 SrcReg = TmpReg;
1687 break;
1688 }
1689 }
1690 }
1691
Rafael Espindolaea09c592014-02-18 22:05:46 +00001692 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001693 TII.get(TargetOpcode::COPY), RetRegs[i])
1694 .addReg(SrcReg);
1695 }
1696 }
1697 }
1698
Rafael Espindolaea09c592014-02-18 22:05:46 +00001699 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001700 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001701
1702 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1703 MIB.addReg(RetRegs[i], RegState::Implicit);
1704
1705 return true;
1706}
1707
Bill Schmidt03008132013-08-25 22:33:42 +00001708// Attempt to emit an integer extend of SrcReg into DestReg. Both
1709// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001710// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001711bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1712 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001713 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1714 return false;
1715 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1716 return false;
1717
1718 // Signed extensions use EXTSB, EXTSH, EXTSW.
1719 if (!IsZExt) {
1720 unsigned Opc;
1721 if (SrcVT == MVT::i8)
1722 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1723 else if (SrcVT == MVT::i16)
1724 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1725 else {
1726 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1727 Opc = PPC::EXTSW_32_64;
1728 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001729 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001730 .addReg(SrcReg);
1731
1732 // Unsigned 32-bit extensions use RLWINM.
1733 } else if (DestVT == MVT::i32) {
1734 unsigned MB;
1735 if (SrcVT == MVT::i8)
1736 MB = 24;
1737 else {
1738 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1739 MB = 16;
1740 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001741 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001742 DestReg)
1743 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1744
1745 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1746 } else {
1747 unsigned MB;
1748 if (SrcVT == MVT::i8)
1749 MB = 56;
1750 else if (SrcVT == MVT::i16)
1751 MB = 48;
1752 else
1753 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001754 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001755 TII.get(PPC::RLDICL_32_64), DestReg)
1756 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1757 }
1758
1759 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001760}
1761
1762// Attempt to fast-select an indirect branch instruction.
1763bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1764 unsigned AddrReg = getRegForValue(I->getOperand(0));
1765 if (AddrReg == 0)
1766 return false;
1767
Rafael Espindolaea09c592014-02-18 22:05:46 +00001768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001769 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001770 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001771
1772 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001773 for (const BasicBlock *SuccBB : IB->successors())
1774 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001775
1776 return true;
1777}
1778
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001779// Attempt to fast-select an integer truncate instruction.
1780bool PPCFastISel::SelectTrunc(const Instruction *I) {
1781 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001782 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1783 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001784
1785 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1786 return false;
1787
1788 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1789 return false;
1790
1791 unsigned SrcReg = getRegForValue(Src);
1792 if (!SrcReg)
1793 return false;
1794
1795 // The only interesting case is when we need to switch register classes.
1796 if (SrcVT == MVT::i64) {
1797 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001798 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1799 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001800 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1801 SrcReg = ResultReg;
1802 }
1803
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001804 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001805 return true;
1806}
1807
Bill Schmidtd89f6782013-08-26 19:42:51 +00001808// Attempt to fast-select an integer extend instruction.
1809bool PPCFastISel::SelectIntExt(const Instruction *I) {
1810 Type *DestTy = I->getType();
1811 Value *Src = I->getOperand(0);
1812 Type *SrcTy = Src->getType();
1813
1814 bool IsZExt = isa<ZExtInst>(I);
1815 unsigned SrcReg = getRegForValue(Src);
1816 if (!SrcReg) return false;
1817
1818 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001819 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1820 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001821 if (!SrcEVT.isSimple())
1822 return false;
1823 if (!DestEVT.isSimple())
1824 return false;
1825
1826 MVT SrcVT = SrcEVT.getSimpleVT();
1827 MVT DestVT = DestEVT.getSimpleVT();
1828
1829 // If we know the register class needed for the result of this
1830 // instruction, use it. Otherwise pick the register class of the
1831 // correct size that does not contain X0/R0, since we don't know
1832 // whether downstream uses permit that assignment.
1833 unsigned AssignedReg = FuncInfo.ValueMap[I];
1834 const TargetRegisterClass *RC =
1835 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1836 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1837 &PPC::GPRC_and_GPRC_NOR0RegClass));
1838 unsigned ResultReg = createResultReg(RC);
1839
1840 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1841 return false;
1842
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001843 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001844 return true;
1845}
1846
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001847// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001848// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001849bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001850
1851 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001852 case Instruction::Load:
1853 return SelectLoad(I);
1854 case Instruction::Store:
1855 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001856 case Instruction::Br:
1857 return SelectBranch(I);
1858 case Instruction::IndirectBr:
1859 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001860 case Instruction::FPExt:
1861 return SelectFPExt(I);
1862 case Instruction::FPTrunc:
1863 return SelectFPTrunc(I);
1864 case Instruction::SIToFP:
1865 return SelectIToFP(I, /*IsSigned*/ true);
1866 case Instruction::UIToFP:
1867 return SelectIToFP(I, /*IsSigned*/ false);
1868 case Instruction::FPToSI:
1869 return SelectFPToI(I, /*IsSigned*/ true);
1870 case Instruction::FPToUI:
1871 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001872 case Instruction::Add:
1873 return SelectBinaryIntOp(I, ISD::ADD);
1874 case Instruction::Or:
1875 return SelectBinaryIntOp(I, ISD::OR);
1876 case Instruction::Sub:
1877 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001878 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001879 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001880 case Instruction::Ret:
1881 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001882 case Instruction::Trunc:
1883 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001884 case Instruction::ZExt:
1885 case Instruction::SExt:
1886 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001887 // Here add other flavors of Instruction::XXX that automated
1888 // cases don't catch. For example, switches are terminators
1889 // that aren't yet handled.
1890 default:
1891 break;
1892 }
1893 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001894}
1895
1896// Materialize a floating-point constant into a register, and return
1897// the register number (or zero if we failed to handle it).
1898unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1899 // No plans to handle long double here.
1900 if (VT != MVT::f32 && VT != MVT::f64)
1901 return 0;
1902
1903 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001904 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001905 assert(Align > 0 && "Unexpectedly missing alignment information!");
1906 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1907 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1908 CodeModel::Model CModel = TM.getCodeModel();
1909
Alex Lorenze40c8a22015-08-11 23:09:45 +00001910 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1911 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
1912 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001913
Bill Schmidt03008132013-08-25 22:33:42 +00001914 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1915 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1916
Hal Finkele6698d52015-02-01 15:03:28 +00001917 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001918 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1919 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001921 TmpReg)
1922 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001924 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1925 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001926 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001927 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001928 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001929 // But for large code model, we must generate a LDtocL followed
1930 // by the LF[SD].
1931 if (CModel == CodeModel::Large) {
1932 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001934 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001935 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001936 .addImm(0).addReg(TmpReg2);
1937 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001938 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001939 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1940 .addReg(TmpReg)
1941 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001942 }
1943
1944 return DestReg;
1945}
1946
Bill Schmidtccecf262013-08-30 02:29:45 +00001947// Materialize the address of a global value into a register, and return
1948// the register number (or zero if we failed to handle it).
1949unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1950 assert(VT == MVT::i64 && "Non-address!");
1951 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1952 unsigned DestReg = createResultReg(RC);
1953
1954 // Global values may be plain old object addresses, TLS object
1955 // addresses, constant pool entries, or jump tables. How we generate
1956 // code for these may depend on small, medium, or large code model.
1957 CodeModel::Model CModel = TM.getCodeModel();
1958
1959 // FIXME: Jump tables are not yet required because fast-isel doesn't
1960 // handle switches; if that changes, we need them as well. For now,
1961 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001962
1963 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001964 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001965 return 0;
1966
Hal Finkele6698d52015-02-01 15:03:28 +00001967 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001968 // For small code model, generate a simple TOC load.
1969 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001970 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1971 DestReg)
1972 .addGlobalAddress(GV)
1973 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001974 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001975 // If the address is an externally defined symbol, a symbol with common
1976 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001977 // jump table address (not yet needed), or if we are generating code
1978 // for large code model, we generate:
1979 // LDtocL(GV, ADDIStocHA(%X2, GV))
1980 // Otherwise we generate:
1981 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1982 // Either way, start with the ADDIStocHA:
1983 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001985 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1986
Eric Christopherc1808362015-11-20 20:51:31 +00001987 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
1988 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001990 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00001991 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00001992 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001993 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001994 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00001995 }
Bill Schmidtccecf262013-08-30 02:29:45 +00001996 }
1997
1998 return DestReg;
1999}
2000
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002001// Materialize a 32-bit integer constant into a register, and return
2002// the register number (or zero if we failed to handle it).
2003unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2004 const TargetRegisterClass *RC) {
2005 unsigned Lo = Imm & 0xFFFF;
2006 unsigned Hi = (Imm >> 16) & 0xFFFF;
2007
2008 unsigned ResultReg = createResultReg(RC);
2009 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2010
2011 if (isInt<16>(Imm))
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::LI : PPC::LI8), ResultReg)
2014 .addImm(Imm);
2015 else if (Lo) {
2016 // Both Lo and Hi have nonzero bits.
2017 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002018 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002019 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2020 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002021 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002022 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2023 .addReg(TmpReg).addImm(Lo);
2024 } else
2025 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002026 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002027 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2028 .addImm(Hi);
2029
2030 return ResultReg;
2031}
2032
2033// Materialize a 64-bit integer constant into a register, and return
2034// the register number (or zero if we failed to handle it).
2035unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2036 const TargetRegisterClass *RC) {
2037 unsigned Remainder = 0;
2038 unsigned Shift = 0;
2039
2040 // If the value doesn't fit in 32 bits, see if we can shift it
2041 // so that it fits in 32 bits.
2042 if (!isInt<32>(Imm)) {
2043 Shift = countTrailingZeros<uint64_t>(Imm);
2044 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2045
2046 if (isInt<32>(ImmSh))
2047 Imm = ImmSh;
2048 else {
2049 Remainder = Imm;
2050 Shift = 32;
2051 Imm >>= 32;
2052 }
2053 }
2054
2055 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2056 // (if not shifted).
2057 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2058 if (!Shift)
2059 return TmpReg1;
2060
2061 // If upper 32 bits were not zero, we've built them and need to shift
2062 // them into place.
2063 unsigned TmpReg2;
2064 if (Imm) {
2065 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002067 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2068 } else
2069 TmpReg2 = TmpReg1;
2070
2071 unsigned TmpReg3, Hi, Lo;
2072 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2073 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002074 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002075 TmpReg3).addReg(TmpReg2).addImm(Hi);
2076 } else
2077 TmpReg3 = TmpReg2;
2078
2079 if ((Lo = Remainder & 0xFFFF)) {
2080 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002082 ResultReg).addReg(TmpReg3).addImm(Lo);
2083 return ResultReg;
2084 }
2085
2086 return TmpReg3;
2087}
2088
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002089// Materialize an integer constant into a register, and return
2090// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002091unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2092 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002093 // If we're using CR bit registers for i1 values, handle that as a special
2094 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002095 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002096 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2097 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2098 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2099 return ImmReg;
2100 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002101
Eric Christopher80ba58a2016-01-29 07:19:49 +00002102 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2103 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002104 return 0;
2105
Eric Christopher80ba58a2016-01-29 07:19:49 +00002106 const TargetRegisterClass *RC =
2107 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002108 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002109
2110 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002111 // Since LI will sign extend the constant we need to make sure that for
2112 // our zeroext constants that the sign extended constant fits into 16-bits -
2113 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002114 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002115 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2116 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002117 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002118 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002119 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002120 }
2121
2122 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002123 if (VT == MVT::i64)
2124 return PPCMaterialize64BitInt(Imm, RC);
2125 else if (VT == MVT::i32)
2126 return PPCMaterialize32BitInt(Imm, RC);
2127
2128 return 0;
2129}
2130
2131// Materialize a constant into a register, and return the register
2132// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002133unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002134 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002135
2136 // Only handle simple types.
2137 if (!CEVT.isSimple()) return 0;
2138 MVT VT = CEVT.getSimpleVT();
2139
2140 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2141 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002142 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2143 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002144 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2145 return PPCMaterializeInt(CI, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002146
2147 return 0;
2148}
2149
2150// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002151// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002152unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002153 // Don't handle dynamic allocas.
2154 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2155
2156 MVT VT;
2157 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2158
2159 DenseMap<const AllocaInst*, int>::iterator SI =
2160 FuncInfo.StaticAllocaMap.find(AI);
2161
2162 if (SI != FuncInfo.StaticAllocaMap.end()) {
2163 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002164 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002165 ResultReg).addFrameIndex(SI->second).addImm(0);
2166 return ResultReg;
2167 }
2168
2169 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002170}
2171
Bill Schmidtccecf262013-08-30 02:29:45 +00002172// Fold loads into extends when possible.
2173// FIXME: We can have multiple redundant extend/trunc instructions
2174// following a load. The folding only picks up one. Extend this
2175// to check subsequent instructions for the same pattern and remove
2176// them. Thus ResultReg should be the def reg for the last redundant
2177// instruction in a chain, and all intervening instructions can be
2178// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2179// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002180bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2181 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002182 // Verify we have a legal type before going any further.
2183 MVT VT;
2184 if (!isLoadTypeLegal(LI->getType(), VT))
2185 return false;
2186
2187 // Combine load followed by zero- or sign-extend.
2188 bool IsZExt = false;
2189 switch(MI->getOpcode()) {
2190 default:
2191 return false;
2192
2193 case PPC::RLDICL:
2194 case PPC::RLDICL_32_64: {
2195 IsZExt = true;
2196 unsigned MB = MI->getOperand(3).getImm();
2197 if ((VT == MVT::i8 && MB <= 56) ||
2198 (VT == MVT::i16 && MB <= 48) ||
2199 (VT == MVT::i32 && MB <= 32))
2200 break;
2201 return false;
2202 }
2203
2204 case PPC::RLWINM:
2205 case PPC::RLWINM8: {
2206 IsZExt = true;
2207 unsigned MB = MI->getOperand(3).getImm();
2208 if ((VT == MVT::i8 && MB <= 24) ||
2209 (VT == MVT::i16 && MB <= 16))
2210 break;
2211 return false;
2212 }
2213
2214 case PPC::EXTSB:
2215 case PPC::EXTSB8:
2216 case PPC::EXTSB8_32_64:
2217 /* There is no sign-extending load-byte instruction. */
2218 return false;
2219
2220 case PPC::EXTSH:
2221 case PPC::EXTSH8:
2222 case PPC::EXTSH8_32_64: {
2223 if (VT != MVT::i16 && VT != MVT::i8)
2224 return false;
2225 break;
2226 }
2227
2228 case PPC::EXTSW:
2229 case PPC::EXTSW_32_64: {
2230 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2231 return false;
2232 break;
2233 }
2234 }
2235
2236 // See if we can handle this address.
2237 Address Addr;
2238 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2239 return false;
2240
2241 unsigned ResultReg = MI->getOperand(0).getReg();
2242
Craig Topper062a2ba2014-04-25 05:30:21 +00002243 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002244 return false;
2245
2246 MI->eraseFromParent();
2247 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002248}
2249
2250// Attempt to lower call arguments in a faster way than done by
2251// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002252bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002253 // Defer to normal argument lowering for now. It's reasonably
2254 // efficient. Consider doing something like ARM to handle the
2255 // case where all args fit in registers, no varargs, no float
2256 // or vector args.
2257 return false;
2258}
2259
Bill Schmidt03008132013-08-25 22:33:42 +00002260// Handle materializing integer constants into a register. This is not
2261// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002262unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002263
2264 if (Opc != ISD::Constant)
2265 return 0;
2266
Hal Finkel940ab932014-02-28 00:27:01 +00002267 // If we're using CR bit registers for i1 values, handle that as a special
2268 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002269 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002270 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2272 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2273 return ImmReg;
2274 }
2275
Bill Schmidt03008132013-08-25 22:33:42 +00002276 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2277 VT != MVT::i8 && VT != MVT::i1)
2278 return 0;
2279
2280 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2281 &PPC::GPRCRegClass);
2282 if (VT == MVT::i64)
2283 return PPCMaterialize64BitInt(Imm, RC);
2284 else
2285 return PPCMaterialize32BitInt(Imm, RC);
2286}
2287
Bill Schmidtccecf262013-08-30 02:29:45 +00002288// Override for ADDI and ADDI8 to set the correct register class
2289// on RHS operand 0. The automatic infrastructure naively assumes
2290// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2291// for these cases. At the moment, none of the other automatically
2292// generated RI instructions require special treatment. However, once
2293// SelectSelect is implemented, "isel" requires similar handling.
2294//
2295// Also be conservative about the output register class. Avoid
2296// assigning R0 or X0 to the output register for GPRC and G8RC
2297// register classes, as any such result could be used in ADDI, etc.,
2298// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002299unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002300 const TargetRegisterClass *RC,
2301 unsigned Op0, bool Op0IsKill,
2302 uint64_t Imm) {
2303 if (MachineInstOpcode == PPC::ADDI)
2304 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2305 else if (MachineInstOpcode == PPC::ADDI8)
2306 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2307
2308 const TargetRegisterClass *UseRC =
2309 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2310 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2311
Juergen Ributzka88e32512014-09-03 20:56:59 +00002312 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002313 Op0, Op0IsKill, Imm);
2314}
2315
2316// Override for instructions with one register operand to avoid use of
2317// R0/X0. The automatic infrastructure isn't aware of the context so
2318// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002319unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002320 const TargetRegisterClass* RC,
2321 unsigned Op0, bool Op0IsKill) {
2322 const TargetRegisterClass *UseRC =
2323 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2324 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2325
Juergen Ributzka88e32512014-09-03 20:56:59 +00002326 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002327}
2328
2329// Override for instructions with two register operands to avoid use
2330// of R0/X0. The automatic infrastructure isn't aware of the context
2331// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002332unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002333 const TargetRegisterClass* RC,
2334 unsigned Op0, bool Op0IsKill,
2335 unsigned Op1, bool Op1IsKill) {
2336 const TargetRegisterClass *UseRC =
2337 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2338 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2339
Juergen Ributzka88e32512014-09-03 20:56:59 +00002340 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002341 Op1, Op1IsKill);
2342}
2343
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002344namespace llvm {
2345 // Create the fast instruction selector for PowerPC64 ELF.
2346 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2347 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002348 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002349 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002350 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002351 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002352 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002353 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002354}