blob: 8a5b73eef803bcc6020459737e44218c932c0ca6 [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 {
56
57class ARMFastISel : public FastISel {
58
59 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000062 const TargetMachine &TM;
63 const TargetInstrInfo &TII;
64 const TargetLowering &TLI;
Eric Christopherc9932f62010-10-01 23:24:42 +000065 ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000066
Eric Christopher8cf6c602010-09-29 22:24:45 +000067 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000068 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000069 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000070
Eric Christopherab695882010-07-21 22:26:11 +000071 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000072 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000073 : FastISel(funcInfo),
74 TM(funcInfo.MF->getTarget()),
75 TII(*TM.getInstrInfo()),
76 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000077 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000078 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000079 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000080 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000081 }
82
Eric Christophercb592292010-08-20 00:20:31 +000083 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000084 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC);
86 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill);
89 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill,
92 unsigned Op1, bool Op1IsKill);
93 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 const ConstantFP *FPImm);
101 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 uint64_t Imm);
104 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill,
107 unsigned Op1, bool Op1IsKill,
108 uint64_t Imm);
109 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
110 unsigned Op0, bool Op0IsKill,
111 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000112
Eric Christophercb592292010-08-20 00:20:31 +0000113 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000114 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000115 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherf9764fa2010-09-30 20:49:44 +0000116 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
Eric Christopherab695882010-07-21 22:26:11 +0000117
118 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000119
Eric Christopher83007122010-08-23 21:44:12 +0000120 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000121 private:
Eric Christopher17787722010-10-21 21:47:51 +0000122 bool SelectLoad(const Instruction *I);
123 bool SelectStore(const Instruction *I);
124 bool SelectBranch(const Instruction *I);
125 bool SelectCmp(const Instruction *I);
126 bool SelectFPExt(const Instruction *I);
127 bool SelectFPTrunc(const Instruction *I);
128 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
129 bool SelectSIToFP(const Instruction *I);
130 bool SelectFPToSI(const Instruction *I);
131 bool SelectSDiv(const Instruction *I);
132 bool SelectSRem(const Instruction *I);
133 bool SelectCall(const Instruction *I);
134 bool SelectSelect(const Instruction *I);
Eric Christopher4f512ef2010-10-22 01:28:00 +0000135 bool SelectRet(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000136
Eric Christopher83007122010-08-23 21:44:12 +0000137 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000138 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000139 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000140 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher404be0c2010-10-17 11:08:44 +0000141 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Base, int Offset);
142 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Base, int Offset);
143 bool ARMComputeRegOffset(const Value *Obj, unsigned &Base, int &Offset);
144 void ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000145 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000146 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000147 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000148 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000149 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000150
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000151 // Call handling routines.
152 private:
Eric Christopherfa87d662010-10-18 02:17:53 +0000153 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
154 unsigned &ResultReg);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000155 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000156 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
Eric Christophera9a7a1a2010-09-29 23:11:09 +0000157 SmallVectorImpl<unsigned> &ArgRegs,
158 SmallVectorImpl<EVT> &ArgVTs,
159 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
160 SmallVectorImpl<unsigned> &RegArgs,
161 CallingConv::ID CC,
162 unsigned &NumBytes);
163 bool FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
164 const Instruction *I, CallingConv::ID CC,
165 unsigned &NumBytes);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000166 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000167
168 // OptionalDef handling routines.
169 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000170 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
171 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
172};
Eric Christopherab695882010-07-21 22:26:11 +0000173
174} // end anonymous namespace
175
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000176#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000177
Eric Christopher456144e2010-08-19 00:37:05 +0000178// DefinesOptionalPredicate - This is different from DefinesPredicate in that
179// we don't care about implicit defs here, just places we'll need to add a
180// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
181bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
182 const TargetInstrDesc &TID = MI->getDesc();
183 if (!TID.hasOptionalDef())
184 return false;
185
186 // Look to see if our OptionalDef is defining CPSR or CCR.
187 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
188 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000189 if (!MO.isReg() || !MO.isDef()) continue;
190 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000191 *CPSR = true;
192 }
193 return true;
194}
195
196// If the machine is predicable go ahead and add the predicate operands, if
197// it needs default CC operands add those.
198const MachineInstrBuilder &
199ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
200 MachineInstr *MI = &*MIB;
201
202 // Do we use a predicate?
203 if (TII.isPredicable(MI))
204 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000205
Eric Christopher456144e2010-08-19 00:37:05 +0000206 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
207 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000208 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000209 if (DefinesOptionalPredicate(MI, &CPSR)) {
210 if (CPSR)
211 AddDefaultT1CC(MIB);
212 else
213 AddDefaultCC(MIB);
214 }
215 return MIB;
216}
217
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
219 const TargetRegisterClass* RC) {
220 unsigned ResultReg = createResultReg(RC);
221 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222
Eric Christopher456144e2010-08-19 00:37:05 +0000223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000224 return ResultReg;
225}
226
227unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
228 const TargetRegisterClass *RC,
229 unsigned Op0, bool Op0IsKill) {
230 unsigned ResultReg = createResultReg(RC);
231 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
232
233 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000235 .addReg(Op0, Op0IsKill * RegState::Kill));
236 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000239 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000240 TII.get(TargetOpcode::COPY), ResultReg)
241 .addReg(II.ImplicitDefs[0]));
242 }
243 return ResultReg;
244}
245
246unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
247 const TargetRegisterClass *RC,
248 unsigned Op0, bool Op0IsKill,
249 unsigned Op1, bool Op1IsKill) {
250 unsigned ResultReg = createResultReg(RC);
251 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
252
253 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000255 .addReg(Op0, Op0IsKill * RegState::Kill)
256 .addReg(Op1, Op1IsKill * RegState::Kill));
257 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000259 .addReg(Op0, Op0IsKill * RegState::Kill)
260 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000262 TII.get(TargetOpcode::COPY), ResultReg)
263 .addReg(II.ImplicitDefs[0]));
264 }
265 return ResultReg;
266}
267
268unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
269 const TargetRegisterClass *RC,
270 unsigned Op0, bool Op0IsKill,
271 uint64_t Imm) {
272 unsigned ResultReg = createResultReg(RC);
273 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
274
275 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000276 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000277 .addReg(Op0, Op0IsKill * RegState::Kill)
278 .addImm(Imm));
279 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281 .addReg(Op0, Op0IsKill * RegState::Kill)
282 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 TII.get(TargetOpcode::COPY), ResultReg)
285 .addReg(II.ImplicitDefs[0]));
286 }
287 return ResultReg;
288}
289
290unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
291 const TargetRegisterClass *RC,
292 unsigned Op0, bool Op0IsKill,
293 const ConstantFP *FPImm) {
294 unsigned ResultReg = createResultReg(RC);
295 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
296
297 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000299 .addReg(Op0, Op0IsKill * RegState::Kill)
300 .addFPImm(FPImm));
301 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000302 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000303 .addReg(Op0, Op0IsKill * RegState::Kill)
304 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 TII.get(TargetOpcode::COPY), ResultReg)
307 .addReg(II.ImplicitDefs[0]));
308 }
309 return ResultReg;
310}
311
312unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
313 const TargetRegisterClass *RC,
314 unsigned Op0, bool Op0IsKill,
315 unsigned Op1, bool Op1IsKill,
316 uint64_t Imm) {
317 unsigned ResultReg = createResultReg(RC);
318 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
319
320 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000321 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000322 .addReg(Op0, Op0IsKill * RegState::Kill)
323 .addReg(Op1, Op1IsKill * RegState::Kill)
324 .addImm(Imm));
325 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill)
329 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(II.ImplicitDefs[0]));
333 }
334 return ResultReg;
335}
336
337unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
338 const TargetRegisterClass *RC,
339 uint64_t Imm) {
340 unsigned ResultReg = createResultReg(RC);
341 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000342
Eric Christopher0fe7d542010-08-17 01:25:29 +0000343 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000345 .addImm(Imm));
346 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000348 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000350 TII.get(TargetOpcode::COPY), ResultReg)
351 .addReg(II.ImplicitDefs[0]));
352 }
353 return ResultReg;
354}
355
356unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
357 unsigned Op0, bool Op0IsKill,
358 uint32_t Idx) {
359 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
360 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
361 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000363 DL, TII.get(TargetOpcode::COPY), ResultReg)
364 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
365 return ResultReg;
366}
367
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000368// TODO: Don't worry about 64-bit now, but when this is fixed remove the
369// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000370unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000371 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000372
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000373 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
375 TII.get(ARM::VMOVRS), MoveReg)
376 .addReg(SrcReg));
377 return MoveReg;
378}
379
380unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000381 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000382
Eric Christopheraa3ace12010-09-09 20:49:25 +0000383 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000385 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000386 .addReg(SrcReg));
387 return MoveReg;
388}
389
Eric Christopher9ed58df2010-09-09 00:19:41 +0000390// For double width floating point we need to materialize two constants
391// (the high and the low) into integer registers then use a move to get
392// the combined constant into an FP reg.
393unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
394 const APFloat Val = CFP->getValueAPF();
395 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000396
Eric Christopher9ed58df2010-09-09 00:19:41 +0000397 // This checks to see if we can use VFP3 instructions to materialize
398 // a constant, otherwise we have to go through the constant pool.
399 if (TLI.isFPImmLegal(Val, VT)) {
400 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
401 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
403 DestReg)
404 .addFPImm(CFP));
405 return DestReg;
406 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000407
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000408 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000409 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000410
Eric Christopher238bb162010-09-09 23:50:00 +0000411 // MachineConstantPool wants an explicit alignment.
412 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
413 if (Align == 0) {
414 // TODO: Figure out if this is correct.
415 Align = TD.getTypeAllocSize(CFP->getType());
416 }
417 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
418 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
419 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000420
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000421 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
423 DestReg)
424 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000425 .addReg(0));
426 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000427}
428
Eric Christopher744c7c82010-09-28 22:47:54 +0000429unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
Eric Christopherdccd2c32010-10-11 08:38:55 +0000430
Eric Christopher744c7c82010-09-28 22:47:54 +0000431 // For now 32-bit only.
432 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000433
Eric Christopher56d2b722010-09-02 23:43:26 +0000434 // MachineConstantPool wants an explicit alignment.
435 unsigned Align = TD.getPrefTypeAlignment(C->getType());
436 if (Align == 0) {
437 // TODO: Figure out if this is correct.
438 Align = TD.getTypeAllocSize(C->getType());
439 }
440 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000441 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdccd2c32010-10-11 08:38:55 +0000442
Eric Christopher56d2b722010-09-02 23:43:26 +0000443 if (isThumb)
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000445 TII.get(ARM::t2LDRpci), DestReg)
446 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000447 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000448 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000449 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000450 TII.get(ARM::LDRcp), DestReg)
451 .addConstantPoolIndex(Idx)
Jim Grosbach3e556122010-10-26 22:37:02 +0000452 .addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000453
Eric Christopher56d2b722010-09-02 23:43:26 +0000454 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000455}
456
Eric Christopherc9932f62010-10-01 23:24:42 +0000457unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
Eric Christopher890dbbe2010-10-02 00:32:44 +0000458 // For now 32-bit only.
459 if (VT.getSimpleVT().SimpleTy != MVT::i32) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000460
Eric Christopher890dbbe2010-10-02 00:32:44 +0000461 Reloc::Model RelocM = TM.getRelocationModel();
Eric Christopherdccd2c32010-10-11 08:38:55 +0000462
Eric Christopher890dbbe2010-10-02 00:32:44 +0000463 // TODO: No external globals for now.
464 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000465
Eric Christopher890dbbe2010-10-02 00:32:44 +0000466 // TODO: Need more magic for ARM PIC.
467 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000468
Eric Christopher890dbbe2010-10-02 00:32:44 +0000469 // MachineConstantPool wants an explicit alignment.
470 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
471 if (Align == 0) {
472 // TODO: Figure out if this is correct.
473 Align = TD.getTypeAllocSize(GV->getType());
474 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000475
Eric Christopher890dbbe2010-10-02 00:32:44 +0000476 // Grab index.
477 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
478 unsigned Id = AFI->createConstPoolEntryUId();
479 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
480 ARMCP::CPValue, PCAdj);
481 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000482
Eric Christopher890dbbe2010-10-02 00:32:44 +0000483 // Load value.
484 MachineInstrBuilder MIB;
485 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
486 if (isThumb) {
487 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
488 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
489 .addConstantPoolIndex(Idx);
490 if (RelocM == Reloc::PIC_)
491 MIB.addImm(Id);
492 } else {
493 // The extra reg and immediate are for addrmode2.
494 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
495 DestReg)
496 .addConstantPoolIndex(Idx)
497 .addReg(0).addImm(0);
498 }
499 AddOptionalDefs(MIB);
500 return DestReg;
Eric Christopherc9932f62010-10-01 23:24:42 +0000501}
502
Eric Christopher9ed58df2010-09-09 00:19:41 +0000503unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
504 EVT VT = TLI.getValueType(C->getType(), true);
505
506 // Only handle simple types.
507 if (!VT.isSimple()) return 0;
508
509 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
510 return ARMMaterializeFP(CFP, VT);
Eric Christopherc9932f62010-10-01 23:24:42 +0000511 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
512 return ARMMaterializeGV(GV, VT);
513 else if (isa<ConstantInt>(C))
514 return ARMMaterializeInt(C, VT);
Eric Christopherdccd2c32010-10-11 08:38:55 +0000515
Eric Christopherc9932f62010-10-01 23:24:42 +0000516 return 0;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000517}
518
Eric Christopherf9764fa2010-09-30 20:49:44 +0000519unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
520 // Don't handle dynamic allocas.
521 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000522
Eric Christopherf9764fa2010-09-30 20:49:44 +0000523 EVT VT;
Eric Christopherec8bf972010-10-17 06:07:26 +0000524 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +0000525
Eric Christopherf9764fa2010-09-30 20:49:44 +0000526 DenseMap<const AllocaInst*, int>::iterator SI =
527 FuncInfo.StaticAllocaMap.find(AI);
528
529 // This will get lowered later into the correct offsets and registers
530 // via rewriteXFrameIndex.
531 if (SI != FuncInfo.StaticAllocaMap.end()) {
532 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
533 unsigned ResultReg = createResultReg(RC);
534 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
535 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
536 TII.get(Opc), ResultReg)
537 .addFrameIndex(SI->second)
538 .addImm(0));
539 return ResultReg;
540 }
Eric Christopherdccd2c32010-10-11 08:38:55 +0000541
Eric Christopherf9764fa2010-09-30 20:49:44 +0000542 return 0;
543}
544
Eric Christopherb1cc8482010-08-25 07:23:49 +0000545bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
546 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000547
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548 // Only handle simple types.
549 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000550
Eric Christopherdc908042010-08-31 01:28:42 +0000551 // Handle all legal types, i.e. a register that will directly hold this
552 // value.
553 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000554}
555
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000556bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
557 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000558
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000559 // If this is a type than can be sign or zero-extended to a basic operation
560 // go ahead and accept it now.
561 if (VT == MVT::i8 || VT == MVT::i16)
562 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000563
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000564 return false;
565}
566
Eric Christophercb0b04b2010-08-24 00:07:24 +0000567// Computes the Reg+Offset to get to an object.
Eric Christopher404be0c2010-10-17 11:08:44 +0000568bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Base,
Eric Christopher83007122010-08-23 21:44:12 +0000569 int &Offset) {
570 // Some boilerplate from the X86 FastISel.
571 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000572 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000573 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000574 // Don't walk into other basic blocks; it's possible we haven't
575 // visited them yet, so the instructions may not yet be assigned
576 // virtual registers.
577 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
578 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000579 Opcode = I->getOpcode();
580 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000581 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000582 Opcode = C->getOpcode();
583 U = C;
584 }
585
Eric Christophercb0b04b2010-08-24 00:07:24 +0000586 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000587 if (Ty->getAddressSpace() > 255)
588 // Fast instruction selection doesn't support the special
589 // address spaces.
590 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000591
Eric Christopher83007122010-08-23 21:44:12 +0000592 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000593 default:
Eric Christopher83007122010-08-23 21:44:12 +0000594 break;
Eric Christopher55324332010-10-12 00:43:21 +0000595 case Instruction::BitCast: {
596 // Look through bitcasts.
Eric Christophera3224252010-10-15 21:32:12 +0000597 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000598 }
599 case Instruction::IntToPtr: {
600 // Look past no-op inttoptrs.
601 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000602 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000603 break;
604 }
605 case Instruction::PtrToInt: {
606 // Look past no-op ptrtoints.
607 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Eric Christophera3224252010-10-15 21:32:12 +0000608 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
Eric Christopher55324332010-10-12 00:43:21 +0000609 break;
610 }
Eric Christophereae84392010-10-14 09:29:41 +0000611 case Instruction::GetElementPtr: {
612 int SavedOffset = Offset;
Eric Christopher404be0c2010-10-17 11:08:44 +0000613 unsigned SavedBase = Base;
Eric Christophereae84392010-10-14 09:29:41 +0000614 int TmpOffset = Offset;
Eric Christopher2896df82010-10-15 18:02:07 +0000615
Eric Christophereae84392010-10-14 09:29:41 +0000616 // Iterate through the GEP folding the constants into offsets where
617 // we can.
618 gep_type_iterator GTI = gep_type_begin(U);
619 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
620 i != e; ++i, ++GTI) {
621 const Value *Op = *i;
622 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
623 const StructLayout *SL = TD.getStructLayout(STy);
624 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
625 TmpOffset += SL->getElementOffset(Idx);
626 } else {
Eric Christopher2896df82010-10-15 18:02:07 +0000627 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
628 SmallVector<const Value *, 4> Worklist;
629 Worklist.push_back(Op);
630 do {
631 Op = Worklist.pop_back_val();
632 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
633 // Constant-offset addressing.
634 TmpOffset += CI->getSExtValue() * S;
Eric Christopherdc0b0ef2010-10-17 01:41:46 +0000635 } else if (isa<AddOperator>(Op) &&
Eric Christopher2896df82010-10-15 18:02:07 +0000636 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
637 // An add with a constant operand. Fold the constant.
638 ConstantInt *CI =
639 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
640 TmpOffset += CI->getSExtValue() * S;
641 // Add the other operand back to the work list.
642 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
643 } else
644 goto unsupported_gep;
645 } while (!Worklist.empty());
Eric Christophereae84392010-10-14 09:29:41 +0000646 }
647 }
Eric Christopher2896df82010-10-15 18:02:07 +0000648
649 // Try to grab the base operand now.
Eric Christophereae84392010-10-14 09:29:41 +0000650 Offset = TmpOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000651 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
Eric Christopher2896df82010-10-15 18:02:07 +0000652
653 // We failed, restore everything and try the other options.
Eric Christophereae84392010-10-14 09:29:41 +0000654 Offset = SavedOffset;
Eric Christophera3224252010-10-15 21:32:12 +0000655 Base = SavedBase;
Eric Christopher2896df82010-10-15 18:02:07 +0000656
Eric Christophereae84392010-10-14 09:29:41 +0000657 unsupported_gep:
Eric Christophereae84392010-10-14 09:29:41 +0000658 break;
659 }
Eric Christopher83007122010-08-23 21:44:12 +0000660 case Instruction::Alloca: {
Eric Christopher15418772010-10-12 05:39:06 +0000661 const AllocaInst *AI = cast<AllocaInst>(Obj);
Eric Christopherd56d61a2010-10-17 01:51:42 +0000662 unsigned Reg = TargetMaterializeAlloca(AI);
663
664 if (Reg == 0) return false;
665
Eric Christopher404be0c2010-10-17 11:08:44 +0000666 Base = Reg;
Eric Christopherd56d61a2010-10-17 01:51:42 +0000667 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000668 }
669 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000670
Eric Christophera9c57512010-10-13 21:41:51 +0000671 // Materialize the global variable's address into a reg which can
672 // then be used later to load the variable.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000673 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherede42b02010-10-13 09:11:46 +0000674 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
675 if (Tmp == 0) return false;
Eric Christopher2896df82010-10-15 18:02:07 +0000676
Eric Christopher404be0c2010-10-17 11:08:44 +0000677 Base = Tmp;
Eric Christopherede42b02010-10-13 09:11:46 +0000678 return true;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000679 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000680
Eric Christophercb0b04b2010-08-24 00:07:24 +0000681 // Try to get this in a register if nothing else has worked.
Eric Christopher404be0c2010-10-17 11:08:44 +0000682 if (Base == 0) Base = getRegForValue(Obj);
683 return Base != 0;
Eric Christophereae84392010-10-14 09:29:41 +0000684}
685
Eric Christopher404be0c2010-10-17 11:08:44 +0000686void ARMFastISel::ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT) {
Jim Grosbach6b156392010-10-27 21:39:08 +0000687
Eric Christopher212ae932010-10-21 19:40:30 +0000688 assert(VT.isSimple() && "Non-simple types are invalid here!");
Jim Grosbach6b156392010-10-27 21:39:08 +0000689
Eric Christopher212ae932010-10-21 19:40:30 +0000690 bool needsLowering = false;
691 switch (VT.getSimpleVT().SimpleTy) {
692 default:
693 assert(false && "Unhandled load/store type!");
694 case MVT::i1:
695 case MVT::i8:
696 case MVT::i16:
697 case MVT::i32:
698 // Integer loads/stores handle 12-bit offsets.
699 needsLowering = ((Offset & 0xfff) != Offset);
700 break;
701 case MVT::f32:
702 case MVT::f64:
703 // Floating point operands handle 8-bit offsets.
704 needsLowering = ((Offset & 0xff) != Offset);
705 break;
706 }
Jim Grosbach6b156392010-10-27 21:39:08 +0000707
Eric Christopher212ae932010-10-21 19:40:30 +0000708 // Since the offset is too large for the load/store instruction
Eric Christopher318b6ee2010-09-02 00:53:56 +0000709 // get the reg+offset into a register.
Eric Christopher212ae932010-10-21 19:40:30 +0000710 if (needsLowering) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000711 ARMCC::CondCodes Pred = ARMCC::AL;
712 unsigned PredReg = 0;
713
Eric Christopher2896df82010-10-15 18:02:07 +0000714 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
715 ARM::GPRRegisterClass;
716 unsigned BaseReg = createResultReg(RC);
717
Eric Christophereaa204b2010-09-02 01:39:14 +0000718 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000719 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000720 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000721 static_cast<const ARMBaseInstrInfo&>(TII));
722 else {
723 assert(AFI->isThumb2Function());
724 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher404be0c2010-10-17 11:08:44 +0000725 BaseReg, Base, Offset, Pred, PredReg,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000726 static_cast<const ARMBaseInstrInfo&>(TII));
727 }
Eric Christophereae84392010-10-14 09:29:41 +0000728 Offset = 0;
Eric Christopher404be0c2010-10-17 11:08:44 +0000729 Base = BaseReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000730 }
Eric Christopher83007122010-08-23 21:44:12 +0000731}
732
Eric Christopherb1cc8482010-08-25 07:23:49 +0000733bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000734 unsigned Base, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000735
Eric Christopherb1cc8482010-08-25 07:23:49 +0000736 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000737 unsigned Opc;
Eric Christopheree56ea62010-10-07 05:50:44 +0000738 TargetRegisterClass *RC;
Eric Christopher6dab1372010-09-18 01:59:37 +0000739 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000740 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000741 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000742 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000743 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000744 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000745 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
Eric Christopher7a56f332010-10-08 01:13:17 +0000746 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000747 break;
748 case MVT::i8:
Jim Grosbachc1d30212010-10-27 00:19:44 +0000749 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000750 RC = ARM::GPRRegisterClass;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000751 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000752 case MVT::i32:
Jim Grosbach3e556122010-10-26 22:37:02 +0000753 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
Eric Christopher7a56f332010-10-08 01:13:17 +0000754 RC = ARM::GPRRegisterClass;
Eric Christopherdc908042010-08-31 01:28:42 +0000755 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000756 case MVT::f32:
757 Opc = ARM::VLDRS;
Eric Christopheree56ea62010-10-07 05:50:44 +0000758 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000759 isFloat = true;
760 break;
761 case MVT::f64:
762 Opc = ARM::VLDRD;
Eric Christopheree56ea62010-10-07 05:50:44 +0000763 RC = TLI.getRegClassFor(VT);
Eric Christopher6dab1372010-09-18 01:59:37 +0000764 isFloat = true;
765 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000766 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000767
Eric Christopheree56ea62010-10-07 05:50:44 +0000768 ResultReg = createResultReg(RC);
Jim Grosbach6b156392010-10-27 21:39:08 +0000769
Eric Christopher212ae932010-10-21 19:40:30 +0000770 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000771
Eric Christopher212ae932010-10-21 19:40:30 +0000772 // addrmode5 output depends on the selection dag addressing dividing the
773 // offset by 4 that it then later multiplies. Do this here as well.
774 if (isFloat)
775 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000776
Jim Grosbach3e556122010-10-26 22:37:02 +0000777 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
778 TII.get(Opc), ResultReg)
779 .addReg(Base).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000780 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000781}
782
Eric Christopher43b62be2010-09-27 06:02:23 +0000783bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000784 // Verify we have a legal type before going any further.
785 EVT VT;
786 if (!isLoadTypeLegal(I->getType(), VT))
787 return false;
788
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000789 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000790 unsigned Base = 0;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000791 int Offset = 0;
792
793 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000794 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000795 return false;
796
797 unsigned ResultReg;
Eric Christophera3224252010-10-15 21:32:12 +0000798 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000799
800 UpdateValueMap(I, ResultReg);
801 return true;
802}
803
Eric Christopher318b6ee2010-09-02 00:53:56 +0000804bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
Eric Christopher404be0c2010-10-17 11:08:44 +0000805 unsigned Base, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000806 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000807 bool isFloat = false;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000808 bool needReg0Op = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000809 switch (VT.getSimpleVT().SimpleTy) {
810 default: return false;
811 case MVT::i1:
Eric Christopher2896df82010-10-15 18:02:07 +0000812 case MVT::i8:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000813 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
Eric Christopher15418772010-10-12 05:39:06 +0000814 break;
815 case MVT::i16:
Eric Christopher45c60712010-10-17 01:40:27 +0000816 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000817 needReg0Op = true;
Eric Christopher15418772010-10-12 05:39:06 +0000818 break;
Eric Christopher47650ec2010-10-16 01:10:35 +0000819 case MVT::i32:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000820 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
Eric Christopher47650ec2010-10-16 01:10:35 +0000821 break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000822 case MVT::f32:
823 if (!Subtarget->hasVFP2()) return false;
824 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000825 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000826 break;
827 case MVT::f64:
828 if (!Subtarget->hasVFP2()) return false;
829 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000830 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000831 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000832 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000833
Eric Christopher212ae932010-10-21 19:40:30 +0000834 ARMSimplifyRegOffset(Base, Offset, VT);
Jim Grosbach6b156392010-10-27 21:39:08 +0000835
Eric Christopher212ae932010-10-21 19:40:30 +0000836 // addrmode5 output depends on the selection dag addressing dividing the
837 // offset by 4 that it then later multiplies. Do this here as well.
838 if (isFloat)
839 Offset /= 4;
Jim Grosbach6b156392010-10-27 21:39:08 +0000840
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000841
842 // FIXME: The 'needReg0Op' bit goes away once STRH is converted to
843 // not use the mega-addrmode stuff.
844 if (!needReg0Op)
Eric Christopherb74558a2010-09-18 01:23:38 +0000845 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000846 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000847 .addReg(SrcReg).addReg(Base).addImm(Offset));
Eric Christopher318b6ee2010-09-02 00:53:56 +0000848 else
849 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher45547b82010-10-01 20:46:04 +0000850 TII.get(StrOpc))
Eric Christopher404be0c2010-10-17 11:08:44 +0000851 .addReg(SrcReg).addReg(Base).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000852
Eric Christopher318b6ee2010-09-02 00:53:56 +0000853 return true;
854}
855
Eric Christopher43b62be2010-09-27 06:02:23 +0000856bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000857 Value *Op0 = I->getOperand(0);
858 unsigned SrcReg = 0;
859
Eric Christopher543cf052010-09-01 22:16:27 +0000860 // Yay type legalization
861 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000862 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000863 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000864
Eric Christopher1b61ef42010-09-02 01:48:11 +0000865 // Get the value to be stored into a register.
866 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000867 if (SrcReg == 0)
868 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000869
Eric Christopher318b6ee2010-09-02 00:53:56 +0000870 // Our register and offset with innocuous defaults.
Eric Christopher404be0c2010-10-17 11:08:44 +0000871 unsigned Base = 0;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000872 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000873
Eric Christopher318b6ee2010-09-02 00:53:56 +0000874 // See if we can handle this as Reg + Offset
Eric Christophera3224252010-10-15 21:32:12 +0000875 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000876 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000877
Eric Christophera3224252010-10-15 21:32:12 +0000878 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000879
Eric Christophera5b1e682010-09-17 22:28:18 +0000880 return true;
881}
882
883static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
884 switch (Pred) {
885 // Needs two compares...
886 case CmpInst::FCMP_ONE:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000887 case CmpInst::FCMP_UEQ:
Eric Christophera5b1e682010-09-17 22:28:18 +0000888 default:
889 assert(false && "Unhandled CmpInst::Predicate!");
890 return ARMCC::AL;
891 case CmpInst::ICMP_EQ:
892 case CmpInst::FCMP_OEQ:
893 return ARMCC::EQ;
894 case CmpInst::ICMP_SGT:
895 case CmpInst::FCMP_OGT:
896 return ARMCC::GT;
897 case CmpInst::ICMP_SGE:
898 case CmpInst::FCMP_OGE:
899 return ARMCC::GE;
900 case CmpInst::ICMP_UGT:
901 case CmpInst::FCMP_UGT:
902 return ARMCC::HI;
903 case CmpInst::FCMP_OLT:
904 return ARMCC::MI;
905 case CmpInst::ICMP_ULE:
906 case CmpInst::FCMP_OLE:
907 return ARMCC::LS;
908 case CmpInst::FCMP_ORD:
909 return ARMCC::VC;
910 case CmpInst::FCMP_UNO:
911 return ARMCC::VS;
912 case CmpInst::FCMP_UGE:
913 return ARMCC::PL;
914 case CmpInst::ICMP_SLT:
915 case CmpInst::FCMP_ULT:
Eric Christopherdccd2c32010-10-11 08:38:55 +0000916 return ARMCC::LT;
Eric Christophera5b1e682010-09-17 22:28:18 +0000917 case CmpInst::ICMP_SLE:
918 case CmpInst::FCMP_ULE:
919 return ARMCC::LE;
920 case CmpInst::FCMP_UNE:
921 case CmpInst::ICMP_NE:
922 return ARMCC::NE;
923 case CmpInst::ICMP_UGE:
924 return ARMCC::HS;
925 case CmpInst::ICMP_ULT:
926 return ARMCC::LO;
927 }
Eric Christopher543cf052010-09-01 22:16:27 +0000928}
929
Eric Christopher43b62be2010-09-27 06:02:23 +0000930bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000931 const BranchInst *BI = cast<BranchInst>(I);
932 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
933 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000934
Eric Christophere5734102010-09-03 00:35:47 +0000935 // Simple branch support.
Eric Christopher0e6233b2010-10-29 21:08:19 +0000936
937 // If we can, avoid recomputing the compare - redoing it could lead to wonky
938 // behavior.
939 // TODO: Factor this out.
940 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
941 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
942 const Type *Ty = CI->getOperand(0)->getType();
943 EVT VT = TLI.getValueType(Ty);
944 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
945 if (isFloat && !Subtarget->hasVFP2())
946 return false;
947
948 unsigned CmpOpc;
949 unsigned CondReg;
950 switch (VT.getSimpleVT().SimpleTy) {
951 default: return false;
952 // TODO: Verify compares.
953 case MVT::f32:
954 CmpOpc = ARM::VCMPES;
955 CondReg = ARM::FPSCR;
956 break;
957 case MVT::f64:
958 CmpOpc = ARM::VCMPED;
959 CondReg = ARM::FPSCR;
960 break;
961 case MVT::i32:
962 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
963 CondReg = ARM::CPSR;
964 break;
965 }
966
967 // Get the compare predicate.
968 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
969
970 // We may not handle every CC for now.
971 if (ARMPred == ARMCC::AL) return false;
972
973 unsigned Arg1 = getRegForValue(CI->getOperand(0));
974 if (Arg1 == 0) return false;
975
976 unsigned Arg2 = getRegForValue(CI->getOperand(1));
977 if (Arg2 == 0) return false;
978
979 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
980 TII.get(CmpOpc))
981 .addReg(Arg1).addReg(Arg2));
982
983 // For floating point we need to move the result to a comparison register
984 // that we can then use for branches.
985 if (isFloat)
986 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
987 TII.get(ARM::FMSTAT)));
988
989 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
990 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
991 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
992 FastEmitBranch(FBB, DL);
993 FuncInfo.MBB->addSuccessor(TBB);
994 return true;
995 }
996 }
997
998 unsigned CmpReg = getRegForValue(BI->getCondition());
999 if (CmpReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001000
Eric Christopher229207a2010-09-29 01:14:47 +00001001 // Re-set the flags just in case.
1002 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1003 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
Eric Christopher0e6233b2010-10-29 21:08:19 +00001004 .addReg(CmpReg).addImm(1));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001005
Eric Christophere5734102010-09-03 00:35:47 +00001006 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +00001007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +00001008 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +00001009 FastEmitBranch(FBB, DL);
1010 FuncInfo.MBB->addSuccessor(TBB);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001011 return true;
Eric Christophere5734102010-09-03 00:35:47 +00001012}
1013
Eric Christopher43b62be2010-09-27 06:02:23 +00001014bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +00001015 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001016
Eric Christopherd43393a2010-09-08 23:13:45 +00001017 EVT VT;
1018 const Type *Ty = CI->getOperand(0)->getType();
1019 if (!isTypeLegal(Ty, VT))
1020 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001021
Eric Christopherd43393a2010-09-08 23:13:45 +00001022 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1023 if (isFloat && !Subtarget->hasVFP2())
1024 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001025
Eric Christopherd43393a2010-09-08 23:13:45 +00001026 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +00001027 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +00001028 switch (VT.getSimpleVT().SimpleTy) {
1029 default: return false;
1030 // TODO: Verify compares.
1031 case MVT::f32:
1032 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +00001033 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001034 break;
1035 case MVT::f64:
1036 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +00001037 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001038 break;
1039 case MVT::i32:
1040 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +00001041 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +00001042 break;
1043 }
1044
Eric Christopher229207a2010-09-29 01:14:47 +00001045 // Get the compare predicate.
1046 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
Eric Christopherdccd2c32010-10-11 08:38:55 +00001047
Eric Christopher229207a2010-09-29 01:14:47 +00001048 // We may not handle every CC for now.
1049 if (ARMPred == ARMCC::AL) return false;
1050
Eric Christopherd43393a2010-09-08 23:13:45 +00001051 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1052 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001053
Eric Christopherd43393a2010-09-08 23:13:45 +00001054 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1055 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001056
Eric Christopherd43393a2010-09-08 23:13:45 +00001057 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1058 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +00001059
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001060 // For floating point we need to move the result to a comparison register
1061 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +00001062 if (isFloat)
1063 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1064 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +00001065
Eric Christopher229207a2010-09-29 01:14:47 +00001066 // Now set a register based on the comparison. Explicitly set the predicates
1067 // here.
Eric Christopher338c2532010-10-07 05:31:49 +00001068 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001069 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
Eric Christopher5d18d922010-10-07 05:39:19 +00001070 : ARM::GPRRegisterClass;
1071 unsigned DestReg = createResultReg(RC);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001072 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +00001073 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +00001074 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1076 .addReg(ZeroReg).addImm(1)
1077 .addImm(ARMPred).addReg(CondReg);
1078
Eric Christophera5b1e682010-09-17 22:28:18 +00001079 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +00001080 return true;
1081}
1082
Eric Christopher43b62be2010-09-27 06:02:23 +00001083bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +00001084 // Make sure we have VFP and that we're extending float to double.
1085 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001086
Eric Christopher46203602010-09-09 00:26:48 +00001087 Value *V = I->getOperand(0);
1088 if (!I->getType()->isDoubleTy() ||
1089 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001090
Eric Christopher46203602010-09-09 00:26:48 +00001091 unsigned Op = getRegForValue(V);
1092 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001093
Eric Christopher46203602010-09-09 00:26:48 +00001094 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001095 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001096 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +00001097 .addReg(Op));
1098 UpdateValueMap(I, Result);
1099 return true;
1100}
1101
Eric Christopher43b62be2010-09-27 06:02:23 +00001102bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +00001103 // Make sure we have VFP and that we're truncating double to float.
1104 if (!Subtarget->hasVFP2()) return false;
1105
1106 Value *V = I->getOperand(0);
Eric Christopher022b7fb2010-10-05 23:13:24 +00001107 if (!(I->getType()->isFloatTy() &&
1108 V->getType()->isDoubleTy())) return false;
Eric Christopherce07b542010-09-09 20:26:31 +00001109
1110 unsigned Op = getRegForValue(V);
1111 if (Op == 0) return false;
1112
1113 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +00001114 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +00001115 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +00001116 .addReg(Op));
1117 UpdateValueMap(I, Result);
1118 return true;
1119}
1120
Eric Christopher43b62be2010-09-27 06:02:23 +00001121bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001122 // Make sure we have VFP.
1123 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001124
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001125 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001126 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001127 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001128 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001129
Eric Christopher9a040492010-09-09 18:54:59 +00001130 unsigned Op = getRegForValue(I->getOperand(0));
1131 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001132
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001133 // The conversion routine works on fp-reg to fp-reg and the operand above
1134 // was an integer, move it to the fp registers if possible.
Eric Christopher022b7fb2010-10-05 23:13:24 +00001135 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001136 if (FP == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001137
Eric Christopher9a040492010-09-09 18:54:59 +00001138 unsigned Opc;
1139 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1140 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1141 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001142
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001143 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +00001144 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1145 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001146 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +00001147 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001148 return true;
1149}
1150
Eric Christopher43b62be2010-09-27 06:02:23 +00001151bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +00001152 // Make sure we have VFP.
1153 if (!Subtarget->hasVFP2()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001154
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001155 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +00001156 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +00001157 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +00001158 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001159
Eric Christopher9a040492010-09-09 18:54:59 +00001160 unsigned Op = getRegForValue(I->getOperand(0));
1161 if (Op == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001162
Eric Christopher9a040492010-09-09 18:54:59 +00001163 unsigned Opc;
1164 const Type *OpTy = I->getOperand(0)->getType();
1165 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1166 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1167 else return 0;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001168
Eric Christopher022b7fb2010-10-05 23:13:24 +00001169 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1170 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
Eric Christopher9a040492010-09-09 18:54:59 +00001171 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1172 ResultReg)
1173 .addReg(Op));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001174
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001175 // This result needs to be in an integer register, but the conversion only
1176 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +00001177 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001178 if (IntReg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001179
Eric Christopher9ee4ce22010-09-09 21:44:45 +00001180 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +00001181 return true;
1182}
1183
Eric Christopher3bbd3962010-10-11 08:27:59 +00001184bool ARMFastISel::SelectSelect(const Instruction *I) {
1185 EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
1186 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
1187 return false;
1188
1189 // Things need to be register sized for register moves.
1190 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
1191 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1192
1193 unsigned CondReg = getRegForValue(I->getOperand(0));
1194 if (CondReg == 0) return false;
1195 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1196 if (Op1Reg == 0) return false;
1197 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1198 if (Op2Reg == 0) return false;
1199
1200 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1202 .addReg(CondReg).addImm(1));
1203 unsigned ResultReg = createResultReg(RC);
1204 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1206 .addReg(Op1Reg).addReg(Op2Reg)
1207 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1208 UpdateValueMap(I, ResultReg);
1209 return true;
1210}
1211
Eric Christopher08637852010-09-30 22:34:19 +00001212bool ARMFastISel::SelectSDiv(const Instruction *I) {
1213 EVT VT;
1214 const Type *Ty = I->getType();
1215 if (!isTypeLegal(Ty, VT))
1216 return false;
1217
1218 // If we have integer div support we should have selected this automagically.
1219 // In case we have a real miss go ahead and return false and we'll pick
1220 // it up later.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001221 if (Subtarget->hasDivide()) return false;
1222
Eric Christopher08637852010-09-30 22:34:19 +00001223 // Otherwise emit a libcall.
1224 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Eric Christopher7bdc4de2010-10-11 08:31:54 +00001225 if (VT == MVT::i8)
1226 LC = RTLIB::SDIV_I8;
1227 else if (VT == MVT::i16)
Eric Christopher08637852010-09-30 22:34:19 +00001228 LC = RTLIB::SDIV_I16;
1229 else if (VT == MVT::i32)
1230 LC = RTLIB::SDIV_I32;
1231 else if (VT == MVT::i64)
1232 LC = RTLIB::SDIV_I64;
1233 else if (VT == MVT::i128)
1234 LC = RTLIB::SDIV_I128;
1235 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopherdccd2c32010-10-11 08:38:55 +00001236
Eric Christopher08637852010-09-30 22:34:19 +00001237 return ARMEmitLibcall(I, LC);
1238}
1239
Eric Christopher6a880d62010-10-11 08:37:26 +00001240bool ARMFastISel::SelectSRem(const Instruction *I) {
1241 EVT VT;
1242 const Type *Ty = I->getType();
1243 if (!isTypeLegal(Ty, VT))
1244 return false;
1245
1246 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1247 if (VT == MVT::i8)
1248 LC = RTLIB::SREM_I8;
1249 else if (VT == MVT::i16)
1250 LC = RTLIB::SREM_I16;
1251 else if (VT == MVT::i32)
1252 LC = RTLIB::SREM_I32;
1253 else if (VT == MVT::i64)
1254 LC = RTLIB::SREM_I64;
1255 else if (VT == MVT::i128)
1256 LC = RTLIB::SREM_I128;
Eric Christophera1640d92010-10-11 08:40:05 +00001257 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
Eric Christopher2896df82010-10-15 18:02:07 +00001258
Eric Christopher6a880d62010-10-11 08:37:26 +00001259 return ARMEmitLibcall(I, LC);
1260}
1261
Eric Christopher43b62be2010-09-27 06:02:23 +00001262bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +00001263 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +00001264
Eric Christopherbc39b822010-09-09 00:53:57 +00001265 // We can get here in the case when we want to use NEON for our fp
1266 // operations, but can't figure out how to. Just use the vfp instructions
1267 // if we have them.
1268 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +00001269 const Type *Ty = I->getType();
1270 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1271 if (isFloat && !Subtarget->hasVFP2())
1272 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001273
Eric Christopherbc39b822010-09-09 00:53:57 +00001274 unsigned Op1 = getRegForValue(I->getOperand(0));
1275 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001276
Eric Christopherbc39b822010-09-09 00:53:57 +00001277 unsigned Op2 = getRegForValue(I->getOperand(1));
1278 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001279
Eric Christopherbc39b822010-09-09 00:53:57 +00001280 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +00001281 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
1282 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +00001283 switch (ISDOpcode) {
1284 default: return false;
1285 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001286 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001287 break;
1288 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001289 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001290 break;
1291 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +00001292 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +00001293 break;
1294 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001295 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1297 TII.get(Opc), ResultReg)
1298 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001299 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001300 return true;
1301}
1302
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001303// Call Handling Code
1304
Eric Christopherfa87d662010-10-18 02:17:53 +00001305bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1306 EVT SrcVT, unsigned &ResultReg) {
1307 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1308 Src, /*TODO: Kill=*/false);
Jim Grosbach6b156392010-10-27 21:39:08 +00001309
Eric Christopherfa87d662010-10-18 02:17:53 +00001310 if (RR != 0) {
1311 ResultReg = RR;
1312 return true;
1313 } else
Jim Grosbach6b156392010-10-27 21:39:08 +00001314 return false;
Eric Christopherfa87d662010-10-18 02:17:53 +00001315}
1316
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001317// This is largely taken directly from CCAssignFnForNode - we don't support
1318// varargs in FastISel so that part has been removed.
1319// TODO: We may not support all of this.
1320CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1321 switch (CC) {
1322 default:
1323 llvm_unreachable("Unsupported calling convention");
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001324 case CallingConv::Fast:
Evan Cheng1f8b40d2010-10-22 18:57:05 +00001325 // Ignore fastcc. Silence compiler warnings.
1326 (void)RetFastCC_ARM_APCS;
1327 (void)FastCC_ARM_APCS;
1328 // Fallthrough
1329 case CallingConv::C:
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001330 // Use target triple & subtarget features to do actual dispatch.
1331 if (Subtarget->isAAPCS_ABI()) {
1332 if (Subtarget->hasVFP2() &&
1333 FloatABIType == FloatABI::Hard)
1334 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1335 else
1336 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1337 } else
1338 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1339 case CallingConv::ARM_AAPCS_VFP:
1340 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1341 case CallingConv::ARM_AAPCS:
1342 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1343 case CallingConv::ARM_APCS:
1344 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1345 }
1346}
1347
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001348bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1349 SmallVectorImpl<unsigned> &ArgRegs,
1350 SmallVectorImpl<EVT> &ArgVTs,
1351 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1352 SmallVectorImpl<unsigned> &RegArgs,
1353 CallingConv::ID CC,
1354 unsigned &NumBytes) {
1355 SmallVector<CCValAssign, 16> ArgLocs;
1356 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1357 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1358
1359 // Get a count of how many bytes are to be pushed on the stack.
1360 NumBytes = CCInfo.getNextStackOffset();
1361
1362 // Issue CALLSEQ_START
1363 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1365 TII.get(AdjStackDown))
1366 .addImm(NumBytes));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001367
1368 // Process the args.
1369 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1370 CCValAssign &VA = ArgLocs[i];
1371 unsigned Arg = ArgRegs[VA.getValNo()];
1372 EVT ArgVT = ArgVTs[VA.getValNo()];
1373
Eric Christophera4633f52010-10-23 09:37:17 +00001374 // We don't handle NEON parameters yet.
1375 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1376 return false;
1377
Eric Christopherf9764fa2010-09-30 20:49:44 +00001378 // Handle arg promotion, etc.
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001379 switch (VA.getLocInfo()) {
1380 case CCValAssign::Full: break;
Eric Christopherfa87d662010-10-18 02:17:53 +00001381 case CCValAssign::SExt: {
1382 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1383 Arg, ArgVT, Arg);
1384 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1385 Emitted = true;
1386 ArgVT = VA.getLocVT();
1387 break;
1388 }
1389 case CCValAssign::ZExt: {
1390 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1391 Arg, ArgVT, Arg);
1392 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1393 Emitted = true;
1394 ArgVT = VA.getLocVT();
1395 break;
1396 }
1397 case CCValAssign::AExt: {
Eric Christopherfa87d662010-10-18 02:17:53 +00001398 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1399 Arg, ArgVT, Arg);
1400 if (!Emitted)
1401 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1402 Arg, ArgVT, Arg);
1403 if (!Emitted)
1404 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1405 Arg, ArgVT, Arg);
1406
1407 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1408 ArgVT = VA.getLocVT();
1409 break;
1410 }
1411 case CCValAssign::BCvt: {
1412 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(),
1413 VA.getLocVT().getSimpleVT(),
1414 ISD::BIT_CONVERT, Arg, /*TODO: Kill=*/false);
1415 assert(BC != 0 && "Failed to emit a bitcast!");
1416 Arg = BC;
1417 ArgVT = VA.getLocVT();
1418 break;
1419 }
1420 default: llvm_unreachable("Unknown arg promotion!");
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001421 }
1422
1423 // Now copy/store arg to correct locations.
Eric Christopherfb0b8922010-10-11 21:20:02 +00001424 if (VA.isRegLoc() && !VA.needsCustom()) {
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopherf9764fa2010-09-30 20:49:44 +00001426 VA.getLocReg())
1427 .addReg(Arg);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001428 RegArgs.push_back(VA.getLocReg());
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001429 } else if (VA.needsCustom()) {
1430 // TODO: We need custom lowering for vector (v2f64) args.
1431 if (VA.getLocVT() != MVT::f64) return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001432
Eric Christopher2d8f6fe2010-10-21 00:01:47 +00001433 CCValAssign &NextVA = ArgLocs[++i];
1434
1435 // TODO: Only handle register args for now.
1436 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1437
1438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1439 TII.get(ARM::VMOVRRD), VA.getLocReg())
1440 .addReg(NextVA.getLocReg(), RegState::Define)
1441 .addReg(Arg));
1442 RegArgs.push_back(VA.getLocReg());
1443 RegArgs.push_back(NextVA.getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001444 } else {
Eric Christopher5b924802010-10-21 20:09:54 +00001445 assert(VA.isMemLoc());
1446 // Need to store on the stack.
1447 unsigned Base = ARM::SP;
1448 int Offset = VA.getLocMemOffset();
1449
1450 if (!ARMEmitStore(ArgVT, Arg, Base, Offset)) return false;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001451 }
1452 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001453 return true;
1454}
1455
1456bool ARMFastISel::FinishCall(EVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1457 const Instruction *I, CallingConv::ID CC,
1458 unsigned &NumBytes) {
1459 // Issue CALLSEQ_END
1460 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eric Christopherfb0b8922010-10-11 21:20:02 +00001461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1462 TII.get(AdjStackUp))
1463 .addImm(NumBytes).addImm(0));
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001464
1465 // Now the return value.
1466 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1467 SmallVector<CCValAssign, 16> RVLocs;
1468 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1469 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1470
1471 // Copy all of the result registers out of their specified physreg.
Eric Christopher14df8822010-10-01 00:00:11 +00001472 if (RVLocs.size() == 2 && RetVT.getSimpleVT().SimpleTy == MVT::f64) {
1473 // For this move we copy into two registers and then move into the
1474 // double fp reg we want.
Eric Christopher14df8822010-10-01 00:00:11 +00001475 EVT DestVT = RVLocs[0].getValVT();
1476 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1477 unsigned ResultReg = createResultReg(DstRC);
1478 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1479 TII.get(ARM::VMOVDRR), ResultReg)
Eric Christopher3659ac22010-10-20 08:02:24 +00001480 .addReg(RVLocs[0].getLocReg())
1481 .addReg(RVLocs[1].getLocReg()));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001482
Eric Christopher3659ac22010-10-20 08:02:24 +00001483 UsedRegs.push_back(RVLocs[0].getLocReg());
1484 UsedRegs.push_back(RVLocs[1].getLocReg());
Jim Grosbach6b156392010-10-27 21:39:08 +00001485
Eric Christopherdccd2c32010-10-11 08:38:55 +00001486 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001487 UpdateValueMap(I, ResultReg);
1488 } else {
Jim Grosbach95369592010-10-13 23:34:31 +00001489 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
Eric Christopher14df8822010-10-01 00:00:11 +00001490 EVT CopyVT = RVLocs[0].getValVT();
1491 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001492
Eric Christopher14df8822010-10-01 00:00:11 +00001493 unsigned ResultReg = createResultReg(DstRC);
1494 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1495 ResultReg).addReg(RVLocs[0].getLocReg());
1496 UsedRegs.push_back(RVLocs[0].getLocReg());
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001497
Eric Christopherdccd2c32010-10-11 08:38:55 +00001498 // Finally update the result.
Eric Christopher14df8822010-10-01 00:00:11 +00001499 UpdateValueMap(I, ResultReg);
1500 }
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001501 }
1502
Eric Christopherdccd2c32010-10-11 08:38:55 +00001503 return true;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001504}
1505
Eric Christopher4f512ef2010-10-22 01:28:00 +00001506bool ARMFastISel::SelectRet(const Instruction *I) {
1507 const ReturnInst *Ret = cast<ReturnInst>(I);
1508 const Function &F = *I->getParent()->getParent();
Jim Grosbach6b156392010-10-27 21:39:08 +00001509
Eric Christopher4f512ef2010-10-22 01:28:00 +00001510 if (!FuncInfo.CanLowerReturn)
1511 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001512
Eric Christopher4f512ef2010-10-22 01:28:00 +00001513 if (F.isVarArg())
1514 return false;
1515
1516 CallingConv::ID CC = F.getCallingConv();
1517 if (Ret->getNumOperands() > 0) {
1518 SmallVector<ISD::OutputArg, 4> Outs;
1519 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1520 Outs, TLI);
1521
1522 // Analyze operands of the call, assigning locations to each operand.
1523 SmallVector<CCValAssign, 16> ValLocs;
1524 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1525 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1526
1527 const Value *RV = Ret->getOperand(0);
1528 unsigned Reg = getRegForValue(RV);
1529 if (Reg == 0)
1530 return false;
1531
1532 // Only handle a single return value for now.
1533 if (ValLocs.size() != 1)
1534 return false;
1535
1536 CCValAssign &VA = ValLocs[0];
Jim Grosbach6b156392010-10-27 21:39:08 +00001537
Eric Christopher4f512ef2010-10-22 01:28:00 +00001538 // Don't bother handling odd stuff for now.
1539 if (VA.getLocInfo() != CCValAssign::Full)
1540 return false;
1541 // Only handle register returns for now.
1542 if (!VA.isRegLoc())
1543 return false;
1544 // TODO: For now, don't try to handle cases where getLocInfo()
1545 // says Full but the types don't match.
1546 if (VA.getValVT() != TLI.getValueType(RV->getType()))
1547 return false;
Jim Grosbach6b156392010-10-27 21:39:08 +00001548
Eric Christopher4f512ef2010-10-22 01:28:00 +00001549 // Make the copy.
1550 unsigned SrcReg = Reg + VA.getValNo();
1551 unsigned DstReg = VA.getLocReg();
1552 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1553 // Avoid a cross-class copy. This is very unlikely.
1554 if (!SrcRC->contains(DstReg))
1555 return false;
1556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1557 DstReg).addReg(SrcReg);
1558
1559 // Mark the register as live out of the function.
1560 MRI.addLiveOut(VA.getLocReg());
1561 }
Jim Grosbach6b156392010-10-27 21:39:08 +00001562
Eric Christopher4f512ef2010-10-22 01:28:00 +00001563 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1564 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1565 TII.get(RetOpc)));
1566 return true;
1567}
1568
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001569// A quick function that will emit a call for a named libcall in F with the
1570// vector of passed arguments for the Instruction in I. We can assume that we
Eric Christopherdccd2c32010-10-11 08:38:55 +00001571// can emit a call for any libcall we can produce. This is an abridged version
1572// of the full call infrastructure since we won't need to worry about things
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001573// like computed function pointers or strange arguments at call sites.
1574// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1575// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001576bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1577 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001578
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001579 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001580 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001581 EVT RetVT;
1582 if (RetTy->isVoidTy())
1583 RetVT = MVT::isVoid;
1584 else if (!isTypeLegal(RetTy, RetVT))
1585 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001586
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001587 // For now we're using BLX etc on the assumption that we have v5t ops.
1588 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001589
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001590 // Set up the argument vectors.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001591 SmallVector<Value*, 8> Args;
1592 SmallVector<unsigned, 8> ArgRegs;
1593 SmallVector<EVT, 8> ArgVTs;
1594 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1595 Args.reserve(I->getNumOperands());
1596 ArgRegs.reserve(I->getNumOperands());
1597 ArgVTs.reserve(I->getNumOperands());
1598 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001599 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001600 Value *Op = I->getOperand(i);
1601 unsigned Arg = getRegForValue(Op);
1602 if (Arg == 0) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001603
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001604 const Type *ArgTy = Op->getType();
1605 EVT ArgVT;
1606 if (!isTypeLegal(ArgTy, ArgVT)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001607
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001608 ISD::ArgFlagsTy Flags;
1609 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1610 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001611
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001612 Args.push_back(Op);
1613 ArgRegs.push_back(Arg);
1614 ArgVTs.push_back(ArgVT);
1615 ArgFlags.push_back(Flags);
1616 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001617
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001618 // Handle the arguments now that we've gotten them.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001619 SmallVector<unsigned, 4> RegArgs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001620 unsigned NumBytes;
1621 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1622 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001623
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001624 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001625 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001626 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001627 unsigned CallOpc;
1628 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001629 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001630 else
1631 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001632 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001633 .addExternalSymbol(TLI.getLibcallName(Call));
Eric Christopherdccd2c32010-10-11 08:38:55 +00001634
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001635 // Add implicit physical register uses to the call.
1636 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1637 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001638
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001639 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001640 SmallVector<unsigned, 4> UsedRegs;
Eric Christophera9a7a1a2010-09-29 23:11:09 +00001641 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001642
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001643 // Set all unused physreg defs as dead.
1644 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001645
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001646 return true;
1647}
1648
Eric Christopherf9764fa2010-09-30 20:49:44 +00001649bool ARMFastISel::SelectCall(const Instruction *I) {
1650 const CallInst *CI = cast<CallInst>(I);
1651 const Value *Callee = CI->getCalledValue();
1652
1653 // Can't handle inline asm or worry about intrinsics yet.
1654 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1655
Eric Christophere6ca6772010-10-01 21:33:12 +00001656 // Only handle global variable Callees that are direct calls.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001657 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
Eric Christophere6ca6772010-10-01 21:33:12 +00001658 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1659 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001660
Eric Christopherf9764fa2010-09-30 20:49:44 +00001661 // Check the calling convention.
1662 ImmutableCallSite CS(CI);
1663 CallingConv::ID CC = CS.getCallingConv();
Eric Christopher4cf34c62010-10-18 06:49:12 +00001664
Eric Christopherf9764fa2010-09-30 20:49:44 +00001665 // TODO: Avoid some calling conventions?
Eric Christopherdccd2c32010-10-11 08:38:55 +00001666
Eric Christopherf9764fa2010-09-30 20:49:44 +00001667 // Let SDISel handle vararg functions.
1668 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1669 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1670 if (FTy->isVarArg())
1671 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001672
Eric Christopherf9764fa2010-09-30 20:49:44 +00001673 // Handle *simple* calls for now.
1674 const Type *RetTy = I->getType();
1675 EVT RetVT;
1676 if (RetTy->isVoidTy())
1677 RetVT = MVT::isVoid;
1678 else if (!isTypeLegal(RetTy, RetVT))
1679 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001680
Eric Christopherf9764fa2010-09-30 20:49:44 +00001681 // For now we're using BLX etc on the assumption that we have v5t ops.
1682 // TODO: Maybe?
1683 if (!Subtarget->hasV5TOps()) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001684
Eric Christopherf9764fa2010-09-30 20:49:44 +00001685 // Set up the argument vectors.
1686 SmallVector<Value*, 8> Args;
1687 SmallVector<unsigned, 8> ArgRegs;
1688 SmallVector<EVT, 8> ArgVTs;
1689 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1690 Args.reserve(CS.arg_size());
1691 ArgRegs.reserve(CS.arg_size());
1692 ArgVTs.reserve(CS.arg_size());
1693 ArgFlags.reserve(CS.arg_size());
1694 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1695 i != e; ++i) {
1696 unsigned Arg = getRegForValue(*i);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001697
Eric Christopherf9764fa2010-09-30 20:49:44 +00001698 if (Arg == 0)
1699 return false;
1700 ISD::ArgFlagsTy Flags;
1701 unsigned AttrInd = i - CS.arg_begin() + 1;
1702 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1703 Flags.setSExt();
1704 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1705 Flags.setZExt();
1706
1707 // FIXME: Only handle *easy* calls for now.
1708 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1709 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1710 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1711 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1712 return false;
1713
1714 const Type *ArgTy = (*i)->getType();
1715 EVT ArgVT;
1716 if (!isTypeLegal(ArgTy, ArgVT))
1717 return false;
1718 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1719 Flags.setOrigAlign(OriginalAlignment);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001720
Eric Christopherf9764fa2010-09-30 20:49:44 +00001721 Args.push_back(*i);
1722 ArgRegs.push_back(Arg);
1723 ArgVTs.push_back(ArgVT);
1724 ArgFlags.push_back(Flags);
1725 }
Eric Christopherdccd2c32010-10-11 08:38:55 +00001726
Eric Christopherf9764fa2010-09-30 20:49:44 +00001727 // Handle the arguments now that we've gotten them.
1728 SmallVector<unsigned, 4> RegArgs;
1729 unsigned NumBytes;
1730 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1731 return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001732
Eric Christopherf9764fa2010-09-30 20:49:44 +00001733 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001734 // TODO: Turn this into the table of arm call ops.
Eric Christopherf9764fa2010-09-30 20:49:44 +00001735 MachineInstrBuilder MIB;
1736 unsigned CallOpc;
1737 if(isThumb)
1738 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1739 else
1740 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1741 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1742 .addGlobalAddress(GV, 0, 0);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001743
Eric Christopherf9764fa2010-09-30 20:49:44 +00001744 // Add implicit physical register uses to the call.
1745 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1746 MIB.addReg(RegArgs[i]);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001747
Eric Christopherf9764fa2010-09-30 20:49:44 +00001748 // Finish off the call including any return values.
Eric Christopherdccd2c32010-10-11 08:38:55 +00001749 SmallVector<unsigned, 4> UsedRegs;
Eric Christopherf9764fa2010-09-30 20:49:44 +00001750 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001751
Eric Christopherf9764fa2010-09-30 20:49:44 +00001752 // Set all unused physreg defs as dead.
1753 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherdccd2c32010-10-11 08:38:55 +00001754
Eric Christopherf9764fa2010-09-30 20:49:44 +00001755 return true;
Eric Christopherdccd2c32010-10-11 08:38:55 +00001756
Eric Christopherf9764fa2010-09-30 20:49:44 +00001757}
1758
Eric Christopher56d2b722010-09-02 23:43:26 +00001759// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001760bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001761 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001762 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001763
Eric Christopherab695882010-07-21 22:26:11 +00001764 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001765 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001766 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001767 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001768 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001769 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001770 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001771 case Instruction::ICmp:
1772 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001773 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001774 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001775 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001776 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001777 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001778 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001779 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001780 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001781 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001782 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001783 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001784 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001785 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001786 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001787 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001788 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001789 return SelectSDiv(I);
Eric Christopher6a880d62010-10-11 08:37:26 +00001790 case Instruction::SRem:
1791 return SelectSRem(I);
Eric Christopherf9764fa2010-09-30 20:49:44 +00001792 case Instruction::Call:
1793 return SelectCall(I);
Eric Christopher3bbd3962010-10-11 08:27:59 +00001794 case Instruction::Select:
1795 return SelectSelect(I);
Eric Christopher4f512ef2010-10-22 01:28:00 +00001796 case Instruction::Ret:
1797 return SelectRet(I);
Eric Christopherab695882010-07-21 22:26:11 +00001798 default: break;
1799 }
1800 return false;
1801}
1802
1803namespace llvm {
1804 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopherfeadddd2010-10-11 20:05:22 +00001805 // Completely untested on non-darwin.
1806 const TargetMachine &TM = funcInfo.MF->getTarget();
1807 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher6e5367d2010-10-18 22:53:53 +00001808 if (Subtarget->isTargetDarwin() && !DisableARMFastISel)
Eric Christopherfeadddd2010-10-11 20:05:22 +00001809 return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001810 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001811 }
1812}