blob: 26f48b3083168f158309fbd9cab0c6e78dedf3e6 [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);
126 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 uint64_t Imm);
129 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
130 const TargetRegisterClass *RC,
131 unsigned Op0, bool Op0IsKill,
132 unsigned Op1, bool Op1IsKill,
133 uint64_t Imm);
134 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
135 unsigned Op0, bool Op0IsKill,
136 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000137
Eric Christophercb592292010-08-20 00:20:31 +0000138 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000139 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000140 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000141 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000142
143 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000144
Eric Christopher83007122010-08-23 21:44:12 +0000145 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000146 private:
Eric Christopher17787722010-10-21 21:47:51 +0000147 bool SelectLoad(const Instruction *I);
148 bool SelectStore(const Instruction *I);
149 bool SelectBranch(const Instruction *I);
150 bool SelectCmp(const Instruction *I);
151 bool SelectFPExt(const Instruction *I);
152 bool SelectFPTrunc(const Instruction *I);
153 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
154 bool SelectSIToFP(const Instruction *I);
155 bool SelectFPToSI(const Instruction *I);
156 bool SelectSDiv(const Instruction *I);
157 bool SelectSRem(const Instruction *I);
158 bool SelectCall(const Instruction *I);
159 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000160 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000161
Eric Christopher83007122010-08-23 21:44:12 +0000162 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000163 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000164 bool isTypeLegal(const Type *Ty, MVT &VT);
165 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000166 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
167 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
168 bool ARMComputeAddress(const Value *Obj, Address &Addr);
169 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000170 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000171 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000172 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000173 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000174 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopher872f4a22011-02-22 01:37:10 +0000175 unsigned ARMSelectCallOp(const GlobalValue *GV);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000176
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000177 // Call handling routines.
178 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000179 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
180 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000181 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000182 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000183 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000184 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000185 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
186 SmallVectorImpl<unsigned> &RegArgs,
187 CallingConv::ID CC,
188 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000189 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000190 const Instruction *I, CallingConv::ID CC,
191 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000192 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000193
194 // OptionalDef handling routines.
195 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000196 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
197 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
Eric Christopher564857f2010-12-01 01:40:24 +0000198 void AddLoadStoreOperands(EVT VT, Address &Addr,
199 const MachineInstrBuilder &MIB);
Eric Christopher456144e2010-08-19 00:37:05 +0000200};
Eric Christopherab695882010-07-21 22:26:11 +0000201
202} // end anonymous namespace
203
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000204#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000205
Eric Christopher456144e2010-08-19 00:37:05 +0000206// DefinesOptionalPredicate - This is different from DefinesPredicate in that
207// we don't care about implicit defs here, just places we'll need to add a
208// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
209bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
210 const TargetInstrDesc &TID = MI->getDesc();
211 if (!TID.hasOptionalDef())
212 return false;
213
214 // Look to see if our OptionalDef is defining CPSR or CCR.
215 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
216 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000217 if (!MO.isReg() || !MO.isDef()) continue;
218 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000219 *CPSR = true;
220 }
221 return true;
222}
223
224// If the machine is predicable go ahead and add the predicate operands, if
225// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000226// TODO: If we want to support thumb1 then we'll need to deal with optional
227// CPSR defs that need to be added before the remaining operands. See s_cc_out
228// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000229const MachineInstrBuilder &
230ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
231 MachineInstr *MI = &*MIB;
232
233 // Do we use a predicate?
234 if (TII.isPredicable(MI))
235 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000236
Eric Christopher456144e2010-08-19 00:37:05 +0000237 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
238 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000239 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000240 if (DefinesOptionalPredicate(MI, &CPSR)) {
241 if (CPSR)
242 AddDefaultT1CC(MIB);
243 else
244 AddDefaultCC(MIB);
245 }
246 return MIB;
247}
248
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
250 const TargetRegisterClass* RC) {
251 unsigned ResultReg = createResultReg(RC);
252 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
253
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000255 return ResultReg;
256}
257
258unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
259 const TargetRegisterClass *RC,
260 unsigned Op0, bool Op0IsKill) {
261 unsigned ResultReg = createResultReg(RC);
262 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
263
264 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000266 .addReg(Op0, Op0IsKill * RegState::Kill));
267 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000269 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000271 TII.get(TargetOpcode::COPY), ResultReg)
272 .addReg(II.ImplicitDefs[0]));
273 }
274 return ResultReg;
275}
276
277unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
278 const TargetRegisterClass *RC,
279 unsigned Op0, bool Op0IsKill,
280 unsigned Op1, bool Op1IsKill) {
281 unsigned ResultReg = createResultReg(RC);
282 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
283
284 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000285 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000286 .addReg(Op0, Op0IsKill * RegState::Kill)
287 .addReg(Op1, Op1IsKill * RegState::Kill));
288 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 .addReg(Op0, Op0IsKill * RegState::Kill)
291 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 TII.get(TargetOpcode::COPY), ResultReg)
294 .addReg(II.ImplicitDefs[0]));
295 }
296 return ResultReg;
297}
298
299unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
300 const TargetRegisterClass *RC,
301 unsigned Op0, bool Op0IsKill,
302 uint64_t Imm) {
303 unsigned ResultReg = createResultReg(RC);
304 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
305
306 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 .addReg(Op0, Op0IsKill * RegState::Kill)
309 .addImm(Imm));
310 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 .addReg(Op0, Op0IsKill * RegState::Kill)
313 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000315 TII.get(TargetOpcode::COPY), ResultReg)
316 .addReg(II.ImplicitDefs[0]));
317 }
318 return ResultReg;
319}
320
321unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
322 const TargetRegisterClass *RC,
323 unsigned Op0, bool Op0IsKill,
324 const ConstantFP *FPImm) {
325 unsigned ResultReg = createResultReg(RC);
326 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
327
328 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addFPImm(FPImm));
332 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000334 .addReg(Op0, Op0IsKill * RegState::Kill)
335 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000336 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000337 TII.get(TargetOpcode::COPY), ResultReg)
338 .addReg(II.ImplicitDefs[0]));
339 }
340 return ResultReg;
341}
342
343unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
344 const TargetRegisterClass *RC,
345 unsigned Op0, bool Op0IsKill,
346 unsigned Op1, bool Op1IsKill,
347 uint64_t Imm) {
348 unsigned ResultReg = createResultReg(RC);
349 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
350
351 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addReg(Op1, Op1IsKill * RegState::Kill)
355 .addImm(Imm));
356 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000358 .addReg(Op0, Op0IsKill * RegState::Kill)
359 .addReg(Op1, Op1IsKill * RegState::Kill)
360 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000361 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000362 TII.get(TargetOpcode::COPY), ResultReg)
363 .addReg(II.ImplicitDefs[0]));
364 }
365 return ResultReg;
366}
367
368unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
369 const TargetRegisterClass *RC,
370 uint64_t Imm) {
371 unsigned ResultReg = createResultReg(RC);
372 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000373
Eric Christopher0fe7d542010-08-17 01:25:29 +0000374 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000376 .addImm(Imm));
377 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000379 .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_extractsubreg(MVT RetVT,
388 unsigned Op0, bool Op0IsKill,
389 uint32_t Idx) {
390 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
391 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
392 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000393 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000394 DL, TII.get(TargetOpcode::COPY), ResultReg)
395 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
396 return ResultReg;
397}
398
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000399// TODO: Don't worry about 64-bit now, but when this is fixed remove the
400// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000401unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000402 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000403
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000404 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
405 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
406 TII.get(ARM::VMOVRS), MoveReg)
407 .addReg(SrcReg));
408 return MoveReg;
409}
410
411unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000412 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000413
Eric Christopheraa3ace12010-09-09 20:49:25 +0000414 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000416 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000417 .addReg(SrcReg));
418 return MoveReg;
419}
420
Eric Christopher9ed58df2010-09-09 00:19:41 +0000421// For double width floating point we need to materialize two constants
422// (the high and the low) into integer registers then use a move to get
423// the combined constant into an FP reg.
424unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
425 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000426 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000427
Eric Christopher9ed58df2010-09-09 00:19:41 +0000428 // This checks to see if we can use VFP3 instructions to materialize
429 // a constant, otherwise we have to go through the constant pool.
430 if (TLI.isFPImmLegal(Val, VT)) {
431 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
432 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
433 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
434 DestReg)
435 .addFPImm(CFP));
436 return DestReg;
437 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000438
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000439 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000440 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000441
Eric Christopher238bb162010-09-09 23:50:00 +0000442 // MachineConstantPool wants an explicit alignment.
443 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
444 if (Align == 0) {
445 // TODO: Figure out if this is correct.
446 Align = TD.getTypeAllocSize(CFP->getType());
447 }
448 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
449 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
450 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000451
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000452 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000453 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
454 DestReg)
455 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000456 .addReg(0));
457 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000458}
459
Eric Christopher744c7c82010-09-28 22:47:54 +0000460unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000461
Eric Christopher744c7c82010-09-28 22:47:54 +0000462 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000463 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000464
Eric Christophere5b13cf2010-11-03 20:21:17 +0000465 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
466
467 // If we can do this in a single instruction without a constant pool entry
468 // do so now.
469 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000470 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000471 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
472 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000473 TII.get(Opc), DestReg)
474 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000475 return DestReg;
476 }
477
Eric Christopher56d2b722010-09-02 23:43:26 +0000478 // MachineConstantPool wants an explicit alignment.
479 unsigned Align = TD.getPrefTypeAlignment(C->getType());
480 if (Align == 0) {
481 // TODO: Figure out if this is correct.
482 Align = TD.getTypeAllocSize(C->getType());
483 }
484 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000485
Eric Christopher56d2b722010-09-02 23:43:26 +0000486 if (isThumb)
487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000488 TII.get(ARM::t2LDRpci), DestReg)
489 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000490 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000491 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000493 TII.get(ARM::LDRcp), DestReg)
494 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000495 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000496
Eric Christopher56d2b722010-09-02 23:43:26 +0000497 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000498}
499
Eric Christopherc9932f62010-10-01 23:24:42 +0000500unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000501 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000502 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000503
Eric Christopher890dbbe2010-10-02 00:32:44 +0000504 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000505
Eric Christopher890dbbe2010-10-02 00:32:44 +0000506 // TODO: No external globals for now.
507 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000508
Eric Christopher890dbbe2010-10-02 00:32:44 +0000509 // TODO: Need more magic for ARM PIC.
510 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000511
Eric Christopher890dbbe2010-10-02 00:32:44 +0000512 // MachineConstantPool wants an explicit alignment.
513 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
514 if (Align == 0) {
515 // TODO: Figure out if this is correct.
516 Align = TD.getTypeAllocSize(GV->getType());
517 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000518
Eric Christopher890dbbe2010-10-02 00:32:44 +0000519 // Grab index.
520 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000521 unsigned Id = AFI->createPICLabelUId();
Eric Christopher890dbbe2010-10-02 00:32:44 +0000522 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
523 ARMCP::CPValue, PCAdj);
524 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000525
Eric Christopher890dbbe2010-10-02 00:32:44 +0000526 // Load value.
527 MachineInstrBuilder MIB;
528 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
529 if (isThumb) {
530 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
531 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
532 .addConstantPoolIndex(Idx);
533 if (RelocM == Reloc::PIC_)
534 MIB.addImm(Id);
535 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000536 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000537 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
538 DestReg)
539 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000540 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000541 }
542 AddOptionalDefs(MIB);
543 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000544}
545
Eric Christopher9ed58df2010-09-09 00:19:41 +0000546unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
547 EVT VT = TLI.getValueType(C->getType(), true);
548
549 // Only handle simple types.
550 if (!VT.isSimple()) return 0;
551
552 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
553 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000554 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
555 return ARMMaterializeGV(GV, VT);
556 else if (isa<ConstantInt>(C))
557 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000558
Eric Christopherc9932f62010-10-01 23:24:42 +0000559 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000560}
561
Eric Christopherf9764fa2010-09-30 20:49:44 +0000562unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
563 // Don't handle dynamic allocas.
564 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000565
Duncan Sands1440e8b2010-11-03 11:35:31 +0000566 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000567 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000568
Eric Christopherf9764fa2010-09-30 20:49:44 +0000569 DenseMap<const AllocaInst*, int>::iterator SI =
570 FuncInfo.StaticAllocaMap.find(AI);
571
572 // This will get lowered later into the correct offsets and registers
573 // via rewriteXFrameIndex.
574 if (SI != FuncInfo.StaticAllocaMap.end()) {
575 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
576 unsigned ResultReg = createResultReg(RC);
577 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
578 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
579 TII.get(Opc), ResultReg)
580 .addFrameIndex(SI->second)
581 .addImm(0));
582 return ResultReg;
583 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000584
Eric Christopherf9764fa2010-09-30 20:49:44 +0000585 return 0;
586}
587
Duncan Sands1440e8b2010-11-03 11:35:31 +0000588bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
589 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000590
Eric Christopherb1cc8482010-08-25 07:23:49 +0000591 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000592 if (evt == MVT::Other || !evt.isSimple()) return false;
593 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000594
Eric Christopherdc908042010-08-31 01:28:42 +0000595 // Handle all legal types, i.e. a register that will directly hold this
596 // value.
597 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000598}
599
Duncan Sands1440e8b2010-11-03 11:35:31 +0000600bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000601 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000602
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000603 // If this is a type than can be sign or zero-extended to a basic operation
604 // go ahead and accept it now.
605 if (VT == MVT::i8 || VT == MVT::i16)
606 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000607
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000608 return false;
609}
610
Eric Christopher88de86b2010-11-19 22:36:41 +0000611// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000612bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000613 // Some boilerplate from the X86 FastISel.
614 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000615 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000616 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000617 // Don't walk into other basic blocks unless the object is an alloca from
618 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000619 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
620 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
621 Opcode = I->getOpcode();
622 U = I;
623 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000624 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000625 Opcode = C->getOpcode();
626 U = C;
627 }
628
Eric Christophercb0b04b2010-08-24 00:07:24 +0000629 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000630 if (Ty->getAddressSpace() > 255)
631 // Fast instruction selection doesn't support the special
632 // address spaces.
633 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000634
Eric Christopher83007122010-08-23 21:44:12 +0000635 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000636 default:
Eric Christopher83007122010-08-23 21:44:12 +0000637 break;
Eric Christopher55324332010-10-12 00:43:21 +0000638 case Instruction::BitCast: {
639 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000640 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000641 }
642 case Instruction::IntToPtr: {
643 // Look past no-op inttoptrs.
644 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000645 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000646 break;
647 }
648 case Instruction::PtrToInt: {
649 // Look past no-op ptrtoints.
650 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000651 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000652 break;
653 }
Eric Christophereae84392010-10-14 09:29:41 +0000654 case Instruction::GetElementPtr: {
Eric Christopherb3716582010-11-19 22:39:56 +0000655 Address SavedAddr = Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000656 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000657
Eric Christophereae84392010-10-14 09:29:41 +0000658 // Iterate through the GEP folding the constants into offsets where
659 // we can.
660 gep_type_iterator GTI = gep_type_begin(U);
661 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
662 i != e; ++i, ++GTI) {
663 const Value *Op = *i;
664 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
665 const StructLayout *SL = TD.getStructLayout(STy);
666 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
667 TmpOffset += SL->getElementOffset(Idx);
668 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000669 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
670 SmallVector<const Value *, 4> Worklist;
671 Worklist.push_back(Op);
672 do {
673 Op = Worklist.pop_back_val();
674 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
675 // Constant-offset addressing.
676 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000677 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000678 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
679 // An add with a constant operand. Fold the constant.
680 ConstantInt *CI =
681 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
682 TmpOffset += CI->getSExtValue() * S;
683 // Add the other operand back to the work list.
684 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
685 } else
686 goto unsupported_gep;
687 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000688 }
689 }
Eric Christopher2896df82010-10-15 18:02:07 +0000690
691 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000692 Addr.Offset = TmpOffset;
693 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000694
695 // We failed, restore everything and try the other options.
Eric Christopherb3716582010-11-19 22:39:56 +0000696 Addr = SavedAddr;
Eric Christopher2896df82010-10-15 18:02:07 +0000697
Eric Christophereae84392010-10-14 09:29:41 +0000698 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000699 break;
700 }
Eric Christopher83007122010-08-23 21:44:12 +0000701 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000702 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopher827656d2010-11-20 22:38:27 +0000703 DenseMap<const AllocaInst*, int>::iterator SI =
704 FuncInfo.StaticAllocaMap.find(AI);
705 if (SI != FuncInfo.StaticAllocaMap.end()) {
706 Addr.BaseType = Address::FrameIndexBase;
707 Addr.Base.FI = SI->second;
708 return true;
709 }
710 break;
Eric Christopher83007122010-08-23 21:44:12 +0000711 }
712 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000713
Eric Christophera9c57512010-10-13 21:41:51 +0000714 // Materialize the global variable's address into a reg which can
715 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000716 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000717 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
718 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000719
Eric Christopher0d581222010-11-19 22:30:02 +0000720 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000721 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000722 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000723
Eric Christophercb0b04b2010-08-24 00:07:24 +0000724 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000725 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
726 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000727}
728
Eric Christopher0d581222010-11-19 22:30:02 +0000729void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000730
Eric Christopher212ae932010-10-21 19:40:30 +0000731 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000732
Eric Christopher212ae932010-10-21 19:40:30 +0000733 bool needsLowering = false;
734 switch (VT.getSimpleVT().SimpleTy) {
735 default:
736 assert(false && "Unhandled load/store type!");
737 case MVT::i1:
738 case MVT::i8:
739 case MVT::i16:
740 case MVT::i32:
741 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000742 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000743 break;
744 case MVT::f32:
745 case MVT::f64:
746 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000747 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000748 break;
749 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000750
Eric Christopher827656d2010-11-20 22:38:27 +0000751 // If this is a stack pointer and the offset needs to be simplified then
752 // put the alloca address into a register, set the base type back to
753 // register and continue. This should almost never happen.
754 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
755 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
756 ARM::GPRRegisterClass;
757 unsigned ResultReg = createResultReg(RC);
758 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
759 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
760 TII.get(Opc), ResultReg)
761 .addFrameIndex(Addr.Base.FI)
762 .addImm(0));
763 Addr.Base.Reg = ResultReg;
764 Addr.BaseType = Address::RegBase;
765 }
766
Eric Christopher212ae932010-10-21 19:40:30 +0000767 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000768 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000769 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000770 ARMCC::CondCodes Pred = ARMCC::AL;
771 unsigned PredReg = 0;
772
Eric Christopher2896df82010-10-15 18:02:07 +0000773 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
774 ARM::GPRRegisterClass;
775 unsigned BaseReg = createResultReg(RC);
776
Eric Christophereaa204b2010-09-02 01:39:14 +0000777 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000778 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000779 BaseReg, Addr.Base.Reg, Addr.Offset,
780 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000781 static_cast<const ARMBaseInstrInfo&>(TII));
782 else {
783 assert(AFI->isThumb2Function());
784 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000785 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000786 static_cast<const ARMBaseInstrInfo&>(TII));
787 }
Eric Christopher0d581222010-11-19 22:30:02 +0000788 Addr.Offset = 0;
789 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000790 }
Eric Christopher83007122010-08-23 21:44:12 +0000791}
792
Eric Christopher564857f2010-12-01 01:40:24 +0000793void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
794 const MachineInstrBuilder &MIB) {
795 // addrmode5 output depends on the selection dag addressing dividing the
796 // offset by 4 that it then later multiplies. Do this here as well.
797 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
798 VT.getSimpleVT().SimpleTy == MVT::f64)
799 Addr.Offset /= 4;
800
801 // Frame base works a bit differently. Handle it separately.
802 if (Addr.BaseType == Address::FrameIndexBase) {
803 int FI = Addr.Base.FI;
804 int Offset = Addr.Offset;
805 MachineMemOperand *MMO =
806 FuncInfo.MF->getMachineMemOperand(
807 MachinePointerInfo::getFixedStack(FI, Offset),
808 MachineMemOperand::MOLoad,
809 MFI.getObjectSize(FI),
810 MFI.getObjectAlignment(FI));
811 // Now add the rest of the operands.
812 MIB.addFrameIndex(FI);
813
814 // ARM halfword load/stores need an additional operand.
815 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
816
817 MIB.addImm(Addr.Offset);
818 MIB.addMemOperand(MMO);
819 } else {
820 // Now add the rest of the operands.
821 MIB.addReg(Addr.Base.Reg);
822
823 // ARM halfword load/stores need an additional operand.
824 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16) MIB.addReg(0);
825
826 MIB.addImm(Addr.Offset);
827 }
828 AddOptionalDefs(MIB);
829}
830
Eric Christopher0d581222010-11-19 22:30:02 +0000831bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000832
Eric Christopherb1cc8482010-08-25 07:23:49 +0000833 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000834 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000835 TargetRegisterClass *RC;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000836 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000837 // This is mostly going to be Neon/vector support.
838 default: return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000839 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000840 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000841 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000842 break;
843 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000844 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000845 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000846 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000847 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000848 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000849 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000850 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000851 case MVT::f32:
852 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000853 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000854 break;
855 case MVT::f64:
856 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000857 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000858 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000859 }
Eric Christopher564857f2010-12-01 01:40:24 +0000860 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000861 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000862
Eric Christopher564857f2010-12-01 01:40:24 +0000863 // Create the base instruction, then add the operands.
864 ResultReg = createResultReg(RC);
865 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
866 TII.get(Opc), ResultReg);
867 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopherdc908042010-08-31 01:28:42 +0000868 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000869}
870
Eric Christopher43b62be2010-09-27 06:02:23 +0000871bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000872 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000873 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000874 if (!isLoadTypeLegal(I->getType(), VT))
875 return false;
876
Eric Christopher564857f2010-12-01 01:40:24 +0000877 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000878 Address Addr;
Eric Christopher564857f2010-12-01 01:40:24 +0000879 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000880
881 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000882 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000883 UpdateValueMap(I, ResultReg);
884 return true;
885}
886
Eric Christopher0d581222010-11-19 22:30:02 +0000887bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000888 unsigned StrOpc;
889 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher564857f2010-12-01 01:40:24 +0000890 // This is mostly going to be Neon/vector support.
Eric Christopher318b6ee2010-09-02 00:53:56 +0000891 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000892 case MVT::i1: {
893 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
894 ARM::GPRRegisterClass);
895 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
896 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
897 TII.get(Opc), Res)
898 .addReg(SrcReg).addImm(1));
899 SrcReg = Res;
900 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000901 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000902 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000903 break;
904 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000905 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Eric Christopher15418772010-10-12 05:39:06 +0000906 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000907 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000908 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000909 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000910 case MVT::f32:
911 if (!Subtarget->hasVFP2()) return false;
912 StrOpc = ARM::VSTRS;
913 break;
914 case MVT::f64:
915 if (!Subtarget->hasVFP2()) return false;
916 StrOpc = ARM::VSTRD;
917 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000918 }
Eric Christopher564857f2010-12-01 01:40:24 +0000919 // Simplify this down to something we can handle.
Eric Christopher0d581222010-11-19 22:30:02 +0000920 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000921
Eric Christopher564857f2010-12-01 01:40:24 +0000922 // Create the base instruction, then add the operands.
923 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
924 TII.get(StrOpc))
925 .addReg(SrcReg, getKillRegState(true));
926 AddLoadStoreOperands(VT, Addr, MIB);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000927 return true;
928}
929
Eric Christopher43b62be2010-09-27 06:02:23 +0000930bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000931 Value *Op0 = I->getOperand(0);
932 unsigned SrcReg = 0;
933
Eric Christopher564857f2010-12-01 01:40:24 +0000934 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000935 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000936 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000937 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000938
Eric Christopher1b61ef42010-09-02 01:48:11 +0000939 // Get the value to be stored into a register.
940 SrcReg = getRegForValue(Op0);
Eric Christopher564857f2010-12-01 01:40:24 +0000941 if (SrcReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000942
Eric Christopher564857f2010-12-01 01:40:24 +0000943 // See if we can handle this address.
Eric Christopher0d581222010-11-19 22:30:02 +0000944 Address Addr;
Eric Christopher0d581222010-11-19 22:30:02 +0000945 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000946 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000947
Eric Christopher0d581222010-11-19 22:30:02 +0000948 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christophera5b1e682010-09-17 22:28:18 +0000949 return true;
950}
951
952static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
953 switch (Pred) {
954 // Needs two compares...
955 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000956 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000957 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000958 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000959 return ARMCC::AL;
960 case CmpInst::ICMP_EQ:
961 case CmpInst::FCMP_OEQ:
962 return ARMCC::EQ;
963 case CmpInst::ICMP_SGT:
964 case CmpInst::FCMP_OGT:
965 return ARMCC::GT;
966 case CmpInst::ICMP_SGE:
967 case CmpInst::FCMP_OGE:
968 return ARMCC::GE;
969 case CmpInst::ICMP_UGT:
970 case CmpInst::FCMP_UGT:
971 return ARMCC::HI;
972 case CmpInst::FCMP_OLT:
973 return ARMCC::MI;
974 case CmpInst::ICMP_ULE:
975 case CmpInst::FCMP_OLE:
976 return ARMCC::LS;
977 case CmpInst::FCMP_ORD:
978 return ARMCC::VC;
979 case CmpInst::FCMP_UNO:
980 return ARMCC::VS;
981 case CmpInst::FCMP_UGE:
982 return ARMCC::PL;
983 case CmpInst::ICMP_SLT:
984 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000985 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000986 case CmpInst::ICMP_SLE:
987 case CmpInst::FCMP_ULE:
988 return ARMCC::LE;
989 case CmpInst::FCMP_UNE:
990 case CmpInst::ICMP_NE:
991 return ARMCC::NE;
992 case CmpInst::ICMP_UGE:
993 return ARMCC::HS;
994 case CmpInst::ICMP_ULT:
995 return ARMCC::LO;
996 }
Eric Christopher543cf052010-09-01 22:16:27 +0000997}
998
Eric Christopher43b62be2010-09-27 06:02:23 +0000999bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +00001000 const BranchInst *BI = cast<BranchInst>(I);
1001 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1002 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +00001003
Eric Christophere5734102010-09-03 00:35:47 +00001004 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +00001005
Eric Christopher0e6233b2010-10-29 21:08:19 +00001006 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1007 // behavior.
1008 // TODO: Factor this out.
1009 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1010 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001011 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001012 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +00001013 if (!isTypeLegal(Ty, VT))
1014 return false;
1015
Eric Christopher0e6233b2010-10-29 21:08:19 +00001016 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1017 if (isFloat && !Subtarget->hasVFP2())
1018 return false;
1019
1020 unsigned CmpOpc;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001021 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001022 default: return false;
1023 // TODO: Verify compares.
1024 case MVT::f32:
1025 CmpOpc = ARM::VCMPES;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001026 break;
1027 case MVT::f64:
1028 CmpOpc = ARM::VCMPED;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001029 break;
1030 case MVT::i32:
1031 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher0e6233b2010-10-29 21:08:19 +00001032 break;
1033 }
1034
1035 // Get the compare predicate.
1036 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1037
1038 // We may not handle every CC for now.
1039 if (ARMPred == ARMCC::AL) return false;
1040
1041 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1042 if (Arg1 == 0) return false;
1043
1044 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1045 if (Arg2 == 0) return false;
1046
1047 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1048 TII.get(CmpOpc))
1049 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001050
Eric Christopher0e6233b2010-10-29 21:08:19 +00001051 // For floating point we need to move the result to a comparison register
1052 // that we can then use for branches.
1053 if (isFloat)
1054 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1055 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001056
Eric Christopher0e6233b2010-10-29 21:08:19 +00001057 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1059 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1060 FastEmitBranch(FBB, DL);
1061 FuncInfo.MBB->addSuccessor(TBB);
1062 return true;
1063 }
1064 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001065
Eric Christopher0e6233b2010-10-29 21:08:19 +00001066 unsigned CmpReg = getRegForValue(BI->getCondition());
1067 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001068
Eric Christopher229207a2010-09-29 01:14:47 +00001069 // Re-set the flags just in case.
1070 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1071 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001072 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001073
Eric Christophere5734102010-09-03 00:35:47 +00001074 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001076 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001077 FastEmitBranch(FBB, DL);
1078 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001079 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001080}
1081
Eric Christopher43b62be2010-09-27 06:02:23 +00001082bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001083 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001084
Duncan Sands1440e8b2010-11-03 11:35:31 +00001085 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001086 const Type *Ty = CI->getOperand(0)->getType();
1087 if (!isTypeLegal(Ty, VT))
1088 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001089
Eric Christopherd43393a2010-09-08 23:13:45 +00001090 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1091 if (isFloat && !Subtarget->hasVFP2())
1092 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001093
Eric Christopherd43393a2010-09-08 23:13:45 +00001094 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001095 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001096 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001097 default: return false;
1098 // TODO: Verify compares.
1099 case MVT::f32:
1100 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001101 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001102 break;
1103 case MVT::f64:
1104 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001105 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001106 break;
1107 case MVT::i32:
1108 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001109 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001110 break;
1111 }
1112
Eric Christopher229207a2010-09-29 01:14:47 +00001113 // Get the compare predicate.
1114 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001115
Eric Christopher229207a2010-09-29 01:14:47 +00001116 // We may not handle every CC for now.
1117 if (ARMPred == ARMCC::AL) return false;
1118
Eric Christopherd43393a2010-09-08 23:13:45 +00001119 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1120 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001121
Eric Christopherd43393a2010-09-08 23:13:45 +00001122 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1123 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001124
Eric Christopherd43393a2010-09-08 23:13:45 +00001125 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1126 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001127
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001128 // For floating point we need to move the result to a comparison register
1129 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001130 if (isFloat)
1131 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1132 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001133
Eric Christopher229207a2010-09-29 01:14:47 +00001134 // Now set a register based on the comparison. Explicitly set the predicates
1135 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001136 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001137 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001138 : ARM::GPRRegisterClass;
1139 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001140 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001141 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001142 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1144 .addReg(ZeroReg).addImm(1)
1145 .addImm(ARMPred).addReg(CondReg);
1146
Eric Christophera5b1e682010-09-17 22:28:18 +00001147 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001148 return true;
1149}
1150
Eric Christopher43b62be2010-09-27 06:02:23 +00001151bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001152 // Make sure we have VFP and that we're extending float to double.
1153 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001154
Eric Christopher46203602010-09-09 00:26:48 +00001155 Value *V = I->getOperand(0);
1156 if (!I->getType()->isDoubleTy() ||
1157 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001158
Eric Christopher46203602010-09-09 00:26:48 +00001159 unsigned Op = getRegForValue(V);
1160 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001161
Eric Christopher46203602010-09-09 00:26:48 +00001162 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001163 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001164 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001165 .addReg(Op));
1166 UpdateValueMap(I, Result);
1167 return true;
1168}
1169
Eric Christopher43b62be2010-09-27 06:02:23 +00001170bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001171 // Make sure we have VFP and that we're truncating double to float.
1172 if (!Subtarget->hasVFP2()) return false;
1173
1174 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001175 if (!(I->getType()->isFloatTy() &&
1176 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001177
1178 unsigned Op = getRegForValue(V);
1179 if (Op == 0) return false;
1180
1181 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001182 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001183 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001184 .addReg(Op));
1185 UpdateValueMap(I, Result);
1186 return true;
1187}
1188
Eric Christopher43b62be2010-09-27 06:02:23 +00001189bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001190 // Make sure we have VFP.
1191 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001192
Duncan Sands1440e8b2010-11-03 11:35:31 +00001193 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001194 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001195 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001196 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001197
Eric Christopher9a040492010-09-09 18:54:59 +00001198 unsigned Op = getRegForValue(I->getOperand(0));
1199 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001200
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001201 // The conversion routine works on fp-reg to fp-reg and the operand above
1202 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001203 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001204 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001205
Eric Christopher9a040492010-09-09 18:54:59 +00001206 unsigned Opc;
1207 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1208 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1209 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001210
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001211 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001212 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1213 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001214 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001215 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001216 return true;
1217}
1218
Eric Christopher43b62be2010-09-27 06:02:23 +00001219bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001220 // Make sure we have VFP.
1221 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001222
Duncan Sands1440e8b2010-11-03 11:35:31 +00001223 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001224 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001225 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001226 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001227
Eric Christopher9a040492010-09-09 18:54:59 +00001228 unsigned Op = getRegForValue(I->getOperand(0));
1229 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001230
Eric Christopher9a040492010-09-09 18:54:59 +00001231 unsigned Opc;
1232 const Type *OpTy = I->getOperand(0)->getType();
1233 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1234 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1235 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001236
Eric Christopher022b7fb2010-10-05 23:13:24 +00001237 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1238 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001239 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1240 ResultReg)
1241 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001242
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001243 // This result needs to be in an integer register, but the conversion only
1244 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001245 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001246 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001247
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001248 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001249 return true;
1250}
1251
Eric Christopher3bbd3962010-10-11 08:27:59 +00001252bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001253 MVT VT;
1254 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001255 return false;
1256
1257 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001258 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001259 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1260
1261 unsigned CondReg = getRegForValue(I->getOperand(0));
1262 if (CondReg == 0) return false;
1263 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1264 if (Op1Reg == 0) return false;
1265 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1266 if (Op2Reg == 0) return false;
1267
1268 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1270 .addReg(CondReg).addImm(1));
1271 unsigned ResultReg = createResultReg(RC);
1272 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1274 .addReg(Op1Reg).addReg(Op2Reg)
1275 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1276 UpdateValueMap(I, ResultReg);
1277 return true;
1278}
1279
Eric Christopher08637852010-09-30 22:34:19 +00001280bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001281 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001282 const Type *Ty = I->getType();
1283 if (!isTypeLegal(Ty, VT))
1284 return false;
1285
1286 // If we have integer div support we should have selected this automagically.
1287 // In case we have a real miss go ahead and return false and we'll pick
1288 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001289 if (Subtarget->hasDivide()) return false;
1290
Eric Christopher08637852010-09-30 22:34:19 +00001291 // Otherwise emit a libcall.
1292 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001293 if (VT == MVT::i8)
1294 LC = RTLIB::SDIV_I8;
1295 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001296 LC = RTLIB::SDIV_I16;
1297 else if (VT == MVT::i32)
1298 LC = RTLIB::SDIV_I32;
1299 else if (VT == MVT::i64)
1300 LC = RTLIB::SDIV_I64;
1301 else if (VT == MVT::i128)
1302 LC = RTLIB::SDIV_I128;
1303 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001304
Eric Christopher08637852010-09-30 22:34:19 +00001305 return ARMEmitLibcall(I, LC);
1306}
1307
Eric Christopher6a880d62010-10-11 08:37:26 +00001308bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001309 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001310 const Type *Ty = I->getType();
1311 if (!isTypeLegal(Ty, VT))
1312 return false;
1313
1314 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1315 if (VT == MVT::i8)
1316 LC = RTLIB::SREM_I8;
1317 else if (VT == MVT::i16)
1318 LC = RTLIB::SREM_I16;
1319 else if (VT == MVT::i32)
1320 LC = RTLIB::SREM_I32;
1321 else if (VT == MVT::i64)
1322 LC = RTLIB::SREM_I64;
1323 else if (VT == MVT::i128)
1324 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001325 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001326
Eric Christopher6a880d62010-10-11 08:37:26 +00001327 return ARMEmitLibcall(I, LC);
1328}
1329
Eric Christopher43b62be2010-09-27 06:02:23 +00001330bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001331 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001332
Eric Christopherbc39b822010-09-09 00:53:57 +00001333 // We can get here in the case when we want to use NEON for our fp
1334 // operations, but can't figure out how to. Just use the vfp instructions
1335 // if we have them.
1336 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001337 const Type *Ty = I->getType();
1338 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1339 if (isFloat && !Subtarget->hasVFP2())
1340 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001341
Eric Christopherbc39b822010-09-09 00:53:57 +00001342 unsigned Op1 = getRegForValue(I->getOperand(0));
1343 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001344
Eric Christopherbc39b822010-09-09 00:53:57 +00001345 unsigned Op2 = getRegForValue(I->getOperand(1));
1346 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001347
Eric Christopherbc39b822010-09-09 00:53:57 +00001348 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001349 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001350 switch (ISDOpcode) {
1351 default: return false;
1352 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001353 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001354 break;
1355 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001356 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001357 break;
1358 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001359 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001360 break;
1361 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001362 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001363 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1364 TII.get(Opc), ResultReg)
1365 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001366 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001367 return true;
1368}
1369
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001370// Call Handling Code
1371
Eric Christopherfa87d662010-10-18 02:17:53 +00001372bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1373 EVT SrcVT, unsigned &ResultReg) {
1374 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1375 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001376
Eric Christopherfa87d662010-10-18 02:17:53 +00001377 if (RR != 0) {
1378 ResultReg = RR;
1379 return true;
1380 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001381 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001382}
1383
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001384// This is largely taken directly from CCAssignFnForNode - we don't support
1385// varargs in FastISel so that part has been removed.
1386// TODO: We may not support all of this.
1387CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1388 switch (CC) {
1389 default:
1390 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001391 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001392 // Ignore fastcc. Silence compiler warnings.
1393 (void)RetFastCC_ARM_APCS;
1394 (void)FastCC_ARM_APCS;
1395 // Fallthrough
1396 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001397 // Use target triple & subtarget features to do actual dispatch.
1398 if (Subtarget->isAAPCS_ABI()) {
1399 if (Subtarget->hasVFP2() &&
1400 FloatABIType == FloatABI::Hard)
1401 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1402 else
1403 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1404 } else
1405 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1406 case CallingConv::ARM_AAPCS_VFP:
1407 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1408 case CallingConv::ARM_AAPCS:
1409 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1410 case CallingConv::ARM_APCS:
1411 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1412 }
1413}
1414
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001415bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1416 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001417 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001418 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1419 SmallVectorImpl<unsigned> &RegArgs,
1420 CallingConv::ID CC,
1421 unsigned &NumBytes) {
1422 SmallVector<CCValAssign, 16> ArgLocs;
1423 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1424 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1425
1426 // Get a count of how many bytes are to be pushed on the stack.
1427 NumBytes = CCInfo.getNextStackOffset();
1428
1429 // Issue CALLSEQ_START
1430 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001431 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1432 TII.get(AdjStackDown))
1433 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001434
1435 // Process the args.
1436 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1437 CCValAssign &VA = ArgLocs[i];
1438 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001439 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001440
Eric Christopher4a2b3162011-01-27 05:44:56 +00001441 // We don't handle NEON/vector parameters yet.
1442 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
Eric Christophera4633f52010-10-23 09:37:17 +00001443 return false;
1444
Eric Christopherf9764fa2010-09-30 20:49:44 +00001445 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001446 switch (VA.getLocInfo()) {
1447 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001448 case CCValAssign::SExt: {
1449 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1450 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001451 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001452 Emitted = true;
1453 ArgVT = VA.getLocVT();
1454 break;
1455 }
1456 case CCValAssign::ZExt: {
1457 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1458 Arg, ArgVT, Arg);
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001459 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001460 Emitted = true;
1461 ArgVT = VA.getLocVT();
1462 break;
1463 }
1464 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001465 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1466 Arg, ArgVT, Arg);
1467 if (!Emitted)
1468 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1469 Arg, ArgVT, Arg);
1470 if (!Emitted)
1471 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1472 Arg, ArgVT, Arg);
1473
Chris Lattner54c6d6f2011-01-05 18:41:05 +00001474 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Eric Christopherfa87d662010-10-18 02:17:53 +00001475 ArgVT = VA.getLocVT();
1476 break;
1477 }
1478 case CCValAssign::BCvt: {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001479 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001480 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001481 assert(BC != 0 && "Failed to emit a bitcast!");
1482 Arg = BC;
1483 ArgVT = VA.getLocVT();
1484 break;
1485 }
1486 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001487 }
1488
1489 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001490 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001491 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001492 VA.getLocReg())
1493 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001494 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001495 } else if (VA.needsCustom()) {
1496 // TODO: We need custom lowering for vector (v2f64) args.
1497 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001498
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001499 CCValAssign &NextVA = ArgLocs[++i];
1500
1501 // TODO: Only handle register args for now.
1502 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1503
1504 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1505 TII.get(ARM::VMOVRRD), VA.getLocReg())
1506 .addReg(NextVA.getLocReg(), RegState::Define)
1507 .addReg(Arg));
1508 RegArgs.push_back(VA.getLocReg());
1509 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001510 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001511 assert(VA.isMemLoc());
1512 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001513 Address Addr;
1514 Addr.BaseType = Address::RegBase;
1515 Addr.Base.Reg = ARM::SP;
1516 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001517
Eric Christopher0d581222010-11-19 22:30:02 +00001518 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001519 }
1520 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001521 return true;
1522}
1523
Duncan Sands1440e8b2010-11-03 11:35:31 +00001524bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001525 const Instruction *I, CallingConv::ID CC,
1526 unsigned &NumBytes) {
1527 // Issue CALLSEQ_END
1528 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001529 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1530 TII.get(AdjStackUp))
1531 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001532
1533 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001534 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001535 SmallVector<CCValAssign, 16> RVLocs;
1536 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1537 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1538
1539 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001540 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001541 // For this move we copy into two registers and then move into the
1542 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001543 EVT DestVT = RVLocs[0].getValVT();
1544 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1545 unsigned ResultReg = createResultReg(DstRC);
1546 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1547 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001548 .addReg(RVLocs[0].getLocReg())
1549 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001550
Eric Christopher3659ac22010-10-20 08:02:24 +00001551 UsedRegs.push_back(RVLocs[0].getLocReg());
1552 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001553
Eric Christopherdccd2c32010-10-11 08:38:55 +00001554 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001555 UpdateValueMap(I, ResultReg);
1556 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001557 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001558 EVT CopyVT = RVLocs[0].getValVT();
1559 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001560
Eric Christopher14df8822010-10-01 00:00:11 +00001561 unsigned ResultReg = createResultReg(DstRC);
1562 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1563 ResultReg).addReg(RVLocs[0].getLocReg());
1564 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001565
Eric Christopherdccd2c32010-10-11 08:38:55 +00001566 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001567 UpdateValueMap(I, ResultReg);
1568 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001569 }
1570
Eric Christopherdccd2c32010-10-11 08:38:55 +00001571 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001572}
1573
Eric Christopher4f512ef2010-10-22 01:28:00 +00001574bool ARMFastISel::SelectRet(const Instruction *I) {
1575 const ReturnInst *Ret = cast<ReturnInst>(I);
1576 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001577
Eric Christopher4f512ef2010-10-22 01:28:00 +00001578 if (!FuncInfo.CanLowerReturn)
1579 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001580
Eric Christopher4f512ef2010-10-22 01:28:00 +00001581 if (F.isVarArg())
1582 return false;
1583
1584 CallingConv::ID CC = F.getCallingConv();
1585 if (Ret->getNumOperands() > 0) {
1586 SmallVector<ISD::OutputArg, 4> Outs;
1587 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1588 Outs, TLI);
1589
1590 // Analyze operands of the call, assigning locations to each operand.
1591 SmallVector<CCValAssign, 16> ValLocs;
1592 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1593 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1594
1595 const Value *RV = Ret->getOperand(0);
1596 unsigned Reg = getRegForValue(RV);
1597 if (Reg == 0)
1598 return false;
1599
1600 // Only handle a single return value for now.
1601 if (ValLocs.size() != 1)
1602 return false;
1603
1604 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001605
Eric Christopher4f512ef2010-10-22 01:28:00 +00001606 // Don't bother handling odd stuff for now.
1607 if (VA.getLocInfo() != CCValAssign::Full)
1608 return false;
1609 // Only handle register returns for now.
1610 if (!VA.isRegLoc())
1611 return false;
1612 // TODO: For now, don't try to handle cases where getLocInfo()
1613 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001614 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001615 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001616
Eric Christopher4f512ef2010-10-22 01:28:00 +00001617 // Make the copy.
1618 unsigned SrcReg = Reg + VA.getValNo();
1619 unsigned DstReg = VA.getLocReg();
1620 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1621 // Avoid a cross-class copy. This is very unlikely.
1622 if (!SrcRC->contains(DstReg))
1623 return false;
1624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1625 DstReg).addReg(SrcReg);
1626
1627 // Mark the register as live out of the function.
1628 MRI.addLiveOut(VA.getLocReg());
1629 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001630
Eric Christopher4f512ef2010-10-22 01:28:00 +00001631 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1632 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1633 TII.get(RetOpc)));
1634 return true;
1635}
1636
Eric Christopher872f4a22011-02-22 01:37:10 +00001637unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1638
1639 // Depend our opcode for thumb on whether or not we're targeting an
1640 // externally callable function. For libcalls we'll just pass a NULL GV
1641 // in here.
1642 bool isExternal = false;
1643 if (!GV || GV->hasExternalLinkage()) isExternal = true;
1644
1645 // Darwin needs the r9 versions of the opcodes.
1646 bool isDarwin = Subtarget->isTargetDarwin();
1647 if (isThumb && isExternal) {
1648 return isDarwin ? ARM::tBLXi_r9 : ARM::tBLXi;
1649 } else if (isThumb) {
1650 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1651 } else {
1652 return isDarwin ? ARM::BLr9 : ARM::BL;
1653 }
1654}
1655
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001656// A quick function that will emit a call for a named libcall in F with the
1657// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001658// can emit a call for any libcall we can produce. This is an abridged version
1659// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001660// like computed function pointers or strange arguments at call sites.
1661// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1662// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001663bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1664 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001665
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001666 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001667 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001668 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001669 if (RetTy->isVoidTy())
1670 RetVT = MVT::isVoid;
1671 else if (!isTypeLegal(RetTy, RetVT))
1672 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001673
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001674 // For now we're using BLX etc on the assumption that we have v5t ops.
1675 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001676
Eric Christopher836c6242010-12-15 23:47:29 +00001677 // TODO: For now if we have long calls specified we don't handle the call.
1678 if (EnableARMLongCalls) return false;
1679
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001680 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001681 SmallVector<Value*, 8> Args;
1682 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001683 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001684 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1685 Args.reserve(I->getNumOperands());
1686 ArgRegs.reserve(I->getNumOperands());
1687 ArgVTs.reserve(I->getNumOperands());
1688 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001689 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001690 Value *Op = I->getOperand(i);
1691 unsigned Arg = getRegForValue(Op);
1692 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001693
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001694 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001695 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001696 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001698 ISD::ArgFlagsTy Flags;
1699 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1700 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001701
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001702 Args.push_back(Op);
1703 ArgRegs.push_back(Arg);
1704 ArgVTs.push_back(ArgVT);
1705 ArgFlags.push_back(Flags);
1706 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001707
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001708 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001709 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001710 unsigned NumBytes;
1711 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1712 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001713
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001714 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001715 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001716 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001717 unsigned CallOpc = ARMSelectCallOp(NULL);
1718 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001719 // Explicitly adding the predicate here.
1720 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1721 TII.get(CallOpc)))
1722 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopher872f4a22011-02-22 01:37:10 +00001723 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001724 // Explicitly adding the predicate here.
1725 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1726 TII.get(CallOpc))
1727 .addExternalSymbol(TLI.getLibcallName(Call)));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001728
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001729 // Add implicit physical register uses to the call.
1730 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1731 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001732
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001733 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001734 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001735 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001736
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001737 // Set all unused physreg defs as dead.
1738 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001739
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001740 return true;
1741}
1742
Eric Christopherf9764fa2010-09-30 20:49:44 +00001743bool ARMFastISel::SelectCall(const Instruction *I) {
1744 const CallInst *CI = cast<CallInst>(I);
1745 const Value *Callee = CI->getCalledValue();
1746
1747 // Can't handle inline asm or worry about intrinsics yet.
1748 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1749
Eric Christophere6ca6772010-10-01 21:33:12 +00001750 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001751 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001752 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1753 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001754
Eric Christopherf9764fa2010-09-30 20:49:44 +00001755 // Check the calling convention.
1756 ImmutableCallSite CS(CI);
1757 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001758
Eric Christopherf9764fa2010-09-30 20:49:44 +00001759 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001760
Eric Christopherf9764fa2010-09-30 20:49:44 +00001761 // Let SDISel handle vararg functions.
1762 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1763 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1764 if (FTy->isVarArg())
1765 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001766
Eric Christopherf9764fa2010-09-30 20:49:44 +00001767 // Handle *simple* calls for now.
1768 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001769 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001770 if (RetTy->isVoidTy())
1771 RetVT = MVT::isVoid;
1772 else if (!isTypeLegal(RetTy, RetVT))
1773 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001774
Eric Christopherf9764fa2010-09-30 20:49:44 +00001775 // For now we're using BLX etc on the assumption that we have v5t ops.
1776 // TODO: Maybe?
1777 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001778
Eric Christopher836c6242010-12-15 23:47:29 +00001779 // TODO: For now if we have long calls specified we don't handle the call.
1780 if (EnableARMLongCalls) return false;
1781
Eric Christopherf9764fa2010-09-30 20:49:44 +00001782 // Set up the argument vectors.
1783 SmallVector<Value*, 8> Args;
1784 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001785 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001786 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1787 Args.reserve(CS.arg_size());
1788 ArgRegs.reserve(CS.arg_size());
1789 ArgVTs.reserve(CS.arg_size());
1790 ArgFlags.reserve(CS.arg_size());
1791 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1792 i != e; ++i) {
1793 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001794
Eric Christopherf9764fa2010-09-30 20:49:44 +00001795 if (Arg == 0)
1796 return false;
1797 ISD::ArgFlagsTy Flags;
1798 unsigned AttrInd = i - CS.arg_begin() + 1;
1799 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1800 Flags.setSExt();
1801 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1802 Flags.setZExt();
1803
1804 // FIXME: Only handle *easy* calls for now.
1805 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1806 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1807 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1808 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1809 return false;
1810
1811 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001812 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001813 if (!isTypeLegal(ArgTy, ArgVT))
1814 return false;
1815 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1816 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001817
Eric Christopherf9764fa2010-09-30 20:49:44 +00001818 Args.push_back(*i);
1819 ArgRegs.push_back(Arg);
1820 ArgVTs.push_back(ArgVT);
1821 ArgFlags.push_back(Flags);
1822 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001823
Eric Christopherf9764fa2010-09-30 20:49:44 +00001824 // Handle the arguments now that we've gotten them.
1825 SmallVector<unsigned, 4> RegArgs;
1826 unsigned NumBytes;
1827 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1828 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001829
Eric Christopherf9764fa2010-09-30 20:49:44 +00001830 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001831 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001832 MachineInstrBuilder MIB;
Eric Christopher872f4a22011-02-22 01:37:10 +00001833 unsigned CallOpc = ARMSelectCallOp(GV);
Eric Christopher7bb59962010-11-29 21:56:23 +00001834 // Explicitly adding the predicate here.
Eric Christopher872f4a22011-02-22 01:37:10 +00001835 if(isThumb)
Eric Christopherc19aadb2010-12-21 03:50:43 +00001836 // Explicitly adding the predicate here.
1837 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1838 TII.get(CallOpc)))
1839 .addGlobalAddress(GV, 0, 0);
Eric Christopher872f4a22011-02-22 01:37:10 +00001840 else
Eric Christopherc19aadb2010-12-21 03:50:43 +00001841 // Explicitly adding the predicate here.
1842 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1843 TII.get(CallOpc))
1844 .addGlobalAddress(GV, 0, 0));
Eric Christopherc19aadb2010-12-21 03:50:43 +00001845
Eric Christopherf9764fa2010-09-30 20:49:44 +00001846 // Add implicit physical register uses to the call.
1847 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1848 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001849
Eric Christopherf9764fa2010-09-30 20:49:44 +00001850 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001851 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001852 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001853
Eric Christopherf9764fa2010-09-30 20:49:44 +00001854 // Set all unused physreg defs as dead.
1855 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001856
Eric Christopherf9764fa2010-09-30 20:49:44 +00001857 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001858
Eric Christopherf9764fa2010-09-30 20:49:44 +00001859}
1860
Eric Christopher56d2b722010-09-02 23:43:26 +00001861// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001862bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001863
Eric Christopherab695882010-07-21 22:26:11 +00001864 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001865 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001866 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001867 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001868 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001869 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001870 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001871 case Instruction::ICmp:
1872 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001873 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001874 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001875 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001876 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001877 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001878 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001879 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001880 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001881 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001882 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001883 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001884 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001885 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001886 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001887 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001888 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001889 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001890 case Instruction::SRem:
1891 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001892 case Instruction::Call:
1893 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001894 case Instruction::Select:
1895 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001896 case Instruction::Ret:
1897 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001898 default: break;
1899 }
1900 return false;
1901}
1902
1903namespace llvm {
1904 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001905 // Completely untested on non-darwin.
1906 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001907
Eric Christopheraaa8df42010-11-02 01:21:28 +00001908 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001909 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001910 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001911 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001912 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001913 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001914 }
1915}