blob: cd19568306e139d33c6c02239ed623d709e0faeb [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
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "PPC.h"
Strahinja Petrovice682b802016-05-09 12:27:39 +000018#include "PPCCCState.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "PPCCallingConv.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"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetLowering.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000033#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000034#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000035#include "llvm/IR/GlobalAlias.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Operator.h"
39#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000040#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;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000149 bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
150 return RC->getID() == PPC::VSFRCRegClassID;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000151 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000152 bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
153 return RC->getID() == PPC::VSSRCRegClassID;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000154 }
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);
Rafael Espindola463aed82016-06-21 20:09:22 +0000189 LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000190
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000191 private:
192 #include "PPCGenFastISel.inc"
193
194};
195
196} // end anonymous namespace
197
Bill Schmidtd89f6782013-08-26 19:42:51 +0000198#include "PPCGenCallingConv.inc"
199
Rafael Espindola463aed82016-06-21 20:09:22 +0000200// Function whose sole purpose is to kill compiler warnings
201// stemming from unused functions included from PPCGenCallingConv.inc.
202CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
203 if (Flag == 1)
204 return CC_PPC32_SVR4;
205 else if (Flag == 2)
206 return CC_PPC32_SVR4_ByVal;
207 else if (Flag == 3)
208 return CC_PPC32_SVR4_VarArg;
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000209 else if (Flag == 4)
210 return RetCC_PPC_Cold;
Rafael Espindola463aed82016-06-21 20:09:22 +0000211 else
212 return RetCC_PPC;
213}
214
Bill Schmidt03008132013-08-25 22:33:42 +0000215static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
216 switch (Pred) {
217 // These are not representable with any single compare.
218 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000219 case CmpInst::FCMP_TRUE:
220 // Major concern about the following 6 cases is NaN result. The comparison
221 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
222 // only one of which will be set. The result is generated by fcmpu
223 // instruction. However, bc instruction only inspects one of the first 3
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000224 // bits, so when un is set, bc instruction may jump to an undesired
Tim Shen5cdf7502016-03-17 22:27:58 +0000225 // place.
226 //
227 // More specifically, if we expect an unordered comparison and un is set, we
228 // expect to always go to true branch; in such case UEQ, UGT and ULT still
229 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
230 // since they are tested by inspecting !eq, !lt, !gt, respectively.
231 //
232 // Similarly, for ordered comparison, when un is set, we always expect the
233 // result to be false. In such case OGT, OLT and OEQ is good, since they are
234 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
235 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000236 case CmpInst::FCMP_UEQ:
237 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000238 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000239 case CmpInst::FCMP_OGE:
240 case CmpInst::FCMP_OLE:
241 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000242 default:
243 return Optional<PPC::Predicate>();
244
245 case CmpInst::FCMP_OEQ:
246 case CmpInst::ICMP_EQ:
247 return PPC::PRED_EQ;
248
249 case CmpInst::FCMP_OGT:
250 case CmpInst::ICMP_UGT:
251 case CmpInst::ICMP_SGT:
252 return PPC::PRED_GT;
253
Tim Shen5cdf7502016-03-17 22:27:58 +0000254 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000255 case CmpInst::ICMP_UGE:
256 case CmpInst::ICMP_SGE:
257 return PPC::PRED_GE;
258
259 case CmpInst::FCMP_OLT:
260 case CmpInst::ICMP_ULT:
261 case CmpInst::ICMP_SLT:
262 return PPC::PRED_LT;
263
Tim Shen5cdf7502016-03-17 22:27:58 +0000264 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000265 case CmpInst::ICMP_ULE:
266 case CmpInst::ICMP_SLE:
267 return PPC::PRED_LE;
268
Tim Shen5cdf7502016-03-17 22:27:58 +0000269 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000270 case CmpInst::ICMP_NE:
271 return PPC::PRED_NE;
272
273 case CmpInst::FCMP_ORD:
274 return PPC::PRED_NU;
275
276 case CmpInst::FCMP_UNO:
277 return PPC::PRED_UN;
278 }
279}
280
Bill Schmidtccecf262013-08-30 02:29:45 +0000281// Determine whether the type Ty is simple enough to be handled by
282// fast-isel, and return its equivalent machine type in VT.
283// FIXME: Copied directly from ARM -- factor into base class?
284bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000285 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000286
287 // Only handle simple types.
288 if (Evt == MVT::Other || !Evt.isSimple()) return false;
289 VT = Evt.getSimpleVT();
290
291 // Handle all legal types, i.e. a register that will directly hold this
292 // value.
293 return TLI.isTypeLegal(VT);
294}
295
296// Determine whether the type Ty is simple enough to be handled by
297// fast-isel as a load target, and return its equivalent machine type in VT.
298bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
299 if (isTypeLegal(Ty, VT)) return true;
300
301 // If this is a type than can be sign or zero-extended to a basic operation
302 // go ahead and accept it now.
303 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
304 return true;
305 }
306
307 return false;
308}
309
Hal Finkel5f2a1372015-05-23 12:18:10 +0000310bool PPCFastISel::isValueAvailable(const Value *V) const {
311 if (!isa<Instruction>(V))
312 return true;
313
314 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000315 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000316}
317
Bill Schmidtccecf262013-08-30 02:29:45 +0000318// Given a value Obj, create an Address object Addr that represents its
319// address. Return false if we can't handle it.
320bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000321 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000322 unsigned Opcode = Instruction::UserOp1;
323 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
324 // Don't walk into other basic blocks unless the object is an alloca from
325 // another block, otherwise it may not have a virtual register assigned.
326 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
327 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
328 Opcode = I->getOpcode();
329 U = I;
330 }
331 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
332 Opcode = C->getOpcode();
333 U = C;
334 }
335
336 switch (Opcode) {
337 default:
338 break;
339 case Instruction::BitCast:
340 // Look through bitcasts.
341 return PPCComputeAddress(U->getOperand(0), Addr);
342 case Instruction::IntToPtr:
343 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000344 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
345 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000346 return PPCComputeAddress(U->getOperand(0), Addr);
347 break;
348 case Instruction::PtrToInt:
349 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000350 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000351 return PPCComputeAddress(U->getOperand(0), Addr);
352 break;
353 case Instruction::GetElementPtr: {
354 Address SavedAddr = Addr;
355 long TmpOffset = Addr.Offset;
356
357 // Iterate through the GEP folding the constants into offsets where
358 // we can.
359 gep_type_iterator GTI = gep_type_begin(U);
360 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
361 II != IE; ++II, ++GTI) {
362 const Value *Op = *II;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000363 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000364 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000365 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
366 TmpOffset += SL->getElementOffset(Idx);
367 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000368 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000369 for (;;) {
370 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
371 // Constant-offset addressing.
372 TmpOffset += CI->getSExtValue() * S;
373 break;
374 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000375 if (canFoldAddIntoGEP(U, Op)) {
376 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000377 ConstantInt *CI =
378 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
379 TmpOffset += CI->getSExtValue() * S;
380 // Iterate on the other operand.
381 Op = cast<AddOperator>(Op)->getOperand(0);
382 continue;
383 }
384 // Unsupported
385 goto unsupported_gep;
386 }
387 }
388 }
389
390 // Try to grab the base operand now.
391 Addr.Offset = TmpOffset;
392 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
393
394 // We failed, restore everything and try the other options.
395 Addr = SavedAddr;
396
397 unsupported_gep:
398 break;
399 }
400 case Instruction::Alloca: {
401 const AllocaInst *AI = cast<AllocaInst>(Obj);
402 DenseMap<const AllocaInst*, int>::iterator SI =
403 FuncInfo.StaticAllocaMap.find(AI);
404 if (SI != FuncInfo.StaticAllocaMap.end()) {
405 Addr.BaseType = Address::FrameIndexBase;
406 Addr.Base.FI = SI->second;
407 return true;
408 }
409 break;
410 }
411 }
412
413 // FIXME: References to parameters fall through to the behavior
414 // below. They should be able to reference a frame index since
415 // they are stored to the stack, so we can get "ld rx, offset(r1)"
416 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
417 // just contain the parameter. Try to handle this with a FI.
418
419 // Try to get this in a register if nothing else has worked.
420 if (Addr.Base.Reg == 0)
421 Addr.Base.Reg = getRegForValue(Obj);
422
423 // Prevent assignment of base register to X0, which is inappropriate
424 // for loads and stores alike.
425 if (Addr.Base.Reg != 0)
426 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
427
428 return Addr.Base.Reg != 0;
429}
430
431// Fix up some addresses that can't be used directly. For example, if
432// an offset won't fit in an instruction field, we may need to move it
433// into an index register.
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000434void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000435 unsigned &IndexReg) {
436
437 // Check whether the offset fits in the instruction field.
438 if (!isInt<16>(Addr.Offset))
439 UseOffset = false;
440
441 // If this is a stack pointer and the offset needs to be simplified then
442 // put the alloca address into a register, set the base type back to
443 // register and continue. This should almost never happen.
444 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
445 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000447 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
448 Addr.Base.Reg = ResultReg;
449 Addr.BaseType = Address::RegBase;
450 }
451
452 if (!UseOffset) {
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000453 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
Bill Schmidtccecf262013-08-30 02:29:45 +0000454 const ConstantInt *Offset =
455 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
456 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
457 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
458 }
459}
460
461// Emit a load instruction if possible, returning true if we succeeded,
462// otherwise false. See commentary below for how the register class of
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000463// the load is determined.
Bill Schmidtccecf262013-08-30 02:29:45 +0000464bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
465 const TargetRegisterClass *RC,
466 bool IsZExt, unsigned FP64LoadOpc) {
467 unsigned Opc;
468 bool UseOffset = true;
469
470 // If ResultReg is given, it determines the register class of the load.
471 // Otherwise, RC is the register class to use. If the result of the
472 // load isn't anticipated in this block, both may be zero, in which
473 // case we must make a conservative guess. In particular, don't assign
474 // R0 or X0 to the result register, as the result may be used in a load,
475 // store, add-immediate, or isel that won't permit this. (Though
476 // perhaps the spill and reload of live-exit values would handle this?)
477 const TargetRegisterClass *UseRC =
478 (ResultReg ? MRI.getRegClass(ResultReg) :
479 (RC ? RC :
480 (VT == MVT::f64 ? &PPC::F8RCRegClass :
481 (VT == MVT::f32 ? &PPC::F4RCRegClass :
482 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
483 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
484
485 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
486
487 switch (VT.SimpleTy) {
488 default: // e.g., vector types not handled
489 return false;
490 case MVT::i8:
491 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
492 break;
493 case MVT::i16:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000494 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
495 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
Bill Schmidtccecf262013-08-30 02:29:45 +0000496 break;
497 case MVT::i32:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000498 Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
499 : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
Bill Schmidtccecf262013-08-30 02:29:45 +0000500 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
501 UseOffset = false;
502 break;
503 case MVT::i64:
504 Opc = PPC::LD;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000505 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
Bill Schmidtccecf262013-08-30 02:29:45 +0000506 "64-bit load with 32-bit target??");
507 UseOffset = ((Addr.Offset & 3) == 0);
508 break;
509 case MVT::f32:
510 Opc = PPC::LFS;
511 break;
512 case MVT::f64:
513 Opc = FP64LoadOpc;
514 break;
515 }
516
517 // If necessary, materialize the offset into a register and use
518 // the indexed form. Also handle stack pointers with special needs.
519 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000520 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000521
522 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
523 // be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000524 bool IsVSSRC = isVSSRCRegClass(UseRC);
525 bool IsVSFRC = isVSFRCRegClass(UseRC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000526 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000527 bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000528 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000529 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
530 (Addr.Offset == 0)) {
531 UseOffset = false;
532 }
533
Bill Schmidtccecf262013-08-30 02:29:45 +0000534 if (ResultReg == 0)
535 ResultReg = createResultReg(UseRC);
536
537 // Note: If we still have a frame index here, we know the offset is
538 // in range, as otherwise PPCSimplifyAddress would have converted it
539 // into a RegBase.
540 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000541 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000542 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000543
Alex Lorenze40c8a22015-08-11 23:09:45 +0000544 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
545 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
546 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000547 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
548 MFI.getObjectAlignment(Addr.Base.FI));
549
Rafael Espindolaea09c592014-02-18 22:05:46 +0000550 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000551 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
552
553 // Base reg with offset in range.
554 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000555 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000556 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000557
Rafael Espindolaea09c592014-02-18 22:05:46 +0000558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000559 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
560
561 // Indexed form.
562 } else {
563 // Get the RR opcode corresponding to the RI one. FIXME: It would be
564 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
565 // is hard to get at.
566 switch (Opc) {
567 default: llvm_unreachable("Unexpected opcode!");
568 case PPC::LBZ: Opc = PPC::LBZX; break;
569 case PPC::LBZ8: Opc = PPC::LBZX8; break;
570 case PPC::LHZ: Opc = PPC::LHZX; break;
571 case PPC::LHZ8: Opc = PPC::LHZX8; break;
572 case PPC::LHA: Opc = PPC::LHAX; break;
573 case PPC::LHA8: Opc = PPC::LHAX8; break;
574 case PPC::LWZ: Opc = PPC::LWZX; break;
575 case PPC::LWZ8: Opc = PPC::LWZX8; break;
576 case PPC::LWA: Opc = PPC::LWAX; break;
577 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
578 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000579 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000580 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000581 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000582
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000583 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
584 ResultReg);
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000585
586 // If we have an index register defined we use it in the store inst,
587 // otherwise we use X0 as base as it makes the vector instructions to
588 // use zero in the computation of the effective address regardless the
589 // content of the register.
590 if (IndexReg)
591 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
592 else
593 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000594 }
595
596 return true;
597}
598
599// Attempt to fast-select a load instruction.
600bool PPCFastISel::SelectLoad(const Instruction *I) {
601 // FIXME: No atomic loads are supported.
602 if (cast<LoadInst>(I)->isAtomic())
603 return false;
604
605 // Verify we have a legal type before going any further.
606 MVT VT;
607 if (!isLoadTypeLegal(I->getType(), VT))
608 return false;
609
610 // See if we can handle this address.
611 Address Addr;
612 if (!PPCComputeAddress(I->getOperand(0), Addr))
613 return false;
614
615 // Look at the currently assigned register for this instruction
616 // to determine the required register class. This is necessary
617 // to constrain RA from using R0/X0 when this is not legal.
618 unsigned AssignedReg = FuncInfo.ValueMap[I];
619 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000620 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000621
622 unsigned ResultReg = 0;
623 if (!PPCEmitLoad(VT, ResultReg, Addr, RC))
624 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000625 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000626 return true;
627}
628
629// Emit a store instruction to store SrcReg at Addr.
630bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
631 assert(SrcReg && "Nothing to store!");
632 unsigned Opc;
633 bool UseOffset = true;
634
635 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
636 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
637
638 switch (VT.SimpleTy) {
639 default: // e.g., vector types not handled
640 return false;
641 case MVT::i8:
642 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
643 break;
644 case MVT::i16:
645 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
646 break;
647 case MVT::i32:
648 assert(Is32BitInt && "Not GPRC for i32??");
649 Opc = PPC::STW;
650 break;
651 case MVT::i64:
652 Opc = PPC::STD;
653 UseOffset = ((Addr.Offset & 3) == 0);
654 break;
655 case MVT::f32:
656 Opc = PPC::STFS;
657 break;
658 case MVT::f64:
659 Opc = PPC::STFD;
660 break;
661 }
662
663 // If necessary, materialize the offset into a register and use
664 // the indexed form. Also handle stack pointers with special needs.
665 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000666 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000667
Bill Seurer8c728ae2014-12-05 20:15:56 +0000668 // If this is a potential VSX store with an offset of 0, a VSX indexed store
669 // can be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000670 bool IsVSSRC = isVSSRCRegClass(RC);
671 bool IsVSFRC = isVSFRCRegClass(RC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000672 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
673 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
674 if ((Is32VSXStore || Is64VSXStore) &&
675 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000676 (Addr.Offset == 0)) {
677 UseOffset = false;
678 }
679
Bill Schmidtccecf262013-08-30 02:29:45 +0000680 // Note: If we still have a frame index here, we know the offset is
681 // in range, as otherwise PPCSimplifyAddress would have converted it
682 // into a RegBase.
683 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000684 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000685 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000686
Alex Lorenze40c8a22015-08-11 23:09:45 +0000687 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
688 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
689 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000690 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
691 MFI.getObjectAlignment(Addr.Base.FI));
692
Rafael Espindolaea09c592014-02-18 22:05:46 +0000693 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
694 .addReg(SrcReg)
695 .addImm(Addr.Offset)
696 .addFrameIndex(Addr.Base.FI)
697 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000698
699 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000700 } else if (UseOffset) {
701 // VSX only provides an indexed store.
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000702 if (Is32VSXStore || Is64VSXStore)
703 return false;
704
Rafael Espindolaea09c592014-02-18 22:05:46 +0000705 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000706 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
707
708 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000709 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000710 // Get the RR opcode corresponding to the RI one. FIXME: It would be
711 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
712 // is hard to get at.
713 switch (Opc) {
714 default: llvm_unreachable("Unexpected opcode!");
715 case PPC::STB: Opc = PPC::STBX; break;
716 case PPC::STH : Opc = PPC::STHX; break;
717 case PPC::STW : Opc = PPC::STWX; break;
718 case PPC::STB8: Opc = PPC::STBX8; break;
719 case PPC::STH8: Opc = PPC::STHX8; break;
720 case PPC::STW8: Opc = PPC::STWX8; break;
721 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000722 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000723 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000724 }
Samuel Antaof6815602015-03-17 15:00:57 +0000725
726 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
727 .addReg(SrcReg);
728
729 // If we have an index register defined we use it in the store inst,
730 // otherwise we use X0 as base as it makes the vector instructions to
731 // use zero in the computation of the effective address regardless the
732 // content of the register.
733 if (IndexReg)
734 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
735 else
736 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000737 }
738
739 return true;
740}
741
742// Attempt to fast-select a store instruction.
743bool PPCFastISel::SelectStore(const Instruction *I) {
744 Value *Op0 = I->getOperand(0);
745 unsigned SrcReg = 0;
746
747 // FIXME: No atomics loads are supported.
748 if (cast<StoreInst>(I)->isAtomic())
749 return false;
750
751 // Verify we have a legal type before going any further.
752 MVT VT;
753 if (!isLoadTypeLegal(Op0->getType(), VT))
754 return false;
755
756 // Get the value to be stored into a register.
757 SrcReg = getRegForValue(Op0);
758 if (SrcReg == 0)
759 return false;
760
761 // See if we can handle this address.
762 Address Addr;
763 if (!PPCComputeAddress(I->getOperand(1), Addr))
764 return false;
765
766 if (!PPCEmitStore(VT, SrcReg, Addr))
767 return false;
768
769 return true;
770}
771
Bill Schmidt03008132013-08-25 22:33:42 +0000772// Attempt to fast-select a branch instruction.
773bool PPCFastISel::SelectBranch(const Instruction *I) {
774 const BranchInst *BI = cast<BranchInst>(I);
775 MachineBasicBlock *BrBB = FuncInfo.MBB;
776 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
777 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
778
779 // For now, just try the simplest case where it's fed by a compare.
780 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000781 if (isValueAvailable(CI)) {
782 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
783 if (!OptPPCPred)
784 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000785
Hal Finkel5f2a1372015-05-23 12:18:10 +0000786 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000787
Hal Finkel5f2a1372015-05-23 12:18:10 +0000788 // Take advantage of fall-through opportunities.
789 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
790 std::swap(TBB, FBB);
791 PPCPred = PPC::InvertPredicate(PPCPred);
792 }
793
794 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
795
796 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
797 CondReg))
798 return false;
799
800 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
801 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000802 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000803 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000804 }
Bill Schmidt03008132013-08-25 22:33:42 +0000805 } else if (const ConstantInt *CI =
806 dyn_cast<ConstantInt>(BI->getCondition())) {
807 uint64_t Imm = CI->getZExtValue();
808 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000809 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000810 return true;
811 }
812
813 // FIXME: ARM looks for a case where the block containing the compare
814 // has been split from the block containing the branch. If this happens,
815 // there is a vreg available containing the result of the compare. I'm
816 // not sure we can do much, as we've lost the predicate information with
817 // the compare instruction -- we have a 4-bit CR but don't know which bit
818 // to test here.
819 return false;
820}
821
822// Attempt to emit a compare of the two source values. Signed and unsigned
823// comparisons are supported. Return false if we can't handle it.
824bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
825 bool IsZExt, unsigned DestReg) {
826 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000827 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000828 if (!SrcEVT.isSimple())
829 return false;
830 MVT SrcVT = SrcEVT.getSimpleVT();
831
Eric Christopher1b8e7632014-05-22 01:07:24 +0000832 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000833 return false;
834
Bill Schmidt03008132013-08-25 22:33:42 +0000835 // See if operand 2 is an immediate encodeable in the compare.
836 // FIXME: Operands are not in canonical order at -O0, so an immediate
837 // operand in position 1 is a lost opportunity for now. We are
838 // similar to ARM in this regard.
839 long Imm = 0;
840 bool UseImm = false;
841
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000842 // Only 16-bit integer constants can be represented in compares for
Bill Schmidt03008132013-08-25 22:33:42 +0000843 // PowerPC. Others will be materialized into a register.
844 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
845 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
846 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
847 const APInt &CIVal = ConstInt->getValue();
848 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
849 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
850 UseImm = true;
851 }
852 }
853
854 unsigned CmpOpc;
855 bool NeedsExt = false;
856 switch (SrcVT.SimpleTy) {
857 default: return false;
858 case MVT::f32:
859 CmpOpc = PPC::FCMPUS;
860 break;
861 case MVT::f64:
862 CmpOpc = PPC::FCMPUD;
863 break;
864 case MVT::i1:
865 case MVT::i8:
866 case MVT::i16:
867 NeedsExt = true;
868 // Intentional fall-through.
869 case MVT::i32:
870 if (!UseImm)
871 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
872 else
873 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
874 break;
875 case MVT::i64:
876 if (!UseImm)
877 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
878 else
879 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
880 break;
881 }
882
883 unsigned SrcReg1 = getRegForValue(SrcValue1);
884 if (SrcReg1 == 0)
885 return false;
886
887 unsigned SrcReg2 = 0;
888 if (!UseImm) {
889 SrcReg2 = getRegForValue(SrcValue2);
890 if (SrcReg2 == 0)
891 return false;
892 }
893
894 if (NeedsExt) {
895 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
896 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
897 return false;
898 SrcReg1 = ExtReg;
899
900 if (!UseImm) {
901 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
902 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
903 return false;
904 SrcReg2 = ExtReg;
905 }
906 }
907
908 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000910 .addReg(SrcReg1).addReg(SrcReg2);
911 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000913 .addReg(SrcReg1).addImm(Imm);
914
915 return true;
916}
917
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000918// Attempt to fast-select a floating-point extend instruction.
919bool PPCFastISel::SelectFPExt(const Instruction *I) {
920 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000921 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
922 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000923
924 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
925 return false;
926
927 unsigned SrcReg = getRegForValue(Src);
928 if (!SrcReg)
929 return false;
930
931 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000932 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000933 return true;
934}
935
936// Attempt to fast-select a floating-point truncate instruction.
937bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
938 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000939 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
940 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000941
942 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
943 return false;
944
945 unsigned SrcReg = getRegForValue(Src);
946 if (!SrcReg)
947 return false;
948
949 // Round the result to single precision.
950 unsigned DestReg = createResultReg(&PPC::F4RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000951 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000952 .addReg(SrcReg);
953
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000954 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000955 return true;
956}
957
958// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +0000959// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000960// those should be used instead of moving via a stack slot when the
961// subtarget permits.
962// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
963// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
964// case to 8 bytes which produces tighter code but wastes stack space.
965unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
966 bool IsSigned) {
967
968 // If necessary, extend 32-bit int to 64-bit.
969 if (SrcVT == MVT::i32) {
970 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
971 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
972 return 0;
973 SrcReg = TmpReg;
974 }
975
976 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
977 Address Addr;
978 Addr.BaseType = Address::FrameIndexBase;
979 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
980
981 // Store the value from the GPR.
982 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
983 return 0;
984
985 // Load the integer value into an FPR. The kind of load used depends
986 // on a number of conditions.
987 unsigned LoadOpc = PPC::LFD;
988
989 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +0000990 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000991 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000992 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000993 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000994 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +0000995 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +0000996 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000997 }
998
999 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1000 unsigned ResultReg = 0;
1001 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1002 return 0;
1003
1004 return ResultReg;
1005}
1006
1007// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001008// FIXME: Once fast-isel has better support for VSX, conversions using
1009// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001010bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1011 MVT DstVT;
1012 Type *DstTy = I->getType();
1013 if (!isTypeLegal(DstTy, DstVT))
1014 return false;
1015
1016 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1017 return false;
1018
1019 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001020 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001021 if (!SrcEVT.isSimple())
1022 return false;
1023
1024 MVT SrcVT = SrcEVT.getSimpleVT();
1025
1026 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1027 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1028 return false;
1029
1030 unsigned SrcReg = getRegForValue(Src);
1031 if (SrcReg == 0)
1032 return false;
1033
1034 // We can only lower an unsigned convert if we have the newer
1035 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001036 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001037 return false;
1038
1039 // FIXME: For now we require the newer floating-point conversion operations
1040 // (which are present only on P7 and A2 server models) when converting
1041 // to single-precision float. Otherwise we have to generate a lot of
1042 // fiddly code to avoid double rounding. If necessary, the fiddly code
1043 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001044 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001045 return false;
1046
1047 // Extend the input if necessary.
1048 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1049 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1050 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1051 return false;
1052 SrcVT = MVT::i64;
1053 SrcReg = TmpReg;
1054 }
1055
1056 // Move the integer value to an FPR.
1057 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1058 if (FPReg == 0)
1059 return false;
1060
1061 // Determine the opcode for the conversion.
1062 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1063 unsigned DestReg = createResultReg(RC);
1064 unsigned Opc;
1065
1066 if (DstVT == MVT::f32)
1067 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1068 else
1069 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1070
1071 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001072 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001073 .addReg(FPReg);
1074
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001075 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001076 return true;
1077}
1078
1079// Move the floating-point value in SrcReg into an integer destination
1080// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001081// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001082// those should be used instead of moving via a stack slot when the
1083// subtarget permits.
1084unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1085 unsigned SrcReg, bool IsSigned) {
1086 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1087 // Note that if have STFIWX available, we could use a 4-byte stack
1088 // slot for i32, but this being fast-isel we'll just go with the
1089 // easiest code gen possible.
1090 Address Addr;
1091 Addr.BaseType = Address::FrameIndexBase;
1092 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1093
1094 // Store the value from the FPR.
1095 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1096 return 0;
1097
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001098 // Reload it into a GPR. If we want an i32 on big endian, modify the
1099 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001100 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001101 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001102
1103 // Look at the currently assigned register for this instruction
1104 // to determine the required register class.
1105 unsigned AssignedReg = FuncInfo.ValueMap[I];
1106 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001107 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001108
1109 unsigned ResultReg = 0;
1110 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1111 return 0;
1112
1113 return ResultReg;
1114}
1115
1116// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001117// FIXME: Once fast-isel has better support for VSX, conversions using
1118// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001119bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1120 MVT DstVT, SrcVT;
1121 Type *DstTy = I->getType();
1122 if (!isTypeLegal(DstTy, DstVT))
1123 return false;
1124
1125 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1126 return false;
1127
Bill Schmidt83973ef2014-06-24 20:05:18 +00001128 // If we don't have FCTIDUZ and we need it, punt to SelectionDAG.
1129 if (DstVT == MVT::i64 && !IsSigned && !PPCSubTarget->hasFPCVT())
1130 return false;
1131
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001132 Value *Src = I->getOperand(0);
1133 Type *SrcTy = Src->getType();
1134 if (!isTypeLegal(SrcTy, SrcVT))
1135 return false;
1136
1137 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1138 return false;
1139
1140 unsigned SrcReg = getRegForValue(Src);
1141 if (SrcReg == 0)
1142 return false;
1143
1144 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001145 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001146 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1147 if (InRC == &PPC::F4RCRegClass) {
1148 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001150 TII.get(TargetOpcode::COPY), TmpReg)
1151 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001152 SrcReg = TmpReg;
1153 }
1154
1155 // Determine the opcode for the conversion, which takes place
1156 // entirely within FPRs.
1157 unsigned DestReg = createResultReg(&PPC::F8RCRegClass);
1158 unsigned Opc;
1159
1160 if (DstVT == MVT::i32)
1161 if (IsSigned)
1162 Opc = PPC::FCTIWZ;
1163 else
Eric Christopher1b8e7632014-05-22 01:07:24 +00001164 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001165 else
1166 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1167
1168 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001169 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001170 .addReg(SrcReg);
1171
1172 // Now move the integer value from a float register to an integer register.
1173 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1174 if (IntReg == 0)
1175 return false;
1176
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001177 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001178 return true;
1179}
1180
Bill Schmidtccecf262013-08-30 02:29:45 +00001181// Attempt to fast-select a binary integer operation that isn't already
1182// handled automatically.
1183bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001184 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001185
1186 // We can get here in the case when we have a binary operation on a non-legal
1187 // type and the target independent selector doesn't know how to handle it.
1188 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1189 return false;
1190
1191 // Look at the currently assigned register for this instruction
1192 // to determine the required register class. If there is no register,
1193 // make a conservative choice (don't assign R0).
1194 unsigned AssignedReg = FuncInfo.ValueMap[I];
1195 const TargetRegisterClass *RC =
1196 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1197 &PPC::GPRC_and_GPRC_NOR0RegClass);
1198 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1199
1200 unsigned Opc;
1201 switch (ISDOpcode) {
1202 default: return false;
1203 case ISD::ADD:
1204 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1205 break;
1206 case ISD::OR:
1207 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1208 break;
1209 case ISD::SUB:
1210 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1211 break;
1212 }
1213
1214 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1215 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1216 if (SrcReg1 == 0) return false;
1217
1218 // Handle case of small immediate operand.
1219 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1220 const APInt &CIVal = ConstInt->getValue();
1221 int Imm = (int)CIVal.getSExtValue();
1222 bool UseImm = true;
1223 if (isInt<16>(Imm)) {
1224 switch (Opc) {
1225 default:
1226 llvm_unreachable("Missing case!");
1227 case PPC::ADD4:
1228 Opc = PPC::ADDI;
1229 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1230 break;
1231 case PPC::ADD8:
1232 Opc = PPC::ADDI8;
1233 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1234 break;
1235 case PPC::OR:
1236 Opc = PPC::ORI;
1237 break;
1238 case PPC::OR8:
1239 Opc = PPC::ORI8;
1240 break;
1241 case PPC::SUBF:
1242 if (Imm == -32768)
1243 UseImm = false;
1244 else {
1245 Opc = PPC::ADDI;
1246 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1247 Imm = -Imm;
1248 }
1249 break;
1250 case PPC::SUBF8:
1251 if (Imm == -32768)
1252 UseImm = false;
1253 else {
1254 Opc = PPC::ADDI8;
1255 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1256 Imm = -Imm;
1257 }
1258 break;
1259 }
1260
1261 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1263 ResultReg)
1264 .addReg(SrcReg1)
1265 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001266 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001267 return true;
1268 }
1269 }
1270 }
1271
1272 // Reg-reg case.
1273 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1274 if (SrcReg2 == 0) return false;
1275
1276 // Reverse operands for subtract-from.
1277 if (ISDOpcode == ISD::SUB)
1278 std::swap(SrcReg1, SrcReg2);
1279
Rafael Espindolaea09c592014-02-18 22:05:46 +00001280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001281 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001282 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001283 return true;
1284}
1285
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001286// Handle arguments to a call that we're attempting to fast-select.
1287// Return false if the arguments are too complex for us at the moment.
1288bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1289 SmallVectorImpl<unsigned> &ArgRegs,
1290 SmallVectorImpl<MVT> &ArgVTs,
1291 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1292 SmallVectorImpl<unsigned> &RegArgs,
1293 CallingConv::ID CC,
1294 unsigned &NumBytes,
1295 bool IsVarArg) {
1296 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001297 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001298
1299 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001300 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001301 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001302
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001303 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1304
1305 // Bail out if we can't handle any of the arguments.
1306 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1307 CCValAssign &VA = ArgLocs[I];
1308 MVT ArgVT = ArgVTs[VA.getValNo()];
1309
1310 // Skip vector arguments for now, as well as long double and
1311 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001312 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001313 !VA.isRegLoc() || VA.needsCustom())
1314 return false;
1315
1316 // Skip bit-converted arguments for now.
1317 if (VA.getLocInfo() == CCValAssign::BCvt)
1318 return false;
1319 }
1320
1321 // Get a count of how many bytes are to be pushed onto the stack.
1322 NumBytes = CCInfo.getNextStackOffset();
1323
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001324 // The prolog code of the callee may store up to 8 GPR argument registers to
1325 // the stack, allowing va_start to index over them in memory if its varargs.
1326 // Because we cannot tell if this is needed on the caller side, we have to
1327 // conservatively assume that it is needed. As such, make sure we have at
1328 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001329 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001330 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001331
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001332 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001334 TII.get(TII.getCallFrameSetupOpcode()))
Serge Pavlovd526b132017-05-09 13:35:13 +00001335 .addImm(NumBytes).addImm(0);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001336
1337 // Prepare to assign register arguments. Every argument uses up a
1338 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001339 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001340 unsigned NextGPR = PPC::X3;
1341 unsigned NextFPR = PPC::F1;
1342
1343 // Process arguments.
1344 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1345 CCValAssign &VA = ArgLocs[I];
1346 unsigned Arg = ArgRegs[VA.getValNo()];
1347 MVT ArgVT = ArgVTs[VA.getValNo()];
1348
1349 // Handle argument promotion and bitcasts.
1350 switch (VA.getLocInfo()) {
1351 default:
1352 llvm_unreachable("Unknown loc info!");
1353 case CCValAssign::Full:
1354 break;
1355 case CCValAssign::SExt: {
1356 MVT DestVT = VA.getLocVT();
1357 const TargetRegisterClass *RC =
1358 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1359 unsigned TmpReg = createResultReg(RC);
1360 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1361 llvm_unreachable("Failed to emit a sext!");
1362 ArgVT = DestVT;
1363 Arg = TmpReg;
1364 break;
1365 }
1366 case CCValAssign::AExt:
1367 case CCValAssign::ZExt: {
1368 MVT DestVT = VA.getLocVT();
1369 const TargetRegisterClass *RC =
1370 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1371 unsigned TmpReg = createResultReg(RC);
1372 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1373 llvm_unreachable("Failed to emit a zext!");
1374 ArgVT = DestVT;
1375 Arg = TmpReg;
1376 break;
1377 }
1378 case CCValAssign::BCvt: {
1379 // FIXME: Not yet handled.
1380 llvm_unreachable("Should have bailed before getting here!");
1381 break;
1382 }
1383 }
1384
1385 // Copy this argument to the appropriate register.
1386 unsigned ArgReg;
1387 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1388 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001389 if (CC != CallingConv::Fast)
1390 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001391 } else
1392 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001393
1394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1395 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001396 RegArgs.push_back(ArgReg);
1397 }
1398
1399 return true;
1400}
1401
1402// For a call that we've determined we can fast-select, finish the
1403// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001404bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1405 CallingConv::ID CC = CLI.CallConv;
1406
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001407 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001409 TII.get(TII.getCallFrameDestroyOpcode()))
1410 .addImm(NumBytes).addImm(0);
1411
1412 // Next, generate a copy to obtain the return value.
1413 // FIXME: No multi-register return values yet, though I don't foresee
1414 // any real difficulties there.
1415 if (RetVT != MVT::isVoid) {
1416 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001417 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001418 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1419 CCValAssign &VA = RVLocs[0];
1420 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1421 assert(VA.isRegLoc() && "Can only return in registers!");
1422
1423 MVT DestVT = VA.getValVT();
1424 MVT CopyVT = DestVT;
1425
1426 // Ints smaller than a register still arrive in a full 64-bit
1427 // register, so make sure we recognize this.
1428 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1429 CopyVT = MVT::i64;
1430
1431 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001432 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001433
1434 if (RetVT == CopyVT) {
1435 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1436 ResultReg = createResultReg(CpyRC);
1437
Rafael Espindolaea09c592014-02-18 22:05:46 +00001438 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001439 TII.get(TargetOpcode::COPY), ResultReg)
1440 .addReg(SourcePhysReg);
1441
1442 // If necessary, round the floating result to single precision.
1443 } else if (CopyVT == MVT::f64) {
1444 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001446 ResultReg).addReg(SourcePhysReg);
1447
1448 // If only the low half of a general register is needed, generate
1449 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1450 // used along the fast-isel path (not lowered), and downstream logic
1451 // also doesn't like a direct subreg copy on a physical reg.)
1452 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1453 ResultReg = createResultReg(&PPC::GPRCRegClass);
1454 // Convert physical register from G8RC to GPRC.
1455 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001457 TII.get(TargetOpcode::COPY), ResultReg)
1458 .addReg(SourcePhysReg);
1459 }
1460
Bill Schmidt0954ea12013-08-30 23:25:30 +00001461 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001462 CLI.InRegs.push_back(SourcePhysReg);
1463 CLI.ResultReg = ResultReg;
1464 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001465 }
Hal Finkel934361a2015-01-14 01:07:51 +00001466
1467 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001468}
1469
Hal Finkel934361a2015-01-14 01:07:51 +00001470bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1471 CallingConv::ID CC = CLI.CallConv;
1472 bool IsTailCall = CLI.IsTailCall;
1473 bool IsVarArg = CLI.IsVarArg;
1474 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001475 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001476
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001477 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001478 return false;
1479
1480 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001481 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001482 return false;
1483
Hal Finkel934361a2015-01-14 01:07:51 +00001484 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001485 if (IsVarArg)
1486 return false;
1487
1488 // Handle simple calls for now, with legal return types and
1489 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001490 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001491 MVT RetVT;
1492 if (RetTy->isVoidTy())
1493 RetVT = MVT::isVoid;
1494 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1495 RetVT != MVT::i8)
1496 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001497 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1498 // We can't handle boolean returns when CR bits are in use.
1499 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001500
1501 // FIXME: No multi-register return values yet.
1502 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1503 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1504 RetVT != MVT::f64) {
1505 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001506 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001507 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1508 if (RVLocs.size() > 1)
1509 return false;
1510 }
1511
1512 // Bail early if more than 8 arguments, as we only currently
1513 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001514 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001515 if (NumArgs > 8)
1516 return false;
1517
1518 // Set up the argument vectors.
1519 SmallVector<Value*, 8> Args;
1520 SmallVector<unsigned, 8> ArgRegs;
1521 SmallVector<MVT, 8> ArgVTs;
1522 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1523
1524 Args.reserve(NumArgs);
1525 ArgRegs.reserve(NumArgs);
1526 ArgVTs.reserve(NumArgs);
1527 ArgFlags.reserve(NumArgs);
1528
Hal Finkel934361a2015-01-14 01:07:51 +00001529 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001530 // Only handle easy calls for now. It would be reasonably easy
1531 // to handle <= 8-byte structures passed ByVal in registers, but we
1532 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001533 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1534 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001535 return false;
1536
Hal Finkel934361a2015-01-14 01:07:51 +00001537 Value *ArgValue = CLI.OutVals[i];
1538 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001539 MVT ArgVT;
1540 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1541 return false;
1542
1543 if (ArgVT.isVector())
1544 return false;
1545
Hal Finkel934361a2015-01-14 01:07:51 +00001546 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001547 if (Arg == 0)
1548 return false;
1549
Hal Finkel934361a2015-01-14 01:07:51 +00001550 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001551 ArgRegs.push_back(Arg);
1552 ArgVTs.push_back(ArgVT);
1553 ArgFlags.push_back(Flags);
1554 }
1555
1556 // Process the arguments.
1557 SmallVector<unsigned, 8> RegArgs;
1558 unsigned NumBytes;
1559
1560 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1561 RegArgs, CC, NumBytes, IsVarArg))
1562 return false;
1563
Hal Finkel934361a2015-01-14 01:07:51 +00001564 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001565 // FIXME: No handling for function pointers yet. This requires
1566 // implementing the function descriptor (OPD) setup.
1567 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001568 if (!GV) {
1569 // patchpoints are a special case; they always dispatch to a pointer value.
1570 // However, we don't actually want to generate the indirect call sequence
1571 // here (that will be generated, as necessary, during asm printing), and
1572 // the call we generate here will be erased by FastISel::selectPatchpoint,
1573 // so don't try very hard...
1574 if (CLI.IsPatchPoint)
1575 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1576 else
1577 return false;
1578 } else {
1579 // Build direct call with NOP for TOC restore.
1580 // FIXME: We can and should optimize away the NOP for local calls.
1581 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1582 TII.get(PPC::BL8_NOP));
1583 // Add callee.
1584 MIB.addGlobalAddress(GV);
1585 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001586
1587 // Add implicit physical register uses to the call.
1588 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1589 MIB.addReg(RegArgs[II], RegState::Implicit);
1590
Hal Finkelaf519932015-01-19 07:20:27 +00001591 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1592 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001593 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001594 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001595
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001596 // Add a register mask with the call-preserved registers. Proper
1597 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001598 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001599
Hal Finkel934361a2015-01-14 01:07:51 +00001600 CLI.Call = MIB;
1601
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001602 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001603 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001604}
1605
Bill Schmidtd89f6782013-08-26 19:42:51 +00001606// Attempt to fast-select a return instruction.
1607bool PPCFastISel::SelectRet(const Instruction *I) {
1608
1609 if (!FuncInfo.CanLowerReturn)
1610 return false;
1611
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001612 if (TLI.supportSplitCSR(FuncInfo.MF))
1613 return false;
1614
Bill Schmidtd89f6782013-08-26 19:42:51 +00001615 const ReturnInst *Ret = cast<ReturnInst>(I);
1616 const Function &F = *I->getParent()->getParent();
1617
1618 // Build a list of return value registers.
1619 SmallVector<unsigned, 4> RetRegs;
1620 CallingConv::ID CC = F.getCallingConv();
1621
1622 if (Ret->getNumOperands() > 0) {
1623 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001624 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001625
1626 // Analyze operands of the call, assigning locations to each operand.
1627 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001628 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001629 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1630 const Value *RV = Ret->getOperand(0);
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001631
Bill Schmidtd89f6782013-08-26 19:42:51 +00001632 // FIXME: Only one output register for now.
1633 if (ValLocs.size() > 1)
1634 return false;
1635
Eric Christopherf0024d12015-07-25 00:48:08 +00001636 // Special case for returning a constant integer of any size - materialize
1637 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001638 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001639 CCValAssign &VA = ValLocs[0];
1640
1641 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001642 // We still need to worry about properly extending the sign. For example,
1643 // we could have only a single bit or a constant that needs zero
1644 // extension rather than sign extension. Make sure we pass the return
1645 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001646 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001647 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001648
Rafael Espindolaea09c592014-02-18 22:05:46 +00001649 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001650 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1651
Bill Schmidtd89f6782013-08-26 19:42:51 +00001652 RetRegs.push_back(RetReg);
1653
1654 } else {
1655 unsigned Reg = getRegForValue(RV);
1656
1657 if (Reg == 0)
1658 return false;
1659
1660 // Copy the result values into the output registers.
1661 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1662
1663 CCValAssign &VA = ValLocs[i];
1664 assert(VA.isRegLoc() && "Can only return in registers!");
1665 RetRegs.push_back(VA.getLocReg());
1666 unsigned SrcReg = Reg + VA.getValNo();
1667
Mehdi Amini44ede332015-07-09 02:09:04 +00001668 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001669 if (!RVEVT.isSimple())
1670 return false;
1671 MVT RVVT = RVEVT.getSimpleVT();
1672 MVT DestVT = VA.getLocVT();
1673
1674 if (RVVT != DestVT && RVVT != MVT::i8 &&
1675 RVVT != MVT::i16 && RVVT != MVT::i32)
1676 return false;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001677
Bill Schmidtd89f6782013-08-26 19:42:51 +00001678 if (RVVT != DestVT) {
1679 switch (VA.getLocInfo()) {
1680 default:
1681 llvm_unreachable("Unknown loc info!");
1682 case CCValAssign::Full:
1683 llvm_unreachable("Full value assign but types don't match?");
1684 case CCValAssign::AExt:
1685 case CCValAssign::ZExt: {
1686 const TargetRegisterClass *RC =
1687 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1688 unsigned TmpReg = createResultReg(RC);
1689 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1690 return false;
1691 SrcReg = TmpReg;
1692 break;
1693 }
1694 case CCValAssign::SExt: {
1695 const TargetRegisterClass *RC =
1696 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1697 unsigned TmpReg = createResultReg(RC);
1698 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1699 return false;
1700 SrcReg = TmpReg;
1701 break;
1702 }
1703 }
1704 }
1705
Rafael Espindolaea09c592014-02-18 22:05:46 +00001706 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001707 TII.get(TargetOpcode::COPY), RetRegs[i])
1708 .addReg(SrcReg);
1709 }
1710 }
1711 }
1712
Rafael Espindolaea09c592014-02-18 22:05:46 +00001713 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001714 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001715
1716 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1717 MIB.addReg(RetRegs[i], RegState::Implicit);
1718
1719 return true;
1720}
1721
Bill Schmidt03008132013-08-25 22:33:42 +00001722// Attempt to emit an integer extend of SrcReg into DestReg. Both
1723// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001724// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001725bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1726 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001727 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1728 return false;
1729 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1730 return false;
1731
1732 // Signed extensions use EXTSB, EXTSH, EXTSW.
1733 if (!IsZExt) {
1734 unsigned Opc;
1735 if (SrcVT == MVT::i8)
1736 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1737 else if (SrcVT == MVT::i16)
1738 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1739 else {
1740 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1741 Opc = PPC::EXTSW_32_64;
1742 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001744 .addReg(SrcReg);
1745
1746 // Unsigned 32-bit extensions use RLWINM.
1747 } else if (DestVT == MVT::i32) {
1748 unsigned MB;
1749 if (SrcVT == MVT::i8)
1750 MB = 24;
1751 else {
1752 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1753 MB = 16;
1754 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001755 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001756 DestReg)
1757 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1758
1759 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1760 } else {
1761 unsigned MB;
1762 if (SrcVT == MVT::i8)
1763 MB = 56;
1764 else if (SrcVT == MVT::i16)
1765 MB = 48;
1766 else
1767 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001769 TII.get(PPC::RLDICL_32_64), DestReg)
1770 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1771 }
1772
1773 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001774}
1775
1776// Attempt to fast-select an indirect branch instruction.
1777bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1778 unsigned AddrReg = getRegForValue(I->getOperand(0));
1779 if (AddrReg == 0)
1780 return false;
1781
Rafael Espindolaea09c592014-02-18 22:05:46 +00001782 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001783 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001784 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001785
1786 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001787 for (const BasicBlock *SuccBB : IB->successors())
1788 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001789
1790 return true;
1791}
1792
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001793// Attempt to fast-select an integer truncate instruction.
1794bool PPCFastISel::SelectTrunc(const Instruction *I) {
1795 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001796 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1797 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001798
1799 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1800 return false;
1801
1802 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1803 return false;
1804
1805 unsigned SrcReg = getRegForValue(Src);
1806 if (!SrcReg)
1807 return false;
1808
1809 // The only interesting case is when we need to switch register classes.
1810 if (SrcVT == MVT::i64) {
1811 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1813 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001814 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1815 SrcReg = ResultReg;
1816 }
1817
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001818 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001819 return true;
1820}
1821
Bill Schmidtd89f6782013-08-26 19:42:51 +00001822// Attempt to fast-select an integer extend instruction.
1823bool PPCFastISel::SelectIntExt(const Instruction *I) {
1824 Type *DestTy = I->getType();
1825 Value *Src = I->getOperand(0);
1826 Type *SrcTy = Src->getType();
1827
1828 bool IsZExt = isa<ZExtInst>(I);
1829 unsigned SrcReg = getRegForValue(Src);
1830 if (!SrcReg) return false;
1831
1832 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001833 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1834 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001835 if (!SrcEVT.isSimple())
1836 return false;
1837 if (!DestEVT.isSimple())
1838 return false;
1839
1840 MVT SrcVT = SrcEVT.getSimpleVT();
1841 MVT DestVT = DestEVT.getSimpleVT();
1842
1843 // If we know the register class needed for the result of this
1844 // instruction, use it. Otherwise pick the register class of the
1845 // correct size that does not contain X0/R0, since we don't know
1846 // whether downstream uses permit that assignment.
1847 unsigned AssignedReg = FuncInfo.ValueMap[I];
1848 const TargetRegisterClass *RC =
1849 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1850 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1851 &PPC::GPRC_and_GPRC_NOR0RegClass));
1852 unsigned ResultReg = createResultReg(RC);
1853
1854 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1855 return false;
1856
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001857 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001858 return true;
1859}
1860
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001861// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001862// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001863bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001864
1865 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001866 case Instruction::Load:
1867 return SelectLoad(I);
1868 case Instruction::Store:
1869 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001870 case Instruction::Br:
1871 return SelectBranch(I);
1872 case Instruction::IndirectBr:
1873 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001874 case Instruction::FPExt:
1875 return SelectFPExt(I);
1876 case Instruction::FPTrunc:
1877 return SelectFPTrunc(I);
1878 case Instruction::SIToFP:
1879 return SelectIToFP(I, /*IsSigned*/ true);
1880 case Instruction::UIToFP:
1881 return SelectIToFP(I, /*IsSigned*/ false);
1882 case Instruction::FPToSI:
1883 return SelectFPToI(I, /*IsSigned*/ true);
1884 case Instruction::FPToUI:
1885 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001886 case Instruction::Add:
1887 return SelectBinaryIntOp(I, ISD::ADD);
1888 case Instruction::Or:
1889 return SelectBinaryIntOp(I, ISD::OR);
1890 case Instruction::Sub:
1891 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001892 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001893 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001894 case Instruction::Ret:
1895 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001896 case Instruction::Trunc:
1897 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001898 case Instruction::ZExt:
1899 case Instruction::SExt:
1900 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001901 // Here add other flavors of Instruction::XXX that automated
1902 // cases don't catch. For example, switches are terminators
1903 // that aren't yet handled.
1904 default:
1905 break;
1906 }
1907 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001908}
1909
1910// Materialize a floating-point constant into a register, and return
1911// the register number (or zero if we failed to handle it).
1912unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1913 // No plans to handle long double here.
1914 if (VT != MVT::f32 && VT != MVT::f64)
1915 return 0;
1916
1917 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001918 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001919 assert(Align > 0 && "Unexpectedly missing alignment information!");
1920 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Ulrich Weigandc3b495a2016-08-05 15:22:05 +00001921 const TargetRegisterClass *RC =
1922 (VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass;
1923 unsigned DestReg = createResultReg(RC);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001924 CodeModel::Model CModel = TM.getCodeModel();
1925
Alex Lorenze40c8a22015-08-11 23:09:45 +00001926 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1927 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
1928 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001929
Bill Schmidt03008132013-08-25 22:33:42 +00001930 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1931 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1932
Hal Finkele6698d52015-02-01 15:03:28 +00001933 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00001934 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
Rafael Espindola79e238a2017-08-03 02:16:21 +00001935 if (CModel == CodeModel::Small) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001936 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00001937 TmpReg)
1938 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001939 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00001940 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
1941 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001942 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001944 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00001945 // But for large code model, we must generate a LDtocL followed
1946 // by the LF[SD].
1947 if (CModel == CodeModel::Large) {
1948 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001949 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00001950 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001951 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001952 .addImm(0)
1953 .addReg(TmpReg2);
1954 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00001956 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
1957 .addReg(TmpReg)
1958 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001959 }
1960
1961 return DestReg;
1962}
1963
Bill Schmidtccecf262013-08-30 02:29:45 +00001964// Materialize the address of a global value into a register, and return
1965// the register number (or zero if we failed to handle it).
1966unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
1967 assert(VT == MVT::i64 && "Non-address!");
1968 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
1969 unsigned DestReg = createResultReg(RC);
1970
1971 // Global values may be plain old object addresses, TLS object
1972 // addresses, constant pool entries, or jump tables. How we generate
1973 // code for these may depend on small, medium, or large code model.
1974 CodeModel::Model CModel = TM.getCodeModel();
1975
1976 // FIXME: Jump tables are not yet required because fast-isel doesn't
1977 // handle switches; if that changes, we need them as well. For now,
1978 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00001979
1980 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001981 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00001982 return 0;
1983
Hal Finkele6698d52015-02-01 15:03:28 +00001984 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00001985 // For small code model, generate a simple TOC load.
Rafael Espindola79e238a2017-08-03 02:16:21 +00001986 if (CModel == CodeModel::Small)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001987 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
1988 DestReg)
1989 .addGlobalAddress(GV)
1990 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00001991 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00001992 // If the address is an externally defined symbol, a symbol with common
1993 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00001994 // jump table address (not yet needed), or if we are generating code
1995 // for large code model, we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001996 // LDtocL(GV, ADDIStocHA(%x2, GV))
Bill Schmidtccecf262013-08-30 02:29:45 +00001997 // Otherwise we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00001998 // ADDItocL(ADDIStocHA(%x2, GV), GV)
Bill Schmidtccecf262013-08-30 02:29:45 +00001999 // Either way, start with the ADDIStocHA:
2000 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002001 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00002002 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2003
Eric Christopherc1808362015-11-20 20:51:31 +00002004 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2005 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002007 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00002008 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00002009 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002011 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00002012 }
Bill Schmidtccecf262013-08-30 02:29:45 +00002013 }
2014
2015 return DestReg;
2016}
2017
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002018// Materialize a 32-bit integer constant into a register, and return
2019// the register number (or zero if we failed to handle it).
2020unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2021 const TargetRegisterClass *RC) {
2022 unsigned Lo = Imm & 0xFFFF;
2023 unsigned Hi = (Imm >> 16) & 0xFFFF;
2024
2025 unsigned ResultReg = createResultReg(RC);
2026 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2027
2028 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002029 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002030 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2031 .addImm(Imm);
2032 else if (Lo) {
2033 // Both Lo and Hi have nonzero bits.
2034 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002035 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002036 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2037 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002039 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2040 .addReg(TmpReg).addImm(Lo);
2041 } else
2042 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002043 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002044 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002045 .addImm(Hi);
2046
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002047 return ResultReg;
2048}
2049
2050// Materialize a 64-bit integer constant into a register, and return
2051// the register number (or zero if we failed to handle it).
2052unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2053 const TargetRegisterClass *RC) {
2054 unsigned Remainder = 0;
2055 unsigned Shift = 0;
2056
2057 // If the value doesn't fit in 32 bits, see if we can shift it
2058 // so that it fits in 32 bits.
2059 if (!isInt<32>(Imm)) {
2060 Shift = countTrailingZeros<uint64_t>(Imm);
2061 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2062
2063 if (isInt<32>(ImmSh))
2064 Imm = ImmSh;
2065 else {
2066 Remainder = Imm;
2067 Shift = 32;
2068 Imm >>= 32;
2069 }
2070 }
2071
2072 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2073 // (if not shifted).
2074 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2075 if (!Shift)
2076 return TmpReg1;
2077
2078 // If upper 32 bits were not zero, we've built them and need to shift
2079 // them into place.
2080 unsigned TmpReg2;
2081 if (Imm) {
2082 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002084 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2085 } else
2086 TmpReg2 = TmpReg1;
2087
2088 unsigned TmpReg3, Hi, Lo;
2089 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2090 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002092 TmpReg3).addReg(TmpReg2).addImm(Hi);
2093 } else
2094 TmpReg3 = TmpReg2;
2095
2096 if ((Lo = Remainder & 0xFFFF)) {
2097 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002098 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002099 ResultReg).addReg(TmpReg3).addImm(Lo);
2100 return ResultReg;
2101 }
2102
2103 return TmpReg3;
2104}
2105
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002106// Materialize an integer constant into a register, and return
2107// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002108unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2109 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002110 // If we're using CR bit registers for i1 values, handle that as a special
2111 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002112 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002113 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2114 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2115 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2116 return ImmReg;
2117 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002118
Eric Christopher80ba58a2016-01-29 07:19:49 +00002119 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2120 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002121 return 0;
2122
Eric Christopher80ba58a2016-01-29 07:19:49 +00002123 const TargetRegisterClass *RC =
2124 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002125 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002126
2127 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002128 // Since LI will sign extend the constant we need to make sure that for
2129 // our zeroext constants that the sign extended constant fits into 16-bits -
2130 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002131 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002132 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2133 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002134 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002135 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002136 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002137 }
2138
2139 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002140 if (VT == MVT::i64)
2141 return PPCMaterialize64BitInt(Imm, RC);
2142 else if (VT == MVT::i32)
2143 return PPCMaterialize32BitInt(Imm, RC);
2144
2145 return 0;
2146}
2147
2148// Materialize a constant into a register, and return the register
2149// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002150unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002151 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002152
2153 // Only handle simple types.
2154 if (!CEVT.isSimple()) return 0;
2155 MVT VT = CEVT.getSimpleVT();
2156
2157 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2158 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002159 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2160 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002161 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Hal Finkel73390c72016-09-04 06:07:19 +00002162 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2163 // assumes that constant PHI operands will be zero extended, and failure to
2164 // match that assumption will cause problems if we sign extend here but
2165 // some user of a PHI is in a block for which we fall back to full SDAG
2166 // instruction selection.
2167 return PPCMaterializeInt(CI, VT, false);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002168
2169 return 0;
2170}
2171
2172// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002173// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002174unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002175 // Don't handle dynamic allocas.
2176 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2177
2178 MVT VT;
2179 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2180
2181 DenseMap<const AllocaInst*, int>::iterator SI =
2182 FuncInfo.StaticAllocaMap.find(AI);
2183
2184 if (SI != FuncInfo.StaticAllocaMap.end()) {
2185 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002187 ResultReg).addFrameIndex(SI->second).addImm(0);
2188 return ResultReg;
2189 }
2190
2191 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002192}
2193
Bill Schmidtccecf262013-08-30 02:29:45 +00002194// Fold loads into extends when possible.
2195// FIXME: We can have multiple redundant extend/trunc instructions
2196// following a load. The folding only picks up one. Extend this
2197// to check subsequent instructions for the same pattern and remove
2198// them. Thus ResultReg should be the def reg for the last redundant
2199// instruction in a chain, and all intervening instructions can be
2200// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2201// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002202bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2203 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002204 // Verify we have a legal type before going any further.
2205 MVT VT;
2206 if (!isLoadTypeLegal(LI->getType(), VT))
2207 return false;
2208
2209 // Combine load followed by zero- or sign-extend.
2210 bool IsZExt = false;
2211 switch(MI->getOpcode()) {
2212 default:
2213 return false;
2214
2215 case PPC::RLDICL:
2216 case PPC::RLDICL_32_64: {
2217 IsZExt = true;
2218 unsigned MB = MI->getOperand(3).getImm();
2219 if ((VT == MVT::i8 && MB <= 56) ||
2220 (VT == MVT::i16 && MB <= 48) ||
2221 (VT == MVT::i32 && MB <= 32))
2222 break;
2223 return false;
2224 }
2225
2226 case PPC::RLWINM:
2227 case PPC::RLWINM8: {
2228 IsZExt = true;
2229 unsigned MB = MI->getOperand(3).getImm();
2230 if ((VT == MVT::i8 && MB <= 24) ||
2231 (VT == MVT::i16 && MB <= 16))
2232 break;
2233 return false;
2234 }
2235
2236 case PPC::EXTSB:
2237 case PPC::EXTSB8:
2238 case PPC::EXTSB8_32_64:
2239 /* There is no sign-extending load-byte instruction. */
2240 return false;
2241
2242 case PPC::EXTSH:
2243 case PPC::EXTSH8:
2244 case PPC::EXTSH8_32_64: {
2245 if (VT != MVT::i16 && VT != MVT::i8)
2246 return false;
2247 break;
2248 }
2249
2250 case PPC::EXTSW:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002251 case PPC::EXTSW_32:
Bill Schmidtccecf262013-08-30 02:29:45 +00002252 case PPC::EXTSW_32_64: {
2253 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2254 return false;
2255 break;
2256 }
2257 }
2258
2259 // See if we can handle this address.
2260 Address Addr;
2261 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2262 return false;
2263
2264 unsigned ResultReg = MI->getOperand(0).getReg();
2265
Craig Topper062a2ba2014-04-25 05:30:21 +00002266 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt))
Bill Schmidtccecf262013-08-30 02:29:45 +00002267 return false;
2268
2269 MI->eraseFromParent();
2270 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002271}
2272
2273// Attempt to lower call arguments in a faster way than done by
2274// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002275bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002276 // Defer to normal argument lowering for now. It's reasonably
2277 // efficient. Consider doing something like ARM to handle the
2278 // case where all args fit in registers, no varargs, no float
2279 // or vector args.
2280 return false;
2281}
2282
Bill Schmidt03008132013-08-25 22:33:42 +00002283// Handle materializing integer constants into a register. This is not
2284// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002285unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002286
Bill Schmidt03008132013-08-25 22:33:42 +00002287 if (Opc != ISD::Constant)
2288 return 0;
2289
Hal Finkel940ab932014-02-28 00:27:01 +00002290 // If we're using CR bit registers for i1 values, handle that as a special
2291 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002292 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002293 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2295 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2296 return ImmReg;
2297 }
2298
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002299 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2300 VT != MVT::i1)
Bill Schmidt03008132013-08-25 22:33:42 +00002301 return 0;
2302
2303 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2304 &PPC::GPRCRegClass);
2305 if (VT == MVT::i64)
2306 return PPCMaterialize64BitInt(Imm, RC);
2307 else
2308 return PPCMaterialize32BitInt(Imm, RC);
2309}
2310
Bill Schmidtccecf262013-08-30 02:29:45 +00002311// Override for ADDI and ADDI8 to set the correct register class
2312// on RHS operand 0. The automatic infrastructure naively assumes
2313// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2314// for these cases. At the moment, none of the other automatically
2315// generated RI instructions require special treatment. However, once
2316// SelectSelect is implemented, "isel" requires similar handling.
2317//
2318// Also be conservative about the output register class. Avoid
2319// assigning R0 or X0 to the output register for GPRC and G8RC
2320// register classes, as any such result could be used in ADDI, etc.,
2321// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002322unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002323 const TargetRegisterClass *RC,
2324 unsigned Op0, bool Op0IsKill,
2325 uint64_t Imm) {
2326 if (MachineInstOpcode == PPC::ADDI)
2327 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2328 else if (MachineInstOpcode == PPC::ADDI8)
2329 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2330
2331 const TargetRegisterClass *UseRC =
2332 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2333 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2334
Juergen Ributzka88e32512014-09-03 20:56:59 +00002335 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002336 Op0, Op0IsKill, Imm);
2337}
2338
2339// Override for instructions with one register operand to avoid use of
2340// R0/X0. The automatic infrastructure isn't aware of the context so
2341// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002342unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002343 const TargetRegisterClass* RC,
2344 unsigned Op0, bool Op0IsKill) {
2345 const TargetRegisterClass *UseRC =
2346 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2347 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2348
Juergen Ributzka88e32512014-09-03 20:56:59 +00002349 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002350}
2351
2352// Override for instructions with two register operands to avoid use
2353// of R0/X0. The automatic infrastructure isn't aware of the context
2354// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002355unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002356 const TargetRegisterClass* RC,
2357 unsigned Op0, bool Op0IsKill,
2358 unsigned Op1, bool Op1IsKill) {
2359 const TargetRegisterClass *UseRC =
2360 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2361 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2362
Juergen Ributzka88e32512014-09-03 20:56:59 +00002363 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002364 Op1, Op1IsKill);
2365}
2366
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002367namespace llvm {
2368 // Create the fast instruction selector for PowerPC64 ELF.
2369 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2370 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002371 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002372 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002373 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002374 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002375 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002376 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002377}