blob: 54f3dfba79771db52b4351570881ffaffb54f5ac [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 Christopherab695882010-07-21 22:26:11 +000018#include "ARMRegisterInfo.h"
19#include "ARMTargetMachine.h"
20#include "ARMSubtarget.h"
21#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Instructions.h"
25#include "llvm/IntrinsicInst.h"
26#include "llvm/CodeGen/Analysis.h"
27#include "llvm/CodeGen/FastISel.h"
28#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/MachineConstantPool.h"
32#include "llvm/CodeGen/MachineFrameInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000035#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000038#include "llvm/Target/TargetData.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetLowering.h"
41#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000042#include "llvm/Target/TargetOptions.h"
43using namespace llvm;
44
Eric Christopher038fea52010-08-17 00:46:57 +000045static cl::opt<bool>
46EnableARMFastISel("arm-fast-isel",
47 cl::desc("Turn on experimental ARM fast-isel support"),
48 cl::init(false), cl::Hidden);
49
Eric Christopherab695882010-07-21 22:26:11 +000050namespace {
51
52class ARMFastISel : public FastISel {
53
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
56 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000057 const TargetMachine &TM;
58 const TargetInstrInfo &TII;
59 const TargetLowering &TLI;
Eric Christopher7fe55b72010-08-23 22:32:45 +000060 const ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000061
Eric Christophereaa204b2010-09-02 01:39:14 +000062 // Convenience variable to avoid checking all the time.
63 bool isThumb;
64
Eric Christopherab695882010-07-21 22:26:11 +000065 public:
Eric Christopherac1a19e2010-09-09 01:06:51 +000066 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
Eric Christopher0fe7d542010-08-17 01:25:29 +000067 : FastISel(funcInfo),
68 TM(funcInfo.MF->getTarget()),
69 TII(*TM.getInstrInfo()),
70 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000071 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000072 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000073 isThumb = AFI->isThumbFunction();
Eric Christopherab695882010-07-21 22:26:11 +000074 }
75
Eric Christophercb592292010-08-20 00:20:31 +000076 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000077 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
78 const TargetRegisterClass *RC);
79 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
80 const TargetRegisterClass *RC,
81 unsigned Op0, bool Op0IsKill);
82 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC,
84 unsigned Op0, bool Op0IsKill,
85 unsigned Op1, bool Op1IsKill);
86 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 uint64_t Imm);
90 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
93 const ConstantFP *FPImm);
94 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 unsigned Op1, bool Op1IsKill,
101 uint64_t Imm);
102 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
103 unsigned Op0, bool Op0IsKill,
104 uint32_t Idx);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000105
Eric Christophercb592292010-08-20 00:20:31 +0000106 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000107 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000108 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000109
110 #include "ARMGenFastISel.inc"
Eric Christopherac1a19e2010-09-09 01:06:51 +0000111
Eric Christopher83007122010-08-23 21:44:12 +0000112 // Instruction selection routines.
113 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000114 virtual bool ARMSelectStore(const Instruction *I);
Eric Christophere5734102010-09-03 00:35:47 +0000115 virtual bool ARMSelectBranch(const Instruction *I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000116 virtual bool ARMSelectCmp(const Instruction *I);
Eric Christopher46203602010-09-09 00:26:48 +0000117 virtual bool ARMSelectFPExt(const Instruction *I);
Eric Christopherce07b542010-09-09 20:26:31 +0000118 virtual bool ARMSelectFPTrunc(const Instruction *I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000119 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopher9a040492010-09-09 18:54:59 +0000120 virtual bool ARMSelectSIToFP(const Instruction *I);
121 virtual bool ARMSelectFPToSI(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000122
Eric Christopher83007122010-08-23 21:44:12 +0000123 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000124 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000125 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000126 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000127 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000128 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000129 bool ARMLoadAlloca(const Instruction *I, EVT VT);
130 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000131 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000132 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
133 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000134
Eric Christopher456144e2010-08-19 00:37:05 +0000135 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
136 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
137};
Eric Christopherab695882010-07-21 22:26:11 +0000138
139} // end anonymous namespace
140
141// #include "ARMGenCallingConv.inc"
142
Eric Christopher456144e2010-08-19 00:37:05 +0000143// DefinesOptionalPredicate - This is different from DefinesPredicate in that
144// we don't care about implicit defs here, just places we'll need to add a
145// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
146bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
147 const TargetInstrDesc &TID = MI->getDesc();
148 if (!TID.hasOptionalDef())
149 return false;
150
151 // Look to see if our OptionalDef is defining CPSR or CCR.
152 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
153 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000154 if (!MO.isReg() || !MO.isDef()) continue;
155 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000156 *CPSR = true;
157 }
158 return true;
159}
160
161// If the machine is predicable go ahead and add the predicate operands, if
162// it needs default CC operands add those.
163const MachineInstrBuilder &
164ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
165 MachineInstr *MI = &*MIB;
166
167 // Do we use a predicate?
168 if (TII.isPredicable(MI))
169 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000170
Eric Christopher456144e2010-08-19 00:37:05 +0000171 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
172 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000173 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000174 if (DefinesOptionalPredicate(MI, &CPSR)) {
175 if (CPSR)
176 AddDefaultT1CC(MIB);
177 else
178 AddDefaultCC(MIB);
179 }
180 return MIB;
181}
182
Eric Christopher0fe7d542010-08-17 01:25:29 +0000183unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
184 const TargetRegisterClass* RC) {
185 unsigned ResultReg = createResultReg(RC);
186 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
187
Eric Christopher456144e2010-08-19 00:37:05 +0000188 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000189 return ResultReg;
190}
191
192unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
193 const TargetRegisterClass *RC,
194 unsigned Op0, bool Op0IsKill) {
195 unsigned ResultReg = createResultReg(RC);
196 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
197
198 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000199 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000200 .addReg(Op0, Op0IsKill * RegState::Kill));
201 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000202 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000203 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000205 TII.get(TargetOpcode::COPY), ResultReg)
206 .addReg(II.ImplicitDefs[0]));
207 }
208 return ResultReg;
209}
210
211unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
212 const TargetRegisterClass *RC,
213 unsigned Op0, bool Op0IsKill,
214 unsigned Op1, bool Op1IsKill) {
215 unsigned ResultReg = createResultReg(RC);
216 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
217
218 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000219 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000220 .addReg(Op0, Op0IsKill * RegState::Kill)
221 .addReg(Op1, Op1IsKill * RegState::Kill));
222 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000224 .addReg(Op0, Op0IsKill * RegState::Kill)
225 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000227 TII.get(TargetOpcode::COPY), ResultReg)
228 .addReg(II.ImplicitDefs[0]));
229 }
230 return ResultReg;
231}
232
233unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
234 const TargetRegisterClass *RC,
235 unsigned Op0, bool Op0IsKill,
236 uint64_t Imm) {
237 unsigned ResultReg = createResultReg(RC);
238 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
239
240 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000241 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000242 .addReg(Op0, Op0IsKill * RegState::Kill)
243 .addImm(Imm));
244 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000245 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000246 .addReg(Op0, Op0IsKill * RegState::Kill)
247 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249 TII.get(TargetOpcode::COPY), ResultReg)
250 .addReg(II.ImplicitDefs[0]));
251 }
252 return ResultReg;
253}
254
255unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
256 const TargetRegisterClass *RC,
257 unsigned Op0, bool Op0IsKill,
258 const ConstantFP *FPImm) {
259 unsigned ResultReg = createResultReg(RC);
260 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
261
262 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000264 .addReg(Op0, Op0IsKill * RegState::Kill)
265 .addFPImm(FPImm));
266 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000267 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268 .addReg(Op0, Op0IsKill * RegState::Kill)
269 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000271 TII.get(TargetOpcode::COPY), ResultReg)
272 .addReg(II.ImplicitDefs[0]));
273 }
274 return ResultReg;
275}
276
277unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
278 const TargetRegisterClass *RC,
279 unsigned Op0, bool Op0IsKill,
280 unsigned Op1, bool Op1IsKill,
281 uint64_t Imm) {
282 unsigned ResultReg = createResultReg(RC);
283 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
284
285 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 .addReg(Op0, Op0IsKill * RegState::Kill)
288 .addReg(Op1, Op1IsKill * RegState::Kill)
289 .addImm(Imm));
290 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000292 .addReg(Op0, Op0IsKill * RegState::Kill)
293 .addReg(Op1, Op1IsKill * RegState::Kill)
294 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000296 TII.get(TargetOpcode::COPY), ResultReg)
297 .addReg(II.ImplicitDefs[0]));
298 }
299 return ResultReg;
300}
301
302unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
303 const TargetRegisterClass *RC,
304 uint64_t Imm) {
305 unsigned ResultReg = createResultReg(RC);
306 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000307
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 .addImm(Imm));
311 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000313 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000315 TII.get(TargetOpcode::COPY), ResultReg)
316 .addReg(II.ImplicitDefs[0]));
317 }
318 return ResultReg;
319}
320
321unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
322 unsigned Op0, bool Op0IsKill,
323 uint32_t Idx) {
324 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
325 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
326 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000328 DL, TII.get(TargetOpcode::COPY), ResultReg)
329 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
330 return ResultReg;
331}
332
Eric Christopher9ed58df2010-09-09 00:19:41 +0000333// For double width floating point we need to materialize two constants
334// (the high and the low) into integer registers then use a move to get
335// the combined constant into an FP reg.
336unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
337 const APFloat Val = CFP->getValueAPF();
338 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000339
Eric Christopher9ed58df2010-09-09 00:19:41 +0000340 // This checks to see if we can use VFP3 instructions to materialize
341 // a constant, otherwise we have to go through the constant pool.
342 if (TLI.isFPImmLegal(Val, VT)) {
343 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
344 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
345 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
346 DestReg)
347 .addFPImm(CFP));
348 return DestReg;
349 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000350
Eric Christopher9ed58df2010-09-09 00:19:41 +0000351 // No 64-bit at the moment.
352 if (is64bit) return 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000353
Eric Christopher9ed58df2010-09-09 00:19:41 +0000354 // Load this from the constant pool.
355 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000356
Eric Christopher9ed58df2010-09-09 00:19:41 +0000357 // If we have a floating point constant we expect it in a floating point
358 // register.
359 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
360 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
361 TII.get(ARM::VMOVRS), MoveReg)
362 .addReg(DestReg));
363 return MoveReg;
364}
365
366unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000367 // MachineConstantPool wants an explicit alignment.
368 unsigned Align = TD.getPrefTypeAlignment(C->getType());
369 if (Align == 0) {
370 // TODO: Figure out if this is correct.
371 Align = TD.getTypeAllocSize(C->getType());
372 }
373 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
374
Eric Christopher845c5752010-09-08 18:56:34 +0000375 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000376 if (isThumb)
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(ARM::t2LDRpci))
379 .addReg(DestReg).addConstantPoolIndex(Idx));
380 else
381 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
382 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000383 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000384 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000385
Eric Christopher56d2b722010-09-02 23:43:26 +0000386 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000387}
388
Eric Christopher9ed58df2010-09-09 00:19:41 +0000389unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
390 EVT VT = TLI.getValueType(C->getType(), true);
391
392 // Only handle simple types.
393 if (!VT.isSimple()) return 0;
394
395 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
396 return ARMMaterializeFP(CFP, VT);
397 return ARMMaterializeInt(C);
398}
399
Eric Christopherb1cc8482010-08-25 07:23:49 +0000400bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
401 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000402
Eric Christopherb1cc8482010-08-25 07:23:49 +0000403 // Only handle simple types.
404 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000405
Eric Christopherdc908042010-08-31 01:28:42 +0000406 // Handle all legal types, i.e. a register that will directly hold this
407 // value.
408 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000409}
410
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000411bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
412 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000413
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000414 // If this is a type than can be sign or zero-extended to a basic operation
415 // go ahead and accept it now.
416 if (VT == MVT::i8 || VT == MVT::i16)
417 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000418
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000419 return false;
420}
421
Eric Christophercb0b04b2010-08-24 00:07:24 +0000422// Computes the Reg+Offset to get to an object.
423bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000424 int &Offset) {
425 // Some boilerplate from the X86 FastISel.
426 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000427 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000428 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000429 // Don't walk into other basic blocks; it's possible we haven't
430 // visited them yet, so the instructions may not yet be assigned
431 // virtual registers.
432 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
433 return false;
434
435 Opcode = I->getOpcode();
436 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000437 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000438 Opcode = C->getOpcode();
439 U = C;
440 }
441
Eric Christophercb0b04b2010-08-24 00:07:24 +0000442 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000443 if (Ty->getAddressSpace() > 255)
444 // Fast instruction selection doesn't support the special
445 // address spaces.
446 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000447
Eric Christopher83007122010-08-23 21:44:12 +0000448 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000449 default:
Eric Christopher83007122010-08-23 21:44:12 +0000450 //errs() << "Failing Opcode is: " << *Op1 << "\n";
451 break;
452 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000453 assert(false && "Alloca should have been handled earlier!");
454 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000455 }
456 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000457
Eric Christophercb0b04b2010-08-24 00:07:24 +0000458 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
459 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000460 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000461 return false;
462 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000463
Eric Christophercb0b04b2010-08-24 00:07:24 +0000464 // Try to get this in a register if nothing else has worked.
465 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000466 if (Reg == 0) return false;
467
468 // Since the offset may be too large for the load instruction
469 // get the reg+offset into a register.
470 // TODO: Verify the additions work, otherwise we'll need to add the
471 // offset instead of 0 to the instructions and do all sorts of operand
472 // munging.
473 // TODO: Optimize this somewhat.
474 if (Offset != 0) {
475 ARMCC::CondCodes Pred = ARMCC::AL;
476 unsigned PredReg = 0;
477
Eric Christophereaa204b2010-09-02 01:39:14 +0000478 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000479 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
480 Reg, Reg, Offset, Pred, PredReg,
481 static_cast<const ARMBaseInstrInfo&>(TII));
482 else {
483 assert(AFI->isThumb2Function());
484 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
485 Reg, Reg, Offset, Pred, PredReg,
486 static_cast<const ARMBaseInstrInfo&>(TII));
487 }
488 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000489
Eric Christopher318b6ee2010-09-02 00:53:56 +0000490 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000491}
492
Eric Christopher30b66332010-09-08 21:49:50 +0000493bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000494 Value *Op0 = I->getOperand(0);
495
496 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000497 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
498 DenseMap<const AllocaInst*, int>::iterator SI =
499 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000500
Eric Christophere24d66f2010-08-24 22:07:27 +0000501 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000502 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000503 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000504 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000505 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000506 TM.getRegisterInfo());
507 UpdateValueMap(I, ResultReg);
508 return true;
509 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000510 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000511 return false;
512}
513
Eric Christopherb1cc8482010-08-25 07:23:49 +0000514bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
515 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000516
Eric Christopherb1cc8482010-08-25 07:23:49 +0000517 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000518 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000519
Eric Christopherb1cc8482010-08-25 07:23:49 +0000520 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000521 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000522 assert(false && "Trying to emit for an unhandled type!");
523 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000524 case MVT::i16:
525 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
526 VT = MVT::i32;
527 break;
528 case MVT::i8:
529 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
530 VT = MVT::i32;
531 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000532 case MVT::i32:
533 Opc = isThumb ? ARM::tLDR : ARM::LDR;
534 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000535 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000536
Eric Christopherdc908042010-08-31 01:28:42 +0000537 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000538
Eric Christopherdc908042010-08-31 01:28:42 +0000539 // TODO: Fix the Addressing modes so that these can share some code.
540 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
541 if (isThumb)
542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
543 TII.get(Opc), ResultReg)
544 .addReg(Reg).addImm(Offset).addReg(0));
545 else
546 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
547 TII.get(Opc), ResultReg)
548 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000549 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550}
551
Eric Christopher30b66332010-09-08 21:49:50 +0000552bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000553 Value *Op1 = I->getOperand(1);
554
555 // Verify it's an alloca.
556 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
557 DenseMap<const AllocaInst*, int>::iterator SI =
558 FuncInfo.StaticAllocaMap.find(AI);
559
560 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000561 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000562 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000563 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000564 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000565 TM.getRegisterInfo());
566 return true;
567 }
568 }
569 return false;
570}
571
Eric Christopher318b6ee2010-09-02 00:53:56 +0000572bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
573 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000574 unsigned StrOpc;
575 switch (VT.getSimpleVT().SimpleTy) {
576 default: return false;
577 case MVT::i1:
578 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
579 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
580 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000581 case MVT::f32:
582 if (!Subtarget->hasVFP2()) return false;
583 StrOpc = ARM::VSTRS;
584 break;
585 case MVT::f64:
586 if (!Subtarget->hasVFP2()) return false;
587 StrOpc = ARM::VSTRD;
588 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000589 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000590
Eric Christopher318b6ee2010-09-02 00:53:56 +0000591 if (isThumb)
592 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
593 TII.get(StrOpc), SrcReg)
594 .addReg(DstReg).addImm(Offset).addReg(0));
595 else
596 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
597 TII.get(StrOpc), SrcReg)
598 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000599
Eric Christopher318b6ee2010-09-02 00:53:56 +0000600 return true;
601}
602
603bool ARMFastISel::ARMSelectStore(const Instruction *I) {
604 Value *Op0 = I->getOperand(0);
605 unsigned SrcReg = 0;
606
Eric Christopher543cf052010-09-01 22:16:27 +0000607 // Yay type legalization
608 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000609 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000610 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000611
Eric Christopher1b61ef42010-09-02 01:48:11 +0000612 // Get the value to be stored into a register.
613 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000614 if (SrcReg == 0)
615 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000616
Eric Christopher318b6ee2010-09-02 00:53:56 +0000617 // If we're an alloca we know we have a frame index and can emit the store
618 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000619 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000620 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000621
Eric Christopher318b6ee2010-09-02 00:53:56 +0000622 // Our register and offset with innocuous defaults.
623 unsigned Reg = 0;
624 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000625
Eric Christopher318b6ee2010-09-02 00:53:56 +0000626 // See if we can handle this as Reg + Offset
627 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
628 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000629
Eric Christopher318b6ee2010-09-02 00:53:56 +0000630 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000631
Eric Christopher543cf052010-09-01 22:16:27 +0000632 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000633}
634
Eric Christopher83007122010-08-23 21:44:12 +0000635bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000636 // Verify we have a legal type before going any further.
637 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000638 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000639 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000640
Eric Christopher30b66332010-09-08 21:49:50 +0000641 // If we're an alloca we know we have a frame index and can emit the load
642 // directly in short order.
643 if (ARMLoadAlloca(I, VT))
644 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000645
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000646 // Our register and offset with innocuous defaults.
647 unsigned Reg = 0;
648 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000649
Eric Christopher83007122010-08-23 21:44:12 +0000650 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000651 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000652 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000653
Eric Christopherb1cc8482010-08-25 07:23:49 +0000654 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000655 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000656
Eric Christopherf06f3092010-08-24 00:50:47 +0000657 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000658 return true;
659}
660
Eric Christophere5734102010-09-03 00:35:47 +0000661bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
662 const BranchInst *BI = cast<BranchInst>(I);
663 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
664 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000665
Eric Christophere5734102010-09-03 00:35:47 +0000666 // Simple branch support.
667 unsigned CondReg = getRegForValue(BI->getCondition());
668 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000669
Eric Christophere5734102010-09-03 00:35:47 +0000670 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
671 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
672 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
673 .addReg(CondReg).addReg(CondReg));
674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
675 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
676 FastEmitBranch(FBB, DL);
677 FuncInfo.MBB->addSuccessor(TBB);
678 return true;
679}
680
Eric Christopherd43393a2010-09-08 23:13:45 +0000681bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
682 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christopherd43393a2010-09-08 23:13:45 +0000684 EVT VT;
685 const Type *Ty = CI->getOperand(0)->getType();
686 if (!isTypeLegal(Ty, VT))
687 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000688
Eric Christopherd43393a2010-09-08 23:13:45 +0000689 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
690 if (isFloat && !Subtarget->hasVFP2())
691 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000692
Eric Christopherd43393a2010-09-08 23:13:45 +0000693 unsigned CmpOpc;
694 switch (VT.getSimpleVT().SimpleTy) {
695 default: return false;
696 // TODO: Verify compares.
697 case MVT::f32:
698 CmpOpc = ARM::VCMPES;
699 break;
700 case MVT::f64:
701 CmpOpc = ARM::VCMPED;
702 break;
703 case MVT::i32:
704 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
705 break;
706 }
707
708 unsigned Arg1 = getRegForValue(CI->getOperand(0));
709 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000710
Eric Christopherd43393a2010-09-08 23:13:45 +0000711 unsigned Arg2 = getRegForValue(CI->getOperand(1));
712 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000713
Eric Christopherd43393a2010-09-08 23:13:45 +0000714 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
715 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000716
Eric Christopherd43393a2010-09-08 23:13:45 +0000717 // For floating point we need to move the result to a register we can
718 // actually do something with.
719 if (isFloat)
720 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
721 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000722
723 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000724 return true;
725}
726
Eric Christopher46203602010-09-09 00:26:48 +0000727bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
728 // Make sure we have VFP and that we're extending float to double.
729 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000730
Eric Christopher46203602010-09-09 00:26:48 +0000731 Value *V = I->getOperand(0);
732 if (!I->getType()->isDoubleTy() ||
733 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000734
Eric Christopher46203602010-09-09 00:26:48 +0000735 unsigned Op = getRegForValue(V);
736 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000737
Eric Christopher46203602010-09-09 00:26:48 +0000738 unsigned Result = createResultReg(ARM::DPRRegisterClass);
739
Eric Christopherac1a19e2010-09-09 01:06:51 +0000740 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000741 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000742 .addReg(Op));
743 UpdateValueMap(I, Result);
744 return true;
745}
746
747bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
748 // Make sure we have VFP and that we're truncating double to float.
749 if (!Subtarget->hasVFP2()) return false;
750
751 Value *V = I->getOperand(0);
752 if (!I->getType()->isFloatTy() ||
753 !V->getType()->isDoubleTy()) return false;
754
755 unsigned Op = getRegForValue(V);
756 if (Op == 0) return false;
757
758 unsigned Result = createResultReg(ARM::SPRRegisterClass);
759
760 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000761 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000762 .addReg(Op));
763 UpdateValueMap(I, Result);
764 return true;
765}
766
Eric Christopher9a040492010-09-09 18:54:59 +0000767bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
768 // Make sure we have VFP.
769 if (!Subtarget->hasVFP2()) return false;
770
771 EVT VT;
772 const Type *Ty = I->getType();
773 if (!isTypeLegal(Ty, VT))
774 return false;
775
776 unsigned Op = getRegForValue(I->getOperand(0));
777 if (Op == 0) return false;
778
779 unsigned Opc;
780 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
781 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
782 else return 0;
783
784 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
785 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
786 ResultReg)
787 .addReg(Op));
Eric Christopherce07b542010-09-09 20:26:31 +0000788 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000789 return true;
790}
791
792bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
793 // Make sure we have VFP.
794 if (!Subtarget->hasVFP2()) return false;
795
796 EVT VT;
797 const Type *RetTy = I->getType();
798 if (!isTypeLegal(RetTy, VT))
799 return false;
800
801 unsigned Op = getRegForValue(I->getOperand(0));
802 if (Op == 0) return false;
803
804 unsigned Opc;
805 const Type *OpTy = I->getOperand(0)->getType();
806 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
807 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
808 else return 0;
809
810 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
811 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
812 ResultReg)
813 .addReg(Op));
Eric Christopherce07b542010-09-09 20:26:31 +0000814 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000815 return true;
816}
817
Eric Christopherbc39b822010-09-09 00:53:57 +0000818bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000819 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000820
Eric Christopherbc39b822010-09-09 00:53:57 +0000821 // We can get here in the case when we want to use NEON for our fp
822 // operations, but can't figure out how to. Just use the vfp instructions
823 // if we have them.
824 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000825 const Type *Ty = I->getType();
826 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
827 if (isFloat && !Subtarget->hasVFP2())
828 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000829
Eric Christopherbc39b822010-09-09 00:53:57 +0000830 unsigned Op1 = getRegForValue(I->getOperand(0));
831 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000832
Eric Christopherbc39b822010-09-09 00:53:57 +0000833 unsigned Op2 = getRegForValue(I->getOperand(1));
834 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000835
Eric Christopherbc39b822010-09-09 00:53:57 +0000836 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000837 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
838 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000839 switch (ISDOpcode) {
840 default: return false;
841 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000842 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000843 break;
844 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000845 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000846 break;
847 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000848 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000849 break;
850 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000851 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000852 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
853 TII.get(Opc), ResultReg)
854 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000855 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000856 return true;
857}
858
Eric Christopher56d2b722010-09-02 23:43:26 +0000859// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000860bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000861 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000862 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000863
Eric Christopherab695882010-07-21 22:26:11 +0000864 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000865 case Instruction::Load:
866 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000867 case Instruction::Store:
868 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000869 case Instruction::Br:
870 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000871 case Instruction::ICmp:
872 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000873 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000874 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000875 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +0000876 case Instruction::FPTrunc:
877 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000878 case Instruction::SIToFP:
879 return ARMSelectSIToFP(I);
880 case Instruction::FPToSI:
881 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000882 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000883 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000884 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000885 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000886 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000887 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000888 default: break;
889 }
890 return false;
891}
892
893namespace llvm {
894 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000895 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000896 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000897 }
898}