blob: d8425d89da92e61730152689da0499140194d744 [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 }
Zi Xuan Wufec749f2019-01-30 02:56:22 +0000154 unsigned copyRegToRegClass(const TargetRegisterClass *ToRC,
155 unsigned SrcReg, unsigned Flag = 0,
156 unsigned SubReg = 0) {
157 unsigned TmpReg = createResultReg(ToRC);
158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
159 TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg, Flag, SubReg);
160 return TmpReg;
161 }
Bill Schmidt03008132013-08-25 22:33:42 +0000162 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000163 bool isZExt, unsigned DestReg,
164 const PPC::Predicate Pred);
Daniel Sanders0c476112019-08-15 19:22:08 +0000165 bool PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
Bill Schmidtccecf262013-08-30 02:29:45 +0000166 const TargetRegisterClass *RC, bool IsZExt = true,
167 unsigned FP64LoadOpc = PPC::LFD);
168 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
169 bool PPCComputeAddress(const Value *Obj, Address &Addr);
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000170 void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000171 unsigned &IndexReg);
Bill Schmidt03008132013-08-25 22:33:42 +0000172 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
173 unsigned DestReg, bool IsZExt);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000174 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
Bill Schmidtccecf262013-08-30 02:29:45 +0000175 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +0000176 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
177 bool UseSExt = true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000178 unsigned PPCMaterialize32BitInt(int64_t Imm,
179 const TargetRegisterClass *RC);
180 unsigned PPCMaterialize64BitInt(int64_t Imm,
181 const TargetRegisterClass *RC);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000182 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
183 unsigned SrcReg, bool IsSigned);
184 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000185
Bill Schmidtd89f6782013-08-26 19:42:51 +0000186 // Call handling routines.
187 private:
Bill Schmidt8470b0f2013-08-30 22:18:55 +0000188 bool processCallArgs(SmallVectorImpl<Value*> &Args,
189 SmallVectorImpl<unsigned> &ArgRegs,
190 SmallVectorImpl<MVT> &ArgVTs,
191 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
192 SmallVectorImpl<unsigned> &RegArgs,
193 CallingConv::ID CC,
194 unsigned &NumBytes,
195 bool IsVarArg);
Hal Finkel934361a2015-01-14 01:07:51 +0000196 bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
Bill Schmidtd89f6782013-08-26 19:42:51 +0000197
Bill Schmidt0cf702f2013-07-30 00:50:39 +0000198 private:
199 #include "PPCGenFastISel.inc"
200
201};
202
203} // end anonymous namespace
204
Bill Schmidt03008132013-08-25 22:33:42 +0000205static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
206 switch (Pred) {
207 // These are not representable with any single compare.
208 case CmpInst::FCMP_FALSE:
Tim Shen5cdf7502016-03-17 22:27:58 +0000209 case CmpInst::FCMP_TRUE:
210 // Major concern about the following 6 cases is NaN result. The comparison
211 // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
212 // only one of which will be set. The result is generated by fcmpu
213 // instruction. However, bc instruction only inspects one of the first 3
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000214 // bits, so when un is set, bc instruction may jump to an undesired
Tim Shen5cdf7502016-03-17 22:27:58 +0000215 // place.
216 //
217 // More specifically, if we expect an unordered comparison and un is set, we
218 // expect to always go to true branch; in such case UEQ, UGT and ULT still
219 // give false, which are undesired; but UNE, UGE, ULE happen to give true,
220 // since they are tested by inspecting !eq, !lt, !gt, respectively.
221 //
222 // Similarly, for ordered comparison, when un is set, we always expect the
223 // result to be false. In such case OGT, OLT and OEQ is good, since they are
224 // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
225 // and ONE are tested through !lt, !gt and !eq, and these are true.
Bill Schmidt03008132013-08-25 22:33:42 +0000226 case CmpInst::FCMP_UEQ:
227 case CmpInst::FCMP_UGT:
Bill Schmidt03008132013-08-25 22:33:42 +0000228 case CmpInst::FCMP_ULT:
Tim Shen5cdf7502016-03-17 22:27:58 +0000229 case CmpInst::FCMP_OGE:
230 case CmpInst::FCMP_OLE:
231 case CmpInst::FCMP_ONE:
Bill Schmidt03008132013-08-25 22:33:42 +0000232 default:
233 return Optional<PPC::Predicate>();
234
235 case CmpInst::FCMP_OEQ:
236 case CmpInst::ICMP_EQ:
237 return PPC::PRED_EQ;
238
239 case CmpInst::FCMP_OGT:
240 case CmpInst::ICMP_UGT:
241 case CmpInst::ICMP_SGT:
242 return PPC::PRED_GT;
243
Tim Shen5cdf7502016-03-17 22:27:58 +0000244 case CmpInst::FCMP_UGE:
Bill Schmidt03008132013-08-25 22:33:42 +0000245 case CmpInst::ICMP_UGE:
246 case CmpInst::ICMP_SGE:
247 return PPC::PRED_GE;
248
249 case CmpInst::FCMP_OLT:
250 case CmpInst::ICMP_ULT:
251 case CmpInst::ICMP_SLT:
252 return PPC::PRED_LT;
253
Tim Shen5cdf7502016-03-17 22:27:58 +0000254 case CmpInst::FCMP_ULE:
Bill Schmidt03008132013-08-25 22:33:42 +0000255 case CmpInst::ICMP_ULE:
256 case CmpInst::ICMP_SLE:
257 return PPC::PRED_LE;
258
Tim Shen5cdf7502016-03-17 22:27:58 +0000259 case CmpInst::FCMP_UNE:
Bill Schmidt03008132013-08-25 22:33:42 +0000260 case CmpInst::ICMP_NE:
261 return PPC::PRED_NE;
262
263 case CmpInst::FCMP_ORD:
264 return PPC::PRED_NU;
265
266 case CmpInst::FCMP_UNO:
267 return PPC::PRED_UN;
268 }
269}
270
Bill Schmidtccecf262013-08-30 02:29:45 +0000271// Determine whether the type Ty is simple enough to be handled by
272// fast-isel, and return its equivalent machine type in VT.
273// FIXME: Copied directly from ARM -- factor into base class?
274bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000275 EVT Evt = TLI.getValueType(DL, Ty, true);
Bill Schmidtccecf262013-08-30 02:29:45 +0000276
277 // Only handle simple types.
278 if (Evt == MVT::Other || !Evt.isSimple()) return false;
279 VT = Evt.getSimpleVT();
280
281 // Handle all legal types, i.e. a register that will directly hold this
282 // value.
283 return TLI.isTypeLegal(VT);
284}
285
286// Determine whether the type Ty is simple enough to be handled by
287// fast-isel as a load target, and return its equivalent machine type in VT.
288bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
289 if (isTypeLegal(Ty, VT)) return true;
290
291 // If this is a type than can be sign or zero-extended to a basic operation
292 // go ahead and accept it now.
293 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
294 return true;
295 }
296
297 return false;
298}
299
Hal Finkel5f2a1372015-05-23 12:18:10 +0000300bool PPCFastISel::isValueAvailable(const Value *V) const {
301 if (!isa<Instruction>(V))
302 return true;
303
304 const auto *I = cast<Instruction>(V);
Alexander Kornienko175a7cb2015-12-28 13:38:42 +0000305 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
Hal Finkel5f2a1372015-05-23 12:18:10 +0000306}
307
Bill Schmidtccecf262013-08-30 02:29:45 +0000308// Given a value Obj, create an Address object Addr that represents its
309// address. Return false if we can't handle it.
310bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000311 const User *U = nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000312 unsigned Opcode = Instruction::UserOp1;
313 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
314 // Don't walk into other basic blocks unless the object is an alloca from
315 // another block, otherwise it may not have a virtual register assigned.
316 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
317 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
318 Opcode = I->getOpcode();
319 U = I;
320 }
321 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
322 Opcode = C->getOpcode();
323 U = C;
324 }
325
326 switch (Opcode) {
327 default:
328 break;
329 case Instruction::BitCast:
330 // Look through bitcasts.
331 return PPCComputeAddress(U->getOperand(0), Addr);
332 case Instruction::IntToPtr:
333 // Look past no-op inttoptrs.
Mehdi Amini44ede332015-07-09 02:09:04 +0000334 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
335 TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000336 return PPCComputeAddress(U->getOperand(0), Addr);
337 break;
338 case Instruction::PtrToInt:
339 // Look past no-op ptrtoints.
Mehdi Amini44ede332015-07-09 02:09:04 +0000340 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
Bill Schmidtccecf262013-08-30 02:29:45 +0000341 return PPCComputeAddress(U->getOperand(0), Addr);
342 break;
343 case Instruction::GetElementPtr: {
344 Address SavedAddr = Addr;
345 long TmpOffset = Addr.Offset;
346
347 // Iterate through the GEP folding the constants into offsets where
348 // we can.
349 gep_type_iterator GTI = gep_type_begin(U);
350 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
351 II != IE; ++II, ++GTI) {
352 const Value *Op = *II;
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000353 if (StructType *STy = GTI.getStructTypeOrNull()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000354 const StructLayout *SL = DL.getStructLayout(STy);
Bill Schmidtccecf262013-08-30 02:29:45 +0000355 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
356 TmpOffset += SL->getElementOffset(Idx);
357 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000358 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Bill Schmidtccecf262013-08-30 02:29:45 +0000359 for (;;) {
360 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
361 // Constant-offset addressing.
362 TmpOffset += CI->getSExtValue() * S;
363 break;
364 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000365 if (canFoldAddIntoGEP(U, Op)) {
366 // A compatible add with a constant operand. Fold the constant.
Bill Schmidtccecf262013-08-30 02:29:45 +0000367 ConstantInt *CI =
368 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
369 TmpOffset += CI->getSExtValue() * S;
370 // Iterate on the other operand.
371 Op = cast<AddOperator>(Op)->getOperand(0);
372 continue;
373 }
374 // Unsupported
375 goto unsupported_gep;
376 }
377 }
378 }
379
380 // Try to grab the base operand now.
381 Addr.Offset = TmpOffset;
382 if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
383
384 // We failed, restore everything and try the other options.
385 Addr = SavedAddr;
386
387 unsupported_gep:
388 break;
389 }
390 case Instruction::Alloca: {
391 const AllocaInst *AI = cast<AllocaInst>(Obj);
392 DenseMap<const AllocaInst*, int>::iterator SI =
393 FuncInfo.StaticAllocaMap.find(AI);
394 if (SI != FuncInfo.StaticAllocaMap.end()) {
395 Addr.BaseType = Address::FrameIndexBase;
396 Addr.Base.FI = SI->second;
397 return true;
398 }
399 break;
400 }
401 }
402
403 // FIXME: References to parameters fall through to the behavior
404 // below. They should be able to reference a frame index since
405 // they are stored to the stack, so we can get "ld rx, offset(r1)"
406 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
407 // just contain the parameter. Try to handle this with a FI.
408
409 // Try to get this in a register if nothing else has worked.
410 if (Addr.Base.Reg == 0)
411 Addr.Base.Reg = getRegForValue(Obj);
412
413 // Prevent assignment of base register to X0, which is inappropriate
414 // for loads and stores alike.
415 if (Addr.Base.Reg != 0)
416 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
417
418 return Addr.Base.Reg != 0;
419}
420
421// Fix up some addresses that can't be used directly. For example, if
422// an offset won't fit in an instruction field, we may need to move it
423// into an index register.
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000424void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
Bill Schmidtccecf262013-08-30 02:29:45 +0000425 unsigned &IndexReg) {
426
427 // Check whether the offset fits in the instruction field.
428 if (!isInt<16>(Addr.Offset))
429 UseOffset = false;
430
431 // If this is a stack pointer and the offset needs to be simplified then
432 // put the alloca address into a register, set the base type back to
433 // register and continue. This should almost never happen.
434 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
435 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidtccecf262013-08-30 02:29:45 +0000437 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
438 Addr.Base.Reg = ResultReg;
439 Addr.BaseType = Address::RegBase;
440 }
441
442 if (!UseOffset) {
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000443 IntegerType *OffsetTy = Type::getInt64Ty(*Context);
Bill Schmidtccecf262013-08-30 02:29:45 +0000444 const ConstantInt *Offset =
445 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
446 IndexReg = PPCMaterializeInt(Offset, MVT::i64);
447 assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
448 }
449}
450
451// Emit a load instruction if possible, returning true if we succeeded,
452// otherwise false. See commentary below for how the register class of
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000453// the load is determined.
Daniel Sanders0c476112019-08-15 19:22:08 +0000454bool PPCFastISel::PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
Bill Schmidtccecf262013-08-30 02:29:45 +0000455 const TargetRegisterClass *RC,
456 bool IsZExt, unsigned FP64LoadOpc) {
457 unsigned Opc;
458 bool UseOffset = true;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000459 bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidtccecf262013-08-30 02:29:45 +0000460
461 // If ResultReg is given, it determines the register class of the load.
462 // Otherwise, RC is the register class to use. If the result of the
463 // load isn't anticipated in this block, both may be zero, in which
464 // case we must make a conservative guess. In particular, don't assign
465 // R0 or X0 to the result register, as the result may be used in a load,
466 // store, add-immediate, or isel that won't permit this. (Though
467 // perhaps the spill and reload of live-exit values would handle this?)
468 const TargetRegisterClass *UseRC =
469 (ResultReg ? MRI.getRegClass(ResultReg) :
470 (RC ? RC :
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000471 (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
Craig Topper36e04d142019-09-12 22:07:35 +0000472 (VT == MVT::f32 ? (HasSPE ? &PPC::GPRCRegClass : &PPC::F4RCRegClass) :
Bill Schmidtccecf262013-08-30 02:29:45 +0000473 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
474 &PPC::GPRC_and_GPRC_NOR0RegClass)))));
475
476 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
477
478 switch (VT.SimpleTy) {
479 default: // e.g., vector types not handled
480 return false;
481 case MVT::i8:
482 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
483 break;
484 case MVT::i16:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000485 Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
486 : (Is32BitInt ? PPC::LHA : PPC::LHA8));
Bill Schmidtccecf262013-08-30 02:29:45 +0000487 break;
488 case MVT::i32:
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000489 Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
490 : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
Bill Schmidtccecf262013-08-30 02:29:45 +0000491 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
492 UseOffset = false;
493 break;
494 case MVT::i64:
495 Opc = PPC::LD;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000496 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
Bill Schmidtccecf262013-08-30 02:29:45 +0000497 "64-bit load with 32-bit target??");
498 UseOffset = ((Addr.Offset & 3) == 0);
499 break;
500 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000501 Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000502 break;
503 case MVT::f64:
504 Opc = FP64LoadOpc;
505 break;
506 }
507
508 // If necessary, materialize the offset into a register and use
509 // the indexed form. Also handle stack pointers with special needs.
510 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000511 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Seurer8c728ae2014-12-05 20:15:56 +0000512
513 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
514 // be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000515 bool IsVSSRC = isVSSRCRegClass(UseRC);
516 bool IsVSFRC = isVSFRCRegClass(UseRC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000517 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000518 bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000519 if ((Is32VSXLoad || Is64VSXLoad) &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000520 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
521 (Addr.Offset == 0)) {
522 UseOffset = false;
523 }
524
Bill Schmidtccecf262013-08-30 02:29:45 +0000525 if (ResultReg == 0)
526 ResultReg = createResultReg(UseRC);
527
528 // Note: If we still have a frame index here, we know the offset is
529 // in range, as otherwise PPCSimplifyAddress would have converted it
530 // into a RegBase.
531 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000532 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000533 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000534
Alex Lorenze40c8a22015-08-11 23:09:45 +0000535 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
536 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
537 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000538 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
539 MFI.getObjectAlignment(Addr.Base.FI));
540
Rafael Espindolaea09c592014-02-18 22:05:46 +0000541 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000542 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
543
544 // Base reg with offset in range.
545 } else if (UseOffset) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000546 // VSX only provides an indexed load.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000547 if (Is32VSXLoad || Is64VSXLoad) return false;
Bill Schmidtccecf262013-08-30 02:29:45 +0000548
Rafael Espindolaea09c592014-02-18 22:05:46 +0000549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +0000550 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
551
552 // Indexed form.
553 } else {
554 // Get the RR opcode corresponding to the RI one. FIXME: It would be
555 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
556 // is hard to get at.
557 switch (Opc) {
558 default: llvm_unreachable("Unexpected opcode!");
559 case PPC::LBZ: Opc = PPC::LBZX; break;
560 case PPC::LBZ8: Opc = PPC::LBZX8; break;
561 case PPC::LHZ: Opc = PPC::LHZX; break;
562 case PPC::LHZ8: Opc = PPC::LHZX8; break;
563 case PPC::LHA: Opc = PPC::LHAX; break;
564 case PPC::LHA8: Opc = PPC::LHAX8; break;
565 case PPC::LWZ: Opc = PPC::LWZX; break;
566 case PPC::LWZ8: Opc = PPC::LWZX8; break;
567 case PPC::LWA: Opc = PPC::LWAX; break;
568 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
569 case PPC::LD: Opc = PPC::LDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000570 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000571 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000572 case PPC::EVLDD: Opc = PPC::EVLDDX; break;
573 case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000574 }
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000575
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000576 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
577 ResultReg);
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000578
579 // If we have an index register defined we use it in the store inst,
580 // otherwise we use X0 as base as it makes the vector instructions to
581 // use zero in the computation of the effective address regardless the
582 // content of the register.
583 if (IndexReg)
584 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
585 else
586 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000587 }
588
589 return true;
590}
591
592// Attempt to fast-select a load instruction.
593bool PPCFastISel::SelectLoad(const Instruction *I) {
594 // FIXME: No atomic loads are supported.
595 if (cast<LoadInst>(I)->isAtomic())
596 return false;
597
598 // Verify we have a legal type before going any further.
599 MVT VT;
600 if (!isLoadTypeLegal(I->getType(), VT))
601 return false;
602
603 // See if we can handle this address.
604 Address Addr;
605 if (!PPCComputeAddress(I->getOperand(0), Addr))
606 return false;
607
608 // Look at the currently assigned register for this instruction
609 // to determine the required register class. This is necessary
610 // to constrain RA from using R0/X0 when this is not legal.
611 unsigned AssignedReg = FuncInfo.ValueMap[I];
612 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +0000613 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidtccecf262013-08-30 02:29:45 +0000614
Daniel Sanders0c476112019-08-15 19:22:08 +0000615 Register ResultReg = 0;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000616 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
617 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +0000618 return false;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000619 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000620 return true;
621}
622
623// Emit a store instruction to store SrcReg at Addr.
624bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
625 assert(SrcReg && "Nothing to store!");
626 unsigned Opc;
627 bool UseOffset = true;
628
629 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
630 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
631
632 switch (VT.SimpleTy) {
633 default: // e.g., vector types not handled
634 return false;
635 case MVT::i8:
636 Opc = Is32BitInt ? PPC::STB : PPC::STB8;
637 break;
638 case MVT::i16:
639 Opc = Is32BitInt ? PPC::STH : PPC::STH8;
640 break;
641 case MVT::i32:
642 assert(Is32BitInt && "Not GPRC for i32??");
643 Opc = PPC::STW;
644 break;
645 case MVT::i64:
646 Opc = PPC::STD;
647 UseOffset = ((Addr.Offset & 3) == 0);
648 break;
649 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000650 Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
Bill Schmidtccecf262013-08-30 02:29:45 +0000651 break;
652 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000653 Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
Bill Schmidtccecf262013-08-30 02:29:45 +0000654 break;
655 }
656
657 // If necessary, materialize the offset into a register and use
658 // the indexed form. Also handle stack pointers with special needs.
659 unsigned IndexReg = 0;
Ulrich Weigand3707ba82016-03-31 15:37:06 +0000660 PPCSimplifyAddress(Addr, UseOffset, IndexReg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000661
Bill Seurer8c728ae2014-12-05 20:15:56 +0000662 // If this is a potential VSX store with an offset of 0, a VSX indexed store
663 // can be used.
Ulrich Weigandc3b495a2016-08-05 15:22:05 +0000664 bool IsVSSRC = isVSSRCRegClass(RC);
665 bool IsVSFRC = isVSFRCRegClass(RC);
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000666 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
667 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
668 if ((Is32VSXStore || Is64VSXStore) &&
669 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
Bill Seurer8c728ae2014-12-05 20:15:56 +0000670 (Addr.Offset == 0)) {
671 UseOffset = false;
672 }
673
Bill Schmidtccecf262013-08-30 02:29:45 +0000674 // Note: If we still have a frame index here, we know the offset is
675 // in range, as otherwise PPCSimplifyAddress would have converted it
676 // into a RegBase.
677 if (Addr.BaseType == Address::FrameIndexBase) {
Bill Seurer8c728ae2014-12-05 20:15:56 +0000678 // VSX only provides an indexed store.
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000679 if (Is32VSXStore || Is64VSXStore) return false;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000680
Alex Lorenze40c8a22015-08-11 23:09:45 +0000681 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
682 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
683 Addr.Offset),
Bill Schmidtccecf262013-08-30 02:29:45 +0000684 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
685 MFI.getObjectAlignment(Addr.Base.FI));
686
Rafael Espindolaea09c592014-02-18 22:05:46 +0000687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
688 .addReg(SrcReg)
689 .addImm(Addr.Offset)
690 .addFrameIndex(Addr.Base.FI)
691 .addMemOperand(MMO);
Bill Schmidtccecf262013-08-30 02:29:45 +0000692
693 // Base reg with offset in range.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000694 } else if (UseOffset) {
695 // VSX only provides an indexed store.
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000696 if (Is32VSXStore || Is64VSXStore)
697 return false;
698
Rafael Espindolaea09c592014-02-18 22:05:46 +0000699 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
Bill Schmidtccecf262013-08-30 02:29:45 +0000700 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
701
702 // Indexed form.
Bill Seurer8c728ae2014-12-05 20:15:56 +0000703 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +0000704 // Get the RR opcode corresponding to the RI one. FIXME: It would be
705 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
706 // is hard to get at.
707 switch (Opc) {
708 default: llvm_unreachable("Unexpected opcode!");
709 case PPC::STB: Opc = PPC::STBX; break;
710 case PPC::STH : Opc = PPC::STHX; break;
711 case PPC::STW : Opc = PPC::STWX; break;
712 case PPC::STB8: Opc = PPC::STBX8; break;
713 case PPC::STH8: Opc = PPC::STHX8; break;
714 case PPC::STW8: Opc = PPC::STWX8; break;
715 case PPC::STD: Opc = PPC::STDX; break;
Nemanja Ivanovic376e1732015-05-29 17:13:25 +0000716 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
Bill Seurer8c728ae2014-12-05 20:15:56 +0000717 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000718 case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
719 case PPC::SPESTW: Opc = PPC::SPESTWX; break;
Bill Schmidtccecf262013-08-30 02:29:45 +0000720 }
Samuel Antaof6815602015-03-17 15:00:57 +0000721
722 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
723 .addReg(SrcReg);
724
725 // If we have an index register defined we use it in the store inst,
726 // otherwise we use X0 as base as it makes the vector instructions to
727 // use zero in the computation of the effective address regardless the
728 // content of the register.
729 if (IndexReg)
730 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
731 else
732 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
Bill Schmidtccecf262013-08-30 02:29:45 +0000733 }
734
735 return true;
736}
737
738// Attempt to fast-select a store instruction.
739bool PPCFastISel::SelectStore(const Instruction *I) {
740 Value *Op0 = I->getOperand(0);
741 unsigned SrcReg = 0;
742
743 // FIXME: No atomics loads are supported.
744 if (cast<StoreInst>(I)->isAtomic())
745 return false;
746
747 // Verify we have a legal type before going any further.
748 MVT VT;
749 if (!isLoadTypeLegal(Op0->getType(), VT))
750 return false;
751
752 // Get the value to be stored into a register.
753 SrcReg = getRegForValue(Op0);
754 if (SrcReg == 0)
755 return false;
756
757 // See if we can handle this address.
758 Address Addr;
759 if (!PPCComputeAddress(I->getOperand(1), Addr))
760 return false;
761
762 if (!PPCEmitStore(VT, SrcReg, Addr))
763 return false;
764
765 return true;
766}
767
Bill Schmidt03008132013-08-25 22:33:42 +0000768// Attempt to fast-select a branch instruction.
769bool PPCFastISel::SelectBranch(const Instruction *I) {
770 const BranchInst *BI = cast<BranchInst>(I);
771 MachineBasicBlock *BrBB = FuncInfo.MBB;
772 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
773 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
774
775 // For now, just try the simplest case where it's fed by a compare.
776 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Hal Finkel5f2a1372015-05-23 12:18:10 +0000777 if (isValueAvailable(CI)) {
778 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
779 if (!OptPPCPred)
780 return false;
Bill Schmidt03008132013-08-25 22:33:42 +0000781
Hal Finkel5f2a1372015-05-23 12:18:10 +0000782 PPC::Predicate PPCPred = OptPPCPred.getValue();
Bill Schmidt03008132013-08-25 22:33:42 +0000783
Hal Finkel5f2a1372015-05-23 12:18:10 +0000784 // Take advantage of fall-through opportunities.
785 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
786 std::swap(TBB, FBB);
787 PPCPred = PPC::InvertPredicate(PPCPred);
788 }
789
790 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
791
792 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000793 CondReg, PPCPred))
Hal Finkel5f2a1372015-05-23 12:18:10 +0000794 return false;
795
796 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000797 .addImm(PPCSubTarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
798 .addReg(CondReg).addMBB(TBB);
Matthias Braunccfc9c82015-08-26 01:55:47 +0000799 finishCondBranch(BI->getParent(), TBB, FBB);
Hal Finkel5f2a1372015-05-23 12:18:10 +0000800 return true;
Bill Schmidt03008132013-08-25 22:33:42 +0000801 }
Bill Schmidt03008132013-08-25 22:33:42 +0000802 } else if (const ConstantInt *CI =
803 dyn_cast<ConstantInt>(BI->getCondition())) {
804 uint64_t Imm = CI->getZExtValue();
805 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000806 fastEmitBranch(Target, DbgLoc);
Bill Schmidt03008132013-08-25 22:33:42 +0000807 return true;
808 }
809
810 // FIXME: ARM looks for a case where the block containing the compare
811 // has been split from the block containing the branch. If this happens,
812 // there is a vreg available containing the result of the compare. I'm
813 // not sure we can do much, as we've lost the predicate information with
814 // the compare instruction -- we have a 4-bit CR but don't know which bit
815 // to test here.
816 return false;
817}
818
819// Attempt to emit a compare of the two source values. Signed and unsigned
820// comparisons are supported. Return false if we can't handle it.
821bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000822 bool IsZExt, unsigned DestReg,
823 const PPC::Predicate Pred) {
Bill Schmidt03008132013-08-25 22:33:42 +0000824 Type *Ty = SrcValue1->getType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000825 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
Bill Schmidt03008132013-08-25 22:33:42 +0000826 if (!SrcEVT.isSimple())
827 return false;
828 MVT SrcVT = SrcEVT.getSimpleVT();
829
Eric Christopher1b8e7632014-05-22 01:07:24 +0000830 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
Hal Finkel940ab932014-02-28 00:27:01 +0000831 return false;
832
Bill Schmidt03008132013-08-25 22:33:42 +0000833 // See if operand 2 is an immediate encodeable in the compare.
834 // FIXME: Operands are not in canonical order at -O0, so an immediate
835 // operand in position 1 is a lost opportunity for now. We are
836 // similar to ARM in this regard.
837 long Imm = 0;
838 bool UseImm = false;
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000839 const bool HasSPE = PPCSubTarget->hasSPE();
Bill Schmidt03008132013-08-25 22:33:42 +0000840
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000841 // Only 16-bit integer constants can be represented in compares for
Bill Schmidt03008132013-08-25 22:33:42 +0000842 // PowerPC. Others will be materialized into a register.
843 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
844 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
845 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
846 const APInt &CIVal = ConstInt->getValue();
847 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
848 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
849 UseImm = true;
850 }
851 }
852
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000853 unsigned SrcReg1 = getRegForValue(SrcValue1);
854 if (SrcReg1 == 0)
855 return false;
856
857 unsigned SrcReg2 = 0;
858 if (!UseImm) {
859 SrcReg2 = getRegForValue(SrcValue2);
860 if (SrcReg2 == 0)
861 return false;
862 }
863
Bill Schmidt03008132013-08-25 22:33:42 +0000864 unsigned CmpOpc;
865 bool NeedsExt = false;
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000866
867 auto RC1 = MRI.getRegClass(SrcReg1);
868 auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;
869
Bill Schmidt03008132013-08-25 22:33:42 +0000870 switch (SrcVT.SimpleTy) {
871 default: return false;
872 case MVT::f32:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000873 if (HasSPE) {
874 switch (Pred) {
875 default: return false;
876 case PPC::PRED_EQ:
877 CmpOpc = PPC::EFSCMPEQ;
878 break;
879 case PPC::PRED_LT:
880 CmpOpc = PPC::EFSCMPLT;
881 break;
882 case PPC::PRED_GT:
883 CmpOpc = PPC::EFSCMPGT;
884 break;
885 }
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000886 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000887 CmpOpc = PPC::FCMPUS;
Zi Xuan Wufec749f2019-01-30 02:56:22 +0000888 if (isVSSRCRegClass(RC1))
889 SrcReg1 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg1);
890 if (RC2 && isVSSRCRegClass(RC2))
891 SrcReg2 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg2);
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000892 }
Bill Schmidt03008132013-08-25 22:33:42 +0000893 break;
894 case MVT::f64:
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000895 if (HasSPE) {
896 switch (Pred) {
897 default: return false;
898 case PPC::PRED_EQ:
899 CmpOpc = PPC::EFDCMPEQ;
900 break;
901 case PPC::PRED_LT:
902 CmpOpc = PPC::EFDCMPLT;
903 break;
904 case PPC::PRED_GT:
905 CmpOpc = PPC::EFDCMPGT;
906 break;
907 }
Zi Xuan Wu308a6092019-01-25 07:24:59 +0000908 } else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000909 CmpOpc = PPC::XSCMPUDP;
910 } else {
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000911 CmpOpc = PPC::FCMPUD;
Zi Xuan Wu64c956e2019-01-10 06:20:14 +0000912 }
Bill Schmidt03008132013-08-25 22:33:42 +0000913 break;
914 case MVT::i1:
915 case MVT::i8:
916 case MVT::i16:
917 NeedsExt = true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000918 LLVM_FALLTHROUGH;
Bill Schmidt03008132013-08-25 22:33:42 +0000919 case MVT::i32:
920 if (!UseImm)
921 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
922 else
923 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
924 break;
925 case MVT::i64:
926 if (!UseImm)
927 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
928 else
929 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
930 break;
931 }
932
Bill Schmidt03008132013-08-25 22:33:42 +0000933 if (NeedsExt) {
934 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
935 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
936 return false;
937 SrcReg1 = ExtReg;
938
939 if (!UseImm) {
940 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
941 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
942 return false;
943 SrcReg2 = ExtReg;
944 }
945 }
946
947 if (!UseImm)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000949 .addReg(SrcReg1).addReg(SrcReg2);
950 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000951 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +0000952 .addReg(SrcReg1).addImm(Imm);
953
954 return true;
955}
956
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000957// Attempt to fast-select a floating-point extend instruction.
958bool PPCFastISel::SelectFPExt(const Instruction *I) {
959 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000960 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
961 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000962
963 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
964 return false;
965
966 unsigned SrcReg = getRegForValue(Src);
967 if (!SrcReg)
968 return false;
969
970 // No code is generated for a FP extend.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +0000971 updateValueMap(I, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000972 return true;
973}
974
975// Attempt to fast-select a floating-point truncate instruction.
976bool PPCFastISel::SelectFPTrunc(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::f64 || DestVT != MVT::f32)
982 return false;
983
984 unsigned SrcReg = getRegForValue(Src);
985 if (!SrcReg)
986 return false;
987
988 // Round the result to single precision.
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000989 unsigned DestReg;
Kang Zhang4faa4092019-02-25 02:46:16 +0000990 auto RC = MRI.getRegClass(SrcReg);
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000991 if (PPCSubTarget->hasSPE()) {
Craig Topper36e04d142019-09-12 22:07:35 +0000992 DestReg = createResultReg(&PPC::GPRCRegClass);
Justin Hibbitsd52990c2018-07-18 04:25:10 +0000993 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
994 TII.get(PPC::EFSCFD), DestReg)
995 .addReg(SrcReg);
Kang Zhang4faa4092019-02-25 02:46:16 +0000996 } else if (isVSFRCRegClass(RC)) {
997 DestReg = createResultReg(&PPC::VSSRCRegClass);
998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
999 TII.get(PPC::XSRSP), DestReg)
1000 .addReg(SrcReg);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001001 } else {
1002 DestReg = createResultReg(&PPC::F4RCRegClass);
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1004 TII.get(PPC::FRSP), DestReg)
1005 .addReg(SrcReg);
1006 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001007
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001008 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001009 return true;
1010}
1011
1012// Move an i32 or i64 value in a GPR to an f64 value in an FPR.
Samuel Antao1194b8f2014-10-09 20:42:56 +00001013// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001014// those should be used instead of moving via a stack slot when the
1015// subtarget permits.
1016// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1017// stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1018// case to 8 bytes which produces tighter code but wastes stack space.
1019unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1020 bool IsSigned) {
1021
1022 // If necessary, extend 32-bit int to 64-bit.
1023 if (SrcVT == MVT::i32) {
1024 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1025 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1026 return 0;
1027 SrcReg = TmpReg;
1028 }
1029
1030 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1031 Address Addr;
1032 Addr.BaseType = Address::FrameIndexBase;
1033 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1034
1035 // Store the value from the GPR.
1036 if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1037 return 0;
1038
1039 // Load the integer value into an FPR. The kind of load used depends
1040 // on a number of conditions.
1041 unsigned LoadOpc = PPC::LFD;
1042
1043 if (SrcVT == MVT::i32) {
Bill Schmidtff9622e2014-03-18 14:32:50 +00001044 if (!IsSigned) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001045 LoadOpc = PPC::LFIWZX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001046 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Eric Christopher1b8e7632014-05-22 01:07:24 +00001047 } else if (PPCSubTarget->hasLFIWAX()) {
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001048 LoadOpc = PPC::LFIWAX;
Samuel Antao1194b8f2014-10-09 20:42:56 +00001049 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidtff9622e2014-03-18 14:32:50 +00001050 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001051 }
1052
1053 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
Daniel Sanders0c476112019-08-15 19:22:08 +00001054 Register ResultReg = 0;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001055 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1056 return 0;
1057
1058 return ResultReg;
1059}
1060
1061// Attempt to fast-select an integer-to-floating-point conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001062// FIXME: Once fast-isel has better support for VSX, conversions using
1063// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001064bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1065 MVT DstVT;
1066 Type *DstTy = I->getType();
1067 if (!isTypeLegal(DstTy, DstVT))
1068 return false;
1069
1070 if (DstVT != MVT::f32 && DstVT != MVT::f64)
1071 return false;
1072
1073 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001074 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001075 if (!SrcEVT.isSimple())
1076 return false;
1077
1078 MVT SrcVT = SrcEVT.getSimpleVT();
1079
1080 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1081 SrcVT != MVT::i32 && SrcVT != MVT::i64)
1082 return false;
1083
1084 unsigned SrcReg = getRegForValue(Src);
1085 if (SrcReg == 0)
1086 return false;
1087
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001088 // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
1089 if (PPCSubTarget->hasSPE()) {
1090 unsigned Opc;
1091 if (DstVT == MVT::f32)
1092 Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1093 else
1094 Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1095
1096 unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
1097 // Generate the convert.
1098 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1099 .addReg(SrcReg);
1100 updateValueMap(I, DestReg);
1101 return true;
1102 }
1103
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001104 // We can only lower an unsigned convert if we have the newer
1105 // floating-point conversion operations.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001106 if (!IsSigned && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001107 return false;
1108
1109 // FIXME: For now we require the newer floating-point conversion operations
1110 // (which are present only on P7 and A2 server models) when converting
1111 // to single-precision float. Otherwise we have to generate a lot of
1112 // fiddly code to avoid double rounding. If necessary, the fiddly code
1113 // can be found in PPCTargetLowering::LowerINT_TO_FP().
Eric Christopher1b8e7632014-05-22 01:07:24 +00001114 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001115 return false;
1116
1117 // Extend the input if necessary.
1118 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1119 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1120 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1121 return false;
1122 SrcVT = MVT::i64;
1123 SrcReg = TmpReg;
1124 }
1125
1126 // Move the integer value to an FPR.
1127 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1128 if (FPReg == 0)
1129 return false;
1130
1131 // Determine the opcode for the conversion.
1132 const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1133 unsigned DestReg = createResultReg(RC);
1134 unsigned Opc;
1135
1136 if (DstVT == MVT::f32)
1137 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1138 else
1139 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1140
1141 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001142 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001143 .addReg(FPReg);
1144
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001145 updateValueMap(I, DestReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001146 return true;
1147}
1148
1149// Move the floating-point value in SrcReg into an integer destination
1150// register, and return the register (or zero if we can't handle it).
Samuel Antao1194b8f2014-10-09 20:42:56 +00001151// FIXME: When direct register moves are implemented (see PowerISA 2.07),
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001152// those should be used instead of moving via a stack slot when the
1153// subtarget permits.
1154unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1155 unsigned SrcReg, bool IsSigned) {
1156 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1157 // Note that if have STFIWX available, we could use a 4-byte stack
1158 // slot for i32, but this being fast-isel we'll just go with the
1159 // easiest code gen possible.
1160 Address Addr;
1161 Addr.BaseType = Address::FrameIndexBase;
1162 Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1163
1164 // Store the value from the FPR.
1165 if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1166 return 0;
1167
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001168 // Reload it into a GPR. If we want an i32 on big endian, modify the
1169 // address to have a 4-byte offset so we load from the right place.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001170 if (VT == MVT::i32)
Nemanja Ivanovic1a5706c2016-02-29 16:42:27 +00001171 Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001172
1173 // Look at the currently assigned register for this instruction
1174 // to determine the required register class.
1175 unsigned AssignedReg = FuncInfo.ValueMap[I];
1176 const TargetRegisterClass *RC =
Craig Topper062a2ba2014-04-25 05:30:21 +00001177 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001178
Daniel Sanders0c476112019-08-15 19:22:08 +00001179 Register ResultReg = 0;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001180 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1181 return 0;
1182
1183 return ResultReg;
1184}
1185
1186// Attempt to fast-select a floating-point-to-integer conversion.
Nemanja Ivanovicc38b5312015-04-11 10:40:42 +00001187// FIXME: Once fast-isel has better support for VSX, conversions using
1188// direct moves should be implemented.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001189bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1190 MVT DstVT, SrcVT;
1191 Type *DstTy = I->getType();
1192 if (!isTypeLegal(DstTy, DstVT))
1193 return false;
1194
1195 if (DstVT != MVT::i32 && DstVT != MVT::i64)
1196 return false;
1197
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001198 // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
1199 if (DstVT == MVT::i64 && !IsSigned &&
1200 !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
Bill Schmidt83973ef2014-06-24 20:05:18 +00001201 return false;
1202
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001203 Value *Src = I->getOperand(0);
1204 Type *SrcTy = Src->getType();
1205 if (!isTypeLegal(SrcTy, SrcVT))
1206 return false;
1207
1208 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1209 return false;
1210
1211 unsigned SrcReg = getRegForValue(Src);
1212 if (SrcReg == 0)
1213 return false;
1214
Kang Zhang4faa4092019-02-25 02:46:16 +00001215 // Convert f32 to f64 or convert VSSRC to VSFRC if necessary. This is just a
1216 // meaningless copy to get the register class right.
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001217 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
Zi Xuan Wufec749f2019-01-30 02:56:22 +00001218 if (InRC == &PPC::F4RCRegClass)
1219 SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
Kang Zhang4faa4092019-02-25 02:46:16 +00001220 else if (InRC == &PPC::VSSRCRegClass)
1221 SrcReg = copyRegToRegClass(&PPC::VSFRCRegClass, SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001222
1223 // Determine the opcode for the conversion, which takes place
Kang Zhang4faa4092019-02-25 02:46:16 +00001224 // entirely within FPRs or VSRs.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001225 unsigned DestReg;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001226 unsigned Opc;
Kang Zhang4faa4092019-02-25 02:46:16 +00001227 auto RC = MRI.getRegClass(SrcReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001228
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001229 if (PPCSubTarget->hasSPE()) {
Justin Hibbits22e939a2018-07-18 05:19:25 +00001230 DestReg = createResultReg(&PPC::GPRCRegClass);
1231 if (IsSigned)
Craig Topper36e04d142019-09-12 22:07:35 +00001232 Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
Justin Hibbits22e939a2018-07-18 05:19:25 +00001233 else
Craig Topper36e04d142019-09-12 22:07:35 +00001234 Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
Kang Zhang4faa4092019-02-25 02:46:16 +00001235 } else if (isVSFRCRegClass(RC)) {
1236 DestReg = createResultReg(&PPC::VSFRCRegClass);
1237 if (DstVT == MVT::i32)
1238 Opc = IsSigned ? PPC::XSCVDPSXWS : PPC::XSCVDPUXWS;
1239 else
1240 Opc = IsSigned ? PPC::XSCVDPSXDS : PPC::XSCVDPUXDS;
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001241 } else {
1242 DestReg = createResultReg(&PPC::F8RCRegClass);
1243 if (DstVT == MVT::i32)
1244 if (IsSigned)
1245 Opc = PPC::FCTIWZ;
1246 else
1247 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001248 else
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001249 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1250 }
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001251
1252 // Generate the convert.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001254 .addReg(SrcReg);
1255
1256 // Now move the integer value from a float register to an integer register.
Justin Hibbitsd52990c2018-07-18 04:25:10 +00001257 unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
1258 PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1259
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001260 if (IntReg == 0)
1261 return false;
1262
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001263 updateValueMap(I, IntReg);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001264 return true;
1265}
1266
Bill Schmidtccecf262013-08-30 02:29:45 +00001267// Attempt to fast-select a binary integer operation that isn't already
1268// handled automatically.
1269bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001270 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidtccecf262013-08-30 02:29:45 +00001271
1272 // We can get here in the case when we have a binary operation on a non-legal
1273 // type and the target independent selector doesn't know how to handle it.
1274 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1275 return false;
1276
1277 // Look at the currently assigned register for this instruction
1278 // to determine the required register class. If there is no register,
1279 // make a conservative choice (don't assign R0).
1280 unsigned AssignedReg = FuncInfo.ValueMap[I];
1281 const TargetRegisterClass *RC =
1282 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1283 &PPC::GPRC_and_GPRC_NOR0RegClass);
1284 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1285
1286 unsigned Opc;
1287 switch (ISDOpcode) {
1288 default: return false;
1289 case ISD::ADD:
1290 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1291 break;
1292 case ISD::OR:
1293 Opc = IsGPRC ? PPC::OR : PPC::OR8;
1294 break;
1295 case ISD::SUB:
1296 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1297 break;
1298 }
1299
1300 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1301 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1302 if (SrcReg1 == 0) return false;
1303
1304 // Handle case of small immediate operand.
1305 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1306 const APInt &CIVal = ConstInt->getValue();
1307 int Imm = (int)CIVal.getSExtValue();
1308 bool UseImm = true;
1309 if (isInt<16>(Imm)) {
1310 switch (Opc) {
1311 default:
1312 llvm_unreachable("Missing case!");
1313 case PPC::ADD4:
1314 Opc = PPC::ADDI;
1315 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1316 break;
1317 case PPC::ADD8:
1318 Opc = PPC::ADDI8;
1319 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1320 break;
1321 case PPC::OR:
1322 Opc = PPC::ORI;
1323 break;
1324 case PPC::OR8:
1325 Opc = PPC::ORI8;
1326 break;
1327 case PPC::SUBF:
1328 if (Imm == -32768)
1329 UseImm = false;
1330 else {
1331 Opc = PPC::ADDI;
1332 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1333 Imm = -Imm;
1334 }
1335 break;
1336 case PPC::SUBF8:
1337 if (Imm == -32768)
1338 UseImm = false;
1339 else {
1340 Opc = PPC::ADDI8;
1341 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1342 Imm = -Imm;
1343 }
1344 break;
1345 }
1346
1347 if (UseImm) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1349 ResultReg)
1350 .addReg(SrcReg1)
1351 .addImm(Imm);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001352 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001353 return true;
1354 }
1355 }
1356 }
1357
1358 // Reg-reg case.
1359 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1360 if (SrcReg2 == 0) return false;
1361
1362 // Reverse operands for subtract-from.
1363 if (ISDOpcode == ISD::SUB)
1364 std::swap(SrcReg1, SrcReg2);
1365
Rafael Espindolaea09c592014-02-18 22:05:46 +00001366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Bill Schmidtccecf262013-08-30 02:29:45 +00001367 .addReg(SrcReg1).addReg(SrcReg2);
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001368 updateValueMap(I, ResultReg);
Bill Schmidtccecf262013-08-30 02:29:45 +00001369 return true;
1370}
1371
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001372// Handle arguments to a call that we're attempting to fast-select.
1373// Return false if the arguments are too complex for us at the moment.
1374bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1375 SmallVectorImpl<unsigned> &ArgRegs,
1376 SmallVectorImpl<MVT> &ArgVTs,
1377 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1378 SmallVectorImpl<unsigned> &RegArgs,
1379 CallingConv::ID CC,
1380 unsigned &NumBytes,
1381 bool IsVarArg) {
1382 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001383 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001384
1385 // Reserve space for the linkage area on the stack.
Eric Christophera4ae2132015-02-13 22:22:57 +00001386 unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001387 CCInfo.AllocateStack(LinkageSize, 8);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001388
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001389 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1390
1391 // Bail out if we can't handle any of the arguments.
1392 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1393 CCValAssign &VA = ArgLocs[I];
1394 MVT ArgVT = ArgVTs[VA.getValNo()];
1395
1396 // Skip vector arguments for now, as well as long double and
1397 // uint128_t, and anything that isn't passed in a register.
Hal Finkel940ab932014-02-28 00:27:01 +00001398 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001399 !VA.isRegLoc() || VA.needsCustom())
1400 return false;
1401
1402 // Skip bit-converted arguments for now.
1403 if (VA.getLocInfo() == CCValAssign::BCvt)
1404 return false;
1405 }
1406
1407 // Get a count of how many bytes are to be pushed onto the stack.
1408 NumBytes = CCInfo.getNextStackOffset();
1409
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001410 // The prolog code of the callee may store up to 8 GPR argument registers to
1411 // the stack, allowing va_start to index over them in memory if its varargs.
1412 // Because we cannot tell if this is needed on the caller side, we have to
1413 // conservatively assume that it is needed. As such, make sure we have at
1414 // least enough stack space for the caller to store the 8 GPRs.
Ulrich Weigand8658f172014-07-20 23:43:15 +00001415 // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
Ulrich Weigand8ca988f2014-06-23 14:15:53 +00001416 NumBytes = std::max(NumBytes, LinkageSize + 64);
Ulrich Weigandf316e1d2014-06-23 13:47:52 +00001417
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001418 // Issue CALLSEQ_START.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001420 TII.get(TII.getCallFrameSetupOpcode()))
Serge Pavlovd526b132017-05-09 13:35:13 +00001421 .addImm(NumBytes).addImm(0);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001422
1423 // Prepare to assign register arguments. Every argument uses up a
1424 // GPR protocol register even if it's passed in a floating-point
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001425 // register (unless we're using the fast calling convention).
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001426 unsigned NextGPR = PPC::X3;
1427 unsigned NextFPR = PPC::F1;
1428
1429 // Process arguments.
1430 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1431 CCValAssign &VA = ArgLocs[I];
1432 unsigned Arg = ArgRegs[VA.getValNo()];
1433 MVT ArgVT = ArgVTs[VA.getValNo()];
1434
1435 // Handle argument promotion and bitcasts.
1436 switch (VA.getLocInfo()) {
1437 default:
1438 llvm_unreachable("Unknown loc info!");
1439 case CCValAssign::Full:
1440 break;
1441 case CCValAssign::SExt: {
1442 MVT DestVT = VA.getLocVT();
1443 const TargetRegisterClass *RC =
1444 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1445 unsigned TmpReg = createResultReg(RC);
1446 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1447 llvm_unreachable("Failed to emit a sext!");
1448 ArgVT = DestVT;
1449 Arg = TmpReg;
1450 break;
1451 }
1452 case CCValAssign::AExt:
1453 case CCValAssign::ZExt: {
1454 MVT DestVT = VA.getLocVT();
1455 const TargetRegisterClass *RC =
1456 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1457 unsigned TmpReg = createResultReg(RC);
1458 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1459 llvm_unreachable("Failed to emit a zext!");
1460 ArgVT = DestVT;
1461 Arg = TmpReg;
1462 break;
1463 }
1464 case CCValAssign::BCvt: {
1465 // FIXME: Not yet handled.
1466 llvm_unreachable("Should have bailed before getting here!");
1467 break;
1468 }
1469 }
1470
1471 // Copy this argument to the appropriate register.
1472 unsigned ArgReg;
1473 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1474 ArgReg = NextFPR++;
Hal Finkelf81b6dd2015-01-18 12:08:47 +00001475 if (CC != CallingConv::Fast)
1476 ++NextGPR;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001477 } else
1478 ArgReg = NextGPR++;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001479
1480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1481 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001482 RegArgs.push_back(ArgReg);
1483 }
1484
1485 return true;
1486}
1487
1488// For a call that we've determined we can fast-select, finish the
1489// call sequence and generate a copy to obtain the return value (if any).
Hal Finkel934361a2015-01-14 01:07:51 +00001490bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1491 CallingConv::ID CC = CLI.CallConv;
1492
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001493 // Issue CallSEQ_END.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001494 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001495 TII.get(TII.getCallFrameDestroyOpcode()))
1496 .addImm(NumBytes).addImm(0);
1497
1498 // Next, generate a copy to obtain the return value.
1499 // FIXME: No multi-register return values yet, though I don't foresee
1500 // any real difficulties there.
1501 if (RetVT != MVT::isVoid) {
1502 SmallVector<CCValAssign, 16> RVLocs;
Hal Finkel934361a2015-01-14 01:07:51 +00001503 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001504 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1505 CCValAssign &VA = RVLocs[0];
1506 assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1507 assert(VA.isRegLoc() && "Can only return in registers!");
1508
1509 MVT DestVT = VA.getValVT();
1510 MVT CopyVT = DestVT;
1511
1512 // Ints smaller than a register still arrive in a full 64-bit
1513 // register, so make sure we recognize this.
1514 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1515 CopyVT = MVT::i64;
1516
1517 unsigned SourcePhysReg = VA.getLocReg();
Bill Schmidt0954ea12013-08-30 23:25:30 +00001518 unsigned ResultReg = 0;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001519
1520 if (RetVT == CopyVT) {
1521 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
Zi Xuan Wufec749f2019-01-30 02:56:22 +00001522 ResultReg = copyRegToRegClass(CpyRC, SourcePhysReg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001523
1524 // If necessary, round the floating result to single precision.
1525 } else if (CopyVT == MVT::f64) {
1526 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001527 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001528 ResultReg).addReg(SourcePhysReg);
1529
1530 // If only the low half of a general register is needed, generate
1531 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1532 // used along the fast-isel path (not lowered), and downstream logic
1533 // also doesn't like a direct subreg copy on a physical reg.)
1534 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001535 // Convert physical register from G8RC to GPRC.
1536 SourcePhysReg -= PPC::X0 - PPC::R0;
Zi Xuan Wufec749f2019-01-30 02:56:22 +00001537 ResultReg = copyRegToRegClass(&PPC::GPRCRegClass, SourcePhysReg);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001538 }
1539
Bill Schmidt0954ea12013-08-30 23:25:30 +00001540 assert(ResultReg && "ResultReg unset!");
Hal Finkel934361a2015-01-14 01:07:51 +00001541 CLI.InRegs.push_back(SourcePhysReg);
1542 CLI.ResultReg = ResultReg;
1543 CLI.NumResultRegs = 1;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001544 }
Hal Finkel934361a2015-01-14 01:07:51 +00001545
1546 return true;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001547}
1548
Hal Finkel934361a2015-01-14 01:07:51 +00001549bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1550 CallingConv::ID CC = CLI.CallConv;
1551 bool IsTailCall = CLI.IsTailCall;
1552 bool IsVarArg = CLI.IsVarArg;
1553 const Value *Callee = CLI.Callee;
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001554 const MCSymbol *Symbol = CLI.Symbol;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001555
Rafael Espindolace4c2bc2015-06-23 12:21:54 +00001556 if (!Callee && !Symbol)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001557 return false;
1558
1559 // Allow SelectionDAG isel to handle tail calls.
Hal Finkel934361a2015-01-14 01:07:51 +00001560 if (IsTailCall)
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001561 return false;
1562
Hal Finkel934361a2015-01-14 01:07:51 +00001563 // Let SDISel handle vararg functions.
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001564 if (IsVarArg)
1565 return false;
1566
1567 // Handle simple calls for now, with legal return types and
1568 // those that can be extended.
Hal Finkel934361a2015-01-14 01:07:51 +00001569 Type *RetTy = CLI.RetTy;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001570 MVT RetVT;
1571 if (RetTy->isVoidTy())
1572 RetVT = MVT::isVoid;
1573 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1574 RetVT != MVT::i8)
1575 return false;
Hal Finkel50271aae2015-04-01 00:40:48 +00001576 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1577 // We can't handle boolean returns when CR bits are in use.
1578 return false;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001579
1580 // FIXME: No multi-register return values yet.
1581 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1582 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1583 RetVT != MVT::f64) {
1584 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001585 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001586 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1587 if (RVLocs.size() > 1)
1588 return false;
1589 }
1590
1591 // Bail early if more than 8 arguments, as we only currently
1592 // handle arguments passed in registers.
Hal Finkel934361a2015-01-14 01:07:51 +00001593 unsigned NumArgs = CLI.OutVals.size();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001594 if (NumArgs > 8)
1595 return false;
1596
1597 // Set up the argument vectors.
1598 SmallVector<Value*, 8> Args;
1599 SmallVector<unsigned, 8> ArgRegs;
1600 SmallVector<MVT, 8> ArgVTs;
1601 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1602
1603 Args.reserve(NumArgs);
1604 ArgRegs.reserve(NumArgs);
1605 ArgVTs.reserve(NumArgs);
1606 ArgFlags.reserve(NumArgs);
1607
Hal Finkel934361a2015-01-14 01:07:51 +00001608 for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001609 // Only handle easy calls for now. It would be reasonably easy
1610 // to handle <= 8-byte structures passed ByVal in registers, but we
1611 // have to ensure they are right-justified in the register.
Hal Finkel934361a2015-01-14 01:07:51 +00001612 ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1613 if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001614 return false;
1615
Hal Finkel934361a2015-01-14 01:07:51 +00001616 Value *ArgValue = CLI.OutVals[i];
1617 Type *ArgTy = ArgValue->getType();
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001618 MVT ArgVT;
1619 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1620 return false;
1621
1622 if (ArgVT.isVector())
1623 return false;
1624
Hal Finkel934361a2015-01-14 01:07:51 +00001625 unsigned Arg = getRegForValue(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001626 if (Arg == 0)
1627 return false;
1628
Hal Finkel934361a2015-01-14 01:07:51 +00001629 Args.push_back(ArgValue);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001630 ArgRegs.push_back(Arg);
1631 ArgVTs.push_back(ArgVT);
1632 ArgFlags.push_back(Flags);
1633 }
1634
1635 // Process the arguments.
1636 SmallVector<unsigned, 8> RegArgs;
1637 unsigned NumBytes;
1638
1639 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1640 RegArgs, CC, NumBytes, IsVarArg))
1641 return false;
1642
Hal Finkel934361a2015-01-14 01:07:51 +00001643 MachineInstrBuilder MIB;
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001644 // FIXME: No handling for function pointers yet. This requires
1645 // implementing the function descriptor (OPD) setup.
1646 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Hal Finkel934361a2015-01-14 01:07:51 +00001647 if (!GV) {
1648 // patchpoints are a special case; they always dispatch to a pointer value.
1649 // However, we don't actually want to generate the indirect call sequence
1650 // here (that will be generated, as necessary, during asm printing), and
1651 // the call we generate here will be erased by FastISel::selectPatchpoint,
1652 // so don't try very hard...
1653 if (CLI.IsPatchPoint)
1654 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1655 else
1656 return false;
1657 } else {
1658 // Build direct call with NOP for TOC restore.
1659 // FIXME: We can and should optimize away the NOP for local calls.
1660 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1661 TII.get(PPC::BL8_NOP));
1662 // Add callee.
1663 MIB.addGlobalAddress(GV);
1664 }
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001665
1666 // Add implicit physical register uses to the call.
1667 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1668 MIB.addReg(RegArgs[II], RegState::Implicit);
1669
Hal Finkelaf519932015-01-19 07:20:27 +00001670 // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1671 // into the call.
Hal Finkele6698d52015-02-01 15:03:28 +00001672 PPCFuncInfo->setUsesTOCBasePtr();
Hal Finkelc3168122015-01-19 07:44:45 +00001673 MIB.addReg(PPC::X2, RegState::Implicit);
Ulrich Weigandaa0ac4f2014-07-20 23:31:44 +00001674
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001675 // Add a register mask with the call-preserved registers. Proper
1676 // defs for return values will be added by setPhysRegsDeadExcept().
Eric Christopher9deb75d2015-03-11 22:42:13 +00001677 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001678
Hal Finkel934361a2015-01-14 01:07:51 +00001679 CLI.Call = MIB;
1680
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001681 // Finish off the call including any return values.
Hal Finkel934361a2015-01-14 01:07:51 +00001682 return finishCall(RetVT, CLI, NumBytes);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001683}
1684
Bill Schmidtd89f6782013-08-26 19:42:51 +00001685// Attempt to fast-select a return instruction.
1686bool PPCFastISel::SelectRet(const Instruction *I) {
1687
1688 if (!FuncInfo.CanLowerReturn)
1689 return false;
1690
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +00001691 if (TLI.supportSplitCSR(FuncInfo.MF))
1692 return false;
1693
Bill Schmidtd89f6782013-08-26 19:42:51 +00001694 const ReturnInst *Ret = cast<ReturnInst>(I);
1695 const Function &F = *I->getParent()->getParent();
1696
1697 // Build a list of return value registers.
1698 SmallVector<unsigned, 4> RetRegs;
1699 CallingConv::ID CC = F.getCallingConv();
1700
1701 if (Ret->getNumOperands() > 0) {
1702 SmallVector<ISD::OutputArg, 4> Outs;
Matt Arsenault81920b02018-07-28 13:25:19 +00001703 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001704
1705 // Analyze operands of the call, assigning locations to each operand.
1706 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001707 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001708 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1709 const Value *RV = Ret->getOperand(0);
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001710
Bill Schmidtd89f6782013-08-26 19:42:51 +00001711 // FIXME: Only one output register for now.
1712 if (ValLocs.size() > 1)
1713 return false;
1714
Eric Christopherf0024d12015-07-25 00:48:08 +00001715 // Special case for returning a constant integer of any size - materialize
1716 // the constant as an i64 and copy it to the return register.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001717 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
Samuel Antao61570df2014-09-17 23:25:06 +00001718 CCValAssign &VA = ValLocs[0];
1719
Daniel Sanders0c476112019-08-15 19:22:08 +00001720 Register RetReg = VA.getLocReg();
Eric Christopherf0024d12015-07-25 00:48:08 +00001721 // We still need to worry about properly extending the sign. For example,
1722 // we could have only a single bit or a constant that needs zero
1723 // extension rather than sign extension. Make sure we pass the return
1724 // value extension property to integer materialization.
Eric Christopher03df7ac2015-07-25 00:48:06 +00001725 unsigned SrcReg =
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00001726 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
Samuel Antao61570df2014-09-17 23:25:06 +00001727
Rafael Espindolaea09c592014-02-18 22:05:46 +00001728 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Samuel Antao61570df2014-09-17 23:25:06 +00001729 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1730
Bill Schmidtd89f6782013-08-26 19:42:51 +00001731 RetRegs.push_back(RetReg);
1732
1733 } else {
1734 unsigned Reg = getRegForValue(RV);
1735
1736 if (Reg == 0)
1737 return false;
1738
1739 // Copy the result values into the output registers.
1740 for (unsigned i = 0; i < ValLocs.size(); ++i) {
1741
1742 CCValAssign &VA = ValLocs[i];
1743 assert(VA.isRegLoc() && "Can only return in registers!");
1744 RetRegs.push_back(VA.getLocReg());
1745 unsigned SrcReg = Reg + VA.getValNo();
1746
Mehdi Amini44ede332015-07-09 02:09:04 +00001747 EVT RVEVT = TLI.getValueType(DL, RV->getType());
Bill Schmidtd89f6782013-08-26 19:42:51 +00001748 if (!RVEVT.isSimple())
1749 return false;
1750 MVT RVVT = RVEVT.getSimpleVT();
1751 MVT DestVT = VA.getLocVT();
1752
1753 if (RVVT != DestVT && RVVT != MVT::i8 &&
1754 RVVT != MVT::i16 && RVVT != MVT::i32)
1755 return false;
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00001756
Bill Schmidtd89f6782013-08-26 19:42:51 +00001757 if (RVVT != DestVT) {
1758 switch (VA.getLocInfo()) {
1759 default:
1760 llvm_unreachable("Unknown loc info!");
1761 case CCValAssign::Full:
1762 llvm_unreachable("Full value assign but types don't match?");
1763 case CCValAssign::AExt:
1764 case CCValAssign::ZExt: {
1765 const TargetRegisterClass *RC =
1766 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1767 unsigned TmpReg = createResultReg(RC);
1768 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1769 return false;
1770 SrcReg = TmpReg;
1771 break;
1772 }
1773 case CCValAssign::SExt: {
1774 const TargetRegisterClass *RC =
1775 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1776 unsigned TmpReg = createResultReg(RC);
1777 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1778 return false;
1779 SrcReg = TmpReg;
1780 break;
1781 }
1782 }
1783 }
1784
Rafael Espindolaea09c592014-02-18 22:05:46 +00001785 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001786 TII.get(TargetOpcode::COPY), RetRegs[i])
1787 .addReg(SrcReg);
1788 }
1789 }
1790 }
1791
Rafael Espindolaea09c592014-02-18 22:05:46 +00001792 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Hal Finkelf4a22c02015-01-13 17:47:54 +00001793 TII.get(PPC::BLR8));
Bill Schmidtd89f6782013-08-26 19:42:51 +00001794
1795 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1796 MIB.addReg(RetRegs[i], RegState::Implicit);
1797
1798 return true;
1799}
1800
Bill Schmidt03008132013-08-25 22:33:42 +00001801// Attempt to emit an integer extend of SrcReg into DestReg. Both
1802// signed and zero extensions are supported. Return false if we
Bill Schmidtd89f6782013-08-26 19:42:51 +00001803// can't handle it.
Bill Schmidt03008132013-08-25 22:33:42 +00001804bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1805 unsigned DestReg, bool IsZExt) {
Bill Schmidtd89f6782013-08-26 19:42:51 +00001806 if (DestVT != MVT::i32 && DestVT != MVT::i64)
1807 return false;
1808 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1809 return false;
1810
1811 // Signed extensions use EXTSB, EXTSH, EXTSW.
1812 if (!IsZExt) {
1813 unsigned Opc;
1814 if (SrcVT == MVT::i8)
1815 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1816 else if (SrcVT == MVT::i16)
1817 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1818 else {
1819 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1820 Opc = PPC::EXTSW_32_64;
1821 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtd89f6782013-08-26 19:42:51 +00001823 .addReg(SrcReg);
1824
1825 // Unsigned 32-bit extensions use RLWINM.
1826 } else if (DestVT == MVT::i32) {
1827 unsigned MB;
1828 if (SrcVT == MVT::i8)
1829 MB = 24;
1830 else {
1831 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1832 MB = 16;
1833 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00001834 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
Bill Schmidtd89f6782013-08-26 19:42:51 +00001835 DestReg)
1836 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1837
1838 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1839 } else {
1840 unsigned MB;
1841 if (SrcVT == MVT::i8)
1842 MB = 56;
1843 else if (SrcVT == MVT::i16)
1844 MB = 48;
1845 else
1846 MB = 32;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001847 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidtd89f6782013-08-26 19:42:51 +00001848 TII.get(PPC::RLDICL_32_64), DestReg)
1849 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1850 }
1851
1852 return true;
Bill Schmidt03008132013-08-25 22:33:42 +00001853}
1854
1855// Attempt to fast-select an indirect branch instruction.
1856bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1857 unsigned AddrReg = getRegForValue(I->getOperand(0));
1858 if (AddrReg == 0)
1859 return false;
1860
Rafael Espindolaea09c592014-02-18 22:05:46 +00001861 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
Bill Schmidt03008132013-08-25 22:33:42 +00001862 .addReg(AddrReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
Bill Schmidt03008132013-08-25 22:33:42 +00001864
1865 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
Pete Cooperebcd7482015-08-06 20:22:46 +00001866 for (const BasicBlock *SuccBB : IB->successors())
1867 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
Bill Schmidt03008132013-08-25 22:33:42 +00001868
1869 return true;
1870}
1871
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001872// Attempt to fast-select an integer truncate instruction.
1873bool PPCFastISel::SelectTrunc(const Instruction *I) {
1874 Value *Src = I->getOperand(0);
Mehdi Amini44ede332015-07-09 02:09:04 +00001875 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1876 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001877
1878 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1879 return false;
1880
1881 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1882 return false;
1883
1884 unsigned SrcReg = getRegForValue(Src);
1885 if (!SrcReg)
1886 return false;
1887
1888 // The only interesting case is when we need to switch register classes.
Zi Xuan Wufec749f2019-01-30 02:56:22 +00001889 if (SrcVT == MVT::i64)
1890 SrcReg = copyRegToRegClass(&PPC::GPRCRegClass, SrcReg, 0, PPC::sub_32);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001891
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001892 updateValueMap(I, SrcReg);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001893 return true;
1894}
1895
Bill Schmidtd89f6782013-08-26 19:42:51 +00001896// Attempt to fast-select an integer extend instruction.
1897bool PPCFastISel::SelectIntExt(const Instruction *I) {
1898 Type *DestTy = I->getType();
1899 Value *Src = I->getOperand(0);
1900 Type *SrcTy = Src->getType();
1901
1902 bool IsZExt = isa<ZExtInst>(I);
1903 unsigned SrcReg = getRegForValue(Src);
1904 if (!SrcReg) return false;
1905
1906 EVT SrcEVT, DestEVT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001907 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1908 DestEVT = TLI.getValueType(DL, DestTy, true);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001909 if (!SrcEVT.isSimple())
1910 return false;
1911 if (!DestEVT.isSimple())
1912 return false;
1913
1914 MVT SrcVT = SrcEVT.getSimpleVT();
1915 MVT DestVT = DestEVT.getSimpleVT();
1916
1917 // If we know the register class needed for the result of this
1918 // instruction, use it. Otherwise pick the register class of the
1919 // correct size that does not contain X0/R0, since we don't know
1920 // whether downstream uses permit that assignment.
1921 unsigned AssignedReg = FuncInfo.ValueMap[I];
1922 const TargetRegisterClass *RC =
1923 (AssignedReg ? MRI.getRegClass(AssignedReg) :
1924 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1925 &PPC::GPRC_and_GPRC_NOR0RegClass));
1926 unsigned ResultReg = createResultReg(RC);
1927
1928 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1929 return false;
1930
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001931 updateValueMap(I, ResultReg);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001932 return true;
1933}
1934
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001935// Attempt to fast-select an instruction that wasn't handled by
Bill Schmidt03008132013-08-25 22:33:42 +00001936// the table-generated machinery.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00001937bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
Bill Schmidt03008132013-08-25 22:33:42 +00001938
1939 switch (I->getOpcode()) {
Bill Schmidtccecf262013-08-30 02:29:45 +00001940 case Instruction::Load:
1941 return SelectLoad(I);
1942 case Instruction::Store:
1943 return SelectStore(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001944 case Instruction::Br:
1945 return SelectBranch(I);
1946 case Instruction::IndirectBr:
1947 return SelectIndirectBr(I);
Bill Schmidt8d86fe72013-08-30 15:18:11 +00001948 case Instruction::FPExt:
1949 return SelectFPExt(I);
1950 case Instruction::FPTrunc:
1951 return SelectFPTrunc(I);
1952 case Instruction::SIToFP:
1953 return SelectIToFP(I, /*IsSigned*/ true);
1954 case Instruction::UIToFP:
1955 return SelectIToFP(I, /*IsSigned*/ false);
1956 case Instruction::FPToSI:
1957 return SelectFPToI(I, /*IsSigned*/ true);
1958 case Instruction::FPToUI:
1959 return SelectFPToI(I, /*IsSigned*/ false);
Bill Schmidtccecf262013-08-30 02:29:45 +00001960 case Instruction::Add:
1961 return SelectBinaryIntOp(I, ISD::ADD);
1962 case Instruction::Or:
1963 return SelectBinaryIntOp(I, ISD::OR);
1964 case Instruction::Sub:
1965 return SelectBinaryIntOp(I, ISD::SUB);
Bill Schmidt8470b0f2013-08-30 22:18:55 +00001966 case Instruction::Call:
Jason Liu60ec2482019-06-06 19:13:36 +00001967 // On AIX, call lowering uses the DAG-ISEL path currently so that the
1968 // callee of the direct function call instruction will be mapped to the
1969 // symbol for the function's entry point, which is distinct from the
1970 // function descriptor symbol. The latter is the symbol whose XCOFF symbol
1971 // name is the C-linkage name of the source level function.
1972 if (TM.getTargetTriple().isOSAIX())
1973 break;
Hal Finkel934361a2015-01-14 01:07:51 +00001974 return selectCall(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001975 case Instruction::Ret:
1976 return SelectRet(I);
Bill Schmidt9d9510d2013-08-30 23:31:33 +00001977 case Instruction::Trunc:
1978 return SelectTrunc(I);
Bill Schmidtd89f6782013-08-26 19:42:51 +00001979 case Instruction::ZExt:
1980 case Instruction::SExt:
1981 return SelectIntExt(I);
Bill Schmidt03008132013-08-25 22:33:42 +00001982 // Here add other flavors of Instruction::XXX that automated
1983 // cases don't catch. For example, switches are terminators
1984 // that aren't yet handled.
1985 default:
1986 break;
1987 }
1988 return false;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00001989}
1990
1991// Materialize a floating-point constant into a register, and return
1992// the register number (or zero if we failed to handle it).
1993unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1994 // No plans to handle long double here.
1995 if (VT != MVT::f32 && VT != MVT::f64)
1996 return 0;
1997
1998 // All FP constants are loaded from the constant pool.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001999 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002000 assert(Align > 0 && "Unexpectedly missing alignment information!");
2001 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002002 const bool HasSPE = PPCSubTarget->hasSPE();
2003 const TargetRegisterClass *RC;
2004 if (HasSPE)
Craig Topper36e04d142019-09-12 22:07:35 +00002005 RC = ((VT == MVT::f32) ? &PPC::GPRCRegClass : &PPC::SPERCRegClass);
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002006 else
2007 RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2008
Ulrich Weigandc3b495a2016-08-05 15:22:05 +00002009 unsigned DestReg = createResultReg(RC);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002010 CodeModel::Model CModel = TM.getCodeModel();
2011
Alex Lorenze40c8a22015-08-11 23:09:45 +00002012 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2013 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2014 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002015
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002016 unsigned Opc;
2017
2018 if (HasSPE)
2019 Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2020 else
2021 Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2022
Bill Schmidt03008132013-08-25 22:33:42 +00002023 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2024
Hal Finkele6698d52015-02-01 15:03:28 +00002025 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidt03008132013-08-25 22:33:42 +00002026 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
Rafael Espindola79e238a2017-08-03 02:16:21 +00002027 if (CModel == CodeModel::Small) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
Bill Schmidt03008132013-08-25 22:33:42 +00002029 TmpReg)
2030 .addConstantPoolIndex(Idx).addReg(PPC::X2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002031 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidt03008132013-08-25 22:33:42 +00002032 .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2033 } else {
Jason Liu8dd563e2019-07-22 19:55:33 +00002034 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA8(X2, Idx)).
2035 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002036 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
Bill Schmidtbb381d72013-09-17 20:03:25 +00002037 // But for large code model, we must generate a LDtocL followed
2038 // by the LF[SD].
2039 if (CModel == CodeModel::Large) {
2040 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtbb381d72013-09-17 20:03:25 +00002042 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002043 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002044 .addImm(0)
2045 .addReg(TmpReg2);
2046 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00002047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
Bill Schmidtbb381d72013-09-17 20:03:25 +00002048 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2049 .addReg(TmpReg)
2050 .addMemOperand(MMO);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002051 }
2052
2053 return DestReg;
2054}
2055
Bill Schmidtccecf262013-08-30 02:29:45 +00002056// Materialize the address of a global value into a register, and return
2057// the register number (or zero if we failed to handle it).
2058unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2059 assert(VT == MVT::i64 && "Non-address!");
2060 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2061 unsigned DestReg = createResultReg(RC);
2062
2063 // Global values may be plain old object addresses, TLS object
2064 // addresses, constant pool entries, or jump tables. How we generate
2065 // code for these may depend on small, medium, or large code model.
2066 CodeModel::Model CModel = TM.getCodeModel();
2067
2068 // FIXME: Jump tables are not yet required because fast-isel doesn't
2069 // handle switches; if that changes, we need them as well. For now,
2070 // what follows assumes everything's a generic (or TLS) global address.
Bill Schmidtccecf262013-08-30 02:29:45 +00002071
2072 // FIXME: We don't yet handle the complexity of TLS.
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002073 if (GV->isThreadLocal())
Bill Schmidtccecf262013-08-30 02:29:45 +00002074 return 0;
2075
Hal Finkele6698d52015-02-01 15:03:28 +00002076 PPCFuncInfo->setUsesTOCBasePtr();
Bill Schmidtccecf262013-08-30 02:29:45 +00002077 // For small code model, generate a simple TOC load.
Rafael Espindola79e238a2017-08-03 02:16:21 +00002078 if (CModel == CodeModel::Small)
Rafael Espindolaea09c592014-02-18 22:05:46 +00002079 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2080 DestReg)
2081 .addGlobalAddress(GV)
2082 .addReg(PPC::X2);
Bill Schmidtccecf262013-08-30 02:29:45 +00002083 else {
Bill Schmidt5d82f092014-06-16 21:36:02 +00002084 // If the address is an externally defined symbol, a symbol with common
2085 // or externally available linkage, a non-local function address, or a
Bill Schmidtccecf262013-08-30 02:29:45 +00002086 // jump table address (not yet needed), or if we are generating code
2087 // for large code model, we generate:
Jason Liu8dd563e2019-07-22 19:55:33 +00002088 // LDtocL(GV, ADDIStocHA8(%x2, GV))
Bill Schmidtccecf262013-08-30 02:29:45 +00002089 // Otherwise we generate:
Jason Liu8dd563e2019-07-22 19:55:33 +00002090 // ADDItocL(ADDIStocHA8(%x2, GV), GV)
2091 // Either way, start with the ADDIStocHA8:
Bill Schmidtccecf262013-08-30 02:29:45 +00002092 unsigned HighPartReg = createResultReg(RC);
Jason Liu8dd563e2019-07-22 19:55:33 +00002093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
Bill Schmidtccecf262013-08-30 02:29:45 +00002094 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2095
Jinsong Jie065e5f2019-09-20 18:21:07 +00002096 if (PPCSubTarget->isGVIndirectSymbol(GV)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002097 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002098 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
Eric Christopherc1808362015-11-20 20:51:31 +00002099 } else {
Bill Schmidtccecf262013-08-30 02:29:45 +00002100 // Otherwise generate the ADDItocL.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002101 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
Bill Schmidtccecf262013-08-30 02:29:45 +00002102 DestReg).addReg(HighPartReg).addGlobalAddress(GV);
Eric Christopherc1808362015-11-20 20:51:31 +00002103 }
Bill Schmidtccecf262013-08-30 02:29:45 +00002104 }
2105
2106 return DestReg;
2107}
2108
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002109// Materialize a 32-bit integer constant into a register, and return
2110// the register number (or zero if we failed to handle it).
2111unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2112 const TargetRegisterClass *RC) {
2113 unsigned Lo = Imm & 0xFFFF;
2114 unsigned Hi = (Imm >> 16) & 0xFFFF;
2115
2116 unsigned ResultReg = createResultReg(RC);
2117 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2118
2119 if (isInt<16>(Imm))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002120 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002121 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2122 .addImm(Imm);
2123 else if (Lo) {
2124 // Both Lo and Hi have nonzero bits.
2125 unsigned TmpReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002126 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002127 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2128 .addImm(Hi);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002129 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002130 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2131 .addReg(TmpReg).addImm(Lo);
2132 } else
2133 // Just Hi bits.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002134 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002135 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002136 .addImm(Hi);
2137
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002138 return ResultReg;
2139}
2140
2141// Materialize a 64-bit integer constant into a register, and return
2142// the register number (or zero if we failed to handle it).
2143unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2144 const TargetRegisterClass *RC) {
2145 unsigned Remainder = 0;
2146 unsigned Shift = 0;
2147
2148 // If the value doesn't fit in 32 bits, see if we can shift it
2149 // so that it fits in 32 bits.
2150 if (!isInt<32>(Imm)) {
2151 Shift = countTrailingZeros<uint64_t>(Imm);
2152 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2153
2154 if (isInt<32>(ImmSh))
2155 Imm = ImmSh;
2156 else {
2157 Remainder = Imm;
2158 Shift = 32;
2159 Imm >>= 32;
2160 }
2161 }
2162
2163 // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2164 // (if not shifted).
2165 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2166 if (!Shift)
2167 return TmpReg1;
2168
2169 // If upper 32 bits were not zero, we've built them and need to shift
2170 // them into place.
2171 unsigned TmpReg2;
2172 if (Imm) {
2173 TmpReg2 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002175 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2176 } else
2177 TmpReg2 = TmpReg1;
2178
2179 unsigned TmpReg3, Hi, Lo;
2180 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2181 TmpReg3 = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002182 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002183 TmpReg3).addReg(TmpReg2).addImm(Hi);
2184 } else
2185 TmpReg3 = TmpReg2;
2186
2187 if ((Lo = Remainder & 0xFFFF)) {
2188 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002190 ResultReg).addReg(TmpReg3).addImm(Lo);
2191 return ResultReg;
2192 }
2193
2194 return TmpReg3;
2195}
2196
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002197// Materialize an integer constant into a register, and return
2198// the register number (or zero if we failed to handle it).
Eric Christopher03df7ac2015-07-25 00:48:06 +00002199unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2200 bool UseSExt) {
Hal Finkel940ab932014-02-28 00:27:01 +00002201 // If we're using CR bit registers for i1 values, handle that as a special
2202 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002203 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002204 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2206 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2207 return ImmReg;
2208 }
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002209
Eric Christopher80ba58a2016-01-29 07:19:49 +00002210 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2211 VT != MVT::i1)
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002212 return 0;
2213
Eric Christopher80ba58a2016-01-29 07:19:49 +00002214 const TargetRegisterClass *RC =
2215 ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002216 int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002217
2218 // If the constant is in range, use a load-immediate.
Eric Christopher7d9b9b22016-01-29 07:20:30 +00002219 // Since LI will sign extend the constant we need to make sure that for
2220 // our zeroext constants that the sign extended constant fits into 16-bits -
2221 // a range of 0..0x7fff.
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002222 if (isInt<16>(Imm)) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002223 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2224 unsigned ImmReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
Nemanja Ivanovicb6fdce42016-02-04 23:14:42 +00002226 .addImm(Imm);
Eric Christopherf0024d12015-07-25 00:48:08 +00002227 return ImmReg;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002228 }
2229
2230 // Construct the constant piecewise.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002231 if (VT == MVT::i64)
2232 return PPCMaterialize64BitInt(Imm, RC);
2233 else if (VT == MVT::i32)
2234 return PPCMaterialize32BitInt(Imm, RC);
2235
2236 return 0;
2237}
2238
2239// Materialize a constant into a register, and return the register
2240// number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002241unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002242 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002243
2244 // Only handle simple types.
2245 if (!CEVT.isSimple()) return 0;
2246 MVT VT = CEVT.getSimpleVT();
2247
2248 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2249 return PPCMaterializeFP(CFP, VT);
Bill Schmidtccecf262013-08-30 02:29:45 +00002250 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2251 return PPCMaterializeGV(GV, VT);
Eric Christopher03df7ac2015-07-25 00:48:06 +00002252 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Hal Finkel73390c72016-09-04 06:07:19 +00002253 // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2254 // assumes that constant PHI operands will be zero extended, and failure to
2255 // match that assumption will cause problems if we sign extend here but
2256 // some user of a PHI is in a block for which we fall back to full SDAG
2257 // instruction selection.
2258 return PPCMaterializeInt(CI, VT, false);
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002259
2260 return 0;
2261}
2262
2263// Materialize the address created by an alloca into a register, and
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002264// return the register number (or zero if we failed to handle it).
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002265unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002266 // Don't handle dynamic allocas.
2267 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2268
2269 MVT VT;
2270 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2271
2272 DenseMap<const AllocaInst*, int>::iterator SI =
2273 FuncInfo.StaticAllocaMap.find(AI);
2274
2275 if (SI != FuncInfo.StaticAllocaMap.end()) {
2276 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
Bill Schmidteb8d6f72013-08-31 02:33:40 +00002278 ResultReg).addFrameIndex(SI->second).addImm(0);
2279 return ResultReg;
2280 }
2281
2282 return 0;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002283}
2284
Bill Schmidtccecf262013-08-30 02:29:45 +00002285// Fold loads into extends when possible.
2286// FIXME: We can have multiple redundant extend/trunc instructions
2287// following a load. The folding only picks up one. Extend this
2288// to check subsequent instructions for the same pattern and remove
2289// them. Thus ResultReg should be the def reg for the last redundant
2290// instruction in a chain, and all intervening instructions can be
2291// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2292// to add ELF64-NOT: rldicl to the appropriate tests when this works.
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002293bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2294 const LoadInst *LI) {
Bill Schmidtccecf262013-08-30 02:29:45 +00002295 // Verify we have a legal type before going any further.
2296 MVT VT;
2297 if (!isLoadTypeLegal(LI->getType(), VT))
2298 return false;
2299
2300 // Combine load followed by zero- or sign-extend.
2301 bool IsZExt = false;
2302 switch(MI->getOpcode()) {
2303 default:
2304 return false;
2305
2306 case PPC::RLDICL:
2307 case PPC::RLDICL_32_64: {
2308 IsZExt = true;
2309 unsigned MB = MI->getOperand(3).getImm();
2310 if ((VT == MVT::i8 && MB <= 56) ||
2311 (VT == MVT::i16 && MB <= 48) ||
2312 (VT == MVT::i32 && MB <= 32))
2313 break;
2314 return false;
2315 }
2316
2317 case PPC::RLWINM:
2318 case PPC::RLWINM8: {
2319 IsZExt = true;
2320 unsigned MB = MI->getOperand(3).getImm();
2321 if ((VT == MVT::i8 && MB <= 24) ||
2322 (VT == MVT::i16 && MB <= 16))
2323 break;
2324 return false;
2325 }
2326
2327 case PPC::EXTSB:
2328 case PPC::EXTSB8:
2329 case PPC::EXTSB8_32_64:
2330 /* There is no sign-extending load-byte instruction. */
2331 return false;
2332
2333 case PPC::EXTSH:
2334 case PPC::EXTSH8:
2335 case PPC::EXTSH8_32_64: {
2336 if (VT != MVT::i16 && VT != MVT::i8)
2337 return false;
2338 break;
2339 }
2340
2341 case PPC::EXTSW:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002342 case PPC::EXTSW_32:
Bill Schmidtccecf262013-08-30 02:29:45 +00002343 case PPC::EXTSW_32_64: {
2344 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2345 return false;
2346 break;
2347 }
2348 }
2349
2350 // See if we can handle this address.
2351 Address Addr;
2352 if (!PPCComputeAddress(LI->getOperand(0), Addr))
2353 return false;
2354
Daniel Sanders0c476112019-08-15 19:22:08 +00002355 Register ResultReg = MI->getOperand(0).getReg();
Bill Schmidtccecf262013-08-30 02:29:45 +00002356
Justin Hibbitsd52990c2018-07-18 04:25:10 +00002357 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
2358 PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
Bill Schmidtccecf262013-08-30 02:29:45 +00002359 return false;
2360
Tim Northover256a16d2018-12-17 17:25:53 +00002361 MachineBasicBlock::iterator I(MI);
2362 removeDeadCode(I, std::next(I));
Bill Schmidtccecf262013-08-30 02:29:45 +00002363 return true;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002364}
2365
2366// Attempt to lower call arguments in a faster way than done by
2367// the selection DAG code.
Juergen Ributzka5b8bb4d2014-09-03 20:56:52 +00002368bool PPCFastISel::fastLowerArguments() {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002369 // Defer to normal argument lowering for now. It's reasonably
2370 // efficient. Consider doing something like ARM to handle the
2371 // case where all args fit in registers, no varargs, no float
2372 // or vector args.
2373 return false;
2374}
2375
Bill Schmidt03008132013-08-25 22:33:42 +00002376// Handle materializing integer constants into a register. This is not
2377// automatically generated for PowerPC, so must be explicitly created here.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002378unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002379
Bill Schmidt03008132013-08-25 22:33:42 +00002380 if (Opc != ISD::Constant)
2381 return 0;
2382
Hal Finkel940ab932014-02-28 00:27:01 +00002383 // If we're using CR bit registers for i1 values, handle that as a special
2384 // case first.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002385 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
Hal Finkel940ab932014-02-28 00:27:01 +00002386 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2388 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2389 return ImmReg;
2390 }
2391
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +00002392 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2393 VT != MVT::i1)
Bill Schmidt03008132013-08-25 22:33:42 +00002394 return 0;
2395
2396 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2397 &PPC::GPRCRegClass);
2398 if (VT == MVT::i64)
2399 return PPCMaterialize64BitInt(Imm, RC);
2400 else
2401 return PPCMaterialize32BitInt(Imm, RC);
2402}
2403
Bill Schmidtccecf262013-08-30 02:29:45 +00002404// Override for ADDI and ADDI8 to set the correct register class
2405// on RHS operand 0. The automatic infrastructure naively assumes
2406// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2407// for these cases. At the moment, none of the other automatically
2408// generated RI instructions require special treatment. However, once
2409// SelectSelect is implemented, "isel" requires similar handling.
2410//
2411// Also be conservative about the output register class. Avoid
2412// assigning R0 or X0 to the output register for GPRC and G8RC
2413// register classes, as any such result could be used in ADDI, etc.,
2414// where those regs have another meaning.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002415unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002416 const TargetRegisterClass *RC,
2417 unsigned Op0, bool Op0IsKill,
2418 uint64_t Imm) {
2419 if (MachineInstOpcode == PPC::ADDI)
2420 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2421 else if (MachineInstOpcode == PPC::ADDI8)
2422 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2423
2424 const TargetRegisterClass *UseRC =
2425 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2426 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2427
Juergen Ributzka88e32512014-09-03 20:56:59 +00002428 return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
Bill Schmidtccecf262013-08-30 02:29:45 +00002429 Op0, Op0IsKill, Imm);
2430}
2431
2432// Override for instructions with one register operand to avoid use of
2433// R0/X0. The automatic infrastructure isn't aware of the context so
2434// we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002435unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002436 const TargetRegisterClass* RC,
2437 unsigned Op0, bool Op0IsKill) {
2438 const TargetRegisterClass *UseRC =
2439 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2440 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2441
Juergen Ributzka88e32512014-09-03 20:56:59 +00002442 return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
Bill Schmidtccecf262013-08-30 02:29:45 +00002443}
2444
2445// Override for instructions with two register operands to avoid use
2446// of R0/X0. The automatic infrastructure isn't aware of the context
2447// so we must be conservative.
Juergen Ributzka88e32512014-09-03 20:56:59 +00002448unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
Bill Schmidtccecf262013-08-30 02:29:45 +00002449 const TargetRegisterClass* RC,
2450 unsigned Op0, bool Op0IsKill,
2451 unsigned Op1, bool Op1IsKill) {
2452 const TargetRegisterClass *UseRC =
2453 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2454 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2455
Juergen Ributzka88e32512014-09-03 20:56:59 +00002456 return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
Bill Schmidtccecf262013-08-30 02:29:45 +00002457 Op1, Op1IsKill);
2458}
2459
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002460namespace llvm {
2461 // Create the fast instruction selector for PowerPC64 ELF.
2462 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2463 const TargetLibraryInfo *LibInfo) {
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002464 // Only available on 64-bit ELF for now.
Eric Christophercccae792015-01-30 22:02:31 +00002465 const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
Sean Fertile5f85a7b2019-08-22 15:11:28 +00002466 if (Subtarget.is64BitELFABI())
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002467 return new PPCFastISel(FuncInfo, LibInfo);
Craig Topper062a2ba2014-04-25 05:30:21 +00002468 return nullptr;
Bill Schmidt0cf702f2013-07-30 00:50:39 +00002469 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002470}