blob: 719cf5a61b5e1cb4a5a639cf6a0cdc895849eca9 [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 Christopherab695882010-07-21 22:26:11 +000055namespace {
Eric Christopher0d581222010-11-19 22:30:02 +000056
57 // All possible address modes, plus some.
58 typedef struct Address {
59 enum {
60 RegBase,
61 FrameIndexBase
62 } BaseType;
63
64 union {
65 unsigned Reg;
66 int FI;
67 } Base;
68
69 int Offset;
70 unsigned Scale;
71 unsigned PlusReg;
72
73 // Innocuous defaults for our address.
74 Address()
75 : BaseType(RegBase), Offset(0), Scale(0), PlusReg(0) {
76 Base.Reg = 0;
77 }
78 } Address;
Eric Christopherab695882010-07-21 22:26:11 +000079
80class ARMFastISel : public FastISel {
81
82 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
83 /// make the right decision when generating code for different targets.
84 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000085 const TargetMachine &TM;
86 const TargetInstrInfo &TII;
87 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000088 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000089
Eric Christopher8cf6c602010-09-29 22:24:45 +000090 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000091 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000092 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000093
Eric Christopherab695882010-07-21 22:26:11 +000094 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000095 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000096 : FastISel(funcInfo),
97 TM(funcInfo.MF->getTarget()),
98 TII(*TM.getInstrInfo()),
99 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +0000100 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +0000101 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +0000102 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +0000103 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +0000104 }
105
Eric Christophercb592292010-08-20 00:20:31 +0000106 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +0000107 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
108 const TargetRegisterClass *RC);
109 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill);
112 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
113 const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill,
115 unsigned Op1, bool Op1IsKill);
116 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
117 const TargetRegisterClass *RC,
118 unsigned Op0, bool Op0IsKill,
119 uint64_t Imm);
120 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
121 const TargetRegisterClass *RC,
122 unsigned Op0, bool Op0IsKill,
123 const ConstantFP *FPImm);
124 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
125 const TargetRegisterClass *RC,
126 uint64_t Imm);
127 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
128 const TargetRegisterClass *RC,
129 unsigned Op0, bool Op0IsKill,
130 unsigned Op1, bool Op1IsKill,
131 uint64_t Imm);
132 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
133 unsigned Op0, bool Op0IsKill,
134 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000135
Eric Christophercb592292010-08-20 00:20:31 +0000136 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000137 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000138 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000139 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000140
141 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000142
Eric Christopher83007122010-08-23 21:44:12 +0000143 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000144 private:
Eric Christopher17787722010-10-21 21:47:51 +0000145 bool SelectLoad(const Instruction *I);
146 bool SelectStore(const Instruction *I);
147 bool SelectBranch(const Instruction *I);
148 bool SelectCmp(const Instruction *I);
149 bool SelectFPExt(const Instruction *I);
150 bool SelectFPTrunc(const Instruction *I);
151 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
152 bool SelectSIToFP(const Instruction *I);
153 bool SelectFPToSI(const Instruction *I);
154 bool SelectSDiv(const Instruction *I);
155 bool SelectSRem(const Instruction *I);
156 bool SelectCall(const Instruction *I);
157 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000158 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000159
Eric Christopher83007122010-08-23 21:44:12 +0000160 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000161 private:
Duncan Sands1440e8b2010-11-03 11:35:31 +0000162 bool isTypeLegal(const Type *Ty, MVT &VT);
163 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
Eric Christopher0d581222010-11-19 22:30:02 +0000164 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr);
165 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
166 bool ARMComputeAddress(const Value *Obj, Address &Addr);
167 void ARMSimplifyAddress(Address &Addr, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000168 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000169 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000170 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000171 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000172 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000173
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000174 // Call handling routines.
175 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000176 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
177 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000178 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000179 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000180 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000181 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000182 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
183 SmallVectorImpl<unsigned> &RegArgs,
184 CallingConv::ID CC,
185 unsigned &NumBytes);
Duncan Sands1440e8b2010-11-03 11:35:31 +0000186 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000187 const Instruction *I, CallingConv::ID CC,
188 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000189 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000190
191 // OptionalDef handling routines.
192 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000193 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
194 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
195};
Eric Christopherab695882010-07-21 22:26:11 +0000196
197} // end anonymous namespace
198
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000199#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000200
Eric Christopher456144e2010-08-19 00:37:05 +0000201// DefinesOptionalPredicate - This is different from DefinesPredicate in that
202// we don't care about implicit defs here, just places we'll need to add a
203// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
204bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
205 const TargetInstrDesc &TID = MI->getDesc();
206 if (!TID.hasOptionalDef())
207 return false;
208
209 // Look to see if our OptionalDef is defining CPSR or CCR.
210 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
211 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000212 if (!MO.isReg() || !MO.isDef()) continue;
213 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000214 *CPSR = true;
215 }
216 return true;
217}
218
219// If the machine is predicable go ahead and add the predicate operands, if
220// it needs default CC operands add those.
Eric Christopheraaa8df42010-11-02 01:21:28 +0000221// TODO: If we want to support thumb1 then we'll need to deal with optional
222// CPSR defs that need to be added before the remaining operands. See s_cc_out
223// for descriptions why.
Eric Christopher456144e2010-08-19 00:37:05 +0000224const MachineInstrBuilder &
225ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
226 MachineInstr *MI = &*MIB;
227
228 // Do we use a predicate?
229 if (TII.isPredicable(MI))
230 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000231
Eric Christopher456144e2010-08-19 00:37:05 +0000232 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
233 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000234 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000235 if (DefinesOptionalPredicate(MI, &CPSR)) {
236 if (CPSR)
237 AddDefaultT1CC(MIB);
238 else
239 AddDefaultCC(MIB);
240 }
241 return MIB;
242}
243
Eric Christopher0fe7d542010-08-17 01:25:29 +0000244unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
245 const TargetRegisterClass* RC) {
246 unsigned ResultReg = createResultReg(RC);
247 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
248
Eric Christopher456144e2010-08-19 00:37:05 +0000249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000250 return ResultReg;
251}
252
253unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
254 const TargetRegisterClass *RC,
255 unsigned Op0, bool Op0IsKill) {
256 unsigned ResultReg = createResultReg(RC);
257 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
258
259 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000261 .addReg(Op0, Op0IsKill * RegState::Kill));
262 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000264 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000266 TII.get(TargetOpcode::COPY), ResultReg)
267 .addReg(II.ImplicitDefs[0]));
268 }
269 return ResultReg;
270}
271
272unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
273 const TargetRegisterClass *RC,
274 unsigned Op0, bool Op0IsKill,
275 unsigned Op1, bool Op1IsKill) {
276 unsigned ResultReg = createResultReg(RC);
277 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
278
279 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281 .addReg(Op0, Op0IsKill * RegState::Kill)
282 .addReg(Op1, Op1IsKill * RegState::Kill));
283 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000284 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000285 .addReg(Op0, Op0IsKill * RegState::Kill)
286 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 TII.get(TargetOpcode::COPY), ResultReg)
289 .addReg(II.ImplicitDefs[0]));
290 }
291 return ResultReg;
292}
293
294unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
295 const TargetRegisterClass *RC,
296 unsigned Op0, bool Op0IsKill,
297 uint64_t Imm) {
298 unsigned ResultReg = createResultReg(RC);
299 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
300
301 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 .addReg(Op0, Op0IsKill * RegState::Kill)
304 .addImm(Imm));
305 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 .addReg(Op0, Op0IsKill * RegState::Kill)
308 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 TII.get(TargetOpcode::COPY), ResultReg)
311 .addReg(II.ImplicitDefs[0]));
312 }
313 return ResultReg;
314}
315
316unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
317 const TargetRegisterClass *RC,
318 unsigned Op0, bool Op0IsKill,
319 const ConstantFP *FPImm) {
320 unsigned ResultReg = createResultReg(RC);
321 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
322
323 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addFPImm(FPImm));
327 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000332 TII.get(TargetOpcode::COPY), ResultReg)
333 .addReg(II.ImplicitDefs[0]));
334 }
335 return ResultReg;
336}
337
338unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
339 const TargetRegisterClass *RC,
340 unsigned Op0, bool Op0IsKill,
341 unsigned Op1, bool Op1IsKill,
342 uint64_t Imm) {
343 unsigned ResultReg = createResultReg(RC);
344 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
345
346 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348 .addReg(Op0, Op0IsKill * RegState::Kill)
349 .addReg(Op1, Op1IsKill * RegState::Kill)
350 .addImm(Imm));
351 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addReg(Op1, Op1IsKill * RegState::Kill)
355 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000357 TII.get(TargetOpcode::COPY), ResultReg)
358 .addReg(II.ImplicitDefs[0]));
359 }
360 return ResultReg;
361}
362
363unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
364 const TargetRegisterClass *RC,
365 uint64_t Imm) {
366 unsigned ResultReg = createResultReg(RC);
367 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000368
Eric Christopher0fe7d542010-08-17 01:25:29 +0000369 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000371 .addImm(Imm));
372 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000374 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000376 TII.get(TargetOpcode::COPY), ResultReg)
377 .addReg(II.ImplicitDefs[0]));
378 }
379 return ResultReg;
380}
381
382unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
383 unsigned Op0, bool Op0IsKill,
384 uint32_t Idx) {
385 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
386 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
387 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000389 DL, TII.get(TargetOpcode::COPY), ResultReg)
390 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
391 return ResultReg;
392}
393
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000394// TODO: Don't worry about 64-bit now, but when this is fixed remove the
395// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000396unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000397 if (VT == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000398
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000399 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
401 TII.get(ARM::VMOVRS), MoveReg)
402 .addReg(SrcReg));
403 return MoveReg;
404}
405
406unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Duncan Sandscdfad362010-11-03 12:17:33 +0000407 if (VT == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000408
Eric Christopheraa3ace12010-09-09 20:49:25 +0000409 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
410 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000411 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000412 .addReg(SrcReg));
413 return MoveReg;
414}
415
Eric Christopher9ed58df2010-09-09 00:19:41 +0000416// For double width floating point we need to materialize two constants
417// (the high and the low) into integer registers then use a move to get
418// the combined constant into an FP reg.
419unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
420 const APFloat Val = CFP->getValueAPF();
Duncan Sandscdfad362010-11-03 12:17:33 +0000421 bool is64bit = VT == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000422
Eric Christopher9ed58df2010-09-09 00:19:41 +0000423 // This checks to see if we can use VFP3 instructions to materialize
424 // a constant, otherwise we have to go through the constant pool.
425 if (TLI.isFPImmLegal(Val, VT)) {
426 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
427 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
428 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
429 DestReg)
430 .addFPImm(CFP));
431 return DestReg;
432 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000433
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000434 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000435 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000436
Eric Christopher238bb162010-09-09 23:50:00 +0000437 // MachineConstantPool wants an explicit alignment.
438 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
439 if (Align == 0) {
440 // TODO: Figure out if this is correct.
441 Align = TD.getTypeAllocSize(CFP->getType());
442 }
443 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
444 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
445 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000446
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000447 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000448 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
449 DestReg)
450 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000451 .addReg(0));
452 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000453}
454
Eric Christopher744c7c82010-09-28 22:47:54 +0000455unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000456
Eric Christopher744c7c82010-09-28 22:47:54 +0000457 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000458 if (VT != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000459
Eric Christophere5b13cf2010-11-03 20:21:17 +0000460 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
461
462 // If we can do this in a single instruction without a constant pool entry
463 // do so now.
464 const ConstantInt *CI = cast<ConstantInt>(C);
Eric Christopher5e262bc2010-11-06 07:53:11 +0000465 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
Eric Christophere5b13cf2010-11-03 20:21:17 +0000466 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
467 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Jim Grosbach3ea4daa2010-11-19 18:01:37 +0000468 TII.get(Opc), DestReg)
469 .addImm(CI->getSExtValue()));
Eric Christophere5b13cf2010-11-03 20:21:17 +0000470 return DestReg;
471 }
472
Eric Christopher56d2b722010-09-02 23:43:26 +0000473 // MachineConstantPool wants an explicit alignment.
474 unsigned Align = TD.getPrefTypeAlignment(C->getType());
475 if (Align == 0) {
476 // TODO: Figure out if this is correct.
477 Align = TD.getTypeAllocSize(C->getType());
478 }
479 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000480
Eric Christopher56d2b722010-09-02 23:43:26 +0000481 if (isThumb)
482 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000483 TII.get(ARM::t2LDRpci), DestReg)
484 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000485 else
Eric Christopherd0c82a62010-11-12 09:48:30 +0000486 // The extra immediate is for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000487 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000488 TII.get(ARM::LDRcp), DestReg)
489 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000490 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000491
Eric Christopher56d2b722010-09-02 23:43:26 +0000492 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000493}
494
Eric Christopherc9932f62010-10-01 23:24:42 +0000495unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000496 // For now 32-bit only.
Duncan Sandscdfad362010-11-03 12:17:33 +0000497 if (VT != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000498
Eric Christopher890dbbe2010-10-02 00:32:44 +0000499 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000500
Eric Christopher890dbbe2010-10-02 00:32:44 +0000501 // TODO: No external globals for now.
502 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000503
Eric Christopher890dbbe2010-10-02 00:32:44 +0000504 // TODO: Need more magic for ARM PIC.
505 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000506
Eric Christopher890dbbe2010-10-02 00:32:44 +0000507 // MachineConstantPool wants an explicit alignment.
508 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
509 if (Align == 0) {
510 // TODO: Figure out if this is correct.
511 Align = TD.getTypeAllocSize(GV->getType());
512 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000513
Eric Christopher890dbbe2010-10-02 00:32:44 +0000514 // Grab index.
515 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
516 unsigned Id = AFI->createConstPoolEntryUId();
517 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
518 ARMCP::CPValue, PCAdj);
519 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000520
Eric Christopher890dbbe2010-10-02 00:32:44 +0000521 // Load value.
522 MachineInstrBuilder MIB;
523 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
524 if (isThumb) {
525 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
526 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
527 .addConstantPoolIndex(Idx);
528 if (RelocM == Reloc::PIC_)
529 MIB.addImm(Id);
530 } else {
Eric Christopherd0c82a62010-11-12 09:48:30 +0000531 // The extra immediate is for addrmode2.
Eric Christopher890dbbe2010-10-02 00:32:44 +0000532 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
533 DestReg)
534 .addConstantPoolIndex(Idx)
Eric Christopherd0c82a62010-11-12 09:48:30 +0000535 .addImm(0);
Eric Christopher890dbbe2010-10-02 00:32:44 +0000536 }
537 AddOptionalDefs(MIB);
538 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000539}
540
Eric Christopher9ed58df2010-09-09 00:19:41 +0000541unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
542 EVT VT = TLI.getValueType(C->getType(), true);
543
544 // Only handle simple types.
545 if (!VT.isSimple()) return 0;
546
547 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
548 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000549 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
550 return ARMMaterializeGV(GV, VT);
551 else if (isa<ConstantInt>(C))
552 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000553
Eric Christopherc9932f62010-10-01 23:24:42 +0000554 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000555}
556
Eric Christopherf9764fa2010-09-30 20:49:44 +0000557unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
558 // Don't handle dynamic allocas.
559 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000560
Duncan Sands1440e8b2010-11-03 11:35:31 +0000561 MVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000562 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000563
Eric Christopherf9764fa2010-09-30 20:49:44 +0000564 DenseMap<const AllocaInst*, int>::iterator SI =
565 FuncInfo.StaticAllocaMap.find(AI);
566
567 // This will get lowered later into the correct offsets and registers
568 // via rewriteXFrameIndex.
569 if (SI != FuncInfo.StaticAllocaMap.end()) {
570 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
571 unsigned ResultReg = createResultReg(RC);
572 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
573 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
574 TII.get(Opc), ResultReg)
575 .addFrameIndex(SI->second)
576 .addImm(0));
577 return ResultReg;
578 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000579
Eric Christopherf9764fa2010-09-30 20:49:44 +0000580 return 0;
581}
582
Duncan Sands1440e8b2010-11-03 11:35:31 +0000583bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
584 EVT evt = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000585
Eric Christopherb1cc8482010-08-25 07:23:49 +0000586 // Only handle simple types.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000587 if (evt == MVT::Other || !evt.isSimple()) return false;
588 VT = evt.getSimpleVT();
Eric Christopherac1a19e2010-09-09 01:06:51 +0000589
Eric Christopherdc908042010-08-31 01:28:42 +0000590 // Handle all legal types, i.e. a register that will directly hold this
591 // value.
592 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000593}
594
Duncan Sands1440e8b2010-11-03 11:35:31 +0000595bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000596 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000597
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000598 // If this is a type than can be sign or zero-extended to a basic operation
599 // go ahead and accept it now.
600 if (VT == MVT::i8 || VT == MVT::i16)
601 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000602
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000603 return false;
604}
605
Eric Christopher88de86b2010-11-19 22:36:41 +0000606// Computes the address to get to an object.
Eric Christopher0d581222010-11-19 22:30:02 +0000607bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
Eric Christopher83007122010-08-23 21:44:12 +0000608 // Some boilerplate from the X86 FastISel.
609 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000610 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000611 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher2d630d72010-11-19 22:37:58 +0000612 // Don't walk into other basic blocks unless the object is an alloca from
613 // another block, otherwise it may not have a virtual register assigned.
Eric Christopher76dda7e2010-11-15 21:11:06 +0000614 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
615 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
616 Opcode = I->getOpcode();
617 U = I;
618 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000619 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000620 Opcode = C->getOpcode();
621 U = C;
622 }
623
Eric Christophercb0b04b2010-08-24 00:07:24 +0000624 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000625 if (Ty->getAddressSpace() > 255)
626 // Fast instruction selection doesn't support the special
627 // address spaces.
628 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000629
Eric Christopher83007122010-08-23 21:44:12 +0000630 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000631 default:
Eric Christopher83007122010-08-23 21:44:12 +0000632 break;
Eric Christopher55324332010-10-12 00:43:21 +0000633 case Instruction::BitCast: {
634 // Look through bitcasts.
Eric Christopher0d581222010-11-19 22:30:02 +0000635 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000636 }
637 case Instruction::IntToPtr: {
638 // Look past no-op inttoptrs.
639 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000640 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000641 break;
642 }
643 case Instruction::PtrToInt: {
644 // Look past no-op ptrtoints.
645 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christopher0d581222010-11-19 22:30:02 +0000646 return ARMComputeAddress(U->getOperand(0), Addr);
Eric Christopher55324332010-10-12 00:43:21 +0000647 break;
648 }
Eric Christophereae84392010-10-14 09:29:41 +0000649 case Instruction::GetElementPtr: {
Eric Christopher0d581222010-11-19 22:30:02 +0000650 int SavedOffset = Addr.Offset;
651 unsigned SavedBase = Addr.Base.Reg;
652 int TmpOffset = Addr.Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000653
Eric Christophereae84392010-10-14 09:29:41 +0000654 // Iterate through the GEP folding the constants into offsets where
655 // we can.
656 gep_type_iterator GTI = gep_type_begin(U);
657 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
658 i != e; ++i, ++GTI) {
659 const Value *Op = *i;
660 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
661 const StructLayout *SL = TD.getStructLayout(STy);
662 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
663 TmpOffset += SL->getElementOffset(Idx);
664 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000665 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
666 SmallVector<const Value *, 4> Worklist;
667 Worklist.push_back(Op);
668 do {
669 Op = Worklist.pop_back_val();
670 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
671 // Constant-offset addressing.
672 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000673 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000674 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
675 // An add with a constant operand. Fold the constant.
676 ConstantInt *CI =
677 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
678 TmpOffset += CI->getSExtValue() * S;
679 // Add the other operand back to the work list.
680 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
681 } else
682 goto unsupported_gep;
683 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000684 }
685 }
Eric Christopher2896df82010-10-15 18:02:07 +0000686
687 // Try to grab the base operand now.
Eric Christopher0d581222010-11-19 22:30:02 +0000688 Addr.Offset = TmpOffset;
689 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000690
691 // We failed, restore everything and try the other options.
Eric Christopher0d581222010-11-19 22:30:02 +0000692 Addr.Offset = SavedOffset;
693 Addr.Base.Reg = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000694
Eric Christophereae84392010-10-14 09:29:41 +0000695 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000696 break;
697 }
Eric Christopher83007122010-08-23 21:44:12 +0000698 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000699 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000700 unsigned Reg = TargetMaterializeAlloca(AI);
701
702 if (Reg == 0) return false;
703
Eric Christopher0d581222010-11-19 22:30:02 +0000704 Addr.Base.Reg = Reg;
Eric Christopherd56d61a2010-10-17 01:51:42 +0000705 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000706 }
707 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000708
Eric Christophera9c57512010-10-13 21:41:51 +0000709 // Materialize the global variable's address into a reg which can
710 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000711 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000712 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
713 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000714
Eric Christopher0d581222010-11-19 22:30:02 +0000715 Addr.Base.Reg = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000716 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000717 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000718
Eric Christophercb0b04b2010-08-24 00:07:24 +0000719 // Try to get this in a register if nothing else has worked.
Eric Christopher0d581222010-11-19 22:30:02 +0000720 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
721 return Addr.Base.Reg != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000722}
723
Eric Christopher0d581222010-11-19 22:30:02 +0000724void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000725
Eric Christopher212ae932010-10-21 19:40:30 +0000726 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000727
Eric Christopher212ae932010-10-21 19:40:30 +0000728 bool needsLowering = false;
729 switch (VT.getSimpleVT().SimpleTy) {
730 default:
731 assert(false && "Unhandled load/store type!");
732 case MVT::i1:
733 case MVT::i8:
734 case MVT::i16:
735 case MVT::i32:
736 // Integer loads/stores handle 12-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000737 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000738 break;
739 case MVT::f32:
740 case MVT::f64:
741 // Floating point operands handle 8-bit offsets.
Eric Christopher0d581222010-11-19 22:30:02 +0000742 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
Eric Christopher212ae932010-10-21 19:40:30 +0000743 break;
744 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000745
Eric Christopher212ae932010-10-21 19:40:30 +0000746 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000747 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000748 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000749 ARMCC::CondCodes Pred = ARMCC::AL;
750 unsigned PredReg = 0;
751
Eric Christopher2896df82010-10-15 18:02:07 +0000752 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
753 ARM::GPRRegisterClass;
754 unsigned BaseReg = createResultReg(RC);
755
Eric Christophereaa204b2010-09-02 01:39:14 +0000756 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000757 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000758 BaseReg, Addr.Base.Reg, Addr.Offset,
759 Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000760 static_cast<const ARMBaseInstrInfo&>(TII));
761 else {
762 assert(AFI->isThumb2Function());
763 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0d581222010-11-19 22:30:02 +0000764 BaseReg, Addr.Base.Reg, Addr.Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000765 static_cast<const ARMBaseInstrInfo&>(TII));
766 }
Eric Christopher0d581222010-11-19 22:30:02 +0000767 Addr.Offset = 0;
768 Addr.Base.Reg = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000769 }
Eric Christopher83007122010-08-23 21:44:12 +0000770}
771
Eric Christopher0d581222010-11-19 22:30:02 +0000772bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000773
Eric Christopherb1cc8482010-08-25 07:23:49 +0000774 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000775 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000776 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000777 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000778 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000779 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000780 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000781 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000782 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000783 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000784 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000785 break;
786 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000787 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000788 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000789 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000790 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000791 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000792 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000793 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000794 case MVT::f32:
795 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000796 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000797 isFloat = true;
798 break;
799 case MVT::f64:
800 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000801 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000802 isFloat = true;
803 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000804 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000805
Eric Christopheree56ea62010-10-07 05:50:44 +0000806 ResultReg = createResultReg(RC);
Jim Grosbach6b156392010-10-27 21:39:08 +0000807
Eric Christopher0d581222010-11-19 22:30:02 +0000808 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000809
Eric Christopher212ae932010-10-21 19:40:30 +0000810 // addrmode5 output depends on the selection dag addressing dividing the
811 // offset by 4 that it then later multiplies. Do this here as well.
812 if (isFloat)
Eric Christopher0d581222010-11-19 22:30:02 +0000813 Addr.Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000814
Eric Christopherd0c82a62010-11-12 09:48:30 +0000815 // LDRH needs an additional operand.
816 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16)
817 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
818 TII.get(Opc), ResultReg)
Eric Christopher0d581222010-11-19 22:30:02 +0000819 .addReg(Addr.Base.Reg).addReg(0).addImm(Addr.Offset));
Eric Christopherd0c82a62010-11-12 09:48:30 +0000820 else
821 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
822 TII.get(Opc), ResultReg)
Eric Christopher0d581222010-11-19 22:30:02 +0000823 .addReg(Addr.Base.Reg).addImm(Addr.Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000824 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000825}
826
Eric Christopher43b62be2010-09-27 06:02:23 +0000827bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000828 // Verify we have a legal type before going any further.
Duncan Sands1440e8b2010-11-03 11:35:31 +0000829 MVT VT;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000830 if (!isLoadTypeLegal(I->getType(), VT))
831 return false;
832
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000833 // Our register and offset with innocuous defaults.
Eric Christopher0d581222010-11-19 22:30:02 +0000834 Address Addr;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000835
836 // See if we can handle this as Reg + Offset
Eric Christopher0d581222010-11-19 22:30:02 +0000837 if (!ARMComputeAddress(I->getOperand(0), Addr))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000838 return false;
839
840 unsigned ResultReg;
Eric Christopher0d581222010-11-19 22:30:02 +0000841 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000842
843 UpdateValueMap(I, ResultReg);
844 return true;
845}
846
Eric Christopher0d581222010-11-19 22:30:02 +0000847bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000848 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000849 bool isFloat = false;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000850 bool needReg0Op = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000851 switch (VT.getSimpleVT().SimpleTy) {
852 default: return false;
Eric Christopher4c914122010-11-02 23:59:09 +0000853 case MVT::i1: {
854 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
855 ARM::GPRRegisterClass);
856 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
857 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
858 TII.get(Opc), Res)
859 .addReg(SrcReg).addImm(1));
860 SrcReg = Res;
861 } // Fallthrough here.
Eric Christopher2896df82010-10-15 18:02:07 +0000862 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000863 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000864 break;
865 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000866 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000867 needReg0Op = true;
Eric Christopher15418772010-10-12 05:39:06 +0000868 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000869 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000870 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000871 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000872 case MVT::f32:
873 if (!Subtarget->hasVFP2()) return false;
874 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000875 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000876 break;
877 case MVT::f64:
878 if (!Subtarget->hasVFP2()) return false;
879 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000880 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000881 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000882 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000883
Eric Christopher0d581222010-11-19 22:30:02 +0000884 ARMSimplifyAddress(Addr, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000885
Eric Christopher212ae932010-10-21 19:40:30 +0000886 // addrmode5 output depends on the selection dag addressing dividing the
887 // offset by 4 that it then later multiplies. Do this here as well.
888 if (isFloat)
Eric Christopher0d581222010-11-19 22:30:02 +0000889 Addr.Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000890
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000891 // FIXME: The 'needReg0Op' bit goes away once STRH is converted to
892 // not use the mega-addrmode stuff.
893 if (!needReg0Op)
Eric Christopherb74558a2010-09-18 01:23:38 +0000894 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000895 TII.get(StrOpc))
Eric Christopher0d581222010-11-19 22:30:02 +0000896 .addReg(SrcReg).addReg(Addr.Base.Reg).addImm(Addr.Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000897 else
898 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000899 TII.get(StrOpc))
Eric Christopher0d581222010-11-19 22:30:02 +0000900 .addReg(SrcReg).addReg(Addr.Base.Reg)
901 .addReg(0).addImm(Addr.Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000902
Eric Christopher318b6ee2010-09-02 00:53:56 +0000903 return true;
904}
905
Eric Christopher43b62be2010-09-27 06:02:23 +0000906bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000907 Value *Op0 = I->getOperand(0);
908 unsigned SrcReg = 0;
909
Eric Christopher543cf052010-09-01 22:16:27 +0000910 // Yay type legalization
Duncan Sands1440e8b2010-11-03 11:35:31 +0000911 MVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000912 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000913 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000914
Eric Christopher1b61ef42010-09-02 01:48:11 +0000915 // Get the value to be stored into a register.
916 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000917 if (SrcReg == 0)
918 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000919
Eric Christopher318b6ee2010-09-02 00:53:56 +0000920 // Our register and offset with innocuous defaults.
Eric Christopher0d581222010-11-19 22:30:02 +0000921 Address Addr;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000922
Eric Christopher318b6ee2010-09-02 00:53:56 +0000923 // See if we can handle this as Reg + Offset
Eric Christopher0d581222010-11-19 22:30:02 +0000924 if (!ARMComputeAddress(I->getOperand(1), Addr))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000925 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000926
Eric Christopher0d581222010-11-19 22:30:02 +0000927 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000928
Eric Christophera5b1e682010-09-17 22:28:18 +0000929 return true;
930}
931
932static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
933 switch (Pred) {
934 // Needs two compares...
935 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000936 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000937 default:
Eric Christopher4053e632010-11-02 01:24:49 +0000938 // AL is our "false" for now. The other two need more compares.
Eric Christophera5b1e682010-09-17 22:28:18 +0000939 return ARMCC::AL;
940 case CmpInst::ICMP_EQ:
941 case CmpInst::FCMP_OEQ:
942 return ARMCC::EQ;
943 case CmpInst::ICMP_SGT:
944 case CmpInst::FCMP_OGT:
945 return ARMCC::GT;
946 case CmpInst::ICMP_SGE:
947 case CmpInst::FCMP_OGE:
948 return ARMCC::GE;
949 case CmpInst::ICMP_UGT:
950 case CmpInst::FCMP_UGT:
951 return ARMCC::HI;
952 case CmpInst::FCMP_OLT:
953 return ARMCC::MI;
954 case CmpInst::ICMP_ULE:
955 case CmpInst::FCMP_OLE:
956 return ARMCC::LS;
957 case CmpInst::FCMP_ORD:
958 return ARMCC::VC;
959 case CmpInst::FCMP_UNO:
960 return ARMCC::VS;
961 case CmpInst::FCMP_UGE:
962 return ARMCC::PL;
963 case CmpInst::ICMP_SLT:
964 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000965 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000966 case CmpInst::ICMP_SLE:
967 case CmpInst::FCMP_ULE:
968 return ARMCC::LE;
969 case CmpInst::FCMP_UNE:
970 case CmpInst::ICMP_NE:
971 return ARMCC::NE;
972 case CmpInst::ICMP_UGE:
973 return ARMCC::HS;
974 case CmpInst::ICMP_ULT:
975 return ARMCC::LO;
976 }
Eric Christopher543cf052010-09-01 22:16:27 +0000977}
978
Eric Christopher43b62be2010-09-27 06:02:23 +0000979bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000980 const BranchInst *BI = cast<BranchInst>(I);
981 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
982 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000983
Eric Christophere5734102010-09-03 00:35:47 +0000984 // Simple branch support.
Jim Grosbach16cb3762010-11-09 19:22:26 +0000985
Eric Christopher0e6233b2010-10-29 21:08:19 +0000986 // If we can, avoid recomputing the compare - redoing it could lead to wonky
987 // behavior.
988 // TODO: Factor this out.
989 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
990 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000991 MVT VT;
Eric Christopher0e6233b2010-10-29 21:08:19 +0000992 const Type *Ty = CI->getOperand(0)->getType();
Eric Christopher76d61472010-10-30 21:25:26 +0000993 if (!isTypeLegal(Ty, VT))
994 return false;
995
Eric Christopher0e6233b2010-10-29 21:08:19 +0000996 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
997 if (isFloat && !Subtarget->hasVFP2())
998 return false;
999
1000 unsigned CmpOpc;
1001 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001002 switch (VT.SimpleTy) {
Eric Christopher0e6233b2010-10-29 21:08:19 +00001003 default: return false;
1004 // TODO: Verify compares.
1005 case MVT::f32:
1006 CmpOpc = ARM::VCMPES;
1007 CondReg = ARM::FPSCR;
1008 break;
1009 case MVT::f64:
1010 CmpOpc = ARM::VCMPED;
1011 CondReg = ARM::FPSCR;
1012 break;
1013 case MVT::i32:
1014 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1015 CondReg = ARM::CPSR;
1016 break;
1017 }
1018
1019 // Get the compare predicate.
1020 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1021
1022 // We may not handle every CC for now.
1023 if (ARMPred == ARMCC::AL) return false;
1024
1025 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1026 if (Arg1 == 0) return false;
1027
1028 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1029 if (Arg2 == 0) return false;
1030
1031 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1032 TII.get(CmpOpc))
1033 .addReg(Arg1).addReg(Arg2));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001034
Eric Christopher0e6233b2010-10-29 21:08:19 +00001035 // For floating point we need to move the result to a comparison register
1036 // that we can then use for branches.
1037 if (isFloat)
1038 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1039 TII.get(ARM::FMSTAT)));
Jim Grosbach16cb3762010-11-09 19:22:26 +00001040
Eric Christopher0e6233b2010-10-29 21:08:19 +00001041 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1043 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1044 FastEmitBranch(FBB, DL);
1045 FuncInfo.MBB->addSuccessor(TBB);
1046 return true;
1047 }
1048 }
Jim Grosbach16cb3762010-11-09 19:22:26 +00001049
Eric Christopher0e6233b2010-10-29 21:08:19 +00001050 unsigned CmpReg = getRegForValue(BI->getCondition());
1051 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001052
Eric Christopher229207a2010-09-29 01:14:47 +00001053 // Re-set the flags just in case.
1054 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1055 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001056 .addReg(CmpReg).addImm(0));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001057
Eric Christophere5734102010-09-03 00:35:47 +00001058 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher000cf702010-11-03 04:29:11 +00001060 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001061 FastEmitBranch(FBB, DL);
1062 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001063 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001064}
1065
Eric Christopher43b62be2010-09-27 06:02:23 +00001066bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001067 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001068
Duncan Sands1440e8b2010-11-03 11:35:31 +00001069 MVT VT;
Eric Christopherd43393a2010-09-08 23:13:45 +00001070 const Type *Ty = CI->getOperand(0)->getType();
1071 if (!isTypeLegal(Ty, VT))
1072 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001073
Eric Christopherd43393a2010-09-08 23:13:45 +00001074 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1075 if (isFloat && !Subtarget->hasVFP2())
1076 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001077
Eric Christopherd43393a2010-09-08 23:13:45 +00001078 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001079 unsigned CondReg;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001080 switch (VT.SimpleTy) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001081 default: return false;
1082 // TODO: Verify compares.
1083 case MVT::f32:
1084 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001085 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001086 break;
1087 case MVT::f64:
1088 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001089 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001090 break;
1091 case MVT::i32:
1092 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001093 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001094 break;
1095 }
1096
Eric Christopher229207a2010-09-29 01:14:47 +00001097 // Get the compare predicate.
1098 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001099
Eric Christopher229207a2010-09-29 01:14:47 +00001100 // We may not handle every CC for now.
1101 if (ARMPred == ARMCC::AL) return false;
1102
Eric Christopherd43393a2010-09-08 23:13:45 +00001103 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1104 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001105
Eric Christopherd43393a2010-09-08 23:13:45 +00001106 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1107 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001108
Eric Christopherd43393a2010-09-08 23:13:45 +00001109 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1110 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001111
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001112 // For floating point we need to move the result to a comparison register
1113 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001114 if (isFloat)
1115 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1116 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001117
Eric Christopher229207a2010-09-29 01:14:47 +00001118 // Now set a register based on the comparison. Explicitly set the predicates
1119 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001120 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001121 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001122 : ARM::GPRRegisterClass;
1123 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001124 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001125 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001126 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1127 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1128 .addReg(ZeroReg).addImm(1)
1129 .addImm(ARMPred).addReg(CondReg);
1130
Eric Christophera5b1e682010-09-17 22:28:18 +00001131 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001132 return true;
1133}
1134
Eric Christopher43b62be2010-09-27 06:02:23 +00001135bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001136 // Make sure we have VFP and that we're extending float to double.
1137 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001138
Eric Christopher46203602010-09-09 00:26:48 +00001139 Value *V = I->getOperand(0);
1140 if (!I->getType()->isDoubleTy() ||
1141 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001142
Eric Christopher46203602010-09-09 00:26:48 +00001143 unsigned Op = getRegForValue(V);
1144 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001145
Eric Christopher46203602010-09-09 00:26:48 +00001146 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001147 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001148 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001149 .addReg(Op));
1150 UpdateValueMap(I, Result);
1151 return true;
1152}
1153
Eric Christopher43b62be2010-09-27 06:02:23 +00001154bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001155 // Make sure we have VFP and that we're truncating double to float.
1156 if (!Subtarget->hasVFP2()) return false;
1157
1158 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001159 if (!(I->getType()->isFloatTy() &&
1160 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001161
1162 unsigned Op = getRegForValue(V);
1163 if (Op == 0) return false;
1164
1165 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001166 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001167 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001168 .addReg(Op));
1169 UpdateValueMap(I, Result);
1170 return true;
1171}
1172
Eric Christopher43b62be2010-09-27 06:02:23 +00001173bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001174 // Make sure we have VFP.
1175 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001176
Duncan Sands1440e8b2010-11-03 11:35:31 +00001177 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001178 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001179 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001180 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001181
Eric Christopher9a040492010-09-09 18:54:59 +00001182 unsigned Op = getRegForValue(I->getOperand(0));
1183 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001184
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001185 // The conversion routine works on fp-reg to fp-reg and the operand above
1186 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001187 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001188 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001189
Eric Christopher9a040492010-09-09 18:54:59 +00001190 unsigned Opc;
1191 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1192 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1193 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001194
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001195 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001196 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1197 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001198 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001199 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001200 return true;
1201}
1202
Eric Christopher43b62be2010-09-27 06:02:23 +00001203bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001204 // Make sure we have VFP.
1205 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001206
Duncan Sands1440e8b2010-11-03 11:35:31 +00001207 MVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001208 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001209 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001210 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001211
Eric Christopher9a040492010-09-09 18:54:59 +00001212 unsigned Op = getRegForValue(I->getOperand(0));
1213 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001214
Eric Christopher9a040492010-09-09 18:54:59 +00001215 unsigned Opc;
1216 const Type *OpTy = I->getOperand(0)->getType();
1217 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1218 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1219 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001220
Eric Christopher022b7fb2010-10-05 23:13:24 +00001221 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1222 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1224 ResultReg)
1225 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001226
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001227 // This result needs to be in an integer register, but the conversion only
1228 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001229 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001230 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001231
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001232 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001233 return true;
1234}
1235
Eric Christopher3bbd3962010-10-11 08:27:59 +00001236bool ARMFastISel::SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001237 MVT VT;
1238 if (!isTypeLegal(I->getType(), VT))
Eric Christopher3bbd3962010-10-11 08:27:59 +00001239 return false;
1240
1241 // Things need to be register sized for register moves.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001242 if (VT != MVT::i32) return false;
Eric Christopher3bbd3962010-10-11 08:27:59 +00001243 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1244
1245 unsigned CondReg = getRegForValue(I->getOperand(0));
1246 if (CondReg == 0) return false;
1247 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1248 if (Op1Reg == 0) return false;
1249 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1250 if (Op2Reg == 0) return false;
1251
1252 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1254 .addReg(CondReg).addImm(1));
1255 unsigned ResultReg = createResultReg(RC);
1256 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1257 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1258 .addReg(Op1Reg).addReg(Op2Reg)
1259 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1260 UpdateValueMap(I, ResultReg);
1261 return true;
1262}
1263
Eric Christopher08637852010-09-30 22:34:19 +00001264bool ARMFastISel::SelectSDiv(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001265 MVT VT;
Eric Christopher08637852010-09-30 22:34:19 +00001266 const Type *Ty = I->getType();
1267 if (!isTypeLegal(Ty, VT))
1268 return false;
1269
1270 // If we have integer div support we should have selected this automagically.
1271 // In case we have a real miss go ahead and return false and we'll pick
1272 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001273 if (Subtarget->hasDivide()) return false;
1274
Eric Christopher08637852010-09-30 22:34:19 +00001275 // Otherwise emit a libcall.
1276 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001277 if (VT == MVT::i8)
1278 LC = RTLIB::SDIV_I8;
1279 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001280 LC = RTLIB::SDIV_I16;
1281 else if (VT == MVT::i32)
1282 LC = RTLIB::SDIV_I32;
1283 else if (VT == MVT::i64)
1284 LC = RTLIB::SDIV_I64;
1285 else if (VT == MVT::i128)
1286 LC = RTLIB::SDIV_I128;
1287 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001288
Eric Christopher08637852010-09-30 22:34:19 +00001289 return ARMEmitLibcall(I, LC);
1290}
1291
Eric Christopher6a880d62010-10-11 08:37:26 +00001292bool ARMFastISel::SelectSRem(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001293 MVT VT;
Eric Christopher6a880d62010-10-11 08:37:26 +00001294 const Type *Ty = I->getType();
1295 if (!isTypeLegal(Ty, VT))
1296 return false;
1297
1298 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1299 if (VT == MVT::i8)
1300 LC = RTLIB::SREM_I8;
1301 else if (VT == MVT::i16)
1302 LC = RTLIB::SREM_I16;
1303 else if (VT == MVT::i32)
1304 LC = RTLIB::SREM_I32;
1305 else if (VT == MVT::i64)
1306 LC = RTLIB::SREM_I64;
1307 else if (VT == MVT::i128)
1308 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001309 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001310
Eric Christopher6a880d62010-10-11 08:37:26 +00001311 return ARMEmitLibcall(I, LC);
1312}
1313
Eric Christopher43b62be2010-09-27 06:02:23 +00001314bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001315 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001316
Eric Christopherbc39b822010-09-09 00:53:57 +00001317 // We can get here in the case when we want to use NEON for our fp
1318 // operations, but can't figure out how to. Just use the vfp instructions
1319 // if we have them.
1320 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001321 const Type *Ty = I->getType();
1322 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1323 if (isFloat && !Subtarget->hasVFP2())
1324 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001325
Eric Christopherbc39b822010-09-09 00:53:57 +00001326 unsigned Op1 = getRegForValue(I->getOperand(0));
1327 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001328
Eric Christopherbc39b822010-09-09 00:53:57 +00001329 unsigned Op2 = getRegForValue(I->getOperand(1));
1330 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001331
Eric Christopherbc39b822010-09-09 00:53:57 +00001332 unsigned Opc;
Duncan Sandscdfad362010-11-03 12:17:33 +00001333 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001334 switch (ISDOpcode) {
1335 default: return false;
1336 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001337 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001338 break;
1339 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001340 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001341 break;
1342 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001343 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001344 break;
1345 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001346 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1348 TII.get(Opc), ResultReg)
1349 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001350 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001351 return true;
1352}
1353
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001354// Call Handling Code
1355
Eric Christopherfa87d662010-10-18 02:17:53 +00001356bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1357 EVT SrcVT, unsigned &ResultReg) {
1358 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1359 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001360
Eric Christopherfa87d662010-10-18 02:17:53 +00001361 if (RR != 0) {
1362 ResultReg = RR;
1363 return true;
1364 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001365 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001366}
1367
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001368// This is largely taken directly from CCAssignFnForNode - we don't support
1369// varargs in FastISel so that part has been removed.
1370// TODO: We may not support all of this.
1371CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1372 switch (CC) {
1373 default:
1374 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001375 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001376 // Ignore fastcc. Silence compiler warnings.
1377 (void)RetFastCC_ARM_APCS;
1378 (void)FastCC_ARM_APCS;
1379 // Fallthrough
1380 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001381 // Use target triple & subtarget features to do actual dispatch.
1382 if (Subtarget->isAAPCS_ABI()) {
1383 if (Subtarget->hasVFP2() &&
1384 FloatABIType == FloatABI::Hard)
1385 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1386 else
1387 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1388 } else
1389 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1390 case CallingConv::ARM_AAPCS_VFP:
1391 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1392 case CallingConv::ARM_AAPCS:
1393 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1394 case CallingConv::ARM_APCS:
1395 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1396 }
1397}
1398
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001399bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1400 SmallVectorImpl<unsigned> &ArgRegs,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001401 SmallVectorImpl<MVT> &ArgVTs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001402 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1403 SmallVectorImpl<unsigned> &RegArgs,
1404 CallingConv::ID CC,
1405 unsigned &NumBytes) {
1406 SmallVector<CCValAssign, 16> ArgLocs;
1407 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1408 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1409
1410 // Get a count of how many bytes are to be pushed on the stack.
1411 NumBytes = CCInfo.getNextStackOffset();
1412
1413 // Issue CALLSEQ_START
1414 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1416 TII.get(AdjStackDown))
1417 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001418
1419 // Process the args.
1420 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1421 CCValAssign &VA = ArgLocs[i];
1422 unsigned Arg = ArgRegs[VA.getValNo()];
Duncan Sands1440e8b2010-11-03 11:35:31 +00001423 MVT ArgVT = ArgVTs[VA.getValNo()];
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001424
Eric Christophera4633f52010-10-23 09:37:17 +00001425 // We don't handle NEON parameters yet.
1426 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1427 return false;
1428
Eric Christopherf9764fa2010-09-30 20:49:44 +00001429 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001430 switch (VA.getLocInfo()) {
1431 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001432 case CCValAssign::SExt: {
1433 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1434 Arg, ArgVT, Arg);
1435 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1436 Emitted = true;
1437 ArgVT = VA.getLocVT();
1438 break;
1439 }
1440 case CCValAssign::ZExt: {
1441 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1442 Arg, ArgVT, Arg);
1443 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1444 Emitted = true;
1445 ArgVT = VA.getLocVT();
1446 break;
1447 }
1448 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001449 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1450 Arg, ArgVT, Arg);
1451 if (!Emitted)
1452 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1453 Arg, ArgVT, Arg);
1454 if (!Emitted)
1455 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1456 Arg, ArgVT, Arg);
1457
1458 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1459 ArgVT = VA.getLocVT();
1460 break;
1461 }
1462 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001463 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BIT_CONVERT, Arg,
1464 /*TODO: Kill=*/false);
Eric Christopherfa87d662010-10-18 02:17:53 +00001465 assert(BC != 0 && "Failed to emit a bitcast!");
1466 Arg = BC;
1467 ArgVT = VA.getLocVT();
1468 break;
1469 }
1470 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001471 }
1472
1473 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001474 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001476 VA.getLocReg())
1477 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001478 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001479 } else if (VA.needsCustom()) {
1480 // TODO: We need custom lowering for vector (v2f64) args.
1481 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001482
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001483 CCValAssign &NextVA = ArgLocs[++i];
1484
1485 // TODO: Only handle register args for now.
1486 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1487
1488 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1489 TII.get(ARM::VMOVRRD), VA.getLocReg())
1490 .addReg(NextVA.getLocReg(), RegState::Define)
1491 .addReg(Arg));
1492 RegArgs.push_back(VA.getLocReg());
1493 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001494 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001495 assert(VA.isMemLoc());
1496 // Need to store on the stack.
Eric Christopher0d581222010-11-19 22:30:02 +00001497 Address Addr;
1498 Addr.BaseType = Address::RegBase;
1499 Addr.Base.Reg = ARM::SP;
1500 Addr.Offset = VA.getLocMemOffset();
Eric Christopher5b924802010-10-21 20:09:54 +00001501
Eric Christopher0d581222010-11-19 22:30:02 +00001502 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001503 }
1504 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001505 return true;
1506}
1507
Duncan Sands1440e8b2010-11-03 11:35:31 +00001508bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001509 const Instruction *I, CallingConv::ID CC,
1510 unsigned &NumBytes) {
1511 // Issue CALLSEQ_END
1512 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001513 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1514 TII.get(AdjStackUp))
1515 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001516
1517 // Now the return value.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001518 if (RetVT != MVT::isVoid) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001519 SmallVector<CCValAssign, 16> RVLocs;
1520 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1521 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1522
1523 // Copy all of the result registers out of their specified physreg.
Duncan Sands1440e8b2010-11-03 11:35:31 +00001524 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
Eric Christopher14df8822010-10-01 00:00:11 +00001525 // For this move we copy into two registers and then move into the
1526 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001527 EVT DestVT = RVLocs[0].getValVT();
1528 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1529 unsigned ResultReg = createResultReg(DstRC);
1530 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1531 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001532 .addReg(RVLocs[0].getLocReg())
1533 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001534
Eric Christopher3659ac22010-10-20 08:02:24 +00001535 UsedRegs.push_back(RVLocs[0].getLocReg());
1536 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001537
Eric Christopherdccd2c32010-10-11 08:38:55 +00001538 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001539 UpdateValueMap(I, ResultReg);
1540 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001541 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001542 EVT CopyVT = RVLocs[0].getValVT();
1543 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001544
Eric Christopher14df8822010-10-01 00:00:11 +00001545 unsigned ResultReg = createResultReg(DstRC);
1546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1547 ResultReg).addReg(RVLocs[0].getLocReg());
1548 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001549
Eric Christopherdccd2c32010-10-11 08:38:55 +00001550 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001551 UpdateValueMap(I, ResultReg);
1552 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001553 }
1554
Eric Christopherdccd2c32010-10-11 08:38:55 +00001555 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001556}
1557
Eric Christopher4f512ef2010-10-22 01:28:00 +00001558bool ARMFastISel::SelectRet(const Instruction *I) {
1559 const ReturnInst *Ret = cast<ReturnInst>(I);
1560 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001561
Eric Christopher4f512ef2010-10-22 01:28:00 +00001562 if (!FuncInfo.CanLowerReturn)
1563 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001564
Eric Christopher4f512ef2010-10-22 01:28:00 +00001565 if (F.isVarArg())
1566 return false;
1567
1568 CallingConv::ID CC = F.getCallingConv();
1569 if (Ret->getNumOperands() > 0) {
1570 SmallVector<ISD::OutputArg, 4> Outs;
1571 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1572 Outs, TLI);
1573
1574 // Analyze operands of the call, assigning locations to each operand.
1575 SmallVector<CCValAssign, 16> ValLocs;
1576 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1577 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1578
1579 const Value *RV = Ret->getOperand(0);
1580 unsigned Reg = getRegForValue(RV);
1581 if (Reg == 0)
1582 return false;
1583
1584 // Only handle a single return value for now.
1585 if (ValLocs.size() != 1)
1586 return false;
1587
1588 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001589
Eric Christopher4f512ef2010-10-22 01:28:00 +00001590 // Don't bother handling odd stuff for now.
1591 if (VA.getLocInfo() != CCValAssign::Full)
1592 return false;
1593 // Only handle register returns for now.
1594 if (!VA.isRegLoc())
1595 return false;
1596 // TODO: For now, don't try to handle cases where getLocInfo()
1597 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +00001598 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Eric Christopher4f512ef2010-10-22 01:28:00 +00001599 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001600
Eric Christopher4f512ef2010-10-22 01:28:00 +00001601 // Make the copy.
1602 unsigned SrcReg = Reg + VA.getValNo();
1603 unsigned DstReg = VA.getLocReg();
1604 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1605 // Avoid a cross-class copy. This is very unlikely.
1606 if (!SrcRC->contains(DstReg))
1607 return false;
1608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1609 DstReg).addReg(SrcReg);
1610
1611 // Mark the register as live out of the function.
1612 MRI.addLiveOut(VA.getLocReg());
1613 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001614
Eric Christopher4f512ef2010-10-22 01:28:00 +00001615 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1616 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1617 TII.get(RetOpc)));
1618 return true;
1619}
1620
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001621// A quick function that will emit a call for a named libcall in F with the
1622// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001623// can emit a call for any libcall we can produce. This is an abridged version
1624// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001625// like computed function pointers or strange arguments at call sites.
1626// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1627// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001628bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1629 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001630
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001631 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001632 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001633 MVT RetVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001634 if (RetTy->isVoidTy())
1635 RetVT = MVT::isVoid;
1636 else if (!isTypeLegal(RetTy, RetVT))
1637 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001638
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001639 // For now we're using BLX etc on the assumption that we have v5t ops.
1640 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001641
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001642 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001643 SmallVector<Value*, 8> Args;
1644 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001645 SmallVector<MVT, 8> ArgVTs;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001646 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1647 Args.reserve(I->getNumOperands());
1648 ArgRegs.reserve(I->getNumOperands());
1649 ArgVTs.reserve(I->getNumOperands());
1650 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001651 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001652 Value *Op = I->getOperand(i);
1653 unsigned Arg = getRegForValue(Op);
1654 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001655
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001656 const Type *ArgTy = Op->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001657 MVT ArgVT;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001658 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001659
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001660 ISD::ArgFlagsTy Flags;
1661 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1662 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001663
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001664 Args.push_back(Op);
1665 ArgRegs.push_back(Arg);
1666 ArgVTs.push_back(ArgVT);
1667 ArgFlags.push_back(Flags);
1668 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001669
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001670 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001671 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001672 unsigned NumBytes;
1673 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1674 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001675
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001676 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001677 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001678 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001679 unsigned CallOpc;
1680 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001681 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001682 else
1683 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001684 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001685 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001686
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001687 // Add implicit physical register uses to the call.
1688 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1689 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001690
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001691 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001692 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001693 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001694
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001695 // Set all unused physreg defs as dead.
1696 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001698 return true;
1699}
1700
Eric Christopherf9764fa2010-09-30 20:49:44 +00001701bool ARMFastISel::SelectCall(const Instruction *I) {
1702 const CallInst *CI = cast<CallInst>(I);
1703 const Value *Callee = CI->getCalledValue();
1704
1705 // Can't handle inline asm or worry about intrinsics yet.
1706 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1707
Eric Christophere6ca6772010-10-01 21:33:12 +00001708 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001709 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001710 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1711 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001712
Eric Christopherf9764fa2010-09-30 20:49:44 +00001713 // Check the calling convention.
1714 ImmutableCallSite CS(CI);
1715 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001716
Eric Christopherf9764fa2010-09-30 20:49:44 +00001717 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001718
Eric Christopherf9764fa2010-09-30 20:49:44 +00001719 // Let SDISel handle vararg functions.
1720 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1721 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1722 if (FTy->isVarArg())
1723 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001724
Eric Christopherf9764fa2010-09-30 20:49:44 +00001725 // Handle *simple* calls for now.
1726 const Type *RetTy = I->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001727 MVT RetVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001728 if (RetTy->isVoidTy())
1729 RetVT = MVT::isVoid;
1730 else if (!isTypeLegal(RetTy, RetVT))
1731 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001732
Eric Christopherf9764fa2010-09-30 20:49:44 +00001733 // For now we're using BLX etc on the assumption that we have v5t ops.
1734 // TODO: Maybe?
1735 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001736
Eric Christopherf9764fa2010-09-30 20:49:44 +00001737 // Set up the argument vectors.
1738 SmallVector<Value*, 8> Args;
1739 SmallVector<unsigned, 8> ArgRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001740 SmallVector<MVT, 8> ArgVTs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001741 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1742 Args.reserve(CS.arg_size());
1743 ArgRegs.reserve(CS.arg_size());
1744 ArgVTs.reserve(CS.arg_size());
1745 ArgFlags.reserve(CS.arg_size());
1746 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1747 i != e; ++i) {
1748 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001749
Eric Christopherf9764fa2010-09-30 20:49:44 +00001750 if (Arg == 0)
1751 return false;
1752 ISD::ArgFlagsTy Flags;
1753 unsigned AttrInd = i - CS.arg_begin() + 1;
1754 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1755 Flags.setSExt();
1756 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1757 Flags.setZExt();
1758
1759 // FIXME: Only handle *easy* calls for now.
1760 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1761 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1762 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1763 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1764 return false;
1765
1766 const Type *ArgTy = (*i)->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001767 MVT ArgVT;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001768 if (!isTypeLegal(ArgTy, ArgVT))
1769 return false;
1770 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1771 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001772
Eric Christopherf9764fa2010-09-30 20:49:44 +00001773 Args.push_back(*i);
1774 ArgRegs.push_back(Arg);
1775 ArgVTs.push_back(ArgVT);
1776 ArgFlags.push_back(Flags);
1777 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001778
Eric Christopherf9764fa2010-09-30 20:49:44 +00001779 // Handle the arguments now that we've gotten them.
1780 SmallVector<unsigned, 4> RegArgs;
1781 unsigned NumBytes;
1782 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1783 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001784
Eric Christopherf9764fa2010-09-30 20:49:44 +00001785 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001786 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001787 MachineInstrBuilder MIB;
1788 unsigned CallOpc;
1789 if(isThumb)
1790 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1791 else
1792 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1793 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1794 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001795
Eric Christopherf9764fa2010-09-30 20:49:44 +00001796 // Add implicit physical register uses to the call.
1797 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1798 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001799
Eric Christopherf9764fa2010-09-30 20:49:44 +00001800 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001801 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001802 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001803
Eric Christopherf9764fa2010-09-30 20:49:44 +00001804 // Set all unused physreg defs as dead.
1805 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001806
Eric Christopherf9764fa2010-09-30 20:49:44 +00001807 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001808
Eric Christopherf9764fa2010-09-30 20:49:44 +00001809}
1810
Eric Christopher56d2b722010-09-02 23:43:26 +00001811// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001812bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopherac1a19e2010-09-09 01:06:51 +00001813
Eric Christopherab695882010-07-21 22:26:11 +00001814 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001815 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001816 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001817 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001818 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001819 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001820 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001821 case Instruction::ICmp:
1822 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001823 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001824 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001825 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001826 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001827 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001828 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001829 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001830 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001831 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001832 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001833 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001834 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001835 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001836 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001837 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001838 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001839 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001840 case Instruction::SRem:
1841 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001842 case Instruction::Call:
1843 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001844 case Instruction::Select:
1845 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001846 case Instruction::Ret:
1847 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001848 default: break;
1849 }
1850 return false;
1851}
1852
1853namespace llvm {
1854 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001855 // Completely untested on non-darwin.
1856 const TargetMachine &TM = funcInfo.MF->getTarget();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001857
Eric Christopheraaa8df42010-11-02 01:21:28 +00001858 // Darwin and thumb1 only for now.
Eric Christopherfeadddd2010-10-11 20:05:22 +00001859 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Grosbach16cb3762010-11-09 19:22:26 +00001860 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
Eric Christopheraaa8df42010-11-02 01:21:28 +00001861 !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001862 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001863 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001864 }
1865}