blob: 19d941374004df3b870ed588fe25beb3fcd39a47 [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
864 unsigned CmpOpc;
865 bool NeedsExt = false;
866 switch (SrcVT.SimpleTy) {
867 default: return false;
868 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000869 if (HasSPE) {
870 switch (Pred) {
871 default: return false;
872 case PPC::PRED_EQ:
873 CmpOpc = PPC::EFSCMPEQ;
874 break;
875 case PPC::PRED_LT:
876 CmpOpc = PPC::EFSCMPLT;
877 break;
878 case PPC::PRED_GT:
879 CmpOpc = PPC::EFSCMPGT;
880 break;
881 }
882 } else
883 CmpOpc = PPC::FCMPUS;
Bill Schmidt03008132013-08-25 22:33:42 +0000884 break;
885 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000886 if (HasSPE) {
887 switch (Pred) {
888 default: return false;
889 case PPC::PRED_EQ:
890 CmpOpc = PPC::EFDCMPEQ;
891 break;
892 case PPC::PRED_LT:
893 CmpOpc = PPC::EFDCMPLT;
894 break;
895 case PPC::PRED_GT:
896 CmpOpc = PPC::EFDCMPGT;
897 break;
898 }
899 } else
900 CmpOpc = PPC::FCMPUD;
Bill Schmidt03008132013-08-25 22:33:42 +0000901 break;
902 case MVT::i1:
903 case MVT::i8:
904 case MVT::i16:
905 NeedsExt = true;
906 // Intentional fall-through.
907 case MVT::i32:
908 if (!UseImm)
909 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
910 else
911 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
912 break;
913 case MVT::i64:
914 if (!UseImm)
915 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
916 else
917 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
918 break;
919 }
920
921 unsigned SrcReg1 = getRegForValue(SrcValue1);
922 if (SrcReg1 == 0)
923 return false;
924
925 unsigned SrcReg2 = 0;
926 if (!UseImm) {
927 SrcReg2 = getRegForValue(SrcValue2);
928 if (SrcReg2 == 0)
929 return false;
930 }
931
932 if (NeedsExt) {
933 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
934 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
935 return false;
936 SrcReg1 = ExtReg;
937
938 if (!UseImm) {
939 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
940 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
941 return false;
942 SrcReg2 = ExtReg;
943 }
944 }
945
946 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000948 .addReg(SrcReg1).addReg(SrcReg2);
949 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000951 .addReg(SrcReg1).addImm(Imm);
952
953 return true;
954}
955
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000956// Attempt to fast-select a floating-point extend instruction.
957bool PPCFastISel::SelectFPExt(const Instruction *I) {
958 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000959 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
960 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000961
962 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
963 return false;
964
965 unsigned SrcReg = getRegForValue(Src);
966 if (!SrcReg)
967 return false;
968
969 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000970 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000971 return true;
972}
973
974// Attempt to fast-select a floating-point truncate instruction.
975bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
976 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000977 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
978 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000979
980 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
981 return false;
982
983 unsigned SrcReg = getRegForValue(Src);
984 if (!SrcReg)
985 return false;
986
987 // Round the result to single precision.
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000988 unsigned DestReg;
989
990 if (PPCSubTarget->hasSPE()) {
991 DestReg = createResultReg(&PPC::SPE4RCRegClass);
992 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
993 TII.get(PPC::EFSCFD), DestReg)
994 .addReg(SrcReg);
995 } else {
996 DestReg = createResultReg(&PPC::F4RCRegClass);
997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
998 TII.get(PPC::FRSP), DestReg)
999 .addReg(SrcReg);
1000 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001001
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001002 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001003 return true;
1004}
1005
1006// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +00001007// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001008// those should be used instead of moving via a stack slot when the
1009// subtarget permits.
1010// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1011// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1012// case to 8 bytes which produces tighter code but wastes stack space.
1013unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1014 bool IsSigned) {
1015
1016 // If necessary, extend 32-bit int to 64-bit.
1017 if (SrcVT == MVT::i32) {
1018 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1019 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1020 return 0;
1021 SrcReg = TmpReg;
1022 }
1023
1024 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1025 Address Addr;
1026 Addr.BaseType = Address::FrameIndexBase;
1027 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1028
1029 // Store the value from the GPR.
1030 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1031 return 0;
1032
1033 // Load the integer value into an FPR. The kind of load used depends
1034 // on a number of conditions.
1035 unsigned LoadOpc = PPC::LFD;
1036
1037 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +00001038 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001039 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001040 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +00001041 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001042 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001043 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +00001044 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001045 }
1046
1047 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1048 unsigned ResultReg = 0;
1049 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1050 return 0;
1051
1052 return ResultReg;
1053}
1054
1055// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001056// FIXME: Once fast-isel has better support for VSX, conversions using
1057// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001058bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1059 MVT DstVT;
1060 Type *DstTy = I->getType();
1061 if (!isTypeLegal(DstTy, DstVT))
1062 return false;
1063
1064 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1065 return false;
1066
1067 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001068 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001069 if (!SrcEVT.isSimple())
1070 return false;
1071
1072 MVT SrcVT = SrcEVT.getSimpleVT();
1073
1074 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1075 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1076 return false;
1077
1078 unsigned SrcReg = getRegForValue(Src);
1079 if (SrcReg == 0)
1080 return false;
1081
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001082 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1083 if (PPCSubTarget->hasSPE()) {
1084 unsigned Opc;
1085 if (DstVT == MVT::f32)
1086 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1087 else
1088 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1089
1090 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1091 // Generate the convert.
1092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1093 .addReg(SrcReg);
1094 updateValueMap(I, DestReg);
1095 return true;
1096 }
1097
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001098 // We can only lower an unsigned convert if we have the newer
1099 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001100 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001101 return false;
1102
1103 // FIXME: For now we require the newer floating-point conversion operations
1104 // (which are present only on P7 and A2 server models) when converting
1105 // to single-precision float. Otherwise we have to generate a lot of
1106 // fiddly code to avoid double rounding. If necessary, the fiddly code
1107 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001108 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001109 return false;
1110
1111 // Extend the input if necessary.
1112 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1113 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1114 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1115 return false;
1116 SrcVT = MVT::i64;
1117 SrcReg = TmpReg;
1118 }
1119
1120 // Move the integer value to an FPR.
1121 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1122 if (FPReg == 0)
1123 return false;
1124
1125 // Determine the opcode for the conversion.
1126 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1127 unsigned DestReg = createResultReg(RC);
1128 unsigned Opc;
1129
1130 if (DstVT == MVT::f32)
1131 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1132 else
1133 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1134
1135 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001137 .addReg(FPReg);
1138
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001139 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001140 return true;
1141}
1142
1143// Move the floating-point value in SrcReg into an integer destination
1144// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001145// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001146// those should be used instead of moving via a stack slot when the
1147// subtarget permits.
1148unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1149 unsigned SrcReg, bool IsSigned) {
1150 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1151 // Note that if have STFIWX available, we could use a 4-byte stack
1152 // slot for i32, but this being fast-isel we'll just go with the
1153 // easiest code gen possible.
1154 Address Addr;
1155 Addr.BaseType = Address::FrameIndexBase;
1156 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1157
1158 // Store the value from the FPR.
1159 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1160 return 0;
1161
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001162 // Reload it into a GPR. If we want an i32 on big endian, modify the
1163 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001164 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001165 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001166
1167 // Look at the currently assigned register for this instruction
1168 // to determine the required register class.
1169 unsigned AssignedReg = FuncInfo.ValueMap[I];
1170 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001171 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001172
1173 unsigned ResultReg = 0;
1174 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1175 return 0;
1176
1177 return ResultReg;
1178}
1179
1180// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001181// FIXME: Once fast-isel has better support for VSX, conversions using
1182// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001183bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1184 MVT DstVT, SrcVT;
1185 Type *DstTy = I->getType();
1186 if (!isTypeLegal(DstTy, DstVT))
1187 return false;
1188
1189 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1190 return false;
1191
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001192 // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
1193 if (DstVT == MVT::i64 && !IsSigned &&
1194 !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
Bill Schmidt83973ef2014-06-24 20:05:18 +00001195 return false;
1196
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001197 Value *Src = I->getOperand(0);
1198 Type *SrcTy = Src->getType();
1199 if (!isTypeLegal(SrcTy, SrcVT))
1200 return false;
1201
1202 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1203 return false;
1204
1205 unsigned SrcReg = getRegForValue(Src);
1206 if (SrcReg == 0)
1207 return false;
1208
1209 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001210 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001211 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1212 if (InRC == &PPC::F4RCRegClass) {
1213 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001215 TII.get(TargetOpcode::COPY), TmpReg)
1216 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001217 SrcReg = TmpReg;
1218 }
1219
1220 // Determine the opcode for the conversion, which takes place
1221 // entirely within FPRs.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001222 unsigned DestReg;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001223 unsigned Opc;
1224
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001225 if (PPCSubTarget->hasSPE()) {
1226 if (DstVT == MVT::i32) {
1227 DestReg = createResultReg(&PPC::GPRCRegClass);
1228 if (IsSigned)
1229 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1230 else
1231 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
1232 }
1233 } else {
1234 DestReg = createResultReg(&PPC::F8RCRegClass);
1235 if (DstVT == MVT::i32)
1236 if (IsSigned)
1237 Opc = PPC::FCTIWZ;
1238 else
1239 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001240 else
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001241 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1242 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001243
1244 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001246 .addReg(SrcReg);
1247
1248 // Now move the integer value from a float register to an integer register.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001249 unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
1250 PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1251
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001252 if (IntReg == 0)
1253 return false;
1254
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001255 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001256 return true;
1257}
1258
Bill Schmidtccecf262013-08-30 02:29:45 +00001259// Attempt to fast-select a binary integer operation that isn't already
1260// handled automatically.
1261bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001262 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001263
1264 // We can get here in the case when we have a binary operation on a non-legal
1265 // type and the target independent selector doesn't know how to handle it.
1266 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1267 return false;
1268
1269 // Look at the currently assigned register for this instruction
1270 // to determine the required register class. If there is no register,
1271 // make a conservative choice (don't assign R0).
1272 unsigned AssignedReg = FuncInfo.ValueMap[I];
1273 const TargetRegisterClass *RC =
1274 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1275 &PPC::GPRC_and_GPRC_NOR0RegClass);
1276 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1277
1278 unsigned Opc;
1279 switch (ISDOpcode) {
1280 default: return false;
1281 case ISD::ADD:
1282 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1283 break;
1284 case ISD::OR:
1285 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1286 break;
1287 case ISD::SUB:
1288 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1289 break;
1290 }
1291
1292 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1293 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1294 if (SrcReg1 == 0) return false;
1295
1296 // Handle case of small immediate operand.
1297 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1298 const APInt &CIVal = ConstInt->getValue();
1299 int Imm = (int)CIVal.getSExtValue();
1300 bool UseImm = true;
1301 if (isInt<16>(Imm)) {
1302 switch (Opc) {
1303 default:
1304 llvm_unreachable("Missing case!");
1305 case PPC::ADD4:
1306 Opc = PPC::ADDI;
1307 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1308 break;
1309 case PPC::ADD8:
1310 Opc = PPC::ADDI8;
1311 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1312 break;
1313 case PPC::OR:
1314 Opc = PPC::ORI;
1315 break;
1316 case PPC::OR8:
1317 Opc = PPC::ORI8;
1318 break;
1319 case PPC::SUBF:
1320 if (Imm == -32768)
1321 UseImm = false;
1322 else {
1323 Opc = PPC::ADDI;
1324 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1325 Imm = -Imm;
1326 }
1327 break;
1328 case PPC::SUBF8:
1329 if (Imm == -32768)
1330 UseImm = false;
1331 else {
1332 Opc = PPC::ADDI8;
1333 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1334 Imm = -Imm;
1335 }
1336 break;
1337 }
1338
1339 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001340 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1341 ResultReg)
1342 .addReg(SrcReg1)
1343 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001344 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001345 return true;
1346 }
1347 }
1348 }
1349
1350 // Reg-reg case.
1351 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1352 if (SrcReg2 == 0) return false;
1353
1354 // Reverse operands for subtract-from.
1355 if (ISDOpcode == ISD::SUB)
1356 std::swap(SrcReg1, SrcReg2);
1357
Rafael Espindolaea09c592014-02-18 22:05:46 +00001358 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001359 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001360 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001361 return true;
1362}
1363
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001364// Handle arguments to a call that we're attempting to fast-select.
1365// Return false if the arguments are too complex for us at the moment.
1366bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1367 SmallVectorImpl<unsigned> &ArgRegs,
1368 SmallVectorImpl<MVT> &ArgVTs,
1369 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1370 SmallVectorImpl<unsigned> &RegArgs,
1371 CallingConv::ID CC,
1372 unsigned &NumBytes,
1373 bool IsVarArg) {
1374 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001375 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001376
1377 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001378 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001379 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001380
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001381 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1382
1383 // Bail out if we can't handle any of the arguments.
1384 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1385 CCValAssign &VA = ArgLocs[I];
1386 MVT ArgVT = ArgVTs[VA.getValNo()];
1387
1388 // Skip vector arguments for now, as well as long double and
1389 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001390 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001391 !VA.isRegLoc() || VA.needsCustom())
1392 return false;
1393
1394 // Skip bit-converted arguments for now.
1395 if (VA.getLocInfo() == CCValAssign::BCvt)
1396 return false;
1397 }
1398
1399 // Get a count of how many bytes are to be pushed onto the stack.
1400 NumBytes = CCInfo.getNextStackOffset();
1401
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001402 // The prolog code of the callee may store up to 8 GPR argument registers to
1403 // the stack, allowing va_start to index over them in memory if its varargs.
1404 // Because we cannot tell if this is needed on the caller side, we have to
1405 // conservatively assume that it is needed. As such, make sure we have at
1406 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001407 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001408 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001409
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001410 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001412 TII.get(TII.getCallFrameSetupOpcode()))
Serge Pavlovd526b132017-05-09 13:35:13 +00001413 .addImm(NumBytes).addImm(0);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001414
1415 // Prepare to assign register arguments. Every argument uses up a
1416 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001417 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001418 unsigned NextGPR = PPC::X3;
1419 unsigned NextFPR = PPC::F1;
1420
1421 // Process arguments.
1422 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1423 CCValAssign &VA = ArgLocs[I];
1424 unsigned Arg = ArgRegs[VA.getValNo()];
1425 MVT ArgVT = ArgVTs[VA.getValNo()];
1426
1427 // Handle argument promotion and bitcasts.
1428 switch (VA.getLocInfo()) {
1429 default:
1430 llvm_unreachable("Unknown loc info!");
1431 case CCValAssign::Full:
1432 break;
1433 case CCValAssign::SExt: {
1434 MVT DestVT = VA.getLocVT();
1435 const TargetRegisterClass *RC =
1436 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1437 unsigned TmpReg = createResultReg(RC);
1438 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1439 llvm_unreachable("Failed to emit a sext!");
1440 ArgVT = DestVT;
1441 Arg = TmpReg;
1442 break;
1443 }
1444 case CCValAssign::AExt:
1445 case CCValAssign::ZExt: {
1446 MVT DestVT = VA.getLocVT();
1447 const TargetRegisterClass *RC =
1448 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1449 unsigned TmpReg = createResultReg(RC);
1450 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1451 llvm_unreachable("Failed to emit a zext!");
1452 ArgVT = DestVT;
1453 Arg = TmpReg;
1454 break;
1455 }
1456 case CCValAssign::BCvt: {
1457 // FIXME: Not yet handled.
1458 llvm_unreachable("Should have bailed before getting here!");
1459 break;
1460 }
1461 }
1462
1463 // Copy this argument to the appropriate register.
1464 unsigned ArgReg;
1465 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1466 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001467 if (CC != CallingConv::Fast)
1468 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001469 } else
1470 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001471
1472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1473 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001474 RegArgs.push_back(ArgReg);
1475 }
1476
1477 return true;
1478}
1479
1480// For a call that we've determined we can fast-select, finish the
1481// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001482bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1483 CallingConv::ID CC = CLI.CallConv;
1484
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001485 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001486 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001487 TII.get(TII.getCallFrameDestroyOpcode()))
1488 .addImm(NumBytes).addImm(0);
1489
1490 // Next, generate a copy to obtain the return value.
1491 // FIXME: No multi-register return values yet, though I don't foresee
1492 // any real difficulties there.
1493 if (RetVT != MVT::isVoid) {
1494 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001495 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001496 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1497 CCValAssign &VA = RVLocs[0];
1498 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1499 assert(VA.isRegLoc() && "Can only return in registers!");
1500
1501 MVT DestVT = VA.getValVT();
1502 MVT CopyVT = DestVT;
1503
1504 // Ints smaller than a register still arrive in a full 64-bit
1505 // register, so make sure we recognize this.
1506 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1507 CopyVT = MVT::i64;
1508
1509 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001510 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001511
1512 if (RetVT == CopyVT) {
1513 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1514 ResultReg = createResultReg(CpyRC);
1515
Rafael Espindolaea09c592014-02-18 22:05:46 +00001516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001517 TII.get(TargetOpcode::COPY), ResultReg)
1518 .addReg(SourcePhysReg);
1519
1520 // If necessary, round the floating result to single precision.
1521 } else if (CopyVT == MVT::f64) {
1522 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001523 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001524 ResultReg).addReg(SourcePhysReg);
1525
1526 // If only the low half of a general register is needed, generate
1527 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1528 // used along the fast-isel path (not lowered), and downstream logic
1529 // also doesn't like a direct subreg copy on a physical reg.)
1530 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1531 ResultReg = createResultReg(&PPC::GPRCRegClass);
1532 // Convert physical register from G8RC to GPRC.
1533 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001534 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001535 TII.get(TargetOpcode::COPY), ResultReg)
1536 .addReg(SourcePhysReg);
1537 }
1538
Bill Schmidt0954ea12013-08-30 23:25:30 +00001539 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001540 CLI.InRegs.push_back(SourcePhysReg);
1541 CLI.ResultReg = ResultReg;
1542 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001543 }
Hal Finkel934361a2015-01-14 01:07:51 +00001544
1545 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001546}
1547
Hal Finkel934361a2015-01-14 01:07:51 +00001548bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1549 CallingConv::ID CC = CLI.CallConv;
1550 bool IsTailCall = CLI.IsTailCall;
1551 bool IsVarArg = CLI.IsVarArg;
1552 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001553 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001554
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001555 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001556 return false;
1557
1558 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001559 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001560 return false;
1561
Hal Finkel934361a2015-01-14 01:07:51 +00001562 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001563 if (IsVarArg)
1564 return false;
1565
1566 // Handle simple calls for now, with legal return types and
1567 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001568 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001569 MVT RetVT;
1570 if (RetTy->isVoidTy())
1571 RetVT = MVT::isVoid;
1572 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1573 RetVT != MVT::i8)
1574 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001575 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1576 // We can't handle boolean returns when CR bits are in use.
1577 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001578
1579 // FIXME: No multi-register return values yet.
1580 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1581 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1582 RetVT != MVT::f64) {
1583 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001584 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001585 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1586 if (RVLocs.size() > 1)
1587 return false;
1588 }
1589
1590 // Bail early if more than 8 arguments, as we only currently
1591 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001592 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001593 if (NumArgs > 8)
1594 return false;
1595
1596 // Set up the argument vectors.
1597 SmallVector<Value*, 8> Args;
1598 SmallVector<unsigned, 8> ArgRegs;
1599 SmallVector<MVT, 8> ArgVTs;
1600 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1601
1602 Args.reserve(NumArgs);
1603 ArgRegs.reserve(NumArgs);
1604 ArgVTs.reserve(NumArgs);
1605 ArgFlags.reserve(NumArgs);
1606
Hal Finkel934361a2015-01-14 01:07:51 +00001607 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001608 // Only handle easy calls for now. It would be reasonably easy
1609 // to handle <= 8-byte structures passed ByVal in registers, but we
1610 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001611 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1612 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001613 return false;
1614
Hal Finkel934361a2015-01-14 01:07:51 +00001615 Value *ArgValue = CLI.OutVals[i];
1616 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001617 MVT ArgVT;
1618 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1619 return false;
1620
1621 if (ArgVT.isVector())
1622 return false;
1623
Hal Finkel934361a2015-01-14 01:07:51 +00001624 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001625 if (Arg == 0)
1626 return false;
1627
Hal Finkel934361a2015-01-14 01:07:51 +00001628 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001629 ArgRegs.push_back(Arg);
1630 ArgVTs.push_back(ArgVT);
1631 ArgFlags.push_back(Flags);
1632 }
1633
1634 // Process the arguments.
1635 SmallVector<unsigned, 8> RegArgs;
1636 unsigned NumBytes;
1637
1638 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1639 RegArgs, CC, NumBytes, IsVarArg))
1640 return false;
1641
Hal Finkel934361a2015-01-14 01:07:51 +00001642 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001643 // FIXME: No handling for function pointers yet. This requires
1644 // implementing the function descriptor (OPD) setup.
1645 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001646 if (!GV) {
1647 // patchpoints are a special case; they always dispatch to a pointer value.
1648 // However, we don't actually want to generate the indirect call sequence
1649 // here (that will be generated, as necessary, during asm printing), and
1650 // the call we generate here will be erased by FastISel::selectPatchpoint,
1651 // so don't try very hard...
1652 if (CLI.IsPatchPoint)
1653 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1654 else
1655 return false;
1656 } else {
1657 // Build direct call with NOP for TOC restore.
1658 // FIXME: We can and should optimize away the NOP for local calls.
1659 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1660 TII.get(PPC::BL8_NOP));
1661 // Add callee.
1662 MIB.addGlobalAddress(GV);
1663 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001664
1665 // Add implicit physical register uses to the call.
1666 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1667 MIB.addReg(RegArgs[II], RegState::Implicit);
1668
Hal Finkelaf519932015-01-19 07:20:27 +00001669 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1670 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001671 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001672 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001673
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001674 // Add a register mask with the call-preserved registers. Proper
1675 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001676 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001677
Hal Finkel934361a2015-01-14 01:07:51 +00001678 CLI.Call = MIB;
1679
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001680 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001681 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001682}
1683
Bill Schmidtd89f6782013-08-26 19:42:51 +00001684// Attempt to fast-select a return instruction.
1685bool PPCFastISel::SelectRet(const Instruction *I) {
1686
1687 if (!FuncInfo.CanLowerReturn)
1688 return false;
1689
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001690 if (TLI.supportSplitCSR(FuncInfo.MF))
1691 return false;
1692
Bill Schmidtd89f6782013-08-26 19:42:51 +00001693 const ReturnInst *Ret = cast<ReturnInst>(I);
1694 const Function &F = *I->getParent()->getParent();
1695
1696 // Build a list of return value registers.
1697 SmallVector<unsigned, 4> RetRegs;
1698 CallingConv::ID CC = F.getCallingConv();
1699
1700 if (Ret->getNumOperands() > 0) {
1701 SmallVector<ISD::OutputArg, 4> Outs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001702 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001703
1704 // Analyze operands of the call, assigning locations to each operand.
1705 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001706 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001707 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1708 const Value *RV = Ret->getOperand(0);
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001709
Bill Schmidtd89f6782013-08-26 19:42:51 +00001710 // FIXME: Only one output register for now.
1711 if (ValLocs.size() > 1)
1712 return false;
1713
Eric Christopherf0024d12015-07-25 00:48:08 +00001714 // Special case for returning a constant integer of any size - materialize
1715 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001716 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001717 CCValAssign &VA = ValLocs[0];
1718
1719 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001720 // We still need to worry about properly extending the sign. For example,
1721 // we could have only a single bit or a constant that needs zero
1722 // extension rather than sign extension. Make sure we pass the return
1723 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001724 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001725 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001726
Rafael Espindolaea09c592014-02-18 22:05:46 +00001727 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001728 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1729
Bill Schmidtd89f6782013-08-26 19:42:51 +00001730 RetRegs.push_back(RetReg);
1731
1732 } else {
1733 unsigned Reg = getRegForValue(RV);
1734
1735 if (Reg == 0)
1736 return false;
1737
1738 // Copy the result values into the output registers.
1739 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1740
1741 CCValAssign &VA = ValLocs[i];
1742 assert(VA.isRegLoc() && "Can only return in registers!");
1743 RetRegs.push_back(VA.getLocReg());
1744 unsigned SrcReg = Reg + VA.getValNo();
1745
Mehdi Amini44ede332015-07-09 02:09:04 +00001746 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001747 if (!RVEVT.isSimple())
1748 return false;
1749 MVT RVVT = RVEVT.getSimpleVT();
1750 MVT DestVT = VA.getLocVT();
1751
1752 if (RVVT != DestVT && RVVT != MVT::i8 &&
1753 RVVT != MVT::i16 && RVVT != MVT::i32)
1754 return false;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001755
Bill Schmidtd89f6782013-08-26 19:42:51 +00001756 if (RVVT != DestVT) {
1757 switch (VA.getLocInfo()) {
1758 default:
1759 llvm_unreachable("Unknown loc info!");
1760 case CCValAssign::Full:
1761 llvm_unreachable("Full value assign but types don't match?");
1762 case CCValAssign::AExt:
1763 case CCValAssign::ZExt: {
1764 const TargetRegisterClass *RC =
1765 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1766 unsigned TmpReg = createResultReg(RC);
1767 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1768 return false;
1769 SrcReg = TmpReg;
1770 break;
1771 }
1772 case CCValAssign::SExt: {
1773 const TargetRegisterClass *RC =
1774 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1775 unsigned TmpReg = createResultReg(RC);
1776 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1777 return false;
1778 SrcReg = TmpReg;
1779 break;
1780 }
1781 }
1782 }
1783
Rafael Espindolaea09c592014-02-18 22:05:46 +00001784 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001785 TII.get(TargetOpcode::COPY), RetRegs[i])
1786 .addReg(SrcReg);
1787 }
1788 }
1789 }
1790
Rafael Espindolaea09c592014-02-18 22:05:46 +00001791 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001792 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001793
1794 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1795 MIB.addReg(RetRegs[i], RegState::Implicit);
1796
1797 return true;
1798}
1799
Bill Schmidt03008132013-08-25 22:33:42 +00001800// Attempt to emit an integer extend of SrcReg into DestReg. Both
1801// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001802// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001803bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1804 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001805 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1806 return false;
1807 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1808 return false;
1809
1810 // Signed extensions use EXTSB, EXTSH, EXTSW.
1811 if (!IsZExt) {
1812 unsigned Opc;
1813 if (SrcVT == MVT::i8)
1814 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1815 else if (SrcVT == MVT::i16)
1816 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1817 else {
1818 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1819 Opc = PPC::EXTSW_32_64;
1820 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001822 .addReg(SrcReg);
1823
1824 // Unsigned 32-bit extensions use RLWINM.
1825 } else if (DestVT == MVT::i32) {
1826 unsigned MB;
1827 if (SrcVT == MVT::i8)
1828 MB = 24;
1829 else {
1830 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1831 MB = 16;
1832 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001833 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001834 DestReg)
1835 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1836
1837 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1838 } else {
1839 unsigned MB;
1840 if (SrcVT == MVT::i8)
1841 MB = 56;
1842 else if (SrcVT == MVT::i16)
1843 MB = 48;
1844 else
1845 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001847 TII.get(PPC::RLDICL_32_64), DestReg)
1848 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1849 }
1850
1851 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001852}
1853
1854// Attempt to fast-select an indirect branch instruction.
1855bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1856 unsigned AddrReg = getRegForValue(I->getOperand(0));
1857 if (AddrReg == 0)
1858 return false;
1859
Rafael Espindolaea09c592014-02-18 22:05:46 +00001860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001861 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001862 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001863
1864 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001865 for (const BasicBlock *SuccBB : IB->successors())
1866 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001867
1868 return true;
1869}
1870
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001871// Attempt to fast-select an integer truncate instruction.
1872bool PPCFastISel::SelectTrunc(const Instruction *I) {
1873 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001874 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1875 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001876
1877 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1878 return false;
1879
1880 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1881 return false;
1882
1883 unsigned SrcReg = getRegForValue(Src);
1884 if (!SrcReg)
1885 return false;
1886
1887 // The only interesting case is when we need to switch register classes.
1888 if (SrcVT == MVT::i64) {
1889 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001890 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1891 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001892 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1893 SrcReg = ResultReg;
1894 }
1895
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001896 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001897 return true;
1898}
1899
Bill Schmidtd89f6782013-08-26 19:42:51 +00001900// Attempt to fast-select an integer extend instruction.
1901bool PPCFastISel::SelectIntExt(const Instruction *I) {
1902 Type *DestTy = I->getType();
1903 Value *Src = I->getOperand(0);
1904 Type *SrcTy = Src->getType();
1905
1906 bool IsZExt = isa<ZExtInst>(I);
1907 unsigned SrcReg = getRegForValue(Src);
1908 if (!SrcReg) return false;
1909
1910 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001911 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1912 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001913 if (!SrcEVT.isSimple())
1914 return false;
1915 if (!DestEVT.isSimple())
1916 return false;
1917
1918 MVT SrcVT = SrcEVT.getSimpleVT();
1919 MVT DestVT = DestEVT.getSimpleVT();
1920
1921 // If we know the register class needed for the result of this
1922 // instruction, use it. Otherwise pick the register class of the
1923 // correct size that does not contain X0/R0, since we don't know
1924 // whether downstream uses permit that assignment.
1925 unsigned AssignedReg = FuncInfo.ValueMap[I];
1926 const TargetRegisterClass *RC =
1927 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1928 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1929 &PPC::GPRC_and_GPRC_NOR0RegClass));
1930 unsigned ResultReg = createResultReg(RC);
1931
1932 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1933 return false;
1934
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001935 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001936 return true;
1937}
1938
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001939// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001940// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001941bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001942
1943 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001944 case Instruction::Load:
1945 return SelectLoad(I);
1946 case Instruction::Store:
1947 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001948 case Instruction::Br:
1949 return SelectBranch(I);
1950 case Instruction::IndirectBr:
1951 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001952 case Instruction::FPExt:
1953 return SelectFPExt(I);
1954 case Instruction::FPTrunc:
1955 return SelectFPTrunc(I);
1956 case Instruction::SIToFP:
1957 return SelectIToFP(I, /*IsSigned*/ true);
1958 case Instruction::UIToFP:
1959 return SelectIToFP(I, /*IsSigned*/ false);
1960 case Instruction::FPToSI:
1961 return SelectFPToI(I, /*IsSigned*/ true);
1962 case Instruction::FPToUI:
1963 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001964 case Instruction::Add:
1965 return SelectBinaryIntOp(I, ISD::ADD);
1966 case Instruction::Or:
1967 return SelectBinaryIntOp(I, ISD::OR);
1968 case Instruction::Sub:
1969 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001970 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001971 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001972 case Instruction::Ret:
1973 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001974 case Instruction::Trunc:
1975 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001976 case Instruction::ZExt:
1977 case Instruction::SExt:
1978 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001979 // Here add other flavors of Instruction::XXX that automated
1980 // cases don't catch. For example, switches are terminators
1981 // that aren't yet handled.
1982 default:
1983 break;
1984 }
1985 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001986}
1987
1988// Materialize a floating-point constant into a register, and return
1989// the register number (or zero if we failed to handle it).
1990unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1991 // No plans to handle long double here.
1992 if (VT != MVT::f32 && VT != MVT::f64)
1993 return 0;
1994
1995 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001996 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001997 assert(Align > 0 && "Unexpectedly missing alignment information!");
1998 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001999 const bool HasSPE = PPCSubTarget->hasSPE();
2000 const TargetRegisterClass *RC;
2001 if (HasSPE)
2002 RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
2003 else
2004 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2005
Ulrich Weigandc3b495a2016-08-05 15:22:05 +00002006 unsigned DestReg = createResultReg(RC);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002007 CodeModel::Model CModel = TM.getCodeModel();
2008
Alex Lorenze40c8a22015-08-11 23:09:45 +00002009 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2010 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2011 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002012
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002013 unsigned Opc;
2014
2015 if (HasSPE)
2016 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2017 else
2018 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2019
Bill Schmidt03008132013-08-25 22:33:42 +00002020 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2021
Hal Finkele6698d52015-02-01 15:03:28 +00002022 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00002023 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
Rafael Espindola79e238a2017-08-03 02:16:21 +00002024 if (CModel == CodeModel::Small) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00002026 TmpReg)
2027 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00002029 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2030 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002031 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00002032 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002033 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00002034 // But for large code model, we must generate a LDtocL followed
2035 // by the LF[SD].
2036 if (CModel == CodeModel::Large) {
2037 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00002039 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002041 .addImm(0)
2042 .addReg(TmpReg2);
2043 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00002044 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00002045 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2046 .addReg(TmpReg)
2047 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002048 }
2049
2050 return DestReg;
2051}
2052
Bill Schmidtccecf262013-08-30 02:29:45 +00002053// Materialize the address of a global value into a register, and return
2054// the register number (or zero if we failed to handle it).
2055unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2056 assert(VT == MVT::i64 && "Non-address!");
2057 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2058 unsigned DestReg = createResultReg(RC);
2059
2060 // Global values may be plain old object addresses, TLS object
2061 // addresses, constant pool entries, or jump tables. How we generate
2062 // code for these may depend on small, medium, or large code model.
2063 CodeModel::Model CModel = TM.getCodeModel();
2064
2065 // FIXME: Jump tables are not yet required because fast-isel doesn't
2066 // handle switches; if that changes, we need them as well. For now,
2067 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00002068
2069 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002070 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00002071 return 0;
2072
Hal Finkele6698d52015-02-01 15:03:28 +00002073 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00002074 // For small code model, generate a simple TOC load.
Rafael Espindola79e238a2017-08-03 02:16:21 +00002075 if (CModel == CodeModel::Small)
Rafael Espindolaea09c592014-02-18 22:05:46 +00002076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2077 DestReg)
2078 .addGlobalAddress(GV)
2079 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00002080 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00002081 // If the address is an externally defined symbol, a symbol with common
2082 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00002083 // jump table address (not yet needed), or if we are generating code
2084 // for large code model, we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002085 // LDtocL(GV, ADDIStocHA(%x2, GV))
Bill Schmidtccecf262013-08-30 02:29:45 +00002086 // Otherwise we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002087 // ADDItocL(ADDIStocHA(%x2, GV), GV)
Bill Schmidtccecf262013-08-30 02:29:45 +00002088 // Either way, start with the ADDIStocHA:
2089 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00002091 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2092
Eric Christopherc1808362015-11-20 20:51:31 +00002093 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2094 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002096 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00002097 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00002098 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002099 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002100 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00002101 }
Bill Schmidtccecf262013-08-30 02:29:45 +00002102 }
2103
2104 return DestReg;
2105}
2106
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002107// Materialize a 32-bit integer constant into a register, and return
2108// the register number (or zero if we failed to handle it).
2109unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2110 const TargetRegisterClass *RC) {
2111 unsigned Lo = Imm & 0xFFFF;
2112 unsigned Hi = (Imm >> 16) & 0xFFFF;
2113
2114 unsigned ResultReg = createResultReg(RC);
2115 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2116
2117 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002118 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002119 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2120 .addImm(Imm);
2121 else if (Lo) {
2122 // Both Lo and Hi have nonzero bits.
2123 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002124 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002125 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2126 .addImm(Hi);
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::ORI : PPC::ORI8), ResultReg)
2129 .addReg(TmpReg).addImm(Lo);
2130 } else
2131 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002133 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002134 .addImm(Hi);
2135
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002136 return ResultReg;
2137}
2138
2139// Materialize a 64-bit integer constant into a register, and return
2140// the register number (or zero if we failed to handle it).
2141unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2142 const TargetRegisterClass *RC) {
2143 unsigned Remainder = 0;
2144 unsigned Shift = 0;
2145
2146 // If the value doesn't fit in 32 bits, see if we can shift it
2147 // so that it fits in 32 bits.
2148 if (!isInt<32>(Imm)) {
2149 Shift = countTrailingZeros<uint64_t>(Imm);
2150 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2151
2152 if (isInt<32>(ImmSh))
2153 Imm = ImmSh;
2154 else {
2155 Remainder = Imm;
2156 Shift = 32;
2157 Imm >>= 32;
2158 }
2159 }
2160
2161 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2162 // (if not shifted).
2163 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2164 if (!Shift)
2165 return TmpReg1;
2166
2167 // If upper 32 bits were not zero, we've built them and need to shift
2168 // them into place.
2169 unsigned TmpReg2;
2170 if (Imm) {
2171 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002173 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2174 } else
2175 TmpReg2 = TmpReg1;
2176
2177 unsigned TmpReg3, Hi, Lo;
2178 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2179 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002181 TmpReg3).addReg(TmpReg2).addImm(Hi);
2182 } else
2183 TmpReg3 = TmpReg2;
2184
2185 if ((Lo = Remainder & 0xFFFF)) {
2186 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002187 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002188 ResultReg).addReg(TmpReg3).addImm(Lo);
2189 return ResultReg;
2190 }
2191
2192 return TmpReg3;
2193}
2194
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002195// Materialize an integer constant into a register, and return
2196// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002197unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2198 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002199 // If we're using CR bit registers for i1 values, handle that as a special
2200 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002201 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002202 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2203 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2204 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2205 return ImmReg;
2206 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002207
Eric Christopher80ba58a2016-01-29 07:19:49 +00002208 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2209 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002210 return 0;
2211
Eric Christopher80ba58a2016-01-29 07:19:49 +00002212 const TargetRegisterClass *RC =
2213 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002214 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002215
2216 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002217 // Since LI will sign extend the constant we need to make sure that for
2218 // our zeroext constants that the sign extended constant fits into 16-bits -
2219 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002220 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002221 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2222 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002224 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002225 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002226 }
2227
2228 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002229 if (VT == MVT::i64)
2230 return PPCMaterialize64BitInt(Imm, RC);
2231 else if (VT == MVT::i32)
2232 return PPCMaterialize32BitInt(Imm, RC);
2233
2234 return 0;
2235}
2236
2237// Materialize a constant into a register, and return the register
2238// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002239unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002240 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002241
2242 // Only handle simple types.
2243 if (!CEVT.isSimple()) return 0;
2244 MVT VT = CEVT.getSimpleVT();
2245
2246 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2247 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002248 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2249 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002250 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Hal Finkel73390c72016-09-04 06:07:19 +00002251 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2252 // assumes that constant PHI operands will be zero extended, and failure to
2253 // match that assumption will cause problems if we sign extend here but
2254 // some user of a PHI is in a block for which we fall back to full SDAG
2255 // instruction selection.
2256 return PPCMaterializeInt(CI, VT, false);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002257
2258 return 0;
2259}
2260
2261// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002262// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002263unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002264 // Don't handle dynamic allocas.
2265 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2266
2267 MVT VT;
2268 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2269
2270 DenseMap<const AllocaInst*, int>::iterator SI =
2271 FuncInfo.StaticAllocaMap.find(AI);
2272
2273 if (SI != FuncInfo.StaticAllocaMap.end()) {
2274 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002276 ResultReg).addFrameIndex(SI->second).addImm(0);
2277 return ResultReg;
2278 }
2279
2280 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002281}
2282
Bill Schmidtccecf262013-08-30 02:29:45 +00002283// Fold loads into extends when possible.
2284// FIXME: We can have multiple redundant extend/trunc instructions
2285// following a load. The folding only picks up one. Extend this
2286// to check subsequent instructions for the same pattern and remove
2287// them. Thus ResultReg should be the def reg for the last redundant
2288// instruction in a chain, and all intervening instructions can be
2289// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2290// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002291bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2292 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002293 // Verify we have a legal type before going any further.
2294 MVT VT;
2295 if (!isLoadTypeLegal(LI->getType(), VT))
2296 return false;
2297
2298 // Combine load followed by zero- or sign-extend.
2299 bool IsZExt = false;
2300 switch(MI->getOpcode()) {
2301 default:
2302 return false;
2303
2304 case PPC::RLDICL:
2305 case PPC::RLDICL_32_64: {
2306 IsZExt = true;
2307 unsigned MB = MI->getOperand(3).getImm();
2308 if ((VT == MVT::i8 && MB <= 56) ||
2309 (VT == MVT::i16 && MB <= 48) ||
2310 (VT == MVT::i32 && MB <= 32))
2311 break;
2312 return false;
2313 }
2314
2315 case PPC::RLWINM:
2316 case PPC::RLWINM8: {
2317 IsZExt = true;
2318 unsigned MB = MI->getOperand(3).getImm();
2319 if ((VT == MVT::i8 && MB <= 24) ||
2320 (VT == MVT::i16 && MB <= 16))
2321 break;
2322 return false;
2323 }
2324
2325 case PPC::EXTSB:
2326 case PPC::EXTSB8:
2327 case PPC::EXTSB8_32_64:
2328 /* There is no sign-extending load-byte instruction. */
2329 return false;
2330
2331 case PPC::EXTSH:
2332 case PPC::EXTSH8:
2333 case PPC::EXTSH8_32_64: {
2334 if (VT != MVT::i16 && VT != MVT::i8)
2335 return false;
2336 break;
2337 }
2338
2339 case PPC::EXTSW:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002340 case PPC::EXTSW_32:
Bill Schmidtccecf262013-08-30 02:29:45 +00002341 case PPC::EXTSW_32_64: {
2342 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2343 return false;
2344 break;
2345 }
2346 }
2347
2348 // See if we can handle this address.
2349 Address Addr;
2350 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2351 return false;
2352
2353 unsigned ResultReg = MI->getOperand(0).getReg();
2354
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002355 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2356 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +00002357 return false;
2358
2359 MI->eraseFromParent();
2360 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002361}
2362
2363// Attempt to lower call arguments in a faster way than done by
2364// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002365bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002366 // Defer to normal argument lowering for now. It's reasonably
2367 // efficient. Consider doing something like ARM to handle the
2368 // case where all args fit in registers, no varargs, no float
2369 // or vector args.
2370 return false;
2371}
2372
Bill Schmidt03008132013-08-25 22:33:42 +00002373// Handle materializing integer constants into a register. This is not
2374// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002375unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002376
Bill Schmidt03008132013-08-25 22:33:42 +00002377 if (Opc != ISD::Constant)
2378 return 0;
2379
Hal Finkel940ab932014-02-28 00:27:01 +00002380 // If we're using CR bit registers for i1 values, handle that as a special
2381 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002382 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002383 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2385 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2386 return ImmReg;
2387 }
2388
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002389 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2390 VT != MVT::i1)
Bill Schmidt03008132013-08-25 22:33:42 +00002391 return 0;
2392
2393 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2394 &PPC::GPRCRegClass);
2395 if (VT == MVT::i64)
2396 return PPCMaterialize64BitInt(Imm, RC);
2397 else
2398 return PPCMaterialize32BitInt(Imm, RC);
2399}
2400
Bill Schmidtccecf262013-08-30 02:29:45 +00002401// Override for ADDI and ADDI8 to set the correct register class
2402// on RHS operand 0. The automatic infrastructure naively assumes
2403// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2404// for these cases. At the moment, none of the other automatically
2405// generated RI instructions require special treatment. However, once
2406// SelectSelect is implemented, "isel" requires similar handling.
2407//
2408// Also be conservative about the output register class. Avoid
2409// assigning R0 or X0 to the output register for GPRC and G8RC
2410// register classes, as any such result could be used in ADDI, etc.,
2411// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002412unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002413 const TargetRegisterClass *RC,
2414 unsigned Op0, bool Op0IsKill,
2415 uint64_t Imm) {
2416 if (MachineInstOpcode == PPC::ADDI)
2417 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2418 else if (MachineInstOpcode == PPC::ADDI8)
2419 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2420
2421 const TargetRegisterClass *UseRC =
2422 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2423 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2424
Juergen Ributzka88e32512014-09-03 20:56:59 +00002425 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002426 Op0, Op0IsKill, Imm);
2427}
2428
2429// Override for instructions with one register operand to avoid use of
2430// R0/X0. The automatic infrastructure isn't aware of the context so
2431// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002432unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002433 const TargetRegisterClass* RC,
2434 unsigned Op0, bool Op0IsKill) {
2435 const TargetRegisterClass *UseRC =
2436 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2437 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2438
Juergen Ributzka88e32512014-09-03 20:56:59 +00002439 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002440}
2441
2442// Override for instructions with two register operands to avoid use
2443// of R0/X0. The automatic infrastructure isn't aware of the context
2444// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002445unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002446 const TargetRegisterClass* RC,
2447 unsigned Op0, bool Op0IsKill,
2448 unsigned Op1, bool Op1IsKill) {
2449 const TargetRegisterClass *UseRC =
2450 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2451 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2452
Juergen Ributzka88e32512014-09-03 20:56:59 +00002453 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002454 Op1, Op1IsKill);
2455}
2456
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002457namespace llvm {
2458 // Create the fast instruction selector for PowerPC64 ELF.
2459 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2460 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002461 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002462 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002463 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002464 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002465 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002466 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002467}