blob: a79f299485699ef7cb0e89447bd62860dd51b5cc [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 Christopher8cf6c602010-09-29 22:24:45 +000064 // Convenience variables to avoid some queries.
Eric Christophereaa204b2010-09-02 01:39:14 +000065 bool isThumb;
Eric Christopher8cf6c602010-09-29 22:24:45 +000066 LLVMContext *Context;
Eric Christophereaa204b2010-09-02 01:39:14 +000067
Eric Christopherab695882010-07-21 22:26:11 +000068 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000069 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000070 : FastISel(funcInfo),
71 TM(funcInfo.MF->getTarget()),
72 TII(*TM.getInstrInfo()),
73 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000074 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000075 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000076 isThumb = AFI->isThumbFunction();
Eric Christopher8cf6c602010-09-29 22:24:45 +000077 Context = &funcInfo.Fn->getContext();
Eric Christopherab695882010-07-21 22:26:11 +000078 }
79
Eric Christophercb592292010-08-20 00:20:31 +000080 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000081 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
82 const TargetRegisterClass *RC);
83 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
84 const TargetRegisterClass *RC,
85 unsigned Op0, bool Op0IsKill);
86 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 unsigned Op1, bool Op1IsKill);
90 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
93 uint64_t Imm);
94 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 unsigned Op0, bool Op0IsKill,
97 const ConstantFP *FPImm);
98 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
100 uint64_t Imm);
101 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
103 unsigned Op0, bool Op0IsKill,
104 unsigned Op1, bool Op1IsKill,
105 uint64_t Imm);
106 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
107 unsigned Op0, bool Op0IsKill,
108 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000109
Eric Christophercb592292010-08-20 00:20:31 +0000110 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000111 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000112 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000113
114 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000115
Eric Christopher83007122010-08-23 21:44:12 +0000116 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000117 private:
Eric Christopher43b62be2010-09-27 06:02:23 +0000118 virtual bool SelectLoad(const Instruction *I);
119 virtual bool SelectStore(const Instruction *I);
120 virtual bool SelectBranch(const Instruction *I);
121 virtual bool SelectCmp(const Instruction *I);
122 virtual bool SelectFPExt(const Instruction *I);
123 virtual bool SelectFPTrunc(const Instruction *I);
124 virtual bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
125 virtual bool SelectSIToFP(const Instruction *I);
126 virtual bool SelectFPToSI(const Instruction *I);
127 virtual bool SelectSDiv(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000128
Eric Christopher83007122010-08-23 21:44:12 +0000129 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000130 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000131 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000132 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000133 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000134 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000135 bool ARMLoadAlloca(const Instruction *I, EVT VT);
136 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000137 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000138 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000139 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000140 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000141 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000142
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000143 // Call handling routines.
144 private:
145 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
Eric Christopher7ed8ec92010-09-28 01:21:42 +0000146 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000147
148 // OptionalDef handling routines.
149 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000150 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
151 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
152};
Eric Christopherab695882010-07-21 22:26:11 +0000153
154} // end anonymous namespace
155
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000156#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000157
Eric Christopher456144e2010-08-19 00:37:05 +0000158// DefinesOptionalPredicate - This is different from DefinesPredicate in that
159// we don't care about implicit defs here, just places we'll need to add a
160// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
161bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
162 const TargetInstrDesc &TID = MI->getDesc();
163 if (!TID.hasOptionalDef())
164 return false;
165
166 // Look to see if our OptionalDef is defining CPSR or CCR.
167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000169 if (!MO.isReg() || !MO.isDef()) continue;
170 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000171 *CPSR = true;
172 }
173 return true;
174}
175
176// If the machine is predicable go ahead and add the predicate operands, if
177// it needs default CC operands add those.
178const MachineInstrBuilder &
179ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
180 MachineInstr *MI = &*MIB;
181
182 // Do we use a predicate?
183 if (TII.isPredicable(MI))
184 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000185
Eric Christopher456144e2010-08-19 00:37:05 +0000186 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
187 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000188 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000189 if (DefinesOptionalPredicate(MI, &CPSR)) {
190 if (CPSR)
191 AddDefaultT1CC(MIB);
192 else
193 AddDefaultCC(MIB);
194 }
195 return MIB;
196}
197
Eric Christopher0fe7d542010-08-17 01:25:29 +0000198unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
199 const TargetRegisterClass* RC) {
200 unsigned ResultReg = createResultReg(RC);
201 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
202
Eric Christopher456144e2010-08-19 00:37:05 +0000203 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000204 return ResultReg;
205}
206
207unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
208 const TargetRegisterClass *RC,
209 unsigned Op0, bool Op0IsKill) {
210 unsigned ResultReg = createResultReg(RC);
211 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
212
213 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000215 .addReg(Op0, Op0IsKill * RegState::Kill));
216 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000217 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000219 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000220 TII.get(TargetOpcode::COPY), ResultReg)
221 .addReg(II.ImplicitDefs[0]));
222 }
223 return ResultReg;
224}
225
226unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
227 const TargetRegisterClass *RC,
228 unsigned Op0, bool Op0IsKill,
229 unsigned Op1, bool Op1IsKill) {
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 .addReg(Op1, Op1IsKill * RegState::Kill));
237 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000239 .addReg(Op0, Op0IsKill * RegState::Kill)
240 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000241 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000242 TII.get(TargetOpcode::COPY), ResultReg)
243 .addReg(II.ImplicitDefs[0]));
244 }
245 return ResultReg;
246}
247
248unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
249 const TargetRegisterClass *RC,
250 unsigned Op0, bool Op0IsKill,
251 uint64_t Imm) {
252 unsigned ResultReg = createResultReg(RC);
253 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
254
255 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000256 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000257 .addReg(Op0, Op0IsKill * RegState::Kill)
258 .addImm(Imm));
259 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000261 .addReg(Op0, Op0IsKill * RegState::Kill)
262 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000264 TII.get(TargetOpcode::COPY), ResultReg)
265 .addReg(II.ImplicitDefs[0]));
266 }
267 return ResultReg;
268}
269
270unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
271 const TargetRegisterClass *RC,
272 unsigned Op0, bool Op0IsKill,
273 const ConstantFP *FPImm) {
274 unsigned ResultReg = createResultReg(RC);
275 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
276
277 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 .addReg(Op0, Op0IsKill * RegState::Kill)
280 .addFPImm(FPImm));
281 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000285 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000286 TII.get(TargetOpcode::COPY), ResultReg)
287 .addReg(II.ImplicitDefs[0]));
288 }
289 return ResultReg;
290}
291
292unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
293 const TargetRegisterClass *RC,
294 unsigned Op0, bool Op0IsKill,
295 unsigned Op1, bool Op1IsKill,
296 uint64_t Imm) {
297 unsigned ResultReg = createResultReg(RC);
298 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
299
300 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addReg(Op1, Op1IsKill * RegState::Kill)
304 .addImm(Imm));
305 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 .addReg(Op0, Op0IsKill * RegState::Kill)
308 .addReg(Op1, Op1IsKill * RegState::Kill)
309 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 TII.get(TargetOpcode::COPY), ResultReg)
312 .addReg(II.ImplicitDefs[0]));
313 }
314 return ResultReg;
315}
316
317unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
318 const TargetRegisterClass *RC,
319 uint64_t Imm) {
320 unsigned ResultReg = createResultReg(RC);
321 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000322
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 .addImm(Imm));
326 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000328 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(II.ImplicitDefs[0]));
332 }
333 return ResultReg;
334}
335
336unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
337 unsigned Op0, bool Op0IsKill,
338 uint32_t Idx) {
339 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
340 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
341 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000343 DL, TII.get(TargetOpcode::COPY), ResultReg)
344 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
345 return ResultReg;
346}
347
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000348// TODO: Don't worry about 64-bit now, but when this is fixed remove the
349// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000350unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000351 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
352
353 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
355 TII.get(ARM::VMOVRS), MoveReg)
356 .addReg(SrcReg));
357 return MoveReg;
358}
359
360unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000361 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
362
Eric Christopheraa3ace12010-09-09 20:49:25 +0000363 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
364 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000365 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000366 .addReg(SrcReg));
367 return MoveReg;
368}
369
Eric Christopher9ed58df2010-09-09 00:19:41 +0000370// For double width floating point we need to materialize two constants
371// (the high and the low) into integer registers then use a move to get
372// the combined constant into an FP reg.
373unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
374 const APFloat Val = CFP->getValueAPF();
375 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000376
Eric Christopher9ed58df2010-09-09 00:19:41 +0000377 // This checks to see if we can use VFP3 instructions to materialize
378 // a constant, otherwise we have to go through the constant pool.
379 if (TLI.isFPImmLegal(Val, VT)) {
380 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
381 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
382 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
383 DestReg)
384 .addFPImm(CFP));
385 return DestReg;
386 }
Eric Christopher238bb162010-09-09 23:50:00 +0000387
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000388 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000389 if (!Subtarget->hasVFP2()) return false;
390
391 // MachineConstantPool wants an explicit alignment.
392 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
393 if (Align == 0) {
394 // TODO: Figure out if this is correct.
395 Align = TD.getTypeAllocSize(CFP->getType());
396 }
397 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
398 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
399 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
400
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000401 // The extra reg is for addrmode5.
Eric Christopherf5732c42010-09-28 00:35:09 +0000402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
403 DestReg)
404 .addConstantPoolIndex(Idx)
Eric Christopher238bb162010-09-09 23:50:00 +0000405 .addReg(0));
406 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000407}
408
Eric Christopher744c7c82010-09-28 22:47:54 +0000409unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
410
411 // For now 32-bit only.
412 if (VT.getSimpleVT().SimpleTy != MVT::i32) return false;
413
Eric Christopher56d2b722010-09-02 23:43:26 +0000414 // MachineConstantPool wants an explicit alignment.
415 unsigned Align = TD.getPrefTypeAlignment(C->getType());
416 if (Align == 0) {
417 // TODO: Figure out if this is correct.
418 Align = TD.getTypeAllocSize(C->getType());
419 }
420 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher744c7c82010-09-28 22:47:54 +0000421 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000422
Eric Christopher56d2b722010-09-02 23:43:26 +0000423 if (isThumb)
424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000425 TII.get(ARM::t2LDRpci), DestReg)
426 .addConstantPoolIndex(Idx));
Eric Christopher56d2b722010-09-02 23:43:26 +0000427 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000428 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000429 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopherfd609802010-09-28 21:55:34 +0000430 TII.get(ARM::LDRcp), DestReg)
431 .addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000432 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000433
Eric Christopher56d2b722010-09-02 23:43:26 +0000434 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000435}
436
Eric Christopher9ed58df2010-09-09 00:19:41 +0000437unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
438 EVT VT = TLI.getValueType(C->getType(), true);
439
440 // Only handle simple types.
441 if (!VT.isSimple()) return 0;
442
443 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
444 return ARMMaterializeFP(CFP, VT);
Eric Christopher744c7c82010-09-28 22:47:54 +0000445 return ARMMaterializeInt(C, VT);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000446}
447
Eric Christopherb1cc8482010-08-25 07:23:49 +0000448bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
449 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000450
Eric Christopherb1cc8482010-08-25 07:23:49 +0000451 // Only handle simple types.
452 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000453
Eric Christopherdc908042010-08-31 01:28:42 +0000454 // Handle all legal types, i.e. a register that will directly hold this
455 // value.
456 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000457}
458
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000459bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
460 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000461
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000462 // If this is a type than can be sign or zero-extended to a basic operation
463 // go ahead and accept it now.
464 if (VT == MVT::i8 || VT == MVT::i16)
465 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000466
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000467 return false;
468}
469
Eric Christophercb0b04b2010-08-24 00:07:24 +0000470// Computes the Reg+Offset to get to an object.
471bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000472 int &Offset) {
473 // Some boilerplate from the X86 FastISel.
474 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000475 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000476 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000477 // Don't walk into other basic blocks; it's possible we haven't
478 // visited them yet, so the instructions may not yet be assigned
479 // virtual registers.
480 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
481 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000482 Opcode = I->getOpcode();
483 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000484 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000485 Opcode = C->getOpcode();
486 U = C;
487 }
488
Eric Christophercb0b04b2010-08-24 00:07:24 +0000489 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000490 if (Ty->getAddressSpace() > 255)
491 // Fast instruction selection doesn't support the special
492 // address spaces.
493 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000494
Eric Christopher83007122010-08-23 21:44:12 +0000495 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000496 default:
Eric Christopher83007122010-08-23 21:44:12 +0000497 break;
498 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000499 assert(false && "Alloca should have been handled earlier!");
500 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000501 }
502 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000503
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000504 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000505 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000506 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000507 return false;
508 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000509
Eric Christophercb0b04b2010-08-24 00:07:24 +0000510 // Try to get this in a register if nothing else has worked.
511 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000512 if (Reg == 0) return false;
513
514 // Since the offset may be too large for the load instruction
515 // get the reg+offset into a register.
516 // TODO: Verify the additions work, otherwise we'll need to add the
517 // offset instead of 0 to the instructions and do all sorts of operand
518 // munging.
519 // TODO: Optimize this somewhat.
520 if (Offset != 0) {
521 ARMCC::CondCodes Pred = ARMCC::AL;
522 unsigned PredReg = 0;
523
Eric Christophereaa204b2010-09-02 01:39:14 +0000524 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000525 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
526 Reg, Reg, Offset, Pred, PredReg,
527 static_cast<const ARMBaseInstrInfo&>(TII));
528 else {
529 assert(AFI->isThumb2Function());
530 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
531 Reg, Reg, Offset, Pred, PredReg,
532 static_cast<const ARMBaseInstrInfo&>(TII));
533 }
534 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000535 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000536}
537
Eric Christopher30b66332010-09-08 21:49:50 +0000538bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000539 Value *Op0 = I->getOperand(0);
540
541 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000542 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
543 DenseMap<const AllocaInst*, int>::iterator SI =
544 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000545
Eric Christophere24d66f2010-08-24 22:07:27 +0000546 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000547 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000549 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000551 TM.getRegisterInfo());
552 UpdateValueMap(I, ResultReg);
553 return true;
554 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000555 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000556 return false;
557}
558
Eric Christopherb1cc8482010-08-25 07:23:49 +0000559bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
560 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000561
Eric Christopherb1cc8482010-08-25 07:23:49 +0000562 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000563 unsigned Opc;
Eric Christopher6dab1372010-09-18 01:59:37 +0000564 bool isFloat = false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000565 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000566 default:
Eric Christopher98de5b42010-09-29 00:49:09 +0000567 // This is mostly going to be Neon/vector support.
Eric Christopher548d1bb2010-08-30 23:48:26 +0000568 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000569 case MVT::i16:
570 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
571 VT = MVT::i32;
572 break;
573 case MVT::i8:
574 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
575 VT = MVT::i32;
576 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000577 case MVT::i32:
578 Opc = isThumb ? ARM::tLDR : ARM::LDR;
579 break;
Eric Christopher6dab1372010-09-18 01:59:37 +0000580 case MVT::f32:
581 Opc = ARM::VLDRS;
582 isFloat = true;
583 break;
584 case MVT::f64:
585 Opc = ARM::VLDRD;
586 isFloat = true;
587 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000588 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000589
Eric Christopherdc908042010-08-31 01:28:42 +0000590 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000591
Eric Christopherdc908042010-08-31 01:28:42 +0000592 // TODO: Fix the Addressing modes so that these can share some code.
593 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
Eric Christopher6dab1372010-09-18 01:59:37 +0000594 // The thumb addressing mode has operands swapped from the arm addressing
595 // mode, the floating point one only has two operands.
596 if (isFloat)
597 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
598 TII.get(Opc), ResultReg)
599 .addReg(Reg).addImm(Offset));
600 else if (isThumb)
Eric Christopherdc908042010-08-31 01:28:42 +0000601 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
602 TII.get(Opc), ResultReg)
603 .addReg(Reg).addImm(Offset).addReg(0));
604 else
605 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
606 TII.get(Opc), ResultReg)
607 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000608 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000609}
610
Eric Christopher43b62be2010-09-27 06:02:23 +0000611bool ARMFastISel::SelectLoad(const Instruction *I) {
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000612 // Verify we have a legal type before going any further.
613 EVT VT;
614 if (!isLoadTypeLegal(I->getType(), VT))
615 return false;
616
617 // If we're an alloca we know we have a frame index and can emit the load
618 // directly in short order.
619 if (ARMLoadAlloca(I, VT))
620 return true;
621
622 // Our register and offset with innocuous defaults.
623 unsigned Reg = 0;
624 int Offset = 0;
625
626 // See if we can handle this as Reg + Offset
627 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
628 return false;
629
630 unsigned ResultReg;
631 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
632
633 UpdateValueMap(I, ResultReg);
634 return true;
635}
636
Eric Christopher30b66332010-09-08 21:49:50 +0000637bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000638 Value *Op1 = I->getOperand(1);
639
640 // Verify it's an alloca.
641 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
642 DenseMap<const AllocaInst*, int>::iterator SI =
643 FuncInfo.StaticAllocaMap.find(AI);
644
645 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000646 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000647 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000648 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000649 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000650 TM.getRegisterInfo());
651 return true;
652 }
653 }
654 return false;
655}
656
Eric Christopher318b6ee2010-09-02 00:53:56 +0000657bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
658 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000659 unsigned StrOpc;
Eric Christopherb74558a2010-09-18 01:23:38 +0000660 bool isFloat = false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000661 switch (VT.getSimpleVT().SimpleTy) {
662 default: return false;
663 case MVT::i1:
664 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
665 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
666 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000667 case MVT::f32:
668 if (!Subtarget->hasVFP2()) return false;
669 StrOpc = ARM::VSTRS;
Eric Christopherb74558a2010-09-18 01:23:38 +0000670 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000671 break;
672 case MVT::f64:
673 if (!Subtarget->hasVFP2()) return false;
674 StrOpc = ARM::VSTRD;
Eric Christopherb74558a2010-09-18 01:23:38 +0000675 isFloat = true;
Eric Christopher56d2b722010-09-02 23:43:26 +0000676 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000677 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000678
Eric Christopherb74558a2010-09-18 01:23:38 +0000679 // The thumb addressing mode has operands swapped from the arm addressing
680 // mode, the floating point one only has two operands.
Eric Christopher6dab1372010-09-18 01:59:37 +0000681 if (isFloat)
Eric Christopherb74558a2010-09-18 01:23:38 +0000682 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
683 TII.get(StrOpc), SrcReg)
684 .addReg(DstReg).addImm(Offset));
Eric Christopher6dab1372010-09-18 01:59:37 +0000685 else if (isThumb)
686 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
687 TII.get(StrOpc), SrcReg)
688 .addReg(DstReg).addImm(Offset).addReg(0));
689
Eric Christopher318b6ee2010-09-02 00:53:56 +0000690 else
691 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
692 TII.get(StrOpc), SrcReg)
693 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000694
Eric Christopher318b6ee2010-09-02 00:53:56 +0000695 return true;
696}
697
Eric Christopher43b62be2010-09-27 06:02:23 +0000698bool ARMFastISel::SelectStore(const Instruction *I) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000699 Value *Op0 = I->getOperand(0);
700 unsigned SrcReg = 0;
701
Eric Christopher543cf052010-09-01 22:16:27 +0000702 // Yay type legalization
703 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000704 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000705 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000706
Eric Christopher1b61ef42010-09-02 01:48:11 +0000707 // Get the value to be stored into a register.
708 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000709 if (SrcReg == 0)
710 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000711
Eric Christopher318b6ee2010-09-02 00:53:56 +0000712 // If we're an alloca we know we have a frame index and can emit the store
713 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000714 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000715 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000716
Eric Christopher318b6ee2010-09-02 00:53:56 +0000717 // Our register and offset with innocuous defaults.
718 unsigned Reg = 0;
719 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000720
Eric Christopher318b6ee2010-09-02 00:53:56 +0000721 // See if we can handle this as Reg + Offset
722 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
723 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000724
Eric Christopher318b6ee2010-09-02 00:53:56 +0000725 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000726
Eric Christophera5b1e682010-09-17 22:28:18 +0000727 return true;
728}
729
730static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
731 switch (Pred) {
732 // Needs two compares...
733 case CmpInst::FCMP_ONE:
734 case CmpInst::FCMP_UEQ:
735 default:
736 assert(false && "Unhandled CmpInst::Predicate!");
737 return ARMCC::AL;
738 case CmpInst::ICMP_EQ:
739 case CmpInst::FCMP_OEQ:
740 return ARMCC::EQ;
741 case CmpInst::ICMP_SGT:
742 case CmpInst::FCMP_OGT:
743 return ARMCC::GT;
744 case CmpInst::ICMP_SGE:
745 case CmpInst::FCMP_OGE:
746 return ARMCC::GE;
747 case CmpInst::ICMP_UGT:
748 case CmpInst::FCMP_UGT:
749 return ARMCC::HI;
750 case CmpInst::FCMP_OLT:
751 return ARMCC::MI;
752 case CmpInst::ICMP_ULE:
753 case CmpInst::FCMP_OLE:
754 return ARMCC::LS;
755 case CmpInst::FCMP_ORD:
756 return ARMCC::VC;
757 case CmpInst::FCMP_UNO:
758 return ARMCC::VS;
759 case CmpInst::FCMP_UGE:
760 return ARMCC::PL;
761 case CmpInst::ICMP_SLT:
762 case CmpInst::FCMP_ULT:
763 return ARMCC::LT;
764 case CmpInst::ICMP_SLE:
765 case CmpInst::FCMP_ULE:
766 return ARMCC::LE;
767 case CmpInst::FCMP_UNE:
768 case CmpInst::ICMP_NE:
769 return ARMCC::NE;
770 case CmpInst::ICMP_UGE:
771 return ARMCC::HS;
772 case CmpInst::ICMP_ULT:
773 return ARMCC::LO;
774 }
Eric Christopher543cf052010-09-01 22:16:27 +0000775}
776
Eric Christopher43b62be2010-09-27 06:02:23 +0000777bool ARMFastISel::SelectBranch(const Instruction *I) {
Eric Christophere5734102010-09-03 00:35:47 +0000778 const BranchInst *BI = cast<BranchInst>(I);
779 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
780 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000781
Eric Christophere5734102010-09-03 00:35:47 +0000782 // Simple branch support.
Eric Christopher229207a2010-09-29 01:14:47 +0000783 // TODO: Try to avoid the re-computation in some places.
784 unsigned CondReg = getRegForValue(BI->getCondition());
Eric Christophere5734102010-09-03 00:35:47 +0000785 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000786
Eric Christopher229207a2010-09-29 01:14:47 +0000787 // Re-set the flags just in case.
788 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
789 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
790 .addReg(CondReg).addImm(1));
Eric Christophera5b1e682010-09-17 22:28:18 +0000791
Eric Christophere5734102010-09-03 00:35:47 +0000792 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
Eric Christophere5734102010-09-03 00:35:47 +0000793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
Eric Christopher229207a2010-09-29 01:14:47 +0000794 .addMBB(TBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Eric Christophere5734102010-09-03 00:35:47 +0000795 FastEmitBranch(FBB, DL);
796 FuncInfo.MBB->addSuccessor(TBB);
Eric Christophera5b1e682010-09-17 22:28:18 +0000797 return true;
Eric Christophere5734102010-09-03 00:35:47 +0000798}
799
Eric Christopher43b62be2010-09-27 06:02:23 +0000800bool ARMFastISel::SelectCmp(const Instruction *I) {
Eric Christopherd43393a2010-09-08 23:13:45 +0000801 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000802
Eric Christopherd43393a2010-09-08 23:13:45 +0000803 EVT VT;
804 const Type *Ty = CI->getOperand(0)->getType();
805 if (!isTypeLegal(Ty, VT))
806 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000807
Eric Christopherd43393a2010-09-08 23:13:45 +0000808 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
809 if (isFloat && !Subtarget->hasVFP2())
810 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000811
Eric Christopherd43393a2010-09-08 23:13:45 +0000812 unsigned CmpOpc;
Eric Christopher229207a2010-09-29 01:14:47 +0000813 unsigned CondReg;
Eric Christopherd43393a2010-09-08 23:13:45 +0000814 switch (VT.getSimpleVT().SimpleTy) {
815 default: return false;
816 // TODO: Verify compares.
817 case MVT::f32:
818 CmpOpc = ARM::VCMPES;
Eric Christopher229207a2010-09-29 01:14:47 +0000819 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000820 break;
821 case MVT::f64:
822 CmpOpc = ARM::VCMPED;
Eric Christopher229207a2010-09-29 01:14:47 +0000823 CondReg = ARM::FPSCR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000824 break;
825 case MVT::i32:
826 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
Eric Christopher229207a2010-09-29 01:14:47 +0000827 CondReg = ARM::CPSR;
Eric Christopherd43393a2010-09-08 23:13:45 +0000828 break;
829 }
830
Eric Christopher229207a2010-09-29 01:14:47 +0000831 // Get the compare predicate.
832 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
833
834 // We may not handle every CC for now.
835 if (ARMPred == ARMCC::AL) return false;
836
Eric Christopherd43393a2010-09-08 23:13:45 +0000837 unsigned Arg1 = getRegForValue(CI->getOperand(0));
838 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000839
Eric Christopherd43393a2010-09-08 23:13:45 +0000840 unsigned Arg2 = getRegForValue(CI->getOperand(1));
841 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000842
Eric Christopherd43393a2010-09-08 23:13:45 +0000843 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
844 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000845
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000846 // For floating point we need to move the result to a comparison register
847 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000848 if (isFloat)
849 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
850 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000851
Eric Christopher229207a2010-09-29 01:14:47 +0000852 // Now set a register based on the comparison. Explicitly set the predicates
853 // here.
854 unsigned MovCCOpc = isThumb ? ARM::tMOVCCi : ARM::MOVCCi;
855 unsigned DestReg = createResultReg(ARM::GPRRegisterClass);
856 Constant *Zero
Eric Christopher8cf6c602010-09-29 22:24:45 +0000857 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
Eric Christopher229207a2010-09-29 01:14:47 +0000858 unsigned ZeroReg = TargetMaterializeConstant(Zero);
859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
860 .addReg(ZeroReg).addImm(1)
861 .addImm(ARMPred).addReg(CondReg);
862
Eric Christophera5b1e682010-09-17 22:28:18 +0000863 UpdateValueMap(I, DestReg);
Eric Christopherd43393a2010-09-08 23:13:45 +0000864 return true;
865}
866
Eric Christopher43b62be2010-09-27 06:02:23 +0000867bool ARMFastISel::SelectFPExt(const Instruction *I) {
Eric Christopher46203602010-09-09 00:26:48 +0000868 // Make sure we have VFP and that we're extending float to double.
869 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000870
Eric Christopher46203602010-09-09 00:26:48 +0000871 Value *V = I->getOperand(0);
872 if (!I->getType()->isDoubleTy() ||
873 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000874
Eric Christopher46203602010-09-09 00:26:48 +0000875 unsigned Op = getRegForValue(V);
876 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000877
Eric Christopher46203602010-09-09 00:26:48 +0000878 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000879 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000880 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000881 .addReg(Op));
882 UpdateValueMap(I, Result);
883 return true;
884}
885
Eric Christopher43b62be2010-09-27 06:02:23 +0000886bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
Eric Christopherce07b542010-09-09 20:26:31 +0000887 // Make sure we have VFP and that we're truncating double to float.
888 if (!Subtarget->hasVFP2()) return false;
889
890 Value *V = I->getOperand(0);
891 if (!I->getType()->isFloatTy() ||
892 !V->getType()->isDoubleTy()) return false;
893
894 unsigned Op = getRegForValue(V);
895 if (Op == 0) return false;
896
897 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000898 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000899 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000900 .addReg(Op));
901 UpdateValueMap(I, Result);
902 return true;
903}
904
Eric Christopher43b62be2010-09-27 06:02:23 +0000905bool ARMFastISel::SelectSIToFP(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000906 // Make sure we have VFP.
907 if (!Subtarget->hasVFP2()) return false;
908
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000909 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000910 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000911 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000912 return false;
913
914 unsigned Op = getRegForValue(I->getOperand(0));
915 if (Op == 0) return false;
916
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000917 // The conversion routine works on fp-reg to fp-reg and the operand above
918 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000919 unsigned FP = ARMMoveToFPReg(DstVT, Op);
920 if (FP == 0) return false;
921
Eric Christopher9a040492010-09-09 18:54:59 +0000922 unsigned Opc;
923 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
924 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
925 else return 0;
926
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000927 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000928 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
929 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000930 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000931 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000932 return true;
933}
934
Eric Christopher43b62be2010-09-27 06:02:23 +0000935bool ARMFastISel::SelectFPToSI(const Instruction *I) {
Eric Christopher9a040492010-09-09 18:54:59 +0000936 // Make sure we have VFP.
937 if (!Subtarget->hasVFP2()) return false;
938
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000939 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000940 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000941 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000942 return false;
943
944 unsigned Op = getRegForValue(I->getOperand(0));
945 if (Op == 0) return false;
946
947 unsigned Opc;
948 const Type *OpTy = I->getOperand(0)->getType();
949 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
950 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
951 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000952 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000953
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000954 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000955 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
956 ResultReg)
957 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000958
959 // This result needs to be in an integer register, but the conversion only
960 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000961 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000962 if (IntReg == 0) return false;
963
964 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000965 return true;
966}
967
Eric Christopher43b62be2010-09-27 06:02:23 +0000968bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000969 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000970
Eric Christopherbc39b822010-09-09 00:53:57 +0000971 // We can get here in the case when we want to use NEON for our fp
972 // operations, but can't figure out how to. Just use the vfp instructions
973 // if we have them.
974 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000975 const Type *Ty = I->getType();
976 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
977 if (isFloat && !Subtarget->hasVFP2())
978 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000979
Eric Christopherbc39b822010-09-09 00:53:57 +0000980 unsigned Op1 = getRegForValue(I->getOperand(0));
981 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000982
Eric Christopherbc39b822010-09-09 00:53:57 +0000983 unsigned Op2 = getRegForValue(I->getOperand(1));
984 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000985
Eric Christopherbc39b822010-09-09 00:53:57 +0000986 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000987 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
988 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000989 switch (ISDOpcode) {
990 default: return false;
991 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000992 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000993 break;
994 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000995 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000996 break;
997 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000998 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000999 break;
1000 }
Eric Christopherbd6bf082010-09-09 01:02:03 +00001001 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +00001002 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1003 TII.get(Opc), ResultReg)
1004 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +00001005 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +00001006 return true;
1007}
1008
Eric Christopherd10cd7b2010-09-10 23:18:12 +00001009// Call Handling Code
1010
1011// This is largely taken directly from CCAssignFnForNode - we don't support
1012// varargs in FastISel so that part has been removed.
1013// TODO: We may not support all of this.
1014CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1015 switch (CC) {
1016 default:
1017 llvm_unreachable("Unsupported calling convention");
1018 case CallingConv::C:
1019 case CallingConv::Fast:
1020 // Use target triple & subtarget features to do actual dispatch.
1021 if (Subtarget->isAAPCS_ABI()) {
1022 if (Subtarget->hasVFP2() &&
1023 FloatABIType == FloatABI::Hard)
1024 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1025 else
1026 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1027 } else
1028 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1029 case CallingConv::ARM_AAPCS_VFP:
1030 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1031 case CallingConv::ARM_AAPCS:
1032 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1033 case CallingConv::ARM_APCS:
1034 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1035 }
1036}
1037
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001038// A quick function that will emit a call for a named libcall in F with the
1039// vector of passed arguments for the Instruction in I. We can assume that we
1040// can emit a call for any libcall we can produce. This is an abridged version
1041// of the full call infrastructure since we won't need to worry about things
1042// like computed function pointers or strange arguments at call sites.
1043// TODO: Try to unify this and the normal call bits for ARM, then try to unify
1044// with X86.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001045bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1046 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1047
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001048 // Handle *simple* calls for now.
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001049 const Type *RetTy = I->getType();
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001050 EVT RetVT;
1051 if (RetTy->isVoidTy())
1052 RetVT = MVT::isVoid;
1053 else if (!isTypeLegal(RetTy, RetVT))
1054 return false;
1055
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001056 // For now we're using BLX etc on the assumption that we have v5t ops.
1057 if (!Subtarget->hasV5TOps()) return false;
1058
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001059 // Abridged from the X86 FastISel call selection mechanism
1060 SmallVector<Value*, 8> Args;
1061 SmallVector<unsigned, 8> ArgRegs;
1062 SmallVector<EVT, 8> ArgVTs;
1063 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1064 Args.reserve(I->getNumOperands());
1065 ArgRegs.reserve(I->getNumOperands());
1066 ArgVTs.reserve(I->getNumOperands());
1067 ArgFlags.reserve(I->getNumOperands());
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001068 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001069 Value *Op = I->getOperand(i);
1070 unsigned Arg = getRegForValue(Op);
1071 if (Arg == 0) return false;
1072
1073 const Type *ArgTy = Op->getType();
1074 EVT ArgVT;
1075 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1076
1077 ISD::ArgFlagsTy Flags;
1078 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1079 Flags.setOrigAlign(OriginalAlignment);
1080
1081 Args.push_back(Op);
1082 ArgRegs.push_back(Arg);
1083 ArgVTs.push_back(ArgVT);
1084 ArgFlags.push_back(Flags);
1085 }
1086
1087 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher8cf6c602010-09-29 22:24:45 +00001088 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001089 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1090
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001091 // Get a count of how many bytes are to be pushed on the stack.
1092 unsigned NumBytes = CCInfo.getNextStackOffset();
1093
1094 // Issue CALLSEQ_START
1095 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1096 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1097 .addImm(NumBytes);
1098
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001099 // Process the args.
1100 SmallVector<unsigned, 4> RegArgs;
1101 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1102 CCValAssign &VA = ArgLocs[i];
1103 unsigned Arg = ArgRegs[VA.getValNo()];
1104 EVT ArgVT = ArgVTs[VA.getValNo()];
1105
1106 // Should we ever have to promote?
1107 switch (VA.getLocInfo()) {
1108 case CCValAssign::Full: break;
1109 default:
1110 assert(false && "Handle arg promotion for libcalls?");
1111 return false;
1112 }
1113
1114 // Now copy/store arg to correct locations.
1115 if (VA.isRegLoc()) {
1116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001117 VA.getLocReg())
1118 .addReg(Arg);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001119 RegArgs.push_back(VA.getLocReg());
1120 } else {
1121 // Need to store
1122 return false;
1123 }
1124 }
1125
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001126 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1127 // TODO: Turn this into the table of arm call ops.
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001128 MachineInstrBuilder MIB;
Eric Christopherc1095562010-09-18 02:32:38 +00001129 unsigned CallOpc;
1130 if(isThumb)
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001131 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
Eric Christopherc1095562010-09-18 02:32:38 +00001132 else
1133 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001134 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001135 .addExternalSymbol(TLI.getLibcallName(Call));
1136
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001137 // Add implicit physical register uses to the call.
1138 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1139 MIB.addReg(RegArgs[i]);
1140
Eric Christopher7ed8ec92010-09-28 01:21:42 +00001141 // Issue CALLSEQ_END
1142 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1144 .addImm(NumBytes).addImm(0);
1145
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001146 // Now the return value.
1147 SmallVector<unsigned, 4> UsedRegs;
1148 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1149 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher8cf6c602010-09-29 22:24:45 +00001150 CCState CCInfo(CC, false, TM, RVLocs, *Context);
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}