blob: 54433b19a0b1451d314bf0a9deba8b71f14c5578 [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 Christopher83007122010-08-23 21:44:12 +0000116 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000117 virtual bool ARMSelectStore(const Instruction *I);
Eric Christophere5734102010-09-03 00:35:47 +0000118 virtual bool ARMSelectBranch(const Instruction *I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000119 virtual bool ARMSelectCmp(const Instruction *I);
Eric Christopher46203602010-09-09 00:26:48 +0000120 virtual bool ARMSelectFPExt(const Instruction *I);
Eric Christopherce07b542010-09-09 20:26:31 +0000121 virtual bool ARMSelectFPTrunc(const Instruction *I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000122 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopher9a040492010-09-09 18:54:59 +0000123 virtual bool ARMSelectSIToFP(const Instruction *I);
124 virtual bool ARMSelectFPToSI(const Instruction *I);
Eric Christopherbb3e5da2010-09-14 23:03:37 +0000125 virtual bool ARMSelectSDiv(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);
137 unsigned ARMMaterializeInt(const Constant *C);
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 Christopherbb3e5da2010-09-14 23:03:37 +0000144 bool ARMEmitLibcall(const Instruction *I, Function *F);
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 Christopher238bb162010-09-09 23:50:00 +0000400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
401 .addReg(DestReg).addConstantPoolIndex(Idx)
402 .addReg(0));
403 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000404}
405
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000406// TODO: Verify 64-bit.
Eric Christopher9ed58df2010-09-09 00:19:41 +0000407unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000408 // MachineConstantPool wants an explicit alignment.
409 unsigned Align = TD.getPrefTypeAlignment(C->getType());
410 if (Align == 0) {
411 // TODO: Figure out if this is correct.
412 Align = TD.getTypeAllocSize(C->getType());
413 }
414 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher845c5752010-09-08 18:56:34 +0000415 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000416
Eric Christopher56d2b722010-09-02 23:43:26 +0000417 if (isThumb)
418 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
419 TII.get(ARM::t2LDRpci))
420 .addReg(DestReg).addConstantPoolIndex(Idx));
421 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000422 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
424 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000425 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000426 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000427
Eric Christopher56d2b722010-09-02 23:43:26 +0000428 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000429}
430
Eric Christopher9ed58df2010-09-09 00:19:41 +0000431unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
432 EVT VT = TLI.getValueType(C->getType(), true);
433
434 // Only handle simple types.
435 if (!VT.isSimple()) return 0;
436
437 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
438 return ARMMaterializeFP(CFP, VT);
439 return ARMMaterializeInt(C);
440}
441
Eric Christopherb1cc8482010-08-25 07:23:49 +0000442bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
443 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000444
Eric Christopherb1cc8482010-08-25 07:23:49 +0000445 // Only handle simple types.
446 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000447
Eric Christopherdc908042010-08-31 01:28:42 +0000448 // Handle all legal types, i.e. a register that will directly hold this
449 // value.
450 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000451}
452
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000453bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
454 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000455
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000456 // If this is a type than can be sign or zero-extended to a basic operation
457 // go ahead and accept it now.
458 if (VT == MVT::i8 || VT == MVT::i16)
459 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000460
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000461 return false;
462}
463
Eric Christophercb0b04b2010-08-24 00:07:24 +0000464// Computes the Reg+Offset to get to an object.
465bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000466 int &Offset) {
467 // Some boilerplate from the X86 FastISel.
468 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000469 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000470 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000471 // Don't walk into other basic blocks; it's possible we haven't
472 // visited them yet, so the instructions may not yet be assigned
473 // virtual registers.
474 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
475 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000476 Opcode = I->getOpcode();
477 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000478 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000479 Opcode = C->getOpcode();
480 U = C;
481 }
482
Eric Christophercb0b04b2010-08-24 00:07:24 +0000483 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000484 if (Ty->getAddressSpace() > 255)
485 // Fast instruction selection doesn't support the special
486 // address spaces.
487 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000488
Eric Christopher83007122010-08-23 21:44:12 +0000489 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000490 default:
Eric Christopher83007122010-08-23 21:44:12 +0000491 break;
492 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000493 assert(false && "Alloca should have been handled earlier!");
494 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000495 }
496 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000497
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000498 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000499 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000500 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000501 return false;
502 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000503
Eric Christophercb0b04b2010-08-24 00:07:24 +0000504 // Try to get this in a register if nothing else has worked.
505 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000506 if (Reg == 0) return false;
507
508 // Since the offset may be too large for the load instruction
509 // get the reg+offset into a register.
510 // TODO: Verify the additions work, otherwise we'll need to add the
511 // offset instead of 0 to the instructions and do all sorts of operand
512 // munging.
513 // TODO: Optimize this somewhat.
514 if (Offset != 0) {
515 ARMCC::CondCodes Pred = ARMCC::AL;
516 unsigned PredReg = 0;
517
Eric Christophereaa204b2010-09-02 01:39:14 +0000518 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000519 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
520 Reg, Reg, Offset, Pred, PredReg,
521 static_cast<const ARMBaseInstrInfo&>(TII));
522 else {
523 assert(AFI->isThumb2Function());
524 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
525 Reg, Reg, Offset, Pred, PredReg,
526 static_cast<const ARMBaseInstrInfo&>(TII));
527 }
528 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000529 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000530}
531
Eric Christopher30b66332010-09-08 21:49:50 +0000532bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000533 Value *Op0 = I->getOperand(0);
534
535 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000536 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
537 DenseMap<const AllocaInst*, int>::iterator SI =
538 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000539
Eric Christophere24d66f2010-08-24 22:07:27 +0000540 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000541 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000542 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000543 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000544 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000545 TM.getRegisterInfo());
546 UpdateValueMap(I, ResultReg);
547 return true;
548 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000549 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000550 return false;
551}
552
Eric Christopherb1cc8482010-08-25 07:23:49 +0000553bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
554 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000555
Eric Christopherb1cc8482010-08-25 07:23:49 +0000556 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000557 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000558
Eric Christopherb1cc8482010-08-25 07:23:49 +0000559 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000560 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000561 assert(false && "Trying to emit for an unhandled type!");
562 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000563 case MVT::i16:
564 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
565 VT = MVT::i32;
566 break;
567 case MVT::i8:
568 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
569 VT = MVT::i32;
570 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000571 case MVT::i32:
572 Opc = isThumb ? ARM::tLDR : ARM::LDR;
573 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000574 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000575
Eric Christopherdc908042010-08-31 01:28:42 +0000576 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000577
Eric Christopherdc908042010-08-31 01:28:42 +0000578 // TODO: Fix the Addressing modes so that these can share some code.
579 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
580 if (isThumb)
581 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
582 TII.get(Opc), ResultReg)
583 .addReg(Reg).addImm(Offset).addReg(0));
584 else
585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
586 TII.get(Opc), ResultReg)
587 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000588 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000589}
590
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000591bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
592 // Verify we have a legal type before going any further.
593 EVT VT;
594 if (!isLoadTypeLegal(I->getType(), VT))
595 return false;
596
597 // If we're an alloca we know we have a frame index and can emit the load
598 // directly in short order.
599 if (ARMLoadAlloca(I, VT))
600 return true;
601
602 // Our register and offset with innocuous defaults.
603 unsigned Reg = 0;
604 int Offset = 0;
605
606 // See if we can handle this as Reg + Offset
607 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
608 return false;
609
610 unsigned ResultReg;
611 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
612
613 UpdateValueMap(I, ResultReg);
614 return true;
615}
616
Eric Christopher30b66332010-09-08 21:49:50 +0000617bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000618 Value *Op1 = I->getOperand(1);
619
620 // Verify it's an alloca.
621 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
622 DenseMap<const AllocaInst*, int>::iterator SI =
623 FuncInfo.StaticAllocaMap.find(AI);
624
625 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000626 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000627 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000628 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000629 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000630 TM.getRegisterInfo());
631 return true;
632 }
633 }
634 return false;
635}
636
Eric Christopher318b6ee2010-09-02 00:53:56 +0000637bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
638 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000639 unsigned StrOpc;
640 switch (VT.getSimpleVT().SimpleTy) {
641 default: return false;
642 case MVT::i1:
643 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
644 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
645 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000646 case MVT::f32:
647 if (!Subtarget->hasVFP2()) return false;
648 StrOpc = ARM::VSTRS;
649 break;
650 case MVT::f64:
651 if (!Subtarget->hasVFP2()) return false;
652 StrOpc = ARM::VSTRD;
653 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000654 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000655
Eric Christopher318b6ee2010-09-02 00:53:56 +0000656 if (isThumb)
657 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
658 TII.get(StrOpc), SrcReg)
659 .addReg(DstReg).addImm(Offset).addReg(0));
660 else
661 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
662 TII.get(StrOpc), SrcReg)
663 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000664
Eric Christopher318b6ee2010-09-02 00:53:56 +0000665 return true;
666}
667
668bool ARMFastISel::ARMSelectStore(const Instruction *I) {
669 Value *Op0 = I->getOperand(0);
670 unsigned SrcReg = 0;
671
Eric Christopher543cf052010-09-01 22:16:27 +0000672 // Yay type legalization
673 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000674 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000675 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000676
Eric Christopher1b61ef42010-09-02 01:48:11 +0000677 // Get the value to be stored into a register.
678 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000679 if (SrcReg == 0)
680 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000681
Eric Christopher318b6ee2010-09-02 00:53:56 +0000682 // If we're an alloca we know we have a frame index and can emit the store
683 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000684 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000685 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000686
Eric Christopher318b6ee2010-09-02 00:53:56 +0000687 // Our register and offset with innocuous defaults.
688 unsigned Reg = 0;
689 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000690
Eric Christopher318b6ee2010-09-02 00:53:56 +0000691 // See if we can handle this as Reg + Offset
692 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
693 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000694
Eric Christopher318b6ee2010-09-02 00:53:56 +0000695 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000696
Eric Christopher543cf052010-09-01 22:16:27 +0000697 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000698}
699
Eric Christophere5734102010-09-03 00:35:47 +0000700bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
701 const BranchInst *BI = cast<BranchInst>(I);
702 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
703 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000704
Eric Christophere5734102010-09-03 00:35:47 +0000705 // Simple branch support.
706 unsigned CondReg = getRegForValue(BI->getCondition());
707 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000708
Eric Christophere5734102010-09-03 00:35:47 +0000709 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
710 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
711 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
712 .addReg(CondReg).addReg(CondReg));
713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
714 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
715 FastEmitBranch(FBB, DL);
716 FuncInfo.MBB->addSuccessor(TBB);
717 return true;
718}
719
Eric Christopherd43393a2010-09-08 23:13:45 +0000720bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
721 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000722
Eric Christopherd43393a2010-09-08 23:13:45 +0000723 EVT VT;
724 const Type *Ty = CI->getOperand(0)->getType();
725 if (!isTypeLegal(Ty, VT))
726 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000727
Eric Christopherd43393a2010-09-08 23:13:45 +0000728 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
729 if (isFloat && !Subtarget->hasVFP2())
730 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000731
Eric Christopherd43393a2010-09-08 23:13:45 +0000732 unsigned CmpOpc;
733 switch (VT.getSimpleVT().SimpleTy) {
734 default: return false;
735 // TODO: Verify compares.
736 case MVT::f32:
737 CmpOpc = ARM::VCMPES;
738 break;
739 case MVT::f64:
740 CmpOpc = ARM::VCMPED;
741 break;
742 case MVT::i32:
743 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
744 break;
745 }
746
747 unsigned Arg1 = getRegForValue(CI->getOperand(0));
748 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000749
Eric Christopherd43393a2010-09-08 23:13:45 +0000750 unsigned Arg2 = getRegForValue(CI->getOperand(1));
751 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000752
Eric Christopherd43393a2010-09-08 23:13:45 +0000753 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
754 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000755
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000756 // For floating point we need to move the result to a comparison register
757 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000758 if (isFloat)
759 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
760 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000761
762 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000763 return true;
764}
765
Eric Christopher46203602010-09-09 00:26:48 +0000766bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
767 // Make sure we have VFP and that we're extending float to double.
768 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000769
Eric Christopher46203602010-09-09 00:26:48 +0000770 Value *V = I->getOperand(0);
771 if (!I->getType()->isDoubleTy() ||
772 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000773
Eric Christopher46203602010-09-09 00:26:48 +0000774 unsigned Op = getRegForValue(V);
775 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000776
Eric Christopher46203602010-09-09 00:26:48 +0000777 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000778 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000779 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000780 .addReg(Op));
781 UpdateValueMap(I, Result);
782 return true;
783}
784
785bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
786 // Make sure we have VFP and that we're truncating double to float.
787 if (!Subtarget->hasVFP2()) return false;
788
789 Value *V = I->getOperand(0);
790 if (!I->getType()->isFloatTy() ||
791 !V->getType()->isDoubleTy()) return false;
792
793 unsigned Op = getRegForValue(V);
794 if (Op == 0) return false;
795
796 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000797 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000798 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000799 .addReg(Op));
800 UpdateValueMap(I, Result);
801 return true;
802}
803
Eric Christopher9a040492010-09-09 18:54:59 +0000804bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
805 // Make sure we have VFP.
806 if (!Subtarget->hasVFP2()) return false;
807
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000808 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000809 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000810 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000811 return false;
812
813 unsigned Op = getRegForValue(I->getOperand(0));
814 if (Op == 0) return false;
815
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000816 // The conversion routine works on fp-reg to fp-reg and the operand above
817 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000818 unsigned FP = ARMMoveToFPReg(DstVT, Op);
819 if (FP == 0) return false;
820
Eric Christopher9a040492010-09-09 18:54:59 +0000821 unsigned Opc;
822 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
823 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
824 else return 0;
825
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000826 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000827 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
828 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000829 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000830 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000831 return true;
832}
833
834bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
835 // Make sure we have VFP.
836 if (!Subtarget->hasVFP2()) return false;
837
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000838 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000839 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000840 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000841 return false;
842
843 unsigned Op = getRegForValue(I->getOperand(0));
844 if (Op == 0) return false;
845
846 unsigned Opc;
847 const Type *OpTy = I->getOperand(0)->getType();
848 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
849 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
850 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000851 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000852
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000853 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000854 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
855 ResultReg)
856 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000857
858 // This result needs to be in an integer register, but the conversion only
859 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000860 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000861 if (IntReg == 0) return false;
862
863 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000864 return true;
865}
866
Eric Christopherbc39b822010-09-09 00:53:57 +0000867bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000868 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000869
Eric Christopherbc39b822010-09-09 00:53:57 +0000870 // We can get here in the case when we want to use NEON for our fp
871 // operations, but can't figure out how to. Just use the vfp instructions
872 // if we have them.
873 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000874 const Type *Ty = I->getType();
875 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
876 if (isFloat && !Subtarget->hasVFP2())
877 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000878
Eric Christopherbc39b822010-09-09 00:53:57 +0000879 unsigned Op1 = getRegForValue(I->getOperand(0));
880 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000881
Eric Christopherbc39b822010-09-09 00:53:57 +0000882 unsigned Op2 = getRegForValue(I->getOperand(1));
883 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000884
Eric Christopherbc39b822010-09-09 00:53:57 +0000885 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000886 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
887 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000888 switch (ISDOpcode) {
889 default: return false;
890 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000891 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000892 break;
893 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000894 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000895 break;
896 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000897 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000898 break;
899 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000900 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000901 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
902 TII.get(Opc), ResultReg)
903 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000904 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000905 return true;
906}
907
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000908// Call Handling Code
909
910// This is largely taken directly from CCAssignFnForNode - we don't support
911// varargs in FastISel so that part has been removed.
912// TODO: We may not support all of this.
913CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
914 switch (CC) {
915 default:
916 llvm_unreachable("Unsupported calling convention");
917 case CallingConv::C:
918 case CallingConv::Fast:
919 // Use target triple & subtarget features to do actual dispatch.
920 if (Subtarget->isAAPCS_ABI()) {
921 if (Subtarget->hasVFP2() &&
922 FloatABIType == FloatABI::Hard)
923 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
924 else
925 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
926 } else
927 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
928 case CallingConv::ARM_AAPCS_VFP:
929 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
930 case CallingConv::ARM_AAPCS:
931 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
932 case CallingConv::ARM_APCS:
933 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
934 }
935}
936
Eric Christopherbb3e5da2010-09-14 23:03:37 +0000937// A quick function that will emit a call for a named libcall in F with the
938// vector of passed arguments for the Instruction in I. We can assume that we
939// can emit a call for any libcall we can produce. This is an abridged version
940// of the full call infrastructure since we won't need to worry about things
941// like computed function pointers or strange arguments at call sites.
942// TODO: Try to unify this and the normal call bits for ARM, then try to unify
943// with X86.
944bool ARMFastISel::ARMEmitLibcall(const Instruction *I, Function *F) {
945 CallingConv::ID CC = F->getCallingConv();
946
947 // Handle *simple* calls for now.
948 const Type *RetTy = F->getReturnType();
949 EVT RetVT;
950 if (RetTy->isVoidTy())
951 RetVT = MVT::isVoid;
952 else if (!isTypeLegal(RetTy, RetVT))
953 return false;
954
955 assert(!F->isVarArg() && "Vararg libcall?!");
956
957 // Abridged from the X86 FastISel call selection mechanism
958 SmallVector<Value*, 8> Args;
959 SmallVector<unsigned, 8> ArgRegs;
960 SmallVector<EVT, 8> ArgVTs;
961 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
962 Args.reserve(I->getNumOperands());
963 ArgRegs.reserve(I->getNumOperands());
964 ArgVTs.reserve(I->getNumOperands());
965 ArgFlags.reserve(I->getNumOperands());
966 for (unsigned i = 0; i < Args.size(); ++i) {
967 Value *Op = I->getOperand(i);
968 unsigned Arg = getRegForValue(Op);
969 if (Arg == 0) return false;
970
971 const Type *ArgTy = Op->getType();
972 EVT ArgVT;
973 if (!isTypeLegal(ArgTy, ArgVT)) return false;
974
975 ISD::ArgFlagsTy Flags;
976 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
977 Flags.setOrigAlign(OriginalAlignment);
978
979 Args.push_back(Op);
980 ArgRegs.push_back(Arg);
981 ArgVTs.push_back(ArgVT);
982 ArgFlags.push_back(Flags);
983 }
984
985 SmallVector<CCValAssign, 16> ArgLocs;
986 CCState CCInfo(CC, false, TM, ArgLocs, F->getContext());
987 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
988
989 // Process the args.
990 SmallVector<unsigned, 4> RegArgs;
991 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
992 CCValAssign &VA = ArgLocs[i];
993 unsigned Arg = ArgRegs[VA.getValNo()];
994 EVT ArgVT = ArgVTs[VA.getValNo()];
995
996 // Should we ever have to promote?
997 switch (VA.getLocInfo()) {
998 case CCValAssign::Full: break;
999 default:
1000 assert(false && "Handle arg promotion for libcalls?");
1001 return false;
1002 }
1003
1004 // Now copy/store arg to correct locations.
1005 if (VA.isRegLoc()) {
1006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1007 VA.getLocReg()).addReg(Arg);
1008 RegArgs.push_back(VA.getLocReg());
1009 } else {
1010 // Need to store
1011 return false;
1012 }
1013 }
1014
1015 // Issue the call, BLr9 for darwin, BL otherwise.
1016 MachineInstrBuilder MIB;
1017 unsigned CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1018 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1019 .addGlobalAddress(F, 0, 0);
1020
1021 // Add implicit physical register uses to the call.
1022 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1023 MIB.addReg(RegArgs[i]);
1024
1025 // Now the return value.
1026 SmallVector<unsigned, 4> UsedRegs;
1027 if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
1028 SmallVector<CCValAssign, 16> RVLocs;
1029 CCState CCInfo(CC, false, TM, RVLocs, F->getContext());
1030 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1031
1032 // Copy all of the result registers out of their specified physreg.
1033 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1034 EVT CopyVT = RVLocs[0].getValVT();
1035 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1036
1037 unsigned ResultReg = createResultReg(DstRC);
1038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1039 ResultReg).addReg(RVLocs[0].getLocReg());
1040 UsedRegs.push_back(RVLocs[0].getLocReg());
1041
1042 // Finally update the result.
1043 UpdateValueMap(I, ResultReg);
1044 }
1045
1046 // Set all unused physreg defs as dead.
1047 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1048
1049 return true;
1050}
1051
1052bool ARMFastISel::ARMSelectSDiv(const Instruction *I) {
1053 EVT VT;
1054 const Type *Ty = I->getType();
1055 if (!isTypeLegal(Ty, VT))
1056 return false;
1057
1058 // If we have integer div support we should have gotten already, emit a
1059 // libcall.
1060 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1061 if (VT == MVT::i16)
1062 LC = RTLIB::SDIV_I16;
1063 else if (VT == MVT::i32)
1064 LC = RTLIB::SDIV_I32;
1065 else if (VT == MVT::i64)
1066 LC = RTLIB::SDIV_I64;
1067 else if (VT == MVT::i128)
1068 LC = RTLIB::SDIV_I128;
1069 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1070
1071 // Binary operand with all the same type.
1072 std::vector<const Type*> ArgTys;
1073 ArgTys.push_back(Ty);
1074 ArgTys.push_back(Ty);
1075 const FunctionType *FTy = FunctionType::get(Ty, ArgTys, false);
1076 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage,
1077 TLI.getLibcallName(LC));
1078 if (Subtarget->isAAPCS_ABI())
1079 F->setCallingConv(CallingConv::ARM_AAPCS);
1080 else
1081 F->setCallingConv(I->getParent()->getParent()->getCallingConv());
1082
1083 return ARMEmitLibcall(I, F);
1084}
1085
Eric Christopher56d2b722010-09-02 23:43:26 +00001086// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +00001087bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +00001088 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +00001089 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +00001090
Eric Christopherab695882010-07-21 22:26:11 +00001091 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +00001092 case Instruction::Load:
1093 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +00001094 case Instruction::Store:
1095 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +00001096 case Instruction::Br:
1097 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +00001098 case Instruction::ICmp:
1099 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +00001100 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +00001101 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +00001102 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +00001103 case Instruction::FPTrunc:
1104 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +00001105 case Instruction::SIToFP:
1106 return ARMSelectSIToFP(I);
1107 case Instruction::FPToSI:
1108 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +00001109 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +00001110 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +00001111 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +00001112 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +00001113 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +00001114 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherbb3e5da2010-09-14 23:03:37 +00001115 case Instruction::SDiv:
1116 return ARMSelectSDiv(I);
Eric Christopherab695882010-07-21 22:26:11 +00001117 default: break;
1118 }
1119 return false;
1120}
1121
1122namespace llvm {
1123 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +00001124 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +00001125 return 0;
Eric Christopherab695882010-07-21 22:26:11 +00001126 }
1127}