blob: 656b4502b142f147ed3839ac8cd62637e87e5770 [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"
27#include "llvm/CodeGen/Analysis.h"
28#include "llvm/CodeGen/FastISel.h"
29#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000032#include "llvm/CodeGen/MachineConstantPool.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000036#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000039#include "llvm/Target/TargetData.h"
40#include "llvm/Target/TargetInstrInfo.h"
41#include "llvm/Target/TargetLowering.h"
42#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000043#include "llvm/Target/TargetOptions.h"
44using namespace llvm;
45
Eric Christopher038fea52010-08-17 00:46:57 +000046static cl::opt<bool>
47EnableARMFastISel("arm-fast-isel",
48 cl::desc("Turn on experimental ARM fast-isel support"),
49 cl::init(false), cl::Hidden);
50
Eric Christopherab695882010-07-21 22:26:11 +000051namespace {
52
53class ARMFastISel : public FastISel {
54
55 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
56 /// make the right decision when generating code for different targets.
57 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000058 const TargetMachine &TM;
59 const TargetInstrInfo &TII;
60 const TargetLowering &TLI;
Eric Christopher7fe55b72010-08-23 22:32:45 +000061 const ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000062
Eric Christophereaa204b2010-09-02 01:39:14 +000063 // Convenience variable to avoid checking all the time.
64 bool isThumb;
65
Eric Christopherab695882010-07-21 22:26:11 +000066 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000067 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000068 : FastISel(funcInfo),
69 TM(funcInfo.MF->getTarget()),
70 TII(*TM.getInstrInfo()),
71 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000072 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000073 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000074 isThumb = AFI->isThumbFunction();
Eric Christopherab695882010-07-21 22:26:11 +000075 }
76
Eric Christophercb592292010-08-20 00:20:31 +000077 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000078 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
79 const TargetRegisterClass *RC);
80 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
81 const TargetRegisterClass *RC,
82 unsigned Op0, bool Op0IsKill);
83 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
84 const TargetRegisterClass *RC,
85 unsigned Op0, bool Op0IsKill,
86 unsigned Op1, bool Op1IsKill);
87 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
88 const TargetRegisterClass *RC,
89 unsigned Op0, bool Op0IsKill,
90 uint64_t Imm);
91 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
92 const TargetRegisterClass *RC,
93 unsigned Op0, bool Op0IsKill,
94 const ConstantFP *FPImm);
95 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
96 const TargetRegisterClass *RC,
97 uint64_t Imm);
98 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
99 const TargetRegisterClass *RC,
100 unsigned Op0, bool Op0IsKill,
101 unsigned Op1, bool Op1IsKill,
102 uint64_t Imm);
103 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
104 unsigned Op0, bool Op0IsKill,
105 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000106
Eric Christophercb592292010-08-20 00:20:31 +0000107 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000108 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000109 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000110
111 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000112
Eric Christopher83007122010-08-23 21:44:12 +0000113 // Instruction selection routines.
Eric Christopher44bff902010-09-10 23:10:30 +0000114 private:
Eric Christopher83007122010-08-23 21:44:12 +0000115 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000116 virtual bool ARMSelectStore(const Instruction *I);
Eric Christophere5734102010-09-03 00:35:47 +0000117 virtual bool ARMSelectBranch(const Instruction *I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000118 virtual bool ARMSelectCmp(const Instruction *I);
Eric Christopher46203602010-09-09 00:26:48 +0000119 virtual bool ARMSelectFPExt(const Instruction *I);
Eric Christopherce07b542010-09-09 20:26:31 +0000120 virtual bool ARMSelectFPTrunc(const Instruction *I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000121 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopher9a040492010-09-09 18:54:59 +0000122 virtual bool ARMSelectSIToFP(const Instruction *I);
123 virtual bool ARMSelectFPToSI(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000124
Eric Christopher83007122010-08-23 21:44:12 +0000125 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000126 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000127 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000128 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000129 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000130 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000131 bool ARMLoadAlloca(const Instruction *I, EVT VT);
132 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000133 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000134 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
135 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000136 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000137 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000138
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000139 // Call handling routines.
140 private:
141 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
142
143 // OptionalDef handling routines.
144 private:
Eric Christopher456144e2010-08-19 00:37:05 +0000145 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
146 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
147};
Eric Christopherab695882010-07-21 22:26:11 +0000148
149} // end anonymous namespace
150
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000151#include "ARMGenCallingConv.inc"
Eric Christopherab695882010-07-21 22:26:11 +0000152
Eric Christopher456144e2010-08-19 00:37:05 +0000153// DefinesOptionalPredicate - This is different from DefinesPredicate in that
154// we don't care about implicit defs here, just places we'll need to add a
155// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
156bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
157 const TargetInstrDesc &TID = MI->getDesc();
158 if (!TID.hasOptionalDef())
159 return false;
160
161 // Look to see if our OptionalDef is defining CPSR or CCR.
162 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
163 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000164 if (!MO.isReg() || !MO.isDef()) continue;
165 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000166 *CPSR = true;
167 }
168 return true;
169}
170
171// If the machine is predicable go ahead and add the predicate operands, if
172// it needs default CC operands add those.
173const MachineInstrBuilder &
174ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
175 MachineInstr *MI = &*MIB;
176
177 // Do we use a predicate?
178 if (TII.isPredicable(MI))
179 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000180
Eric Christopher456144e2010-08-19 00:37:05 +0000181 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
182 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000183 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000184 if (DefinesOptionalPredicate(MI, &CPSR)) {
185 if (CPSR)
186 AddDefaultT1CC(MIB);
187 else
188 AddDefaultCC(MIB);
189 }
190 return MIB;
191}
192
Eric Christopher0fe7d542010-08-17 01:25:29 +0000193unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
194 const TargetRegisterClass* RC) {
195 unsigned ResultReg = createResultReg(RC);
196 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
197
Eric Christopher456144e2010-08-19 00:37:05 +0000198 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000199 return ResultReg;
200}
201
202unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
203 const TargetRegisterClass *RC,
204 unsigned Op0, bool Op0IsKill) {
205 unsigned ResultReg = createResultReg(RC);
206 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
207
208 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000209 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000210 .addReg(Op0, Op0IsKill * RegState::Kill));
211 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000212 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000213 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000215 TII.get(TargetOpcode::COPY), ResultReg)
216 .addReg(II.ImplicitDefs[0]));
217 }
218 return ResultReg;
219}
220
221unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
222 const TargetRegisterClass *RC,
223 unsigned Op0, bool Op0IsKill,
224 unsigned Op1, bool Op1IsKill) {
225 unsigned ResultReg = createResultReg(RC);
226 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
227
228 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000230 .addReg(Op0, Op0IsKill * RegState::Kill)
231 .addReg(Op1, Op1IsKill * RegState::Kill));
232 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 .addReg(Op0, Op0IsKill * RegState::Kill)
235 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 TII.get(TargetOpcode::COPY), ResultReg)
238 .addReg(II.ImplicitDefs[0]));
239 }
240 return ResultReg;
241}
242
243unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
244 const TargetRegisterClass *RC,
245 unsigned Op0, bool Op0IsKill,
246 uint64_t Imm) {
247 unsigned ResultReg = createResultReg(RC);
248 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
249
250 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000252 .addReg(Op0, Op0IsKill * RegState::Kill)
253 .addImm(Imm));
254 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000255 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000256 .addReg(Op0, Op0IsKill * RegState::Kill)
257 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000259 TII.get(TargetOpcode::COPY), ResultReg)
260 .addReg(II.ImplicitDefs[0]));
261 }
262 return ResultReg;
263}
264
265unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
266 const TargetRegisterClass *RC,
267 unsigned Op0, bool Op0IsKill,
268 const ConstantFP *FPImm) {
269 unsigned ResultReg = createResultReg(RC);
270 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
271
272 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274 .addReg(Op0, Op0IsKill * RegState::Kill)
275 .addFPImm(FPImm));
276 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000277 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000278 .addReg(Op0, Op0IsKill * RegState::Kill)
279 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281 TII.get(TargetOpcode::COPY), ResultReg)
282 .addReg(II.ImplicitDefs[0]));
283 }
284 return ResultReg;
285}
286
287unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
288 const TargetRegisterClass *RC,
289 unsigned Op0, bool Op0IsKill,
290 unsigned Op1, bool Op1IsKill,
291 uint64_t Imm) {
292 unsigned ResultReg = createResultReg(RC);
293 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
294
295 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000297 .addReg(Op0, Op0IsKill * RegState::Kill)
298 .addReg(Op1, Op1IsKill * RegState::Kill)
299 .addImm(Imm));
300 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addReg(Op0, Op0IsKill * RegState::Kill)
303 .addReg(Op1, Op1IsKill * RegState::Kill)
304 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 TII.get(TargetOpcode::COPY), ResultReg)
307 .addReg(II.ImplicitDefs[0]));
308 }
309 return ResultReg;
310}
311
312unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
313 const TargetRegisterClass *RC,
314 uint64_t Imm) {
315 unsigned ResultReg = createResultReg(RC);
316 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000317
Eric Christopher0fe7d542010-08-17 01:25:29 +0000318 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000319 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000320 .addImm(Imm));
321 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 TII.get(TargetOpcode::COPY), ResultReg)
326 .addReg(II.ImplicitDefs[0]));
327 }
328 return ResultReg;
329}
330
331unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
332 unsigned Op0, bool Op0IsKill,
333 uint32_t Idx) {
334 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
335 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
336 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000337 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000338 DL, TII.get(TargetOpcode::COPY), ResultReg)
339 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
340 return ResultReg;
341}
342
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000343// TODO: Don't worry about 64-bit now, but when this is fixed remove the
344// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000345unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000346 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
347
348 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
350 TII.get(ARM::VMOVRS), MoveReg)
351 .addReg(SrcReg));
352 return MoveReg;
353}
354
355unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000356 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
357
Eric Christopheraa3ace12010-09-09 20:49:25 +0000358 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000360 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000361 .addReg(SrcReg));
362 return MoveReg;
363}
364
Eric Christopher9ed58df2010-09-09 00:19:41 +0000365// For double width floating point we need to materialize two constants
366// (the high and the low) into integer registers then use a move to get
367// the combined constant into an FP reg.
368unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
369 const APFloat Val = CFP->getValueAPF();
370 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000371
Eric Christopher9ed58df2010-09-09 00:19:41 +0000372 // This checks to see if we can use VFP3 instructions to materialize
373 // a constant, otherwise we have to go through the constant pool.
374 if (TLI.isFPImmLegal(Val, VT)) {
375 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
376 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
378 DestReg)
379 .addFPImm(CFP));
380 return DestReg;
381 }
Eric Christopher238bb162010-09-09 23:50:00 +0000382
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000383 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000384 if (!Subtarget->hasVFP2()) return false;
385
386 // MachineConstantPool wants an explicit alignment.
387 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
388 if (Align == 0) {
389 // TODO: Figure out if this is correct.
390 Align = TD.getTypeAllocSize(CFP->getType());
391 }
392 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
393 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
394 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
395
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000396 // The extra reg is for addrmode5.
Eric Christopher238bb162010-09-09 23:50:00 +0000397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
398 .addReg(DestReg).addConstantPoolIndex(Idx)
399 .addReg(0));
400 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000401}
402
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000403// TODO: Verify 64-bit.
Eric Christopher9ed58df2010-09-09 00:19:41 +0000404unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000405 // MachineConstantPool wants an explicit alignment.
406 unsigned Align = TD.getPrefTypeAlignment(C->getType());
407 if (Align == 0) {
408 // TODO: Figure out if this is correct.
409 Align = TD.getTypeAllocSize(C->getType());
410 }
411 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher845c5752010-09-08 18:56:34 +0000412 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000413
Eric Christopher56d2b722010-09-02 23:43:26 +0000414 if (isThumb)
415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
416 TII.get(ARM::t2LDRpci))
417 .addReg(DestReg).addConstantPoolIndex(Idx));
418 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000419 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
421 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000422 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000423 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000424
Eric Christopher56d2b722010-09-02 23:43:26 +0000425 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000426}
427
Eric Christopher9ed58df2010-09-09 00:19:41 +0000428unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
429 EVT VT = TLI.getValueType(C->getType(), true);
430
431 // Only handle simple types.
432 if (!VT.isSimple()) return 0;
433
434 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
435 return ARMMaterializeFP(CFP, VT);
436 return ARMMaterializeInt(C);
437}
438
Eric Christopherb1cc8482010-08-25 07:23:49 +0000439bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
440 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000441
Eric Christopherb1cc8482010-08-25 07:23:49 +0000442 // Only handle simple types.
443 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000444
Eric Christopherdc908042010-08-31 01:28:42 +0000445 // Handle all legal types, i.e. a register that will directly hold this
446 // value.
447 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000448}
449
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000450bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
451 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000452
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000453 // If this is a type than can be sign or zero-extended to a basic operation
454 // go ahead and accept it now.
455 if (VT == MVT::i8 || VT == MVT::i16)
456 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000457
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000458 return false;
459}
460
Eric Christophercb0b04b2010-08-24 00:07:24 +0000461// Computes the Reg+Offset to get to an object.
462bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000463 int &Offset) {
464 // Some boilerplate from the X86 FastISel.
465 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000466 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000467 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000468 // Don't walk into other basic blocks; it's possible we haven't
469 // visited them yet, so the instructions may not yet be assigned
470 // virtual registers.
471 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
472 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000473 Opcode = I->getOpcode();
474 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000475 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000476 Opcode = C->getOpcode();
477 U = C;
478 }
479
Eric Christophercb0b04b2010-08-24 00:07:24 +0000480 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000481 if (Ty->getAddressSpace() > 255)
482 // Fast instruction selection doesn't support the special
483 // address spaces.
484 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000485
Eric Christopher83007122010-08-23 21:44:12 +0000486 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000487 default:
Eric Christopher83007122010-08-23 21:44:12 +0000488 break;
489 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000490 assert(false && "Alloca should have been handled earlier!");
491 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000492 }
493 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000494
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000495 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000496 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000497 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000498 return false;
499 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000500
Eric Christophercb0b04b2010-08-24 00:07:24 +0000501 // Try to get this in a register if nothing else has worked.
502 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000503 if (Reg == 0) return false;
504
505 // Since the offset may be too large for the load instruction
506 // get the reg+offset into a register.
507 // TODO: Verify the additions work, otherwise we'll need to add the
508 // offset instead of 0 to the instructions and do all sorts of operand
509 // munging.
510 // TODO: Optimize this somewhat.
511 if (Offset != 0) {
512 ARMCC::CondCodes Pred = ARMCC::AL;
513 unsigned PredReg = 0;
514
Eric Christophereaa204b2010-09-02 01:39:14 +0000515 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000516 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
517 Reg, Reg, Offset, Pred, PredReg,
518 static_cast<const ARMBaseInstrInfo&>(TII));
519 else {
520 assert(AFI->isThumb2Function());
521 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
522 Reg, Reg, Offset, Pred, PredReg,
523 static_cast<const ARMBaseInstrInfo&>(TII));
524 }
525 }
Eric Christopher318b6ee2010-09-02 00:53:56 +0000526 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000527}
528
Eric Christopher30b66332010-09-08 21:49:50 +0000529bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000530 Value *Op0 = I->getOperand(0);
531
532 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000533 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
534 DenseMap<const AllocaInst*, int>::iterator SI =
535 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000536
Eric Christophere24d66f2010-08-24 22:07:27 +0000537 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000538 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000539 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000540 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000541 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000542 TM.getRegisterInfo());
543 UpdateValueMap(I, ResultReg);
544 return true;
545 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000546 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000547 return false;
548}
549
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
551 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000552
Eric Christopherb1cc8482010-08-25 07:23:49 +0000553 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000554 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000555
Eric Christopherb1cc8482010-08-25 07:23:49 +0000556 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000557 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000558 assert(false && "Trying to emit for an unhandled type!");
559 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000560 case MVT::i16:
561 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
562 VT = MVT::i32;
563 break;
564 case MVT::i8:
565 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
566 VT = MVT::i32;
567 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000568 case MVT::i32:
569 Opc = isThumb ? ARM::tLDR : ARM::LDR;
570 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000571 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000572
Eric Christopherdc908042010-08-31 01:28:42 +0000573 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000574
Eric Christopherdc908042010-08-31 01:28:42 +0000575 // TODO: Fix the Addressing modes so that these can share some code.
576 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
577 if (isThumb)
578 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
579 TII.get(Opc), ResultReg)
580 .addReg(Reg).addImm(Offset).addReg(0));
581 else
582 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
583 TII.get(Opc), ResultReg)
584 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000585 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000586}
587
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000588bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
589 // Verify we have a legal type before going any further.
590 EVT VT;
591 if (!isLoadTypeLegal(I->getType(), VT))
592 return false;
593
594 // If we're an alloca we know we have a frame index and can emit the load
595 // directly in short order.
596 if (ARMLoadAlloca(I, VT))
597 return true;
598
599 // Our register and offset with innocuous defaults.
600 unsigned Reg = 0;
601 int Offset = 0;
602
603 // See if we can handle this as Reg + Offset
604 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
605 return false;
606
607 unsigned ResultReg;
608 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
609
610 UpdateValueMap(I, ResultReg);
611 return true;
612}
613
Eric Christopher30b66332010-09-08 21:49:50 +0000614bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000615 Value *Op1 = I->getOperand(1);
616
617 // Verify it's an alloca.
618 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
619 DenseMap<const AllocaInst*, int>::iterator SI =
620 FuncInfo.StaticAllocaMap.find(AI);
621
622 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000623 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000624 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000625 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000626 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000627 TM.getRegisterInfo());
628 return true;
629 }
630 }
631 return false;
632}
633
Eric Christopher318b6ee2010-09-02 00:53:56 +0000634bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
635 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000636 unsigned StrOpc;
637 switch (VT.getSimpleVT().SimpleTy) {
638 default: return false;
639 case MVT::i1:
640 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
641 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
642 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000643 case MVT::f32:
644 if (!Subtarget->hasVFP2()) return false;
645 StrOpc = ARM::VSTRS;
646 break;
647 case MVT::f64:
648 if (!Subtarget->hasVFP2()) return false;
649 StrOpc = ARM::VSTRD;
650 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000651 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000652
Eric Christopher318b6ee2010-09-02 00:53:56 +0000653 if (isThumb)
654 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
655 TII.get(StrOpc), SrcReg)
656 .addReg(DstReg).addImm(Offset).addReg(0));
657 else
658 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
659 TII.get(StrOpc), SrcReg)
660 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000661
Eric Christopher318b6ee2010-09-02 00:53:56 +0000662 return true;
663}
664
665bool ARMFastISel::ARMSelectStore(const Instruction *I) {
666 Value *Op0 = I->getOperand(0);
667 unsigned SrcReg = 0;
668
Eric Christopher543cf052010-09-01 22:16:27 +0000669 // Yay type legalization
670 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000671 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000672 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000673
Eric Christopher1b61ef42010-09-02 01:48:11 +0000674 // Get the value to be stored into a register.
675 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000676 if (SrcReg == 0)
677 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000678
Eric Christopher318b6ee2010-09-02 00:53:56 +0000679 // If we're an alloca we know we have a frame index and can emit the store
680 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000681 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000682 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christopher318b6ee2010-09-02 00:53:56 +0000684 // Our register and offset with innocuous defaults.
685 unsigned Reg = 0;
686 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000687
Eric Christopher318b6ee2010-09-02 00:53:56 +0000688 // See if we can handle this as Reg + Offset
689 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
690 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000691
Eric Christopher318b6ee2010-09-02 00:53:56 +0000692 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000693
Eric Christopher543cf052010-09-01 22:16:27 +0000694 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000695}
696
Eric Christophere5734102010-09-03 00:35:47 +0000697bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
698 const BranchInst *BI = cast<BranchInst>(I);
699 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
700 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000701
Eric Christophere5734102010-09-03 00:35:47 +0000702 // Simple branch support.
703 unsigned CondReg = getRegForValue(BI->getCondition());
704 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000705
Eric Christophere5734102010-09-03 00:35:47 +0000706 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
707 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
708 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
709 .addReg(CondReg).addReg(CondReg));
710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
711 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
712 FastEmitBranch(FBB, DL);
713 FuncInfo.MBB->addSuccessor(TBB);
714 return true;
715}
716
Eric Christopherd43393a2010-09-08 23:13:45 +0000717bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
718 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000719
Eric Christopherd43393a2010-09-08 23:13:45 +0000720 EVT VT;
721 const Type *Ty = CI->getOperand(0)->getType();
722 if (!isTypeLegal(Ty, VT))
723 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000724
Eric Christopherd43393a2010-09-08 23:13:45 +0000725 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
726 if (isFloat && !Subtarget->hasVFP2())
727 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000728
Eric Christopherd43393a2010-09-08 23:13:45 +0000729 unsigned CmpOpc;
730 switch (VT.getSimpleVT().SimpleTy) {
731 default: return false;
732 // TODO: Verify compares.
733 case MVT::f32:
734 CmpOpc = ARM::VCMPES;
735 break;
736 case MVT::f64:
737 CmpOpc = ARM::VCMPED;
738 break;
739 case MVT::i32:
740 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
741 break;
742 }
743
744 unsigned Arg1 = getRegForValue(CI->getOperand(0));
745 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000746
Eric Christopherd43393a2010-09-08 23:13:45 +0000747 unsigned Arg2 = getRegForValue(CI->getOperand(1));
748 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000749
Eric Christopherd43393a2010-09-08 23:13:45 +0000750 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
751 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000752
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000753 // For floating point we need to move the result to a comparison register
754 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000755 if (isFloat)
756 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
757 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000758
759 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000760 return true;
761}
762
Eric Christopher46203602010-09-09 00:26:48 +0000763bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
764 // Make sure we have VFP and that we're extending float to double.
765 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000766
Eric Christopher46203602010-09-09 00:26:48 +0000767 Value *V = I->getOperand(0);
768 if (!I->getType()->isDoubleTy() ||
769 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000770
Eric Christopher46203602010-09-09 00:26:48 +0000771 unsigned Op = getRegForValue(V);
772 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000773
Eric Christopher46203602010-09-09 00:26:48 +0000774 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000775 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000776 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000777 .addReg(Op));
778 UpdateValueMap(I, Result);
779 return true;
780}
781
782bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
783 // Make sure we have VFP and that we're truncating double to float.
784 if (!Subtarget->hasVFP2()) return false;
785
786 Value *V = I->getOperand(0);
787 if (!I->getType()->isFloatTy() ||
788 !V->getType()->isDoubleTy()) return false;
789
790 unsigned Op = getRegForValue(V);
791 if (Op == 0) return false;
792
793 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000794 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000795 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000796 .addReg(Op));
797 UpdateValueMap(I, Result);
798 return true;
799}
800
Eric Christopher9a040492010-09-09 18:54:59 +0000801bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
802 // Make sure we have VFP.
803 if (!Subtarget->hasVFP2()) return false;
804
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000805 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000806 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000807 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000808 return false;
809
810 unsigned Op = getRegForValue(I->getOperand(0));
811 if (Op == 0) return false;
812
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000813 // The conversion routine works on fp-reg to fp-reg and the operand above
814 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000815 unsigned FP = ARMMoveToFPReg(DstVT, Op);
816 if (FP == 0) return false;
817
Eric Christopher9a040492010-09-09 18:54:59 +0000818 unsigned Opc;
819 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
820 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
821 else return 0;
822
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000823 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000824 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
825 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000826 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000827 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000828 return true;
829}
830
831bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
832 // Make sure we have VFP.
833 if (!Subtarget->hasVFP2()) return false;
834
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000835 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000836 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000837 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000838 return false;
839
840 unsigned Op = getRegForValue(I->getOperand(0));
841 if (Op == 0) return false;
842
843 unsigned Opc;
844 const Type *OpTy = I->getOperand(0)->getType();
845 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
846 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
847 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000848 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000849
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000850 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000851 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
852 ResultReg)
853 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000854
855 // This result needs to be in an integer register, but the conversion only
856 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000857 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000858 if (IntReg == 0) return false;
859
860 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000861 return true;
862}
863
Eric Christopherbc39b822010-09-09 00:53:57 +0000864bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000865 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000866
Eric Christopherbc39b822010-09-09 00:53:57 +0000867 // We can get here in the case when we want to use NEON for our fp
868 // operations, but can't figure out how to. Just use the vfp instructions
869 // if we have them.
870 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000871 const Type *Ty = I->getType();
872 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
873 if (isFloat && !Subtarget->hasVFP2())
874 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000875
Eric Christopherbc39b822010-09-09 00:53:57 +0000876 unsigned Op1 = getRegForValue(I->getOperand(0));
877 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000878
Eric Christopherbc39b822010-09-09 00:53:57 +0000879 unsigned Op2 = getRegForValue(I->getOperand(1));
880 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000881
Eric Christopherbc39b822010-09-09 00:53:57 +0000882 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000883 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
884 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000885 switch (ISDOpcode) {
886 default: return false;
887 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000888 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000889 break;
890 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000891 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000892 break;
893 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000894 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000895 break;
896 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000897 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000898 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
899 TII.get(Opc), ResultReg)
900 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000901 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000902 return true;
903}
904
Eric Christopherd10cd7b2010-09-10 23:18:12 +0000905// Call Handling Code
906
907// This is largely taken directly from CCAssignFnForNode - we don't support
908// varargs in FastISel so that part has been removed.
909// TODO: We may not support all of this.
910CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
911 switch (CC) {
912 default:
913 llvm_unreachable("Unsupported calling convention");
914 case CallingConv::C:
915 case CallingConv::Fast:
916 // Use target triple & subtarget features to do actual dispatch.
917 if (Subtarget->isAAPCS_ABI()) {
918 if (Subtarget->hasVFP2() &&
919 FloatABIType == FloatABI::Hard)
920 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
921 else
922 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
923 } else
924 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
925 case CallingConv::ARM_AAPCS_VFP:
926 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
927 case CallingConv::ARM_AAPCS:
928 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
929 case CallingConv::ARM_APCS:
930 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
931 }
932}
933
Eric Christopher56d2b722010-09-02 23:43:26 +0000934// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000935bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000936 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000937 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000938
Eric Christopherab695882010-07-21 22:26:11 +0000939 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000940 case Instruction::Load:
941 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000942 case Instruction::Store:
943 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000944 case Instruction::Br:
945 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000946 case Instruction::ICmp:
947 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000948 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000949 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000950 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +0000951 case Instruction::FPTrunc:
952 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000953 case Instruction::SIToFP:
954 return ARMSelectSIToFP(I);
955 case Instruction::FPToSI:
956 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000957 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000958 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000959 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000960 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000961 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000962 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000963 default: break;
964 }
965 return false;
966}
967
968namespace llvm {
969 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000970 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000971 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000972 }
973}