blob: a597c5ef6f6e967a555fd8c97bfe3382b984c98d [file] [log] [blame]
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001//===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bill Schmidt0cf702f2013-07-30 00:50:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the PowerPC-specific support for the FastISel class. Some
10// of the target-specific code is generated by tablegen in the file
11// PPCGenFastISel.inc, which is #included here.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "PPC.h"
Strahinja Petrovice682b802016-05-09 12:27:39 +000017#include "PPCCCState.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "PPCCallingConv.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000019#include "PPCISelLowering.h"
Hal Finkele6698d52015-02-01 15:03:28 +000020#include "PPCMachineFunctionInfo.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000021#include "PPCSubtarget.h"
22#include "PPCTargetMachine.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000023#include "llvm/ADT/Optional.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetLowering.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000032#include "llvm/IR/CallingConv.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000033#include "llvm/IR/GetElementPtrTypeIterator.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000034#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/Support/Debug.h"
Bill Schmidt0cf702f2013-07-30 00:50:39 +000039#include "llvm/Target/TargetMachine.h"
40
Bill Schmidteb8d6f72013-08-31 02:33:40 +000041//===----------------------------------------------------------------------===//
42//
43// TBD:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +000044// fastLowerArguments: Handle simple cases.
Bill Schmidteb8d6f72013-08-31 02:33:40 +000045// PPCMaterializeGV: Handle TLS.
46// SelectCall: Handle function pointers.
47// SelectCall: Handle multi-register return values.
48// SelectCall: Optimize away nops for local calls.
49// processCallArgs: Handle bit-converted arguments.
50// finishCall: Handle multi-register return values.
51// PPCComputeAddress: Handle parameter references as FrameIndex's.
52// PPCEmitCmp: Handle immediate as operand 1.
53// SelectCall: Handle small byval arguments.
54// SelectIntrinsicCall: Implement.
55// SelectSelect: Implement.
56// Consider factoring isTypeLegal into the base class.
57// Implement switches and jump tables.
58//
59//===----------------------------------------------------------------------===//
Bill Schmidt0cf702f2013-07-30 00:50:39 +000060using namespace llvm;
61
Chandler Carruth84e68b22014-04-22 02:41:26 +000062#define DEBUG_TYPE "ppcfastisel"
63
Bill Schmidt0cf702f2013-07-30 00:50:39 +000064namespace {
65
66typedef struct Address {
67 enum {
68 RegBase,
69 FrameIndexBase
70 } BaseType;
71
72 union {
73 unsigned Reg;
74 int FI;
75 } Base;
76
Bill Schmidtccecf262013-08-30 02:29:45 +000077 long Offset;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000078
79 // Innocuous defaults for our address.
80 Address()
81 : BaseType(RegBase), Offset(0) {
82 Base.Reg = 0;
83 }
84} Address;
85
Craig Topper26696312014-03-18 07:27:13 +000086class PPCFastISel final : public FastISel {
Bill Schmidt0cf702f2013-07-30 00:50:39 +000087
88 const TargetMachine &TM;
Eric Christopher85806142015-01-30 02:11:24 +000089 const PPCSubtarget *PPCSubTarget;
Hal Finkele6698d52015-02-01 15:03:28 +000090 PPCFunctionInfo *PPCFuncInfo;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000091 const TargetInstrInfo &TII;
92 const TargetLowering &TLI;
Bill Schmidt0cf702f2013-07-30 00:50:39 +000093 LLVMContext *Context;
94
95 public:
96 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
97 const TargetLibraryInfo *LibInfo)
Eric Christopherd9134482014-08-04 21:25:23 +000098 : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
Eric Christophercccae792015-01-30 22:02:31 +000099 PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
Hal Finkele6698d52015-02-01 15:03:28 +0000100 PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
Eric Christopher85806142015-01-30 02:11:24 +0000101 TII(*PPCSubTarget->getInstrInfo()),
102 TLI(*PPCSubTarget->getTargetLowering()),
Eric Christopherd9134482014-08-04 21:25:23 +0000103 Context(&FuncInfo.Fn->getContext()) {}
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000104
105 // Backend specific FastISel code.
106 private:
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000107 bool fastSelectInstruction(const Instruction *I) override;
108 unsigned fastMaterializeConstant(const Constant *C) override;
109 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000110 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
111 const LoadInst *LI) override;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000112 bool fastLowerArguments() override;
Juergen Ributzka88e32512014-09-03 20:56:59 +0000113 unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
114 unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 uint64_t Imm);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000118 unsigned fastEmitInst_r(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill);
Juergen Ributzka88e32512014-09-03 20:56:59 +0000121 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000122 const TargetRegisterClass *RC,
123 unsigned Op0, bool Op0IsKill,
124 unsigned Op1, bool Op1IsKill);
Bill Schmidt03008132013-08-25 22:33:42 +0000125
Hal Finkel934361a2015-01-14 01:07:51 +0000126 bool fastLowerCall(CallLoweringInfo &CLI) override;
127
Bill Schmidt03008132013-08-25 22:33:42 +0000128 // Instruction selection routines.
129 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000130 bool SelectLoad(const Instruction *I);
131 bool SelectStore(const Instruction *I);
Bill Schmidt03008132013-08-25 22:33:42 +0000132 bool SelectBranch(const Instruction *I);
133 bool SelectIndirectBr(const Instruction *I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000134 bool SelectFPExt(const Instruction *I);
135 bool SelectFPTrunc(const Instruction *I);
136 bool SelectIToFP(const Instruction *I, bool IsSigned);
137 bool SelectFPToI(const Instruction *I, bool IsSigned);
Bill Schmidtccecf262013-08-30 02:29:45 +0000138 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000139 bool SelectRet(const Instruction *I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +0000140 bool SelectTrunc(const Instruction *I);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000141 bool SelectIntExt(const Instruction *I);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000142
143 // Utility routines.
144 private:
Bill Schmidtccecf262013-08-30 02:29:45 +0000145 bool isTypeLegal(Type *Ty, MVT &VT);
146 bool isLoadTypeLegal(Type *Ty, MVT &VT);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000147 bool isValueAvailable(const Value *V) const;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000148 bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
149 return RC->getID() == PPC::VSFRCRegClassID;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000150 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000151 bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
152 return RC->getID() == PPC::VSSRCRegClassID;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000153 }
Bill Schmidt03008132013-08-25 22:33:42 +0000154 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000155 bool isZExt, unsigned DestReg,
156 const PPC::Predicate Pred);
Bill Schmidtccecf262013-08-30 02:29:45 +0000157 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
158 const TargetRegisterClass *RC, bool IsZExt = true,
159 unsigned FP64LoadOpc = PPC::LFD);
160 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
161 bool PPCComputeAddress(const Value *Obj, Address &Addr);
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000162 void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000163 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000164 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
165 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000166 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000167 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000168 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
169 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000170 unsigned PPCMaterialize32BitInt(int64_t Imm,
171 const TargetRegisterClass *RC);
172 unsigned PPCMaterialize64BitInt(int64_t Imm,
173 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000174 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
175 unsigned SrcReg, bool IsSigned);
176 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000177
Bill Schmidtd89f6782013-08-26 19:42:51 +0000178 // Call handling routines.
179 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000180 bool processCallArgs(SmallVectorImpl<Value*> &Args,
181 SmallVectorImpl<unsigned> &ArgRegs,
182 SmallVectorImpl<MVT> &ArgVTs,
183 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
184 SmallVectorImpl<unsigned> &RegArgs,
185 CallingConv::ID CC,
186 unsigned &NumBytes,
187 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000188 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Rafael Espindola463aed82016-06-21 20:09:22 +0000189 LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000190
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000191 private:
192 #include "PPCGenFastISel.inc"
193
194};
195
196} // end anonymous namespace
197
Bill Schmidtd89f6782013-08-26 19:42:51 +0000198#include "PPCGenCallingConv.inc"
199
Rafael Espindola463aed82016-06-21 20:09:22 +0000200// Function whose sole purpose is to kill compiler warnings
201// stemming from unused functions included from PPCGenCallingConv.inc.
202CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
203 if (Flag == 1)
204 return CC_PPC32_SVR4;
205 else if (Flag == 2)
206 return CC_PPC32_SVR4_ByVal;
207 else if (Flag == 3)
208 return CC_PPC32_SVR4_VarArg;
Zaara Syeda1f59ae32018-01-30 16:17:22 +0000209 else if (Flag == 4)
210 return RetCC_PPC_Cold;
Rafael Espindola463aed82016-06-21 20:09:22 +0000211 else
212 return RetCC_PPC;
213}
214
Bill Schmidt03008132013-08-25 22:33:42 +0000215static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
216 switch (Pred) {
217 // These are not representable with any single compare.
218 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000219 case CmpInst::FCMP_TRUE:
220 // Major concern about the following 6 cases is NaN result. The comparison
221 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
222 // only one of which will be set. The result is generated by fcmpu
223 // instruction. However, bc instruction only inspects one of the first 3
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000224 // bits, so when un is set, bc instruction may jump to an undesired
Tim Shen5cdf7502016-03-17 22:27:58 +0000225 // place.
226 //
227 // More specifically, if we expect an unordered comparison and un is set, we
228 // expect to always go to true branch; in such case UEQ, UGT and ULT still
229 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
230 // since they are tested by inspecting !eq, !lt, !gt, respectively.
231 //
232 // Similarly, for ordered comparison, when un is set, we always expect the
233 // result to be false. In such case OGT, OLT and OEQ is good, since they are
234 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
235 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000236 case CmpInst::FCMP_UEQ:
237 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000238 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000239 case CmpInst::FCMP_OGE:
240 case CmpInst::FCMP_OLE:
241 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000242 default:
243 return Optional<PPC::Predicate>();
244
245 case CmpInst::FCMP_OEQ:
246 case CmpInst::ICMP_EQ:
247 return PPC::PRED_EQ;
248
249 case CmpInst::FCMP_OGT:
250 case CmpInst::ICMP_UGT:
251 case CmpInst::ICMP_SGT:
252 return PPC::PRED_GT;
253
Tim Shen5cdf7502016-03-17 22:27:58 +0000254 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000255 case CmpInst::ICMP_UGE:
256 case CmpInst::ICMP_SGE:
257 return PPC::PRED_GE;
258
259 case CmpInst::FCMP_OLT:
260 case CmpInst::ICMP_ULT:
261 case CmpInst::ICMP_SLT:
262 return PPC::PRED_LT;
263
Tim Shen5cdf7502016-03-17 22:27:58 +0000264 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000265 case CmpInst::ICMP_ULE:
266 case CmpInst::ICMP_SLE:
267 return PPC::PRED_LE;
268
Tim Shen5cdf7502016-03-17 22:27:58 +0000269 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000270 case CmpInst::ICMP_NE:
271 return PPC::PRED_NE;
272
273 case CmpInst::FCMP_ORD:
274 return PPC::PRED_NU;
275
276 case CmpInst::FCMP_UNO:
277 return PPC::PRED_UN;
278 }
279}
280
Bill Schmidtccecf262013-08-30 02:29:45 +0000281// Determine whether the type Ty is simple enough to be handled by
282// fast-isel, and return its equivalent machine type in VT.
283// FIXME: Copied directly from ARM -- factor into base class?
284bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000285 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000286
287 // Only handle simple types.
288 if (Evt == MVT::Other || !Evt.isSimple()) return false;
289 VT = Evt.getSimpleVT();
290
291 // Handle all legal types, i.e. a register that will directly hold this
292 // value.
293 return TLI.isTypeLegal(VT);
294}
295
296// Determine whether the type Ty is simple enough to be handled by
297// fast-isel as a load target, and return its equivalent machine type in VT.
298bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
299 if (isTypeLegal(Ty, VT)) return true;
300
301 // If this is a type than can be sign or zero-extended to a basic operation
302 // go ahead and accept it now.
303 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
304 return true;
305 }
306
307 return false;
308}
309
Hal Finkel5f2a1372015-05-23 12:18:10 +0000310bool PPCFastISel::isValueAvailable(const Value *V) const {
311 if (!isa<Instruction>(V))
312 return true;
313
314 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000315 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000316}
317
Bill Schmidtccecf262013-08-30 02:29:45 +0000318// Given a value Obj, create an Address object Addr that represents its
319// address. Return false if we can't handle it.
320bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000321 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000322 unsigned Opcode = Instruction::UserOp1;
323 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
324 // Don't walk into other basic blocks unless the object is an alloca from
325 // another block, otherwise it may not have a virtual register assigned.
326 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
327 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
328 Opcode = I->getOpcode();
329 U = I;
330 }
331 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
332 Opcode = C->getOpcode();
333 U = C;
334 }
335
336 switch (Opcode) {
337 default:
338 break;
339 case Instruction::BitCast:
340 // Look through bitcasts.
341 return PPCComputeAddress(U->getOperand(0), Addr);
342 case Instruction::IntToPtr:
343 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000344 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
345 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000346 return PPCComputeAddress(U->getOperand(0), Addr);
347 break;
348 case Instruction::PtrToInt:
349 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000350 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000351 return PPCComputeAddress(U->getOperand(0), Addr);
352 break;
353 case Instruction::GetElementPtr: {
354 Address SavedAddr = Addr;
355 long TmpOffset = Addr.Offset;
356
357 // Iterate through the GEP folding the constants into offsets where
358 // we can.
359 gep_type_iterator GTI = gep_type_begin(U);
360 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
361 II != IE; ++II, ++GTI) {
362 const Value *Op = *II;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000363 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000364 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000365 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
366 TmpOffset += SL->getElementOffset(Idx);
367 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000368 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000369 for (;;) {
370 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
371 // Constant-offset addressing.
372 TmpOffset += CI->getSExtValue() * S;
373 break;
374 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000375 if (canFoldAddIntoGEP(U, Op)) {
376 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000377 ConstantInt *CI =
378 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
379 TmpOffset += CI->getSExtValue() * S;
380 // Iterate on the other operand.
381 Op = cast<AddOperator>(Op)->getOperand(0);
382 continue;
383 }
384 // Unsupported
385 goto unsupported_gep;
386 }
387 }
388 }
389
390 // Try to grab the base operand now.
391 Addr.Offset = TmpOffset;
392 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
393
394 // We failed, restore everything and try the other options.
395 Addr = SavedAddr;
396
397 unsupported_gep:
398 break;
399 }
400 case Instruction::Alloca: {
401 const AllocaInst *AI = cast<AllocaInst>(Obj);
402 DenseMap<const AllocaInst*, int>::iterator SI =
403 FuncInfo.StaticAllocaMap.find(AI);
404 if (SI != FuncInfo.StaticAllocaMap.end()) {
405 Addr.BaseType = Address::FrameIndexBase;
406 Addr.Base.FI = SI->second;
407 return true;
408 }
409 break;
410 }
411 }
412
413 // FIXME: References to parameters fall through to the behavior
414 // below. They should be able to reference a frame index since
415 // they are stored to the stack, so we can get "ld rx, offset(r1)"
416 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
417 // just contain the parameter. Try to handle this with a FI.
418
419 // Try to get this in a register if nothing else has worked.
420 if (Addr.Base.Reg == 0)
421 Addr.Base.Reg = getRegForValue(Obj);
422
423 // Prevent assignment of base register to X0, which is inappropriate
424 // for loads and stores alike.
425 if (Addr.Base.Reg != 0)
426 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
427
428 return Addr.Base.Reg != 0;
429}
430
431// Fix up some addresses that can't be used directly. For example, if
432// an offset won't fit in an instruction field, we may need to move it
433// into an index register.
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000434void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000435 unsigned &IndexReg) {
436
437 // Check whether the offset fits in the instruction field.
438 if (!isInt<16>(Addr.Offset))
439 UseOffset = false;
440
441 // If this is a stack pointer and the offset needs to be simplified then
442 // put the alloca address into a register, set the base type back to
443 // register and continue. This should almost never happen.
444 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
445 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000447 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
448 Addr.Base.Reg = ResultReg;
449 Addr.BaseType = Address::RegBase;
450 }
451
452 if (!UseOffset) {
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000453 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
Bill Schmidtccecf262013-08-30 02:29:45 +0000454 const ConstantInt *Offset =
455 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
456 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
457 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
458 }
459}
460
461// Emit a load instruction if possible, returning true if we succeeded,
462// otherwise false. See commentary below for how the register class of
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000463// the load is determined.
Bill Schmidtccecf262013-08-30 02:29:45 +0000464bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
465 const TargetRegisterClass *RC,
466 bool IsZExt, unsigned FP64LoadOpc) {
467 unsigned Opc;
468 bool UseOffset = true;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000469 bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidtccecf262013-08-30 02:29:45 +0000470
471 // If ResultReg is given, it determines the register class of the load.
472 // Otherwise, RC is the register class to use. If the result of the
473 // load isn't anticipated in this block, both may be zero, in which
474 // case we must make a conservative guess. In particular, don't assign
475 // R0 or X0 to the result register, as the result may be used in a load,
476 // store, add-immediate, or isel that won't permit this. (Though
477 // perhaps the spill and reload of live-exit values would handle this?)
478 const TargetRegisterClass *UseRC =
479 (ResultReg ? MRI.getRegClass(ResultReg) :
480 (RC ? RC :
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000481 (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
482 (VT == MVT::f32 ? (HasSPE ? &PPC::SPE4RCRegClass : &PPC::F4RCRegClass) :
Bill Schmidtccecf262013-08-30 02:29:45 +0000483 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
484 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
485
486 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
487
488 switch (VT.SimpleTy) {
489 default: // e.g., vector types not handled
490 return false;
491 case MVT::i8:
492 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
493 break;
494 case MVT::i16:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000495 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
496 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
Bill Schmidtccecf262013-08-30 02:29:45 +0000497 break;
498 case MVT::i32:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000499 Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
500 : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
Bill Schmidtccecf262013-08-30 02:29:45 +0000501 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
502 UseOffset = false;
503 break;
504 case MVT::i64:
505 Opc = PPC::LD;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000506 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
Bill Schmidtccecf262013-08-30 02:29:45 +0000507 "64-bit load with 32-bit target??");
508 UseOffset = ((Addr.Offset & 3) == 0);
509 break;
510 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000511 Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000512 break;
513 case MVT::f64:
514 Opc = FP64LoadOpc;
515 break;
516 }
517
518 // If necessary, materialize the offset into a register and use
519 // the indexed form. Also handle stack pointers with special needs.
520 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000521 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000522
523 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
524 // be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000525 bool IsVSSRC = isVSSRCRegClass(UseRC);
526 bool IsVSFRC = isVSFRCRegClass(UseRC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000527 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000528 bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000529 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000530 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
531 (Addr.Offset == 0)) {
532 UseOffset = false;
533 }
534
Bill Schmidtccecf262013-08-30 02:29:45 +0000535 if (ResultReg == 0)
536 ResultReg = createResultReg(UseRC);
537
538 // Note: If we still have a frame index here, we know the offset is
539 // in range, as otherwise PPCSimplifyAddress would have converted it
540 // into a RegBase.
541 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000542 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000543 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000544
Alex Lorenze40c8a22015-08-11 23:09:45 +0000545 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
546 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
547 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000548 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
549 MFI.getObjectAlignment(Addr.Base.FI));
550
Rafael Espindolaea09c592014-02-18 22:05:46 +0000551 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000552 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
553
554 // Base reg with offset in range.
555 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000556 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000557 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000558
Rafael Espindolaea09c592014-02-18 22:05:46 +0000559 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000560 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
561
562 // Indexed form.
563 } else {
564 // Get the RR opcode corresponding to the RI one. FIXME: It would be
565 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
566 // is hard to get at.
567 switch (Opc) {
568 default: llvm_unreachable("Unexpected opcode!");
569 case PPC::LBZ: Opc = PPC::LBZX; break;
570 case PPC::LBZ8: Opc = PPC::LBZX8; break;
571 case PPC::LHZ: Opc = PPC::LHZX; break;
572 case PPC::LHZ8: Opc = PPC::LHZX8; break;
573 case PPC::LHA: Opc = PPC::LHAX; break;
574 case PPC::LHA8: Opc = PPC::LHAX8; break;
575 case PPC::LWZ: Opc = PPC::LWZX; break;
576 case PPC::LWZ8: Opc = PPC::LWZX8; break;
577 case PPC::LWA: Opc = PPC::LWAX; break;
578 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
579 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000580 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000581 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000582 case PPC::EVLDD: Opc = PPC::EVLDDX; break;
583 case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000584 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000585
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000586 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
587 ResultReg);
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000588
589 // If we have an index register defined we use it in the store inst,
590 // otherwise we use X0 as base as it makes the vector instructions to
591 // use zero in the computation of the effective address regardless the
592 // content of the register.
593 if (IndexReg)
594 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
595 else
596 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000597 }
598
599 return true;
600}
601
602// Attempt to fast-select a load instruction.
603bool PPCFastISel::SelectLoad(const Instruction *I) {
604 // FIXME: No atomic loads are supported.
605 if (cast<LoadInst>(I)->isAtomic())
606 return false;
607
608 // Verify we have a legal type before going any further.
609 MVT VT;
610 if (!isLoadTypeLegal(I->getType(), VT))
611 return false;
612
613 // See if we can handle this address.
614 Address Addr;
615 if (!PPCComputeAddress(I->getOperand(0), Addr))
616 return false;
617
618 // Look at the currently assigned register for this instruction
619 // to determine the required register class. This is necessary
620 // to constrain RA from using R0/X0 when this is not legal.
621 unsigned AssignedReg = FuncInfo.ValueMap[I];
622 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000623 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000624
625 unsigned ResultReg = 0;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000626 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
627 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +0000628 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000629 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000630 return true;
631}
632
633// Emit a store instruction to store SrcReg at Addr.
634bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
635 assert(SrcReg && "Nothing to store!");
636 unsigned Opc;
637 bool UseOffset = true;
638
639 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
640 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
641
642 switch (VT.SimpleTy) {
643 default: // e.g., vector types not handled
644 return false;
645 case MVT::i8:
646 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
647 break;
648 case MVT::i16:
649 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
650 break;
651 case MVT::i32:
652 assert(Is32BitInt && "Not GPRC for i32??");
653 Opc = PPC::STW;
654 break;
655 case MVT::i64:
656 Opc = PPC::STD;
657 UseOffset = ((Addr.Offset & 3) == 0);
658 break;
659 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000660 Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000661 break;
662 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000663 Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
Bill Schmidtccecf262013-08-30 02:29:45 +0000664 break;
665 }
666
667 // If necessary, materialize the offset into a register and use
668 // the indexed form. Also handle stack pointers with special needs.
669 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000670 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000671
Bill Seurer8c728ae2014-12-05 20:15:56 +0000672 // If this is a potential VSX store with an offset of 0, a VSX indexed store
673 // can be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000674 bool IsVSSRC = isVSSRCRegClass(RC);
675 bool IsVSFRC = isVSFRCRegClass(RC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000676 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
677 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
678 if ((Is32VSXStore || Is64VSXStore) &&
679 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000680 (Addr.Offset == 0)) {
681 UseOffset = false;
682 }
683
Bill Schmidtccecf262013-08-30 02:29:45 +0000684 // Note: If we still have a frame index here, we know the offset is
685 // in range, as otherwise PPCSimplifyAddress would have converted it
686 // into a RegBase.
687 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000688 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000689 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000690
Alex Lorenze40c8a22015-08-11 23:09:45 +0000691 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
692 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
693 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000694 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
695 MFI.getObjectAlignment(Addr.Base.FI));
696
Rafael Espindolaea09c592014-02-18 22:05:46 +0000697 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
698 .addReg(SrcReg)
699 .addImm(Addr.Offset)
700 .addFrameIndex(Addr.Base.FI)
701 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000702
703 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000704 } else if (UseOffset) {
705 // VSX only provides an indexed store.
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000706 if (Is32VSXStore || Is64VSXStore)
707 return false;
708
Rafael Espindolaea09c592014-02-18 22:05:46 +0000709 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000710 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
711
712 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000713 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000714 // Get the RR opcode corresponding to the RI one. FIXME: It would be
715 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
716 // is hard to get at.
717 switch (Opc) {
718 default: llvm_unreachable("Unexpected opcode!");
719 case PPC::STB: Opc = PPC::STBX; break;
720 case PPC::STH : Opc = PPC::STHX; break;
721 case PPC::STW : Opc = PPC::STWX; break;
722 case PPC::STB8: Opc = PPC::STBX8; break;
723 case PPC::STH8: Opc = PPC::STHX8; break;
724 case PPC::STW8: Opc = PPC::STWX8; break;
725 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000726 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000727 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000728 case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
729 case PPC::SPESTW: Opc = PPC::SPESTWX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000730 }
Samuel Antaof6815602015-03-17 15:00:57 +0000731
732 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
733 .addReg(SrcReg);
734
735 // If we have an index register defined we use it in the store inst,
736 // otherwise we use X0 as base as it makes the vector instructions to
737 // use zero in the computation of the effective address regardless the
738 // content of the register.
739 if (IndexReg)
740 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
741 else
742 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000743 }
744
745 return true;
746}
747
748// Attempt to fast-select a store instruction.
749bool PPCFastISel::SelectStore(const Instruction *I) {
750 Value *Op0 = I->getOperand(0);
751 unsigned SrcReg = 0;
752
753 // FIXME: No atomics loads are supported.
754 if (cast<StoreInst>(I)->isAtomic())
755 return false;
756
757 // Verify we have a legal type before going any further.
758 MVT VT;
759 if (!isLoadTypeLegal(Op0->getType(), VT))
760 return false;
761
762 // Get the value to be stored into a register.
763 SrcReg = getRegForValue(Op0);
764 if (SrcReg == 0)
765 return false;
766
767 // See if we can handle this address.
768 Address Addr;
769 if (!PPCComputeAddress(I->getOperand(1), Addr))
770 return false;
771
772 if (!PPCEmitStore(VT, SrcReg, Addr))
773 return false;
774
775 return true;
776}
777
Bill Schmidt03008132013-08-25 22:33:42 +0000778// Attempt to fast-select a branch instruction.
779bool PPCFastISel::SelectBranch(const Instruction *I) {
780 const BranchInst *BI = cast<BranchInst>(I);
781 MachineBasicBlock *BrBB = FuncInfo.MBB;
782 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
783 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
784
785 // For now, just try the simplest case where it's fed by a compare.
786 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000787 if (isValueAvailable(CI)) {
788 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
789 if (!OptPPCPred)
790 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000791
Hal Finkel5f2a1372015-05-23 12:18:10 +0000792 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000793
Hal Finkel5f2a1372015-05-23 12:18:10 +0000794 // Take advantage of fall-through opportunities.
795 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
796 std::swap(TBB, FBB);
797 PPCPred = PPC::InvertPredicate(PPCPred);
798 }
799
800 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
801
802 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000803 CondReg, PPCPred))
Hal Finkel5f2a1372015-05-23 12:18:10 +0000804 return false;
805
806 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000807 .addImm(PPCSubTarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
808 .addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000809 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000810 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000811 }
Bill Schmidt03008132013-08-25 22:33:42 +0000812 } else if (const ConstantInt *CI =
813 dyn_cast<ConstantInt>(BI->getCondition())) {
814 uint64_t Imm = CI->getZExtValue();
815 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000816 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000817 return true;
818 }
819
820 // FIXME: ARM looks for a case where the block containing the compare
821 // has been split from the block containing the branch. If this happens,
822 // there is a vreg available containing the result of the compare. I'm
823 // not sure we can do much, as we've lost the predicate information with
824 // the compare instruction -- we have a 4-bit CR but don't know which bit
825 // to test here.
826 return false;
827}
828
829// Attempt to emit a compare of the two source values. Signed and unsigned
830// comparisons are supported. Return false if we can't handle it.
831bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000832 bool IsZExt, unsigned DestReg,
833 const PPC::Predicate Pred) {
Bill Schmidt03008132013-08-25 22:33:42 +0000834 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000835 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000836 if (!SrcEVT.isSimple())
837 return false;
838 MVT SrcVT = SrcEVT.getSimpleVT();
839
Eric Christopher1b8e7632014-05-22 01:07:24 +0000840 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000841 return false;
842
Bill Schmidt03008132013-08-25 22:33:42 +0000843 // See if operand 2 is an immediate encodeable in the compare.
844 // FIXME: Operands are not in canonical order at -O0, so an immediate
845 // operand in position 1 is a lost opportunity for now. We are
846 // similar to ARM in this regard.
847 long Imm = 0;
848 bool UseImm = false;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000849 const bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidt03008132013-08-25 22:33:42 +0000850
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000851 // Only 16-bit integer constants can be represented in compares for
Bill Schmidt03008132013-08-25 22:33:42 +0000852 // PowerPC. Others will be materialized into a register.
853 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
854 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
855 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
856 const APInt &CIVal = ConstInt->getValue();
857 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
858 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
859 UseImm = true;
860 }
861 }
862
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000863 unsigned SrcReg1 = getRegForValue(SrcValue1);
864 if (SrcReg1 == 0)
865 return false;
866
867 unsigned SrcReg2 = 0;
868 if (!UseImm) {
869 SrcReg2 = getRegForValue(SrcValue2);
870 if (SrcReg2 == 0)
871 return false;
872 }
873
Bill Schmidt03008132013-08-25 22:33:42 +0000874 unsigned CmpOpc;
875 bool NeedsExt = false;
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000876
877 auto RC1 = MRI.getRegClass(SrcReg1);
878 auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;
879
Bill Schmidt03008132013-08-25 22:33:42 +0000880 switch (SrcVT.SimpleTy) {
881 default: return false;
882 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000883 if (HasSPE) {
884 switch (Pred) {
885 default: return false;
886 case PPC::PRED_EQ:
887 CmpOpc = PPC::EFSCMPEQ;
888 break;
889 case PPC::PRED_LT:
890 CmpOpc = PPC::EFSCMPLT;
891 break;
892 case PPC::PRED_GT:
893 CmpOpc = PPC::EFSCMPGT;
894 break;
895 }
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000896 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000897 CmpOpc = PPC::FCMPUS;
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000898 if (isVSSRCRegClass(RC1)) {
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000899 unsigned TmpReg = createResultReg(&PPC::F4RCRegClass);
900 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
901 TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg1);
902 SrcReg1 = TmpReg;
903 }
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000904 if (RC2 && isVSSRCRegClass(RC2)) {
905 unsigned TmpReg = createResultReg(&PPC::F4RCRegClass);
906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
907 TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg2);
908 SrcReg2 = TmpReg;
909 }
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000910 }
Bill Schmidt03008132013-08-25 22:33:42 +0000911 break;
912 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000913 if (HasSPE) {
914 switch (Pred) {
915 default: return false;
916 case PPC::PRED_EQ:
917 CmpOpc = PPC::EFDCMPEQ;
918 break;
919 case PPC::PRED_LT:
920 CmpOpc = PPC::EFDCMPLT;
921 break;
922 case PPC::PRED_GT:
923 CmpOpc = PPC::EFDCMPGT;
924 break;
925 }
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000926 } else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000927 CmpOpc = PPC::XSCMPUDP;
928 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000929 CmpOpc = PPC::FCMPUD;
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000930 }
Bill Schmidt03008132013-08-25 22:33:42 +0000931 break;
932 case MVT::i1:
933 case MVT::i8:
934 case MVT::i16:
935 NeedsExt = true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000936 LLVM_FALLTHROUGH;
Bill Schmidt03008132013-08-25 22:33:42 +0000937 case MVT::i32:
938 if (!UseImm)
939 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
940 else
941 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
942 break;
943 case MVT::i64:
944 if (!UseImm)
945 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
946 else
947 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
948 break;
949 }
950
Bill Schmidt03008132013-08-25 22:33:42 +0000951 if (NeedsExt) {
952 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
953 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
954 return false;
955 SrcReg1 = ExtReg;
956
957 if (!UseImm) {
958 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
959 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
960 return false;
961 SrcReg2 = ExtReg;
962 }
963 }
964
965 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000966 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000967 .addReg(SrcReg1).addReg(SrcReg2);
968 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000969 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000970 .addReg(SrcReg1).addImm(Imm);
971
972 return true;
973}
974
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000975// Attempt to fast-select a floating-point extend instruction.
976bool PPCFastISel::SelectFPExt(const Instruction *I) {
977 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000978 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
979 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000980
981 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
982 return false;
983
984 unsigned SrcReg = getRegForValue(Src);
985 if (!SrcReg)
986 return false;
987
988 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000989 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000990 return true;
991}
992
993// Attempt to fast-select a floating-point truncate instruction.
994bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
995 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000996 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
997 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000998
999 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1000 return false;
1001
1002 unsigned SrcReg = getRegForValue(Src);
1003 if (!SrcReg)
1004 return false;
1005
1006 // Round the result to single precision.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001007 unsigned DestReg;
1008
1009 if (PPCSubTarget->hasSPE()) {
1010 DestReg = createResultReg(&PPC::SPE4RCRegClass);
1011 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1012 TII.get(PPC::EFSCFD), DestReg)
1013 .addReg(SrcReg);
1014 } else {
1015 DestReg = createResultReg(&PPC::F4RCRegClass);
1016 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1017 TII.get(PPC::FRSP), DestReg)
1018 .addReg(SrcReg);
1019 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001020
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001021 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001022 return true;
1023}
1024
1025// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +00001026// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001027// those should be used instead of moving via a stack slot when the
1028// subtarget permits.
1029// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1030// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1031// case to 8 bytes which produces tighter code but wastes stack space.
1032unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1033 bool IsSigned) {
1034
1035 // If necessary, extend 32-bit int to 64-bit.
1036 if (SrcVT == MVT::i32) {
1037 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1038 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1039 return 0;
1040 SrcReg = TmpReg;
1041 }
1042
1043 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1044 Address Addr;
1045 Addr.BaseType = Address::FrameIndexBase;
1046 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1047
1048 // Store the value from the GPR.
1049 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1050 return 0;
1051
1052 // Load the integer value into an FPR. The kind of load used depends
1053 // on a number of conditions.
1054 unsigned LoadOpc = PPC::LFD;
1055
1056 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +00001057 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001058 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001059 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +00001060 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001061 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001062 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +00001063 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001064 }
1065
1066 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1067 unsigned ResultReg = 0;
1068 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1069 return 0;
1070
1071 return ResultReg;
1072}
1073
1074// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001075// FIXME: Once fast-isel has better support for VSX, conversions using
1076// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001077bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1078 MVT DstVT;
1079 Type *DstTy = I->getType();
1080 if (!isTypeLegal(DstTy, DstVT))
1081 return false;
1082
1083 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1084 return false;
1085
1086 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001087 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001088 if (!SrcEVT.isSimple())
1089 return false;
1090
1091 MVT SrcVT = SrcEVT.getSimpleVT();
1092
1093 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1094 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1095 return false;
1096
1097 unsigned SrcReg = getRegForValue(Src);
1098 if (SrcReg == 0)
1099 return false;
1100
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001101 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1102 if (PPCSubTarget->hasSPE()) {
1103 unsigned Opc;
1104 if (DstVT == MVT::f32)
1105 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1106 else
1107 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1108
1109 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1110 // Generate the convert.
1111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1112 .addReg(SrcReg);
1113 updateValueMap(I, DestReg);
1114 return true;
1115 }
1116
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001117 // We can only lower an unsigned convert if we have the newer
1118 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001119 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001120 return false;
1121
1122 // FIXME: For now we require the newer floating-point conversion operations
1123 // (which are present only on P7 and A2 server models) when converting
1124 // to single-precision float. Otherwise we have to generate a lot of
1125 // fiddly code to avoid double rounding. If necessary, the fiddly code
1126 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001127 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001128 return false;
1129
1130 // Extend the input if necessary.
1131 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1132 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1133 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1134 return false;
1135 SrcVT = MVT::i64;
1136 SrcReg = TmpReg;
1137 }
1138
1139 // Move the integer value to an FPR.
1140 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1141 if (FPReg == 0)
1142 return false;
1143
1144 // Determine the opcode for the conversion.
1145 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1146 unsigned DestReg = createResultReg(RC);
1147 unsigned Opc;
1148
1149 if (DstVT == MVT::f32)
1150 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1151 else
1152 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1153
1154 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001155 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001156 .addReg(FPReg);
1157
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001158 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001159 return true;
1160}
1161
1162// Move the floating-point value in SrcReg into an integer destination
1163// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001164// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001165// those should be used instead of moving via a stack slot when the
1166// subtarget permits.
1167unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1168 unsigned SrcReg, bool IsSigned) {
1169 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1170 // Note that if have STFIWX available, we could use a 4-byte stack
1171 // slot for i32, but this being fast-isel we'll just go with the
1172 // easiest code gen possible.
1173 Address Addr;
1174 Addr.BaseType = Address::FrameIndexBase;
1175 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1176
1177 // Store the value from the FPR.
1178 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1179 return 0;
1180
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001181 // Reload it into a GPR. If we want an i32 on big endian, modify the
1182 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001183 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001184 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001185
1186 // Look at the currently assigned register for this instruction
1187 // to determine the required register class.
1188 unsigned AssignedReg = FuncInfo.ValueMap[I];
1189 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001190 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001191
1192 unsigned ResultReg = 0;
1193 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1194 return 0;
1195
1196 return ResultReg;
1197}
1198
1199// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001200// FIXME: Once fast-isel has better support for VSX, conversions using
1201// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001202bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1203 MVT DstVT, SrcVT;
1204 Type *DstTy = I->getType();
1205 if (!isTypeLegal(DstTy, DstVT))
1206 return false;
1207
1208 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1209 return false;
1210
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001211 // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
1212 if (DstVT == MVT::i64 && !IsSigned &&
1213 !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
Bill Schmidt83973ef2014-06-24 20:05:18 +00001214 return false;
1215
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001216 Value *Src = I->getOperand(0);
1217 Type *SrcTy = Src->getType();
1218 if (!isTypeLegal(SrcTy, SrcVT))
1219 return false;
1220
1221 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1222 return false;
1223
1224 unsigned SrcReg = getRegForValue(Src);
1225 if (SrcReg == 0)
1226 return false;
1227
1228 // Convert f32 to f64 if necessary. This is just a meaningless copy
Ulrich Weigand1931b012016-03-31 14:44:50 +00001229 // to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001230 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1231 if (InRC == &PPC::F4RCRegClass) {
1232 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Ulrich Weigand1931b012016-03-31 14:44:50 +00001234 TII.get(TargetOpcode::COPY), TmpReg)
1235 .addReg(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001236 SrcReg = TmpReg;
1237 }
1238
1239 // Determine the opcode for the conversion, which takes place
1240 // entirely within FPRs.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001241 unsigned DestReg;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001242 unsigned Opc;
1243
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001244 if (PPCSubTarget->hasSPE()) {
Justin Hibbits22e939a2018-07-18 05:19:25 +00001245 DestReg = createResultReg(&PPC::GPRCRegClass);
1246 if (IsSigned)
1247 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1248 else
1249 Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001250 } else {
1251 DestReg = createResultReg(&PPC::F8RCRegClass);
1252 if (DstVT == MVT::i32)
1253 if (IsSigned)
1254 Opc = PPC::FCTIWZ;
1255 else
1256 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001257 else
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001258 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1259 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001260
1261 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001263 .addReg(SrcReg);
1264
1265 // Now move the integer value from a float register to an integer register.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001266 unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
1267 PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1268
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001269 if (IntReg == 0)
1270 return false;
1271
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001272 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001273 return true;
1274}
1275
Bill Schmidtccecf262013-08-30 02:29:45 +00001276// Attempt to fast-select a binary integer operation that isn't already
1277// handled automatically.
1278bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001279 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001280
1281 // We can get here in the case when we have a binary operation on a non-legal
1282 // type and the target independent selector doesn't know how to handle it.
1283 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1284 return false;
1285
1286 // Look at the currently assigned register for this instruction
1287 // to determine the required register class. If there is no register,
1288 // make a conservative choice (don't assign R0).
1289 unsigned AssignedReg = FuncInfo.ValueMap[I];
1290 const TargetRegisterClass *RC =
1291 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1292 &PPC::GPRC_and_GPRC_NOR0RegClass);
1293 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1294
1295 unsigned Opc;
1296 switch (ISDOpcode) {
1297 default: return false;
1298 case ISD::ADD:
1299 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1300 break;
1301 case ISD::OR:
1302 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1303 break;
1304 case ISD::SUB:
1305 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1306 break;
1307 }
1308
1309 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1310 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1311 if (SrcReg1 == 0) return false;
1312
1313 // Handle case of small immediate operand.
1314 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1315 const APInt &CIVal = ConstInt->getValue();
1316 int Imm = (int)CIVal.getSExtValue();
1317 bool UseImm = true;
1318 if (isInt<16>(Imm)) {
1319 switch (Opc) {
1320 default:
1321 llvm_unreachable("Missing case!");
1322 case PPC::ADD4:
1323 Opc = PPC::ADDI;
1324 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1325 break;
1326 case PPC::ADD8:
1327 Opc = PPC::ADDI8;
1328 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1329 break;
1330 case PPC::OR:
1331 Opc = PPC::ORI;
1332 break;
1333 case PPC::OR8:
1334 Opc = PPC::ORI8;
1335 break;
1336 case PPC::SUBF:
1337 if (Imm == -32768)
1338 UseImm = false;
1339 else {
1340 Opc = PPC::ADDI;
1341 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1342 Imm = -Imm;
1343 }
1344 break;
1345 case PPC::SUBF8:
1346 if (Imm == -32768)
1347 UseImm = false;
1348 else {
1349 Opc = PPC::ADDI8;
1350 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1351 Imm = -Imm;
1352 }
1353 break;
1354 }
1355
1356 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1358 ResultReg)
1359 .addReg(SrcReg1)
1360 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001361 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001362 return true;
1363 }
1364 }
1365 }
1366
1367 // Reg-reg case.
1368 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1369 if (SrcReg2 == 0) return false;
1370
1371 // Reverse operands for subtract-from.
1372 if (ISDOpcode == ISD::SUB)
1373 std::swap(SrcReg1, SrcReg2);
1374
Rafael Espindolaea09c592014-02-18 22:05:46 +00001375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001376 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001377 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001378 return true;
1379}
1380
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001381// Handle arguments to a call that we're attempting to fast-select.
1382// Return false if the arguments are too complex for us at the moment.
1383bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1384 SmallVectorImpl<unsigned> &ArgRegs,
1385 SmallVectorImpl<MVT> &ArgVTs,
1386 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1387 SmallVectorImpl<unsigned> &RegArgs,
1388 CallingConv::ID CC,
1389 unsigned &NumBytes,
1390 bool IsVarArg) {
1391 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001392 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001393
1394 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001395 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001396 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001397
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001398 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1399
1400 // Bail out if we can't handle any of the arguments.
1401 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1402 CCValAssign &VA = ArgLocs[I];
1403 MVT ArgVT = ArgVTs[VA.getValNo()];
1404
1405 // Skip vector arguments for now, as well as long double and
1406 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001407 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001408 !VA.isRegLoc() || VA.needsCustom())
1409 return false;
1410
1411 // Skip bit-converted arguments for now.
1412 if (VA.getLocInfo() == CCValAssign::BCvt)
1413 return false;
1414 }
1415
1416 // Get a count of how many bytes are to be pushed onto the stack.
1417 NumBytes = CCInfo.getNextStackOffset();
1418
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001419 // The prolog code of the callee may store up to 8 GPR argument registers to
1420 // the stack, allowing va_start to index over them in memory if its varargs.
1421 // Because we cannot tell if this is needed on the caller side, we have to
1422 // conservatively assume that it is needed. As such, make sure we have at
1423 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001424 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001425 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001426
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001427 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001429 TII.get(TII.getCallFrameSetupOpcode()))
Serge Pavlovd526b132017-05-09 13:35:13 +00001430 .addImm(NumBytes).addImm(0);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001431
1432 // Prepare to assign register arguments. Every argument uses up a
1433 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001434 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001435 unsigned NextGPR = PPC::X3;
1436 unsigned NextFPR = PPC::F1;
1437
1438 // Process arguments.
1439 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1440 CCValAssign &VA = ArgLocs[I];
1441 unsigned Arg = ArgRegs[VA.getValNo()];
1442 MVT ArgVT = ArgVTs[VA.getValNo()];
1443
1444 // Handle argument promotion and bitcasts.
1445 switch (VA.getLocInfo()) {
1446 default:
1447 llvm_unreachable("Unknown loc info!");
1448 case CCValAssign::Full:
1449 break;
1450 case CCValAssign::SExt: {
1451 MVT DestVT = VA.getLocVT();
1452 const TargetRegisterClass *RC =
1453 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1454 unsigned TmpReg = createResultReg(RC);
1455 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1456 llvm_unreachable("Failed to emit a sext!");
1457 ArgVT = DestVT;
1458 Arg = TmpReg;
1459 break;
1460 }
1461 case CCValAssign::AExt:
1462 case CCValAssign::ZExt: {
1463 MVT DestVT = VA.getLocVT();
1464 const TargetRegisterClass *RC =
1465 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1466 unsigned TmpReg = createResultReg(RC);
1467 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1468 llvm_unreachable("Failed to emit a zext!");
1469 ArgVT = DestVT;
1470 Arg = TmpReg;
1471 break;
1472 }
1473 case CCValAssign::BCvt: {
1474 // FIXME: Not yet handled.
1475 llvm_unreachable("Should have bailed before getting here!");
1476 break;
1477 }
1478 }
1479
1480 // Copy this argument to the appropriate register.
1481 unsigned ArgReg;
1482 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1483 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001484 if (CC != CallingConv::Fast)
1485 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001486 } else
1487 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001488
1489 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1490 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001491 RegArgs.push_back(ArgReg);
1492 }
1493
1494 return true;
1495}
1496
1497// For a call that we've determined we can fast-select, finish the
1498// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001499bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1500 CallingConv::ID CC = CLI.CallConv;
1501
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001502 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001504 TII.get(TII.getCallFrameDestroyOpcode()))
1505 .addImm(NumBytes).addImm(0);
1506
1507 // Next, generate a copy to obtain the return value.
1508 // FIXME: No multi-register return values yet, though I don't foresee
1509 // any real difficulties there.
1510 if (RetVT != MVT::isVoid) {
1511 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001512 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001513 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1514 CCValAssign &VA = RVLocs[0];
1515 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1516 assert(VA.isRegLoc() && "Can only return in registers!");
1517
1518 MVT DestVT = VA.getValVT();
1519 MVT CopyVT = DestVT;
1520
1521 // Ints smaller than a register still arrive in a full 64-bit
1522 // register, so make sure we recognize this.
1523 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1524 CopyVT = MVT::i64;
1525
1526 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001527 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001528
1529 if (RetVT == CopyVT) {
1530 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1531 ResultReg = createResultReg(CpyRC);
1532
Rafael Espindolaea09c592014-02-18 22:05:46 +00001533 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001534 TII.get(TargetOpcode::COPY), ResultReg)
1535 .addReg(SourcePhysReg);
1536
1537 // If necessary, round the floating result to single precision.
1538 } else if (CopyVT == MVT::f64) {
1539 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001540 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001541 ResultReg).addReg(SourcePhysReg);
1542
1543 // If only the low half of a general register is needed, generate
1544 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1545 // used along the fast-isel path (not lowered), and downstream logic
1546 // also doesn't like a direct subreg copy on a physical reg.)
1547 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1548 ResultReg = createResultReg(&PPC::GPRCRegClass);
1549 // Convert physical register from G8RC to GPRC.
1550 SourcePhysReg -= PPC::X0 - PPC::R0;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001551 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001552 TII.get(TargetOpcode::COPY), ResultReg)
1553 .addReg(SourcePhysReg);
1554 }
1555
Bill Schmidt0954ea12013-08-30 23:25:30 +00001556 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001557 CLI.InRegs.push_back(SourcePhysReg);
1558 CLI.ResultReg = ResultReg;
1559 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001560 }
Hal Finkel934361a2015-01-14 01:07:51 +00001561
1562 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001563}
1564
Hal Finkel934361a2015-01-14 01:07:51 +00001565bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1566 CallingConv::ID CC = CLI.CallConv;
1567 bool IsTailCall = CLI.IsTailCall;
1568 bool IsVarArg = CLI.IsVarArg;
1569 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001570 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001571
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001572 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001573 return false;
1574
1575 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001576 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001577 return false;
1578
Hal Finkel934361a2015-01-14 01:07:51 +00001579 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001580 if (IsVarArg)
1581 return false;
1582
1583 // Handle simple calls for now, with legal return types and
1584 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001585 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001586 MVT RetVT;
1587 if (RetTy->isVoidTy())
1588 RetVT = MVT::isVoid;
1589 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1590 RetVT != MVT::i8)
1591 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001592 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1593 // We can't handle boolean returns when CR bits are in use.
1594 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001595
1596 // FIXME: No multi-register return values yet.
1597 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1598 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1599 RetVT != MVT::f64) {
1600 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001601 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001602 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1603 if (RVLocs.size() > 1)
1604 return false;
1605 }
1606
1607 // Bail early if more than 8 arguments, as we only currently
1608 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001609 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001610 if (NumArgs > 8)
1611 return false;
1612
1613 // Set up the argument vectors.
1614 SmallVector<Value*, 8> Args;
1615 SmallVector<unsigned, 8> ArgRegs;
1616 SmallVector<MVT, 8> ArgVTs;
1617 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1618
1619 Args.reserve(NumArgs);
1620 ArgRegs.reserve(NumArgs);
1621 ArgVTs.reserve(NumArgs);
1622 ArgFlags.reserve(NumArgs);
1623
Hal Finkel934361a2015-01-14 01:07:51 +00001624 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001625 // Only handle easy calls for now. It would be reasonably easy
1626 // to handle <= 8-byte structures passed ByVal in registers, but we
1627 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001628 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1629 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001630 return false;
1631
Hal Finkel934361a2015-01-14 01:07:51 +00001632 Value *ArgValue = CLI.OutVals[i];
1633 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001634 MVT ArgVT;
1635 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1636 return false;
1637
1638 if (ArgVT.isVector())
1639 return false;
1640
Hal Finkel934361a2015-01-14 01:07:51 +00001641 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001642 if (Arg == 0)
1643 return false;
1644
Hal Finkel934361a2015-01-14 01:07:51 +00001645 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001646 ArgRegs.push_back(Arg);
1647 ArgVTs.push_back(ArgVT);
1648 ArgFlags.push_back(Flags);
1649 }
1650
1651 // Process the arguments.
1652 SmallVector<unsigned, 8> RegArgs;
1653 unsigned NumBytes;
1654
1655 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1656 RegArgs, CC, NumBytes, IsVarArg))
1657 return false;
1658
Hal Finkel934361a2015-01-14 01:07:51 +00001659 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001660 // FIXME: No handling for function pointers yet. This requires
1661 // implementing the function descriptor (OPD) setup.
1662 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001663 if (!GV) {
1664 // patchpoints are a special case; they always dispatch to a pointer value.
1665 // However, we don't actually want to generate the indirect call sequence
1666 // here (that will be generated, as necessary, during asm printing), and
1667 // the call we generate here will be erased by FastISel::selectPatchpoint,
1668 // so don't try very hard...
1669 if (CLI.IsPatchPoint)
1670 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1671 else
1672 return false;
1673 } else {
1674 // Build direct call with NOP for TOC restore.
1675 // FIXME: We can and should optimize away the NOP for local calls.
1676 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1677 TII.get(PPC::BL8_NOP));
1678 // Add callee.
1679 MIB.addGlobalAddress(GV);
1680 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001681
1682 // Add implicit physical register uses to the call.
1683 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1684 MIB.addReg(RegArgs[II], RegState::Implicit);
1685
Hal Finkelaf519932015-01-19 07:20:27 +00001686 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1687 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001688 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001689 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001690
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001691 // Add a register mask with the call-preserved registers. Proper
1692 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001693 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001694
Hal Finkel934361a2015-01-14 01:07:51 +00001695 CLI.Call = MIB;
1696
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001697 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001698 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001699}
1700
Bill Schmidtd89f6782013-08-26 19:42:51 +00001701// Attempt to fast-select a return instruction.
1702bool PPCFastISel::SelectRet(const Instruction *I) {
1703
1704 if (!FuncInfo.CanLowerReturn)
1705 return false;
1706
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001707 if (TLI.supportSplitCSR(FuncInfo.MF))
1708 return false;
1709
Bill Schmidtd89f6782013-08-26 19:42:51 +00001710 const ReturnInst *Ret = cast<ReturnInst>(I);
1711 const Function &F = *I->getParent()->getParent();
1712
1713 // Build a list of return value registers.
1714 SmallVector<unsigned, 4> RetRegs;
1715 CallingConv::ID CC = F.getCallingConv();
1716
1717 if (Ret->getNumOperands() > 0) {
1718 SmallVector<ISD::OutputArg, 4> Outs;
Matt Arsenault81920b02018-07-28 13:25:19 +00001719 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001720
1721 // Analyze operands of the call, assigning locations to each operand.
1722 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001723 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001724 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1725 const Value *RV = Ret->getOperand(0);
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001726
Bill Schmidtd89f6782013-08-26 19:42:51 +00001727 // FIXME: Only one output register for now.
1728 if (ValLocs.size() > 1)
1729 return false;
1730
Eric Christopherf0024d12015-07-25 00:48:08 +00001731 // Special case for returning a constant integer of any size - materialize
1732 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001733 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001734 CCValAssign &VA = ValLocs[0];
1735
1736 unsigned RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001737 // We still need to worry about properly extending the sign. For example,
1738 // we could have only a single bit or a constant that needs zero
1739 // extension rather than sign extension. Make sure we pass the return
1740 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001741 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001742 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001743
Rafael Espindolaea09c592014-02-18 22:05:46 +00001744 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001745 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1746
Bill Schmidtd89f6782013-08-26 19:42:51 +00001747 RetRegs.push_back(RetReg);
1748
1749 } else {
1750 unsigned Reg = getRegForValue(RV);
1751
1752 if (Reg == 0)
1753 return false;
1754
1755 // Copy the result values into the output registers.
1756 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1757
1758 CCValAssign &VA = ValLocs[i];
1759 assert(VA.isRegLoc() && "Can only return in registers!");
1760 RetRegs.push_back(VA.getLocReg());
1761 unsigned SrcReg = Reg + VA.getValNo();
1762
Mehdi Amini44ede332015-07-09 02:09:04 +00001763 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001764 if (!RVEVT.isSimple())
1765 return false;
1766 MVT RVVT = RVEVT.getSimpleVT();
1767 MVT DestVT = VA.getLocVT();
1768
1769 if (RVVT != DestVT && RVVT != MVT::i8 &&
1770 RVVT != MVT::i16 && RVVT != MVT::i32)
1771 return false;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001772
Bill Schmidtd89f6782013-08-26 19:42:51 +00001773 if (RVVT != DestVT) {
1774 switch (VA.getLocInfo()) {
1775 default:
1776 llvm_unreachable("Unknown loc info!");
1777 case CCValAssign::Full:
1778 llvm_unreachable("Full value assign but types don't match?");
1779 case CCValAssign::AExt:
1780 case CCValAssign::ZExt: {
1781 const TargetRegisterClass *RC =
1782 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1783 unsigned TmpReg = createResultReg(RC);
1784 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1785 return false;
1786 SrcReg = TmpReg;
1787 break;
1788 }
1789 case CCValAssign::SExt: {
1790 const TargetRegisterClass *RC =
1791 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1792 unsigned TmpReg = createResultReg(RC);
1793 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1794 return false;
1795 SrcReg = TmpReg;
1796 break;
1797 }
1798 }
1799 }
1800
Rafael Espindolaea09c592014-02-18 22:05:46 +00001801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001802 TII.get(TargetOpcode::COPY), RetRegs[i])
1803 .addReg(SrcReg);
1804 }
1805 }
1806 }
1807
Rafael Espindolaea09c592014-02-18 22:05:46 +00001808 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001809 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001810
1811 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1812 MIB.addReg(RetRegs[i], RegState::Implicit);
1813
1814 return true;
1815}
1816
Bill Schmidt03008132013-08-25 22:33:42 +00001817// Attempt to emit an integer extend of SrcReg into DestReg. Both
1818// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001819// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001820bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1821 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001822 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1823 return false;
1824 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1825 return false;
1826
1827 // Signed extensions use EXTSB, EXTSH, EXTSW.
1828 if (!IsZExt) {
1829 unsigned Opc;
1830 if (SrcVT == MVT::i8)
1831 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1832 else if (SrcVT == MVT::i16)
1833 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1834 else {
1835 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1836 Opc = PPC::EXTSW_32_64;
1837 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001839 .addReg(SrcReg);
1840
1841 // Unsigned 32-bit extensions use RLWINM.
1842 } else if (DestVT == MVT::i32) {
1843 unsigned MB;
1844 if (SrcVT == MVT::i8)
1845 MB = 24;
1846 else {
1847 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1848 MB = 16;
1849 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001850 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001851 DestReg)
1852 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1853
1854 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1855 } else {
1856 unsigned MB;
1857 if (SrcVT == MVT::i8)
1858 MB = 56;
1859 else if (SrcVT == MVT::i16)
1860 MB = 48;
1861 else
1862 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001864 TII.get(PPC::RLDICL_32_64), DestReg)
1865 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1866 }
1867
1868 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001869}
1870
1871// Attempt to fast-select an indirect branch instruction.
1872bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1873 unsigned AddrReg = getRegForValue(I->getOperand(0));
1874 if (AddrReg == 0)
1875 return false;
1876
Rafael Espindolaea09c592014-02-18 22:05:46 +00001877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001878 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001879 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001880
1881 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001882 for (const BasicBlock *SuccBB : IB->successors())
1883 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001884
1885 return true;
1886}
1887
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001888// Attempt to fast-select an integer truncate instruction.
1889bool PPCFastISel::SelectTrunc(const Instruction *I) {
1890 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001891 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1892 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001893
1894 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1895 return false;
1896
1897 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1898 return false;
1899
1900 unsigned SrcReg = getRegForValue(Src);
1901 if (!SrcReg)
1902 return false;
1903
1904 // The only interesting case is when we need to switch register classes.
1905 if (SrcVT == MVT::i64) {
1906 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1908 TII.get(TargetOpcode::COPY),
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001909 ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1910 SrcReg = ResultReg;
1911 }
1912
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001913 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001914 return true;
1915}
1916
Bill Schmidtd89f6782013-08-26 19:42:51 +00001917// Attempt to fast-select an integer extend instruction.
1918bool PPCFastISel::SelectIntExt(const Instruction *I) {
1919 Type *DestTy = I->getType();
1920 Value *Src = I->getOperand(0);
1921 Type *SrcTy = Src->getType();
1922
1923 bool IsZExt = isa<ZExtInst>(I);
1924 unsigned SrcReg = getRegForValue(Src);
1925 if (!SrcReg) return false;
1926
1927 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001928 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1929 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001930 if (!SrcEVT.isSimple())
1931 return false;
1932 if (!DestEVT.isSimple())
1933 return false;
1934
1935 MVT SrcVT = SrcEVT.getSimpleVT();
1936 MVT DestVT = DestEVT.getSimpleVT();
1937
1938 // If we know the register class needed for the result of this
1939 // instruction, use it. Otherwise pick the register class of the
1940 // correct size that does not contain X0/R0, since we don't know
1941 // whether downstream uses permit that assignment.
1942 unsigned AssignedReg = FuncInfo.ValueMap[I];
1943 const TargetRegisterClass *RC =
1944 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1945 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1946 &PPC::GPRC_and_GPRC_NOR0RegClass));
1947 unsigned ResultReg = createResultReg(RC);
1948
1949 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1950 return false;
1951
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001952 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001953 return true;
1954}
1955
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001956// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001957// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001958bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001959
1960 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001961 case Instruction::Load:
1962 return SelectLoad(I);
1963 case Instruction::Store:
1964 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001965 case Instruction::Br:
1966 return SelectBranch(I);
1967 case Instruction::IndirectBr:
1968 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001969 case Instruction::FPExt:
1970 return SelectFPExt(I);
1971 case Instruction::FPTrunc:
1972 return SelectFPTrunc(I);
1973 case Instruction::SIToFP:
1974 return SelectIToFP(I, /*IsSigned*/ true);
1975 case Instruction::UIToFP:
1976 return SelectIToFP(I, /*IsSigned*/ false);
1977 case Instruction::FPToSI:
1978 return SelectFPToI(I, /*IsSigned*/ true);
1979 case Instruction::FPToUI:
1980 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001981 case Instruction::Add:
1982 return SelectBinaryIntOp(I, ISD::ADD);
1983 case Instruction::Or:
1984 return SelectBinaryIntOp(I, ISD::OR);
1985 case Instruction::Sub:
1986 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001987 case Instruction::Call:
Hal Finkel934361a2015-01-14 01:07:51 +00001988 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001989 case Instruction::Ret:
1990 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001991 case Instruction::Trunc:
1992 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001993 case Instruction::ZExt:
1994 case Instruction::SExt:
1995 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001996 // Here add other flavors of Instruction::XXX that automated
1997 // cases don't catch. For example, switches are terminators
1998 // that aren't yet handled.
1999 default:
2000 break;
2001 }
2002 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002003}
2004
2005// Materialize a floating-point constant into a register, and return
2006// the register number (or zero if we failed to handle it).
2007unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
2008 // No plans to handle long double here.
2009 if (VT != MVT::f32 && VT != MVT::f64)
2010 return 0;
2011
2012 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002013 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002014 assert(Align > 0 && "Unexpectedly missing alignment information!");
2015 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002016 const bool HasSPE = PPCSubTarget->hasSPE();
2017 const TargetRegisterClass *RC;
2018 if (HasSPE)
2019 RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
2020 else
2021 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2022
Ulrich Weigandc3b495a2016-08-05 15:22:05 +00002023 unsigned DestReg = createResultReg(RC);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002024 CodeModel::Model CModel = TM.getCodeModel();
2025
Alex Lorenze40c8a22015-08-11 23:09:45 +00002026 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2027 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2028 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002029
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002030 unsigned Opc;
2031
2032 if (HasSPE)
2033 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2034 else
2035 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2036
Bill Schmidt03008132013-08-25 22:33:42 +00002037 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2038
Hal Finkele6698d52015-02-01 15:03:28 +00002039 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00002040 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
Rafael Espindola79e238a2017-08-03 02:16:21 +00002041 if (CModel == CodeModel::Small) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00002043 TmpReg)
2044 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002045 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00002046 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2047 } else {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002048 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
Rafael Espindolaea09c592014-02-18 22:05:46 +00002049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002050 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00002051 // But for large code model, we must generate a LDtocL followed
2052 // by the LF[SD].
2053 if (CModel == CodeModel::Large) {
2054 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00002056 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002057 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002058 .addImm(0)
2059 .addReg(TmpReg2);
2060 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00002061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00002062 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2063 .addReg(TmpReg)
2064 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002065 }
2066
2067 return DestReg;
2068}
2069
Bill Schmidtccecf262013-08-30 02:29:45 +00002070// Materialize the address of a global value into a register, and return
2071// the register number (or zero if we failed to handle it).
2072unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2073 assert(VT == MVT::i64 && "Non-address!");
2074 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2075 unsigned DestReg = createResultReg(RC);
2076
2077 // Global values may be plain old object addresses, TLS object
2078 // addresses, constant pool entries, or jump tables. How we generate
2079 // code for these may depend on small, medium, or large code model.
2080 CodeModel::Model CModel = TM.getCodeModel();
2081
2082 // FIXME: Jump tables are not yet required because fast-isel doesn't
2083 // handle switches; if that changes, we need them as well. For now,
2084 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00002085
2086 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002087 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00002088 return 0;
2089
Hal Finkele6698d52015-02-01 15:03:28 +00002090 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00002091 // For small code model, generate a simple TOC load.
Rafael Espindola79e238a2017-08-03 02:16:21 +00002092 if (CModel == CodeModel::Small)
Rafael Espindolaea09c592014-02-18 22:05:46 +00002093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2094 DestReg)
2095 .addGlobalAddress(GV)
2096 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00002097 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00002098 // If the address is an externally defined symbol, a symbol with common
2099 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00002100 // jump table address (not yet needed), or if we are generating code
2101 // for large code model, we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002102 // LDtocL(GV, ADDIStocHA(%x2, GV))
Bill Schmidtccecf262013-08-30 02:29:45 +00002103 // Otherwise we generate:
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +00002104 // ADDItocL(ADDIStocHA(%x2, GV), GV)
Bill Schmidtccecf262013-08-30 02:29:45 +00002105 // Either way, start with the ADDIStocHA:
2106 unsigned HighPartReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
Bill Schmidtccecf262013-08-30 02:29:45 +00002108 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2109
Eric Christopherc1808362015-11-20 20:51:31 +00002110 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2111 if (GVFlags & PPCII::MO_NLP_FLAG) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002113 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00002114 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00002115 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002117 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00002118 }
Bill Schmidtccecf262013-08-30 02:29:45 +00002119 }
2120
2121 return DestReg;
2122}
2123
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002124// Materialize a 32-bit integer constant into a register, and return
2125// the register number (or zero if we failed to handle it).
2126unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2127 const TargetRegisterClass *RC) {
2128 unsigned Lo = Imm & 0xFFFF;
2129 unsigned Hi = (Imm >> 16) & 0xFFFF;
2130
2131 unsigned ResultReg = createResultReg(RC);
2132 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2133
2134 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002135 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002136 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2137 .addImm(Imm);
2138 else if (Lo) {
2139 // Both Lo and Hi have nonzero bits.
2140 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002142 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2143 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002145 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2146 .addReg(TmpReg).addImm(Lo);
2147 } else
2148 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002150 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002151 .addImm(Hi);
2152
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002153 return ResultReg;
2154}
2155
2156// Materialize a 64-bit integer constant into a register, and return
2157// the register number (or zero if we failed to handle it).
2158unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2159 const TargetRegisterClass *RC) {
2160 unsigned Remainder = 0;
2161 unsigned Shift = 0;
2162
2163 // If the value doesn't fit in 32 bits, see if we can shift it
2164 // so that it fits in 32 bits.
2165 if (!isInt<32>(Imm)) {
2166 Shift = countTrailingZeros<uint64_t>(Imm);
2167 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2168
2169 if (isInt<32>(ImmSh))
2170 Imm = ImmSh;
2171 else {
2172 Remainder = Imm;
2173 Shift = 32;
2174 Imm >>= 32;
2175 }
2176 }
2177
2178 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2179 // (if not shifted).
2180 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2181 if (!Shift)
2182 return TmpReg1;
2183
2184 // If upper 32 bits were not zero, we've built them and need to shift
2185 // them into place.
2186 unsigned TmpReg2;
2187 if (Imm) {
2188 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002190 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2191 } else
2192 TmpReg2 = TmpReg1;
2193
2194 unsigned TmpReg3, Hi, Lo;
2195 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2196 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002197 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002198 TmpReg3).addReg(TmpReg2).addImm(Hi);
2199 } else
2200 TmpReg3 = TmpReg2;
2201
2202 if ((Lo = Remainder & 0xFFFF)) {
2203 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002204 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002205 ResultReg).addReg(TmpReg3).addImm(Lo);
2206 return ResultReg;
2207 }
2208
2209 return TmpReg3;
2210}
2211
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002212// Materialize an integer constant into a register, and return
2213// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002214unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2215 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002216 // If we're using CR bit registers for i1 values, handle that as a special
2217 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002218 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002219 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2220 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2221 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2222 return ImmReg;
2223 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002224
Eric Christopher80ba58a2016-01-29 07:19:49 +00002225 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2226 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002227 return 0;
2228
Eric Christopher80ba58a2016-01-29 07:19:49 +00002229 const TargetRegisterClass *RC =
2230 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002231 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002232
2233 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002234 // Since LI will sign extend the constant we need to make sure that for
2235 // our zeroext constants that the sign extended constant fits into 16-bits -
2236 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002237 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002238 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2239 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002241 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002242 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002243 }
2244
2245 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002246 if (VT == MVT::i64)
2247 return PPCMaterialize64BitInt(Imm, RC);
2248 else if (VT == MVT::i32)
2249 return PPCMaterialize32BitInt(Imm, RC);
2250
2251 return 0;
2252}
2253
2254// Materialize a constant into a register, and return the register
2255// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002256unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002257 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002258
2259 // Only handle simple types.
2260 if (!CEVT.isSimple()) return 0;
2261 MVT VT = CEVT.getSimpleVT();
2262
2263 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2264 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002265 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2266 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002267 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Hal Finkel73390c72016-09-04 06:07:19 +00002268 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2269 // assumes that constant PHI operands will be zero extended, and failure to
2270 // match that assumption will cause problems if we sign extend here but
2271 // some user of a PHI is in a block for which we fall back to full SDAG
2272 // instruction selection.
2273 return PPCMaterializeInt(CI, VT, false);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002274
2275 return 0;
2276}
2277
2278// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002279// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002280unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002281 // Don't handle dynamic allocas.
2282 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2283
2284 MVT VT;
2285 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2286
2287 DenseMap<const AllocaInst*, int>::iterator SI =
2288 FuncInfo.StaticAllocaMap.find(AI);
2289
2290 if (SI != FuncInfo.StaticAllocaMap.end()) {
2291 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002293 ResultReg).addFrameIndex(SI->second).addImm(0);
2294 return ResultReg;
2295 }
2296
2297 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002298}
2299
Bill Schmidtccecf262013-08-30 02:29:45 +00002300// Fold loads into extends when possible.
2301// FIXME: We can have multiple redundant extend/trunc instructions
2302// following a load. The folding only picks up one. Extend this
2303// to check subsequent instructions for the same pattern and remove
2304// them. Thus ResultReg should be the def reg for the last redundant
2305// instruction in a chain, and all intervening instructions can be
2306// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2307// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002308bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2309 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002310 // Verify we have a legal type before going any further.
2311 MVT VT;
2312 if (!isLoadTypeLegal(LI->getType(), VT))
2313 return false;
2314
2315 // Combine load followed by zero- or sign-extend.
2316 bool IsZExt = false;
2317 switch(MI->getOpcode()) {
2318 default:
2319 return false;
2320
2321 case PPC::RLDICL:
2322 case PPC::RLDICL_32_64: {
2323 IsZExt = true;
2324 unsigned MB = MI->getOperand(3).getImm();
2325 if ((VT == MVT::i8 && MB <= 56) ||
2326 (VT == MVT::i16 && MB <= 48) ||
2327 (VT == MVT::i32 && MB <= 32))
2328 break;
2329 return false;
2330 }
2331
2332 case PPC::RLWINM:
2333 case PPC::RLWINM8: {
2334 IsZExt = true;
2335 unsigned MB = MI->getOperand(3).getImm();
2336 if ((VT == MVT::i8 && MB <= 24) ||
2337 (VT == MVT::i16 && MB <= 16))
2338 break;
2339 return false;
2340 }
2341
2342 case PPC::EXTSB:
2343 case PPC::EXTSB8:
2344 case PPC::EXTSB8_32_64:
2345 /* There is no sign-extending load-byte instruction. */
2346 return false;
2347
2348 case PPC::EXTSH:
2349 case PPC::EXTSH8:
2350 case PPC::EXTSH8_32_64: {
2351 if (VT != MVT::i16 && VT != MVT::i8)
2352 return false;
2353 break;
2354 }
2355
2356 case PPC::EXTSW:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002357 case PPC::EXTSW_32:
Bill Schmidtccecf262013-08-30 02:29:45 +00002358 case PPC::EXTSW_32_64: {
2359 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2360 return false;
2361 break;
2362 }
2363 }
2364
2365 // See if we can handle this address.
2366 Address Addr;
2367 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2368 return false;
2369
2370 unsigned ResultReg = MI->getOperand(0).getReg();
2371
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002372 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2373 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +00002374 return false;
2375
Tim Northover256a16d2018-12-17 17:25:53 +00002376 MachineBasicBlock::iterator I(MI);
2377 removeDeadCode(I, std::next(I));
Bill Schmidtccecf262013-08-30 02:29:45 +00002378 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002379}
2380
2381// Attempt to lower call arguments in a faster way than done by
2382// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002383bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002384 // Defer to normal argument lowering for now. It's reasonably
2385 // efficient. Consider doing something like ARM to handle the
2386 // case where all args fit in registers, no varargs, no float
2387 // or vector args.
2388 return false;
2389}
2390
Bill Schmidt03008132013-08-25 22:33:42 +00002391// Handle materializing integer constants into a register. This is not
2392// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002393unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002394
Bill Schmidt03008132013-08-25 22:33:42 +00002395 if (Opc != ISD::Constant)
2396 return 0;
2397
Hal Finkel940ab932014-02-28 00:27:01 +00002398 // If we're using CR bit registers for i1 values, handle that as a special
2399 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002400 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002401 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2402 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2403 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2404 return ImmReg;
2405 }
2406
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002407 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2408 VT != MVT::i1)
Bill Schmidt03008132013-08-25 22:33:42 +00002409 return 0;
2410
2411 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2412 &PPC::GPRCRegClass);
2413 if (VT == MVT::i64)
2414 return PPCMaterialize64BitInt(Imm, RC);
2415 else
2416 return PPCMaterialize32BitInt(Imm, RC);
2417}
2418
Bill Schmidtccecf262013-08-30 02:29:45 +00002419// Override for ADDI and ADDI8 to set the correct register class
2420// on RHS operand 0. The automatic infrastructure naively assumes
2421// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2422// for these cases. At the moment, none of the other automatically
2423// generated RI instructions require special treatment. However, once
2424// SelectSelect is implemented, "isel" requires similar handling.
2425//
2426// Also be conservative about the output register class. Avoid
2427// assigning R0 or X0 to the output register for GPRC and G8RC
2428// register classes, as any such result could be used in ADDI, etc.,
2429// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002430unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002431 const TargetRegisterClass *RC,
2432 unsigned Op0, bool Op0IsKill,
2433 uint64_t Imm) {
2434 if (MachineInstOpcode == PPC::ADDI)
2435 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2436 else if (MachineInstOpcode == PPC::ADDI8)
2437 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2438
2439 const TargetRegisterClass *UseRC =
2440 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2441 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2442
Juergen Ributzka88e32512014-09-03 20:56:59 +00002443 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002444 Op0, Op0IsKill, Imm);
2445}
2446
2447// Override for instructions with one register operand to avoid use of
2448// R0/X0. The automatic infrastructure isn't aware of the context so
2449// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002450unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002451 const TargetRegisterClass* RC,
2452 unsigned Op0, bool Op0IsKill) {
2453 const TargetRegisterClass *UseRC =
2454 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2455 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2456
Juergen Ributzka88e32512014-09-03 20:56:59 +00002457 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002458}
2459
2460// Override for instructions with two register operands to avoid use
2461// of R0/X0. The automatic infrastructure isn't aware of the context
2462// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002463unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002464 const TargetRegisterClass* RC,
2465 unsigned Op0, bool Op0IsKill,
2466 unsigned Op1, bool Op1IsKill) {
2467 const TargetRegisterClass *UseRC =
2468 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2469 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2470
Juergen Ributzka88e32512014-09-03 20:56:59 +00002471 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002472 Op1, Op1IsKill);
2473}
2474
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002475namespace llvm {
2476 // Create the fast instruction selector for PowerPC64 ELF.
2477 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2478 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002479 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002480 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Eric Christopher85806142015-01-30 02:11:24 +00002481 if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002482 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002483 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002484 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002485}