blob: acb448569c40a596bf65fa36649eb0a3af34089f [file] [log] [blame]
Eric Christopherab695882010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherd10cd7b2010-09-10 23:18:12 +000018#include "ARMCallingConv.h"
Eric Christopherab695882010-07-21 22:26:11 +000019#include "ARMRegisterInfo.h"
20#include "ARMTargetMachine.h"
21#include "ARMSubtarget.h"
Eric Christopherc9932f62010-10-01 23:24:42 +000022#include "ARMConstantPoolValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000023#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Instructions.h"
27#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000028#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000029#include "llvm/CodeGen/Analysis.h"
30#include "llvm/CodeGen/FastISel.h"
31#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000034#include "llvm/CodeGen/MachineConstantPool.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000036#include "llvm/CodeGen/MachineMemOperand.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopherd56d61a2010-10-17 01:51:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopherab695882010-07-21 22:26:11 +000039#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000040#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000041#include "llvm/Support/ErrorHandling.h"
42#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000043#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetLowering.h"
46#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000047#include "llvm/Target/TargetOptions.h"
48using namespace llvm;
49
Eric Christopher038fea52010-08-17 00:46:57 +000050static cl::opt<bool>
Eric Christopher6e5367d2010-10-18 22:53:53 +000051DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
Eric Christopherfeadddd2010-10-11 20:05:22 +000053 cl::init(false), cl::Hidden);
Eric Christopher038fea52010-08-17 00:46:57 +000054
Eric Christopher836c6242010-12-15 23:47:29 +000055extern cl::opt<bool> EnableARMLongCalls;
56
Eric Christopherab695882010-07-21 22:26:11 +000057namespace {
Eric Christopher827656d2010-11-20 22:38:27 +000058
Eric Christopher0d581222010-11-19 22:30:02 +000059 // All possible address modes, plus some.
60 typedef struct Address {
61 enum {
62 RegBase,
63 FrameIndexBase
64 } BaseType;
Eric Christopher827656d2010-11-20 22:38:27 +000065
Eric Christopher0d581222010-11-19 22:30:02 +000066 union {
67 unsigned Reg;
68 int FI;
69 } Base;
Eric Christopher827656d2010-11-20 22:38:27 +000070
Eric Christopher0d581222010-11-19 22:30:02 +000071 int Offset;
72 unsigned Scale;
73 unsigned PlusReg;
Eric Christopher827656d2010-11-20 22:38:27 +000074
Eric Christopher0d581222010-11-19 22:30:02 +000075 // Innocuous defaults for our address.
76 Address()
77 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
78 Base.Reg = 0;
79 }
80 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000081
82class ARMFastISel : public FastISel {
83
84 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
85 /// make the right decision when generating code for different targets.
86 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000087 const TargetMachine &TM;
88 const TargetInstrInfo &TII;
89 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000090 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000091
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000093 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000094 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000095
Eric Christopherab695882010-07-21 22:26:11 +000096 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000097 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000098 : FastISel(funcInfo),
99 TM(funcInfo.MF->getTarget()),
100 TII(*TM.getInstrInfo()),
101 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000102 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000103 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000104 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000105 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000106 }
107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000109 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC);
111 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
112 const TargetRegisterClass *RC,
113 unsigned Op0, bool Op0IsKill);
114 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
115 const TargetRegisterClass *RC,
116 unsigned Op0, bool Op0IsKill,
117 unsigned Op1, bool Op1IsKill);
118 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
121 uint64_t Imm);
122 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 const ConstantFP *FPImm);
Eric Christopher0fe7d542010-08-17 01:25:29 +0000126 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 unsigned Op1, bool Op1IsKill,
130 uint64_t Imm);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000131 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
133 uint64_t Imm);
134
Eric Christopher0fe7d542010-08-17 01:25:29 +0000135 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
136 unsigned Op0, bool Op0IsKill,
137 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000138
Eric Christophercb592292010-08-20 00:20:31 +0000139 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000140 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000141 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000142 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000143
144 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000145
Eric Christopher83007122010-08-23 21:44:12 +0000146 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000147 private:
Eric Christopher17787722010-10-21 21:47:51 +0000148 bool SelectLoad(const Instruction *I);
149 bool SelectStore(const Instruction *I);
150 bool SelectBranch(const Instruction *I);
151 bool SelectCmp(const Instruction *I);
152 bool SelectFPExt(const Instruction *I);
153 bool SelectFPTrunc(const Instruction *I);
154 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
155 bool SelectSIToFP(const Instruction *I);
156 bool SelectFPToSI(const Instruction *I);
157 bool SelectSDiv(const Instruction *I);
158 bool SelectSRem(const Instruction *I);
159 bool SelectCall(const Instruction *I);
160 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000161 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000162
Eric Christopher83007122010-08-23 21:44:12 +0000163 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000164 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000165 bool isTypeLegal(const Type *Ty, MVT &VT);
166 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000167 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
168 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
169 bool ARMComputeAddress(const Value *Obj, Address &Addr);
170 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000171 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000172 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000173 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000174 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000175 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000176 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000177
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000178 // Call handling routines.
179 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000180 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
181 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000182 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000183 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000184 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000185 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000186 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
187 SmallVectorImpl<unsigned> &RegArgs,
188 CallingConv::ID CC,
189 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000190 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000191 const Instruction *I, CallingConv::ID CC,
192 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000193 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000194
195 // OptionalDef handling routines.
196 private:
Eric Christopheraf3dce52011-03-12 01:09:29 +0000197 bool isARMNEONPred(const MachineInstr *MI);
Eric Christopher456144e2010-08-19 00:37:05 +0000198 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
199 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000200 void AddLoadStoreOperands(EVT VT, Address &Addr,
201 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000202};
Eric Christopherab695882010-07-21 22:26:11 +0000203
204} // end anonymous namespace
205
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000206#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000207
Eric Christopher456144e2010-08-19 00:37:05 +0000208// DefinesOptionalPredicate - This is different from DefinesPredicate in that
209// we don't care about implicit defs here, just places we'll need to add a
210// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
211bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
212 const TargetInstrDesc &TID = MI->getDesc();
213 if (!TID.hasOptionalDef())
214 return false;
215
216 // Look to see if our OptionalDef is defining CPSR or CCR.
217 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
218 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000219 if (!MO.isReg() || !MO.isDef()) continue;
220 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000221 *CPSR = true;
222 }
223 return true;
224}
225
Eric Christopheraf3dce52011-03-12 01:09:29 +0000226bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
227 const TargetInstrDesc &TID = MI->getDesc();
228
229 // If we're a thumb2 or not NEON function we were handled via isPredicable.
230 if ((TID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
231 AFI->isThumb2Function())
232 return false;
233
234 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i)
235 if (TID.OpInfo[i].isPredicate())
236 return true;
237
238 return false;
239}
240
Eric Christopher456144e2010-08-19 00:37:05 +0000241// If the machine is predicable go ahead and add the predicate operands, if
242// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000243// TODO: If we want to support thumb1 then we'll need to deal with optional
244// CPSR defs that need to be added before the remaining operands. See s_cc_out
245// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000246const MachineInstrBuilder &
247ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
248 MachineInstr *MI = &*MIB;
249
Eric Christopheraf3dce52011-03-12 01:09:29 +0000250 // Do we use a predicate? or...
251 // Are we NEON in ARM mode and have a predicate operand? If so, I know
252 // we're not predicable but add it anyways.
253 if (TII.isPredicable(MI) || isARMNEONPred(MI))
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddDefaultPred(MIB);
Eric Christopheraf3dce52011-03-12 01:09:29 +0000255
Eric Christopher456144e2010-08-19 00:37:05 +0000256 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
257 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000258 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000259 if (DefinesOptionalPredicate(MI, &CPSR)) {
260 if (CPSR)
261 AddDefaultT1CC(MIB);
262 else
263 AddDefaultCC(MIB);
264 }
265 return MIB;
266}
267
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
269 const TargetRegisterClass* RC) {
270 unsigned ResultReg = createResultReg(RC);
271 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
272
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274 return ResultReg;
275}
276
277unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
278 const TargetRegisterClass *RC,
279 unsigned Op0, bool Op0IsKill) {
280 unsigned ResultReg = createResultReg(RC);
281 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
282
283 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000284 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000285 .addReg(Op0, Op0IsKill * RegState::Kill));
286 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 TII.get(TargetOpcode::COPY), ResultReg)
291 .addReg(II.ImplicitDefs[0]));
292 }
293 return ResultReg;
294}
295
296unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill,
299 unsigned Op1, bool Op1IsKill) {
300 unsigned ResultReg = createResultReg(RC);
301 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
302
303 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill)
306 .addReg(Op1, Op1IsKill * RegState::Kill));
307 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addReg(Op0, Op0IsKill * RegState::Kill)
310 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
314 }
315 return ResultReg;
316}
317
318unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 uint64_t Imm) {
322 unsigned ResultReg = createResultReg(RC);
323 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
324
325 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addImm(Imm));
329 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
336 }
337 return ResultReg;
338}
339
340unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 const ConstantFP *FPImm) {
344 unsigned ResultReg = createResultReg(RC);
345 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
346
347 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000348 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000349 .addReg(Op0, Op0IsKill * RegState::Kill)
350 .addFPImm(FPImm));
351 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000356 TII.get(TargetOpcode::COPY), ResultReg)
357 .addReg(II.ImplicitDefs[0]));
358 }
359 return ResultReg;
360}
361
362unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
363 const TargetRegisterClass *RC,
364 unsigned Op0, bool Op0IsKill,
365 unsigned Op1, bool Op1IsKill,
366 uint64_t Imm) {
367 unsigned ResultReg = createResultReg(RC);
368 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
369
370 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000372 .addReg(Op0, Op0IsKill * RegState::Kill)
373 .addReg(Op1, Op1IsKill * RegState::Kill)
374 .addImm(Imm));
375 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000377 .addReg(Op0, Op0IsKill * RegState::Kill)
378 .addReg(Op1, Op1IsKill * RegState::Kill)
379 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000381 TII.get(TargetOpcode::COPY), ResultReg)
382 .addReg(II.ImplicitDefs[0]));
383 }
384 return ResultReg;
385}
386
387unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
388 const TargetRegisterClass *RC,
389 uint64_t Imm) {
390 unsigned ResultReg = createResultReg(RC);
391 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000392
Eric Christopher0fe7d542010-08-17 01:25:29 +0000393 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000394 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000395 .addImm(Imm));
396 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000398 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000400 TII.get(TargetOpcode::COPY), ResultReg)
401 .addReg(II.ImplicitDefs[0]));
402 }
403 return ResultReg;
404}
405
406unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
407 unsigned Op0, bool Op0IsKill,
408 uint32_t Idx) {
409 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
410 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
411 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000412 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000413 DL, TII.get(TargetOpcode::COPY), ResultReg)
414 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
415 return ResultReg;
416}
417
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000418// TODO: Don't worry about 64-bit now, but when this is fixed remove the
419// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000420unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000421 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000422
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000423 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
425 TII.get(ARM::VMOVRS), MoveReg)
426 .addReg(SrcReg));
427 return MoveReg;
428}
429
430unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000431 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000432
Eric Christopheraa3ace12010-09-09 20:49:25 +0000433 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
434 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000435 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000436 .addReg(SrcReg));
437 return MoveReg;
438}
439
Eric Christopher9ed58df2010-09-09 00:19:41 +0000440// For double width floating point we need to materialize two constants
441// (the high and the low) into integer registers then use a move to get
442// the combined constant into an FP reg.
443unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
444 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000445 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000446
Eric Christopher9ed58df2010-09-09 00:19:41 +0000447 // This checks to see if we can use VFP3 instructions to materialize
448 // a constant, otherwise we have to go through the constant pool.
449 if (TLI.isFPImmLegal(Val, VT)) {
450 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
451 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
452 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
453 DestReg)
454 .addFPImm(CFP));
455 return DestReg;
456 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000457
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000458 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000459 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher238bb162010-09-09 23:50:00 +0000461 // MachineConstantPool wants an explicit alignment.
462 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
463 if (Align == 0) {
464 // TODO: Figure out if this is correct.
465 Align = TD.getTypeAllocSize(CFP->getType());
466 }
467 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
468 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
469 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000470
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000471 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
473 DestReg)
474 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000475 .addReg(0));
476 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000477}
478
Eric Christopher744c7c82010-09-28 22:47:54 +0000479unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000480
Eric Christopher744c7c82010-09-28 22:47:54 +0000481 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000482 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000483
Eric Christophere5b13cf2010-11-03 20:21:17 +0000484 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
485
486 // If we can do this in a single instruction without a constant pool entry
487 // do so now.
488 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000489 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000490 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
491 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000492 TII.get(Opc), DestReg)
493 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000494 return DestReg;
495 }
496
Eric Christopher56d2b722010-09-02 23:43:26 +0000497 // MachineConstantPool wants an explicit alignment.
498 unsigned Align = TD.getPrefTypeAlignment(C->getType());
499 if (Align == 0) {
500 // TODO: Figure out if this is correct.
501 Align = TD.getTypeAllocSize(C->getType());
502 }
503 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000504
Eric Christopher56d2b722010-09-02 23:43:26 +0000505 if (isThumb)
506 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000507 TII.get(ARM::t2LDRpci), DestReg)
508 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000509 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000510 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000511 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000512 TII.get(ARM::LDRcp), DestReg)
513 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000514 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000515
Eric Christopher56d2b722010-09-02 23:43:26 +0000516 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000517}
518
Eric Christopherc9932f62010-10-01 23:24:42 +0000519unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000520 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000521 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000522
Eric Christopher890dbbe2010-10-02 00:32:44 +0000523 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000524
Eric Christopher890dbbe2010-10-02 00:32:44 +0000525 // TODO: No external globals for now.
526 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000527
Eric Christopher890dbbe2010-10-02 00:32:44 +0000528 // TODO: Need more magic for ARM PIC.
529 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000530
Eric Christopher890dbbe2010-10-02 00:32:44 +0000531 // MachineConstantPool wants an explicit alignment.
532 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
533 if (Align == 0) {
534 // TODO: Figure out if this is correct.
535 Align = TD.getTypeAllocSize(GV->getType());
536 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000537
Eric Christopher890dbbe2010-10-02 00:32:44 +0000538 // Grab index.
539 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000540 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000541 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
542 ARMCP::CPValue, PCAdj);
543 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000544
Eric Christopher890dbbe2010-10-02 00:32:44 +0000545 // Load value.
546 MachineInstrBuilder MIB;
547 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
548 if (isThumb) {
549 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
550 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
551 .addConstantPoolIndex(Idx);
552 if (RelocM == Reloc::PIC_)
553 MIB.addImm(Id);
554 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000555 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000556 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
557 DestReg)
558 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000559 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000560 }
561 AddOptionalDefs(MIB);
562 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000563}
564
Eric Christopher9ed58df2010-09-09 00:19:41 +0000565unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
566 EVT VT = TLI.getValueType(C->getType(), true);
567
568 // Only handle simple types.
569 if (!VT.isSimple()) return 0;
570
571 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
572 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000573 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
574 return ARMMaterializeGV(GV, VT);
575 else if (isa<ConstantInt>(C))
576 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000577
Eric Christopherc9932f62010-10-01 23:24:42 +0000578 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000579}
580
Eric Christopherf9764fa2010-09-30 20:49:44 +0000581unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
582 // Don't handle dynamic allocas.
583 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000584
Duncan Sands1440e8b2010-11-03 11:35:31 +0000585 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000586 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000587
Eric Christopherf9764fa2010-09-30 20:49:44 +0000588 DenseMap<const AllocaInst*, int>::iterator SI =
589 FuncInfo.StaticAllocaMap.find(AI);
590
591 // This will get lowered later into the correct offsets and registers
592 // via rewriteXFrameIndex.
593 if (SI != FuncInfo.StaticAllocaMap.end()) {
594 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
595 unsigned ResultReg = createResultReg(RC);
596 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
598 TII.get(Opc), ResultReg)
599 .addFrameIndex(SI->second)
600 .addImm(0));
601 return ResultReg;
602 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000603
Eric Christopherf9764fa2010-09-30 20:49:44 +0000604 return 0;
605}
606
Duncan Sands1440e8b2010-11-03 11:35:31 +0000607bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
608 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000609
Eric Christopherb1cc8482010-08-25 07:23:49 +0000610 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000611 if (evt == MVT::Other || !evt.isSimple()) return false;
612 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000613
Eric Christopherdc908042010-08-31 01:28:42 +0000614 // Handle all legal types, i.e. a register that will directly hold this
615 // value.
616 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000617}
618
Duncan Sands1440e8b2010-11-03 11:35:31 +0000619bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000620 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000621
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000622 // If this is a type than can be sign or zero-extended to a basic operation
623 // go ahead and accept it now.
624 if (VT == MVT::i8 || VT == MVT::i16)
625 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000626
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000627 return false;
628}
629
Eric Christopher88de86b2010-11-19 22:36:41 +0000630// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000631bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000632 // Some boilerplate from the X86 FastISel.
633 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000634 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000635 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000636 // Don't walk into other basic blocks unless the object is an alloca from
637 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000638 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
639 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
640 Opcode = I->getOpcode();
641 U = I;
642 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000643 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000644 Opcode = C->getOpcode();
645 U = C;
646 }
647
Eric Christophercb0b04b2010-08-24 00:07:24 +0000648 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000649 if (Ty->getAddressSpace() > 255)
650 // Fast instruction selection doesn't support the special
651 // address spaces.
652 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000653
Eric Christopher83007122010-08-23 21:44:12 +0000654 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000655 default:
Eric Christopher83007122010-08-23 21:44:12 +0000656 break;
Eric Christopher55324332010-10-12 00:43:21 +0000657 case Instruction::BitCast: {
658 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000659 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000660 }
661 case Instruction::IntToPtr: {
662 // Look past no-op inttoptrs.
663 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000664 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000665 break;
666 }
667 case Instruction::PtrToInt: {
668 // Look past no-op ptrtoints.
669 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000670 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000671 break;
672 }
Eric Christophereae84392010-10-14 09:29:41 +0000673 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000674 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000675 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000676
Eric Christophereae84392010-10-14 09:29:41 +0000677 // Iterate through the GEP folding the constants into offsets where
678 // we can.
679 gep_type_iterator GTI = gep_type_begin(U);
680 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
681 i != e; ++i, ++GTI) {
682 const Value *Op = *i;
683 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
684 const StructLayout *SL = TD.getStructLayout(STy);
685 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
686 TmpOffset += SL->getElementOffset(Idx);
687 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000688 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
689 SmallVector<const Value *, 4> Worklist;
690 Worklist.push_back(Op);
691 do {
692 Op = Worklist.pop_back_val();
693 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
694 // Constant-offset addressing.
695 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000696 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000697 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
698 // An add with a constant operand. Fold the constant.
699 ConstantInt *CI =
700 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
701 TmpOffset += CI->getSExtValue() * S;
702 // Add the other operand back to the work list.
703 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
704 } else
705 goto unsupported_gep;
706 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000707 }
708 }
Eric Christopher2896df82010-10-15 18:02:07 +0000709
710 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000711 Addr.Offset = TmpOffset;
712 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000713
714 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000715 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000716
Eric Christophereae84392010-10-14 09:29:41 +0000717 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000718 break;
719 }
Eric Christopher83007122010-08-23 21:44:12 +0000720 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000721 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000722 DenseMap<const AllocaInst*, int>::iterator SI =
723 FuncInfo.StaticAllocaMap.find(AI);
724 if (SI != FuncInfo.StaticAllocaMap.end()) {
725 Addr.BaseType = Address::FrameIndexBase;
726 Addr.Base.FI = SI->second;
727 return true;
728 }
729 break;
Eric Christopher83007122010-08-23 21:44:12 +0000730 }
731 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000732
Eric Christophera9c57512010-10-13 21:41:51 +0000733 // Materialize the global variable's address into a reg which can
734 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000735 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000736 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
737 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000738
Eric Christopher0d581222010-11-19 22:30:02 +0000739 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000740 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000741 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000742
Eric Christophercb0b04b2010-08-24 00:07:24 +0000743 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000744 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
745 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000746}
747
Eric Christopher0d581222010-11-19 22:30:02 +0000748void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000749
Eric Christopher212ae932010-10-21 19:40:30 +0000750 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000751
Eric Christopher212ae932010-10-21 19:40:30 +0000752 bool needsLowering = false;
753 switch (VT.getSimpleVT().SimpleTy) {
754 default:
755 assert(false && "Unhandled load/store type!");
756 case MVT::i1:
757 case MVT::i8:
758 case MVT::i16:
759 case MVT::i32:
760 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000761 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000762 break;
763 case MVT::f32:
764 case MVT::f64:
765 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000766 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000767 break;
768 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000769
Eric Christopher827656d2010-11-20 22:38:27 +0000770 // If this is a stack pointer and the offset needs to be simplified then
771 // put the alloca address into a register, set the base type back to
772 // register and continue. This should almost never happen.
773 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
774 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
775 ARM::GPRRegisterClass;
776 unsigned ResultReg = createResultReg(RC);
777 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
778 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
779 TII.get(Opc), ResultReg)
780 .addFrameIndex(Addr.Base.FI)
781 .addImm(0));
782 Addr.Base.Reg = ResultReg;
783 Addr.BaseType = Address::RegBase;
784 }
785
Eric Christopher212ae932010-10-21 19:40:30 +0000786 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000787 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000788 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000789 ARMCC::CondCodes Pred = ARMCC::AL;
790 unsigned PredReg = 0;
791
Eric Christopher2896df82010-10-15 18:02:07 +0000792 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
793 ARM::GPRRegisterClass;
794 unsigned BaseReg = createResultReg(RC);
795
Eric Christophereaa204b2010-09-02 01:39:14 +0000796 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000797 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000798 BaseReg, Addr.Base.Reg, Addr.Offset,
799 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000800 static_cast<const ARMBaseInstrInfo&>(TII));
801 else {
802 assert(AFI->isThumb2Function());
803 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000804 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000805 static_cast<const ARMBaseInstrInfo&>(TII));
806 }
Eric Christopher0d581222010-11-19 22:30:02 +0000807 Addr.Offset = 0;
808 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000809 }
Eric Christopher83007122010-08-23 21:44:12 +0000810}
811
Eric Christopher564857f2010-12-01 01:40:24 +0000812void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
813 const MachineInstrBuilder &MIB) {
814 // addrmode5 output depends on the selection dag addressing dividing the
815 // offset by 4 that it then later multiplies. Do this here as well.
816 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
817 VT.getSimpleVT().SimpleTy == MVT::f64)
818 Addr.Offset /= 4;
819
820 // Frame base works a bit differently. Handle it separately.
821 if (Addr.BaseType == Address::FrameIndexBase) {
822 int FI = Addr.Base.FI;
823 int Offset = Addr.Offset;
824 MachineMemOperand *MMO =
825 FuncInfo.MF->getMachineMemOperand(
826 MachinePointerInfo::getFixedStack(FI, Offset),
827 MachineMemOperand::MOLoad,
828 MFI.getObjectSize(FI),
829 MFI.getObjectAlignment(FI));
830 // Now add the rest of the operands.
831 MIB.addFrameIndex(FI);
832
833 // ARM halfword load/stores need an additional operand.
834 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
835
836 MIB.addImm(Addr.Offset);
837 MIB.addMemOperand(MMO);
838 } else {
839 // Now add the rest of the operands.
840 MIB.addReg(Addr.Base.Reg);
841
842 // ARM halfword load/stores need an additional operand.
843 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
844
845 MIB.addImm(Addr.Offset);
846 }
847 AddOptionalDefs(MIB);
848}
849
Eric Christopher0d581222010-11-19 22:30:02 +0000850bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000851
Eric Christopherb1cc8482010-08-25 07:23:49 +0000852 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000853 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000854 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000855 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000856 // This is mostly going to be Neon/vector support.
857 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000858 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000859 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000860 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000861 break;
862 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000863 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000864 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000865 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000866 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000867 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000868 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000869 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000870 case MVT::f32:
871 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000872 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000873 break;
874 case MVT::f64:
875 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000876 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000877 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000878 }
Eric Christopher564857f2010-12-01 01:40:24 +0000879 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000880 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000881
Eric Christopher564857f2010-12-01 01:40:24 +0000882 // Create the base instruction, then add the operands.
883 ResultReg = createResultReg(RC);
884 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
885 TII.get(Opc), ResultReg);
886 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000887 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000888}
889
Eric Christopher43b62be2010-09-27 06:02:23 +0000890bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000891 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000892 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000893 if (!isLoadTypeLegal(I->getType(), VT))
894 return false;
895
Eric Christopher564857f2010-12-01 01:40:24 +0000896 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000897 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000898 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000899
900 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000901 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000902 UpdateValueMap(I, ResultReg);
903 return true;
904}
905
Eric Christopher0d581222010-11-19 22:30:02 +0000906bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000907 unsigned StrOpc;
908 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000909 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000910 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000911 case MVT::i1: {
912 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
913 ARM::GPRRegisterClass);
914 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
915 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
916 TII.get(Opc), Res)
917 .addReg(SrcReg).addImm(1));
918 SrcReg = Res;
919 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000920 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000921 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000922 break;
923 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000924 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000925 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000926 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000927 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000928 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000929 case MVT::f32:
930 if (!Subtarget->hasVFP2()) return false;
931 StrOpc = ARM::VSTRS;
932 break;
933 case MVT::f64:
934 if (!Subtarget->hasVFP2()) return false;
935 StrOpc = ARM::VSTRD;
936 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000937 }
Eric Christopher564857f2010-12-01 01:40:24 +0000938 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000939 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000940
Eric Christopher564857f2010-12-01 01:40:24 +0000941 // Create the base instruction, then add the operands.
942 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
943 TII.get(StrOpc))
944 .addReg(SrcReg, getKillRegState(true));
945 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000946 return true;
947}
948
Eric Christopher43b62be2010-09-27 06:02:23 +0000949bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000950 Value *Op0 = I->getOperand(0);
951 unsigned SrcReg = 0;
952
Eric Christopher564857f2010-12-01 01:40:24 +0000953 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000954 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000955 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000956 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000957
Eric Christopher1b61ef42010-09-02 01:48:11 +0000958 // Get the value to be stored into a register.
959 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000960 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000961
Eric Christopher564857f2010-12-01 01:40:24 +0000962 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000963 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000964 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000965 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000966
Eric Christopher0d581222010-11-19 22:30:02 +0000967 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000968 return true;
969}
970
971static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
972 switch (Pred) {
973 // Needs two compares...
974 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000975 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000976 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000977 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000978 return ARMCC::AL;
979 case CmpInst::ICMP_EQ:
980 case CmpInst::FCMP_OEQ:
981 return ARMCC::EQ;
982 case CmpInst::ICMP_SGT:
983 case CmpInst::FCMP_OGT:
984 return ARMCC::GT;
985 case CmpInst::ICMP_SGE:
986 case CmpInst::FCMP_OGE:
987 return ARMCC::GE;
988 case CmpInst::ICMP_UGT:
989 case CmpInst::FCMP_UGT:
990 return ARMCC::HI;
991 case CmpInst::FCMP_OLT:
992 return ARMCC::MI;
993 case CmpInst::ICMP_ULE:
994 case CmpInst::FCMP_OLE:
995 return ARMCC::LS;
996 case CmpInst::FCMP_ORD:
997 return ARMCC::VC;
998 case CmpInst::FCMP_UNO:
999 return ARMCC::VS;
1000 case CmpInst::FCMP_UGE:
1001 return ARMCC::PL;
1002 case CmpInst::ICMP_SLT:
1003 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +00001004 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +00001005 case CmpInst::ICMP_SLE:
1006 case CmpInst::FCMP_ULE:
1007 return ARMCC::LE;
1008 case CmpInst::FCMP_UNE:
1009 case CmpInst::ICMP_NE:
1010 return ARMCC::NE;
1011 case CmpInst::ICMP_UGE:
1012 return ARMCC::HS;
1013 case CmpInst::ICMP_ULT:
1014 return ARMCC::LO;
1015 }
Eric Christopher543cf052010-09-01 22:16:27 +00001016}
1017
Eric Christopher43b62be2010-09-27 06:02:23 +00001018bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001019 const BranchInst *BI = cast<BranchInst>(I);
1020 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1021 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001022
Eric Christophere5734102010-09-03 00:35:47 +00001023 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001024
Eric Christopher0e6233b2010-10-29 21:08:19 +00001025 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1026 // behavior.
1027 // TODO: Factor this out.
1028 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1029 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001030 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001031 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001032 if (!isTypeLegal(Ty, VT))
1033 return false;
1034
Eric Christopher0e6233b2010-10-29 21:08:19 +00001035 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1036 if (isFloat && !Subtarget->hasVFP2())
1037 return false;
1038
1039 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001040 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001041 default: return false;
1042 // TODO: Verify compares.
1043 case MVT::f32:
1044 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001045 break;
1046 case MVT::f64:
1047 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001048 break;
1049 case MVT::i32:
1050 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001051 break;
1052 }
1053
1054 // Get the compare predicate.
1055 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1056
1057 // We may not handle every CC for now.
1058 if (ARMPred == ARMCC::AL) return false;
1059
1060 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1061 if (Arg1 == 0) return false;
1062
1063 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1064 if (Arg2 == 0) return false;
1065
1066 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1067 TII.get(CmpOpc))
1068 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001069
Eric Christopher0e6233b2010-10-29 21:08:19 +00001070 // For floating point we need to move the result to a comparison register
1071 // that we can then use for branches.
1072 if (isFloat)
1073 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1074 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001075
Eric Christopher0e6233b2010-10-29 21:08:19 +00001076 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1078 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1079 FastEmitBranch(FBB, DL);
1080 FuncInfo.MBB->addSuccessor(TBB);
1081 return true;
1082 }
1083 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001084
Eric Christopher0e6233b2010-10-29 21:08:19 +00001085 unsigned CmpReg = getRegForValue(BI->getCondition());
1086 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001087
Eric Christopher229207a2010-09-29 01:14:47 +00001088 // Re-set the flags just in case.
1089 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1090 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001091 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001092
Eric Christophere5734102010-09-03 00:35:47 +00001093 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001094 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001095 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001096 FastEmitBranch(FBB, DL);
1097 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001098 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001099}
1100
Eric Christopher43b62be2010-09-27 06:02:23 +00001101bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001102 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001103
Duncan Sands1440e8b2010-11-03 11:35:31 +00001104 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001105 const Type *Ty = CI->getOperand(0)->getType();
1106 if (!isTypeLegal(Ty, VT))
1107 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001108
Eric Christopherd43393a2010-09-08 23:13:45 +00001109 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1110 if (isFloat && !Subtarget->hasVFP2())
1111 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001112
Eric Christopherd43393a2010-09-08 23:13:45 +00001113 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001114 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001115 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001116 default: return false;
1117 // TODO: Verify compares.
1118 case MVT::f32:
1119 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001120 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001121 break;
1122 case MVT::f64:
1123 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001124 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001125 break;
1126 case MVT::i32:
1127 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001128 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001129 break;
1130 }
1131
Eric Christopher229207a2010-09-29 01:14:47 +00001132 // Get the compare predicate.
1133 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001134
Eric Christopher229207a2010-09-29 01:14:47 +00001135 // We may not handle every CC for now.
1136 if (ARMPred == ARMCC::AL) return false;
1137
Eric Christopherd43393a2010-09-08 23:13:45 +00001138 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1139 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001140
Eric Christopherd43393a2010-09-08 23:13:45 +00001141 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1142 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001143
Eric Christopherd43393a2010-09-08 23:13:45 +00001144 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1145 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001146
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001147 // For floating point we need to move the result to a comparison register
1148 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001149 if (isFloat)
1150 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1151 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001152
Eric Christopher229207a2010-09-29 01:14:47 +00001153 // Now set a register based on the comparison. Explicitly set the predicates
1154 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001155 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001156 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001157 : ARM::GPRRegisterClass;
1158 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001159 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001160 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001161 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1162 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1163 .addReg(ZeroReg).addImm(1)
1164 .addImm(ARMPred).addReg(CondReg);
1165
Eric Christophera5b1e682010-09-17 22:28:18 +00001166 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001167 return true;
1168}
1169
Eric Christopher43b62be2010-09-27 06:02:23 +00001170bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001171 // Make sure we have VFP and that we're extending float to double.
1172 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001173
Eric Christopher46203602010-09-09 00:26:48 +00001174 Value *V = I->getOperand(0);
1175 if (!I->getType()->isDoubleTy() ||
1176 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001177
Eric Christopher46203602010-09-09 00:26:48 +00001178 unsigned Op = getRegForValue(V);
1179 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001180
Eric Christopher46203602010-09-09 00:26:48 +00001181 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001182 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001183 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001184 .addReg(Op));
1185 UpdateValueMap(I, Result);
1186 return true;
1187}
1188
Eric Christopher43b62be2010-09-27 06:02:23 +00001189bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001190 // Make sure we have VFP and that we're truncating double to float.
1191 if (!Subtarget->hasVFP2()) return false;
1192
1193 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001194 if (!(I->getType()->isFloatTy() &&
1195 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001196
1197 unsigned Op = getRegForValue(V);
1198 if (Op == 0) return false;
1199
1200 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001202 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001203 .addReg(Op));
1204 UpdateValueMap(I, Result);
1205 return true;
1206}
1207
Eric Christopher43b62be2010-09-27 06:02:23 +00001208bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001209 // Make sure we have VFP.
1210 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001211
Duncan Sands1440e8b2010-11-03 11:35:31 +00001212 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001213 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001214 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001215 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001216
Eric Christopher9a040492010-09-09 18:54:59 +00001217 unsigned Op = getRegForValue(I->getOperand(0));
1218 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001219
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001220 // The conversion routine works on fp-reg to fp-reg and the operand above
1221 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001222 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001223 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001224
Eric Christopher9a040492010-09-09 18:54:59 +00001225 unsigned Opc;
1226 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1227 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1228 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001229
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001230 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001231 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1232 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001233 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001234 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001235 return true;
1236}
1237
Eric Christopher43b62be2010-09-27 06:02:23 +00001238bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001239 // Make sure we have VFP.
1240 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001241
Duncan Sands1440e8b2010-11-03 11:35:31 +00001242 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001243 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001244 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001245 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001246
Eric Christopher9a040492010-09-09 18:54:59 +00001247 unsigned Op = getRegForValue(I->getOperand(0));
1248 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001249
Eric Christopher9a040492010-09-09 18:54:59 +00001250 unsigned Opc;
1251 const Type *OpTy = I->getOperand(0)->getType();
1252 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1253 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1254 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001255
Eric Christopher022b7fb2010-10-05 23:13:24 +00001256 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1257 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1259 ResultReg)
1260 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001261
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001262 // This result needs to be in an integer register, but the conversion only
1263 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001264 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001265 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001266
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001267 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001268 return true;
1269}
1270
Eric Christopher3bbd3962010-10-11 08:27:59 +00001271bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001272 MVT VT;
1273 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001274 return false;
1275
1276 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001277 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001278 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1279
1280 unsigned CondReg = getRegForValue(I->getOperand(0));
1281 if (CondReg == 0) return false;
1282 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1283 if (Op1Reg == 0) return false;
1284 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1285 if (Op2Reg == 0) return false;
1286
1287 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1289 .addReg(CondReg).addImm(1));
1290 unsigned ResultReg = createResultReg(RC);
1291 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1293 .addReg(Op1Reg).addReg(Op2Reg)
1294 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1295 UpdateValueMap(I, ResultReg);
1296 return true;
1297}
1298
Eric Christopher08637852010-09-30 22:34:19 +00001299bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001300 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001301 const Type *Ty = I->getType();
1302 if (!isTypeLegal(Ty, VT))
1303 return false;
1304
1305 // If we have integer div support we should have selected this automagically.
1306 // In case we have a real miss go ahead and return false and we'll pick
1307 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001308 if (Subtarget->hasDivide()) return false;
1309
Eric Christopher08637852010-09-30 22:34:19 +00001310 // Otherwise emit a libcall.
1311 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001312 if (VT == MVT::i8)
1313 LC = RTLIB::SDIV_I8;
1314 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001315 LC = RTLIB::SDIV_I16;
1316 else if (VT == MVT::i32)
1317 LC = RTLIB::SDIV_I32;
1318 else if (VT == MVT::i64)
1319 LC = RTLIB::SDIV_I64;
1320 else if (VT == MVT::i128)
1321 LC = RTLIB::SDIV_I128;
1322 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001323
Eric Christopher08637852010-09-30 22:34:19 +00001324 return ARMEmitLibcall(I, LC);
1325}
1326
Eric Christopher6a880d62010-10-11 08:37:26 +00001327bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001328 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001329 const Type *Ty = I->getType();
1330 if (!isTypeLegal(Ty, VT))
1331 return false;
1332
1333 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1334 if (VT == MVT::i8)
1335 LC = RTLIB::SREM_I8;
1336 else if (VT == MVT::i16)
1337 LC = RTLIB::SREM_I16;
1338 else if (VT == MVT::i32)
1339 LC = RTLIB::SREM_I32;
1340 else if (VT == MVT::i64)
1341 LC = RTLIB::SREM_I64;
1342 else if (VT == MVT::i128)
1343 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001344 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001345
Eric Christopher6a880d62010-10-11 08:37:26 +00001346 return ARMEmitLibcall(I, LC);
1347}
1348
Eric Christopher43b62be2010-09-27 06:02:23 +00001349bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001350 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001351
Eric Christopherbc39b822010-09-09 00:53:57 +00001352 // We can get here in the case when we want to use NEON for our fp
1353 // operations, but can't figure out how to. Just use the vfp instructions
1354 // if we have them.
1355 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001356 const Type *Ty = I->getType();
1357 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1358 if (isFloat && !Subtarget->hasVFP2())
1359 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001360
Eric Christopherbc39b822010-09-09 00:53:57 +00001361 unsigned Op1 = getRegForValue(I->getOperand(0));
1362 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001363
Eric Christopherbc39b822010-09-09 00:53:57 +00001364 unsigned Op2 = getRegForValue(I->getOperand(1));
1365 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001366
Eric Christopherbc39b822010-09-09 00:53:57 +00001367 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001368 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001369 switch (ISDOpcode) {
1370 default: return false;
1371 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001372 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001373 break;
1374 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001375 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001376 break;
1377 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001378 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001379 break;
1380 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001381 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1383 TII.get(Opc), ResultReg)
1384 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001385 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001386 return true;
1387}
1388
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001389// Call Handling Code
1390
Eric Christopherfa87d662010-10-18 02:17:53 +00001391bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1392 EVT SrcVT, unsigned &ResultReg) {
1393 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1394 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001395
Eric Christopherfa87d662010-10-18 02:17:53 +00001396 if (RR != 0) {
1397 ResultReg = RR;
1398 return true;
1399 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001400 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001401}
1402
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001403// This is largely taken directly from CCAssignFnForNode - we don't support
1404// varargs in FastISel so that part has been removed.
1405// TODO: We may not support all of this.
1406CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1407 switch (CC) {
1408 default:
1409 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001410 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001411 // Ignore fastcc. Silence compiler warnings.
1412 (void)RetFastCC_ARM_APCS;
1413 (void)FastCC_ARM_APCS;
1414 // Fallthrough
1415 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001416 // Use target triple & subtarget features to do actual dispatch.
1417 if (Subtarget->isAAPCS_ABI()) {
1418 if (Subtarget->hasVFP2() &&
1419 FloatABIType == FloatABI::Hard)
1420 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1421 else
1422 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1423 } else
1424 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1425 case CallingConv::ARM_AAPCS_VFP:
1426 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1427 case CallingConv::ARM_AAPCS:
1428 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1429 case CallingConv::ARM_APCS:
1430 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1431 }
1432}
1433
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001434bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1435 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001436 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001437 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1438 SmallVectorImpl<unsigned> &RegArgs,
1439 CallingConv::ID CC,
1440 unsigned &NumBytes) {
1441 SmallVector<CCValAssign, 16> ArgLocs;
1442 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1443 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1444
1445 // Get a count of how many bytes are to be pushed on the stack.
1446 NumBytes = CCInfo.getNextStackOffset();
1447
1448 // Issue CALLSEQ_START
1449 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001450 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1451 TII.get(AdjStackDown))
1452 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001453
1454 // Process the args.
1455 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1456 CCValAssign &VA = ArgLocs[i];
1457 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001458 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001459
Eric Christopher4a2b3162011-01-27 05:44:56 +00001460 // We don't handle NEON/vector parameters yet.
1461 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001462 return false;
1463
Eric Christopherf9764fa2010-09-30 20:49:44 +00001464 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001465 switch (VA.getLocInfo()) {
1466 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001467 case CCValAssign::SExt: {
1468 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1469 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001470 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001471 Emitted = true;
1472 ArgVT = VA.getLocVT();
1473 break;
1474 }
1475 case CCValAssign::ZExt: {
1476 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1477 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001478 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001479 Emitted = true;
1480 ArgVT = VA.getLocVT();
1481 break;
1482 }
1483 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001484 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1485 Arg, ArgVT, Arg);
1486 if (!Emitted)
1487 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1488 Arg, ArgVT, Arg);
1489 if (!Emitted)
1490 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1491 Arg, ArgVT, Arg);
1492
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001493 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001494 ArgVT = VA.getLocVT();
1495 break;
1496 }
1497 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001498 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001499 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001500 assert(BC != 0 && "Failed to emit a bitcast!");
1501 Arg = BC;
1502 ArgVT = VA.getLocVT();
1503 break;
1504 }
1505 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001506 }
1507
1508 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001509 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001511 VA.getLocReg())
1512 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001513 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001514 } else if (VA.needsCustom()) {
1515 // TODO: We need custom lowering for vector (v2f64) args.
1516 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001517
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001518 CCValAssign &NextVA = ArgLocs[++i];
1519
1520 // TODO: Only handle register args for now.
1521 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1522
1523 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1524 TII.get(ARM::VMOVRRD), VA.getLocReg())
1525 .addReg(NextVA.getLocReg(), RegState::Define)
1526 .addReg(Arg));
1527 RegArgs.push_back(VA.getLocReg());
1528 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001529 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001530 assert(VA.isMemLoc());
1531 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001532 Address Addr;
1533 Addr.BaseType = Address::RegBase;
1534 Addr.Base.Reg = ARM::SP;
1535 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001536
Eric Christopher0d581222010-11-19 22:30:02 +00001537 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001538 }
1539 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001540 return true;
1541}
1542
Duncan Sands1440e8b2010-11-03 11:35:31 +00001543bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001544 const Instruction *I, CallingConv::ID CC,
1545 unsigned &NumBytes) {
1546 // Issue CALLSEQ_END
1547 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1549 TII.get(AdjStackUp))
1550 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001551
1552 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001553 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001554 SmallVector<CCValAssign, 16> RVLocs;
1555 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1556 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1557
1558 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001559 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001560 // For this move we copy into two registers and then move into the
1561 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001562 EVT DestVT = RVLocs[0].getValVT();
1563 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1564 unsigned ResultReg = createResultReg(DstRC);
1565 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1566 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001567 .addReg(RVLocs[0].getLocReg())
1568 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001569
Eric Christopher3659ac22010-10-20 08:02:24 +00001570 UsedRegs.push_back(RVLocs[0].getLocReg());
1571 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001572
Eric Christopherdccd2c32010-10-11 08:38:55 +00001573 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001574 UpdateValueMap(I, ResultReg);
1575 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001576 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001577 EVT CopyVT = RVLocs[0].getValVT();
1578 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001579
Eric Christopher14df8822010-10-01 00:00:11 +00001580 unsigned ResultReg = createResultReg(DstRC);
1581 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1582 ResultReg).addReg(RVLocs[0].getLocReg());
1583 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001584
Eric Christopherdccd2c32010-10-11 08:38:55 +00001585 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001586 UpdateValueMap(I, ResultReg);
1587 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001588 }
1589
Eric Christopherdccd2c32010-10-11 08:38:55 +00001590 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001591}
1592
Eric Christopher4f512ef2010-10-22 01:28:00 +00001593bool ARMFastISel::SelectRet(const Instruction *I) {
1594 const ReturnInst *Ret = cast<ReturnInst>(I);
1595 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001596
Eric Christopher4f512ef2010-10-22 01:28:00 +00001597 if (!FuncInfo.CanLowerReturn)
1598 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001599
Eric Christopher4f512ef2010-10-22 01:28:00 +00001600 if (F.isVarArg())
1601 return false;
1602
1603 CallingConv::ID CC = F.getCallingConv();
1604 if (Ret->getNumOperands() > 0) {
1605 SmallVector<ISD::OutputArg, 4> Outs;
1606 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1607 Outs, TLI);
1608
1609 // Analyze operands of the call, assigning locations to each operand.
1610 SmallVector<CCValAssign, 16> ValLocs;
1611 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1612 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1613
1614 const Value *RV = Ret->getOperand(0);
1615 unsigned Reg = getRegForValue(RV);
1616 if (Reg == 0)
1617 return false;
1618
1619 // Only handle a single return value for now.
1620 if (ValLocs.size() != 1)
1621 return false;
1622
1623 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001624
Eric Christopher4f512ef2010-10-22 01:28:00 +00001625 // Don't bother handling odd stuff for now.
1626 if (VA.getLocInfo() != CCValAssign::Full)
1627 return false;
1628 // Only handle register returns for now.
1629 if (!VA.isRegLoc())
1630 return false;
1631 // TODO: For now, don't try to handle cases where getLocInfo()
1632 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001633 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001634 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001635
Eric Christopher4f512ef2010-10-22 01:28:00 +00001636 // Make the copy.
1637 unsigned SrcReg = Reg + VA.getValNo();
1638 unsigned DstReg = VA.getLocReg();
1639 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1640 // Avoid a cross-class copy. This is very unlikely.
1641 if (!SrcRC->contains(DstReg))
1642 return false;
1643 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1644 DstReg).addReg(SrcReg);
1645
1646 // Mark the register as live out of the function.
1647 MRI.addLiveOut(VA.getLocReg());
1648 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001649
Eric Christopher4f512ef2010-10-22 01:28:00 +00001650 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1651 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1652 TII.get(RetOpc)));
1653 return true;
1654}
1655
Eric Christopher872f4a22011-02-22 01:37:10 +00001656unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1657
1658 // Depend our opcode for thumb on whether or not we're targeting an
1659 // externally callable function. For libcalls we'll just pass a NULL GV
1660 // in here.
1661 bool isExternal = false;
1662 if (!GV || GV->hasExternalLinkage()) isExternal = true;
1663
1664 // Darwin needs the r9 versions of the opcodes.
1665 bool isDarwin = Subtarget->isTargetDarwin();
1666 if (isThumb && isExternal) {
1667 return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi;
1668 } else if (isThumb) {
1669 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1670 } else {
1671 return isDarwin ? ARM::BLr9 : ARM::BL;
1672 }
1673}
1674
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001675// A quick function that will emit a call for a named libcall in F with the
1676// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001677// can emit a call for any libcall we can produce. This is an abridged version
1678// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001679// like computed function pointers or strange arguments at call sites.
1680// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1681// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001682bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1683 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001684
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001685 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001686 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001687 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001688 if (RetTy->isVoidTy())
1689 RetVT = MVT::isVoid;
1690 else if (!isTypeLegal(RetTy, RetVT))
1691 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001692
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001693 // For now we're using BLX etc on the assumption that we have v5t ops.
1694 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001695
Eric Christopher836c6242010-12-15 23:47:29 +00001696 // TODO: For now if we have long calls specified we don't handle the call.
1697 if (EnableARMLongCalls) return false;
1698
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001699 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001700 SmallVector<Value*, 8> Args;
1701 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001702 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001703 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1704 Args.reserve(I->getNumOperands());
1705 ArgRegs.reserve(I->getNumOperands());
1706 ArgVTs.reserve(I->getNumOperands());
1707 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001708 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001709 Value *Op = I->getOperand(i);
1710 unsigned Arg = getRegForValue(Op);
1711 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001712
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001713 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001714 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001715 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001716
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001717 ISD::ArgFlagsTy Flags;
1718 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1719 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001720
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001721 Args.push_back(Op);
1722 ArgRegs.push_back(Arg);
1723 ArgVTs.push_back(ArgVT);
1724 ArgFlags.push_back(Flags);
1725 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001726
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001727 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001728 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001729 unsigned NumBytes;
1730 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1731 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001732
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001733 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001734 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001735 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001736 unsigned CallOpc = ARMSelectCallOp(NULL);
1737 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001738 // Explicitly adding the predicate here.
1739 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1740 TII.get(CallOpc)))
1741 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001742 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001743 // Explicitly adding the predicate here.
1744 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1745 TII.get(CallOpc))
1746 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001747
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001748 // Add implicit physical register uses to the call.
1749 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1750 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001752 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001753 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001754 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001755
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001756 // Set all unused physreg defs as dead.
1757 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001758
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001759 return true;
1760}
1761
Eric Christopherf9764fa2010-09-30 20:49:44 +00001762bool ARMFastISel::SelectCall(const Instruction *I) {
1763 const CallInst *CI = cast<CallInst>(I);
1764 const Value *Callee = CI->getCalledValue();
1765
1766 // Can't handle inline asm or worry about intrinsics yet.
1767 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1768
Eric Christophere6ca6772010-10-01 21:33:12 +00001769 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001770 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001771 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1772 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001773
Eric Christopherf9764fa2010-09-30 20:49:44 +00001774 // Check the calling convention.
1775 ImmutableCallSite CS(CI);
1776 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001777
Eric Christopherf9764fa2010-09-30 20:49:44 +00001778 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001779
Eric Christopherf9764fa2010-09-30 20:49:44 +00001780 // Let SDISel handle vararg functions.
1781 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1782 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1783 if (FTy->isVarArg())
1784 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001785
Eric Christopherf9764fa2010-09-30 20:49:44 +00001786 // Handle *simple* calls for now.
1787 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001788 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001789 if (RetTy->isVoidTy())
1790 RetVT = MVT::isVoid;
1791 else if (!isTypeLegal(RetTy, RetVT))
1792 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001793
Eric Christopherf9764fa2010-09-30 20:49:44 +00001794 // For now we're using BLX etc on the assumption that we have v5t ops.
1795 // TODO: Maybe?
1796 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001797
Eric Christopher836c6242010-12-15 23:47:29 +00001798 // TODO: For now if we have long calls specified we don't handle the call.
1799 if (EnableARMLongCalls) return false;
1800
Eric Christopherf9764fa2010-09-30 20:49:44 +00001801 // Set up the argument vectors.
1802 SmallVector<Value*, 8> Args;
1803 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001804 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001805 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1806 Args.reserve(CS.arg_size());
1807 ArgRegs.reserve(CS.arg_size());
1808 ArgVTs.reserve(CS.arg_size());
1809 ArgFlags.reserve(CS.arg_size());
1810 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1811 i != e; ++i) {
1812 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001813
Eric Christopherf9764fa2010-09-30 20:49:44 +00001814 if (Arg == 0)
1815 return false;
1816 ISD::ArgFlagsTy Flags;
1817 unsigned AttrInd = i - CS.arg_begin() + 1;
1818 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1819 Flags.setSExt();
1820 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1821 Flags.setZExt();
1822
1823 // FIXME: Only handle *easy* calls for now.
1824 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1825 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1826 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1827 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1828 return false;
1829
1830 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001831 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001832 if (!isTypeLegal(ArgTy, ArgVT))
1833 return false;
1834 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1835 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001836
Eric Christopherf9764fa2010-09-30 20:49:44 +00001837 Args.push_back(*i);
1838 ArgRegs.push_back(Arg);
1839 ArgVTs.push_back(ArgVT);
1840 ArgFlags.push_back(Flags);
1841 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001842
Eric Christopherf9764fa2010-09-30 20:49:44 +00001843 // Handle the arguments now that we've gotten them.
1844 SmallVector<unsigned, 4> RegArgs;
1845 unsigned NumBytes;
1846 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1847 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001848
Eric Christopherf9764fa2010-09-30 20:49:44 +00001849 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001850 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001851 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001852 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001853 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001854 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001855 // Explicitly adding the predicate here.
1856 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1857 TII.get(CallOpc)))
1858 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001859 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001860 // Explicitly adding the predicate here.
1861 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1862 TII.get(CallOpc))
1863 .addGlobalAddress(GV, 0, 0));
Eric Christopherc19aadb2010-12-21 03:50:43 +00001864
Eric Christopherf9764fa2010-09-30 20:49:44 +00001865 // Add implicit physical register uses to the call.
1866 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1867 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001868
Eric Christopherf9764fa2010-09-30 20:49:44 +00001869 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001870 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001871 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001872
Eric Christopherf9764fa2010-09-30 20:49:44 +00001873 // Set all unused physreg defs as dead.
1874 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001875
Eric Christopherf9764fa2010-09-30 20:49:44 +00001876 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001877
Eric Christopherf9764fa2010-09-30 20:49:44 +00001878}
1879
Eric Christopher56d2b722010-09-02 23:43:26 +00001880// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001881bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001882
Eric Christopherab695882010-07-21 22:26:11 +00001883 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001884 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001885 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001886 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001887 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001888 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001889 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001890 case Instruction::ICmp:
1891 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001892 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001893 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001894 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001895 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001896 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001897 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001898 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001899 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001900 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001901 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001902 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001903 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001904 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001905 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001906 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001907 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001908 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001909 case Instruction::SRem:
1910 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001911 case Instruction::Call:
1912 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001913 case Instruction::Select:
1914 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001915 case Instruction::Ret:
1916 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001917 default: break;
1918 }
1919 return false;
1920}
1921
1922namespace llvm {
1923 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001924 // Completely untested on non-darwin.
1925 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001926
Eric Christopheraaa8df42010-11-02 01:21:28 +00001927 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001928 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001929 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001930 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001931 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001932 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001933 }
1934}