blob: 3b2d92db78b97186a2f73a434b5855d040a59ddc [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,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000156 bool isZExt, unsigned DestReg,
157 const PPC::Predicate Pred);
Bill Schmidtccecf262013-08-30 02:29:45 +0000158 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
159 const TargetRegisterClass *RC, bool IsZExt = true,
160 unsigned FP64LoadOpc = PPC::LFD);
161 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
162 bool PPCComputeAddress(const Value *Obj, Address &Addr);
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000163 void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000164 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000165 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
166 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000167 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000168 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000169 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
170 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000171 unsigned PPCMaterialize32BitInt(int64_t Imm,
172 const TargetRegisterClass *RC);
173 unsigned PPCMaterialize64BitInt(int64_t Imm,
174 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000175 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
176 unsigned SrcReg, bool IsSigned);
177 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000178
Bill Schmidtd89f6782013-08-26 19:42:51 +0000179 // Call handling routines.
180 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000181 bool processCallArgs(SmallVectorImpl<Value*> &Args,
182 SmallVectorImpl<unsigned> &ArgRegs,
183 SmallVectorImpl<MVT> &ArgVTs,
184 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
185 SmallVectorImpl<unsigned> &RegArgs,
186 CallingConv::ID CC,
187 unsigned &NumBytes,
188 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000189 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Rafael Espindola463aed82016-06-21 20:09:22 +0000190 LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000191
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000192 private:
193 #include "PPCGenFastISel.inc"
194
195};
196
197} // end anonymous namespace
198
Bill Schmidtd89f6782013-08-26 19:42:51 +0000199#include "PPCGenCallingConv.inc"
200
Rafael Espindola463aed82016-06-21 20:09:22 +0000201// Function whose sole purpose is to kill compiler warnings
202// stemming from unused functions included from PPCGenCallingConv.inc.
203CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
204 if (Flag == 1)
205 return CC_PPC32_SVR4;
206 else if (Flag == 2)
207 return CC_PPC32_SVR4_ByVal;
208 else if (Flag == 3)
209 return CC_PPC32_SVR4_VarArg;
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000210 else if (Flag == 4)
211 return RetCC_PPC_Cold;
Rafael Espindola463aed82016-06-21 20:09:22 +0000212 else
213 return RetCC_PPC;
214}
215
Bill Schmidt03008132013-08-25 22:33:42 +0000216static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
217 switch (Pred) {
218 // These are not representable with any single compare.
219 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000220 case CmpInst::FCMP_TRUE:
221 // Major concern about the following 6 cases is NaN result. The comparison
222 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
223 // only one of which will be set. The result is generated by fcmpu
224 // instruction. However, bc instruction only inspects one of the first 3
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000225 // bits, so when un is set, bc instruction may jump to an undesired
Tim Shen5cdf7502016-03-17 22:27:58 +0000226 // place.
227 //
228 // More specifically, if we expect an unordered comparison and un is set, we
229 // expect to always go to true branch; in such case UEQ, UGT and ULT still
230 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
231 // since they are tested by inspecting !eq, !lt, !gt, respectively.
232 //
233 // Similarly, for ordered comparison, when un is set, we always expect the
234 // result to be false. In such case OGT, OLT and OEQ is good, since they are
235 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
236 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000237 case CmpInst::FCMP_UEQ:
238 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000239 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000240 case CmpInst::FCMP_OGE:
241 case CmpInst::FCMP_OLE:
242 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000243 default:
244 return Optional<PPC::Predicate>();
245
246 case CmpInst::FCMP_OEQ:
247 case CmpInst::ICMP_EQ:
248 return PPC::PRED_EQ;
249
250 case CmpInst::FCMP_OGT:
251 case CmpInst::ICMP_UGT:
252 case CmpInst::ICMP_SGT:
253 return PPC::PRED_GT;
254
Tim Shen5cdf7502016-03-17 22:27:58 +0000255 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000256 case CmpInst::ICMP_UGE:
257 case CmpInst::ICMP_SGE:
258 return PPC::PRED_GE;
259
260 case CmpInst::FCMP_OLT:
261 case CmpInst::ICMP_ULT:
262 case CmpInst::ICMP_SLT:
263 return PPC::PRED_LT;
264
Tim Shen5cdf7502016-03-17 22:27:58 +0000265 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000266 case CmpInst::ICMP_ULE:
267 case CmpInst::ICMP_SLE:
268 return PPC::PRED_LE;
269
Tim Shen5cdf7502016-03-17 22:27:58 +0000270 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000271 case CmpInst::ICMP_NE:
272 return PPC::PRED_NE;
273
274 case CmpInst::FCMP_ORD:
275 return PPC::PRED_NU;
276
277 case CmpInst::FCMP_UNO:
278 return PPC::PRED_UN;
279 }
280}
281
Bill Schmidtccecf262013-08-30 02:29:45 +0000282// Determine whether the type Ty is simple enough to be handled by
283// fast-isel, and return its equivalent machine type in VT.
284// FIXME: Copied directly from ARM -- factor into base class?
285bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000286 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000287
288 // Only handle simple types.
289 if (Evt == MVT::Other || !Evt.isSimple()) return false;
290 VT = Evt.getSimpleVT();
291
292 // Handle all legal types, i.e. a register that will directly hold this
293 // value.
294 return TLI.isTypeLegal(VT);
295}
296
297// Determine whether the type Ty is simple enough to be handled by
298// fast-isel as a load target, and return its equivalent machine type in VT.
299bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
300 if (isTypeLegal(Ty, VT)) return true;
301
302 // If this is a type than can be sign or zero-extended to a basic operation
303 // go ahead and accept it now.
304 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
305 return true;
306 }
307
308 return false;
309}
310
Hal Finkel5f2a1372015-05-23 12:18:10 +0000311bool PPCFastISel::isValueAvailable(const Value *V) const {
312 if (!isa<Instruction>(V))
313 return true;
314
315 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000316 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000317}
318
Bill Schmidtccecf262013-08-30 02:29:45 +0000319// Given a value Obj, create an Address object Addr that represents its
320// address. Return false if we can't handle it.
321bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000322 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000323 unsigned Opcode = Instruction::UserOp1;
324 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
325 // Don't walk into other basic blocks unless the object is an alloca from
326 // another block, otherwise it may not have a virtual register assigned.
327 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
328 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
329 Opcode = I->getOpcode();
330 U = I;
331 }
332 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
333 Opcode = C->getOpcode();
334 U = C;
335 }
336
337 switch (Opcode) {
338 default:
339 break;
340 case Instruction::BitCast:
341 // Look through bitcasts.
342 return PPCComputeAddress(U->getOperand(0), Addr);
343 case Instruction::IntToPtr:
344 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000345 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
346 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000347 return PPCComputeAddress(U->getOperand(0), Addr);
348 break;
349 case Instruction::PtrToInt:
350 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000351 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000352 return PPCComputeAddress(U->getOperand(0), Addr);
353 break;
354 case Instruction::GetElementPtr: {
355 Address SavedAddr = Addr;
356 long TmpOffset = Addr.Offset;
357
358 // Iterate through the GEP folding the constants into offsets where
359 // we can.
360 gep_type_iterator GTI = gep_type_begin(U);
361 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
362 II != IE; ++II, ++GTI) {
363 const Value *Op = *II;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000364 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000365 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000366 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
367 TmpOffset += SL->getElementOffset(Idx);
368 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000369 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000370 for (;;) {
371 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
372 // Constant-offset addressing.
373 TmpOffset += CI->getSExtValue() * S;
374 break;
375 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000376 if (canFoldAddIntoGEP(U, Op)) {
377 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000378 ConstantInt *CI =
379 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
380 TmpOffset += CI->getSExtValue() * S;
381 // Iterate on the other operand.
382 Op = cast<AddOperator>(Op)->getOperand(0);
383 continue;
384 }
385 // Unsupported
386 goto unsupported_gep;
387 }
388 }
389 }
390
391 // Try to grab the base operand now.
392 Addr.Offset = TmpOffset;
393 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
394
395 // We failed, restore everything and try the other options.
396 Addr = SavedAddr;
397
398 unsupported_gep:
399 break;
400 }
401 case Instruction::Alloca: {
402 const AllocaInst *AI = cast<AllocaInst>(Obj);
403 DenseMap<const AllocaInst*, int>::iterator SI =
404 FuncInfo.StaticAllocaMap.find(AI);
405 if (SI != FuncInfo.StaticAllocaMap.end()) {
406 Addr.BaseType = Address::FrameIndexBase;
407 Addr.Base.FI = SI->second;
408 return true;
409 }
410 break;
411 }
412 }
413
414 // FIXME: References to parameters fall through to the behavior
415 // below. They should be able to reference a frame index since
416 // they are stored to the stack, so we can get "ld rx, offset(r1)"
417 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
418 // just contain the parameter. Try to handle this with a FI.
419
420 // Try to get this in a register if nothing else has worked.
421 if (Addr.Base.Reg == 0)
422 Addr.Base.Reg = getRegForValue(Obj);
423
424 // Prevent assignment of base register to X0, which is inappropriate
425 // for loads and stores alike.
426 if (Addr.Base.Reg != 0)
427 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
428
429 return Addr.Base.Reg != 0;
430}
431
432// Fix up some addresses that can't be used directly. For example, if
433// an offset won't fit in an instruction field, we may need to move it
434// into an index register.
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000435void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000436 unsigned &IndexReg) {
437
438 // Check whether the offset fits in the instruction field.
439 if (!isInt<16>(Addr.Offset))
440 UseOffset = false;
441
442 // If this is a stack pointer and the offset needs to be simplified then
443 // put the alloca address into a register, set the base type back to
444 // register and continue. This should almost never happen.
445 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
446 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000448 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
449 Addr.Base.Reg = ResultReg;
450 Addr.BaseType = Address::RegBase;
451 }
452
453 if (!UseOffset) {
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000454 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
Bill Schmidtccecf262013-08-30 02:29:45 +0000455 const ConstantInt *Offset =
456 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
457 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
458 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
459 }
460}
461
462// Emit a load instruction if possible, returning true if we succeeded,
463// otherwise false. See commentary below for how the register class of
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000464// the load is determined.
Bill Schmidtccecf262013-08-30 02:29:45 +0000465bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
466 const TargetRegisterClass *RC,
467 bool IsZExt, unsigned FP64LoadOpc) {
468 unsigned Opc;
469 bool UseOffset = true;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000470 bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidtccecf262013-08-30 02:29:45 +0000471
472 // If ResultReg is given, it determines the register class of the load.
473 // Otherwise, RC is the register class to use. If the result of the
474 // load isn't anticipated in this block, both may be zero, in which
475 // case we must make a conservative guess. In particular, don't assign
476 // R0 or X0 to the result register, as the result may be used in a load,
477 // store, add-immediate, or isel that won't permit this. (Though
478 // perhaps the spill and reload of live-exit values would handle this?)
479 const TargetRegisterClass *UseRC =
480 (ResultReg ? MRI.getRegClass(ResultReg) :
481 (RC ? RC :
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000482 (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
483 (VT == MVT::f32 ? (HasSPE ? &PPC::SPE4RCRegClass : &PPC::F4RCRegClass) :
Bill Schmidtccecf262013-08-30 02:29:45 +0000484 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
485 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
486
487 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
488
489 switch (VT.SimpleTy) {
490 default: // e.g., vector types not handled
491 return false;
492 case MVT::i8:
493 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
494 break;
495 case MVT::i16:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000496 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
497 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
Bill Schmidtccecf262013-08-30 02:29:45 +0000498 break;
499 case MVT::i32:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000500 Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
501 : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
Bill Schmidtccecf262013-08-30 02:29:45 +0000502 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
503 UseOffset = false;
504 break;
505 case MVT::i64:
506 Opc = PPC::LD;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000507 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
Bill Schmidtccecf262013-08-30 02:29:45 +0000508 "64-bit load with 32-bit target??");
509 UseOffset = ((Addr.Offset & 3) == 0);
510 break;
511 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000512 Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000513 break;
514 case MVT::f64:
515 Opc = FP64LoadOpc;
516 break;
517 }
518
519 // If necessary, materialize the offset into a register and use
520 // the indexed form. Also handle stack pointers with special needs.
521 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000522 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000523
524 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
525 // be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000526 bool IsVSSRC = isVSSRCRegClass(UseRC);
527 bool IsVSFRC = isVSFRCRegClass(UseRC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000528 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000529 bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000530 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000531 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
532 (Addr.Offset == 0)) {
533 UseOffset = false;
534 }
535
Bill Schmidtccecf262013-08-30 02:29:45 +0000536 if (ResultReg == 0)
537 ResultReg = createResultReg(UseRC);
538
539 // Note: If we still have a frame index here, we know the offset is
540 // in range, as otherwise PPCSimplifyAddress would have converted it
541 // into a RegBase.
542 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000543 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000544 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000545
Alex Lorenze40c8a22015-08-11 23:09:45 +0000546 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
547 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
548 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000549 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
550 MFI.getObjectAlignment(Addr.Base.FI));
551
Rafael Espindolaea09c592014-02-18 22:05:46 +0000552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000553 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
554
555 // Base reg with offset in range.
556 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000557 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000558 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000559
Rafael Espindolaea09c592014-02-18 22:05:46 +0000560 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000561 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
562
563 // Indexed form.
564 } else {
565 // Get the RR opcode corresponding to the RI one. FIXME: It would be
566 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
567 // is hard to get at.
568 switch (Opc) {
569 default: llvm_unreachable("Unexpected opcode!");
570 case PPC::LBZ: Opc = PPC::LBZX; break;
571 case PPC::LBZ8: Opc = PPC::LBZX8; break;
572 case PPC::LHZ: Opc = PPC::LHZX; break;
573 case PPC::LHZ8: Opc = PPC::LHZX8; break;
574 case PPC::LHA: Opc = PPC::LHAX; break;
575 case PPC::LHA8: Opc = PPC::LHAX8; break;
576 case PPC::LWZ: Opc = PPC::LWZX; break;
577 case PPC::LWZ8: Opc = PPC::LWZX8; break;
578 case PPC::LWA: Opc = PPC::LWAX; break;
579 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
580 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000581 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000582 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000583 case PPC::EVLDD: Opc = PPC::EVLDDX; break;
584 case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000585 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000586
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000587 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
588 ResultReg);
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000589
590 // If we have an index register defined we use it in the store inst,
591 // otherwise we use X0 as base as it makes the vector instructions to
592 // use zero in the computation of the effective address regardless the
593 // content of the register.
594 if (IndexReg)
595 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
596 else
597 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000598 }
599
600 return true;
601}
602
603// Attempt to fast-select a load instruction.
604bool PPCFastISel::SelectLoad(const Instruction *I) {
605 // FIXME: No atomic loads are supported.
606 if (cast<LoadInst>(I)->isAtomic())
607 return false;
608
609 // Verify we have a legal type before going any further.
610 MVT VT;
611 if (!isLoadTypeLegal(I->getType(), VT))
612 return false;
613
614 // See if we can handle this address.
615 Address Addr;
616 if (!PPCComputeAddress(I->getOperand(0), Addr))
617 return false;
618
619 // Look at the currently assigned register for this instruction
620 // to determine the required register class. This is necessary
621 // to constrain RA from using R0/X0 when this is not legal.
622 unsigned AssignedReg = FuncInfo.ValueMap[I];
623 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000624 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000625
626 unsigned ResultReg = 0;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000627 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
628 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +0000629 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000630 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000631 return true;
632}
633
634// Emit a store instruction to store SrcReg at Addr.
635bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
636 assert(SrcReg && "Nothing to store!");
637 unsigned Opc;
638 bool UseOffset = true;
639
640 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
641 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
642
643 switch (VT.SimpleTy) {
644 default: // e.g., vector types not handled
645 return false;
646 case MVT::i8:
647 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
648 break;
649 case MVT::i16:
650 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
651 break;
652 case MVT::i32:
653 assert(Is32BitInt && "Not GPRC for i32??");
654 Opc = PPC::STW;
655 break;
656 case MVT::i64:
657 Opc = PPC::STD;
658 UseOffset = ((Addr.Offset & 3) == 0);
659 break;
660 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000661 Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000662 break;
663 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000664 Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
Bill Schmidtccecf262013-08-30 02:29:45 +0000665 break;
666 }
667
668 // If necessary, materialize the offset into a register and use
669 // the indexed form. Also handle stack pointers with special needs.
670 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000671 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000672
Bill Seurer8c728ae2014-12-05 20:15:56 +0000673 // If this is a potential VSX store with an offset of 0, a VSX indexed store
674 // can be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000675 bool IsVSSRC = isVSSRCRegClass(RC);
676 bool IsVSFRC = isVSFRCRegClass(RC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000677 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
678 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
679 if ((Is32VSXStore || Is64VSXStore) &&
680 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000681 (Addr.Offset == 0)) {
682 UseOffset = false;
683 }
684
Bill Schmidtccecf262013-08-30 02:29:45 +0000685 // Note: If we still have a frame index here, we know the offset is
686 // in range, as otherwise PPCSimplifyAddress would have converted it
687 // into a RegBase.
688 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000689 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000690 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000691
Alex Lorenze40c8a22015-08-11 23:09:45 +0000692 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
693 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
694 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000695 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
696 MFI.getObjectAlignment(Addr.Base.FI));
697
Rafael Espindolaea09c592014-02-18 22:05:46 +0000698 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
699 .addReg(SrcReg)
700 .addImm(Addr.Offset)
701 .addFrameIndex(Addr.Base.FI)
702 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000703
704 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000705 } else if (UseOffset) {
706 // VSX only provides an indexed store.
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000707 if (Is32VSXStore || Is64VSXStore)
708 return false;
709
Rafael Espindolaea09c592014-02-18 22:05:46 +0000710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000711 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
712
713 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000714 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000715 // Get the RR opcode corresponding to the RI one. FIXME: It would be
716 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
717 // is hard to get at.
718 switch (Opc) {
719 default: llvm_unreachable("Unexpected opcode!");
720 case PPC::STB: Opc = PPC::STBX; break;
721 case PPC::STH : Opc = PPC::STHX; break;
722 case PPC::STW : Opc = PPC::STWX; break;
723 case PPC::STB8: Opc = PPC::STBX8; break;
724 case PPC::STH8: Opc = PPC::STHX8; break;
725 case PPC::STW8: Opc = PPC::STWX8; break;
726 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000727 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000728 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000729 case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
730 case PPC::SPESTW: Opc = PPC::SPESTWX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000731 }
Samuel Antaof6815602015-03-17 15:00:57 +0000732
733 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
734 .addReg(SrcReg);
735
736 // If we have an index register defined we use it in the store inst,
737 // otherwise we use X0 as base as it makes the vector instructions to
738 // use zero in the computation of the effective address regardless the
739 // content of the register.
740 if (IndexReg)
741 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
742 else
743 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000744 }
745
746 return true;
747}
748
749// Attempt to fast-select a store instruction.
750bool PPCFastISel::SelectStore(const Instruction *I) {
751 Value *Op0 = I->getOperand(0);
752 unsigned SrcReg = 0;
753
754 // FIXME: No atomics loads are supported.
755 if (cast<StoreInst>(I)->isAtomic())
756 return false;
757
758 // Verify we have a legal type before going any further.
759 MVT VT;
760 if (!isLoadTypeLegal(Op0->getType(), VT))
761 return false;
762
763 // Get the value to be stored into a register.
764 SrcReg = getRegForValue(Op0);
765 if (SrcReg == 0)
766 return false;
767
768 // See if we can handle this address.
769 Address Addr;
770 if (!PPCComputeAddress(I->getOperand(1), Addr))
771 return false;
772
773 if (!PPCEmitStore(VT, SrcReg, Addr))
774 return false;
775
776 return true;
777}
778
Bill Schmidt03008132013-08-25 22:33:42 +0000779// Attempt to fast-select a branch instruction.
780bool PPCFastISel::SelectBranch(const Instruction *I) {
781 const BranchInst *BI = cast<BranchInst>(I);
782 MachineBasicBlock *BrBB = FuncInfo.MBB;
783 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
784 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
785
786 // For now, just try the simplest case where it's fed by a compare.
787 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000788 if (isValueAvailable(CI)) {
789 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
790 if (!OptPPCPred)
791 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000792
Hal Finkel5f2a1372015-05-23 12:18:10 +0000793 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000794
Hal Finkel5f2a1372015-05-23 12:18:10 +0000795 // Take advantage of fall-through opportunities.
796 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
797 std::swap(TBB, FBB);
798 PPCPred = PPC::InvertPredicate(PPCPred);
799 }
800
801 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
802
803 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000804 CondReg, PPCPred))
Hal Finkel5f2a1372015-05-23 12:18:10 +0000805 return false;
806
807 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000808 .addImm(PPCSubTarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
809 .addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000810 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000811 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000812 }
Bill Schmidt03008132013-08-25 22:33:42 +0000813 } else if (const ConstantInt *CI =
814 dyn_cast<ConstantInt>(BI->getCondition())) {
815 uint64_t Imm = CI->getZExtValue();
816 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000817 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000818 return true;
819 }
820
821 // FIXME: ARM looks for a case where the block containing the compare
822 // has been split from the block containing the branch. If this happens,
823 // there is a vreg available containing the result of the compare. I'm
824 // not sure we can do much, as we've lost the predicate information with
825 // the compare instruction -- we have a 4-bit CR but don't know which bit
826 // to test here.
827 return false;
828}
829
830// Attempt to emit a compare of the two source values. Signed and unsigned
831// comparisons are supported. Return false if we can't handle it.
832bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000833 bool IsZExt, unsigned DestReg,
834 const PPC::Predicate Pred) {
Bill Schmidt03008132013-08-25 22:33:42 +0000835 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000836 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000837 if (!SrcEVT.isSimple())
838 return false;
839 MVT SrcVT = SrcEVT.getSimpleVT();
840
Eric Christopher1b8e7632014-05-22 01:07:24 +0000841 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000842 return false;
843
Bill Schmidt03008132013-08-25 22:33:42 +0000844 // See if operand 2 is an immediate encodeable in the compare.
845 // FIXME: Operands are not in canonical order at -O0, so an immediate
846 // operand in position 1 is a lost opportunity for now. We are
847 // similar to ARM in this regard.
848 long Imm = 0;
849 bool UseImm = false;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000850 const bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidt03008132013-08-25 22:33:42 +0000851
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000852 // Only 16-bit integer constants can be represented in compares for
Bill Schmidt03008132013-08-25 22:33:42 +0000853 // PowerPC. Others will be materialized into a register.
854 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
855 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
856 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
857 const APInt &CIVal = ConstInt->getValue();
858 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
859 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
860 UseImm = true;
861 }
862 }
863
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000864 unsigned SrcReg1 = getRegForValue(SrcValue1);
865 if (SrcReg1 == 0)
866 return false;
867
868 unsigned SrcReg2 = 0;
869 if (!UseImm) {
870 SrcReg2 = getRegForValue(SrcValue2);
871 if (SrcReg2 == 0)
872 return false;
873 }
874
Bill Schmidt03008132013-08-25 22:33:42 +0000875 unsigned CmpOpc;
876 bool NeedsExt = false;
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000877 auto RC = MRI.getRegClass(SrcReg1);
Bill Schmidt03008132013-08-25 22:33:42 +0000878 switch (SrcVT.SimpleTy) {
879 default: return false;
880 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000881 if (HasSPE) {
882 switch (Pred) {
883 default: return false;
884 case PPC::PRED_EQ:
885 CmpOpc = PPC::EFSCMPEQ;
886 break;
887 case PPC::PRED_LT:
888 CmpOpc = PPC::EFSCMPLT;
889 break;
890 case PPC::PRED_GT:
891 CmpOpc = PPC::EFSCMPGT;
892 break;
893 }
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000894 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000895 CmpOpc = PPC::FCMPUS;
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000896 if (isVSSRCRegClass(RC)) {
897 unsigned TmpReg = createResultReg(&PPC::F4RCRegClass);
898 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
899 TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg1);
900 SrcReg1 = TmpReg;
901 }
902 }
Bill Schmidt03008132013-08-25 22:33:42 +0000903 break;
904 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000905 if (HasSPE) {
906 switch (Pred) {
907 default: return false;
908 case PPC::PRED_EQ:
909 CmpOpc = PPC::EFDCMPEQ;
910 break;
911 case PPC::PRED_LT:
912 CmpOpc = PPC::EFDCMPLT;
913 break;
914 case PPC::PRED_GT:
915 CmpOpc = PPC::EFDCMPGT;
916 break;
917 }
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000918 } else if (isVSFRCRegClass(RC)) {
919 CmpOpc = PPC::XSCMPUDP;
920 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000921 CmpOpc = PPC::FCMPUD;
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000922 }
Bill Schmidt03008132013-08-25 22:33:42 +0000923 break;
924 case MVT::i1:
925 case MVT::i8:
926 case MVT::i16:
927 NeedsExt = true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000928 LLVM_FALLTHROUGH;
Bill Schmidt03008132013-08-25 22:33:42 +0000929 case MVT::i32:
930 if (!UseImm)
931 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
932 else
933 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
934 break;
935 case MVT::i64:
936 if (!UseImm)
937 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
938 else
939 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
940 break;
941 }
942
Bill Schmidt03008132013-08-25 22:33:42 +0000943 if (NeedsExt) {
944 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
945 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
946 return false;
947 SrcReg1 = ExtReg;
948
949 if (!UseImm) {
950 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
951 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
952 return false;
953 SrcReg2 = ExtReg;
954 }
955 }
956
957 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000959 .addReg(SrcReg1).addReg(SrcReg2);
960 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000962 .addReg(SrcReg1).addImm(Imm);
963
964 return true;
965}
966
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000967// Attempt to fast-select a floating-point extend instruction.
968bool PPCFastISel::SelectFPExt(const Instruction *I) {
969 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000970 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
971 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000972
973 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
974 return false;
975
976 unsigned SrcReg = getRegForValue(Src);
977 if (!SrcReg)
978 return false;
979
980 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000981 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000982 return true;
983}
984
985// Attempt to fast-select a floating-point truncate instruction.
986bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
987 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000988 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
989 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000990
991 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
992 return false;
993
994 unsigned SrcReg = getRegForValue(Src);
995 if (!SrcReg)
996 return false;
997
998 // Round the result to single precision.
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000999 unsigned DestReg;
1000
1001 if (PPCSubTarget->hasSPE()) {
1002 DestReg = createResultReg(&PPC::SPE4RCRegClass);
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1004 TII.get(PPC::EFSCFD), DestReg)
1005 .addReg(SrcReg);
1006 } else {
1007 DestReg = createResultReg(&PPC::F4RCRegClass);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1009 TII.get(PPC::FRSP), DestReg)
1010 .addReg(SrcReg);
1011 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001012
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001013 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001014 return true;
1015}
1016
1017// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +00001018// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001019// those should be used instead of moving via a stack slot when the
1020// subtarget permits.
1021// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1022// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1023// case to 8 bytes which produces tighter code but wastes stack space.
1024unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1025 bool IsSigned) {
1026
1027 // If necessary, extend 32-bit int to 64-bit.
1028 if (SrcVT == MVT::i32) {
1029 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1030 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1031 return 0;
1032 SrcReg = TmpReg;
1033 }
1034
1035 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1036 Address Addr;
1037 Addr.BaseType = Address::FrameIndexBase;
1038 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1039
1040 // Store the value from the GPR.
1041 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1042 return 0;
1043
1044 // Load the integer value into an FPR. The kind of load used depends
1045 // on a number of conditions.
1046 unsigned LoadOpc = PPC::LFD;
1047
1048 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +00001049 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001050 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001051 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +00001052 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001053 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001054 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +00001055 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001056 }
1057
1058 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1059 unsigned ResultReg = 0;
1060 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1061 return 0;
1062
1063 return ResultReg;
1064}
1065
1066// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001067// FIXME: Once fast-isel has better support for VSX, conversions using
1068// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001069bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1070 MVT DstVT;
1071 Type *DstTy = I->getType();
1072 if (!isTypeLegal(DstTy, DstVT))
1073 return false;
1074
1075 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1076 return false;
1077
1078 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001079 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001080 if (!SrcEVT.isSimple())
1081 return false;
1082
1083 MVT SrcVT = SrcEVT.getSimpleVT();
1084
1085 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1086 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1087 return false;
1088
1089 unsigned SrcReg = getRegForValue(Src);
1090 if (SrcReg == 0)
1091 return false;
1092
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001093 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1094 if (PPCSubTarget->hasSPE()) {
1095 unsigned Opc;
1096 if (DstVT == MVT::f32)
1097 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1098 else
1099 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1100
1101 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1102 // Generate the convert.
1103 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1104 .addReg(SrcReg);
1105 updateValueMap(I, DestReg);
1106 return true;
1107 }
1108
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001109 // We can only lower an unsigned convert if we have the newer
1110 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001111 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001112 return false;
1113
1114 // FIXME: For now we require the newer floating-point conversion operations
1115 // (which are present only on P7 and A2 server models) when converting
1116 // to single-precision float. Otherwise we have to generate a lot of
1117 // fiddly code to avoid double rounding. If necessary, the fiddly code
1118 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001119 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001120 return false;
1121
1122 // Extend the input if necessary.
1123 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1124 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1125 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1126 return false;
1127 SrcVT = MVT::i64;
1128 SrcReg = TmpReg;
1129 }
1130
1131 // Move the integer value to an FPR.
1132 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1133 if (FPReg == 0)
1134 return false;
1135
1136 // Determine the opcode for the conversion.
1137 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1138 unsigned DestReg = createResultReg(RC);
1139 unsigned Opc;
1140
1141 if (DstVT == MVT::f32)
1142 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1143 else
1144 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1145
1146 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001148 .addReg(FPReg);
1149
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001150 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001151 return true;
1152}
1153
1154// Move the floating-point value in SrcReg into an integer destination
1155// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001156// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001157// those should be used instead of moving via a stack slot when the
1158// subtarget permits.
1159unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1160 unsigned SrcReg, bool IsSigned) {
1161 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1162 // Note that if have STFIWX available, we could use a 4-byte stack
1163 // slot for i32, but this being fast-isel we'll just go with the
1164 // easiest code gen possible.
1165 Address Addr;
1166 Addr.BaseType = Address::FrameIndexBase;
1167 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1168
1169 // Store the value from the FPR.
1170 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1171 return 0;
1172
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001173 // Reload it into a GPR. If we want an i32 on big endian, modify the
1174 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001175 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001176 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001177
1178 // Look at the currently assigned register for this instruction
1179 // to determine the required register class.
1180 unsigned AssignedReg = FuncInfo.ValueMap[I];
1181 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001182 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001183
1184 unsigned ResultReg = 0;
1185 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1186 return 0;
1187
1188 return ResultReg;
1189}
1190
1191// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001192// FIXME: Once fast-isel has better support for VSX, conversions using
1193// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001194bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1195 MVT DstVT, SrcVT;
1196 Type *DstTy = I->getType();
1197 if (!isTypeLegal(DstTy, DstVT))
1198 return false;
1199
1200 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1201 return false;
1202
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001203 // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
1204 if (DstVT == MVT::i64 && !IsSigned &&
1205 !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
Bill Schmidt83973ef2014-06-24 20:05:18 +00001206 return false;
1207
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001208 Value *Src = I->getOperand(0);
1209 Type *SrcTy = Src->getType();
1210 if (!isTypeLegal(SrcTy, SrcVT))
1211 return false;
1212
1213 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1214 return false;
1215
1216 unsigned SrcReg = getRegForValue(Src);
1217 if (SrcReg == 0)
1218 return false;
1219
1220 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001221 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001222 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1223 if (InRC == &PPC::F4RCRegClass) {
1224 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001226 TII.get(TargetOpcode::COPY), TmpReg)
1227 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001228 SrcReg = TmpReg;
1229 }
1230
1231 // Determine the opcode for the conversion, which takes place
1232 // entirely within FPRs.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001233 unsigned DestReg;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001234 unsigned Opc;
1235
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001236 if (PPCSubTarget->hasSPE()) {
Justin Hibbits22e939a2018-07-18 05:19:25 +00001237 DestReg = createResultReg(&PPC::GPRCRegClass);
1238 if (IsSigned)
1239 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1240 else
1241 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001242 } else {
1243 DestReg = createResultReg(&PPC::F8RCRegClass);
1244 if (DstVT == MVT::i32)
1245 if (IsSigned)
1246 Opc = PPC::FCTIWZ;
1247 else
1248 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001249 else
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001250 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1251 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001252
1253 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001255 .addReg(SrcReg);
1256
1257 // Now move the integer value from a float register to an integer register.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001258 unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
1259 PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1260
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001261 if (IntReg == 0)
1262 return false;
1263
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001264 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001265 return true;
1266}
1267
Bill Schmidtccecf262013-08-30 02:29:45 +00001268// Attempt to fast-select a binary integer operation that isn't already
1269// handled automatically.
1270bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001271 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001272
1273 // We can get here in the case when we have a binary operation on a non-legal
1274 // type and the target independent selector doesn't know how to handle it.
1275 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1276 return false;
1277
1278 // Look at the currently assigned register for this instruction
1279 // to determine the required register class. If there is no register,
1280 // make a conservative choice (don't assign R0).
1281 unsigned AssignedReg = FuncInfo.ValueMap[I];
1282 const TargetRegisterClass *RC =
1283 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1284 &PPC::GPRC_and_GPRC_NOR0RegClass);
1285 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1286
1287 unsigned Opc;
1288 switch (ISDOpcode) {
1289 default: return false;
1290 case ISD::ADD:
1291 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1292 break;
1293 case ISD::OR:
1294 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1295 break;
1296 case ISD::SUB:
1297 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1298 break;
1299 }
1300
1301 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1302 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1303 if (SrcReg1 == 0) return false;
1304
1305 // Handle case of small immediate operand.
1306 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1307 const APInt &CIVal = ConstInt->getValue();
1308 int Imm = (int)CIVal.getSExtValue();
1309 bool UseImm = true;
1310 if (isInt<16>(Imm)) {
1311 switch (Opc) {
1312 default:
1313 llvm_unreachable("Missing case!");
1314 case PPC::ADD4:
1315 Opc = PPC::ADDI;
1316 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1317 break;
1318 case PPC::ADD8:
1319 Opc = PPC::ADDI8;
1320 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1321 break;
1322 case PPC::OR:
1323 Opc = PPC::ORI;
1324 break;
1325 case PPC::OR8:
1326 Opc = PPC::ORI8;
1327 break;
1328 case PPC::SUBF:
1329 if (Imm == -32768)
1330 UseImm = false;
1331 else {
1332 Opc = PPC::ADDI;
1333 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1334 Imm = -Imm;
1335 }
1336 break;
1337 case PPC::SUBF8:
1338 if (Imm == -32768)
1339 UseImm = false;
1340 else {
1341 Opc = PPC::ADDI8;
1342 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1343 Imm = -Imm;
1344 }
1345 break;
1346 }
1347
1348 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1350 ResultReg)
1351 .addReg(SrcReg1)
1352 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001353 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001354 return true;
1355 }
1356 }
1357 }
1358
1359 // Reg-reg case.
1360 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1361 if (SrcReg2 == 0) return false;
1362
1363 // Reverse operands for subtract-from.
1364 if (ISDOpcode == ISD::SUB)
1365 std::swap(SrcReg1, SrcReg2);
1366
Rafael Espindolaea09c592014-02-18 22:05:46 +00001367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001368 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001369 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001370 return true;
1371}
1372
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001373// Handle arguments to a call that we're attempting to fast-select.
1374// Return false if the arguments are too complex for us at the moment.
1375bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1376 SmallVectorImpl<unsigned> &ArgRegs,
1377 SmallVectorImpl<MVT> &ArgVTs,
1378 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1379 SmallVectorImpl<unsigned> &RegArgs,
1380 CallingConv::ID CC,
1381 unsigned &NumBytes,
1382 bool IsVarArg) {
1383 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001384 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001385
1386 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001387 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001388 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001389
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001390 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1391
1392 // Bail out if we can't handle any of the arguments.
1393 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1394 CCValAssign &VA = ArgLocs[I];
1395 MVT ArgVT = ArgVTs[VA.getValNo()];
1396
1397 // Skip vector arguments for now, as well as long double and
1398 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001399 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001400 !VA.isRegLoc() || VA.needsCustom())
1401 return false;
1402
1403 // Skip bit-converted arguments for now.
1404 if (VA.getLocInfo() == CCValAssign::BCvt)
1405 return false;
1406 }
1407
1408 // Get a count of how many bytes are to be pushed onto the stack.
1409 NumBytes = CCInfo.getNextStackOffset();
1410
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001411 // The prolog code of the callee may store up to 8 GPR argument registers to
1412 // the stack, allowing va_start to index over them in memory if its varargs.
1413 // Because we cannot tell if this is needed on the caller side, we have to
1414 // conservatively assume that it is needed. As such, make sure we have at
1415 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001416 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001417 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001418
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001419 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001421 TII.get(TII.getCallFrameSetupOpcode()))
Serge Pavlovd526b132017-05-09 13:35:13 +00001422 .addImm(NumBytes).addImm(0);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001423
1424 // Prepare to assign register arguments. Every argument uses up a
1425 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001426 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001427 unsigned NextGPR = PPC::X3;
1428 unsigned NextFPR = PPC::F1;
1429
1430 // Process arguments.
1431 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1432 CCValAssign &VA = ArgLocs[I];
1433 unsigned Arg = ArgRegs[VA.getValNo()];
1434 MVT ArgVT = ArgVTs[VA.getValNo()];
1435
1436 // Handle argument promotion and bitcasts.
1437 switch (VA.getLocInfo()) {
1438 default:
1439 llvm_unreachable("Unknown loc info!");
1440 case CCValAssign::Full:
1441 break;
1442 case CCValAssign::SExt: {
1443 MVT DestVT = VA.getLocVT();
1444 const TargetRegisterClass *RC =
1445 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1446 unsigned TmpReg = createResultReg(RC);
1447 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1448 llvm_unreachable("Failed to emit a sext!");
1449 ArgVT = DestVT;
1450 Arg = TmpReg;
1451 break;
1452 }
1453 case CCValAssign::AExt:
1454 case CCValAssign::ZExt: {
1455 MVT DestVT = VA.getLocVT();
1456 const TargetRegisterClass *RC =
1457 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1458 unsigned TmpReg = createResultReg(RC);
1459 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1460 llvm_unreachable("Failed to emit a zext!");
1461 ArgVT = DestVT;
1462 Arg = TmpReg;
1463 break;
1464 }
1465 case CCValAssign::BCvt: {
1466 // FIXME: Not yet handled.
1467 llvm_unreachable("Should have bailed before getting here!");
1468 break;
1469 }
1470 }
1471
1472 // Copy this argument to the appropriate register.
1473 unsigned ArgReg;
1474 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1475 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001476 if (CC != CallingConv::Fast)
1477 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001478 } else
1479 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001480
1481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1482 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001483 RegArgs.push_back(ArgReg);
1484 }
1485
1486 return true;
1487}
1488
1489// For a call that we've determined we can fast-select, finish the
1490// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001491bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1492 CallingConv::ID CC = CLI.CallConv;
1493
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001494 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001496 TII.get(TII.getCallFrameDestroyOpcode()))
1497 .addImm(NumBytes).addImm(0);
1498
1499 // Next, generate a copy to obtain the return value.
1500 // FIXME: No multi-register return values yet, though I don't foresee
1501 // any real difficulties there.
1502 if (RetVT != MVT::isVoid) {
1503 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001504 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001505 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1506 CCValAssign &VA = RVLocs[0];
1507 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1508 assert(VA.isRegLoc() && "Can only return in registers!");
1509
1510 MVT DestVT = VA.getValVT();
1511 MVT CopyVT = DestVT;
1512
1513 // Ints smaller than a register still arrive in a full 64-bit
1514 // register, so make sure we recognize this.
1515 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1516 CopyVT = MVT::i64;
1517
1518 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001519 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001520
1521 if (RetVT == CopyVT) {
1522 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1523 ResultReg = createResultReg(CpyRC);
1524
Rafael Espindolaea09c592014-02-18 22:05:46 +00001525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001526 TII.get(TargetOpcode::COPY), ResultReg)
1527 .addReg(SourcePhysReg);
1528
1529 // If necessary, round the floating result to single precision.
1530 } else if (CopyVT == MVT::f64) {
1531 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001533 ResultReg).addReg(SourcePhysReg);
1534
1535 // If only the low half of a general register is needed, generate
1536 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1537 // used along the fast-isel path (not lowered), and downstream logic
1538 // also doesn't like a direct subreg copy on a physical reg.)
1539 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1540 ResultReg = createResultReg(&PPC::GPRCRegClass);
1541 // Convert physical register from G8RC to GPRC.
1542 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001544 TII.get(TargetOpcode::COPY), ResultReg)
1545 .addReg(SourcePhysReg);
1546 }
1547
Bill Schmidt0954ea12013-08-30 23:25:30 +00001548 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001549 CLI.InRegs.push_back(SourcePhysReg);
1550 CLI.ResultReg = ResultReg;
1551 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001552 }
Hal Finkel934361a2015-01-14 01:07:51 +00001553
1554 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001555}
1556
Hal Finkel934361a2015-01-14 01:07:51 +00001557bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1558 CallingConv::ID CC = CLI.CallConv;
1559 bool IsTailCall = CLI.IsTailCall;
1560 bool IsVarArg = CLI.IsVarArg;
1561 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001562 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001563
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001564 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001565 return false;
1566
1567 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001568 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001569 return false;
1570
Hal Finkel934361a2015-01-14 01:07:51 +00001571 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001572 if (IsVarArg)
1573 return false;
1574
1575 // Handle simple calls for now, with legal return types and
1576 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001577 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001578 MVT RetVT;
1579 if (RetTy->isVoidTy())
1580 RetVT = MVT::isVoid;
1581 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1582 RetVT != MVT::i8)
1583 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001584 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1585 // We can't handle boolean returns when CR bits are in use.
1586 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001587
1588 // FIXME: No multi-register return values yet.
1589 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1590 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1591 RetVT != MVT::f64) {
1592 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001593 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001594 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1595 if (RVLocs.size() > 1)
1596 return false;
1597 }
1598
1599 // Bail early if more than 8 arguments, as we only currently
1600 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001601 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001602 if (NumArgs > 8)
1603 return false;
1604
1605 // Set up the argument vectors.
1606 SmallVector<Value*, 8> Args;
1607 SmallVector<unsigned, 8> ArgRegs;
1608 SmallVector<MVT, 8> ArgVTs;
1609 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1610
1611 Args.reserve(NumArgs);
1612 ArgRegs.reserve(NumArgs);
1613 ArgVTs.reserve(NumArgs);
1614 ArgFlags.reserve(NumArgs);
1615
Hal Finkel934361a2015-01-14 01:07:51 +00001616 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001617 // Only handle easy calls for now. It would be reasonably easy
1618 // to handle <= 8-byte structures passed ByVal in registers, but we
1619 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001620 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1621 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001622 return false;
1623
Hal Finkel934361a2015-01-14 01:07:51 +00001624 Value *ArgValue = CLI.OutVals[i];
1625 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001626 MVT ArgVT;
1627 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1628 return false;
1629
1630 if (ArgVT.isVector())
1631 return false;
1632
Hal Finkel934361a2015-01-14 01:07:51 +00001633 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001634 if (Arg == 0)
1635 return false;
1636
Hal Finkel934361a2015-01-14 01:07:51 +00001637 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001638 ArgRegs.push_back(Arg);
1639 ArgVTs.push_back(ArgVT);
1640 ArgFlags.push_back(Flags);
1641 }
1642
1643 // Process the arguments.
1644 SmallVector<unsigned, 8> RegArgs;
1645 unsigned NumBytes;
1646
1647 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1648 RegArgs, CC, NumBytes, IsVarArg))
1649 return false;
1650
Hal Finkel934361a2015-01-14 01:07:51 +00001651 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001652 // FIXME: No handling for function pointers yet. This requires
1653 // implementing the function descriptor (OPD) setup.
1654 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001655 if (!GV) {
1656 // patchpoints are a special case; they always dispatch to a pointer value.
1657 // However, we don't actually want to generate the indirect call sequence
1658 // here (that will be generated, as necessary, during asm printing), and
1659 // the call we generate here will be erased by FastISel::selectPatchpoint,
1660 // so don't try very hard...
1661 if (CLI.IsPatchPoint)
1662 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1663 else
1664 return false;
1665 } else {
1666 // Build direct call with NOP for TOC restore.
1667 // FIXME: We can and should optimize away the NOP for local calls.
1668 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1669 TII.get(PPC::BL8_NOP));
1670 // Add callee.
1671 MIB.addGlobalAddress(GV);
1672 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001673
1674 // Add implicit physical register uses to the call.
1675 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1676 MIB.addReg(RegArgs[II], RegState::Implicit);
1677
Hal Finkelaf519932015-01-19 07:20:27 +00001678 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1679 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001680 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001681 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001682
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001683 // Add a register mask with the call-preserved registers. Proper
1684 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001685 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001686
Hal Finkel934361a2015-01-14 01:07:51 +00001687 CLI.Call = MIB;
1688
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001689 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001690 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001691}
1692
Bill Schmidtd89f6782013-08-26 19:42:51 +00001693// Attempt to fast-select a return instruction.
1694bool PPCFastISel::SelectRet(const Instruction *I) {
1695
1696 if (!FuncInfo.CanLowerReturn)
1697 return false;
1698
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001699 if (TLI.supportSplitCSR(FuncInfo.MF))
1700 return false;
1701
Bill Schmidtd89f6782013-08-26 19:42:51 +00001702 const ReturnInst *Ret = cast<ReturnInst>(I);
1703 const Function &F = *I->getParent()->getParent();
1704
1705 // Build a list of return value registers.
1706 SmallVector<unsigned, 4> RetRegs;
1707 CallingConv::ID CC = F.getCallingConv();
1708
1709 if (Ret->getNumOperands() > 0) {
1710 SmallVector<ISD::OutputArg, 4> Outs;
Matt Arsenault81920b02018-07-28 13:25:19 +00001711 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001712
1713 // Analyze operands of the call, assigning locations to each operand.
1714 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001715 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001716 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1717 const Value *RV = Ret->getOperand(0);
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001718
Bill Schmidtd89f6782013-08-26 19:42:51 +00001719 // FIXME: Only one output register for now.
1720 if (ValLocs.size() > 1)
1721 return false;
1722
Eric Christopherf0024d12015-07-25 00:48:08 +00001723 // Special case for returning a constant integer of any size - materialize
1724 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001725 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001726 CCValAssign &VA = ValLocs[0];
1727
1728 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001729 // We still need to worry about properly extending the sign. For example,
1730 // we could have only a single bit or a constant that needs zero
1731 // extension rather than sign extension. Make sure we pass the return
1732 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001733 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001734 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001735
Rafael Espindolaea09c592014-02-18 22:05:46 +00001736 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001737 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1738
Bill Schmidtd89f6782013-08-26 19:42:51 +00001739 RetRegs.push_back(RetReg);
1740
1741 } else {
1742 unsigned Reg = getRegForValue(RV);
1743
1744 if (Reg == 0)
1745 return false;
1746
1747 // Copy the result values into the output registers.
1748 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1749
1750 CCValAssign &VA = ValLocs[i];
1751 assert(VA.isRegLoc() && "Can only return in registers!");
1752 RetRegs.push_back(VA.getLocReg());
1753 unsigned SrcReg = Reg + VA.getValNo();
1754
Mehdi Amini44ede332015-07-09 02:09:04 +00001755 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001756 if (!RVEVT.isSimple())
1757 return false;
1758 MVT RVVT = RVEVT.getSimpleVT();
1759 MVT DestVT = VA.getLocVT();
1760
1761 if (RVVT != DestVT && RVVT != MVT::i8 &&
1762 RVVT != MVT::i16 && RVVT != MVT::i32)
1763 return false;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001764
Bill Schmidtd89f6782013-08-26 19:42:51 +00001765 if (RVVT != DestVT) {
1766 switch (VA.getLocInfo()) {
1767 default:
1768 llvm_unreachable("Unknown loc info!");
1769 case CCValAssign::Full:
1770 llvm_unreachable("Full value assign but types don't match?");
1771 case CCValAssign::AExt:
1772 case CCValAssign::ZExt: {
1773 const TargetRegisterClass *RC =
1774 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1775 unsigned TmpReg = createResultReg(RC);
1776 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1777 return false;
1778 SrcReg = TmpReg;
1779 break;
1780 }
1781 case CCValAssign::SExt: {
1782 const TargetRegisterClass *RC =
1783 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1784 unsigned TmpReg = createResultReg(RC);
1785 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1786 return false;
1787 SrcReg = TmpReg;
1788 break;
1789 }
1790 }
1791 }
1792
Rafael Espindolaea09c592014-02-18 22:05:46 +00001793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001794 TII.get(TargetOpcode::COPY), RetRegs[i])
1795 .addReg(SrcReg);
1796 }
1797 }
1798 }
1799
Rafael Espindolaea09c592014-02-18 22:05:46 +00001800 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001801 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001802
1803 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1804 MIB.addReg(RetRegs[i], RegState::Implicit);
1805
1806 return true;
1807}
1808
Bill Schmidt03008132013-08-25 22:33:42 +00001809// Attempt to emit an integer extend of SrcReg into DestReg. Both
1810// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001811// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001812bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1813 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001814 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1815 return false;
1816 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1817 return false;
1818
1819 // Signed extensions use EXTSB, EXTSH, EXTSW.
1820 if (!IsZExt) {
1821 unsigned Opc;
1822 if (SrcVT == MVT::i8)
1823 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1824 else if (SrcVT == MVT::i16)
1825 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1826 else {
1827 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1828 Opc = PPC::EXTSW_32_64;
1829 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001831 .addReg(SrcReg);
1832
1833 // Unsigned 32-bit extensions use RLWINM.
1834 } else if (DestVT == MVT::i32) {
1835 unsigned MB;
1836 if (SrcVT == MVT::i8)
1837 MB = 24;
1838 else {
1839 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1840 MB = 16;
1841 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001843 DestReg)
1844 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1845
1846 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1847 } else {
1848 unsigned MB;
1849 if (SrcVT == MVT::i8)
1850 MB = 56;
1851 else if (SrcVT == MVT::i16)
1852 MB = 48;
1853 else
1854 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001856 TII.get(PPC::RLDICL_32_64), DestReg)
1857 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1858 }
1859
1860 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001861}
1862
1863// Attempt to fast-select an indirect branch instruction.
1864bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1865 unsigned AddrReg = getRegForValue(I->getOperand(0));
1866 if (AddrReg == 0)
1867 return false;
1868
Rafael Espindolaea09c592014-02-18 22:05:46 +00001869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001870 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001872
1873 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001874 for (const BasicBlock *SuccBB : IB->successors())
1875 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001876
1877 return true;
1878}
1879
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001880// Attempt to fast-select an integer truncate instruction.
1881bool PPCFastISel::SelectTrunc(const Instruction *I) {
1882 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001883 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1884 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001885
1886 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1887 return false;
1888
1889 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1890 return false;
1891
1892 unsigned SrcReg = getRegForValue(Src);
1893 if (!SrcReg)
1894 return false;
1895
1896 // The only interesting case is when we need to switch register classes.
1897 if (SrcVT == MVT::i64) {
1898 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001899 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1900 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001901 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1902 SrcReg = ResultReg;
1903 }
1904
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001905 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001906 return true;
1907}
1908
Bill Schmidtd89f6782013-08-26 19:42:51 +00001909// Attempt to fast-select an integer extend instruction.
1910bool PPCFastISel::SelectIntExt(const Instruction *I) {
1911 Type *DestTy = I->getType();
1912 Value *Src = I->getOperand(0);
1913 Type *SrcTy = Src->getType();
1914
1915 bool IsZExt = isa<ZExtInst>(I);
1916 unsigned SrcReg = getRegForValue(Src);
1917 if (!SrcReg) return false;
1918
1919 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001920 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1921 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001922 if (!SrcEVT.isSimple())
1923 return false;
1924 if (!DestEVT.isSimple())
1925 return false;
1926
1927 MVT SrcVT = SrcEVT.getSimpleVT();
1928 MVT DestVT = DestEVT.getSimpleVT();
1929
1930 // If we know the register class needed for the result of this
1931 // instruction, use it. Otherwise pick the register class of the
1932 // correct size that does not contain X0/R0, since we don't know
1933 // whether downstream uses permit that assignment.
1934 unsigned AssignedReg = FuncInfo.ValueMap[I];
1935 const TargetRegisterClass *RC =
1936 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1937 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1938 &PPC::GPRC_and_GPRC_NOR0RegClass));
1939 unsigned ResultReg = createResultReg(RC);
1940
1941 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1942 return false;
1943
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001944 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001945 return true;
1946}
1947
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001948// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001949// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001950bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001951
1952 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001953 case Instruction::Load:
1954 return SelectLoad(I);
1955 case Instruction::Store:
1956 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001957 case Instruction::Br:
1958 return SelectBranch(I);
1959 case Instruction::IndirectBr:
1960 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001961 case Instruction::FPExt:
1962 return SelectFPExt(I);
1963 case Instruction::FPTrunc:
1964 return SelectFPTrunc(I);
1965 case Instruction::SIToFP:
1966 return SelectIToFP(I, /*IsSigned*/ true);
1967 case Instruction::UIToFP:
1968 return SelectIToFP(I, /*IsSigned*/ false);
1969 case Instruction::FPToSI:
1970 return SelectFPToI(I, /*IsSigned*/ true);
1971 case Instruction::FPToUI:
1972 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001973 case Instruction::Add:
1974 return SelectBinaryIntOp(I, ISD::ADD);
1975 case Instruction::Or:
1976 return SelectBinaryIntOp(I, ISD::OR);
1977 case Instruction::Sub:
1978 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001979 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001980 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001981 case Instruction::Ret:
1982 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001983 case Instruction::Trunc:
1984 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001985 case Instruction::ZExt:
1986 case Instruction::SExt:
1987 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001988 // Here add other flavors of Instruction::XXX that automated
1989 // cases don't catch. For example, switches are terminators
1990 // that aren't yet handled.
1991 default:
1992 break;
1993 }
1994 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001995}
1996
1997// Materialize a floating-point constant into a register, and return
1998// the register number (or zero if we failed to handle it).
1999unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
2000 // No plans to handle long double here.
2001 if (VT != MVT::f32 && VT != MVT::f64)
2002 return 0;
2003
2004 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002005 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002006 assert(Align > 0 && "Unexpectedly missing alignment information!");
2007 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002008 const bool HasSPE = PPCSubTarget->hasSPE();
2009 const TargetRegisterClass *RC;
2010 if (HasSPE)
2011 RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
2012 else
2013 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2014
Ulrich Weigandc3b495a2016-08-05 15:22:05 +00002015 unsigned DestReg = createResultReg(RC);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002016 CodeModel::Model CModel = TM.getCodeModel();
2017
Alex Lorenze40c8a22015-08-11 23:09:45 +00002018 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2019 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2020 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002021
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002022 unsigned Opc;
2023
2024 if (HasSPE)
2025 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2026 else
2027 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2028
Bill Schmidt03008132013-08-25 22:33:42 +00002029 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2030
Hal Finkele6698d52015-02-01 15:03:28 +00002031 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00002032 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
Rafael Espindola79e238a2017-08-03 02:16:21 +00002033 if (CModel == CodeModel::Small) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002034 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00002035 TmpReg)
2036 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00002038 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2039 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002040 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00002041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002042 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00002043 // But for large code model, we must generate a LDtocL followed
2044 // by the LF[SD].
2045 if (CModel == CodeModel::Large) {
2046 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00002048 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002050 .addImm(0)
2051 .addReg(TmpReg2);
2052 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00002053 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00002054 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2055 .addReg(TmpReg)
2056 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002057 }
2058
2059 return DestReg;
2060}
2061
Bill Schmidtccecf262013-08-30 02:29:45 +00002062// Materialize the address of a global value into a register, and return
2063// the register number (or zero if we failed to handle it).
2064unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2065 assert(VT == MVT::i64 && "Non-address!");
2066 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2067 unsigned DestReg = createResultReg(RC);
2068
2069 // Global values may be plain old object addresses, TLS object
2070 // addresses, constant pool entries, or jump tables. How we generate
2071 // code for these may depend on small, medium, or large code model.
2072 CodeModel::Model CModel = TM.getCodeModel();
2073
2074 // FIXME: Jump tables are not yet required because fast-isel doesn't
2075 // handle switches; if that changes, we need them as well. For now,
2076 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00002077
2078 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002079 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00002080 return 0;
2081
Hal Finkele6698d52015-02-01 15:03:28 +00002082 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00002083 // For small code model, generate a simple TOC load.
Rafael Espindola79e238a2017-08-03 02:16:21 +00002084 if (CModel == CodeModel::Small)
Rafael Espindolaea09c592014-02-18 22:05:46 +00002085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2086 DestReg)
2087 .addGlobalAddress(GV)
2088 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00002089 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00002090 // If the address is an externally defined symbol, a symbol with common
2091 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00002092 // jump table address (not yet needed), or if we are generating code
2093 // for large code model, we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002094 // LDtocL(GV, ADDIStocHA(%x2, GV))
Bill Schmidtccecf262013-08-30 02:29:45 +00002095 // Otherwise we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002096 // ADDItocL(ADDIStocHA(%x2, GV), GV)
Bill Schmidtccecf262013-08-30 02:29:45 +00002097 // Either way, start with the ADDIStocHA:
2098 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002099 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00002100 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2101
Eric Christopherc1808362015-11-20 20:51:31 +00002102 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2103 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002105 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00002106 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00002107 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002109 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00002110 }
Bill Schmidtccecf262013-08-30 02:29:45 +00002111 }
2112
2113 return DestReg;
2114}
2115
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002116// Materialize a 32-bit integer constant into a register, and return
2117// the register number (or zero if we failed to handle it).
2118unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2119 const TargetRegisterClass *RC) {
2120 unsigned Lo = Imm & 0xFFFF;
2121 unsigned Hi = (Imm >> 16) & 0xFFFF;
2122
2123 unsigned ResultReg = createResultReg(RC);
2124 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2125
2126 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002128 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2129 .addImm(Imm);
2130 else if (Lo) {
2131 // Both Lo and Hi have nonzero bits.
2132 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002134 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2135 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002137 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2138 .addReg(TmpReg).addImm(Lo);
2139 } else
2140 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002142 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002143 .addImm(Hi);
2144
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002145 return ResultReg;
2146}
2147
2148// Materialize a 64-bit integer constant into a register, and return
2149// the register number (or zero if we failed to handle it).
2150unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2151 const TargetRegisterClass *RC) {
2152 unsigned Remainder = 0;
2153 unsigned Shift = 0;
2154
2155 // If the value doesn't fit in 32 bits, see if we can shift it
2156 // so that it fits in 32 bits.
2157 if (!isInt<32>(Imm)) {
2158 Shift = countTrailingZeros<uint64_t>(Imm);
2159 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2160
2161 if (isInt<32>(ImmSh))
2162 Imm = ImmSh;
2163 else {
2164 Remainder = Imm;
2165 Shift = 32;
2166 Imm >>= 32;
2167 }
2168 }
2169
2170 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2171 // (if not shifted).
2172 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2173 if (!Shift)
2174 return TmpReg1;
2175
2176 // If upper 32 bits were not zero, we've built them and need to shift
2177 // them into place.
2178 unsigned TmpReg2;
2179 if (Imm) {
2180 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002182 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2183 } else
2184 TmpReg2 = TmpReg1;
2185
2186 unsigned TmpReg3, Hi, Lo;
2187 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2188 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002190 TmpReg3).addReg(TmpReg2).addImm(Hi);
2191 } else
2192 TmpReg3 = TmpReg2;
2193
2194 if ((Lo = Remainder & 0xFFFF)) {
2195 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002197 ResultReg).addReg(TmpReg3).addImm(Lo);
2198 return ResultReg;
2199 }
2200
2201 return TmpReg3;
2202}
2203
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002204// Materialize an integer constant into a register, and return
2205// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002206unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2207 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002208 // If we're using CR bit registers for i1 values, handle that as a special
2209 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002210 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002211 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2213 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2214 return ImmReg;
2215 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002216
Eric Christopher80ba58a2016-01-29 07:19:49 +00002217 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2218 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002219 return 0;
2220
Eric Christopher80ba58a2016-01-29 07:19:49 +00002221 const TargetRegisterClass *RC =
2222 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002223 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002224
2225 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002226 // Since LI will sign extend the constant we need to make sure that for
2227 // our zeroext constants that the sign extended constant fits into 16-bits -
2228 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002229 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002230 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2231 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002233 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002234 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002235 }
2236
2237 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002238 if (VT == MVT::i64)
2239 return PPCMaterialize64BitInt(Imm, RC);
2240 else if (VT == MVT::i32)
2241 return PPCMaterialize32BitInt(Imm, RC);
2242
2243 return 0;
2244}
2245
2246// Materialize a constant into a register, and return the register
2247// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002248unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002249 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002250
2251 // Only handle simple types.
2252 if (!CEVT.isSimple()) return 0;
2253 MVT VT = CEVT.getSimpleVT();
2254
2255 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2256 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002257 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2258 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002259 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Hal Finkel73390c72016-09-04 06:07:19 +00002260 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2261 // assumes that constant PHI operands will be zero extended, and failure to
2262 // match that assumption will cause problems if we sign extend here but
2263 // some user of a PHI is in a block for which we fall back to full SDAG
2264 // instruction selection.
2265 return PPCMaterializeInt(CI, VT, false);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002266
2267 return 0;
2268}
2269
2270// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002271// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002272unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002273 // Don't handle dynamic allocas.
2274 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2275
2276 MVT VT;
2277 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2278
2279 DenseMap<const AllocaInst*, int>::iterator SI =
2280 FuncInfo.StaticAllocaMap.find(AI);
2281
2282 if (SI != FuncInfo.StaticAllocaMap.end()) {
2283 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002285 ResultReg).addFrameIndex(SI->second).addImm(0);
2286 return ResultReg;
2287 }
2288
2289 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002290}
2291
Bill Schmidtccecf262013-08-30 02:29:45 +00002292// Fold loads into extends when possible.
2293// FIXME: We can have multiple redundant extend/trunc instructions
2294// following a load. The folding only picks up one. Extend this
2295// to check subsequent instructions for the same pattern and remove
2296// them. Thus ResultReg should be the def reg for the last redundant
2297// instruction in a chain, and all intervening instructions can be
2298// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2299// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002300bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2301 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002302 // Verify we have a legal type before going any further.
2303 MVT VT;
2304 if (!isLoadTypeLegal(LI->getType(), VT))
2305 return false;
2306
2307 // Combine load followed by zero- or sign-extend.
2308 bool IsZExt = false;
2309 switch(MI->getOpcode()) {
2310 default:
2311 return false;
2312
2313 case PPC::RLDICL:
2314 case PPC::RLDICL_32_64: {
2315 IsZExt = true;
2316 unsigned MB = MI->getOperand(3).getImm();
2317 if ((VT == MVT::i8 && MB <= 56) ||
2318 (VT == MVT::i16 && MB <= 48) ||
2319 (VT == MVT::i32 && MB <= 32))
2320 break;
2321 return false;
2322 }
2323
2324 case PPC::RLWINM:
2325 case PPC::RLWINM8: {
2326 IsZExt = true;
2327 unsigned MB = MI->getOperand(3).getImm();
2328 if ((VT == MVT::i8 && MB <= 24) ||
2329 (VT == MVT::i16 && MB <= 16))
2330 break;
2331 return false;
2332 }
2333
2334 case PPC::EXTSB:
2335 case PPC::EXTSB8:
2336 case PPC::EXTSB8_32_64:
2337 /* There is no sign-extending load-byte instruction. */
2338 return false;
2339
2340 case PPC::EXTSH:
2341 case PPC::EXTSH8:
2342 case PPC::EXTSH8_32_64: {
2343 if (VT != MVT::i16 && VT != MVT::i8)
2344 return false;
2345 break;
2346 }
2347
2348 case PPC::EXTSW:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002349 case PPC::EXTSW_32:
Bill Schmidtccecf262013-08-30 02:29:45 +00002350 case PPC::EXTSW_32_64: {
2351 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2352 return false;
2353 break;
2354 }
2355 }
2356
2357 // See if we can handle this address.
2358 Address Addr;
2359 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2360 return false;
2361
2362 unsigned ResultReg = MI->getOperand(0).getReg();
2363
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002364 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2365 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +00002366 return false;
2367
Tim Northover256a16d2018-12-17 17:25:53 +00002368 MachineBasicBlock::iterator I(MI);
2369 removeDeadCode(I, std::next(I));
Bill Schmidtccecf262013-08-30 02:29:45 +00002370 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002371}
2372
2373// Attempt to lower call arguments in a faster way than done by
2374// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002375bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002376 // Defer to normal argument lowering for now. It's reasonably
2377 // efficient. Consider doing something like ARM to handle the
2378 // case where all args fit in registers, no varargs, no float
2379 // or vector args.
2380 return false;
2381}
2382
Bill Schmidt03008132013-08-25 22:33:42 +00002383// Handle materializing integer constants into a register. This is not
2384// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002385unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002386
Bill Schmidt03008132013-08-25 22:33:42 +00002387 if (Opc != ISD::Constant)
2388 return 0;
2389
Hal Finkel940ab932014-02-28 00:27:01 +00002390 // If we're using CR bit registers for i1 values, handle that as a special
2391 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002392 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002393 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2395 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2396 return ImmReg;
2397 }
2398
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002399 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2400 VT != MVT::i1)
Bill Schmidt03008132013-08-25 22:33:42 +00002401 return 0;
2402
2403 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2404 &PPC::GPRCRegClass);
2405 if (VT == MVT::i64)
2406 return PPCMaterialize64BitInt(Imm, RC);
2407 else
2408 return PPCMaterialize32BitInt(Imm, RC);
2409}
2410
Bill Schmidtccecf262013-08-30 02:29:45 +00002411// Override for ADDI and ADDI8 to set the correct register class
2412// on RHS operand 0. The automatic infrastructure naively assumes
2413// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2414// for these cases. At the moment, none of the other automatically
2415// generated RI instructions require special treatment. However, once
2416// SelectSelect is implemented, "isel" requires similar handling.
2417//
2418// Also be conservative about the output register class. Avoid
2419// assigning R0 or X0 to the output register for GPRC and G8RC
2420// register classes, as any such result could be used in ADDI, etc.,
2421// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002422unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002423 const TargetRegisterClass *RC,
2424 unsigned Op0, bool Op0IsKill,
2425 uint64_t Imm) {
2426 if (MachineInstOpcode == PPC::ADDI)
2427 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2428 else if (MachineInstOpcode == PPC::ADDI8)
2429 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2430
2431 const TargetRegisterClass *UseRC =
2432 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2433 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2434
Juergen Ributzka88e32512014-09-03 20:56:59 +00002435 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002436 Op0, Op0IsKill, Imm);
2437}
2438
2439// Override for instructions with one register operand to avoid use of
2440// R0/X0. The automatic infrastructure isn't aware of the context so
2441// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002442unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002443 const TargetRegisterClass* RC,
2444 unsigned Op0, bool Op0IsKill) {
2445 const TargetRegisterClass *UseRC =
2446 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2447 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2448
Juergen Ributzka88e32512014-09-03 20:56:59 +00002449 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002450}
2451
2452// Override for instructions with two register operands to avoid use
2453// of R0/X0. The automatic infrastructure isn't aware of the context
2454// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002455unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002456 const TargetRegisterClass* RC,
2457 unsigned Op0, bool Op0IsKill,
2458 unsigned Op1, bool Op1IsKill) {
2459 const TargetRegisterClass *UseRC =
2460 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2461 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2462
Juergen Ributzka88e32512014-09-03 20:56:59 +00002463 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002464 Op1, Op1IsKill);
2465}
2466
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002467namespace llvm {
2468 // Create the fast instruction selector for PowerPC64 ELF.
2469 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2470 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002471 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002472 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002473 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002474 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002475 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002476 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002477}