1f785676fSDimitry Andric //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric // The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric //
10f785676fSDimitry Andric // This file defines the PowerPC-specific support for the FastISel class. Some
11f785676fSDimitry Andric // of the target-specific code is generated by tablegen in the file
12f785676fSDimitry Andric // PPCGenFastISel.inc, which is #included here.
13f785676fSDimitry Andric //
14f785676fSDimitry Andric //===----------------------------------------------------------------------===//
15f785676fSDimitry Andric
1691bc56edSDimitry Andric #include "MCTargetDesc/PPCPredicates.h"
17db17bf38SDimitry Andric #include "PPC.h"
183ca95b02SDimitry Andric #include "PPCCCState.h"
19db17bf38SDimitry Andric #include "PPCCallingConv.h"
20f785676fSDimitry Andric #include "PPCISelLowering.h"
21ff0cc061SDimitry Andric #include "PPCMachineFunctionInfo.h"
22f785676fSDimitry Andric #include "PPCSubtarget.h"
23f785676fSDimitry Andric #include "PPCTargetMachine.h"
24f785676fSDimitry Andric #include "llvm/ADT/Optional.h"
25f785676fSDimitry Andric #include "llvm/CodeGen/CallingConvLower.h"
26f785676fSDimitry Andric #include "llvm/CodeGen/FastISel.h"
27f785676fSDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h"
28f785676fSDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
29f785676fSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
30f785676fSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
31f785676fSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
322cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
33f785676fSDimitry Andric #include "llvm/IR/CallingConv.h"
3491bc56edSDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
35f785676fSDimitry Andric #include "llvm/IR/GlobalAlias.h"
36f785676fSDimitry Andric #include "llvm/IR/GlobalVariable.h"
37f785676fSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
38f785676fSDimitry Andric #include "llvm/IR/Operator.h"
39f785676fSDimitry Andric #include "llvm/Support/Debug.h"
40f785676fSDimitry Andric #include "llvm/Target/TargetMachine.h"
41f785676fSDimitry Andric
42f785676fSDimitry Andric //===----------------------------------------------------------------------===//
43f785676fSDimitry Andric //
44f785676fSDimitry Andric // TBD:
4539d628a0SDimitry Andric // fastLowerArguments: Handle simple cases.
46f785676fSDimitry Andric // PPCMaterializeGV: Handle TLS.
47f785676fSDimitry Andric // SelectCall: Handle function pointers.
48f785676fSDimitry Andric // SelectCall: Handle multi-register return values.
49f785676fSDimitry Andric // SelectCall: Optimize away nops for local calls.
50f785676fSDimitry Andric // processCallArgs: Handle bit-converted arguments.
51f785676fSDimitry Andric // finishCall: Handle multi-register return values.
52f785676fSDimitry Andric // PPCComputeAddress: Handle parameter references as FrameIndex's.
53f785676fSDimitry Andric // PPCEmitCmp: Handle immediate as operand 1.
54f785676fSDimitry Andric // SelectCall: Handle small byval arguments.
55f785676fSDimitry Andric // SelectIntrinsicCall: Implement.
56f785676fSDimitry Andric // SelectSelect: Implement.
57f785676fSDimitry Andric // Consider factoring isTypeLegal into the base class.
58f785676fSDimitry Andric // Implement switches and jump tables.
59f785676fSDimitry Andric //
60f785676fSDimitry Andric //===----------------------------------------------------------------------===//
61f785676fSDimitry Andric using namespace llvm;
62f785676fSDimitry Andric
6391bc56edSDimitry Andric #define DEBUG_TYPE "ppcfastisel"
6491bc56edSDimitry Andric
65f785676fSDimitry Andric namespace {
66f785676fSDimitry Andric
67f785676fSDimitry Andric typedef struct Address {
68f785676fSDimitry Andric enum {
69f785676fSDimitry Andric RegBase,
70f785676fSDimitry Andric FrameIndexBase
71f785676fSDimitry Andric } BaseType;
72f785676fSDimitry Andric
73f785676fSDimitry Andric union {
74f785676fSDimitry Andric unsigned Reg;
75f785676fSDimitry Andric int FI;
76f785676fSDimitry Andric } Base;
77f785676fSDimitry Andric
78f785676fSDimitry Andric long Offset;
79f785676fSDimitry Andric
80f785676fSDimitry Andric // Innocuous defaults for our address.
Address__anon08f32e980111::Address81f785676fSDimitry Andric Address()
82f785676fSDimitry Andric : BaseType(RegBase), Offset(0) {
83f785676fSDimitry Andric Base.Reg = 0;
84f785676fSDimitry Andric }
85f785676fSDimitry Andric } Address;
86f785676fSDimitry Andric
8791bc56edSDimitry Andric class PPCFastISel final : public FastISel {
88f785676fSDimitry Andric
89f785676fSDimitry Andric const TargetMachine &TM;
90ff0cc061SDimitry Andric const PPCSubtarget *PPCSubTarget;
91ff0cc061SDimitry Andric PPCFunctionInfo *PPCFuncInfo;
92f785676fSDimitry Andric const TargetInstrInfo &TII;
93f785676fSDimitry Andric const TargetLowering &TLI;
94f785676fSDimitry Andric LLVMContext *Context;
95f785676fSDimitry Andric
96f785676fSDimitry Andric public:
PPCFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)97f785676fSDimitry Andric explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
98f785676fSDimitry Andric const TargetLibraryInfo *LibInfo)
9939d628a0SDimitry Andric : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
100ff0cc061SDimitry Andric PPCSubTarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
101ff0cc061SDimitry Andric PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
102ff0cc061SDimitry Andric TII(*PPCSubTarget->getInstrInfo()),
103ff0cc061SDimitry Andric TLI(*PPCSubTarget->getTargetLowering()),
104f785676fSDimitry Andric Context(&FuncInfo.Fn->getContext()) {}
105f785676fSDimitry Andric
106f785676fSDimitry Andric // Backend specific FastISel code.
107f785676fSDimitry Andric private:
10839d628a0SDimitry Andric bool fastSelectInstruction(const Instruction *I) override;
10939d628a0SDimitry Andric unsigned fastMaterializeConstant(const Constant *C) override;
11039d628a0SDimitry Andric unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
11191bc56edSDimitry Andric bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
11291bc56edSDimitry Andric const LoadInst *LI) override;
11339d628a0SDimitry Andric bool fastLowerArguments() override;
11439d628a0SDimitry Andric unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
11539d628a0SDimitry Andric unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
116f785676fSDimitry Andric const TargetRegisterClass *RC,
117f785676fSDimitry Andric unsigned Op0, bool Op0IsKill,
118f785676fSDimitry Andric uint64_t Imm);
11939d628a0SDimitry Andric unsigned fastEmitInst_r(unsigned MachineInstOpcode,
120f785676fSDimitry Andric const TargetRegisterClass *RC,
121f785676fSDimitry Andric unsigned Op0, bool Op0IsKill);
12239d628a0SDimitry Andric unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
123f785676fSDimitry Andric const TargetRegisterClass *RC,
124f785676fSDimitry Andric unsigned Op0, bool Op0IsKill,
125f785676fSDimitry Andric unsigned Op1, bool Op1IsKill);
126f785676fSDimitry Andric
12739d628a0SDimitry Andric bool fastLowerCall(CallLoweringInfo &CLI) override;
12839d628a0SDimitry Andric
129f785676fSDimitry Andric // Instruction selection routines.
130f785676fSDimitry Andric private:
131f785676fSDimitry Andric bool SelectLoad(const Instruction *I);
132f785676fSDimitry Andric bool SelectStore(const Instruction *I);
133f785676fSDimitry Andric bool SelectBranch(const Instruction *I);
134f785676fSDimitry Andric bool SelectIndirectBr(const Instruction *I);
135f785676fSDimitry Andric bool SelectFPExt(const Instruction *I);
136f785676fSDimitry Andric bool SelectFPTrunc(const Instruction *I);
137f785676fSDimitry Andric bool SelectIToFP(const Instruction *I, bool IsSigned);
138f785676fSDimitry Andric bool SelectFPToI(const Instruction *I, bool IsSigned);
139f785676fSDimitry Andric bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
140f785676fSDimitry Andric bool SelectRet(const Instruction *I);
141f785676fSDimitry Andric bool SelectTrunc(const Instruction *I);
142f785676fSDimitry Andric bool SelectIntExt(const Instruction *I);
143f785676fSDimitry Andric
144f785676fSDimitry Andric // Utility routines.
145f785676fSDimitry Andric private:
146f785676fSDimitry Andric bool isTypeLegal(Type *Ty, MVT &VT);
147f785676fSDimitry Andric bool isLoadTypeLegal(Type *Ty, MVT &VT);
148ff0cc061SDimitry Andric bool isValueAvailable(const Value *V) const;
isVSFRCRegClass(const TargetRegisterClass * RC) const149d88c1a5aSDimitry Andric bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
150d88c1a5aSDimitry Andric return RC->getID() == PPC::VSFRCRegClassID;
15139d628a0SDimitry Andric }
isVSSRCRegClass(const TargetRegisterClass * RC) const152d88c1a5aSDimitry Andric bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
153d88c1a5aSDimitry Andric return RC->getID() == PPC::VSSRCRegClassID;
15497bc6c73SDimitry Andric }
155f785676fSDimitry Andric bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
1564ba319b5SDimitry Andric bool isZExt, unsigned DestReg,
1574ba319b5SDimitry Andric const PPC::Predicate Pred);
158f785676fSDimitry Andric bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
159f785676fSDimitry Andric const TargetRegisterClass *RC, bool IsZExt = true,
160f785676fSDimitry Andric unsigned FP64LoadOpc = PPC::LFD);
161f785676fSDimitry Andric bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
162f785676fSDimitry Andric bool PPCComputeAddress(const Value *Obj, Address &Addr);
1633ca95b02SDimitry Andric void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
164f785676fSDimitry Andric unsigned &IndexReg);
165f785676fSDimitry Andric bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
166f785676fSDimitry Andric unsigned DestReg, bool IsZExt);
167f785676fSDimitry Andric unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
168f785676fSDimitry Andric unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
1697d523365SDimitry Andric unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
1707d523365SDimitry Andric bool UseSExt = true);
171f785676fSDimitry Andric unsigned PPCMaterialize32BitInt(int64_t Imm,
172f785676fSDimitry Andric const TargetRegisterClass *RC);
173f785676fSDimitry Andric unsigned PPCMaterialize64BitInt(int64_t Imm,
174f785676fSDimitry Andric const TargetRegisterClass *RC);
175f785676fSDimitry Andric unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
176f785676fSDimitry Andric unsigned SrcReg, bool IsSigned);
177f785676fSDimitry Andric unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
178f785676fSDimitry Andric
179f785676fSDimitry Andric // Call handling routines.
180f785676fSDimitry Andric private:
181f785676fSDimitry Andric bool processCallArgs(SmallVectorImpl<Value*> &Args,
182f785676fSDimitry Andric SmallVectorImpl<unsigned> &ArgRegs,
183f785676fSDimitry Andric SmallVectorImpl<MVT> &ArgVTs,
184f785676fSDimitry Andric SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
185f785676fSDimitry Andric SmallVectorImpl<unsigned> &RegArgs,
186f785676fSDimitry Andric CallingConv::ID CC,
187f785676fSDimitry Andric unsigned &NumBytes,
188f785676fSDimitry Andric bool IsVarArg);
18939d628a0SDimitry Andric bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
1903ca95b02SDimitry Andric LLVM_ATTRIBUTE_UNUSED CCAssignFn *usePPC32CCs(unsigned Flag);
191f785676fSDimitry Andric
192f785676fSDimitry Andric private:
193f785676fSDimitry Andric #include "PPCGenFastISel.inc"
194f785676fSDimitry Andric
195f785676fSDimitry Andric };
196f785676fSDimitry Andric
197f785676fSDimitry Andric } // end anonymous namespace
198f785676fSDimitry Andric
199f785676fSDimitry Andric #include "PPCGenCallingConv.inc"
200f785676fSDimitry Andric
201f785676fSDimitry Andric // Function whose sole purpose is to kill compiler warnings
202f785676fSDimitry Andric // stemming from unused functions included from PPCGenCallingConv.inc.
usePPC32CCs(unsigned Flag)203f785676fSDimitry Andric CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) {
204f785676fSDimitry Andric if (Flag == 1)
205f785676fSDimitry Andric return CC_PPC32_SVR4;
206f785676fSDimitry Andric else if (Flag == 2)
207f785676fSDimitry Andric return CC_PPC32_SVR4_ByVal;
208f785676fSDimitry Andric else if (Flag == 3)
209f785676fSDimitry Andric return CC_PPC32_SVR4_VarArg;
2104ba319b5SDimitry Andric else if (Flag == 4)
2114ba319b5SDimitry Andric return RetCC_PPC_Cold;
212f785676fSDimitry Andric else
213f785676fSDimitry Andric return RetCC_PPC;
214f785676fSDimitry Andric }
215f785676fSDimitry Andric
getComparePred(CmpInst::Predicate Pred)216f785676fSDimitry Andric static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
217f785676fSDimitry Andric switch (Pred) {
218f785676fSDimitry Andric // These are not representable with any single compare.
219f785676fSDimitry Andric case CmpInst::FCMP_FALSE:
2203ca95b02SDimitry Andric case CmpInst::FCMP_TRUE:
2213ca95b02SDimitry Andric // Major concern about the following 6 cases is NaN result. The comparison
2223ca95b02SDimitry Andric // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
2233ca95b02SDimitry Andric // only one of which will be set. The result is generated by fcmpu
2243ca95b02SDimitry Andric // instruction. However, bc instruction only inspects one of the first 3
2254ba319b5SDimitry Andric // bits, so when un is set, bc instruction may jump to an undesired
2263ca95b02SDimitry Andric // place.
2273ca95b02SDimitry Andric //
2283ca95b02SDimitry Andric // More specifically, if we expect an unordered comparison and un is set, we
2293ca95b02SDimitry Andric // expect to always go to true branch; in such case UEQ, UGT and ULT still
2303ca95b02SDimitry Andric // give false, which are undesired; but UNE, UGE, ULE happen to give true,
2313ca95b02SDimitry Andric // since they are tested by inspecting !eq, !lt, !gt, respectively.
2323ca95b02SDimitry Andric //
2333ca95b02SDimitry Andric // Similarly, for ordered comparison, when un is set, we always expect the
2343ca95b02SDimitry Andric // result to be false. In such case OGT, OLT and OEQ is good, since they are
2353ca95b02SDimitry Andric // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
2363ca95b02SDimitry Andric // and ONE are tested through !lt, !gt and !eq, and these are true.
237f785676fSDimitry Andric case CmpInst::FCMP_UEQ:
238f785676fSDimitry Andric case CmpInst::FCMP_UGT:
239f785676fSDimitry Andric case CmpInst::FCMP_ULT:
2403ca95b02SDimitry Andric case CmpInst::FCMP_OGE:
2413ca95b02SDimitry Andric case CmpInst::FCMP_OLE:
2423ca95b02SDimitry Andric case CmpInst::FCMP_ONE:
243f785676fSDimitry Andric default:
244f785676fSDimitry Andric return Optional<PPC::Predicate>();
245f785676fSDimitry Andric
246f785676fSDimitry Andric case CmpInst::FCMP_OEQ:
247f785676fSDimitry Andric case CmpInst::ICMP_EQ:
248f785676fSDimitry Andric return PPC::PRED_EQ;
249f785676fSDimitry Andric
250f785676fSDimitry Andric case CmpInst::FCMP_OGT:
251f785676fSDimitry Andric case CmpInst::ICMP_UGT:
252f785676fSDimitry Andric case CmpInst::ICMP_SGT:
253f785676fSDimitry Andric return PPC::PRED_GT;
254f785676fSDimitry Andric
2553ca95b02SDimitry Andric case CmpInst::FCMP_UGE:
256f785676fSDimitry Andric case CmpInst::ICMP_UGE:
257f785676fSDimitry Andric case CmpInst::ICMP_SGE:
258f785676fSDimitry Andric return PPC::PRED_GE;
259f785676fSDimitry Andric
260f785676fSDimitry Andric case CmpInst::FCMP_OLT:
261f785676fSDimitry Andric case CmpInst::ICMP_ULT:
262f785676fSDimitry Andric case CmpInst::ICMP_SLT:
263f785676fSDimitry Andric return PPC::PRED_LT;
264f785676fSDimitry Andric
2653ca95b02SDimitry Andric case CmpInst::FCMP_ULE:
266f785676fSDimitry Andric case CmpInst::ICMP_ULE:
267f785676fSDimitry Andric case CmpInst::ICMP_SLE:
268f785676fSDimitry Andric return PPC::PRED_LE;
269f785676fSDimitry Andric
2703ca95b02SDimitry Andric case CmpInst::FCMP_UNE:
271f785676fSDimitry Andric case CmpInst::ICMP_NE:
272f785676fSDimitry Andric return PPC::PRED_NE;
273f785676fSDimitry Andric
274f785676fSDimitry Andric case CmpInst::FCMP_ORD:
275f785676fSDimitry Andric return PPC::PRED_NU;
276f785676fSDimitry Andric
277f785676fSDimitry Andric case CmpInst::FCMP_UNO:
278f785676fSDimitry Andric return PPC::PRED_UN;
279f785676fSDimitry Andric }
280f785676fSDimitry Andric }
281f785676fSDimitry Andric
282f785676fSDimitry Andric // Determine whether the type Ty is simple enough to be handled by
283f785676fSDimitry Andric // fast-isel, and return its equivalent machine type in VT.
284f785676fSDimitry Andric // FIXME: Copied directly from ARM -- factor into base class?
isTypeLegal(Type * Ty,MVT & VT)285f785676fSDimitry Andric bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
286875ed548SDimitry Andric EVT Evt = TLI.getValueType(DL, Ty, true);
287f785676fSDimitry Andric
288f785676fSDimitry Andric // Only handle simple types.
289f785676fSDimitry Andric if (Evt == MVT::Other || !Evt.isSimple()) return false;
290f785676fSDimitry Andric VT = Evt.getSimpleVT();
291f785676fSDimitry Andric
292f785676fSDimitry Andric // Handle all legal types, i.e. a register that will directly hold this
293f785676fSDimitry Andric // value.
294f785676fSDimitry Andric return TLI.isTypeLegal(VT);
295f785676fSDimitry Andric }
296f785676fSDimitry Andric
297f785676fSDimitry Andric // Determine whether the type Ty is simple enough to be handled by
298f785676fSDimitry Andric // fast-isel as a load target, and return its equivalent machine type in VT.
isLoadTypeLegal(Type * Ty,MVT & VT)299f785676fSDimitry Andric bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
300f785676fSDimitry Andric if (isTypeLegal(Ty, VT)) return true;
301f785676fSDimitry Andric
302f785676fSDimitry Andric // If this is a type than can be sign or zero-extended to a basic operation
303f785676fSDimitry Andric // go ahead and accept it now.
304f785676fSDimitry Andric if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
305f785676fSDimitry Andric return true;
306f785676fSDimitry Andric }
307f785676fSDimitry Andric
308f785676fSDimitry Andric return false;
309f785676fSDimitry Andric }
310f785676fSDimitry Andric
isValueAvailable(const Value * V) const311ff0cc061SDimitry Andric bool PPCFastISel::isValueAvailable(const Value *V) const {
312ff0cc061SDimitry Andric if (!isa<Instruction>(V))
313ff0cc061SDimitry Andric return true;
314ff0cc061SDimitry Andric
315ff0cc061SDimitry Andric const auto *I = cast<Instruction>(V);
3167d523365SDimitry Andric return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
317ff0cc061SDimitry Andric }
318ff0cc061SDimitry Andric
319f785676fSDimitry Andric // Given a value Obj, create an Address object Addr that represents its
320f785676fSDimitry Andric // address. Return false if we can't handle it.
PPCComputeAddress(const Value * Obj,Address & Addr)321f785676fSDimitry Andric bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
32291bc56edSDimitry Andric const User *U = nullptr;
323f785676fSDimitry Andric unsigned Opcode = Instruction::UserOp1;
324f785676fSDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
325f785676fSDimitry Andric // Don't walk into other basic blocks unless the object is an alloca from
326f785676fSDimitry Andric // another block, otherwise it may not have a virtual register assigned.
327f785676fSDimitry Andric if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
328f785676fSDimitry Andric FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
329f785676fSDimitry Andric Opcode = I->getOpcode();
330f785676fSDimitry Andric U = I;
331f785676fSDimitry Andric }
332f785676fSDimitry Andric } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
333f785676fSDimitry Andric Opcode = C->getOpcode();
334f785676fSDimitry Andric U = C;
335f785676fSDimitry Andric }
336f785676fSDimitry Andric
337f785676fSDimitry Andric switch (Opcode) {
338f785676fSDimitry Andric default:
339f785676fSDimitry Andric break;
340f785676fSDimitry Andric case Instruction::BitCast:
341f785676fSDimitry Andric // Look through bitcasts.
342f785676fSDimitry Andric return PPCComputeAddress(U->getOperand(0), Addr);
343f785676fSDimitry Andric case Instruction::IntToPtr:
344f785676fSDimitry Andric // Look past no-op inttoptrs.
345875ed548SDimitry Andric if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
346875ed548SDimitry Andric TLI.getPointerTy(DL))
347f785676fSDimitry Andric return PPCComputeAddress(U->getOperand(0), Addr);
348f785676fSDimitry Andric break;
349f785676fSDimitry Andric case Instruction::PtrToInt:
350f785676fSDimitry Andric // Look past no-op ptrtoints.
351875ed548SDimitry Andric if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
352f785676fSDimitry Andric return PPCComputeAddress(U->getOperand(0), Addr);
353f785676fSDimitry Andric break;
354f785676fSDimitry Andric case Instruction::GetElementPtr: {
355f785676fSDimitry Andric Address SavedAddr = Addr;
356f785676fSDimitry Andric long TmpOffset = Addr.Offset;
357f785676fSDimitry Andric
358f785676fSDimitry Andric // Iterate through the GEP folding the constants into offsets where
359f785676fSDimitry Andric // we can.
360f785676fSDimitry Andric gep_type_iterator GTI = gep_type_begin(U);
361f785676fSDimitry Andric for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
362f785676fSDimitry Andric II != IE; ++II, ++GTI) {
363f785676fSDimitry Andric const Value *Op = *II;
364d88c1a5aSDimitry Andric if (StructType *STy = GTI.getStructTypeOrNull()) {
36591bc56edSDimitry Andric const StructLayout *SL = DL.getStructLayout(STy);
366f785676fSDimitry Andric unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
367f785676fSDimitry Andric TmpOffset += SL->getElementOffset(Idx);
368f785676fSDimitry Andric } else {
36991bc56edSDimitry Andric uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
370f785676fSDimitry Andric for (;;) {
371f785676fSDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
372f785676fSDimitry Andric // Constant-offset addressing.
373f785676fSDimitry Andric TmpOffset += CI->getSExtValue() * S;
374f785676fSDimitry Andric break;
375f785676fSDimitry Andric }
376f785676fSDimitry Andric if (canFoldAddIntoGEP(U, Op)) {
377f785676fSDimitry Andric // A compatible add with a constant operand. Fold the constant.
378f785676fSDimitry Andric ConstantInt *CI =
379f785676fSDimitry Andric cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
380f785676fSDimitry Andric TmpOffset += CI->getSExtValue() * S;
381f785676fSDimitry Andric // Iterate on the other operand.
382f785676fSDimitry Andric Op = cast<AddOperator>(Op)->getOperand(0);
383f785676fSDimitry Andric continue;
384f785676fSDimitry Andric }
385f785676fSDimitry Andric // Unsupported
386f785676fSDimitry Andric goto unsupported_gep;
387f785676fSDimitry Andric }
388f785676fSDimitry Andric }
389f785676fSDimitry Andric }
390f785676fSDimitry Andric
391f785676fSDimitry Andric // Try to grab the base operand now.
392f785676fSDimitry Andric Addr.Offset = TmpOffset;
393f785676fSDimitry Andric if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
394f785676fSDimitry Andric
395f785676fSDimitry Andric // We failed, restore everything and try the other options.
396f785676fSDimitry Andric Addr = SavedAddr;
397f785676fSDimitry Andric
398f785676fSDimitry Andric unsupported_gep:
399f785676fSDimitry Andric break;
400f785676fSDimitry Andric }
401f785676fSDimitry Andric case Instruction::Alloca: {
402f785676fSDimitry Andric const AllocaInst *AI = cast<AllocaInst>(Obj);
403f785676fSDimitry Andric DenseMap<const AllocaInst*, int>::iterator SI =
404f785676fSDimitry Andric FuncInfo.StaticAllocaMap.find(AI);
405f785676fSDimitry Andric if (SI != FuncInfo.StaticAllocaMap.end()) {
406f785676fSDimitry Andric Addr.BaseType = Address::FrameIndexBase;
407f785676fSDimitry Andric Addr.Base.FI = SI->second;
408f785676fSDimitry Andric return true;
409f785676fSDimitry Andric }
410f785676fSDimitry Andric break;
411f785676fSDimitry Andric }
412f785676fSDimitry Andric }
413f785676fSDimitry Andric
414f785676fSDimitry Andric // FIXME: References to parameters fall through to the behavior
415f785676fSDimitry Andric // below. They should be able to reference a frame index since
416f785676fSDimitry Andric // they are stored to the stack, so we can get "ld rx, offset(r1)"
417f785676fSDimitry Andric // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
418f785676fSDimitry Andric // just contain the parameter. Try to handle this with a FI.
419f785676fSDimitry Andric
420f785676fSDimitry Andric // Try to get this in a register if nothing else has worked.
421f785676fSDimitry Andric if (Addr.Base.Reg == 0)
422f785676fSDimitry Andric Addr.Base.Reg = getRegForValue(Obj);
423f785676fSDimitry Andric
424f785676fSDimitry Andric // Prevent assignment of base register to X0, which is inappropriate
425f785676fSDimitry Andric // for loads and stores alike.
426f785676fSDimitry Andric if (Addr.Base.Reg != 0)
427f785676fSDimitry Andric MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
428f785676fSDimitry Andric
429f785676fSDimitry Andric return Addr.Base.Reg != 0;
430f785676fSDimitry Andric }
431f785676fSDimitry Andric
432f785676fSDimitry Andric // Fix up some addresses that can't be used directly. For example, if
433f785676fSDimitry Andric // an offset won't fit in an instruction field, we may need to move it
434f785676fSDimitry Andric // into an index register.
PPCSimplifyAddress(Address & Addr,bool & UseOffset,unsigned & IndexReg)4353ca95b02SDimitry Andric void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
436f785676fSDimitry Andric unsigned &IndexReg) {
437f785676fSDimitry Andric
438f785676fSDimitry Andric // Check whether the offset fits in the instruction field.
439f785676fSDimitry Andric if (!isInt<16>(Addr.Offset))
440f785676fSDimitry Andric UseOffset = false;
441f785676fSDimitry Andric
442f785676fSDimitry Andric // If this is a stack pointer and the offset needs to be simplified then
443f785676fSDimitry Andric // put the alloca address into a register, set the base type back to
444f785676fSDimitry Andric // register and continue. This should almost never happen.
445f785676fSDimitry Andric if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
446f785676fSDimitry Andric unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
44791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
448f785676fSDimitry Andric ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
449f785676fSDimitry Andric Addr.Base.Reg = ResultReg;
450f785676fSDimitry Andric Addr.BaseType = Address::RegBase;
451f785676fSDimitry Andric }
452f785676fSDimitry Andric
453f785676fSDimitry Andric if (!UseOffset) {
4543ca95b02SDimitry Andric IntegerType *OffsetTy = Type::getInt64Ty(*Context);
455f785676fSDimitry Andric const ConstantInt *Offset =
456f785676fSDimitry Andric ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
457f785676fSDimitry Andric IndexReg = PPCMaterializeInt(Offset, MVT::i64);
458f785676fSDimitry Andric assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
459f785676fSDimitry Andric }
460f785676fSDimitry Andric }
461f785676fSDimitry Andric
462f785676fSDimitry Andric // Emit a load instruction if possible, returning true if we succeeded,
463f785676fSDimitry Andric // otherwise false. See commentary below for how the register class of
464f785676fSDimitry Andric // the load is determined.
PPCEmitLoad(MVT VT,unsigned & ResultReg,Address & Addr,const TargetRegisterClass * RC,bool IsZExt,unsigned FP64LoadOpc)465f785676fSDimitry Andric bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
466f785676fSDimitry Andric const TargetRegisterClass *RC,
467f785676fSDimitry Andric bool IsZExt, unsigned FP64LoadOpc) {
468f785676fSDimitry Andric unsigned Opc;
469f785676fSDimitry Andric bool UseOffset = true;
4704ba319b5SDimitry Andric bool HasSPE = PPCSubTarget->hasSPE();
471f785676fSDimitry Andric
472f785676fSDimitry Andric // If ResultReg is given, it determines the register class of the load.
473f785676fSDimitry Andric // Otherwise, RC is the register class to use. If the result of the
474f785676fSDimitry Andric // load isn't anticipated in this block, both may be zero, in which
475f785676fSDimitry Andric // case we must make a conservative guess. In particular, don't assign
476f785676fSDimitry Andric // R0 or X0 to the result register, as the result may be used in a load,
477f785676fSDimitry Andric // store, add-immediate, or isel that won't permit this. (Though
478f785676fSDimitry Andric // perhaps the spill and reload of live-exit values would handle this?)
479f785676fSDimitry Andric const TargetRegisterClass *UseRC =
480f785676fSDimitry Andric (ResultReg ? MRI.getRegClass(ResultReg) :
481f785676fSDimitry Andric (RC ? RC :
4824ba319b5SDimitry Andric (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
4834ba319b5SDimitry Andric (VT == MVT::f32 ? (HasSPE ? &PPC::SPE4RCRegClass : &PPC::F4RCRegClass) :
484f785676fSDimitry Andric (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
485f785676fSDimitry Andric &PPC::GPRC_and_GPRC_NOR0RegClass)))));
486f785676fSDimitry Andric
487f785676fSDimitry Andric bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
488f785676fSDimitry Andric
489f785676fSDimitry Andric switch (VT.SimpleTy) {
490f785676fSDimitry Andric default: // e.g., vector types not handled
491f785676fSDimitry Andric return false;
492f785676fSDimitry Andric case MVT::i8:
493f785676fSDimitry Andric Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
494f785676fSDimitry Andric break;
495f785676fSDimitry Andric case MVT::i16:
496d88c1a5aSDimitry Andric Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
497d88c1a5aSDimitry Andric : (Is32BitInt ? PPC::LHA : PPC::LHA8));
498f785676fSDimitry Andric break;
499f785676fSDimitry Andric case MVT::i32:
500d88c1a5aSDimitry Andric Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
501d88c1a5aSDimitry Andric : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
502f785676fSDimitry Andric if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
503f785676fSDimitry Andric UseOffset = false;
504f785676fSDimitry Andric break;
505f785676fSDimitry Andric case MVT::i64:
506f785676fSDimitry Andric Opc = PPC::LD;
507f785676fSDimitry Andric assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
508f785676fSDimitry Andric "64-bit load with 32-bit target??");
509f785676fSDimitry Andric UseOffset = ((Addr.Offset & 3) == 0);
510f785676fSDimitry Andric break;
511f785676fSDimitry Andric case MVT::f32:
5124ba319b5SDimitry Andric Opc = PPCSubTarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
513f785676fSDimitry Andric break;
514f785676fSDimitry Andric case MVT::f64:
515f785676fSDimitry Andric Opc = FP64LoadOpc;
516f785676fSDimitry Andric break;
517f785676fSDimitry Andric }
518f785676fSDimitry Andric
519f785676fSDimitry Andric // If necessary, materialize the offset into a register and use
520f785676fSDimitry Andric // the indexed form. Also handle stack pointers with special needs.
521f785676fSDimitry Andric unsigned IndexReg = 0;
5223ca95b02SDimitry Andric PPCSimplifyAddress(Addr, UseOffset, IndexReg);
52339d628a0SDimitry Andric
52439d628a0SDimitry Andric // If this is a potential VSX load with an offset of 0, a VSX indexed load can
52539d628a0SDimitry Andric // be used.
526d88c1a5aSDimitry Andric bool IsVSSRC = isVSSRCRegClass(UseRC);
527d88c1a5aSDimitry Andric bool IsVSFRC = isVSFRCRegClass(UseRC);
52897bc6c73SDimitry Andric bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
529d88c1a5aSDimitry Andric bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
53097bc6c73SDimitry Andric if ((Is32VSXLoad || Is64VSXLoad) &&
53139d628a0SDimitry Andric (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
53239d628a0SDimitry Andric (Addr.Offset == 0)) {
53339d628a0SDimitry Andric UseOffset = false;
53439d628a0SDimitry Andric }
53539d628a0SDimitry Andric
536f785676fSDimitry Andric if (ResultReg == 0)
537f785676fSDimitry Andric ResultReg = createResultReg(UseRC);
538f785676fSDimitry Andric
539f785676fSDimitry Andric // Note: If we still have a frame index here, we know the offset is
540f785676fSDimitry Andric // in range, as otherwise PPCSimplifyAddress would have converted it
541f785676fSDimitry Andric // into a RegBase.
542f785676fSDimitry Andric if (Addr.BaseType == Address::FrameIndexBase) {
54339d628a0SDimitry Andric // VSX only provides an indexed load.
54497bc6c73SDimitry Andric if (Is32VSXLoad || Is64VSXLoad) return false;
545f785676fSDimitry Andric
5467d523365SDimitry Andric MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
5477d523365SDimitry Andric MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
5487d523365SDimitry Andric Addr.Offset),
549f785676fSDimitry Andric MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
550f785676fSDimitry Andric MFI.getObjectAlignment(Addr.Base.FI));
551f785676fSDimitry Andric
55291bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
553f785676fSDimitry Andric .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
554f785676fSDimitry Andric
555f785676fSDimitry Andric // Base reg with offset in range.
556f785676fSDimitry Andric } else if (UseOffset) {
55739d628a0SDimitry Andric // VSX only provides an indexed load.
55897bc6c73SDimitry Andric if (Is32VSXLoad || Is64VSXLoad) return false;
559f785676fSDimitry Andric
56091bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
561f785676fSDimitry Andric .addImm(Addr.Offset).addReg(Addr.Base.Reg);
562f785676fSDimitry Andric
563f785676fSDimitry Andric // Indexed form.
564f785676fSDimitry Andric } else {
565f785676fSDimitry Andric // Get the RR opcode corresponding to the RI one. FIXME: It would be
566f785676fSDimitry Andric // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
567f785676fSDimitry Andric // is hard to get at.
568f785676fSDimitry Andric switch (Opc) {
569f785676fSDimitry Andric default: llvm_unreachable("Unexpected opcode!");
570f785676fSDimitry Andric case PPC::LBZ: Opc = PPC::LBZX; break;
571f785676fSDimitry Andric case PPC::LBZ8: Opc = PPC::LBZX8; break;
572f785676fSDimitry Andric case PPC::LHZ: Opc = PPC::LHZX; break;
573f785676fSDimitry Andric case PPC::LHZ8: Opc = PPC::LHZX8; break;
574f785676fSDimitry Andric case PPC::LHA: Opc = PPC::LHAX; break;
575f785676fSDimitry Andric case PPC::LHA8: Opc = PPC::LHAX8; break;
576f785676fSDimitry Andric case PPC::LWZ: Opc = PPC::LWZX; break;
577f785676fSDimitry Andric case PPC::LWZ8: Opc = PPC::LWZX8; break;
578f785676fSDimitry Andric case PPC::LWA: Opc = PPC::LWAX; break;
579f785676fSDimitry Andric case PPC::LWA_32: Opc = PPC::LWAX_32; break;
580f785676fSDimitry Andric case PPC::LD: Opc = PPC::LDX; break;
58197bc6c73SDimitry Andric case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
58239d628a0SDimitry Andric case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
5834ba319b5SDimitry Andric case PPC::EVLDD: Opc = PPC::EVLDDX; break;
5844ba319b5SDimitry Andric case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
585f785676fSDimitry Andric }
586d88c1a5aSDimitry Andric
587d88c1a5aSDimitry Andric auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
588d88c1a5aSDimitry Andric ResultReg);
589d88c1a5aSDimitry Andric
590d88c1a5aSDimitry Andric // If we have an index register defined we use it in the store inst,
591d88c1a5aSDimitry Andric // otherwise we use X0 as base as it makes the vector instructions to
592d88c1a5aSDimitry Andric // use zero in the computation of the effective address regardless the
593d88c1a5aSDimitry Andric // content of the register.
594d88c1a5aSDimitry Andric if (IndexReg)
595d88c1a5aSDimitry Andric MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
596d88c1a5aSDimitry Andric else
597d88c1a5aSDimitry Andric MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
598f785676fSDimitry Andric }
599f785676fSDimitry Andric
600f785676fSDimitry Andric return true;
601f785676fSDimitry Andric }
602f785676fSDimitry Andric
603f785676fSDimitry Andric // Attempt to fast-select a load instruction.
SelectLoad(const Instruction * I)604f785676fSDimitry Andric bool PPCFastISel::SelectLoad(const Instruction *I) {
605f785676fSDimitry Andric // FIXME: No atomic loads are supported.
606f785676fSDimitry Andric if (cast<LoadInst>(I)->isAtomic())
607f785676fSDimitry Andric return false;
608f785676fSDimitry Andric
609f785676fSDimitry Andric // Verify we have a legal type before going any further.
610f785676fSDimitry Andric MVT VT;
611f785676fSDimitry Andric if (!isLoadTypeLegal(I->getType(), VT))
612f785676fSDimitry Andric return false;
613f785676fSDimitry Andric
614f785676fSDimitry Andric // See if we can handle this address.
615f785676fSDimitry Andric Address Addr;
616f785676fSDimitry Andric if (!PPCComputeAddress(I->getOperand(0), Addr))
617f785676fSDimitry Andric return false;
618f785676fSDimitry Andric
619f785676fSDimitry Andric // Look at the currently assigned register for this instruction
620f785676fSDimitry Andric // to determine the required register class. This is necessary
621f785676fSDimitry Andric // to constrain RA from using R0/X0 when this is not legal.
622f785676fSDimitry Andric unsigned AssignedReg = FuncInfo.ValueMap[I];
623f785676fSDimitry Andric const TargetRegisterClass *RC =
62491bc56edSDimitry Andric AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
625f785676fSDimitry Andric
626f785676fSDimitry Andric unsigned ResultReg = 0;
6274ba319b5SDimitry Andric if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
6284ba319b5SDimitry Andric PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
629f785676fSDimitry Andric return false;
63039d628a0SDimitry Andric updateValueMap(I, ResultReg);
631f785676fSDimitry Andric return true;
632f785676fSDimitry Andric }
633f785676fSDimitry Andric
634f785676fSDimitry Andric // Emit a store instruction to store SrcReg at Addr.
PPCEmitStore(MVT VT,unsigned SrcReg,Address & Addr)635f785676fSDimitry Andric bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
636f785676fSDimitry Andric assert(SrcReg && "Nothing to store!");
637f785676fSDimitry Andric unsigned Opc;
638f785676fSDimitry Andric bool UseOffset = true;
639f785676fSDimitry Andric
640f785676fSDimitry Andric const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
641f785676fSDimitry Andric bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
642f785676fSDimitry Andric
643f785676fSDimitry Andric switch (VT.SimpleTy) {
644f785676fSDimitry Andric default: // e.g., vector types not handled
645f785676fSDimitry Andric return false;
646f785676fSDimitry Andric case MVT::i8:
647f785676fSDimitry Andric Opc = Is32BitInt ? PPC::STB : PPC::STB8;
648f785676fSDimitry Andric break;
649f785676fSDimitry Andric case MVT::i16:
650f785676fSDimitry Andric Opc = Is32BitInt ? PPC::STH : PPC::STH8;
651f785676fSDimitry Andric break;
652f785676fSDimitry Andric case MVT::i32:
653f785676fSDimitry Andric assert(Is32BitInt && "Not GPRC for i32??");
654f785676fSDimitry Andric Opc = PPC::STW;
655f785676fSDimitry Andric break;
656f785676fSDimitry Andric case MVT::i64:
657f785676fSDimitry Andric Opc = PPC::STD;
658f785676fSDimitry Andric UseOffset = ((Addr.Offset & 3) == 0);
659f785676fSDimitry Andric break;
660f785676fSDimitry Andric case MVT::f32:
6614ba319b5SDimitry Andric Opc = PPCSubTarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
662f785676fSDimitry Andric break;
663f785676fSDimitry Andric case MVT::f64:
6644ba319b5SDimitry Andric Opc = PPCSubTarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
665f785676fSDimitry Andric break;
666f785676fSDimitry Andric }
667f785676fSDimitry Andric
668f785676fSDimitry Andric // If necessary, materialize the offset into a register and use
669f785676fSDimitry Andric // the indexed form. Also handle stack pointers with special needs.
670f785676fSDimitry Andric unsigned IndexReg = 0;
6713ca95b02SDimitry Andric PPCSimplifyAddress(Addr, UseOffset, IndexReg);
672f785676fSDimitry Andric
67339d628a0SDimitry Andric // If this is a potential VSX store with an offset of 0, a VSX indexed store
67439d628a0SDimitry Andric // can be used.
675d88c1a5aSDimitry Andric bool IsVSSRC = isVSSRCRegClass(RC);
676d88c1a5aSDimitry Andric bool IsVSFRC = isVSFRCRegClass(RC);
67797bc6c73SDimitry Andric bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
67897bc6c73SDimitry Andric bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
67997bc6c73SDimitry Andric if ((Is32VSXStore || Is64VSXStore) &&
68039d628a0SDimitry Andric (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
68139d628a0SDimitry Andric (Addr.Offset == 0)) {
68239d628a0SDimitry Andric UseOffset = false;
68339d628a0SDimitry Andric }
68439d628a0SDimitry Andric
685f785676fSDimitry Andric // Note: If we still have a frame index here, we know the offset is
686f785676fSDimitry Andric // in range, as otherwise PPCSimplifyAddress would have converted it
687f785676fSDimitry Andric // into a RegBase.
688f785676fSDimitry Andric if (Addr.BaseType == Address::FrameIndexBase) {
68939d628a0SDimitry Andric // VSX only provides an indexed store.
69097bc6c73SDimitry Andric if (Is32VSXStore || Is64VSXStore) return false;
69139d628a0SDimitry Andric
6927d523365SDimitry Andric MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
6937d523365SDimitry Andric MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
6947d523365SDimitry Andric Addr.Offset),
695f785676fSDimitry Andric MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
696f785676fSDimitry Andric MFI.getObjectAlignment(Addr.Base.FI));
697f785676fSDimitry Andric
69891bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
69991bc56edSDimitry Andric .addReg(SrcReg)
70091bc56edSDimitry Andric .addImm(Addr.Offset)
70191bc56edSDimitry Andric .addFrameIndex(Addr.Base.FI)
70291bc56edSDimitry Andric .addMemOperand(MMO);
703f785676fSDimitry Andric
704f785676fSDimitry Andric // Base reg with offset in range.
70539d628a0SDimitry Andric } else if (UseOffset) {
70639d628a0SDimitry Andric // VSX only provides an indexed store.
707d88c1a5aSDimitry Andric if (Is32VSXStore || Is64VSXStore)
708d88c1a5aSDimitry Andric return false;
70939d628a0SDimitry Andric
71091bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
711f785676fSDimitry Andric .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
712f785676fSDimitry Andric
713f785676fSDimitry Andric // Indexed form.
71439d628a0SDimitry Andric } else {
715f785676fSDimitry Andric // Get the RR opcode corresponding to the RI one. FIXME: It would be
716f785676fSDimitry Andric // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
717f785676fSDimitry Andric // is hard to get at.
718f785676fSDimitry Andric switch (Opc) {
719f785676fSDimitry Andric default: llvm_unreachable("Unexpected opcode!");
720f785676fSDimitry Andric case PPC::STB: Opc = PPC::STBX; break;
721f785676fSDimitry Andric case PPC::STH : Opc = PPC::STHX; break;
722f785676fSDimitry Andric case PPC::STW : Opc = PPC::STWX; break;
723f785676fSDimitry Andric case PPC::STB8: Opc = PPC::STBX8; break;
724f785676fSDimitry Andric case PPC::STH8: Opc = PPC::STHX8; break;
725f785676fSDimitry Andric case PPC::STW8: Opc = PPC::STWX8; break;
726f785676fSDimitry Andric case PPC::STD: Opc = PPC::STDX; break;
72797bc6c73SDimitry Andric case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
72839d628a0SDimitry Andric case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
7294ba319b5SDimitry Andric case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
7304ba319b5SDimitry Andric case PPC::SPESTW: Opc = PPC::SPESTWX; break;
731f785676fSDimitry Andric }
732ff0cc061SDimitry Andric
733ff0cc061SDimitry Andric auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
734ff0cc061SDimitry Andric .addReg(SrcReg);
735ff0cc061SDimitry Andric
736ff0cc061SDimitry Andric // If we have an index register defined we use it in the store inst,
737ff0cc061SDimitry Andric // otherwise we use X0 as base as it makes the vector instructions to
738ff0cc061SDimitry Andric // use zero in the computation of the effective address regardless the
739ff0cc061SDimitry Andric // content of the register.
740ff0cc061SDimitry Andric if (IndexReg)
741ff0cc061SDimitry Andric MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
742ff0cc061SDimitry Andric else
743ff0cc061SDimitry Andric MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
744f785676fSDimitry Andric }
745f785676fSDimitry Andric
746f785676fSDimitry Andric return true;
747f785676fSDimitry Andric }
748f785676fSDimitry Andric
749f785676fSDimitry Andric // Attempt to fast-select a store instruction.
SelectStore(const Instruction * I)750f785676fSDimitry Andric bool PPCFastISel::SelectStore(const Instruction *I) {
751f785676fSDimitry Andric Value *Op0 = I->getOperand(0);
752f785676fSDimitry Andric unsigned SrcReg = 0;
753f785676fSDimitry Andric
754f785676fSDimitry Andric // FIXME: No atomics loads are supported.
755f785676fSDimitry Andric if (cast<StoreInst>(I)->isAtomic())
756f785676fSDimitry Andric return false;
757f785676fSDimitry Andric
758f785676fSDimitry Andric // Verify we have a legal type before going any further.
759f785676fSDimitry Andric MVT VT;
760f785676fSDimitry Andric if (!isLoadTypeLegal(Op0->getType(), VT))
761f785676fSDimitry Andric return false;
762f785676fSDimitry Andric
763f785676fSDimitry Andric // Get the value to be stored into a register.
764f785676fSDimitry Andric SrcReg = getRegForValue(Op0);
765f785676fSDimitry Andric if (SrcReg == 0)
766f785676fSDimitry Andric return false;
767f785676fSDimitry Andric
768f785676fSDimitry Andric // See if we can handle this address.
769f785676fSDimitry Andric Address Addr;
770f785676fSDimitry Andric if (!PPCComputeAddress(I->getOperand(1), Addr))
771f785676fSDimitry Andric return false;
772f785676fSDimitry Andric
773f785676fSDimitry Andric if (!PPCEmitStore(VT, SrcReg, Addr))
774f785676fSDimitry Andric return false;
775f785676fSDimitry Andric
776f785676fSDimitry Andric return true;
777f785676fSDimitry Andric }
778f785676fSDimitry Andric
779f785676fSDimitry Andric // Attempt to fast-select a branch instruction.
SelectBranch(const Instruction * I)780f785676fSDimitry Andric bool PPCFastISel::SelectBranch(const Instruction *I) {
781f785676fSDimitry Andric const BranchInst *BI = cast<BranchInst>(I);
782f785676fSDimitry Andric MachineBasicBlock *BrBB = FuncInfo.MBB;
783f785676fSDimitry Andric MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
784f785676fSDimitry Andric MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
785f785676fSDimitry Andric
786f785676fSDimitry Andric // For now, just try the simplest case where it's fed by a compare.
787f785676fSDimitry Andric if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
788ff0cc061SDimitry Andric if (isValueAvailable(CI)) {
789f785676fSDimitry Andric Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
790f785676fSDimitry Andric if (!OptPPCPred)
791f785676fSDimitry Andric return false;
792f785676fSDimitry Andric
793f785676fSDimitry Andric PPC::Predicate PPCPred = OptPPCPred.getValue();
794f785676fSDimitry Andric
795f785676fSDimitry Andric // Take advantage of fall-through opportunities.
796f785676fSDimitry Andric if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
797f785676fSDimitry Andric std::swap(TBB, FBB);
798f785676fSDimitry Andric PPCPred = PPC::InvertPredicate(PPCPred);
799f785676fSDimitry Andric }
800f785676fSDimitry Andric
801f785676fSDimitry Andric unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
802f785676fSDimitry Andric
803f785676fSDimitry Andric if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
8044ba319b5SDimitry Andric CondReg, PPCPred))
805f785676fSDimitry Andric return false;
806f785676fSDimitry Andric
80791bc56edSDimitry Andric BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
8084ba319b5SDimitry Andric .addImm(PPCSubTarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
8094ba319b5SDimitry Andric .addReg(CondReg).addMBB(TBB);
8107d523365SDimitry Andric finishCondBranch(BI->getParent(), TBB, FBB);
811f785676fSDimitry Andric return true;
812ff0cc061SDimitry Andric }
813f785676fSDimitry Andric } else if (const ConstantInt *CI =
814f785676fSDimitry Andric dyn_cast<ConstantInt>(BI->getCondition())) {
815f785676fSDimitry Andric uint64_t Imm = CI->getZExtValue();
816f785676fSDimitry Andric MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
81739d628a0SDimitry Andric fastEmitBranch(Target, DbgLoc);
818f785676fSDimitry Andric return true;
819f785676fSDimitry Andric }
820f785676fSDimitry Andric
821f785676fSDimitry Andric // FIXME: ARM looks for a case where the block containing the compare
822f785676fSDimitry Andric // has been split from the block containing the branch. If this happens,
823f785676fSDimitry Andric // there is a vreg available containing the result of the compare. I'm
824f785676fSDimitry Andric // not sure we can do much, as we've lost the predicate information with
825f785676fSDimitry Andric // the compare instruction -- we have a 4-bit CR but don't know which bit
826f785676fSDimitry Andric // to test here.
827f785676fSDimitry Andric return false;
828f785676fSDimitry Andric }
829f785676fSDimitry Andric
830f785676fSDimitry Andric // Attempt to emit a compare of the two source values. Signed and unsigned
831f785676fSDimitry Andric // comparisons are supported. Return false if we can't handle it.
PPCEmitCmp(const Value * SrcValue1,const Value * SrcValue2,bool IsZExt,unsigned DestReg,const PPC::Predicate Pred)832f785676fSDimitry Andric bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
8334ba319b5SDimitry Andric bool IsZExt, unsigned DestReg,
8344ba319b5SDimitry Andric const PPC::Predicate Pred) {
835f785676fSDimitry Andric Type *Ty = SrcValue1->getType();
836875ed548SDimitry Andric EVT SrcEVT = TLI.getValueType(DL, Ty, true);
837f785676fSDimitry Andric if (!SrcEVT.isSimple())
838f785676fSDimitry Andric return false;
839f785676fSDimitry Andric MVT SrcVT = SrcEVT.getSimpleVT();
840f785676fSDimitry Andric
84191bc56edSDimitry Andric if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
84291bc56edSDimitry Andric return false;
84391bc56edSDimitry Andric
844f785676fSDimitry Andric // See if operand 2 is an immediate encodeable in the compare.
845f785676fSDimitry Andric // FIXME: Operands are not in canonical order at -O0, so an immediate
846f785676fSDimitry Andric // operand in position 1 is a lost opportunity for now. We are
847f785676fSDimitry Andric // similar to ARM in this regard.
848f785676fSDimitry Andric long Imm = 0;
849f785676fSDimitry Andric bool UseImm = false;
8504ba319b5SDimitry Andric const bool HasSPE = PPCSubTarget->hasSPE();
851f785676fSDimitry Andric
852f785676fSDimitry Andric // Only 16-bit integer constants can be represented in compares for
853f785676fSDimitry Andric // PowerPC. Others will be materialized into a register.
854f785676fSDimitry Andric if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
855f785676fSDimitry Andric if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
856f785676fSDimitry Andric SrcVT == MVT::i8 || SrcVT == MVT::i1) {
857f785676fSDimitry Andric const APInt &CIVal = ConstInt->getValue();
858f785676fSDimitry Andric Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
859f785676fSDimitry Andric if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
860f785676fSDimitry Andric UseImm = true;
861f785676fSDimitry Andric }
862f785676fSDimitry Andric }
863f785676fSDimitry Andric
864*b5893f02SDimitry Andric unsigned SrcReg1 = getRegForValue(SrcValue1);
865*b5893f02SDimitry Andric if (SrcReg1 == 0)
866*b5893f02SDimitry Andric return false;
867*b5893f02SDimitry Andric
868*b5893f02SDimitry Andric unsigned SrcReg2 = 0;
869*b5893f02SDimitry Andric if (!UseImm) {
870*b5893f02SDimitry Andric SrcReg2 = getRegForValue(SrcValue2);
871*b5893f02SDimitry Andric if (SrcReg2 == 0)
872*b5893f02SDimitry Andric return false;
873*b5893f02SDimitry Andric }
874*b5893f02SDimitry Andric
875f785676fSDimitry Andric unsigned CmpOpc;
876f785676fSDimitry Andric bool NeedsExt = false;
877*b5893f02SDimitry Andric auto RC = MRI.getRegClass(SrcReg1);
878f785676fSDimitry Andric switch (SrcVT.SimpleTy) {
879f785676fSDimitry Andric default: return false;
880f785676fSDimitry Andric case MVT::f32:
8814ba319b5SDimitry Andric if (HasSPE) {
8824ba319b5SDimitry Andric switch (Pred) {
8834ba319b5SDimitry Andric default: return false;
8844ba319b5SDimitry Andric case PPC::PRED_EQ:
8854ba319b5SDimitry Andric CmpOpc = PPC::EFSCMPEQ;
8864ba319b5SDimitry Andric break;
8874ba319b5SDimitry Andric case PPC::PRED_LT:
8884ba319b5SDimitry Andric CmpOpc = PPC::EFSCMPLT;
8894ba319b5SDimitry Andric break;
8904ba319b5SDimitry Andric case PPC::PRED_GT:
8914ba319b5SDimitry Andric CmpOpc = PPC::EFSCMPGT;
8924ba319b5SDimitry Andric break;
8934ba319b5SDimitry Andric }
894*b5893f02SDimitry Andric } else {
895f785676fSDimitry Andric CmpOpc = PPC::FCMPUS;
896*b5893f02SDimitry Andric if (isVSSRCRegClass(RC)) {
897*b5893f02SDimitry Andric unsigned TmpReg = createResultReg(&PPC::F4RCRegClass);
898*b5893f02SDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
899*b5893f02SDimitry Andric TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg1);
900*b5893f02SDimitry Andric SrcReg1 = TmpReg;
901*b5893f02SDimitry Andric }
902*b5893f02SDimitry Andric }
903f785676fSDimitry Andric break;
904f785676fSDimitry Andric case MVT::f64:
9054ba319b5SDimitry Andric if (HasSPE) {
9064ba319b5SDimitry Andric switch (Pred) {
9074ba319b5SDimitry Andric default: return false;
9084ba319b5SDimitry Andric case PPC::PRED_EQ:
9094ba319b5SDimitry Andric CmpOpc = PPC::EFDCMPEQ;
9104ba319b5SDimitry Andric break;
9114ba319b5SDimitry Andric case PPC::PRED_LT:
9124ba319b5SDimitry Andric CmpOpc = PPC::EFDCMPLT;
9134ba319b5SDimitry Andric break;
9144ba319b5SDimitry Andric case PPC::PRED_GT:
9154ba319b5SDimitry Andric CmpOpc = PPC::EFDCMPGT;
9164ba319b5SDimitry Andric break;
9174ba319b5SDimitry Andric }
918*b5893f02SDimitry Andric } else if (isVSFRCRegClass(RC)) {
919*b5893f02SDimitry Andric CmpOpc = PPC::XSCMPUDP;
920*b5893f02SDimitry Andric } else {
921f785676fSDimitry Andric CmpOpc = PPC::FCMPUD;
922*b5893f02SDimitry Andric }
923f785676fSDimitry Andric break;
924f785676fSDimitry Andric case MVT::i1:
925f785676fSDimitry Andric case MVT::i8:
926f785676fSDimitry Andric case MVT::i16:
927f785676fSDimitry Andric NeedsExt = true;
928*b5893f02SDimitry Andric LLVM_FALLTHROUGH;
929f785676fSDimitry Andric case MVT::i32:
930f785676fSDimitry Andric if (!UseImm)
931f785676fSDimitry Andric CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
932f785676fSDimitry Andric else
933f785676fSDimitry Andric CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
934f785676fSDimitry Andric break;
935f785676fSDimitry Andric case MVT::i64:
936f785676fSDimitry Andric if (!UseImm)
937f785676fSDimitry Andric CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
938f785676fSDimitry Andric else
939f785676fSDimitry Andric CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
940f785676fSDimitry Andric break;
941f785676fSDimitry Andric }
942f785676fSDimitry Andric
943f785676fSDimitry Andric if (NeedsExt) {
944f785676fSDimitry Andric unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
945f785676fSDimitry Andric if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
946f785676fSDimitry Andric return false;
947f785676fSDimitry Andric SrcReg1 = ExtReg;
948f785676fSDimitry Andric
949f785676fSDimitry Andric if (!UseImm) {
950f785676fSDimitry Andric unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
951f785676fSDimitry Andric if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
952f785676fSDimitry Andric return false;
953f785676fSDimitry Andric SrcReg2 = ExtReg;
954f785676fSDimitry Andric }
955f785676fSDimitry Andric }
956f785676fSDimitry Andric
957f785676fSDimitry Andric if (!UseImm)
95891bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
959f785676fSDimitry Andric .addReg(SrcReg1).addReg(SrcReg2);
960f785676fSDimitry Andric else
96191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
962f785676fSDimitry Andric .addReg(SrcReg1).addImm(Imm);
963f785676fSDimitry Andric
964f785676fSDimitry Andric return true;
965f785676fSDimitry Andric }
966f785676fSDimitry Andric
967f785676fSDimitry Andric // Attempt to fast-select a floating-point extend instruction.
SelectFPExt(const Instruction * I)968f785676fSDimitry Andric bool PPCFastISel::SelectFPExt(const Instruction *I) {
969f785676fSDimitry Andric Value *Src = I->getOperand(0);
970875ed548SDimitry Andric EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
971875ed548SDimitry Andric EVT DestVT = TLI.getValueType(DL, I->getType(), true);
972f785676fSDimitry Andric
973f785676fSDimitry Andric if (SrcVT != MVT::f32 || DestVT != MVT::f64)
974f785676fSDimitry Andric return false;
975f785676fSDimitry Andric
976f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
977f785676fSDimitry Andric if (!SrcReg)
978f785676fSDimitry Andric return false;
979f785676fSDimitry Andric
980f785676fSDimitry Andric // No code is generated for a FP extend.
98139d628a0SDimitry Andric updateValueMap(I, SrcReg);
982f785676fSDimitry Andric return true;
983f785676fSDimitry Andric }
984f785676fSDimitry Andric
985f785676fSDimitry Andric // Attempt to fast-select a floating-point truncate instruction.
SelectFPTrunc(const Instruction * I)986f785676fSDimitry Andric bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
987f785676fSDimitry Andric Value *Src = I->getOperand(0);
988875ed548SDimitry Andric EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
989875ed548SDimitry Andric EVT DestVT = TLI.getValueType(DL, I->getType(), true);
990f785676fSDimitry Andric
991f785676fSDimitry Andric if (SrcVT != MVT::f64 || DestVT != MVT::f32)
992f785676fSDimitry Andric return false;
993f785676fSDimitry Andric
994f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
995f785676fSDimitry Andric if (!SrcReg)
996f785676fSDimitry Andric return false;
997f785676fSDimitry Andric
998f785676fSDimitry Andric // Round the result to single precision.
9994ba319b5SDimitry Andric unsigned DestReg;
10004ba319b5SDimitry Andric
10014ba319b5SDimitry Andric if (PPCSubTarget->hasSPE()) {
10024ba319b5SDimitry Andric DestReg = createResultReg(&PPC::SPE4RCRegClass);
10034ba319b5SDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
10044ba319b5SDimitry Andric TII.get(PPC::EFSCFD), DestReg)
1005f785676fSDimitry Andric .addReg(SrcReg);
10064ba319b5SDimitry Andric } else {
10074ba319b5SDimitry Andric DestReg = createResultReg(&PPC::F4RCRegClass);
10084ba319b5SDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
10094ba319b5SDimitry Andric TII.get(PPC::FRSP), DestReg)
10104ba319b5SDimitry Andric .addReg(SrcReg);
10114ba319b5SDimitry Andric }
1012f785676fSDimitry Andric
101339d628a0SDimitry Andric updateValueMap(I, DestReg);
1014f785676fSDimitry Andric return true;
1015f785676fSDimitry Andric }
1016f785676fSDimitry Andric
1017f785676fSDimitry Andric // Move an i32 or i64 value in a GPR to an f64 value in an FPR.
10189cac79b3SDimitry Andric // FIXME: When direct register moves are implemented (see PowerISA 2.07),
1019f785676fSDimitry Andric // those should be used instead of moving via a stack slot when the
1020f785676fSDimitry Andric // subtarget permits.
1021f785676fSDimitry Andric // FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
1022f785676fSDimitry Andric // stack slot and 4-byte store/load sequence. Or just sext the 4-byte
1023f785676fSDimitry Andric // case to 8 bytes which produces tighter code but wastes stack space.
PPCMoveToFPReg(MVT SrcVT,unsigned SrcReg,bool IsSigned)1024f785676fSDimitry Andric unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
1025f785676fSDimitry Andric bool IsSigned) {
1026f785676fSDimitry Andric
1027f785676fSDimitry Andric // If necessary, extend 32-bit int to 64-bit.
1028f785676fSDimitry Andric if (SrcVT == MVT::i32) {
1029f785676fSDimitry Andric unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1030f785676fSDimitry Andric if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
1031f785676fSDimitry Andric return 0;
1032f785676fSDimitry Andric SrcReg = TmpReg;
1033f785676fSDimitry Andric }
1034f785676fSDimitry Andric
1035f785676fSDimitry Andric // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1036f785676fSDimitry Andric Address Addr;
1037f785676fSDimitry Andric Addr.BaseType = Address::FrameIndexBase;
1038f785676fSDimitry Andric Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1039f785676fSDimitry Andric
1040f785676fSDimitry Andric // Store the value from the GPR.
1041f785676fSDimitry Andric if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
1042f785676fSDimitry Andric return 0;
1043f785676fSDimitry Andric
1044f785676fSDimitry Andric // Load the integer value into an FPR. The kind of load used depends
1045f785676fSDimitry Andric // on a number of conditions.
1046f785676fSDimitry Andric unsigned LoadOpc = PPC::LFD;
1047f785676fSDimitry Andric
1048f785676fSDimitry Andric if (SrcVT == MVT::i32) {
104985d60e68SDimitry Andric if (!IsSigned) {
1050f785676fSDimitry Andric LoadOpc = PPC::LFIWZX;
10519cac79b3SDimitry Andric Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
105291bc56edSDimitry Andric } else if (PPCSubTarget->hasLFIWAX()) {
1053f785676fSDimitry Andric LoadOpc = PPC::LFIWAX;
10549cac79b3SDimitry Andric Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
105585d60e68SDimitry Andric }
1056f785676fSDimitry Andric }
1057f785676fSDimitry Andric
1058f785676fSDimitry Andric const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1059f785676fSDimitry Andric unsigned ResultReg = 0;
1060f785676fSDimitry Andric if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
1061f785676fSDimitry Andric return 0;
1062f785676fSDimitry Andric
1063f785676fSDimitry Andric return ResultReg;
1064f785676fSDimitry Andric }
1065f785676fSDimitry Andric
1066f785676fSDimitry Andric // Attempt to fast-select an integer-to-floating-point conversion.
1067ff0cc061SDimitry Andric // FIXME: Once fast-isel has better support for VSX, conversions using
1068ff0cc061SDimitry Andric // direct moves should be implemented.
SelectIToFP(const Instruction * I,bool IsSigned)1069f785676fSDimitry Andric bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
1070f785676fSDimitry Andric MVT DstVT;
1071f785676fSDimitry Andric Type *DstTy = I->getType();
1072f785676fSDimitry Andric if (!isTypeLegal(DstTy, DstVT))
1073f785676fSDimitry Andric return false;
1074f785676fSDimitry Andric
1075f785676fSDimitry Andric if (DstVT != MVT::f32 && DstVT != MVT::f64)
1076f785676fSDimitry Andric return false;
1077f785676fSDimitry Andric
1078f785676fSDimitry Andric Value *Src = I->getOperand(0);
1079875ed548SDimitry Andric EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
1080f785676fSDimitry Andric if (!SrcEVT.isSimple())
1081f785676fSDimitry Andric return false;
1082f785676fSDimitry Andric
1083f785676fSDimitry Andric MVT SrcVT = SrcEVT.getSimpleVT();
1084f785676fSDimitry Andric
1085f785676fSDimitry Andric if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
1086f785676fSDimitry Andric SrcVT != MVT::i32 && SrcVT != MVT::i64)
1087f785676fSDimitry Andric return false;
1088f785676fSDimitry Andric
1089f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
1090f785676fSDimitry Andric if (SrcReg == 0)
1091f785676fSDimitry Andric return false;
1092f785676fSDimitry Andric
10934ba319b5SDimitry Andric // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
10944ba319b5SDimitry Andric if (PPCSubTarget->hasSPE()) {
10954ba319b5SDimitry Andric unsigned Opc;
10964ba319b5SDimitry Andric if (DstVT == MVT::f32)
10974ba319b5SDimitry Andric Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
10984ba319b5SDimitry Andric else
10994ba319b5SDimitry Andric Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
11004ba319b5SDimitry Andric
11014ba319b5SDimitry Andric unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
11024ba319b5SDimitry Andric // Generate the convert.
11034ba319b5SDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
11044ba319b5SDimitry Andric .addReg(SrcReg);
11054ba319b5SDimitry Andric updateValueMap(I, DestReg);
11064ba319b5SDimitry Andric return true;
11074ba319b5SDimitry Andric }
11084ba319b5SDimitry Andric
1109f785676fSDimitry Andric // We can only lower an unsigned convert if we have the newer
1110f785676fSDimitry Andric // floating-point conversion operations.
111191bc56edSDimitry Andric if (!IsSigned && !PPCSubTarget->hasFPCVT())
1112f785676fSDimitry Andric return false;
1113f785676fSDimitry Andric
1114f785676fSDimitry Andric // FIXME: For now we require the newer floating-point conversion operations
1115f785676fSDimitry Andric // (which are present only on P7 and A2 server models) when converting
1116f785676fSDimitry Andric // to single-precision float. Otherwise we have to generate a lot of
1117f785676fSDimitry Andric // fiddly code to avoid double rounding. If necessary, the fiddly code
1118f785676fSDimitry Andric // can be found in PPCTargetLowering::LowerINT_TO_FP().
111991bc56edSDimitry Andric if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT())
1120f785676fSDimitry Andric return false;
1121f785676fSDimitry Andric
1122f785676fSDimitry Andric // Extend the input if necessary.
1123f785676fSDimitry Andric if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1124f785676fSDimitry Andric unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
1125f785676fSDimitry Andric if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
1126f785676fSDimitry Andric return false;
1127f785676fSDimitry Andric SrcVT = MVT::i64;
1128f785676fSDimitry Andric SrcReg = TmpReg;
1129f785676fSDimitry Andric }
1130f785676fSDimitry Andric
1131f785676fSDimitry Andric // Move the integer value to an FPR.
1132f785676fSDimitry Andric unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
1133f785676fSDimitry Andric if (FPReg == 0)
1134f785676fSDimitry Andric return false;
1135f785676fSDimitry Andric
1136f785676fSDimitry Andric // Determine the opcode for the conversion.
1137f785676fSDimitry Andric const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1138f785676fSDimitry Andric unsigned DestReg = createResultReg(RC);
1139f785676fSDimitry Andric unsigned Opc;
1140f785676fSDimitry Andric
1141f785676fSDimitry Andric if (DstVT == MVT::f32)
1142f785676fSDimitry Andric Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
1143f785676fSDimitry Andric else
1144f785676fSDimitry Andric Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
1145f785676fSDimitry Andric
1146f785676fSDimitry Andric // Generate the convert.
114791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1148f785676fSDimitry Andric .addReg(FPReg);
1149f785676fSDimitry Andric
115039d628a0SDimitry Andric updateValueMap(I, DestReg);
1151f785676fSDimitry Andric return true;
1152f785676fSDimitry Andric }
1153f785676fSDimitry Andric
1154f785676fSDimitry Andric // Move the floating-point value in SrcReg into an integer destination
1155f785676fSDimitry Andric // register, and return the register (or zero if we can't handle it).
11569cac79b3SDimitry Andric // FIXME: When direct register moves are implemented (see PowerISA 2.07),
1157f785676fSDimitry Andric // those should be used instead of moving via a stack slot when the
1158f785676fSDimitry Andric // subtarget permits.
PPCMoveToIntReg(const Instruction * I,MVT VT,unsigned SrcReg,bool IsSigned)1159f785676fSDimitry Andric unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
1160f785676fSDimitry Andric unsigned SrcReg, bool IsSigned) {
1161f785676fSDimitry Andric // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
1162f785676fSDimitry Andric // Note that if have STFIWX available, we could use a 4-byte stack
1163f785676fSDimitry Andric // slot for i32, but this being fast-isel we'll just go with the
1164f785676fSDimitry Andric // easiest code gen possible.
1165f785676fSDimitry Andric Address Addr;
1166f785676fSDimitry Andric Addr.BaseType = Address::FrameIndexBase;
1167f785676fSDimitry Andric Addr.Base.FI = MFI.CreateStackObject(8, 8, false);
1168f785676fSDimitry Andric
1169f785676fSDimitry Andric // Store the value from the FPR.
1170f785676fSDimitry Andric if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
1171f785676fSDimitry Andric return 0;
1172f785676fSDimitry Andric
11733ca95b02SDimitry Andric // Reload it into a GPR. If we want an i32 on big endian, modify the
11743ca95b02SDimitry Andric // address to have a 4-byte offset so we load from the right place.
1175f785676fSDimitry Andric if (VT == MVT::i32)
11763ca95b02SDimitry Andric Addr.Offset = (PPCSubTarget->isLittleEndian()) ? 0 : 4;
1177f785676fSDimitry Andric
1178f785676fSDimitry Andric // Look at the currently assigned register for this instruction
1179f785676fSDimitry Andric // to determine the required register class.
1180f785676fSDimitry Andric unsigned AssignedReg = FuncInfo.ValueMap[I];
1181f785676fSDimitry Andric const TargetRegisterClass *RC =
118291bc56edSDimitry Andric AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
1183f785676fSDimitry Andric
1184f785676fSDimitry Andric unsigned ResultReg = 0;
1185f785676fSDimitry Andric if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
1186f785676fSDimitry Andric return 0;
1187f785676fSDimitry Andric
1188f785676fSDimitry Andric return ResultReg;
1189f785676fSDimitry Andric }
1190f785676fSDimitry Andric
1191f785676fSDimitry Andric // Attempt to fast-select a floating-point-to-integer conversion.
1192ff0cc061SDimitry Andric // FIXME: Once fast-isel has better support for VSX, conversions using
1193ff0cc061SDimitry Andric // direct moves should be implemented.
SelectFPToI(const Instruction * I,bool IsSigned)1194f785676fSDimitry Andric bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1195f785676fSDimitry Andric MVT DstVT, SrcVT;
1196f785676fSDimitry Andric Type *DstTy = I->getType();
1197f785676fSDimitry Andric if (!isTypeLegal(DstTy, DstVT))
1198f785676fSDimitry Andric return false;
1199f785676fSDimitry Andric
1200f785676fSDimitry Andric if (DstVT != MVT::i32 && DstVT != MVT::i64)
1201f785676fSDimitry Andric return false;
1202f785676fSDimitry Andric
12034ba319b5SDimitry Andric // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
12044ba319b5SDimitry Andric if (DstVT == MVT::i64 && !IsSigned &&
12054ba319b5SDimitry Andric !PPCSubTarget->hasFPCVT() && !PPCSubTarget->hasSPE())
120696b9c776SDimitry Andric return false;
120796b9c776SDimitry Andric
1208f785676fSDimitry Andric Value *Src = I->getOperand(0);
1209f785676fSDimitry Andric Type *SrcTy = Src->getType();
1210f785676fSDimitry Andric if (!isTypeLegal(SrcTy, SrcVT))
1211f785676fSDimitry Andric return false;
1212f785676fSDimitry Andric
1213f785676fSDimitry Andric if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1214f785676fSDimitry Andric return false;
1215f785676fSDimitry Andric
1216f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
1217f785676fSDimitry Andric if (SrcReg == 0)
1218f785676fSDimitry Andric return false;
1219f785676fSDimitry Andric
1220f785676fSDimitry Andric // Convert f32 to f64 if necessary. This is just a meaningless copy
12213ca95b02SDimitry Andric // to get the register class right.
1222f785676fSDimitry Andric const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1223f785676fSDimitry Andric if (InRC == &PPC::F4RCRegClass) {
1224f785676fSDimitry Andric unsigned TmpReg = createResultReg(&PPC::F8RCRegClass);
122591bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
12263ca95b02SDimitry Andric TII.get(TargetOpcode::COPY), TmpReg)
12273ca95b02SDimitry Andric .addReg(SrcReg);
1228f785676fSDimitry Andric SrcReg = TmpReg;
1229f785676fSDimitry Andric }
1230f785676fSDimitry Andric
1231f785676fSDimitry Andric // Determine the opcode for the conversion, which takes place
1232f785676fSDimitry Andric // entirely within FPRs.
12334ba319b5SDimitry Andric unsigned DestReg;
1234f785676fSDimitry Andric unsigned Opc;
1235f785676fSDimitry Andric
12364ba319b5SDimitry Andric if (PPCSubTarget->hasSPE()) {
12374ba319b5SDimitry Andric DestReg = createResultReg(&PPC::GPRCRegClass);
12384ba319b5SDimitry Andric if (IsSigned)
12394ba319b5SDimitry Andric Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
12404ba319b5SDimitry Andric else
12414ba319b5SDimitry Andric Opc = InRC == &PPC::SPE4RCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
12424ba319b5SDimitry Andric } else {
12434ba319b5SDimitry Andric DestReg = createResultReg(&PPC::F8RCRegClass);
1244f785676fSDimitry Andric if (DstVT == MVT::i32)
1245f785676fSDimitry Andric if (IsSigned)
1246f785676fSDimitry Andric Opc = PPC::FCTIWZ;
1247f785676fSDimitry Andric else
124891bc56edSDimitry Andric Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
1249f785676fSDimitry Andric else
1250f785676fSDimitry Andric Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
12514ba319b5SDimitry Andric }
1252f785676fSDimitry Andric
1253f785676fSDimitry Andric // Generate the convert.
125491bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1255f785676fSDimitry Andric .addReg(SrcReg);
1256f785676fSDimitry Andric
1257f785676fSDimitry Andric // Now move the integer value from a float register to an integer register.
12584ba319b5SDimitry Andric unsigned IntReg = PPCSubTarget->hasSPE() ? DestReg :
12594ba319b5SDimitry Andric PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
12604ba319b5SDimitry Andric
1261f785676fSDimitry Andric if (IntReg == 0)
1262f785676fSDimitry Andric return false;
1263f785676fSDimitry Andric
126439d628a0SDimitry Andric updateValueMap(I, IntReg);
1265f785676fSDimitry Andric return true;
1266f785676fSDimitry Andric }
1267f785676fSDimitry Andric
1268f785676fSDimitry Andric // Attempt to fast-select a binary integer operation that isn't already
1269f785676fSDimitry Andric // handled automatically.
SelectBinaryIntOp(const Instruction * I,unsigned ISDOpcode)1270f785676fSDimitry Andric bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1271875ed548SDimitry Andric EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1272f785676fSDimitry Andric
1273f785676fSDimitry Andric // We can get here in the case when we have a binary operation on a non-legal
1274f785676fSDimitry Andric // type and the target independent selector doesn't know how to handle it.
1275f785676fSDimitry Andric if (DestVT != MVT::i16 && DestVT != MVT::i8)
1276f785676fSDimitry Andric return false;
1277f785676fSDimitry Andric
1278f785676fSDimitry Andric // Look at the currently assigned register for this instruction
1279f785676fSDimitry Andric // to determine the required register class. If there is no register,
1280f785676fSDimitry Andric // make a conservative choice (don't assign R0).
1281f785676fSDimitry Andric unsigned AssignedReg = FuncInfo.ValueMap[I];
1282f785676fSDimitry Andric const TargetRegisterClass *RC =
1283f785676fSDimitry Andric (AssignedReg ? MRI.getRegClass(AssignedReg) :
1284f785676fSDimitry Andric &PPC::GPRC_and_GPRC_NOR0RegClass);
1285f785676fSDimitry Andric bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1286f785676fSDimitry Andric
1287f785676fSDimitry Andric unsigned Opc;
1288f785676fSDimitry Andric switch (ISDOpcode) {
1289f785676fSDimitry Andric default: return false;
1290f785676fSDimitry Andric case ISD::ADD:
1291f785676fSDimitry Andric Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1292f785676fSDimitry Andric break;
1293f785676fSDimitry Andric case ISD::OR:
1294f785676fSDimitry Andric Opc = IsGPRC ? PPC::OR : PPC::OR8;
1295f785676fSDimitry Andric break;
1296f785676fSDimitry Andric case ISD::SUB:
1297f785676fSDimitry Andric Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1298f785676fSDimitry Andric break;
1299f785676fSDimitry Andric }
1300f785676fSDimitry Andric
1301f785676fSDimitry Andric unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1302f785676fSDimitry Andric unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1303f785676fSDimitry Andric if (SrcReg1 == 0) return false;
1304f785676fSDimitry Andric
1305f785676fSDimitry Andric // Handle case of small immediate operand.
1306f785676fSDimitry Andric if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1307f785676fSDimitry Andric const APInt &CIVal = ConstInt->getValue();
1308f785676fSDimitry Andric int Imm = (int)CIVal.getSExtValue();
1309f785676fSDimitry Andric bool UseImm = true;
1310f785676fSDimitry Andric if (isInt<16>(Imm)) {
1311f785676fSDimitry Andric switch (Opc) {
1312f785676fSDimitry Andric default:
1313f785676fSDimitry Andric llvm_unreachable("Missing case!");
1314f785676fSDimitry Andric case PPC::ADD4:
1315f785676fSDimitry Andric Opc = PPC::ADDI;
1316f785676fSDimitry Andric MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1317f785676fSDimitry Andric break;
1318f785676fSDimitry Andric case PPC::ADD8:
1319f785676fSDimitry Andric Opc = PPC::ADDI8;
1320f785676fSDimitry Andric MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1321f785676fSDimitry Andric break;
1322f785676fSDimitry Andric case PPC::OR:
1323f785676fSDimitry Andric Opc = PPC::ORI;
1324f785676fSDimitry Andric break;
1325f785676fSDimitry Andric case PPC::OR8:
1326f785676fSDimitry Andric Opc = PPC::ORI8;
1327f785676fSDimitry Andric break;
1328f785676fSDimitry Andric case PPC::SUBF:
1329f785676fSDimitry Andric if (Imm == -32768)
1330f785676fSDimitry Andric UseImm = false;
1331f785676fSDimitry Andric else {
1332f785676fSDimitry Andric Opc = PPC::ADDI;
1333f785676fSDimitry Andric MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1334f785676fSDimitry Andric Imm = -Imm;
1335f785676fSDimitry Andric }
1336f785676fSDimitry Andric break;
1337f785676fSDimitry Andric case PPC::SUBF8:
1338f785676fSDimitry Andric if (Imm == -32768)
1339f785676fSDimitry Andric UseImm = false;
1340f785676fSDimitry Andric else {
1341f785676fSDimitry Andric Opc = PPC::ADDI8;
1342f785676fSDimitry Andric MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1343f785676fSDimitry Andric Imm = -Imm;
1344f785676fSDimitry Andric }
1345f785676fSDimitry Andric break;
1346f785676fSDimitry Andric }
1347f785676fSDimitry Andric
1348f785676fSDimitry Andric if (UseImm) {
134991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
135091bc56edSDimitry Andric ResultReg)
135191bc56edSDimitry Andric .addReg(SrcReg1)
135291bc56edSDimitry Andric .addImm(Imm);
135339d628a0SDimitry Andric updateValueMap(I, ResultReg);
1354f785676fSDimitry Andric return true;
1355f785676fSDimitry Andric }
1356f785676fSDimitry Andric }
1357f785676fSDimitry Andric }
1358f785676fSDimitry Andric
1359f785676fSDimitry Andric // Reg-reg case.
1360f785676fSDimitry Andric unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1361f785676fSDimitry Andric if (SrcReg2 == 0) return false;
1362f785676fSDimitry Andric
1363f785676fSDimitry Andric // Reverse operands for subtract-from.
1364f785676fSDimitry Andric if (ISDOpcode == ISD::SUB)
1365f785676fSDimitry Andric std::swap(SrcReg1, SrcReg2);
1366f785676fSDimitry Andric
136791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1368f785676fSDimitry Andric .addReg(SrcReg1).addReg(SrcReg2);
136939d628a0SDimitry Andric updateValueMap(I, ResultReg);
1370f785676fSDimitry Andric return true;
1371f785676fSDimitry Andric }
1372f785676fSDimitry Andric
1373f785676fSDimitry Andric // Handle arguments to a call that we're attempting to fast-select.
1374f785676fSDimitry Andric // Return false if the arguments are too complex for us at the moment.
processCallArgs(SmallVectorImpl<Value * > & Args,SmallVectorImpl<unsigned> & ArgRegs,SmallVectorImpl<MVT> & ArgVTs,SmallVectorImpl<ISD::ArgFlagsTy> & ArgFlags,SmallVectorImpl<unsigned> & RegArgs,CallingConv::ID CC,unsigned & NumBytes,bool IsVarArg)1375f785676fSDimitry Andric bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
1376f785676fSDimitry Andric SmallVectorImpl<unsigned> &ArgRegs,
1377f785676fSDimitry Andric SmallVectorImpl<MVT> &ArgVTs,
1378f785676fSDimitry Andric SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1379f785676fSDimitry Andric SmallVectorImpl<unsigned> &RegArgs,
1380f785676fSDimitry Andric CallingConv::ID CC,
1381f785676fSDimitry Andric unsigned &NumBytes,
1382f785676fSDimitry Andric bool IsVarArg) {
1383f785676fSDimitry Andric SmallVector<CCValAssign, 16> ArgLocs;
138439d628a0SDimitry Andric CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
138591bc56edSDimitry Andric
138691bc56edSDimitry Andric // Reserve space for the linkage area on the stack.
1387ff0cc061SDimitry Andric unsigned LinkageSize = PPCSubTarget->getFrameLowering()->getLinkageSize();
138891bc56edSDimitry Andric CCInfo.AllocateStack(LinkageSize, 8);
138991bc56edSDimitry Andric
1390f785676fSDimitry Andric CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
1391f785676fSDimitry Andric
1392f785676fSDimitry Andric // Bail out if we can't handle any of the arguments.
1393f785676fSDimitry Andric for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1394f785676fSDimitry Andric CCValAssign &VA = ArgLocs[I];
1395f785676fSDimitry Andric MVT ArgVT = ArgVTs[VA.getValNo()];
1396f785676fSDimitry Andric
1397f785676fSDimitry Andric // Skip vector arguments for now, as well as long double and
1398f785676fSDimitry Andric // uint128_t, and anything that isn't passed in a register.
139991bc56edSDimitry Andric if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
1400f785676fSDimitry Andric !VA.isRegLoc() || VA.needsCustom())
1401f785676fSDimitry Andric return false;
1402f785676fSDimitry Andric
1403f785676fSDimitry Andric // Skip bit-converted arguments for now.
1404f785676fSDimitry Andric if (VA.getLocInfo() == CCValAssign::BCvt)
1405f785676fSDimitry Andric return false;
1406f785676fSDimitry Andric }
1407f785676fSDimitry Andric
1408f785676fSDimitry Andric // Get a count of how many bytes are to be pushed onto the stack.
1409f785676fSDimitry Andric NumBytes = CCInfo.getNextStackOffset();
1410f785676fSDimitry Andric
141191bc56edSDimitry Andric // The prolog code of the callee may store up to 8 GPR argument registers to
141291bc56edSDimitry Andric // the stack, allowing va_start to index over them in memory if its varargs.
141391bc56edSDimitry Andric // Because we cannot tell if this is needed on the caller side, we have to
141491bc56edSDimitry Andric // conservatively assume that it is needed. As such, make sure we have at
141591bc56edSDimitry Andric // least enough stack space for the caller to store the 8 GPRs.
141691bc56edSDimitry Andric // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
141791bc56edSDimitry Andric NumBytes = std::max(NumBytes, LinkageSize + 64);
141891bc56edSDimitry Andric
1419f785676fSDimitry Andric // Issue CALLSEQ_START.
142091bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1421f785676fSDimitry Andric TII.get(TII.getCallFrameSetupOpcode()))
14225517e702SDimitry Andric .addImm(NumBytes).addImm(0);
1423f785676fSDimitry Andric
1424f785676fSDimitry Andric // Prepare to assign register arguments. Every argument uses up a
1425f785676fSDimitry Andric // GPR protocol register even if it's passed in a floating-point
1426ff0cc061SDimitry Andric // register (unless we're using the fast calling convention).
1427f785676fSDimitry Andric unsigned NextGPR = PPC::X3;
1428f785676fSDimitry Andric unsigned NextFPR = PPC::F1;
1429f785676fSDimitry Andric
1430f785676fSDimitry Andric // Process arguments.
1431f785676fSDimitry Andric for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1432f785676fSDimitry Andric CCValAssign &VA = ArgLocs[I];
1433f785676fSDimitry Andric unsigned Arg = ArgRegs[VA.getValNo()];
1434f785676fSDimitry Andric MVT ArgVT = ArgVTs[VA.getValNo()];
1435f785676fSDimitry Andric
1436f785676fSDimitry Andric // Handle argument promotion and bitcasts.
1437f785676fSDimitry Andric switch (VA.getLocInfo()) {
1438f785676fSDimitry Andric default:
1439f785676fSDimitry Andric llvm_unreachable("Unknown loc info!");
1440f785676fSDimitry Andric case CCValAssign::Full:
1441f785676fSDimitry Andric break;
1442f785676fSDimitry Andric case CCValAssign::SExt: {
1443f785676fSDimitry Andric MVT DestVT = VA.getLocVT();
1444f785676fSDimitry Andric const TargetRegisterClass *RC =
1445f785676fSDimitry Andric (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1446f785676fSDimitry Andric unsigned TmpReg = createResultReg(RC);
1447f785676fSDimitry Andric if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
1448f785676fSDimitry Andric llvm_unreachable("Failed to emit a sext!");
1449f785676fSDimitry Andric ArgVT = DestVT;
1450f785676fSDimitry Andric Arg = TmpReg;
1451f785676fSDimitry Andric break;
1452f785676fSDimitry Andric }
1453f785676fSDimitry Andric case CCValAssign::AExt:
1454f785676fSDimitry Andric case CCValAssign::ZExt: {
1455f785676fSDimitry Andric MVT DestVT = VA.getLocVT();
1456f785676fSDimitry Andric const TargetRegisterClass *RC =
1457f785676fSDimitry Andric (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1458f785676fSDimitry Andric unsigned TmpReg = createResultReg(RC);
1459f785676fSDimitry Andric if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
1460f785676fSDimitry Andric llvm_unreachable("Failed to emit a zext!");
1461f785676fSDimitry Andric ArgVT = DestVT;
1462f785676fSDimitry Andric Arg = TmpReg;
1463f785676fSDimitry Andric break;
1464f785676fSDimitry Andric }
1465f785676fSDimitry Andric case CCValAssign::BCvt: {
1466f785676fSDimitry Andric // FIXME: Not yet handled.
1467f785676fSDimitry Andric llvm_unreachable("Should have bailed before getting here!");
1468f785676fSDimitry Andric break;
1469f785676fSDimitry Andric }
1470f785676fSDimitry Andric }
1471f785676fSDimitry Andric
1472f785676fSDimitry Andric // Copy this argument to the appropriate register.
1473f785676fSDimitry Andric unsigned ArgReg;
1474f785676fSDimitry Andric if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
1475f785676fSDimitry Andric ArgReg = NextFPR++;
1476ff0cc061SDimitry Andric if (CC != CallingConv::Fast)
1477f785676fSDimitry Andric ++NextGPR;
1478f785676fSDimitry Andric } else
1479f785676fSDimitry Andric ArgReg = NextGPR++;
1480f785676fSDimitry Andric
148191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
148291bc56edSDimitry Andric TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
1483f785676fSDimitry Andric RegArgs.push_back(ArgReg);
1484f785676fSDimitry Andric }
1485f785676fSDimitry Andric
1486f785676fSDimitry Andric return true;
1487f785676fSDimitry Andric }
1488f785676fSDimitry Andric
1489f785676fSDimitry Andric // For a call that we've determined we can fast-select, finish the
1490f785676fSDimitry Andric // call sequence and generate a copy to obtain the return value (if any).
finishCall(MVT RetVT,CallLoweringInfo & CLI,unsigned & NumBytes)149139d628a0SDimitry Andric bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
149239d628a0SDimitry Andric CallingConv::ID CC = CLI.CallConv;
149339d628a0SDimitry Andric
1494f785676fSDimitry Andric // Issue CallSEQ_END.
149591bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1496f785676fSDimitry Andric TII.get(TII.getCallFrameDestroyOpcode()))
1497f785676fSDimitry Andric .addImm(NumBytes).addImm(0);
1498f785676fSDimitry Andric
1499f785676fSDimitry Andric // Next, generate a copy to obtain the return value.
1500f785676fSDimitry Andric // FIXME: No multi-register return values yet, though I don't foresee
1501f785676fSDimitry Andric // any real difficulties there.
1502f785676fSDimitry Andric if (RetVT != MVT::isVoid) {
1503f785676fSDimitry Andric SmallVector<CCValAssign, 16> RVLocs;
150439d628a0SDimitry Andric CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1505f785676fSDimitry Andric CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1506f785676fSDimitry Andric CCValAssign &VA = RVLocs[0];
1507f785676fSDimitry Andric assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
1508f785676fSDimitry Andric assert(VA.isRegLoc() && "Can only return in registers!");
1509f785676fSDimitry Andric
1510f785676fSDimitry Andric MVT DestVT = VA.getValVT();
1511f785676fSDimitry Andric MVT CopyVT = DestVT;
1512f785676fSDimitry Andric
1513f785676fSDimitry Andric // Ints smaller than a register still arrive in a full 64-bit
1514f785676fSDimitry Andric // register, so make sure we recognize this.
1515f785676fSDimitry Andric if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
1516f785676fSDimitry Andric CopyVT = MVT::i64;
1517f785676fSDimitry Andric
1518f785676fSDimitry Andric unsigned SourcePhysReg = VA.getLocReg();
1519f785676fSDimitry Andric unsigned ResultReg = 0;
1520f785676fSDimitry Andric
1521f785676fSDimitry Andric if (RetVT == CopyVT) {
1522f785676fSDimitry Andric const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1523f785676fSDimitry Andric ResultReg = createResultReg(CpyRC);
1524f785676fSDimitry Andric
152591bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1526f785676fSDimitry Andric TII.get(TargetOpcode::COPY), ResultReg)
1527f785676fSDimitry Andric .addReg(SourcePhysReg);
1528f785676fSDimitry Andric
1529f785676fSDimitry Andric // If necessary, round the floating result to single precision.
1530f785676fSDimitry Andric } else if (CopyVT == MVT::f64) {
1531f785676fSDimitry Andric ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
153291bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
1533f785676fSDimitry Andric ResultReg).addReg(SourcePhysReg);
1534f785676fSDimitry Andric
1535f785676fSDimitry Andric // If only the low half of a general register is needed, generate
1536f785676fSDimitry Andric // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
1537f785676fSDimitry Andric // used along the fast-isel path (not lowered), and downstream logic
1538f785676fSDimitry Andric // also doesn't like a direct subreg copy on a physical reg.)
1539f785676fSDimitry Andric } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
1540f785676fSDimitry Andric ResultReg = createResultReg(&PPC::GPRCRegClass);
1541f785676fSDimitry Andric // Convert physical register from G8RC to GPRC.
1542f785676fSDimitry Andric SourcePhysReg -= PPC::X0 - PPC::R0;
154391bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1544f785676fSDimitry Andric TII.get(TargetOpcode::COPY), ResultReg)
1545f785676fSDimitry Andric .addReg(SourcePhysReg);
1546f785676fSDimitry Andric }
1547f785676fSDimitry Andric
1548f785676fSDimitry Andric assert(ResultReg && "ResultReg unset!");
154939d628a0SDimitry Andric CLI.InRegs.push_back(SourcePhysReg);
155039d628a0SDimitry Andric CLI.ResultReg = ResultReg;
155139d628a0SDimitry Andric CLI.NumResultRegs = 1;
1552f785676fSDimitry Andric }
1553f785676fSDimitry Andric
155439d628a0SDimitry Andric return true;
155539d628a0SDimitry Andric }
1556f785676fSDimitry Andric
fastLowerCall(CallLoweringInfo & CLI)155739d628a0SDimitry Andric bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
155839d628a0SDimitry Andric CallingConv::ID CC = CLI.CallConv;
155939d628a0SDimitry Andric bool IsTailCall = CLI.IsTailCall;
156039d628a0SDimitry Andric bool IsVarArg = CLI.IsVarArg;
156139d628a0SDimitry Andric const Value *Callee = CLI.Callee;
15623dac3a9bSDimitry Andric const MCSymbol *Symbol = CLI.Symbol;
156339d628a0SDimitry Andric
15643dac3a9bSDimitry Andric if (!Callee && !Symbol)
1565f785676fSDimitry Andric return false;
1566f785676fSDimitry Andric
1567f785676fSDimitry Andric // Allow SelectionDAG isel to handle tail calls.
156839d628a0SDimitry Andric if (IsTailCall)
1569f785676fSDimitry Andric return false;
1570f785676fSDimitry Andric
157139d628a0SDimitry Andric // Let SDISel handle vararg functions.
1572f785676fSDimitry Andric if (IsVarArg)
1573f785676fSDimitry Andric return false;
1574f785676fSDimitry Andric
1575f785676fSDimitry Andric // Handle simple calls for now, with legal return types and
1576f785676fSDimitry Andric // those that can be extended.
157739d628a0SDimitry Andric Type *RetTy = CLI.RetTy;
1578f785676fSDimitry Andric MVT RetVT;
1579f785676fSDimitry Andric if (RetTy->isVoidTy())
1580f785676fSDimitry Andric RetVT = MVT::isVoid;
1581f785676fSDimitry Andric else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1582f785676fSDimitry Andric RetVT != MVT::i8)
1583f785676fSDimitry Andric return false;
1584ff0cc061SDimitry Andric else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1585ff0cc061SDimitry Andric // We can't handle boolean returns when CR bits are in use.
1586ff0cc061SDimitry Andric return false;
1587f785676fSDimitry Andric
1588f785676fSDimitry Andric // FIXME: No multi-register return values yet.
1589f785676fSDimitry Andric if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1590f785676fSDimitry Andric RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1591f785676fSDimitry Andric RetVT != MVT::f64) {
1592f785676fSDimitry Andric SmallVector<CCValAssign, 16> RVLocs;
159339d628a0SDimitry Andric CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
1594f785676fSDimitry Andric CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
1595f785676fSDimitry Andric if (RVLocs.size() > 1)
1596f785676fSDimitry Andric return false;
1597f785676fSDimitry Andric }
1598f785676fSDimitry Andric
1599f785676fSDimitry Andric // Bail early if more than 8 arguments, as we only currently
1600f785676fSDimitry Andric // handle arguments passed in registers.
160139d628a0SDimitry Andric unsigned NumArgs = CLI.OutVals.size();
1602f785676fSDimitry Andric if (NumArgs > 8)
1603f785676fSDimitry Andric return false;
1604f785676fSDimitry Andric
1605f785676fSDimitry Andric // Set up the argument vectors.
1606f785676fSDimitry Andric SmallVector<Value*, 8> Args;
1607f785676fSDimitry Andric SmallVector<unsigned, 8> ArgRegs;
1608f785676fSDimitry Andric SmallVector<MVT, 8> ArgVTs;
1609f785676fSDimitry Andric SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1610f785676fSDimitry Andric
1611f785676fSDimitry Andric Args.reserve(NumArgs);
1612f785676fSDimitry Andric ArgRegs.reserve(NumArgs);
1613f785676fSDimitry Andric ArgVTs.reserve(NumArgs);
1614f785676fSDimitry Andric ArgFlags.reserve(NumArgs);
1615f785676fSDimitry Andric
161639d628a0SDimitry Andric for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
1617f785676fSDimitry Andric // Only handle easy calls for now. It would be reasonably easy
1618f785676fSDimitry Andric // to handle <= 8-byte structures passed ByVal in registers, but we
1619f785676fSDimitry Andric // have to ensure they are right-justified in the register.
162039d628a0SDimitry Andric ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
162139d628a0SDimitry Andric if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
1622f785676fSDimitry Andric return false;
1623f785676fSDimitry Andric
162439d628a0SDimitry Andric Value *ArgValue = CLI.OutVals[i];
162539d628a0SDimitry Andric Type *ArgTy = ArgValue->getType();
1626f785676fSDimitry Andric MVT ArgVT;
1627f785676fSDimitry Andric if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
1628f785676fSDimitry Andric return false;
1629f785676fSDimitry Andric
1630f785676fSDimitry Andric if (ArgVT.isVector())
1631f785676fSDimitry Andric return false;
1632f785676fSDimitry Andric
163339d628a0SDimitry Andric unsigned Arg = getRegForValue(ArgValue);
1634f785676fSDimitry Andric if (Arg == 0)
1635f785676fSDimitry Andric return false;
1636f785676fSDimitry Andric
163739d628a0SDimitry Andric Args.push_back(ArgValue);
1638f785676fSDimitry Andric ArgRegs.push_back(Arg);
1639f785676fSDimitry Andric ArgVTs.push_back(ArgVT);
1640f785676fSDimitry Andric ArgFlags.push_back(Flags);
1641f785676fSDimitry Andric }
1642f785676fSDimitry Andric
1643f785676fSDimitry Andric // Process the arguments.
1644f785676fSDimitry Andric SmallVector<unsigned, 8> RegArgs;
1645f785676fSDimitry Andric unsigned NumBytes;
1646f785676fSDimitry Andric
1647f785676fSDimitry Andric if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
1648f785676fSDimitry Andric RegArgs, CC, NumBytes, IsVarArg))
1649f785676fSDimitry Andric return false;
1650f785676fSDimitry Andric
165139d628a0SDimitry Andric MachineInstrBuilder MIB;
1652f785676fSDimitry Andric // FIXME: No handling for function pointers yet. This requires
1653f785676fSDimitry Andric // implementing the function descriptor (OPD) setup.
1654f785676fSDimitry Andric const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
165539d628a0SDimitry Andric if (!GV) {
165639d628a0SDimitry Andric // patchpoints are a special case; they always dispatch to a pointer value.
165739d628a0SDimitry Andric // However, we don't actually want to generate the indirect call sequence
165839d628a0SDimitry Andric // here (that will be generated, as necessary, during asm printing), and
165939d628a0SDimitry Andric // the call we generate here will be erased by FastISel::selectPatchpoint,
166039d628a0SDimitry Andric // so don't try very hard...
166139d628a0SDimitry Andric if (CLI.IsPatchPoint)
166239d628a0SDimitry Andric MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
166339d628a0SDimitry Andric else
1664f785676fSDimitry Andric return false;
166539d628a0SDimitry Andric } else {
1666f785676fSDimitry Andric // Build direct call with NOP for TOC restore.
1667f785676fSDimitry Andric // FIXME: We can and should optimize away the NOP for local calls.
166839d628a0SDimitry Andric MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1669f785676fSDimitry Andric TII.get(PPC::BL8_NOP));
1670f785676fSDimitry Andric // Add callee.
1671f785676fSDimitry Andric MIB.addGlobalAddress(GV);
167239d628a0SDimitry Andric }
1673f785676fSDimitry Andric
1674f785676fSDimitry Andric // Add implicit physical register uses to the call.
1675f785676fSDimitry Andric for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
1676f785676fSDimitry Andric MIB.addReg(RegArgs[II], RegState::Implicit);
1677f785676fSDimitry Andric
1678ff0cc061SDimitry Andric // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1679ff0cc061SDimitry Andric // into the call.
1680ff0cc061SDimitry Andric PPCFuncInfo->setUsesTOCBasePtr();
168191bc56edSDimitry Andric MIB.addReg(PPC::X2, RegState::Implicit);
168291bc56edSDimitry Andric
1683f785676fSDimitry Andric // Add a register mask with the call-preserved registers. Proper
1684f785676fSDimitry Andric // defs for return values will be added by setPhysRegsDeadExcept().
1685ff0cc061SDimitry Andric MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1686f785676fSDimitry Andric
168739d628a0SDimitry Andric CLI.Call = MIB;
168839d628a0SDimitry Andric
1689f785676fSDimitry Andric // Finish off the call including any return values.
169039d628a0SDimitry Andric return finishCall(RetVT, CLI, NumBytes);
1691f785676fSDimitry Andric }
1692f785676fSDimitry Andric
1693f785676fSDimitry Andric // Attempt to fast-select a return instruction.
SelectRet(const Instruction * I)1694f785676fSDimitry Andric bool PPCFastISel::SelectRet(const Instruction *I) {
1695f785676fSDimitry Andric
1696f785676fSDimitry Andric if (!FuncInfo.CanLowerReturn)
1697f785676fSDimitry Andric return false;
1698f785676fSDimitry Andric
16993ca95b02SDimitry Andric if (TLI.supportSplitCSR(FuncInfo.MF))
17003ca95b02SDimitry Andric return false;
17013ca95b02SDimitry Andric
1702f785676fSDimitry Andric const ReturnInst *Ret = cast<ReturnInst>(I);
1703f785676fSDimitry Andric const Function &F = *I->getParent()->getParent();
1704f785676fSDimitry Andric
1705f785676fSDimitry Andric // Build a list of return value registers.
1706f785676fSDimitry Andric SmallVector<unsigned, 4> RetRegs;
1707f785676fSDimitry Andric CallingConv::ID CC = F.getCallingConv();
1708f785676fSDimitry Andric
1709f785676fSDimitry Andric if (Ret->getNumOperands() > 0) {
1710f785676fSDimitry Andric SmallVector<ISD::OutputArg, 4> Outs;
17114ba319b5SDimitry Andric GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1712f785676fSDimitry Andric
1713f785676fSDimitry Andric // Analyze operands of the call, assigning locations to each operand.
1714f785676fSDimitry Andric SmallVector<CCValAssign, 16> ValLocs;
171539d628a0SDimitry Andric CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
1716f785676fSDimitry Andric CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1717f785676fSDimitry Andric const Value *RV = Ret->getOperand(0);
1718f785676fSDimitry Andric
1719f785676fSDimitry Andric // FIXME: Only one output register for now.
1720f785676fSDimitry Andric if (ValLocs.size() > 1)
1721f785676fSDimitry Andric return false;
1722f785676fSDimitry Andric
17237d523365SDimitry Andric // Special case for returning a constant integer of any size - materialize
17247d523365SDimitry Andric // the constant as an i64 and copy it to the return register.
17257d523365SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
17269cac79b3SDimitry Andric CCValAssign &VA = ValLocs[0];
17279cac79b3SDimitry Andric
17289cac79b3SDimitry Andric unsigned RetReg = VA.getLocReg();
17297d523365SDimitry Andric // We still need to worry about properly extending the sign. For example,
17307d523365SDimitry Andric // we could have only a single bit or a constant that needs zero
17317d523365SDimitry Andric // extension rather than sign extension. Make sure we pass the return
17327d523365SDimitry Andric // value extension property to integer materialization.
17337d523365SDimitry Andric unsigned SrcReg =
1734a8bcc4d8SDimitry Andric PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
17359cac79b3SDimitry Andric
173691bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
173791bc56edSDimitry Andric TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
17389cac79b3SDimitry Andric
1739f785676fSDimitry Andric RetRegs.push_back(RetReg);
1740f785676fSDimitry Andric
1741f785676fSDimitry Andric } else {
1742f785676fSDimitry Andric unsigned Reg = getRegForValue(RV);
1743f785676fSDimitry Andric
1744f785676fSDimitry Andric if (Reg == 0)
1745f785676fSDimitry Andric return false;
1746f785676fSDimitry Andric
1747f785676fSDimitry Andric // Copy the result values into the output registers.
1748f785676fSDimitry Andric for (unsigned i = 0; i < ValLocs.size(); ++i) {
1749f785676fSDimitry Andric
1750f785676fSDimitry Andric CCValAssign &VA = ValLocs[i];
1751f785676fSDimitry Andric assert(VA.isRegLoc() && "Can only return in registers!");
1752f785676fSDimitry Andric RetRegs.push_back(VA.getLocReg());
1753f785676fSDimitry Andric unsigned SrcReg = Reg + VA.getValNo();
1754f785676fSDimitry Andric
1755875ed548SDimitry Andric EVT RVEVT = TLI.getValueType(DL, RV->getType());
1756f785676fSDimitry Andric if (!RVEVT.isSimple())
1757f785676fSDimitry Andric return false;
1758f785676fSDimitry Andric MVT RVVT = RVEVT.getSimpleVT();
1759f785676fSDimitry Andric MVT DestVT = VA.getLocVT();
1760f785676fSDimitry Andric
1761f785676fSDimitry Andric if (RVVT != DestVT && RVVT != MVT::i8 &&
1762f785676fSDimitry Andric RVVT != MVT::i16 && RVVT != MVT::i32)
1763f785676fSDimitry Andric return false;
1764f785676fSDimitry Andric
1765f785676fSDimitry Andric if (RVVT != DestVT) {
1766f785676fSDimitry Andric switch (VA.getLocInfo()) {
1767f785676fSDimitry Andric default:
1768f785676fSDimitry Andric llvm_unreachable("Unknown loc info!");
1769f785676fSDimitry Andric case CCValAssign::Full:
1770f785676fSDimitry Andric llvm_unreachable("Full value assign but types don't match?");
1771f785676fSDimitry Andric case CCValAssign::AExt:
1772f785676fSDimitry Andric case CCValAssign::ZExt: {
1773f785676fSDimitry Andric const TargetRegisterClass *RC =
1774f785676fSDimitry Andric (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1775f785676fSDimitry Andric unsigned TmpReg = createResultReg(RC);
1776f785676fSDimitry Andric if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1777f785676fSDimitry Andric return false;
1778f785676fSDimitry Andric SrcReg = TmpReg;
1779f785676fSDimitry Andric break;
1780f785676fSDimitry Andric }
1781f785676fSDimitry Andric case CCValAssign::SExt: {
1782f785676fSDimitry Andric const TargetRegisterClass *RC =
1783f785676fSDimitry Andric (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1784f785676fSDimitry Andric unsigned TmpReg = createResultReg(RC);
1785f785676fSDimitry Andric if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1786f785676fSDimitry Andric return false;
1787f785676fSDimitry Andric SrcReg = TmpReg;
1788f785676fSDimitry Andric break;
1789f785676fSDimitry Andric }
1790f785676fSDimitry Andric }
1791f785676fSDimitry Andric }
1792f785676fSDimitry Andric
179391bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1794f785676fSDimitry Andric TII.get(TargetOpcode::COPY), RetRegs[i])
1795f785676fSDimitry Andric .addReg(SrcReg);
1796f785676fSDimitry Andric }
1797f785676fSDimitry Andric }
1798f785676fSDimitry Andric }
1799f785676fSDimitry Andric
180091bc56edSDimitry Andric MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
180139d628a0SDimitry Andric TII.get(PPC::BLR8));
1802f785676fSDimitry Andric
1803f785676fSDimitry Andric for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1804f785676fSDimitry Andric MIB.addReg(RetRegs[i], RegState::Implicit);
1805f785676fSDimitry Andric
1806f785676fSDimitry Andric return true;
1807f785676fSDimitry Andric }
1808f785676fSDimitry Andric
1809f785676fSDimitry Andric // Attempt to emit an integer extend of SrcReg into DestReg. Both
1810f785676fSDimitry Andric // signed and zero extensions are supported. Return false if we
1811f785676fSDimitry Andric // can't handle it.
PPCEmitIntExt(MVT SrcVT,unsigned SrcReg,MVT DestVT,unsigned DestReg,bool IsZExt)1812f785676fSDimitry Andric bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1813f785676fSDimitry Andric unsigned DestReg, bool IsZExt) {
1814f785676fSDimitry Andric if (DestVT != MVT::i32 && DestVT != MVT::i64)
1815f785676fSDimitry Andric return false;
1816f785676fSDimitry Andric if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1817f785676fSDimitry Andric return false;
1818f785676fSDimitry Andric
1819f785676fSDimitry Andric // Signed extensions use EXTSB, EXTSH, EXTSW.
1820f785676fSDimitry Andric if (!IsZExt) {
1821f785676fSDimitry Andric unsigned Opc;
1822f785676fSDimitry Andric if (SrcVT == MVT::i8)
1823f785676fSDimitry Andric Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1824f785676fSDimitry Andric else if (SrcVT == MVT::i16)
1825f785676fSDimitry Andric Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1826f785676fSDimitry Andric else {
1827f785676fSDimitry Andric assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1828f785676fSDimitry Andric Opc = PPC::EXTSW_32_64;
1829f785676fSDimitry Andric }
183091bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1831f785676fSDimitry Andric .addReg(SrcReg);
1832f785676fSDimitry Andric
1833f785676fSDimitry Andric // Unsigned 32-bit extensions use RLWINM.
1834f785676fSDimitry Andric } else if (DestVT == MVT::i32) {
1835f785676fSDimitry Andric unsigned MB;
1836f785676fSDimitry Andric if (SrcVT == MVT::i8)
1837f785676fSDimitry Andric MB = 24;
1838f785676fSDimitry Andric else {
1839f785676fSDimitry Andric assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1840f785676fSDimitry Andric MB = 16;
1841f785676fSDimitry Andric }
184291bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
1843f785676fSDimitry Andric DestReg)
1844f785676fSDimitry Andric .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1845f785676fSDimitry Andric
1846f785676fSDimitry Andric // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1847f785676fSDimitry Andric } else {
1848f785676fSDimitry Andric unsigned MB;
1849f785676fSDimitry Andric if (SrcVT == MVT::i8)
1850f785676fSDimitry Andric MB = 56;
1851f785676fSDimitry Andric else if (SrcVT == MVT::i16)
1852f785676fSDimitry Andric MB = 48;
1853f785676fSDimitry Andric else
1854f785676fSDimitry Andric MB = 32;
185591bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1856f785676fSDimitry Andric TII.get(PPC::RLDICL_32_64), DestReg)
1857f785676fSDimitry Andric .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1858f785676fSDimitry Andric }
1859f785676fSDimitry Andric
1860f785676fSDimitry Andric return true;
1861f785676fSDimitry Andric }
1862f785676fSDimitry Andric
1863f785676fSDimitry Andric // Attempt to fast-select an indirect branch instruction.
SelectIndirectBr(const Instruction * I)1864f785676fSDimitry Andric bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1865f785676fSDimitry Andric unsigned AddrReg = getRegForValue(I->getOperand(0));
1866f785676fSDimitry Andric if (AddrReg == 0)
1867f785676fSDimitry Andric return false;
1868f785676fSDimitry Andric
186991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
1870f785676fSDimitry Andric .addReg(AddrReg);
187191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
1872f785676fSDimitry Andric
1873f785676fSDimitry Andric const IndirectBrInst *IB = cast<IndirectBrInst>(I);
18747d523365SDimitry Andric for (const BasicBlock *SuccBB : IB->successors())
18757d523365SDimitry Andric FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
1876f785676fSDimitry Andric
1877f785676fSDimitry Andric return true;
1878f785676fSDimitry Andric }
1879f785676fSDimitry Andric
1880f785676fSDimitry Andric // Attempt to fast-select an integer truncate instruction.
SelectTrunc(const Instruction * I)1881f785676fSDimitry Andric bool PPCFastISel::SelectTrunc(const Instruction *I) {
1882f785676fSDimitry Andric Value *Src = I->getOperand(0);
1883875ed548SDimitry Andric EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1884875ed548SDimitry Andric EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1885f785676fSDimitry Andric
1886f785676fSDimitry Andric if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1887f785676fSDimitry Andric return false;
1888f785676fSDimitry Andric
1889f785676fSDimitry Andric if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1890f785676fSDimitry Andric return false;
1891f785676fSDimitry Andric
1892f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
1893f785676fSDimitry Andric if (!SrcReg)
1894f785676fSDimitry Andric return false;
1895f785676fSDimitry Andric
1896f785676fSDimitry Andric // The only interesting case is when we need to switch register classes.
1897f785676fSDimitry Andric if (SrcVT == MVT::i64) {
1898f785676fSDimitry Andric unsigned ResultReg = createResultReg(&PPC::GPRCRegClass);
189991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
190091bc56edSDimitry Andric TII.get(TargetOpcode::COPY),
1901f785676fSDimitry Andric ResultReg).addReg(SrcReg, 0, PPC::sub_32);
1902f785676fSDimitry Andric SrcReg = ResultReg;
1903f785676fSDimitry Andric }
1904f785676fSDimitry Andric
190539d628a0SDimitry Andric updateValueMap(I, SrcReg);
1906f785676fSDimitry Andric return true;
1907f785676fSDimitry Andric }
1908f785676fSDimitry Andric
1909f785676fSDimitry Andric // Attempt to fast-select an integer extend instruction.
SelectIntExt(const Instruction * I)1910f785676fSDimitry Andric bool PPCFastISel::SelectIntExt(const Instruction *I) {
1911f785676fSDimitry Andric Type *DestTy = I->getType();
1912f785676fSDimitry Andric Value *Src = I->getOperand(0);
1913f785676fSDimitry Andric Type *SrcTy = Src->getType();
1914f785676fSDimitry Andric
1915f785676fSDimitry Andric bool IsZExt = isa<ZExtInst>(I);
1916f785676fSDimitry Andric unsigned SrcReg = getRegForValue(Src);
1917f785676fSDimitry Andric if (!SrcReg) return false;
1918f785676fSDimitry Andric
1919f785676fSDimitry Andric EVT SrcEVT, DestEVT;
1920875ed548SDimitry Andric SrcEVT = TLI.getValueType(DL, SrcTy, true);
1921875ed548SDimitry Andric DestEVT = TLI.getValueType(DL, DestTy, true);
1922f785676fSDimitry Andric if (!SrcEVT.isSimple())
1923f785676fSDimitry Andric return false;
1924f785676fSDimitry Andric if (!DestEVT.isSimple())
1925f785676fSDimitry Andric return false;
1926f785676fSDimitry Andric
1927f785676fSDimitry Andric MVT SrcVT = SrcEVT.getSimpleVT();
1928f785676fSDimitry Andric MVT DestVT = DestEVT.getSimpleVT();
1929f785676fSDimitry Andric
1930f785676fSDimitry Andric // If we know the register class needed for the result of this
1931f785676fSDimitry Andric // instruction, use it. Otherwise pick the register class of the
1932f785676fSDimitry Andric // correct size that does not contain X0/R0, since we don't know
1933f785676fSDimitry Andric // whether downstream uses permit that assignment.
1934f785676fSDimitry Andric unsigned AssignedReg = FuncInfo.ValueMap[I];
1935f785676fSDimitry Andric const TargetRegisterClass *RC =
1936f785676fSDimitry Andric (AssignedReg ? MRI.getRegClass(AssignedReg) :
1937f785676fSDimitry Andric (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1938f785676fSDimitry Andric &PPC::GPRC_and_GPRC_NOR0RegClass));
1939f785676fSDimitry Andric unsigned ResultReg = createResultReg(RC);
1940f785676fSDimitry Andric
1941f785676fSDimitry Andric if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1942f785676fSDimitry Andric return false;
1943f785676fSDimitry Andric
194439d628a0SDimitry Andric updateValueMap(I, ResultReg);
1945f785676fSDimitry Andric return true;
1946f785676fSDimitry Andric }
1947f785676fSDimitry Andric
1948f785676fSDimitry Andric // Attempt to fast-select an instruction that wasn't handled by
1949f785676fSDimitry Andric // the table-generated machinery.
fastSelectInstruction(const Instruction * I)195039d628a0SDimitry Andric bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
1951f785676fSDimitry Andric
1952f785676fSDimitry Andric switch (I->getOpcode()) {
1953f785676fSDimitry Andric case Instruction::Load:
1954f785676fSDimitry Andric return SelectLoad(I);
1955f785676fSDimitry Andric case Instruction::Store:
1956f785676fSDimitry Andric return SelectStore(I);
1957f785676fSDimitry Andric case Instruction::Br:
1958f785676fSDimitry Andric return SelectBranch(I);
1959f785676fSDimitry Andric case Instruction::IndirectBr:
1960f785676fSDimitry Andric return SelectIndirectBr(I);
1961f785676fSDimitry Andric case Instruction::FPExt:
1962f785676fSDimitry Andric return SelectFPExt(I);
1963f785676fSDimitry Andric case Instruction::FPTrunc:
1964f785676fSDimitry Andric return SelectFPTrunc(I);
1965f785676fSDimitry Andric case Instruction::SIToFP:
1966f785676fSDimitry Andric return SelectIToFP(I, /*IsSigned*/ true);
1967f785676fSDimitry Andric case Instruction::UIToFP:
1968f785676fSDimitry Andric return SelectIToFP(I, /*IsSigned*/ false);
1969f785676fSDimitry Andric case Instruction::FPToSI:
1970f785676fSDimitry Andric return SelectFPToI(I, /*IsSigned*/ true);
1971f785676fSDimitry Andric case Instruction::FPToUI:
1972f785676fSDimitry Andric return SelectFPToI(I, /*IsSigned*/ false);
1973f785676fSDimitry Andric case Instruction::Add:
1974f785676fSDimitry Andric return SelectBinaryIntOp(I, ISD::ADD);
1975f785676fSDimitry Andric case Instruction::Or:
1976f785676fSDimitry Andric return SelectBinaryIntOp(I, ISD::OR);
1977f785676fSDimitry Andric case Instruction::Sub:
1978f785676fSDimitry Andric return SelectBinaryIntOp(I, ISD::SUB);
1979f785676fSDimitry Andric case Instruction::Call:
198039d628a0SDimitry Andric return selectCall(I);
1981f785676fSDimitry Andric case Instruction::Ret:
1982f785676fSDimitry Andric return SelectRet(I);
1983f785676fSDimitry Andric case Instruction::Trunc:
1984f785676fSDimitry Andric return SelectTrunc(I);
1985f785676fSDimitry Andric case Instruction::ZExt:
1986f785676fSDimitry Andric case Instruction::SExt:
1987f785676fSDimitry Andric return SelectIntExt(I);
1988f785676fSDimitry Andric // Here add other flavors of Instruction::XXX that automated
1989f785676fSDimitry Andric // cases don't catch. For example, switches are terminators
1990f785676fSDimitry Andric // that aren't yet handled.
1991f785676fSDimitry Andric default:
1992f785676fSDimitry Andric break;
1993f785676fSDimitry Andric }
1994f785676fSDimitry Andric return false;
1995f785676fSDimitry Andric }
1996f785676fSDimitry Andric
1997f785676fSDimitry Andric // Materialize a floating-point constant into a register, and return
1998f785676fSDimitry Andric // the register number (or zero if we failed to handle it).
PPCMaterializeFP(const ConstantFP * CFP,MVT VT)1999f785676fSDimitry Andric unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
2000f785676fSDimitry Andric // No plans to handle long double here.
2001f785676fSDimitry Andric if (VT != MVT::f32 && VT != MVT::f64)
2002f785676fSDimitry Andric return 0;
2003f785676fSDimitry Andric
2004f785676fSDimitry Andric // All FP constants are loaded from the constant pool.
200591bc56edSDimitry Andric unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
2006f785676fSDimitry Andric assert(Align > 0 && "Unexpectedly missing alignment information!");
2007f785676fSDimitry Andric unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
20084ba319b5SDimitry Andric const bool HasSPE = PPCSubTarget->hasSPE();
20094ba319b5SDimitry Andric const TargetRegisterClass *RC;
20104ba319b5SDimitry Andric if (HasSPE)
20114ba319b5SDimitry Andric RC = ((VT == MVT::f32) ? &PPC::SPE4RCRegClass : &PPC::SPERCRegClass);
20124ba319b5SDimitry Andric else
20134ba319b5SDimitry Andric RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
20144ba319b5SDimitry Andric
2015d88c1a5aSDimitry Andric unsigned DestReg = createResultReg(RC);
2016f785676fSDimitry Andric CodeModel::Model CModel = TM.getCodeModel();
2017f785676fSDimitry Andric
20187d523365SDimitry Andric MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
20197d523365SDimitry Andric MachinePointerInfo::getConstantPool(*FuncInfo.MF),
20207d523365SDimitry Andric MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
2021f785676fSDimitry Andric
20224ba319b5SDimitry Andric unsigned Opc;
20234ba319b5SDimitry Andric
20244ba319b5SDimitry Andric if (HasSPE)
20254ba319b5SDimitry Andric Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
20264ba319b5SDimitry Andric else
20274ba319b5SDimitry Andric Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
20284ba319b5SDimitry Andric
2029f785676fSDimitry Andric unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2030f785676fSDimitry Andric
2031ff0cc061SDimitry Andric PPCFuncInfo->setUsesTOCBasePtr();
2032f785676fSDimitry Andric // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
20332cab237bSDimitry Andric if (CModel == CodeModel::Small) {
203491bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
2035f785676fSDimitry Andric TmpReg)
2036f785676fSDimitry Andric .addConstantPoolIndex(Idx).addReg(PPC::X2);
203791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2038f785676fSDimitry Andric .addImm(0).addReg(TmpReg).addMemOperand(MMO);
2039f785676fSDimitry Andric } else {
2040f785676fSDimitry Andric // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)).
204191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
2042f785676fSDimitry Andric TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
2043f785676fSDimitry Andric // But for large code model, we must generate a LDtocL followed
2044f785676fSDimitry Andric // by the LF[SD].
2045f785676fSDimitry Andric if (CModel == CodeModel::Large) {
2046f785676fSDimitry Andric unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
204791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2048f785676fSDimitry Andric TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
204991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2050d88c1a5aSDimitry Andric .addImm(0)
2051d88c1a5aSDimitry Andric .addReg(TmpReg2);
2052f785676fSDimitry Andric } else
205391bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
2054f785676fSDimitry Andric .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
2055f785676fSDimitry Andric .addReg(TmpReg)
2056f785676fSDimitry Andric .addMemOperand(MMO);
2057f785676fSDimitry Andric }
2058f785676fSDimitry Andric
2059f785676fSDimitry Andric return DestReg;
2060f785676fSDimitry Andric }
2061f785676fSDimitry Andric
2062f785676fSDimitry Andric // Materialize the address of a global value into a register, and return
2063f785676fSDimitry Andric // the register number (or zero if we failed to handle it).
PPCMaterializeGV(const GlobalValue * GV,MVT VT)2064f785676fSDimitry Andric unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2065f785676fSDimitry Andric assert(VT == MVT::i64 && "Non-address!");
2066f785676fSDimitry Andric const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2067f785676fSDimitry Andric unsigned DestReg = createResultReg(RC);
2068f785676fSDimitry Andric
2069f785676fSDimitry Andric // Global values may be plain old object addresses, TLS object
2070f785676fSDimitry Andric // addresses, constant pool entries, or jump tables. How we generate
2071f785676fSDimitry Andric // code for these may depend on small, medium, or large code model.
2072f785676fSDimitry Andric CodeModel::Model CModel = TM.getCodeModel();
2073f785676fSDimitry Andric
2074f785676fSDimitry Andric // FIXME: Jump tables are not yet required because fast-isel doesn't
2075f785676fSDimitry Andric // handle switches; if that changes, we need them as well. For now,
2076f785676fSDimitry Andric // what follows assumes everything's a generic (or TLS) global address.
2077f785676fSDimitry Andric
2078f785676fSDimitry Andric // FIXME: We don't yet handle the complexity of TLS.
207991bc56edSDimitry Andric if (GV->isThreadLocal())
2080f785676fSDimitry Andric return 0;
2081f785676fSDimitry Andric
2082ff0cc061SDimitry Andric PPCFuncInfo->setUsesTOCBasePtr();
2083f785676fSDimitry Andric // For small code model, generate a simple TOC load.
20842cab237bSDimitry Andric if (CModel == CodeModel::Small)
208591bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
208691bc56edSDimitry Andric DestReg)
208791bc56edSDimitry Andric .addGlobalAddress(GV)
208891bc56edSDimitry Andric .addReg(PPC::X2);
2089f785676fSDimitry Andric else {
209091bc56edSDimitry Andric // If the address is an externally defined symbol, a symbol with common
209191bc56edSDimitry Andric // or externally available linkage, a non-local function address, or a
2092f785676fSDimitry Andric // jump table address (not yet needed), or if we are generating code
2093f785676fSDimitry Andric // for large code model, we generate:
20942cab237bSDimitry Andric // LDtocL(GV, ADDIStocHA(%x2, GV))
2095f785676fSDimitry Andric // Otherwise we generate:
20962cab237bSDimitry Andric // ADDItocL(ADDIStocHA(%x2, GV), GV)
2097f785676fSDimitry Andric // Either way, start with the ADDIStocHA:
2098f785676fSDimitry Andric unsigned HighPartReg = createResultReg(RC);
209991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA),
2100f785676fSDimitry Andric HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2101f785676fSDimitry Andric
21027d523365SDimitry Andric unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
21037d523365SDimitry Andric if (GVFlags & PPCII::MO_NLP_FLAG) {
210491bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2105f785676fSDimitry Andric DestReg).addGlobalAddress(GV).addReg(HighPartReg);
21067d523365SDimitry Andric } else {
2107f785676fSDimitry Andric // Otherwise generate the ADDItocL.
210891bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
2109f785676fSDimitry Andric DestReg).addReg(HighPartReg).addGlobalAddress(GV);
2110f785676fSDimitry Andric }
21117d523365SDimitry Andric }
2112f785676fSDimitry Andric
2113f785676fSDimitry Andric return DestReg;
2114f785676fSDimitry Andric }
2115f785676fSDimitry Andric
2116f785676fSDimitry Andric // Materialize a 32-bit integer constant into a register, and return
2117f785676fSDimitry Andric // the register number (or zero if we failed to handle it).
PPCMaterialize32BitInt(int64_t Imm,const TargetRegisterClass * RC)2118f785676fSDimitry Andric unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
2119f785676fSDimitry Andric const TargetRegisterClass *RC) {
2120f785676fSDimitry Andric unsigned Lo = Imm & 0xFFFF;
2121f785676fSDimitry Andric unsigned Hi = (Imm >> 16) & 0xFFFF;
2122f785676fSDimitry Andric
2123f785676fSDimitry Andric unsigned ResultReg = createResultReg(RC);
2124f785676fSDimitry Andric bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
2125f785676fSDimitry Andric
2126f785676fSDimitry Andric if (isInt<16>(Imm))
212791bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2128f785676fSDimitry Andric TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
2129f785676fSDimitry Andric .addImm(Imm);
2130f785676fSDimitry Andric else if (Lo) {
2131f785676fSDimitry Andric // Both Lo and Hi have nonzero bits.
2132f785676fSDimitry Andric unsigned TmpReg = createResultReg(RC);
213391bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2134f785676fSDimitry Andric TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
2135f785676fSDimitry Andric .addImm(Hi);
213691bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2137f785676fSDimitry Andric TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
2138f785676fSDimitry Andric .addReg(TmpReg).addImm(Lo);
2139f785676fSDimitry Andric } else
2140f785676fSDimitry Andric // Just Hi bits.
214191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2142f785676fSDimitry Andric TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
2143f785676fSDimitry Andric .addImm(Hi);
2144f785676fSDimitry Andric
2145f785676fSDimitry Andric return ResultReg;
2146f785676fSDimitry Andric }
2147f785676fSDimitry Andric
2148f785676fSDimitry Andric // Materialize a 64-bit integer constant into a register, and return
2149f785676fSDimitry Andric // the register number (or zero if we failed to handle it).
PPCMaterialize64BitInt(int64_t Imm,const TargetRegisterClass * RC)2150f785676fSDimitry Andric unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
2151f785676fSDimitry Andric const TargetRegisterClass *RC) {
2152f785676fSDimitry Andric unsigned Remainder = 0;
2153f785676fSDimitry Andric unsigned Shift = 0;
2154f785676fSDimitry Andric
2155f785676fSDimitry Andric // If the value doesn't fit in 32 bits, see if we can shift it
2156f785676fSDimitry Andric // so that it fits in 32 bits.
2157f785676fSDimitry Andric if (!isInt<32>(Imm)) {
2158f785676fSDimitry Andric Shift = countTrailingZeros<uint64_t>(Imm);
2159f785676fSDimitry Andric int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
2160f785676fSDimitry Andric
2161f785676fSDimitry Andric if (isInt<32>(ImmSh))
2162f785676fSDimitry Andric Imm = ImmSh;
2163f785676fSDimitry Andric else {
2164f785676fSDimitry Andric Remainder = Imm;
2165f785676fSDimitry Andric Shift = 32;
2166f785676fSDimitry Andric Imm >>= 32;
2167f785676fSDimitry Andric }
2168f785676fSDimitry Andric }
2169f785676fSDimitry Andric
2170f785676fSDimitry Andric // Handle the high-order 32 bits (if shifted) or the whole 32 bits
2171f785676fSDimitry Andric // (if not shifted).
2172f785676fSDimitry Andric unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
2173f785676fSDimitry Andric if (!Shift)
2174f785676fSDimitry Andric return TmpReg1;
2175f785676fSDimitry Andric
2176f785676fSDimitry Andric // If upper 32 bits were not zero, we've built them and need to shift
2177f785676fSDimitry Andric // them into place.
2178f785676fSDimitry Andric unsigned TmpReg2;
2179f785676fSDimitry Andric if (Imm) {
2180f785676fSDimitry Andric TmpReg2 = createResultReg(RC);
218191bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
2182f785676fSDimitry Andric TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
2183f785676fSDimitry Andric } else
2184f785676fSDimitry Andric TmpReg2 = TmpReg1;
2185f785676fSDimitry Andric
2186f785676fSDimitry Andric unsigned TmpReg3, Hi, Lo;
2187f785676fSDimitry Andric if ((Hi = (Remainder >> 16) & 0xFFFF)) {
2188f785676fSDimitry Andric TmpReg3 = createResultReg(RC);
218991bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
2190f785676fSDimitry Andric TmpReg3).addReg(TmpReg2).addImm(Hi);
2191f785676fSDimitry Andric } else
2192f785676fSDimitry Andric TmpReg3 = TmpReg2;
2193f785676fSDimitry Andric
2194f785676fSDimitry Andric if ((Lo = Remainder & 0xFFFF)) {
2195f785676fSDimitry Andric unsigned ResultReg = createResultReg(RC);
219691bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
2197f785676fSDimitry Andric ResultReg).addReg(TmpReg3).addImm(Lo);
2198f785676fSDimitry Andric return ResultReg;
2199f785676fSDimitry Andric }
2200f785676fSDimitry Andric
2201f785676fSDimitry Andric return TmpReg3;
2202f785676fSDimitry Andric }
2203f785676fSDimitry Andric
2204f785676fSDimitry Andric // Materialize an integer constant into a register, and return
2205f785676fSDimitry Andric // the register number (or zero if we failed to handle it).
PPCMaterializeInt(const ConstantInt * CI,MVT VT,bool UseSExt)22067d523365SDimitry Andric unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
22079cac79b3SDimitry Andric bool UseSExt) {
220891bc56edSDimitry Andric // If we're using CR bit registers for i1 values, handle that as a special
220991bc56edSDimitry Andric // case first.
221091bc56edSDimitry Andric if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
221191bc56edSDimitry Andric unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
221291bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
221391bc56edSDimitry Andric TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
221491bc56edSDimitry Andric return ImmReg;
221591bc56edSDimitry Andric }
2216f785676fSDimitry Andric
22173ca95b02SDimitry Andric if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
22183ca95b02SDimitry Andric VT != MVT::i1)
2219f785676fSDimitry Andric return 0;
2220f785676fSDimitry Andric
22213ca95b02SDimitry Andric const TargetRegisterClass *RC =
22223ca95b02SDimitry Andric ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
2223a8bcc4d8SDimitry Andric int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
2224f785676fSDimitry Andric
2225f785676fSDimitry Andric // If the constant is in range, use a load-immediate.
2226a8bcc4d8SDimitry Andric // Since LI will sign extend the constant we need to make sure that for
2227a8bcc4d8SDimitry Andric // our zeroext constants that the sign extended constant fits into 16-bits -
2228a8bcc4d8SDimitry Andric // a range of 0..0x7fff.
2229a8bcc4d8SDimitry Andric if (isInt<16>(Imm)) {
2230f785676fSDimitry Andric unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2231f785676fSDimitry Andric unsigned ImmReg = createResultReg(RC);
223291bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2233a8bcc4d8SDimitry Andric .addImm(Imm);
2234f785676fSDimitry Andric return ImmReg;
2235f785676fSDimitry Andric }
2236f785676fSDimitry Andric
2237f785676fSDimitry Andric // Construct the constant piecewise.
2238f785676fSDimitry Andric if (VT == MVT::i64)
2239f785676fSDimitry Andric return PPCMaterialize64BitInt(Imm, RC);
2240f785676fSDimitry Andric else if (VT == MVT::i32)
2241f785676fSDimitry Andric return PPCMaterialize32BitInt(Imm, RC);
2242f785676fSDimitry Andric
2243f785676fSDimitry Andric return 0;
2244f785676fSDimitry Andric }
2245f785676fSDimitry Andric
2246f785676fSDimitry Andric // Materialize a constant into a register, and return the register
2247f785676fSDimitry Andric // number (or zero if we failed to handle it).
fastMaterializeConstant(const Constant * C)224839d628a0SDimitry Andric unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
2249875ed548SDimitry Andric EVT CEVT = TLI.getValueType(DL, C->getType(), true);
2250f785676fSDimitry Andric
2251f785676fSDimitry Andric // Only handle simple types.
2252f785676fSDimitry Andric if (!CEVT.isSimple()) return 0;
2253f785676fSDimitry Andric MVT VT = CEVT.getSimpleVT();
2254f785676fSDimitry Andric
2255f785676fSDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2256f785676fSDimitry Andric return PPCMaterializeFP(CFP, VT);
2257f785676fSDimitry Andric else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2258f785676fSDimitry Andric return PPCMaterializeGV(GV, VT);
22597d523365SDimitry Andric else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2260d88c1a5aSDimitry Andric // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
2261d88c1a5aSDimitry Andric // assumes that constant PHI operands will be zero extended, and failure to
2262d88c1a5aSDimitry Andric // match that assumption will cause problems if we sign extend here but
2263d88c1a5aSDimitry Andric // some user of a PHI is in a block for which we fall back to full SDAG
2264d88c1a5aSDimitry Andric // instruction selection.
2265d88c1a5aSDimitry Andric return PPCMaterializeInt(CI, VT, false);
2266f785676fSDimitry Andric
2267f785676fSDimitry Andric return 0;
2268f785676fSDimitry Andric }
2269f785676fSDimitry Andric
2270f785676fSDimitry Andric // Materialize the address created by an alloca into a register, and
2271f785676fSDimitry Andric // return the register number (or zero if we failed to handle it).
fastMaterializeAlloca(const AllocaInst * AI)227239d628a0SDimitry Andric unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
2273f785676fSDimitry Andric // Don't handle dynamic allocas.
2274f785676fSDimitry Andric if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2275f785676fSDimitry Andric
2276f785676fSDimitry Andric MVT VT;
2277f785676fSDimitry Andric if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2278f785676fSDimitry Andric
2279f785676fSDimitry Andric DenseMap<const AllocaInst*, int>::iterator SI =
2280f785676fSDimitry Andric FuncInfo.StaticAllocaMap.find(AI);
2281f785676fSDimitry Andric
2282f785676fSDimitry Andric if (SI != FuncInfo.StaticAllocaMap.end()) {
2283f785676fSDimitry Andric unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
228491bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
2285f785676fSDimitry Andric ResultReg).addFrameIndex(SI->second).addImm(0);
2286f785676fSDimitry Andric return ResultReg;
2287f785676fSDimitry Andric }
2288f785676fSDimitry Andric
2289f785676fSDimitry Andric return 0;
2290f785676fSDimitry Andric }
2291f785676fSDimitry Andric
2292f785676fSDimitry Andric // Fold loads into extends when possible.
2293f785676fSDimitry Andric // FIXME: We can have multiple redundant extend/trunc instructions
2294f785676fSDimitry Andric // following a load. The folding only picks up one. Extend this
2295f785676fSDimitry Andric // to check subsequent instructions for the same pattern and remove
2296f785676fSDimitry Andric // them. Thus ResultReg should be the def reg for the last redundant
2297f785676fSDimitry Andric // instruction in a chain, and all intervening instructions can be
2298f785676fSDimitry Andric // removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
2299f785676fSDimitry Andric // to add ELF64-NOT: rldicl to the appropriate tests when this works.
tryToFoldLoadIntoMI(MachineInstr * MI,unsigned OpNo,const LoadInst * LI)2300f785676fSDimitry Andric bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2301f785676fSDimitry Andric const LoadInst *LI) {
2302f785676fSDimitry Andric // Verify we have a legal type before going any further.
2303f785676fSDimitry Andric MVT VT;
2304f785676fSDimitry Andric if (!isLoadTypeLegal(LI->getType(), VT))
2305f785676fSDimitry Andric return false;
2306f785676fSDimitry Andric
2307f785676fSDimitry Andric // Combine load followed by zero- or sign-extend.
2308f785676fSDimitry Andric bool IsZExt = false;
2309f785676fSDimitry Andric switch(MI->getOpcode()) {
2310f785676fSDimitry Andric default:
2311f785676fSDimitry Andric return false;
2312f785676fSDimitry Andric
2313f785676fSDimitry Andric case PPC::RLDICL:
2314f785676fSDimitry Andric case PPC::RLDICL_32_64: {
2315f785676fSDimitry Andric IsZExt = true;
2316f785676fSDimitry Andric unsigned MB = MI->getOperand(3).getImm();
2317f785676fSDimitry Andric if ((VT == MVT::i8 && MB <= 56) ||
2318f785676fSDimitry Andric (VT == MVT::i16 && MB <= 48) ||
2319f785676fSDimitry Andric (VT == MVT::i32 && MB <= 32))
2320f785676fSDimitry Andric break;
2321f785676fSDimitry Andric return false;
2322f785676fSDimitry Andric }
2323f785676fSDimitry Andric
2324f785676fSDimitry Andric case PPC::RLWINM:
2325f785676fSDimitry Andric case PPC::RLWINM8: {
2326f785676fSDimitry Andric IsZExt = true;
2327f785676fSDimitry Andric unsigned MB = MI->getOperand(3).getImm();
2328f785676fSDimitry Andric if ((VT == MVT::i8 && MB <= 24) ||
2329f785676fSDimitry Andric (VT == MVT::i16 && MB <= 16))
2330f785676fSDimitry Andric break;
2331f785676fSDimitry Andric return false;
2332f785676fSDimitry Andric }
2333f785676fSDimitry Andric
2334f785676fSDimitry Andric case PPC::EXTSB:
2335f785676fSDimitry Andric case PPC::EXTSB8:
2336f785676fSDimitry Andric case PPC::EXTSB8_32_64:
2337f785676fSDimitry Andric /* There is no sign-extending load-byte instruction. */
2338f785676fSDimitry Andric return false;
2339f785676fSDimitry Andric
2340f785676fSDimitry Andric case PPC::EXTSH:
2341f785676fSDimitry Andric case PPC::EXTSH8:
2342f785676fSDimitry Andric case PPC::EXTSH8_32_64: {
2343f785676fSDimitry Andric if (VT != MVT::i16 && VT != MVT::i8)
2344f785676fSDimitry Andric return false;
2345f785676fSDimitry Andric break;
2346f785676fSDimitry Andric }
2347f785676fSDimitry Andric
2348f785676fSDimitry Andric case PPC::EXTSW:
23495517e702SDimitry Andric case PPC::EXTSW_32:
2350f785676fSDimitry Andric case PPC::EXTSW_32_64: {
2351f785676fSDimitry Andric if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2352f785676fSDimitry Andric return false;
2353f785676fSDimitry Andric break;
2354f785676fSDimitry Andric }
2355f785676fSDimitry Andric }
2356f785676fSDimitry Andric
2357f785676fSDimitry Andric // See if we can handle this address.
2358f785676fSDimitry Andric Address Addr;
2359f785676fSDimitry Andric if (!PPCComputeAddress(LI->getOperand(0), Addr))
2360f785676fSDimitry Andric return false;
2361f785676fSDimitry Andric
2362f785676fSDimitry Andric unsigned ResultReg = MI->getOperand(0).getReg();
2363f785676fSDimitry Andric
23644ba319b5SDimitry Andric if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
23654ba319b5SDimitry Andric PPCSubTarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
2366f785676fSDimitry Andric return false;
2367f785676fSDimitry Andric
2368*b5893f02SDimitry Andric MachineBasicBlock::iterator I(MI);
2369*b5893f02SDimitry Andric removeDeadCode(I, std::next(I));
2370f785676fSDimitry Andric return true;
2371f785676fSDimitry Andric }
2372f785676fSDimitry Andric
2373f785676fSDimitry Andric // Attempt to lower call arguments in a faster way than done by
2374f785676fSDimitry Andric // the selection DAG code.
fastLowerArguments()237539d628a0SDimitry Andric bool PPCFastISel::fastLowerArguments() {
2376f785676fSDimitry Andric // Defer to normal argument lowering for now. It's reasonably
2377f785676fSDimitry Andric // efficient. Consider doing something like ARM to handle the
2378f785676fSDimitry Andric // case where all args fit in registers, no varargs, no float
2379f785676fSDimitry Andric // or vector args.
2380f785676fSDimitry Andric return false;
2381f785676fSDimitry Andric }
2382f785676fSDimitry Andric
2383f785676fSDimitry Andric // Handle materializing integer constants into a register. This is not
2384f785676fSDimitry Andric // automatically generated for PowerPC, so must be explicitly created here.
fastEmit_i(MVT Ty,MVT VT,unsigned Opc,uint64_t Imm)238539d628a0SDimitry Andric unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
2386f785676fSDimitry Andric
2387f785676fSDimitry Andric if (Opc != ISD::Constant)
2388f785676fSDimitry Andric return 0;
2389f785676fSDimitry Andric
239091bc56edSDimitry Andric // If we're using CR bit registers for i1 values, handle that as a special
239191bc56edSDimitry Andric // case first.
239291bc56edSDimitry Andric if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
239391bc56edSDimitry Andric unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
239491bc56edSDimitry Andric BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
239591bc56edSDimitry Andric TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
239691bc56edSDimitry Andric return ImmReg;
239791bc56edSDimitry Andric }
239891bc56edSDimitry Andric
2399d88c1a5aSDimitry Andric if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
2400d88c1a5aSDimitry Andric VT != MVT::i1)
2401f785676fSDimitry Andric return 0;
2402f785676fSDimitry Andric
2403f785676fSDimitry Andric const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2404f785676fSDimitry Andric &PPC::GPRCRegClass);
2405f785676fSDimitry Andric if (VT == MVT::i64)
2406f785676fSDimitry Andric return PPCMaterialize64BitInt(Imm, RC);
2407f785676fSDimitry Andric else
2408f785676fSDimitry Andric return PPCMaterialize32BitInt(Imm, RC);
2409f785676fSDimitry Andric }
2410f785676fSDimitry Andric
2411f785676fSDimitry Andric // Override for ADDI and ADDI8 to set the correct register class
2412f785676fSDimitry Andric // on RHS operand 0. The automatic infrastructure naively assumes
2413f785676fSDimitry Andric // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2414f785676fSDimitry Andric // for these cases. At the moment, none of the other automatically
2415f785676fSDimitry Andric // generated RI instructions require special treatment. However, once
2416f785676fSDimitry Andric // SelectSelect is implemented, "isel" requires similar handling.
2417f785676fSDimitry Andric //
2418f785676fSDimitry Andric // Also be conservative about the output register class. Avoid
2419f785676fSDimitry Andric // assigning R0 or X0 to the output register for GPRC and G8RC
2420f785676fSDimitry Andric // register classes, as any such result could be used in ADDI, etc.,
2421f785676fSDimitry Andric // where those regs have another meaning.
fastEmitInst_ri(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill,uint64_t Imm)242239d628a0SDimitry Andric unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
2423f785676fSDimitry Andric const TargetRegisterClass *RC,
2424f785676fSDimitry Andric unsigned Op0, bool Op0IsKill,
2425f785676fSDimitry Andric uint64_t Imm) {
2426f785676fSDimitry Andric if (MachineInstOpcode == PPC::ADDI)
2427f785676fSDimitry Andric MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2428f785676fSDimitry Andric else if (MachineInstOpcode == PPC::ADDI8)
2429f785676fSDimitry Andric MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2430f785676fSDimitry Andric
2431f785676fSDimitry Andric const TargetRegisterClass *UseRC =
2432f785676fSDimitry Andric (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2433f785676fSDimitry Andric (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2434f785676fSDimitry Andric
243539d628a0SDimitry Andric return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC,
2436f785676fSDimitry Andric Op0, Op0IsKill, Imm);
2437f785676fSDimitry Andric }
2438f785676fSDimitry Andric
2439f785676fSDimitry Andric // Override for instructions with one register operand to avoid use of
2440f785676fSDimitry Andric // R0/X0. The automatic infrastructure isn't aware of the context so
2441f785676fSDimitry Andric // we must be conservative.
fastEmitInst_r(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill)244239d628a0SDimitry Andric unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
2443f785676fSDimitry Andric const TargetRegisterClass* RC,
2444f785676fSDimitry Andric unsigned Op0, bool Op0IsKill) {
2445f785676fSDimitry Andric const TargetRegisterClass *UseRC =
2446f785676fSDimitry Andric (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2447f785676fSDimitry Andric (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2448f785676fSDimitry Andric
244939d628a0SDimitry Andric return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill);
2450f785676fSDimitry Andric }
2451f785676fSDimitry Andric
2452f785676fSDimitry Andric // Override for instructions with two register operands to avoid use
2453f785676fSDimitry Andric // of R0/X0. The automatic infrastructure isn't aware of the context
2454f785676fSDimitry Andric // so we must be conservative.
fastEmitInst_rr(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,bool Op0IsKill,unsigned Op1,bool Op1IsKill)245539d628a0SDimitry Andric unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2456f785676fSDimitry Andric const TargetRegisterClass* RC,
2457f785676fSDimitry Andric unsigned Op0, bool Op0IsKill,
2458f785676fSDimitry Andric unsigned Op1, bool Op1IsKill) {
2459f785676fSDimitry Andric const TargetRegisterClass *UseRC =
2460f785676fSDimitry Andric (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2461f785676fSDimitry Andric (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2462f785676fSDimitry Andric
246339d628a0SDimitry Andric return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill,
2464f785676fSDimitry Andric Op1, Op1IsKill);
2465f785676fSDimitry Andric }
2466f785676fSDimitry Andric
2467f785676fSDimitry Andric namespace llvm {
2468f785676fSDimitry Andric // Create the fast instruction selector for PowerPC64 ELF.
createFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)2469f785676fSDimitry Andric FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
2470f785676fSDimitry Andric const TargetLibraryInfo *LibInfo) {
2471f785676fSDimitry Andric // Only available on 64-bit ELF for now.
2472ff0cc061SDimitry Andric const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
2473ff0cc061SDimitry Andric if (Subtarget.isPPC64() && Subtarget.isSVR4ABI())
2474f785676fSDimitry Andric return new PPCFastISel(FuncInfo, LibInfo);
247591bc56edSDimitry Andric return nullptr;
2476f785676fSDimitry Andric }
24773dac3a9bSDimitry Andric }
2478