blob: 842d68bf1c93cca57d47c10cf536effe867a6749 [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"
Strahinja Petrovice682b802016-05-09 12:27:39 +000019#include "PPCCCState.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000020#include "PPCISelLowering.h"
Hal Finkele6698d52015-02-01 15:03:28 +000021#include "PPCMachineFunctionInfo.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000022#include "PPCSubtarget.h"
23#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000024#include "llvm/ADT/Optional.h"
25#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/FastISel.h"
27#include "llvm/CodeGen/FunctionLoweringInfo.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000033#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000034#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000039#include "llvm/Target/TargetLowering.h"
40#include "llvm/Target/TargetMachine.h"
41
Bill Schmidteb8d6f72013-08-31 02:33:40 +000042//===----------------------------------------------------------------------===//
43//
44// TBD:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +000045// fastLowerArguments: Handle simple cases.
Bill Schmidteb8d6f72013-08-31 02:33:40 +000046// PPCMaterializeGV: Handle TLS.
47// SelectCall: Handle function pointers.
48// SelectCall: Handle multi-register return values.
49// SelectCall: Optimize away nops for local calls.
50// processCallArgs: Handle bit-converted arguments.
51// finishCall: Handle multi-register return values.
52// PPCComputeAddress: Handle parameter references as FrameIndex's.
53// PPCEmitCmp: Handle immediate as operand 1.
54// SelectCall: Handle small byval arguments.
55// SelectIntrinsicCall: Implement.
56// SelectSelect: Implement.
57// Consider factoring isTypeLegal into the base class.
58// Implement switches and jump tables.
59//
60//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000061using namespace llvm;
62
Chandler Carruth84e68b22014-04-22 02:41:26 +000063#define DEBUG_TYPE "ppcfastisel"
64
Bill Schmidt0cf702f2013-07-30 00:50:39 +000065namespace {
66
67typedef struct Address {
68 enum {
69 RegBase,
70 FrameIndexBase
71 } BaseType;
72
73 union {
74 unsigned Reg;
75 int FI;
76 } Base;
77
Bill Schmidtccecf262013-08-30 02:29:45 +000078 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000079
80 // Innocuous defaults for our address.
81 Address()
82 : BaseType(RegBase), Offset(0) {
83 Base.Reg = 0;
84 }
85} Address;
86
Craig Topper26696312014-03-18 07:27:13 +000087class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000088
89 const TargetMachine &TM;
Eric Christopher85806142015-01-30 02:11:24 +000090 const PPCSubtarget *PPCSubTarget;
Hal Finkele6698d52015-02-01 15:03:28 +000091 PPCFunctionInfo *PPCFuncInfo;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000092 const TargetInstrInfo &TII;
93 const TargetLowering &TLI;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000094 LLVMContext *Context;
95
96 public:
97 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
98 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000099 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
Eric Christophercccae792015-01-30 22:02:31 +0000100 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
Hal Finkele6698d52015-02-01 15:03:28 +0000101 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
Eric Christopher85806142015-01-30 02:11:24 +0000102 TII(*PPCSubTarget->getInstrInfo()),
103 TLI(*PPCSubTarget->getTargetLowering()),
Eric Christopherd9134482014-08-04 21:25:23 +0000104 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000105
106 // Backend specific FastISel code.
107 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000108 bool fastSelectInstruction(const Instruction *I) override;
109 unsigned fastMaterializeConstant(const Constant *C) override;
110 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000111 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
112 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000113 bool fastLowerArguments() override;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000114 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
115 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000116 const TargetRegisterClass *RC,
117 unsigned Op0, bool Op0IsKill,
118 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000119 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000120 const TargetRegisterClass *RC,
121 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000122 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000126
Hal Finkel934361a2015-01-14 01:07:51 +0000127 bool fastLowerCall(CallLoweringInfo &CLI) override;
128
Bill Schmidt03008132013-08-25 22:33:42 +0000129 // Instruction selection routines.
130 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000131 bool SelectLoad(const Instruction *I);
132 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000133 bool SelectBranch(const Instruction *I);
134 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000135 bool SelectFPExt(const Instruction *I);
136 bool SelectFPTrunc(const Instruction *I);
137 bool SelectIToFP(const Instruction *I, bool IsSigned);
138 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000139 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000140 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000141 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000142 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000143
144 // Utility routines.
145 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000146 bool isTypeLegal(Type *Ty, MVT &VT);
147 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000148 bool isValueAvailable(const Value *V) const;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000149 bool isVSFRCRegister(unsigned Register) const {
150 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
151 }
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000152 bool isVSSRCRegister(unsigned Register) const {
153 return MRI.getRegClass(Register)->getID() == PPC::VSSRCRegClassID;
154 }
Bill Schmidt03008132013-08-25 22:33:42 +0000155 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
156 bool isZExt, unsigned DestReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000157 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
158 const TargetRegisterClass *RC, bool IsZExt = true,
159 unsigned FP64LoadOpc = PPC::LFD);
160 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
161 bool PPCComputeAddress(const Value *Obj, Address &Addr);
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000162 void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000163 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000164 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
165 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000166 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000167 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000168 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
169 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000170 unsigned PPCMaterialize32BitInt(int64_t Imm,
171 const TargetRegisterClass *RC);
172 unsigned PPCMaterialize64BitInt(int64_t Imm,
173 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000174 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
175 unsigned SrcReg, bool IsSigned);
176 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000177
Bill Schmidtd89f6782013-08-26 19:42:51 +0000178 // Call handling routines.
179 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000180 bool processCallArgs(SmallVectorImpl<Value*> &Args,
181 SmallVectorImpl<unsigned> &ArgRegs,
182 SmallVectorImpl<MVT> &ArgVTs,
183 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
184 SmallVectorImpl<unsigned> &RegArgs,
185 CallingConv::ID CC,
186 unsigned &NumBytes,
187 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000188 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000189
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
Bill Schmidt03008132013-08-25 22:33:42 +0000199static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
200 switch (Pred) {
201 // These are not representable with any single compare.
202 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000203 case CmpInst::FCMP_TRUE:
204 // Major concern about the following 6 cases is NaN result. The comparison
205 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
206 // only one of which will be set. The result is generated by fcmpu
207 // instruction. However, bc instruction only inspects one of the first 3
208 // bits, so when un is set, bc instruction may jump to to an undesired
209 // place.
210 //
211 // More specifically, if we expect an unordered comparison and un is set, we
212 // expect to always go to true branch; in such case UEQ, UGT and ULT still
213 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
214 // since they are tested by inspecting !eq, !lt, !gt, respectively.
215 //
216 // Similarly, for ordered comparison, when un is set, we always expect the
217 // result to be false. In such case OGT, OLT and OEQ is good, since they are
218 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
219 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000220 case CmpInst::FCMP_UEQ:
221 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000222 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000223 case CmpInst::FCMP_OGE:
224 case CmpInst::FCMP_OLE:
225 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000226 default:
227 return Optional<PPC::Predicate>();
228
229 case CmpInst::FCMP_OEQ:
230 case CmpInst::ICMP_EQ:
231 return PPC::PRED_EQ;
232
233 case CmpInst::FCMP_OGT:
234 case CmpInst::ICMP_UGT:
235 case CmpInst::ICMP_SGT:
236 return PPC::PRED_GT;
237
Tim Shen5cdf7502016-03-17 22:27:58 +0000238 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000239 case CmpInst::ICMP_UGE:
240 case CmpInst::ICMP_SGE:
241 return PPC::PRED_GE;
242
243 case CmpInst::FCMP_OLT:
244 case CmpInst::ICMP_ULT:
245 case CmpInst::ICMP_SLT:
246 return PPC::PRED_LT;
247
Tim Shen5cdf7502016-03-17 22:27:58 +0000248 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000249 case CmpInst::ICMP_ULE:
250 case CmpInst::ICMP_SLE:
251 return PPC::PRED_LE;
252
Tim Shen5cdf7502016-03-17 22:27:58 +0000253 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000254 case CmpInst::ICMP_NE:
255 return PPC::PRED_NE;
256
257 case CmpInst::FCMP_ORD:
258 return PPC::PRED_NU;
259
260 case CmpInst::FCMP_UNO:
261 return PPC::PRED_UN;
262 }
263}
264
Bill Schmidtccecf262013-08-30 02:29:45 +0000265// Determine whether the type Ty is simple enough to be handled by
266// fast-isel, and return its equivalent machine type in VT.
267// FIXME: Copied directly from ARM -- factor into base class?
268bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000269 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000270
271 // Only handle simple types.
272 if (Evt == MVT::Other || !Evt.isSimple()) return false;
273 VT = Evt.getSimpleVT();
274
275 // Handle all legal types, i.e. a register that will directly hold this
276 // value.
277 return TLI.isTypeLegal(VT);
278}
279
280// Determine whether the type Ty is simple enough to be handled by
281// fast-isel as a load target, and return its equivalent machine type in VT.
282bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
283 if (isTypeLegal(Ty, VT)) return true;
284
285 // If this is a type than can be sign or zero-extended to a basic operation
286 // go ahead and accept it now.
287 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
288 return true;
289 }
290
291 return false;
292}
293
Hal Finkel5f2a1372015-05-23 12:18:10 +0000294bool PPCFastISel::isValueAvailable(const Value *V) const {
295 if (!isa<Instruction>(V))
296 return true;
297
298 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000299 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000300}
301
Bill Schmidtccecf262013-08-30 02:29:45 +0000302// Given a value Obj, create an Address object Addr that represents its
303// address. Return false if we can't handle it.
304bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000305 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000306 unsigned Opcode = Instruction::UserOp1;
307 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
308 // Don't walk into other basic blocks unless the object is an alloca from
309 // another block, otherwise it may not have a virtual register assigned.
310 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
311 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
312 Opcode = I->getOpcode();
313 U = I;
314 }
315 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
316 Opcode = C->getOpcode();
317 U = C;
318 }
319
320 switch (Opcode) {
321 default:
322 break;
323 case Instruction::BitCast:
324 // Look through bitcasts.
325 return PPCComputeAddress(U->getOperand(0), Addr);
326 case Instruction::IntToPtr:
327 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000328 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
329 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000330 return PPCComputeAddress(U->getOperand(0), Addr);
331 break;
332 case Instruction::PtrToInt:
333 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000334 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000335 return PPCComputeAddress(U->getOperand(0), Addr);
336 break;
337 case Instruction::GetElementPtr: {
338 Address SavedAddr = Addr;
339 long TmpOffset = Addr.Offset;
340
341 // Iterate through the GEP folding the constants into offsets where
342 // we can.
343 gep_type_iterator GTI = gep_type_begin(U);
344 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
345 II != IE; ++II, ++GTI) {
346 const Value *Op = *II;
347 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000348 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000349 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
350 TmpOffset += SL->getElementOffset(Idx);
351 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000352 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000353 for (;;) {
354 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
355 // Constant-offset addressing.
356 TmpOffset += CI->getSExtValue() * S;
357 break;
358 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000359 if (canFoldAddIntoGEP(U, Op)) {
360 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000361 ConstantInt *CI =
362 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
363 TmpOffset += CI->getSExtValue() * S;
364 // Iterate on the other operand.
365 Op = cast<AddOperator>(Op)->getOperand(0);
366 continue;
367 }
368 // Unsupported
369 goto unsupported_gep;
370 }
371 }
372 }
373
374 // Try to grab the base operand now.
375 Addr.Offset = TmpOffset;
376 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
377
378 // We failed, restore everything and try the other options.
379 Addr = SavedAddr;
380
381 unsupported_gep:
382 break;
383 }
384 case Instruction::Alloca: {
385 const AllocaInst *AI = cast<AllocaInst>(Obj);
386 DenseMap<const AllocaInst*, int>::iterator SI =
387 FuncInfo.StaticAllocaMap.find(AI);
388 if (SI != FuncInfo.StaticAllocaMap.end()) {
389 Addr.BaseType = Address::FrameIndexBase;
390 Addr.Base.FI = SI->second;
391 return true;
392 }
393 break;
394 }
395 }
396
397 // FIXME: References to parameters fall through to the behavior
398 // below. They should be able to reference a frame index since
399 // they are stored to the stack, so we can get "ld rx, offset(r1)"
400 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
401 // just contain the parameter. Try to handle this with a FI.
402
403 // Try to get this in a register if nothing else has worked.
404 if (Addr.Base.Reg == 0)
405 Addr.Base.Reg = getRegForValue(Obj);
406
407 // Prevent assignment of base register to X0, which is inappropriate
408 // for loads and stores alike.
409 if (Addr.Base.Reg != 0)
410 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
411
412 return Addr.Base.Reg != 0;
413}
414
415// Fix up some addresses that can't be used directly. For example, if
416// an offset won't fit in an instruction field, we may need to move it
417// into an index register.
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000418void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000419 unsigned &IndexReg) {
420
421 // Check whether the offset fits in the instruction field.
422 if (!isInt<16>(Addr.Offset))
423 UseOffset = false;
424
425 // If this is a stack pointer and the offset needs to be simplified then
426 // put the alloca address into a register, set the base type back to
427 // register and continue. This should almost never happen.
428 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
429 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000431 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
432 Addr.Base.Reg = ResultReg;
433 Addr.BaseType = Address::RegBase;
434 }
435
436 if (!UseOffset) {
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000437 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
Bill Schmidtccecf262013-08-30 02:29:45 +0000438 const ConstantInt *Offset =
439 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
440 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
441 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
442 }
443}
444
445// Emit a load instruction if possible, returning true if we succeeded,
446// otherwise false. See commentary below for how the register class of
447// the load is determined.
448bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
449 const TargetRegisterClass *RC,
450 bool IsZExt, unsigned FP64LoadOpc) {
451 unsigned Opc;
452 bool UseOffset = true;
453
454 // If ResultReg is given, it determines the register class of the load.
455 // Otherwise, RC is the register class to use. If the result of the
456 // load isn't anticipated in this block, both may be zero, in which
457 // case we must make a conservative guess. In particular, don't assign
458 // R0 or X0 to the result register, as the result may be used in a load,
459 // store, add-immediate, or isel that won't permit this. (Though
460 // perhaps the spill and reload of live-exit values would handle this?)
461 const TargetRegisterClass *UseRC =
462 (ResultReg ? MRI.getRegClass(ResultReg) :
463 (RC ? RC :
464 (VT == MVT::f64 ? &PPC::F8RCRegClass :
465 (VT == MVT::f32 ? &PPC::F4RCRegClass :
466 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
467 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
468
469 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
470
471 switch (VT.SimpleTy) {
472 default: // e.g., vector types not handled
473 return false;
474 case MVT::i8:
475 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
476 break;
477 case MVT::i16:
478 Opc = (IsZExt ?
479 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) :
480 (Is32BitInt ? PPC::LHA : PPC::LHA8));
481 break;
482 case MVT::i32:
483 Opc = (IsZExt ?
484 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) :
485 (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
486 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
487 UseOffset = false;
488 break;
489 case MVT::i64:
490 Opc = PPC::LD;
491 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
492 "64-bit load with 32-bit target??");
493 UseOffset = ((Addr.Offset & 3) == 0);
494 break;
495 case MVT::f32:
496 Opc = PPC::LFS;
497 break;
498 case MVT::f64:
499 Opc = FP64LoadOpc;
500 break;
501 }
502
503 // If necessary, materialize the offset into a register and use
504 // the indexed form. Also handle stack pointers with special needs.
505 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000506 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000507
508 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
509 // be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000510 bool IsVSSRC = (ResultReg != 0) && isVSSRCRegister(ResultReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000511 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000512 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
513 bool Is64VSXLoad = IsVSSRC && Opc == PPC::LFD;
514 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000515 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
516 (Addr.Offset == 0)) {
517 UseOffset = false;
518 }
519
Bill Schmidtccecf262013-08-30 02:29:45 +0000520 if (ResultReg == 0)
521 ResultReg = createResultReg(UseRC);
522
523 // Note: If we still have a frame index here, we know the offset is
524 // in range, as otherwise PPCSimplifyAddress would have converted it
525 // into a RegBase.
526 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000527 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000528 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000529
Alex Lorenze40c8a22015-08-11 23:09:45 +0000530 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
531 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
532 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000533 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
534 MFI.getObjectAlignment(Addr.Base.FI));
535
Rafael Espindolaea09c592014-02-18 22:05:46 +0000536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000537 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
538
539 // Base reg with offset in range.
540 } else if (UseOffset) {
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
Rafael Espindolaea09c592014-02-18 22:05:46 +0000544 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000545 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
546
547 // Indexed form.
548 } else {
549 // Get the RR opcode corresponding to the RI one. FIXME: It would be
550 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
551 // is hard to get at.
552 switch (Opc) {
553 default: llvm_unreachable("Unexpected opcode!");
554 case PPC::LBZ: Opc = PPC::LBZX; break;
555 case PPC::LBZ8: Opc = PPC::LBZX8; break;
556 case PPC::LHZ: Opc = PPC::LHZX; break;
557 case PPC::LHZ8: Opc = PPC::LHZX8; break;
558 case PPC::LHA: Opc = PPC::LHAX; break;
559 case PPC::LHA8: Opc = PPC::LHAX8; break;
560 case PPC::LWZ: Opc = PPC::LWZX; break;
561 case PPC::LWZ8: Opc = PPC::LWZX8; break;
562 case PPC::LWA: Opc = PPC::LWAX; break;
563 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
564 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000565 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000566 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000567 }
Rafael Espindolaea09c592014-02-18 22:05:46 +0000568 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000569 .addReg(Addr.Base.Reg).addReg(IndexReg);
570 }
571
572 return true;
573}
574
575// Attempt to fast-select a load instruction.
576bool PPCFastISel::SelectLoad(const Instruction *I) {
577 // FIXME: No atomic loads are supported.
578 if (cast<LoadInst>(I)->isAtomic())
579 return false;
580
581 // Verify we have a legal type before going any further.
582 MVT VT;
583 if (!isLoadTypeLegal(I->getType(), VT))
584 return false;
585
586 // See if we can handle this address.
587 Address Addr;
588 if (!PPCComputeAddress(I->getOperand(0), Addr))
589 return false;
590
591 // Look at the currently assigned register for this instruction
592 // to determine the required register class. This is necessary
593 // to constrain RA from using R0/X0 when this is not legal.
594 unsigned AssignedReg = FuncInfo.ValueMap[I];
595 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000596 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000597
598 unsigned ResultReg = 0;
599 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
600 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000601 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000602 return true;
603}
604
605// Emit a store instruction to store SrcReg at Addr.
606bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
607 assert(SrcReg && "Nothing to store!");
608 unsigned Opc;
609 bool UseOffset = true;
610
611 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
612 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
613
614 switch (VT.SimpleTy) {
615 default: // e.g., vector types not handled
616 return false;
617 case MVT::i8:
618 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
619 break;
620 case MVT::i16:
621 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
622 break;
623 case MVT::i32:
624 assert(Is32BitInt && "Not GPRC for i32??");
625 Opc = PPC::STW;
626 break;
627 case MVT::i64:
628 Opc = PPC::STD;
629 UseOffset = ((Addr.Offset & 3) == 0);
630 break;
631 case MVT::f32:
632 Opc = PPC::STFS;
633 break;
634 case MVT::f64:
635 Opc = PPC::STFD;
636 break;
637 }
638
639 // If necessary, materialize the offset into a register and use
640 // the indexed form. Also handle stack pointers with special needs.
641 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000642 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000643
Bill Seurer8c728ae2014-12-05 20:15:56 +0000644 // If this is a potential VSX store with an offset of 0, a VSX indexed store
645 // can be used.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000646 bool IsVSSRC = isVSSRCRegister(SrcReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000647 bool IsVSFRC = isVSFRCRegister(SrcReg);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000648 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
649 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
650 if ((Is32VSXStore || Is64VSXStore) &&
651 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000652 (Addr.Offset == 0)) {
653 UseOffset = false;
654 }
655
Bill Schmidtccecf262013-08-30 02:29:45 +0000656 // Note: If we still have a frame index here, we know the offset is
657 // in range, as otherwise PPCSimplifyAddress would have converted it
658 // into a RegBase.
659 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000660 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000661 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000662
Alex Lorenze40c8a22015-08-11 23:09:45 +0000663 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
664 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
665 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000666 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
667 MFI.getObjectAlignment(Addr.Base.FI));
668
Rafael Espindolaea09c592014-02-18 22:05:46 +0000669 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
670 .addReg(SrcReg)
671 .addImm(Addr.Offset)
672 .addFrameIndex(Addr.Base.FI)
673 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000674
675 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000676 } else if (UseOffset) {
677 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000678 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000679
Rafael Espindolaea09c592014-02-18 22:05:46 +0000680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000681 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
682
683 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000684 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000685 // Get the RR opcode corresponding to the RI one. FIXME: It would be
686 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
687 // is hard to get at.
688 switch (Opc) {
689 default: llvm_unreachable("Unexpected opcode!");
690 case PPC::STB: Opc = PPC::STBX; break;
691 case PPC::STH : Opc = PPC::STHX; break;
692 case PPC::STW : Opc = PPC::STWX; break;
693 case PPC::STB8: Opc = PPC::STBX8; break;
694 case PPC::STH8: Opc = PPC::STHX8; break;
695 case PPC::STW8: Opc = PPC::STWX8; break;
696 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000697 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000698 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000699 }
Samuel Antaof6815602015-03-17 15:00:57 +0000700
701 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
702 .addReg(SrcReg);
703
704 // If we have an index register defined we use it in the store inst,
705 // otherwise we use X0 as base as it makes the vector instructions to
706 // use zero in the computation of the effective address regardless the
707 // content of the register.
708 if (IndexReg)
709 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
710 else
711 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000712 }
713
714 return true;
715}
716
717// Attempt to fast-select a store instruction.
718bool PPCFastISel::SelectStore(const Instruction *I) {
719 Value *Op0 = I->getOperand(0);
720 unsigned SrcReg = 0;
721
722 // FIXME: No atomics loads are supported.
723 if (cast<StoreInst>(I)->isAtomic())
724 return false;
725
726 // Verify we have a legal type before going any further.
727 MVT VT;
728 if (!isLoadTypeLegal(Op0->getType(), VT))
729 return false;
730
731 // Get the value to be stored into a register.
732 SrcReg = getRegForValue(Op0);
733 if (SrcReg == 0)
734 return false;
735
736 // See if we can handle this address.
737 Address Addr;
738 if (!PPCComputeAddress(I->getOperand(1), Addr))
739 return false;
740
741 if (!PPCEmitStore(VT, SrcReg, Addr))
742 return false;
743
744 return true;
745}
746
Bill Schmidt03008132013-08-25 22:33:42 +0000747// Attempt to fast-select a branch instruction.
748bool PPCFastISel::SelectBranch(const Instruction *I) {
749 const BranchInst *BI = cast<BranchInst>(I);
750 MachineBasicBlock *BrBB = FuncInfo.MBB;
751 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
752 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
753
754 // For now, just try the simplest case where it's fed by a compare.
755 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000756 if (isValueAvailable(CI)) {
757 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
758 if (!OptPPCPred)
759 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000760
Hal Finkel5f2a1372015-05-23 12:18:10 +0000761 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000762
Hal Finkel5f2a1372015-05-23 12:18:10 +0000763 // Take advantage of fall-through opportunities.
764 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
765 std::swap(TBB, FBB);
766 PPCPred = PPC::InvertPredicate(PPCPred);
767 }
768
769 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
770
771 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
772 CondReg))
773 return false;
774
775 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
776 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000777 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000778 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000779 }
Bill Schmidt03008132013-08-25 22:33:42 +0000780 } else if (const ConstantInt *CI =
781 dyn_cast<ConstantInt>(BI->getCondition())) {
782 uint64_t Imm = CI->getZExtValue();
783 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000784 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000785 return true;
786 }
787
788 // FIXME: ARM looks for a case where the block containing the compare
789 // has been split from the block containing the branch. If this happens,
790 // there is a vreg available containing the result of the compare. I'm
791 // not sure we can do much, as we've lost the predicate information with
792 // the compare instruction -- we have a 4-bit CR but don't know which bit
793 // to test here.
794 return false;
795}
796
797// Attempt to emit a compare of the two source values. Signed and unsigned
798// comparisons are supported. Return false if we can't handle it.
799bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
800 bool IsZExt, unsigned DestReg) {
801 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000802 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000803 if (!SrcEVT.isSimple())
804 return false;
805 MVT SrcVT = SrcEVT.getSimpleVT();
806
Eric Christopher1b8e7632014-05-22 01:07:24 +0000807 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000808 return false;
809
Bill Schmidt03008132013-08-25 22:33:42 +0000810 // See if operand 2 is an immediate encodeable in the compare.
811 // FIXME: Operands are not in canonical order at -O0, so an immediate
812 // operand in position 1 is a lost opportunity for now. We are
813 // similar to ARM in this regard.
814 long Imm = 0;
815 bool UseImm = false;
816
817 // Only 16-bit integer constants can be represented in compares for
818 // PowerPC. Others will be materialized into a register.
819 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
820 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
821 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
822 const APInt &CIVal = ConstInt->getValue();
823 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
824 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
825 UseImm = true;
826 }
827 }
828
829 unsigned CmpOpc;
830 bool NeedsExt = false;
831 switch (SrcVT.SimpleTy) {
832 default: return false;
833 case MVT::f32:
834 CmpOpc = PPC::FCMPUS;
835 break;
836 case MVT::f64:
837 CmpOpc = PPC::FCMPUD;
838 break;
839 case MVT::i1:
840 case MVT::i8:
841 case MVT::i16:
842 NeedsExt = true;
843 // Intentional fall-through.
844 case MVT::i32:
845 if (!UseImm)
846 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
847 else
848 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
849 break;
850 case MVT::i64:
851 if (!UseImm)
852 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
853 else
854 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
855 break;
856 }
857
858 unsigned SrcReg1 = getRegForValue(SrcValue1);
859 if (SrcReg1 == 0)
860 return false;
861
862 unsigned SrcReg2 = 0;
863 if (!UseImm) {
864 SrcReg2 = getRegForValue(SrcValue2);
865 if (SrcReg2 == 0)
866 return false;
867 }
868
869 if (NeedsExt) {
870 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
871 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
872 return false;
873 SrcReg1 = ExtReg;
874
875 if (!UseImm) {
876 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
877 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
878 return false;
879 SrcReg2 = ExtReg;
880 }
881 }
882
883 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000885 .addReg(SrcReg1).addReg(SrcReg2);
886 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000887 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000888 .addReg(SrcReg1).addImm(Imm);
889
890 return true;
891}
892
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000893// Attempt to fast-select a floating-point extend instruction.
894bool PPCFastISel::SelectFPExt(const Instruction *I) {
895 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000896 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
897 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000898
899 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
900 return false;
901
902 unsigned SrcReg = getRegForValue(Src);
903 if (!SrcReg)
904 return false;
905
906 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000907 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000908 return true;
909}
910
911// Attempt to fast-select a floating-point truncate instruction.
912bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
913 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000914 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
915 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000916
917 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
918 return false;
919
920 unsigned SrcReg = getRegForValue(Src);
921 if (!SrcReg)
922 return false;
923
924 // Round the result to single precision.
925 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000927 .addReg(SrcReg);
928
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000929 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000930 return true;
931}
932
933// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000934// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000935// those should be used instead of moving via a stack slot when the
936// subtarget permits.
937// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
938// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
939// case to 8 bytes which produces tighter code but wastes stack space.
940unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
941 bool IsSigned) {
942
943 // If necessary, extend 32-bit int to 64-bit.
944 if (SrcVT == MVT::i32) {
945 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
946 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
947 return 0;
948 SrcReg = TmpReg;
949 }
950
951 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
952 Address Addr;
953 Addr.BaseType = Address::FrameIndexBase;
954 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
955
956 // Store the value from the GPR.
957 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
958 return 0;
959
960 // Load the integer value into an FPR. The kind of load used depends
961 // on a number of conditions.
962 unsigned LoadOpc = PPC::LFD;
963
964 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000965 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000966 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000967 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000968 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000969 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000970 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000971 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000972 }
973
974 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
975 unsigned ResultReg = 0;
976 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
977 return 0;
978
979 return ResultReg;
980}
981
982// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +0000983// FIXME: Once fast-isel has better support for VSX, conversions using
984// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000985bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
986 MVT DstVT;
987 Type *DstTy = I->getType();
988 if (!isTypeLegal(DstTy, DstVT))
989 return false;
990
991 if (DstVT != MVT::f32 && DstVT != MVT::f64)
992 return false;
993
994 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000995 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000996 if (!SrcEVT.isSimple())
997 return false;
998
999 MVT SrcVT = SrcEVT.getSimpleVT();
1000
1001 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1002 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1003 return false;
1004
1005 unsigned SrcReg = getRegForValue(Src);
1006 if (SrcReg == 0)
1007 return false;
1008
1009 // We can only lower an unsigned convert if we have the newer
1010 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001011 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001012 return false;
1013
1014 // FIXME: For now we require the newer floating-point conversion operations
1015 // (which are present only on P7 and A2 server models) when converting
1016 // to single-precision float. Otherwise we have to generate a lot of
1017 // fiddly code to avoid double rounding. If necessary, the fiddly code
1018 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001019 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001020 return false;
1021
1022 // Extend the input if necessary.
1023 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1024 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1025 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1026 return false;
1027 SrcVT = MVT::i64;
1028 SrcReg = TmpReg;
1029 }
1030
1031 // Move the integer value to an FPR.
1032 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1033 if (FPReg == 0)
1034 return false;
1035
1036 // Determine the opcode for the conversion.
1037 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1038 unsigned DestReg = createResultReg(RC);
1039 unsigned Opc;
1040
1041 if (DstVT == MVT::f32)
1042 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1043 else
1044 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1045
1046 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001048 .addReg(FPReg);
1049
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001050 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001051 return true;
1052}
1053
1054// Move the floating-point value in SrcReg into an integer destination
1055// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001056// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001057// those should be used instead of moving via a stack slot when the
1058// subtarget permits.
1059unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1060 unsigned SrcReg, bool IsSigned) {
1061 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1062 // Note that if have STFIWX available, we could use a 4-byte stack
1063 // slot for i32, but this being fast-isel we'll just go with the
1064 // easiest code gen possible.
1065 Address Addr;
1066 Addr.BaseType = Address::FrameIndexBase;
1067 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1068
1069 // Store the value from the FPR.
1070 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1071 return 0;
1072
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001073 // Reload it into a GPR. If we want an i32 on big endian, modify the
1074 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001075 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001076 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001077
1078 // Look at the currently assigned register for this instruction
1079 // to determine the required register class.
1080 unsigned AssignedReg = FuncInfo.ValueMap[I];
1081 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001082 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001083
1084 unsigned ResultReg = 0;
1085 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1086 return 0;
1087
1088 return ResultReg;
1089}
1090
1091// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001092// FIXME: Once fast-isel has better support for VSX, conversions using
1093// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001094bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1095 MVT DstVT, SrcVT;
1096 Type *DstTy = I->getType();
1097 if (!isTypeLegal(DstTy, DstVT))
1098 return false;
1099
1100 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1101 return false;
1102
Bill Schmidt83973ef2014-06-24 20:05:18 +00001103 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1104 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1105 return false;
1106
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001107 Value *Src = I->getOperand(0);
1108 Type *SrcTy = Src->getType();
1109 if (!isTypeLegal(SrcTy, SrcVT))
1110 return false;
1111
1112 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1113 return false;
1114
1115 unsigned SrcReg = getRegForValue(Src);
1116 if (SrcReg == 0)
1117 return false;
1118
1119 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001120 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001121 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1122 if (InRC == &PPC::F4RCRegClass) {
1123 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001124 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001125 TII.get(TargetOpcode::COPY), TmpReg)
1126 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001127 SrcReg = TmpReg;
1128 }
1129
1130 // Determine the opcode for the conversion, which takes place
1131 // entirely within FPRs.
1132 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1133 unsigned Opc;
1134
1135 if (DstVT == MVT::i32)
1136 if (IsSigned)
1137 Opc = PPC::FCTIWZ;
1138 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001139 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001140 else
1141 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1142
1143 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001145 .addReg(SrcReg);
1146
1147 // Now move the integer value from a float register to an integer register.
1148 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1149 if (IntReg == 0)
1150 return false;
1151
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001152 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001153 return true;
1154}
1155
Bill Schmidtccecf262013-08-30 02:29:45 +00001156// Attempt to fast-select a binary integer operation that isn't already
1157// handled automatically.
1158bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001159 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001160
1161 // We can get here in the case when we have a binary operation on a non-legal
1162 // type and the target independent selector doesn't know how to handle it.
1163 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1164 return false;
1165
1166 // Look at the currently assigned register for this instruction
1167 // to determine the required register class. If there is no register,
1168 // make a conservative choice (don't assign R0).
1169 unsigned AssignedReg = FuncInfo.ValueMap[I];
1170 const TargetRegisterClass *RC =
1171 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1172 &PPC::GPRC_and_GPRC_NOR0RegClass);
1173 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1174
1175 unsigned Opc;
1176 switch (ISDOpcode) {
1177 default: return false;
1178 case ISD::ADD:
1179 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1180 break;
1181 case ISD::OR:
1182 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1183 break;
1184 case ISD::SUB:
1185 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1186 break;
1187 }
1188
1189 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1190 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1191 if (SrcReg1 == 0) return false;
1192
1193 // Handle case of small immediate operand.
1194 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1195 const APInt &CIVal = ConstInt->getValue();
1196 int Imm = (int)CIVal.getSExtValue();
1197 bool UseImm = true;
1198 if (isInt<16>(Imm)) {
1199 switch (Opc) {
1200 default:
1201 llvm_unreachable("Missing case!");
1202 case PPC::ADD4:
1203 Opc = PPC::ADDI;
1204 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1205 break;
1206 case PPC::ADD8:
1207 Opc = PPC::ADDI8;
1208 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1209 break;
1210 case PPC::OR:
1211 Opc = PPC::ORI;
1212 break;
1213 case PPC::OR8:
1214 Opc = PPC::ORI8;
1215 break;
1216 case PPC::SUBF:
1217 if (Imm == -32768)
1218 UseImm = false;
1219 else {
1220 Opc = PPC::ADDI;
1221 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1222 Imm = -Imm;
1223 }
1224 break;
1225 case PPC::SUBF8:
1226 if (Imm == -32768)
1227 UseImm = false;
1228 else {
1229 Opc = PPC::ADDI8;
1230 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1231 Imm = -Imm;
1232 }
1233 break;
1234 }
1235
1236 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1238 ResultReg)
1239 .addReg(SrcReg1)
1240 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001241 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001242 return true;
1243 }
1244 }
1245 }
1246
1247 // Reg-reg case.
1248 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1249 if (SrcReg2 == 0) return false;
1250
1251 // Reverse operands for subtract-from.
1252 if (ISDOpcode == ISD::SUB)
1253 std::swap(SrcReg1, SrcReg2);
1254
Rafael Espindolaea09c592014-02-18 22:05:46 +00001255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001256 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001257 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001258 return true;
1259}
1260
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001261// Handle arguments to a call that we're attempting to fast-select.
1262// Return false if the arguments are too complex for us at the moment.
1263bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1264 SmallVectorImpl<unsigned> &ArgRegs,
1265 SmallVectorImpl<MVT> &ArgVTs,
1266 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1267 SmallVectorImpl<unsigned> &RegArgs,
1268 CallingConv::ID CC,
1269 unsigned &NumBytes,
1270 bool IsVarArg) {
1271 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001272 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001273
1274 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001275 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001276 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001277
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001278 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1279
1280 // Bail out if we can't handle any of the arguments.
1281 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1282 CCValAssign &VA = ArgLocs[I];
1283 MVT ArgVT = ArgVTs[VA.getValNo()];
1284
1285 // Skip vector arguments for now, as well as long double and
1286 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001287 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001288 !VA.isRegLoc() || VA.needsCustom())
1289 return false;
1290
1291 // Skip bit-converted arguments for now.
1292 if (VA.getLocInfo() == CCValAssign::BCvt)
1293 return false;
1294 }
1295
1296 // Get a count of how many bytes are to be pushed onto the stack.
1297 NumBytes = CCInfo.getNextStackOffset();
1298
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001299 // The prolog code of the callee may store up to 8 GPR argument registers to
1300 // the stack, allowing va_start to index over them in memory if its varargs.
1301 // Because we cannot tell if this is needed on the caller side, we have to
1302 // conservatively assume that it is needed. As such, make sure we have at
1303 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001304 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001305 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001306
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001307 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001309 TII.get(TII.getCallFrameSetupOpcode()))
1310 .addImm(NumBytes);
1311
1312 // Prepare to assign register arguments. Every argument uses up a
1313 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001314 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001315 unsigned NextGPR = PPC::X3;
1316 unsigned NextFPR = PPC::F1;
1317
1318 // Process arguments.
1319 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1320 CCValAssign &VA = ArgLocs[I];
1321 unsigned Arg = ArgRegs[VA.getValNo()];
1322 MVT ArgVT = ArgVTs[VA.getValNo()];
1323
1324 // Handle argument promotion and bitcasts.
1325 switch (VA.getLocInfo()) {
1326 default:
1327 llvm_unreachable("Unknown loc info!");
1328 case CCValAssign::Full:
1329 break;
1330 case CCValAssign::SExt: {
1331 MVT DestVT = VA.getLocVT();
1332 const TargetRegisterClass *RC =
1333 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1334 unsigned TmpReg = createResultReg(RC);
1335 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1336 llvm_unreachable("Failed to emit a sext!");
1337 ArgVT = DestVT;
1338 Arg = TmpReg;
1339 break;
1340 }
1341 case CCValAssign::AExt:
1342 case CCValAssign::ZExt: {
1343 MVT DestVT = VA.getLocVT();
1344 const TargetRegisterClass *RC =
1345 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1346 unsigned TmpReg = createResultReg(RC);
1347 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1348 llvm_unreachable("Failed to emit a zext!");
1349 ArgVT = DestVT;
1350 Arg = TmpReg;
1351 break;
1352 }
1353 case CCValAssign::BCvt: {
1354 // FIXME: Not yet handled.
1355 llvm_unreachable("Should have bailed before getting here!");
1356 break;
1357 }
1358 }
1359
1360 // Copy this argument to the appropriate register.
1361 unsigned ArgReg;
1362 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1363 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001364 if (CC != CallingConv::Fast)
1365 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001366 } else
1367 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001368
1369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1370 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001371 RegArgs.push_back(ArgReg);
1372 }
1373
1374 return true;
1375}
1376
1377// For a call that we've determined we can fast-select, finish the
1378// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001379bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1380 CallingConv::ID CC = CLI.CallConv;
1381
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001382 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001384 TII.get(TII.getCallFrameDestroyOpcode()))
1385 .addImm(NumBytes).addImm(0);
1386
1387 // Next, generate a copy to obtain the return value.
1388 // FIXME: No multi-register return values yet, though I don't foresee
1389 // any real difficulties there.
1390 if (RetVT != MVT::isVoid) {
1391 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001392 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001393 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1394 CCValAssign &VA = RVLocs[0];
1395 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1396 assert(VA.isRegLoc() && "Can only return in registers!");
1397
1398 MVT DestVT = VA.getValVT();
1399 MVT CopyVT = DestVT;
1400
1401 // Ints smaller than a register still arrive in a full 64-bit
1402 // register, so make sure we recognize this.
1403 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1404 CopyVT = MVT::i64;
1405
1406 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001407 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001408
1409 if (RetVT == CopyVT) {
1410 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1411 ResultReg = createResultReg(CpyRC);
1412
Rafael Espindolaea09c592014-02-18 22:05:46 +00001413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001414 TII.get(TargetOpcode::COPY), ResultReg)
1415 .addReg(SourcePhysReg);
1416
1417 // If necessary, round the floating result to single precision.
1418 } else if (CopyVT == MVT::f64) {
1419 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001421 ResultReg).addReg(SourcePhysReg);
1422
1423 // If only the low half of a general register is needed, generate
1424 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1425 // used along the fast-isel path (not lowered), and downstream logic
1426 // also doesn't like a direct subreg copy on a physical reg.)
1427 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1428 ResultReg = createResultReg(&PPC::GPRCRegClass);
1429 // Convert physical register from G8RC to GPRC.
1430 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001432 TII.get(TargetOpcode::COPY), ResultReg)
1433 .addReg(SourcePhysReg);
1434 }
1435
Bill Schmidt0954ea12013-08-30 23:25:30 +00001436 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001437 CLI.InRegs.push_back(SourcePhysReg);
1438 CLI.ResultReg = ResultReg;
1439 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001440 }
Hal Finkel934361a2015-01-14 01:07:51 +00001441
1442 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001443}
1444
Hal Finkel934361a2015-01-14 01:07:51 +00001445bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1446 CallingConv::ID CC = CLI.CallConv;
1447 bool IsTailCall = CLI.IsTailCall;
1448 bool IsVarArg = CLI.IsVarArg;
1449 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001450 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001451
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001452 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001453 return false;
1454
1455 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001456 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001457 return false;
1458
Hal Finkel934361a2015-01-14 01:07:51 +00001459 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001460 if (IsVarArg)
1461 return false;
1462
1463 // Handle simple calls for now, with legal return types and
1464 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001465 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001466 MVT RetVT;
1467 if (RetTy->isVoidTy())
1468 RetVT = MVT::isVoid;
1469 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1470 RetVT != MVT::i8)
1471 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001472 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1473 // We can't handle boolean returns when CR bits are in use.
1474 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001475
1476 // FIXME: No multi-register return values yet.
1477 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1478 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1479 RetVT != MVT::f64) {
1480 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001481 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001482 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1483 if (RVLocs.size() > 1)
1484 return false;
1485 }
1486
1487 // Bail early if more than 8 arguments, as we only currently
1488 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001489 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001490 if (NumArgs > 8)
1491 return false;
1492
1493 // Set up the argument vectors.
1494 SmallVector<Value*, 8> Args;
1495 SmallVector<unsigned, 8> ArgRegs;
1496 SmallVector<MVT, 8> ArgVTs;
1497 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1498
1499 Args.reserve(NumArgs);
1500 ArgRegs.reserve(NumArgs);
1501 ArgVTs.reserve(NumArgs);
1502 ArgFlags.reserve(NumArgs);
1503
Hal Finkel934361a2015-01-14 01:07:51 +00001504 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001505 // Only handle easy calls for now. It would be reasonably easy
1506 // to handle <= 8-byte structures passed ByVal in registers, but we
1507 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001508 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1509 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001510 return false;
1511
Hal Finkel934361a2015-01-14 01:07:51 +00001512 Value *ArgValue = CLI.OutVals[i];
1513 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001514 MVT ArgVT;
1515 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1516 return false;
1517
1518 if (ArgVT.isVector())
1519 return false;
1520
Hal Finkel934361a2015-01-14 01:07:51 +00001521 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001522 if (Arg == 0)
1523 return false;
1524
Hal Finkel934361a2015-01-14 01:07:51 +00001525 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001526 ArgRegs.push_back(Arg);
1527 ArgVTs.push_back(ArgVT);
1528 ArgFlags.push_back(Flags);
1529 }
1530
1531 // Process the arguments.
1532 SmallVector<unsigned, 8> RegArgs;
1533 unsigned NumBytes;
1534
1535 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1536 RegArgs, CC, NumBytes, IsVarArg))
1537 return false;
1538
Hal Finkel934361a2015-01-14 01:07:51 +00001539 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001540 // FIXME: No handling for function pointers yet. This requires
1541 // implementing the function descriptor (OPD) setup.
1542 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001543 if (!GV) {
1544 // patchpoints are a special case; they always dispatch to a pointer value.
1545 // However, we don't actually want to generate the indirect call sequence
1546 // here (that will be generated, as necessary, during asm printing), and
1547 // the call we generate here will be erased by FastISel::selectPatchpoint,
1548 // so don't try very hard...
1549 if (CLI.IsPatchPoint)
1550 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1551 else
1552 return false;
1553 } else {
1554 // Build direct call with NOP for TOC restore.
1555 // FIXME: We can and should optimize away the NOP for local calls.
1556 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1557 TII.get(PPC::BL8_NOP));
1558 // Add callee.
1559 MIB.addGlobalAddress(GV);
1560 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001561
1562 // Add implicit physical register uses to the call.
1563 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1564 MIB.addReg(RegArgs[II], RegState::Implicit);
1565
Hal Finkelaf519932015-01-19 07:20:27 +00001566 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1567 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001568 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001569 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001570
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001571 // Add a register mask with the call-preserved registers. Proper
1572 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001573 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001574
Hal Finkel934361a2015-01-14 01:07:51 +00001575 CLI.Call = MIB;
1576
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001577 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001578 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001579}
1580
Bill Schmidtd89f6782013-08-26 19:42:51 +00001581// Attempt to fast-select a return instruction.
1582bool PPCFastISel::SelectRet(const Instruction *I) {
1583
1584 if (!FuncInfo.CanLowerReturn)
1585 return false;
1586
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001587 if (TLI.supportSplitCSR(FuncInfo.MF))
1588 return false;
1589
Bill Schmidtd89f6782013-08-26 19:42:51 +00001590 const ReturnInst *Ret = cast<ReturnInst>(I);
1591 const Function &F = *I->getParent()->getParent();
1592
1593 // Build a list of return value registers.
1594 SmallVector<unsigned, 4> RetRegs;
1595 CallingConv::ID CC = F.getCallingConv();
1596
1597 if (Ret->getNumOperands() > 0) {
1598 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001599 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001600
1601 // Analyze operands of the call, assigning locations to each operand.
1602 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001603 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001604 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1605 const Value *RV = Ret->getOperand(0);
1606
1607 // FIXME: Only one output register for now.
1608 if (ValLocs.size() > 1)
1609 return false;
1610
Eric Christopherf0024d12015-07-25 00:48:08 +00001611 // Special case for returning a constant integer of any size - materialize
1612 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001613 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001614 CCValAssign &VA = ValLocs[0];
1615
1616 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001617 // We still need to worry about properly extending the sign. For example,
1618 // we could have only a single bit or a constant that needs zero
1619 // extension rather than sign extension. Make sure we pass the return
1620 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001621 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001622 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001623
Rafael Espindolaea09c592014-02-18 22:05:46 +00001624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001625 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1626
Bill Schmidtd89f6782013-08-26 19:42:51 +00001627 RetRegs.push_back(RetReg);
1628
1629 } else {
1630 unsigned Reg = getRegForValue(RV);
1631
1632 if (Reg == 0)
1633 return false;
1634
1635 // Copy the result values into the output registers.
1636 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1637
1638 CCValAssign &VA = ValLocs[i];
1639 assert(VA.isRegLoc() && "Can only return in registers!");
1640 RetRegs.push_back(VA.getLocReg());
1641 unsigned SrcReg = Reg + VA.getValNo();
1642
Mehdi Amini44ede332015-07-09 02:09:04 +00001643 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001644 if (!RVEVT.isSimple())
1645 return false;
1646 MVT RVVT = RVEVT.getSimpleVT();
1647 MVT DestVT = VA.getLocVT();
1648
1649 if (RVVT != DestVT && RVVT != MVT::i8 &&
1650 RVVT != MVT::i16 && RVVT != MVT::i32)
1651 return false;
1652
1653 if (RVVT != DestVT) {
1654 switch (VA.getLocInfo()) {
1655 default:
1656 llvm_unreachable("Unknown loc info!");
1657 case CCValAssign::Full:
1658 llvm_unreachable("Full value assign but types don't match?");
1659 case CCValAssign::AExt:
1660 case CCValAssign::ZExt: {
1661 const TargetRegisterClass *RC =
1662 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1663 unsigned TmpReg = createResultReg(RC);
1664 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1665 return false;
1666 SrcReg = TmpReg;
1667 break;
1668 }
1669 case CCValAssign::SExt: {
1670 const TargetRegisterClass *RC =
1671 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1672 unsigned TmpReg = createResultReg(RC);
1673 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1674 return false;
1675 SrcReg = TmpReg;
1676 break;
1677 }
1678 }
1679 }
1680
Rafael Espindolaea09c592014-02-18 22:05:46 +00001681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001682 TII.get(TargetOpcode::COPY), RetRegs[i])
1683 .addReg(SrcReg);
1684 }
1685 }
1686 }
1687
Rafael Espindolaea09c592014-02-18 22:05:46 +00001688 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001689 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001690
1691 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1692 MIB.addReg(RetRegs[i], RegState::Implicit);
1693
1694 return true;
1695}
1696
Bill Schmidt03008132013-08-25 22:33:42 +00001697// Attempt to emit an integer extend of SrcReg into DestReg. Both
1698// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001699// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001700bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1701 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001702 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1703 return false;
1704 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1705 return false;
1706
1707 // Signed extensions use EXTSB, EXTSH, EXTSW.
1708 if (!IsZExt) {
1709 unsigned Opc;
1710 if (SrcVT == MVT::i8)
1711 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1712 else if (SrcVT == MVT::i16)
1713 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1714 else {
1715 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1716 Opc = PPC::EXTSW_32_64;
1717 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001719 .addReg(SrcReg);
1720
1721 // Unsigned 32-bit extensions use RLWINM.
1722 } else if (DestVT == MVT::i32) {
1723 unsigned MB;
1724 if (SrcVT == MVT::i8)
1725 MB = 24;
1726 else {
1727 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1728 MB = 16;
1729 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001730 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001731 DestReg)
1732 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1733
1734 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1735 } else {
1736 unsigned MB;
1737 if (SrcVT == MVT::i8)
1738 MB = 56;
1739 else if (SrcVT == MVT::i16)
1740 MB = 48;
1741 else
1742 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001744 TII.get(PPC::RLDICL_32_64), DestReg)
1745 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1746 }
1747
1748 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001749}
1750
1751// Attempt to fast-select an indirect branch instruction.
1752bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1753 unsigned AddrReg = getRegForValue(I->getOperand(0));
1754 if (AddrReg == 0)
1755 return false;
1756
Rafael Espindolaea09c592014-02-18 22:05:46 +00001757 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001758 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001759 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001760
1761 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001762 for (const BasicBlock *SuccBB : IB->successors())
1763 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001764
1765 return true;
1766}
1767
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001768// Attempt to fast-select an integer truncate instruction.
1769bool PPCFastISel::SelectTrunc(const Instruction *I) {
1770 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001771 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1772 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001773
1774 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1775 return false;
1776
1777 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1778 return false;
1779
1780 unsigned SrcReg = getRegForValue(Src);
1781 if (!SrcReg)
1782 return false;
1783
1784 // The only interesting case is when we need to switch register classes.
1785 if (SrcVT == MVT::i64) {
1786 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001787 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1788 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001789 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1790 SrcReg = ResultReg;
1791 }
1792
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001793 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001794 return true;
1795}
1796
Bill Schmidtd89f6782013-08-26 19:42:51 +00001797// Attempt to fast-select an integer extend instruction.
1798bool PPCFastISel::SelectIntExt(const Instruction *I) {
1799 Type *DestTy = I->getType();
1800 Value *Src = I->getOperand(0);
1801 Type *SrcTy = Src->getType();
1802
1803 bool IsZExt = isa<ZExtInst>(I);
1804 unsigned SrcReg = getRegForValue(Src);
1805 if (!SrcReg) return false;
1806
1807 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001808 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1809 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001810 if (!SrcEVT.isSimple())
1811 return false;
1812 if (!DestEVT.isSimple())
1813 return false;
1814
1815 MVT SrcVT = SrcEVT.getSimpleVT();
1816 MVT DestVT = DestEVT.getSimpleVT();
1817
1818 // If we know the register class needed for the result of this
1819 // instruction, use it. Otherwise pick the register class of the
1820 // correct size that does not contain X0/R0, since we don't know
1821 // whether downstream uses permit that assignment.
1822 unsigned AssignedReg = FuncInfo.ValueMap[I];
1823 const TargetRegisterClass *RC =
1824 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1825 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1826 &PPC::GPRC_and_GPRC_NOR0RegClass));
1827 unsigned ResultReg = createResultReg(RC);
1828
1829 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1830 return false;
1831
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001832 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001833 return true;
1834}
1835
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001836// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001837// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001838bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001839
1840 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001841 case Instruction::Load:
1842 return SelectLoad(I);
1843 case Instruction::Store:
1844 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001845 case Instruction::Br:
1846 return SelectBranch(I);
1847 case Instruction::IndirectBr:
1848 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001849 case Instruction::FPExt:
1850 return SelectFPExt(I);
1851 case Instruction::FPTrunc:
1852 return SelectFPTrunc(I);
1853 case Instruction::SIToFP:
1854 return SelectIToFP(I, /*IsSigned*/ true);
1855 case Instruction::UIToFP:
1856 return SelectIToFP(I, /*IsSigned*/ false);
1857 case Instruction::FPToSI:
1858 return SelectFPToI(I, /*IsSigned*/ true);
1859 case Instruction::FPToUI:
1860 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001861 case Instruction::Add:
1862 return SelectBinaryIntOp(I, ISD::ADD);
1863 case Instruction::Or:
1864 return SelectBinaryIntOp(I, ISD::OR);
1865 case Instruction::Sub:
1866 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001867 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001868 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001869 case Instruction::Ret:
1870 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001871 case Instruction::Trunc:
1872 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001873 case Instruction::ZExt:
1874 case Instruction::SExt:
1875 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001876 // Here add other flavors of Instruction::XXX that automated
1877 // cases don't catch. For example, switches are terminators
1878 // that aren't yet handled.
1879 default:
1880 break;
1881 }
1882 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001883}
1884
1885// Materialize a floating-point constant into a register, and return
1886// the register number (or zero if we failed to handle it).
1887unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1888 // No plans to handle long double here.
1889 if (VT != MVT::f32 && VT != MVT::f64)
1890 return 0;
1891
1892 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001893 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001894 assert(Align > 0 && "Unexpectedly missing alignment information!");
1895 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1896 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1897 CodeModel::Model CModel = TM.getCodeModel();
1898
Alex Lorenze40c8a22015-08-11 23:09:45 +00001899 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1900 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
1901 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001902
Bill Schmidt03008132013-08-25 22:33:42 +00001903 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1904 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1905
Hal Finkele6698d52015-02-01 15:03:28 +00001906 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001907 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
1908 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001910 TmpReg)
1911 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001913 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1914 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001915 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001916 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001917 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001918 // But for large code model, we must generate a LDtocL followed
1919 // by the LF[SD].
1920 if (CModel == CodeModel::Large) {
1921 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001923 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001924 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001925 .addImm(0).addReg(TmpReg2);
1926 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001927 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001928 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1929 .addReg(TmpReg)
1930 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001931 }
1932
1933 return DestReg;
1934}
1935
Bill Schmidtccecf262013-08-30 02:29:45 +00001936// Materialize the address of a global value into a register, and return
1937// the register number (or zero if we failed to handle it).
1938unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1939 assert(VT == MVT::i64 && "Non-address!");
1940 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1941 unsigned DestReg = createResultReg(RC);
1942
1943 // Global values may be plain old object addresses, TLS object
1944 // addresses, constant pool entries, or jump tables. How we generate
1945 // code for these may depend on small, medium, or large code model.
1946 CodeModel::Model CModel = TM.getCodeModel();
1947
1948 // FIXME: Jump tables are not yet required because fast-isel doesn't
1949 // handle switches; if that changes, we need them as well. For now,
1950 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001951
1952 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001953 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001954 return 0;
1955
Hal Finkele6698d52015-02-01 15:03:28 +00001956 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001957 // For small code model, generate a simple TOC load.
1958 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1960 DestReg)
1961 .addGlobalAddress(GV)
1962 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001963 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001964 // If the address is an externally defined symbol, a symbol with common
1965 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001966 // jump table address (not yet needed), or if we are generating code
1967 // for large code model, we generate:
1968 // LDtocL(GV, ADDIStocHA(%X2, GV))
1969 // Otherwise we generate:
1970 // ADDItocL(ADDIStocHA(%X2, GV), GV)
1971 // Either way, start with the ADDIStocHA:
1972 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00001974 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
1975
Eric Christopherc1808362015-11-20 20:51:31 +00001976 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
1977 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001979 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00001980 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00001981 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00001983 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00001984 }
Bill Schmidtccecf262013-08-30 02:29:45 +00001985 }
1986
1987 return DestReg;
1988}
1989
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001990// Materialize a 32-bit integer constant into a register, and return
1991// the register number (or zero if we failed to handle it).
1992unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
1993 const TargetRegisterClass *RC) {
1994 unsigned Lo = Imm & 0xFFFF;
1995 unsigned Hi = (Imm >> 16) & 0xFFFF;
1996
1997 unsigned ResultReg = createResultReg(RC);
1998 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1999
2000 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002001 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002002 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2003 .addImm(Imm);
2004 else if (Lo) {
2005 // Both Lo and Hi have nonzero bits.
2006 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002008 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2009 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002011 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2012 .addReg(TmpReg).addImm(Lo);
2013 } else
2014 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002016 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2017 .addImm(Hi);
2018
2019 return ResultReg;
2020}
2021
2022// Materialize a 64-bit integer constant into a register, and return
2023// the register number (or zero if we failed to handle it).
2024unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2025 const TargetRegisterClass *RC) {
2026 unsigned Remainder = 0;
2027 unsigned Shift = 0;
2028
2029 // If the value doesn't fit in 32 bits, see if we can shift it
2030 // so that it fits in 32 bits.
2031 if (!isInt<32>(Imm)) {
2032 Shift = countTrailingZeros<uint64_t>(Imm);
2033 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2034
2035 if (isInt<32>(ImmSh))
2036 Imm = ImmSh;
2037 else {
2038 Remainder = Imm;
2039 Shift = 32;
2040 Imm >>= 32;
2041 }
2042 }
2043
2044 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2045 // (if not shifted).
2046 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2047 if (!Shift)
2048 return TmpReg1;
2049
2050 // If upper 32 bits were not zero, we've built them and need to shift
2051 // them into place.
2052 unsigned TmpReg2;
2053 if (Imm) {
2054 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002056 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2057 } else
2058 TmpReg2 = TmpReg1;
2059
2060 unsigned TmpReg3, Hi, Lo;
2061 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2062 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002064 TmpReg3).addReg(TmpReg2).addImm(Hi);
2065 } else
2066 TmpReg3 = TmpReg2;
2067
2068 if ((Lo = Remainder & 0xFFFF)) {
2069 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002071 ResultReg).addReg(TmpReg3).addImm(Lo);
2072 return ResultReg;
2073 }
2074
2075 return TmpReg3;
2076}
2077
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002078// Materialize an integer constant into a register, and return
2079// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002080unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2081 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002082 // If we're using CR bit registers for i1 values, handle that as a special
2083 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002084 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002085 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2087 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2088 return ImmReg;
2089 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002090
Eric Christopher80ba58a2016-01-29 07:19:49 +00002091 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2092 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002093 return 0;
2094
Eric Christopher80ba58a2016-01-29 07:19:49 +00002095 const TargetRegisterClass *RC =
2096 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002097 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002098
2099 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002100 // Since LI will sign extend the constant we need to make sure that for
2101 // our zeroext constants that the sign extended constant fits into 16-bits -
2102 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002103 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002104 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2105 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002106 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002107 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002108 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002109 }
2110
2111 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002112 if (VT == MVT::i64)
2113 return PPCMaterialize64BitInt(Imm, RC);
2114 else if (VT == MVT::i32)
2115 return PPCMaterialize32BitInt(Imm, RC);
2116
2117 return 0;
2118}
2119
2120// Materialize a constant into a register, and return the register
2121// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002122unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002123 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002124
2125 // Only handle simple types.
2126 if (!CEVT.isSimple()) return 0;
2127 MVT VT = CEVT.getSimpleVT();
2128
2129 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2130 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002131 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2132 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002133 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2134 return PPCMaterializeInt(CI, VT, VT != MVT::i1);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002135
2136 return 0;
2137}
2138
2139// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002140// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002141unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002142 // Don't handle dynamic allocas.
2143 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2144
2145 MVT VT;
2146 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2147
2148 DenseMap<const AllocaInst*, int>::iterator SI =
2149 FuncInfo.StaticAllocaMap.find(AI);
2150
2151 if (SI != FuncInfo.StaticAllocaMap.end()) {
2152 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002153 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002154 ResultReg).addFrameIndex(SI->second).addImm(0);
2155 return ResultReg;
2156 }
2157
2158 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002159}
2160
Bill Schmidtccecf262013-08-30 02:29:45 +00002161// Fold loads into extends when possible.
2162// FIXME: We can have multiple redundant extend/trunc instructions
2163// following a load. The folding only picks up one. Extend this
2164// to check subsequent instructions for the same pattern and remove
2165// them. Thus ResultReg should be the def reg for the last redundant
2166// instruction in a chain, and all intervening instructions can be
2167// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2168// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002169bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2170 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002171 // Verify we have a legal type before going any further.
2172 MVT VT;
2173 if (!isLoadTypeLegal(LI->getType(), VT))
2174 return false;
2175
2176 // Combine load followed by zero- or sign-extend.
2177 bool IsZExt = false;
2178 switch(MI->getOpcode()) {
2179 default:
2180 return false;
2181
2182 case PPC::RLDICL:
2183 case PPC::RLDICL_32_64: {
2184 IsZExt = true;
2185 unsigned MB = MI->getOperand(3).getImm();
2186 if ((VT == MVT::i8 && MB <= 56) ||
2187 (VT == MVT::i16 && MB <= 48) ||
2188 (VT == MVT::i32 && MB <= 32))
2189 break;
2190 return false;
2191 }
2192
2193 case PPC::RLWINM:
2194 case PPC::RLWINM8: {
2195 IsZExt = true;
2196 unsigned MB = MI->getOperand(3).getImm();
2197 if ((VT == MVT::i8 && MB <= 24) ||
2198 (VT == MVT::i16 && MB <= 16))
2199 break;
2200 return false;
2201 }
2202
2203 case PPC::EXTSB:
2204 case PPC::EXTSB8:
2205 case PPC::EXTSB8_32_64:
2206 /* There is no sign-extending load-byte instruction. */
2207 return false;
2208
2209 case PPC::EXTSH:
2210 case PPC::EXTSH8:
2211 case PPC::EXTSH8_32_64: {
2212 if (VT != MVT::i16 && VT != MVT::i8)
2213 return false;
2214 break;
2215 }
2216
2217 case PPC::EXTSW:
2218 case PPC::EXTSW_32_64: {
2219 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2220 return false;
2221 break;
2222 }
2223 }
2224
2225 // See if we can handle this address.
2226 Address Addr;
2227 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2228 return false;
2229
2230 unsigned ResultReg = MI->getOperand(0).getReg();
2231
Craig Topper062a2ba2014-04-25 05:30:21 +00002232 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002233 return false;
2234
2235 MI->eraseFromParent();
2236 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002237}
2238
2239// Attempt to lower call arguments in a faster way than done by
2240// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002241bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002242 // Defer to normal argument lowering for now. It's reasonably
2243 // efficient. Consider doing something like ARM to handle the
2244 // case where all args fit in registers, no varargs, no float
2245 // or vector args.
2246 return false;
2247}
2248
Bill Schmidt03008132013-08-25 22:33:42 +00002249// Handle materializing integer constants into a register. This is not
2250// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002251unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
Bill Schmidt03008132013-08-25 22:33:42 +00002252
2253 if (Opc != ISD::Constant)
2254 return 0;
2255
Hal Finkel940ab932014-02-28 00:27:01 +00002256 // If we're using CR bit registers for i1 values, handle that as a special
2257 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002258 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002259 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2260 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2261 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2262 return ImmReg;
2263 }
2264
Bill Schmidt03008132013-08-25 22:33:42 +00002265 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 &&
2266 VT != MVT::i8 && VT != MVT::i1)
2267 return 0;
2268
2269 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2270 &PPC::GPRCRegClass);
2271 if (VT == MVT::i64)
2272 return PPCMaterialize64BitInt(Imm, RC);
2273 else
2274 return PPCMaterialize32BitInt(Imm, RC);
2275}
2276
Bill Schmidtccecf262013-08-30 02:29:45 +00002277// Override for ADDI and ADDI8 to set the correct register class
2278// on RHS operand 0. The automatic infrastructure naively assumes
2279// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2280// for these cases. At the moment, none of the other automatically
2281// generated RI instructions require special treatment. However, once
2282// SelectSelect is implemented, "isel" requires similar handling.
2283//
2284// Also be conservative about the output register class. Avoid
2285// assigning R0 or X0 to the output register for GPRC and G8RC
2286// register classes, as any such result could be used in ADDI, etc.,
2287// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002288unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002289 const TargetRegisterClass *RC,
2290 unsigned Op0, bool Op0IsKill,
2291 uint64_t Imm) {
2292 if (MachineInstOpcode == PPC::ADDI)
2293 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2294 else if (MachineInstOpcode == PPC::ADDI8)
2295 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2296
2297 const TargetRegisterClass *UseRC =
2298 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2299 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2300
Juergen Ributzka88e32512014-09-03 20:56:59 +00002301 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002302 Op0, Op0IsKill, Imm);
2303}
2304
2305// Override for instructions with one register operand to avoid use of
2306// R0/X0. The automatic infrastructure isn't aware of the context so
2307// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002308unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002309 const TargetRegisterClass* RC,
2310 unsigned Op0, bool Op0IsKill) {
2311 const TargetRegisterClass *UseRC =
2312 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2313 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2314
Juergen Ributzka88e32512014-09-03 20:56:59 +00002315 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002316}
2317
2318// Override for instructions with two register operands to avoid use
2319// of R0/X0. The automatic infrastructure isn't aware of the context
2320// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002321unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002322 const TargetRegisterClass* RC,
2323 unsigned Op0, bool Op0IsKill,
2324 unsigned Op1, bool Op1IsKill) {
2325 const TargetRegisterClass *UseRC =
2326 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2327 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2328
Juergen Ributzka88e32512014-09-03 20:56:59 +00002329 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002330 Op1, Op1IsKill);
2331}
2332
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002333namespace llvm {
2334 // Create the fast instruction selector for PowerPC64 ELF.
2335 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2336 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002337 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002338 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002339 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002340 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002341 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002342 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002343}