blob: 53ae75200f821cdb315ea09155ce63e91bfa11cf [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"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Instructions.h"
26#include "llvm/IntrinsicInst.h"
Eric Christopherbb3e5da2010-09-14 23:03:37 +000027#include "llvm/Module.h"
Eric Christopherab695882010-07-21 22:26:11 +000028#include "llvm/CodeGen/Analysis.h"
29#include "llvm/CodeGen/FastISel.h"
30#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000033#include "llvm/CodeGen/MachineConstantPool.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineRegisterInfo.h"
36#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000037#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000040#include "llvm/Target/TargetData.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000044#include "llvm/Target/TargetOptions.h"
45using namespace llvm;
46
Eric Christopher038fea52010-08-17 00:46:57 +000047static cl::opt<bool>
48EnableARMFastISel("arm-fast-isel",
49 cl::desc("Turn on experimental ARM fast-isel support"),
50 cl::init(false), cl::Hidden);
51
Eric Christopherab695882010-07-21 22:26:11 +000052namespace {
53
54class ARMFastISel : public FastISel {
55
56 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
57 /// make the right decision when generating code for different targets.
58 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000059 const TargetMachine &TM;
60 const TargetInstrInfo &TII;
61 const TargetLowering &TLI;
Eric Christopher7fe55b72010-08-23 22:32:45 +000062 const ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000063
Eric Christophereaa204b2010-09-02 01:39:14 +000064 // Convenience variable to avoid checking all the time.
65 bool isThumb;
66
Eric Christopherab695882010-07-21 22:26:11 +000067 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000068 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000069 : FastISel(funcInfo),
70 TM(funcInfo.MF->getTarget()),
71 TII(*TM.getInstrInfo()),
72 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000073 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000074 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000075 isThumb = AFI->isThumbFunction();
Eric Christopherab695882010-07-21 22:26:11 +000076 }
77
Eric Christophercb592292010-08-20 00:20:31 +000078 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000079 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
80 const TargetRegisterClass *RC);
81 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
82 const TargetRegisterClass *RC,
83 unsigned Op0, bool Op0IsKill);
84 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC,
86 unsigned Op0, bool Op0IsKill,
87 unsigned Op1, bool Op1IsKill);
88 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
89 const TargetRegisterClass *RC,
90 unsigned Op0, bool Op0IsKill,
91 uint64_t Imm);
92 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
93 const TargetRegisterClass *RC,
94 unsigned Op0, bool Op0IsKill,
95 const ConstantFP *FPImm);
96 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
97 const TargetRegisterClass *RC,
98 uint64_t Imm);
99 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
100 const TargetRegisterClass *RC,
101 unsigned Op0, bool Op0IsKill,
102 unsigned Op1, bool Op1IsKill,
103 uint64_t Imm);
104 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
105 unsigned Op0, bool Op0IsKill,
106 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000107
Eric Christophercb592292010-08-20 00:20:31 +0000108 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000109 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000110 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000111
112 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000113
Eric Christopher83007122010-08-23 21:44:12 +0000114 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000115 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000116 virtual bool SelectLoad(const Instruction *I);
117 virtual bool SelectStore(const Instruction *I);
118 virtual bool SelectBranch(const Instruction *I);
119 virtual bool SelectCmp(const Instruction *I);
120 virtual bool SelectFPExt(const Instruction *I);
121 virtual bool SelectFPTrunc(const Instruction *I);
122 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
123 virtual bool SelectSIToFP(const Instruction *I);
124 virtual bool SelectFPToSI(const Instruction *I);
125 virtual bool SelectSDiv(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000126
Eric Christopher83007122010-08-23 21:44:12 +0000127 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000128 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000129 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000130 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000131 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000132 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000133 bool ARMLoadAlloca(const Instruction *I, EVT VT);
134 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000135 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000136 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000137 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000138 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000139 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000140
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000141 // Call handling routines.
142 private:
143 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000144 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000145
146 // OptionalDef handling routines.
147 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000148 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
149 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
150};
Eric Christopherab695882010-07-21 22:26:11 +0000151
152} // end anonymous namespace
153
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000154#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000155
Eric Christopher456144e2010-08-19 00:37:05 +0000156// DefinesOptionalPredicate - This is different from DefinesPredicate in that
157// we don't care about implicit defs here, just places we'll need to add a
158// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
159bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
160 const TargetInstrDesc &TID = MI->getDesc();
161 if (!TID.hasOptionalDef())
162 return false;
163
164 // Look to see if our OptionalDef is defining CPSR or CCR.
165 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
166 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000167 if (!MO.isReg() || !MO.isDef()) continue;
168 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000169 *CPSR = true;
170 }
171 return true;
172}
173
174// If the machine is predicable go ahead and add the predicate operands, if
175// it needs default CC operands add those.
176const MachineInstrBuilder &
177ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
178 MachineInstr *MI = &*MIB;
179
180 // Do we use a predicate?
181 if (TII.isPredicable(MI))
182 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000183
Eric Christopher456144e2010-08-19 00:37:05 +0000184 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
185 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000186 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000187 if (DefinesOptionalPredicate(MI, &CPSR)) {
188 if (CPSR)
189 AddDefaultT1CC(MIB);
190 else
191 AddDefaultCC(MIB);
192 }
193 return MIB;
194}
195
Eric Christopher0fe7d542010-08-17 01:25:29 +0000196unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
197 const TargetRegisterClass* RC) {
198 unsigned ResultReg = createResultReg(RC);
199 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
200
Eric Christopher456144e2010-08-19 00:37:05 +0000201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000202 return ResultReg;
203}
204
205unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
206 const TargetRegisterClass *RC,
207 unsigned Op0, bool Op0IsKill) {
208 unsigned ResultReg = createResultReg(RC);
209 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
210
211 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000212 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000213 .addReg(Op0, Op0IsKill * RegState::Kill));
214 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000215 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000216 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000217 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218 TII.get(TargetOpcode::COPY), ResultReg)
219 .addReg(II.ImplicitDefs[0]));
220 }
221 return ResultReg;
222}
223
224unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
225 const TargetRegisterClass *RC,
226 unsigned Op0, bool Op0IsKill,
227 unsigned Op1, bool Op1IsKill) {
228 unsigned ResultReg = createResultReg(RC);
229 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
230
231 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000232 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000233 .addReg(Op0, Op0IsKill * RegState::Kill)
234 .addReg(Op1, Op1IsKill * RegState::Kill));
235 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 .addReg(Op0, Op0IsKill * RegState::Kill)
238 .addReg(Op1, Op1IsKill * 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_ri(unsigned MachineInstOpcode,
247 const TargetRegisterClass *RC,
248 unsigned Op0, bool Op0IsKill,
249 uint64_t Imm) {
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 .addImm(Imm));
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 .addImm(Imm));
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_rf(unsigned MachineInstOpcode,
269 const TargetRegisterClass *RC,
270 unsigned Op0, bool Op0IsKill,
271 const ConstantFP *FPImm) {
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 .addFPImm(FPImm));
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 .addFPImm(FPImm));
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_rri(unsigned MachineInstOpcode,
291 const TargetRegisterClass *RC,
292 unsigned Op0, bool Op0IsKill,
293 unsigned Op1, bool Op1IsKill,
294 uint64_t Imm) {
295 unsigned ResultReg = createResultReg(RC);
296 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
297
298 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300 .addReg(Op0, Op0IsKill * RegState::Kill)
301 .addReg(Op1, Op1IsKill * RegState::Kill)
302 .addImm(Imm));
303 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addReg(Op0, Op0IsKill * RegState::Kill)
306 .addReg(Op1, Op1IsKill * RegState::Kill)
307 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 TII.get(TargetOpcode::COPY), ResultReg)
310 .addReg(II.ImplicitDefs[0]));
311 }
312 return ResultReg;
313}
314
315unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 uint64_t Imm) {
318 unsigned ResultReg = createResultReg(RC);
319 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000320
Eric Christopher0fe7d542010-08-17 01:25:29 +0000321 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 .addImm(Imm));
324 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000325 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000326 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000328 TII.get(TargetOpcode::COPY), ResultReg)
329 .addReg(II.ImplicitDefs[0]));
330 }
331 return ResultReg;
332}
333
334unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
335 unsigned Op0, bool Op0IsKill,
336 uint32_t Idx) {
337 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
338 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
339 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000341 DL, TII.get(TargetOpcode::COPY), ResultReg)
342 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
343 return ResultReg;
344}
345
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000346// TODO: Don't worry about 64-bit now, but when this is fixed remove the
347// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000348unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000349 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
350
351 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
353 TII.get(ARM::VMOVRS), MoveReg)
354 .addReg(SrcReg));
355 return MoveReg;
356}
357
358unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000359 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
360
Eric Christopheraa3ace12010-09-09 20:49:25 +0000361 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000363 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000364 .addReg(SrcReg));
365 return MoveReg;
366}
367
Eric Christopher9ed58df2010-09-09 00:19:41 +0000368// For double width floating point we need to materialize two constants
369// (the high and the low) into integer registers then use a move to get
370// the combined constant into an FP reg.
371unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
372 const APFloat Val = CFP->getValueAPF();
373 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000374
Eric Christopher9ed58df2010-09-09 00:19:41 +0000375 // This checks to see if we can use VFP3 instructions to materialize
376 // a constant, otherwise we have to go through the constant pool.
377 if (TLI.isFPImmLegal(Val, VT)) {
378 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
379 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
381 DestReg)
382 .addFPImm(CFP));
383 return DestReg;
384 }
Eric Christopher238bb162010-09-09 23:50:00 +0000385
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000386 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000387 if (!Subtarget->hasVFP2()) return false;
388
389 // MachineConstantPool wants an explicit alignment.
390 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
391 if (Align == 0) {
392 // TODO: Figure out if this is correct.
393 Align = TD.getTypeAllocSize(CFP->getType());
394 }
395 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
396 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
397 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
398
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000399 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
401 DestReg)
402 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000403 .addReg(0));
404 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000405}
406
Eric Christopher744c7c82010-09-28 22:47:54 +0000407unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
408
409 // For now 32-bit only.
410 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
411
Eric Christopher56d2b722010-09-02 23:43:26 +0000412 // MachineConstantPool wants an explicit alignment.
413 unsigned Align = TD.getPrefTypeAlignment(C->getType());
414 if (Align == 0) {
415 // TODO: Figure out if this is correct.
416 Align = TD.getTypeAllocSize(C->getType());
417 }
418 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000419 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000420
Eric Christopher56d2b722010-09-02 23:43:26 +0000421 if (isThumb)
422 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000423 TII.get(ARM::t2LDRpci), DestReg)
424 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000425 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000426 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000428 TII.get(ARM::LDRcp), DestReg)
429 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000430 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000431
Eric Christopher56d2b722010-09-02 23:43:26 +0000432 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000433}
434
Eric Christopher9ed58df2010-09-09 00:19:41 +0000435unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
436 EVT VT = TLI.getValueType(C->getType(), true);
437
438 // Only handle simple types.
439 if (!VT.isSimple()) return 0;
440
441 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
442 return ARMMaterializeFP(CFP, VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000443 return ARMMaterializeInt(C, VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000444}
445
Eric Christopherb1cc8482010-08-25 07:23:49 +0000446bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
447 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000448
Eric Christopherb1cc8482010-08-25 07:23:49 +0000449 // Only handle simple types.
450 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000451
Eric Christopherdc908042010-08-31 01:28:42 +0000452 // Handle all legal types, i.e. a register that will directly hold this
453 // value.
454 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000455}
456
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000457bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
458 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000459
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000460 // If this is a type than can be sign or zero-extended to a basic operation
461 // go ahead and accept it now.
462 if (VT == MVT::i8 || VT == MVT::i16)
463 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000464
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000465 return false;
466}
467
Eric Christophercb0b04b2010-08-24 00:07:24 +0000468// Computes the Reg+Offset to get to an object.
469bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000470 int &Offset) {
471 // Some boilerplate from the X86 FastISel.
472 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000473 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000474 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000475 // Don't walk into other basic blocks; it's possible we haven't
476 // visited them yet, so the instructions may not yet be assigned
477 // virtual registers.
478 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
479 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000480 Opcode = I->getOpcode();
481 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000482 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000483 Opcode = C->getOpcode();
484 U = C;
485 }
486
Eric Christophercb0b04b2010-08-24 00:07:24 +0000487 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000488 if (Ty->getAddressSpace() > 255)
489 // Fast instruction selection doesn't support the special
490 // address spaces.
491 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000492
Eric Christopher83007122010-08-23 21:44:12 +0000493 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000494 default:
Eric Christopher83007122010-08-23 21:44:12 +0000495 break;
496 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000497 assert(false && "Alloca should have been handled earlier!");
498 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000499 }
500 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000501
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000502 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000503 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000504 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000505 return false;
506 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000507
Eric Christophercb0b04b2010-08-24 00:07:24 +0000508 // Try to get this in a register if nothing else has worked.
509 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000510 if (Reg == 0) return false;
511
512 // Since the offset may be too large for the load instruction
513 // get the reg+offset into a register.
514 // TODO: Verify the additions work, otherwise we'll need to add the
515 // offset instead of 0 to the instructions and do all sorts of operand
516 // munging.
517 // TODO: Optimize this somewhat.
518 if (Offset != 0) {
519 ARMCC::CondCodes Pred = ARMCC::AL;
520 unsigned PredReg = 0;
521
Eric Christophereaa204b2010-09-02 01:39:14 +0000522 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000523 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
524 Reg, Reg, Offset, Pred, PredReg,
525 static_cast<const ARMBaseInstrInfo&>(TII));
526 else {
527 assert(AFI->isThumb2Function());
528 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
529 Reg, Reg, Offset, Pred, PredReg,
530 static_cast<const ARMBaseInstrInfo&>(TII));
531 }
532 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000533 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000534}
535
Eric Christopher30b66332010-09-08 21:49:50 +0000536bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000537 Value *Op0 = I->getOperand(0);
538
539 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000540 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
541 DenseMap<const AllocaInst*, int>::iterator SI =
542 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000543
Eric Christophere24d66f2010-08-24 22:07:27 +0000544 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000545 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000546 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000547 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000549 TM.getRegisterInfo());
550 UpdateValueMap(I, ResultReg);
551 return true;
552 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000553 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000554 return false;
555}
556
Eric Christopherb1cc8482010-08-25 07:23:49 +0000557bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
558 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000559
Eric Christopherb1cc8482010-08-25 07:23:49 +0000560 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000561 unsigned Opc;
Eric Christopher6dab1372010-09-18 01:59:37 +0000562 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000563 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000564 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000565 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000566 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000567 case MVT::i16:
568 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
569 VT = MVT::i32;
570 break;
571 case MVT::i8:
572 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
573 VT = MVT::i32;
574 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000575 case MVT::i32:
576 Opc = isThumb ? ARM::tLDR : ARM::LDR;
577 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000578 case MVT::f32:
579 Opc = ARM::VLDRS;
580 isFloat = true;
581 break;
582 case MVT::f64:
583 Opc = ARM::VLDRD;
584 isFloat = true;
585 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000586 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000587
Eric Christopherdc908042010-08-31 01:28:42 +0000588 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000589
Eric Christopherdc908042010-08-31 01:28:42 +0000590 // TODO: Fix the Addressing modes so that these can share some code.
591 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
Eric Christopher6dab1372010-09-18 01:59:37 +0000592 // The thumb addressing mode has operands swapped from the arm addressing
593 // mode, the floating point one only has two operands.
594 if (isFloat)
595 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
596 TII.get(Opc), ResultReg)
597 .addReg(Reg).addImm(Offset));
598 else if (isThumb)
Eric Christopherdc908042010-08-31 01:28:42 +0000599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
600 TII.get(Opc), ResultReg)
601 .addReg(Reg).addImm(Offset).addReg(0));
602 else
603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
604 TII.get(Opc), ResultReg)
605 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000606 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000607}
608
Eric Christopher43b62be2010-09-27 06:02:23 +0000609bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000610 // Verify we have a legal type before going any further.
611 EVT VT;
612 if (!isLoadTypeLegal(I->getType(), VT))
613 return false;
614
615 // If we're an alloca we know we have a frame index and can emit the load
616 // directly in short order.
617 if (ARMLoadAlloca(I, VT))
618 return true;
619
620 // Our register and offset with innocuous defaults.
621 unsigned Reg = 0;
622 int Offset = 0;
623
624 // See if we can handle this as Reg + Offset
625 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
626 return false;
627
628 unsigned ResultReg;
629 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
630
631 UpdateValueMap(I, ResultReg);
632 return true;
633}
634
Eric Christopher30b66332010-09-08 21:49:50 +0000635bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000636 Value *Op1 = I->getOperand(1);
637
638 // Verify it's an alloca.
639 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
640 DenseMap<const AllocaInst*, int>::iterator SI =
641 FuncInfo.StaticAllocaMap.find(AI);
642
643 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000644 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000645 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000646 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000647 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000648 TM.getRegisterInfo());
649 return true;
650 }
651 }
652 return false;
653}
654
Eric Christopher318b6ee2010-09-02 00:53:56 +0000655bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
656 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000657 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000658 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000659 switch (VT.getSimpleVT().SimpleTy) {
660 default: return false;
661 case MVT::i1:
662 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
663 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
664 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000665 case MVT::f32:
666 if (!Subtarget->hasVFP2()) return false;
667 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000668 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000669 break;
670 case MVT::f64:
671 if (!Subtarget->hasVFP2()) return false;
672 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000673 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000674 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000675 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000676
Eric Christopherb74558a2010-09-18 01:23:38 +0000677 // The thumb addressing mode has operands swapped from the arm addressing
678 // mode, the floating point one only has two operands.
Eric Christopher6dab1372010-09-18 01:59:37 +0000679 if (isFloat)
Eric Christopherb74558a2010-09-18 01:23:38 +0000680 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
681 TII.get(StrOpc), SrcReg)
682 .addReg(DstReg).addImm(Offset));
Eric Christopher6dab1372010-09-18 01:59:37 +0000683 else if (isThumb)
684 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
685 TII.get(StrOpc), SrcReg)
686 .addReg(DstReg).addImm(Offset).addReg(0));
687
Eric Christopher318b6ee2010-09-02 00:53:56 +0000688 else
689 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
690 TII.get(StrOpc), SrcReg)
691 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000692
Eric Christopher318b6ee2010-09-02 00:53:56 +0000693 return true;
694}
695
Eric Christopher43b62be2010-09-27 06:02:23 +0000696bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000697 Value *Op0 = I->getOperand(0);
698 unsigned SrcReg = 0;
699
Eric Christopher543cf052010-09-01 22:16:27 +0000700 // Yay type legalization
701 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000702 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000703 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000704
Eric Christopher1b61ef42010-09-02 01:48:11 +0000705 // Get the value to be stored into a register.
706 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000707 if (SrcReg == 0)
708 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000709
Eric Christopher318b6ee2010-09-02 00:53:56 +0000710 // If we're an alloca we know we have a frame index and can emit the store
711 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000712 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000713 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000714
Eric Christopher318b6ee2010-09-02 00:53:56 +0000715 // Our register and offset with innocuous defaults.
716 unsigned Reg = 0;
717 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000718
Eric Christopher318b6ee2010-09-02 00:53:56 +0000719 // See if we can handle this as Reg + Offset
720 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
721 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000722
Eric Christopher318b6ee2010-09-02 00:53:56 +0000723 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000724
Eric Christophera5b1e682010-09-17 22:28:18 +0000725 return true;
726}
727
728static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
729 switch (Pred) {
730 // Needs two compares...
731 case CmpInst::FCMP_ONE:
732 case CmpInst::FCMP_UEQ:
733 default:
734 assert(false && "Unhandled CmpInst::Predicate!");
735 return ARMCC::AL;
736 case CmpInst::ICMP_EQ:
737 case CmpInst::FCMP_OEQ:
738 return ARMCC::EQ;
739 case CmpInst::ICMP_SGT:
740 case CmpInst::FCMP_OGT:
741 return ARMCC::GT;
742 case CmpInst::ICMP_SGE:
743 case CmpInst::FCMP_OGE:
744 return ARMCC::GE;
745 case CmpInst::ICMP_UGT:
746 case CmpInst::FCMP_UGT:
747 return ARMCC::HI;
748 case CmpInst::FCMP_OLT:
749 return ARMCC::MI;
750 case CmpInst::ICMP_ULE:
751 case CmpInst::FCMP_OLE:
752 return ARMCC::LS;
753 case CmpInst::FCMP_ORD:
754 return ARMCC::VC;
755 case CmpInst::FCMP_UNO:
756 return ARMCC::VS;
757 case CmpInst::FCMP_UGE:
758 return ARMCC::PL;
759 case CmpInst::ICMP_SLT:
760 case CmpInst::FCMP_ULT:
761 return ARMCC::LT;
762 case CmpInst::ICMP_SLE:
763 case CmpInst::FCMP_ULE:
764 return ARMCC::LE;
765 case CmpInst::FCMP_UNE:
766 case CmpInst::ICMP_NE:
767 return ARMCC::NE;
768 case CmpInst::ICMP_UGE:
769 return ARMCC::HS;
770 case CmpInst::ICMP_ULT:
771 return ARMCC::LO;
772 }
Eric Christopher543cf052010-09-01 22:16:27 +0000773}
774
Eric Christopher43b62be2010-09-27 06:02:23 +0000775bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000776 const BranchInst *BI = cast<BranchInst>(I);
777 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
778 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000779
Eric Christophere5734102010-09-03 00:35:47 +0000780 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000781 // TODO: Try to avoid the re-computation in some places.
782 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000783 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000784
Eric Christopher229207a2010-09-29 01:14:47 +0000785 // Re-set the flags just in case.
786 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
787 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
788 .addReg(CondReg).addImm(1));
Eric Christophera5b1e682010-09-17 22:28:18 +0000789
Eric Christophere5734102010-09-03 00:35:47 +0000790 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000791 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000792 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000793 FastEmitBranch(FBB, DL);
794 FuncInfo.MBB->addSuccessor(TBB);
Eric Christophera5b1e682010-09-17 22:28:18 +0000795 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000796}
797
Eric Christopher43b62be2010-09-27 06:02:23 +0000798bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000799 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000800
Eric Christopherd43393a2010-09-08 23:13:45 +0000801 EVT VT;
802 const Type *Ty = CI->getOperand(0)->getType();
803 if (!isTypeLegal(Ty, VT))
804 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000805
Eric Christopherd43393a2010-09-08 23:13:45 +0000806 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
807 if (isFloat && !Subtarget->hasVFP2())
808 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000809
Eric Christopherd43393a2010-09-08 23:13:45 +0000810 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000811 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000812 switch (VT.getSimpleVT().SimpleTy) {
813 default: return false;
814 // TODO: Verify compares.
815 case MVT::f32:
816 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000817 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000818 break;
819 case MVT::f64:
820 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000821 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000822 break;
823 case MVT::i32:
824 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000825 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000826 break;
827 }
828
Eric Christopher229207a2010-09-29 01:14:47 +0000829 // Get the compare predicate.
830 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
831
832 // We may not handle every CC for now.
833 if (ARMPred == ARMCC::AL) return false;
834
Eric Christopherd43393a2010-09-08 23:13:45 +0000835 unsigned Arg1 = getRegForValue(CI->getOperand(0));
836 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000837
Eric Christopherd43393a2010-09-08 23:13:45 +0000838 unsigned Arg2 = getRegForValue(CI->getOperand(1));
839 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000840
Eric Christopherd43393a2010-09-08 23:13:45 +0000841 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
842 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000843
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000844 // For floating point we need to move the result to a comparison register
845 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000846 if (isFloat)
847 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
848 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000849
Eric Christopher229207a2010-09-29 01:14:47 +0000850 // Now set a register based on the comparison. Explicitly set the predicates
851 // here.
852 unsigned MovCCOpc = isThumb ? ARM::tMOVCCi : ARM::MOVCCi;
853 unsigned DestReg = createResultReg(ARM::GPRRegisterClass);
854 Constant *Zero
855 = ConstantInt::get(Type::getInt32Ty(I->getType()->getContext()), 0);
856 unsigned ZeroReg = TargetMaterializeConstant(Zero);
857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
858 .addReg(ZeroReg).addImm(1)
859 .addImm(ARMPred).addReg(CondReg);
860
Eric Christophera5b1e682010-09-17 22:28:18 +0000861 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000862 return true;
863}
864
Eric Christopher43b62be2010-09-27 06:02:23 +0000865bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000866 // Make sure we have VFP and that we're extending float to double.
867 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000868
Eric Christopher46203602010-09-09 00:26:48 +0000869 Value *V = I->getOperand(0);
870 if (!I->getType()->isDoubleTy() ||
871 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000872
Eric Christopher46203602010-09-09 00:26:48 +0000873 unsigned Op = getRegForValue(V);
874 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000875
Eric Christopher46203602010-09-09 00:26:48 +0000876 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000877 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000878 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000879 .addReg(Op));
880 UpdateValueMap(I, Result);
881 return true;
882}
883
Eric Christopher43b62be2010-09-27 06:02:23 +0000884bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000885 // Make sure we have VFP and that we're truncating double to float.
886 if (!Subtarget->hasVFP2()) return false;
887
888 Value *V = I->getOperand(0);
889 if (!I->getType()->isFloatTy() ||
890 !V->getType()->isDoubleTy()) return false;
891
892 unsigned Op = getRegForValue(V);
893 if (Op == 0) return false;
894
895 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000896 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000897 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000898 .addReg(Op));
899 UpdateValueMap(I, Result);
900 return true;
901}
902
Eric Christopher43b62be2010-09-27 06:02:23 +0000903bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000904 // Make sure we have VFP.
905 if (!Subtarget->hasVFP2()) return false;
906
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000907 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000908 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000909 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000910 return false;
911
912 unsigned Op = getRegForValue(I->getOperand(0));
913 if (Op == 0) return false;
914
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000915 // The conversion routine works on fp-reg to fp-reg and the operand above
916 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000917 unsigned FP = ARMMoveToFPReg(DstVT, Op);
918 if (FP == 0) return false;
919
Eric Christopher9a040492010-09-09 18:54:59 +0000920 unsigned Opc;
921 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
922 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
923 else return 0;
924
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000925 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000926 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
927 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000928 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000929 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000930 return true;
931}
932
Eric Christopher43b62be2010-09-27 06:02:23 +0000933bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000934 // Make sure we have VFP.
935 if (!Subtarget->hasVFP2()) return false;
936
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000937 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000938 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000939 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000940 return false;
941
942 unsigned Op = getRegForValue(I->getOperand(0));
943 if (Op == 0) return false;
944
945 unsigned Opc;
946 const Type *OpTy = I->getOperand(0)->getType();
947 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
948 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
949 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000950 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000951
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000952 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000953 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
954 ResultReg)
955 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000956
957 // This result needs to be in an integer register, but the conversion only
958 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000959 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000960 if (IntReg == 0) return false;
961
962 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000963 return true;
964}
965
Eric Christopher43b62be2010-09-27 06:02:23 +0000966bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000967 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000968
Eric Christopherbc39b822010-09-09 00:53:57 +0000969 // We can get here in the case when we want to use NEON for our fp
970 // operations, but can't figure out how to. Just use the vfp instructions
971 // if we have them.
972 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000973 const Type *Ty = I->getType();
974 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
975 if (isFloat && !Subtarget->hasVFP2())
976 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000977
Eric Christopherbc39b822010-09-09 00:53:57 +0000978 unsigned Op1 = getRegForValue(I->getOperand(0));
979 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000980
Eric Christopherbc39b822010-09-09 00:53:57 +0000981 unsigned Op2 = getRegForValue(I->getOperand(1));
982 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000983
Eric Christopherbc39b822010-09-09 00:53:57 +0000984 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000985 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
986 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000987 switch (ISDOpcode) {
988 default: return false;
989 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000990 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000991 break;
992 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000993 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000994 break;
995 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000996 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000997 break;
998 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000999 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001000 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1001 TII.get(Opc), ResultReg)
1002 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001003 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001004 return true;
1005}
1006
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001007// Call Handling Code
1008
1009// This is largely taken directly from CCAssignFnForNode - we don't support
1010// varargs in FastISel so that part has been removed.
1011// TODO: We may not support all of this.
1012CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1013 switch (CC) {
1014 default:
1015 llvm_unreachable("Unsupported calling convention");
1016 case CallingConv::C:
1017 case CallingConv::Fast:
1018 // Use target triple & subtarget features to do actual dispatch.
1019 if (Subtarget->isAAPCS_ABI()) {
1020 if (Subtarget->hasVFP2() &&
1021 FloatABIType == FloatABI::Hard)
1022 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1023 else
1024 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1025 } else
1026 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1027 case CallingConv::ARM_AAPCS_VFP:
1028 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1029 case CallingConv::ARM_AAPCS:
1030 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1031 case CallingConv::ARM_APCS:
1032 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1033 }
1034}
1035
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001036// A quick function that will emit a call for a named libcall in F with the
1037// vector of passed arguments for the Instruction in I. We can assume that we
1038// can emit a call for any libcall we can produce. This is an abridged version
1039// of the full call infrastructure since we won't need to worry about things
1040// like computed function pointers or strange arguments at call sites.
1041// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1042// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001043bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1044 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1045
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001046 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001047 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001048 EVT RetVT;
1049 if (RetTy->isVoidTy())
1050 RetVT = MVT::isVoid;
1051 else if (!isTypeLegal(RetTy, RetVT))
1052 return false;
1053
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001054 // For now we're using BLX etc on the assumption that we have v5t ops.
1055 if (!Subtarget->hasV5TOps()) return false;
1056
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001057 // Abridged from the X86 FastISel call selection mechanism
1058 SmallVector<Value*, 8> Args;
1059 SmallVector<unsigned, 8> ArgRegs;
1060 SmallVector<EVT, 8> ArgVTs;
1061 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1062 Args.reserve(I->getNumOperands());
1063 ArgRegs.reserve(I->getNumOperands());
1064 ArgVTs.reserve(I->getNumOperands());
1065 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001066 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001067 Value *Op = I->getOperand(i);
1068 unsigned Arg = getRegForValue(Op);
1069 if (Arg == 0) return false;
1070
1071 const Type *ArgTy = Op->getType();
1072 EVT ArgVT;
1073 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1074
1075 ISD::ArgFlagsTy Flags;
1076 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1077 Flags.setOrigAlign(OriginalAlignment);
1078
1079 Args.push_back(Op);
1080 ArgRegs.push_back(Arg);
1081 ArgVTs.push_back(ArgVT);
1082 ArgFlags.push_back(Flags);
1083 }
1084
1085 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001086 CCState CCInfo(CC, false, TM, ArgLocs,
1087 I->getParent()->getParent()->getContext());
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001088 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1089
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001090 // Get a count of how many bytes are to be pushed on the stack.
1091 unsigned NumBytes = CCInfo.getNextStackOffset();
1092
1093 // Issue CALLSEQ_START
1094 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1096 .addImm(NumBytes);
1097
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001098 // Process the args.
1099 SmallVector<unsigned, 4> RegArgs;
1100 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1101 CCValAssign &VA = ArgLocs[i];
1102 unsigned Arg = ArgRegs[VA.getValNo()];
1103 EVT ArgVT = ArgVTs[VA.getValNo()];
1104
1105 // Should we ever have to promote?
1106 switch (VA.getLocInfo()) {
1107 case CCValAssign::Full: break;
1108 default:
1109 assert(false && "Handle arg promotion for libcalls?");
1110 return false;
1111 }
1112
1113 // Now copy/store arg to correct locations.
1114 if (VA.isRegLoc()) {
1115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001116 VA.getLocReg())
1117 .addReg(Arg);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001118 RegArgs.push_back(VA.getLocReg());
1119 } else {
1120 // Need to store
1121 return false;
1122 }
1123 }
1124
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001125 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1126 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001127 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001128 unsigned CallOpc;
1129 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001130 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001131 else
1132 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001133 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001134 .addExternalSymbol(TLI.getLibcallName(Call));
1135
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001136 // Add implicit physical register uses to the call.
1137 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1138 MIB.addReg(RegArgs[i]);
1139
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001140 // Issue CALLSEQ_END
1141 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1142 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1143 .addImm(NumBytes).addImm(0);
1144
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001145 // Now the return value.
1146 SmallVector<unsigned, 4> UsedRegs;
1147 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1148 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001149 CCState CCInfo(CC, false, TM, RVLocs,
1150 I->getParent()->getParent()->getContext());
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001151 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1152
1153 // Copy all of the result registers out of their specified physreg.
1154 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1155 EVT CopyVT = RVLocs[0].getValVT();
1156 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1157
1158 unsigned ResultReg = createResultReg(DstRC);
1159 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1160 ResultReg).addReg(RVLocs[0].getLocReg());
1161 UsedRegs.push_back(RVLocs[0].getLocReg());
1162
1163 // Finally update the result.
1164 UpdateValueMap(I, ResultReg);
1165 }
1166
1167 // Set all unused physreg defs as dead.
1168 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001169 return true;
1170}
1171
Eric Christopher43b62be2010-09-27 06:02:23 +00001172bool ARMFastISel::SelectSDiv(const Instruction *I) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001173 EVT VT;
1174 const Type *Ty = I->getType();
1175 if (!isTypeLegal(Ty, VT))
1176 return false;
Eric Christopher1127c722010-09-27 06:08:12 +00001177
1178 // If we have integer div support we should have selected this automagically.
1179 // In case we have a real miss go ahead and return false and we'll pick
1180 // it up later.
1181 if (Subtarget->hasDivide()) return false;
1182
1183 // Otherwise emit a libcall.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001184 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1185 if (VT == MVT::i16)
1186 LC = RTLIB::SDIV_I16;
1187 else if (VT == MVT::i32)
1188 LC = RTLIB::SDIV_I32;
1189 else if (VT == MVT::i64)
1190 LC = RTLIB::SDIV_I64;
1191 else if (VT == MVT::i128)
1192 LC = RTLIB::SDIV_I128;
1193 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001194
1195 return ARMEmitLibcall(I, LC);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001196}
1197
Eric Christopher56d2b722010-09-02 23:43:26 +00001198// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001199bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001200 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001201 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001202
Eric Christopherab695882010-07-21 22:26:11 +00001203 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001204 case Instruction::Load:
Eric Christopher43b62be2010-09-27 06:02:23 +00001205 return SelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001206 case Instruction::Store:
Eric Christopher43b62be2010-09-27 06:02:23 +00001207 return SelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001208 case Instruction::Br:
Eric Christopher43b62be2010-09-27 06:02:23 +00001209 return SelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001210 case Instruction::ICmp:
1211 case Instruction::FCmp:
Eric Christopher43b62be2010-09-27 06:02:23 +00001212 return SelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001213 case Instruction::FPExt:
Eric Christopher43b62be2010-09-27 06:02:23 +00001214 return SelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001215 case Instruction::FPTrunc:
Eric Christopher43b62be2010-09-27 06:02:23 +00001216 return SelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001217 case Instruction::SIToFP:
Eric Christopher43b62be2010-09-27 06:02:23 +00001218 return SelectSIToFP(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001219 case Instruction::FPToSI:
Eric Christopher43b62be2010-09-27 06:02:23 +00001220 return SelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001221 case Instruction::FAdd:
Eric Christopher43b62be2010-09-27 06:02:23 +00001222 return SelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001223 case Instruction::FSub:
Eric Christopher43b62be2010-09-27 06:02:23 +00001224 return SelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001225 case Instruction::FMul:
Eric Christopher43b62be2010-09-27 06:02:23 +00001226 return SelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001227 case Instruction::SDiv:
Eric Christopher43b62be2010-09-27 06:02:23 +00001228 return SelectSDiv(I);
Eric Christopherab695882010-07-21 22:26:11 +00001229 default: break;
1230 }
1231 return false;
1232}
1233
1234namespace llvm {
1235 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +00001236 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001237 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001238 }
1239}