blob: 3bd0f6a17af414d0f6d2f0aadd82a67d2ae464d8 [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 Christopheraa3ace12010-09-09 20:49:25 +0000134 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000135
Eric Christopher456144e2010-08-19 00:37:05 +0000136 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
137 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
138};
Eric Christopherab695882010-07-21 22:26:11 +0000139
140} // end anonymous namespace
141
142// #include "ARMGenCallingConv.inc"
143
Eric Christopher456144e2010-08-19 00:37:05 +0000144// DefinesOptionalPredicate - This is different from DefinesPredicate in that
145// we don't care about implicit defs here, just places we'll need to add a
146// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
147bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
148 const TargetInstrDesc &TID = MI->getDesc();
149 if (!TID.hasOptionalDef())
150 return false;
151
152 // Look to see if our OptionalDef is defining CPSR or CCR.
153 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
154 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000155 if (!MO.isReg() || !MO.isDef()) continue;
156 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000157 *CPSR = true;
158 }
159 return true;
160}
161
162// If the machine is predicable go ahead and add the predicate operands, if
163// it needs default CC operands add those.
164const MachineInstrBuilder &
165ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
166 MachineInstr *MI = &*MIB;
167
168 // Do we use a predicate?
169 if (TII.isPredicable(MI))
170 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000171
Eric Christopher456144e2010-08-19 00:37:05 +0000172 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
173 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000174 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000175 if (DefinesOptionalPredicate(MI, &CPSR)) {
176 if (CPSR)
177 AddDefaultT1CC(MIB);
178 else
179 AddDefaultCC(MIB);
180 }
181 return MIB;
182}
183
Eric Christopher0fe7d542010-08-17 01:25:29 +0000184unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
185 const TargetRegisterClass* RC) {
186 unsigned ResultReg = createResultReg(RC);
187 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
188
Eric Christopher456144e2010-08-19 00:37:05 +0000189 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000190 return ResultReg;
191}
192
193unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
194 const TargetRegisterClass *RC,
195 unsigned Op0, bool Op0IsKill) {
196 unsigned ResultReg = createResultReg(RC);
197 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
198
199 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000200 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000201 .addReg(Op0, Op0IsKill * RegState::Kill));
202 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000203 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000204 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000205 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000206 TII.get(TargetOpcode::COPY), ResultReg)
207 .addReg(II.ImplicitDefs[0]));
208 }
209 return ResultReg;
210}
211
212unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
213 const TargetRegisterClass *RC,
214 unsigned Op0, bool Op0IsKill,
215 unsigned Op1, bool Op1IsKill) {
216 unsigned ResultReg = createResultReg(RC);
217 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
218
219 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000220 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000221 .addReg(Op0, Op0IsKill * RegState::Kill)
222 .addReg(Op1, Op1IsKill * RegState::Kill));
223 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000225 .addReg(Op0, Op0IsKill * RegState::Kill)
226 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000227 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000228 TII.get(TargetOpcode::COPY), ResultReg)
229 .addReg(II.ImplicitDefs[0]));
230 }
231 return ResultReg;
232}
233
234unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
235 const TargetRegisterClass *RC,
236 unsigned Op0, bool Op0IsKill,
237 uint64_t Imm) {
238 unsigned ResultReg = createResultReg(RC);
239 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
240
241 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000243 .addReg(Op0, Op0IsKill * RegState::Kill)
244 .addImm(Imm));
245 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000246 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000247 .addReg(Op0, Op0IsKill * RegState::Kill)
248 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000250 TII.get(TargetOpcode::COPY), ResultReg)
251 .addReg(II.ImplicitDefs[0]));
252 }
253 return ResultReg;
254}
255
256unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
257 const TargetRegisterClass *RC,
258 unsigned Op0, bool Op0IsKill,
259 const ConstantFP *FPImm) {
260 unsigned ResultReg = createResultReg(RC);
261 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
262
263 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000265 .addReg(Op0, Op0IsKill * RegState::Kill)
266 .addFPImm(FPImm));
267 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000269 .addReg(Op0, Op0IsKill * RegState::Kill)
270 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000271 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000272 TII.get(TargetOpcode::COPY), ResultReg)
273 .addReg(II.ImplicitDefs[0]));
274 }
275 return ResultReg;
276}
277
278unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
279 const TargetRegisterClass *RC,
280 unsigned Op0, bool Op0IsKill,
281 unsigned Op1, bool Op1IsKill,
282 uint64_t Imm) {
283 unsigned ResultReg = createResultReg(RC);
284 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
285
286 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 .addReg(Op0, Op0IsKill * RegState::Kill)
289 .addReg(Op1, Op1IsKill * RegState::Kill)
290 .addImm(Imm));
291 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addReg(Op0, Op0IsKill * RegState::Kill)
294 .addReg(Op1, Op1IsKill * RegState::Kill)
295 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000297 TII.get(TargetOpcode::COPY), ResultReg)
298 .addReg(II.ImplicitDefs[0]));
299 }
300 return ResultReg;
301}
302
303unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
304 const TargetRegisterClass *RC,
305 uint64_t Imm) {
306 unsigned ResultReg = createResultReg(RC);
307 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000308
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 .addImm(Imm));
312 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000314 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000316 TII.get(TargetOpcode::COPY), ResultReg)
317 .addReg(II.ImplicitDefs[0]));
318 }
319 return ResultReg;
320}
321
322unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
323 unsigned Op0, bool Op0IsKill,
324 uint32_t Idx) {
325 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
326 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
327 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000329 DL, TII.get(TargetOpcode::COPY), ResultReg)
330 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
331 return ResultReg;
332}
333
Eric Christopheraa3ace12010-09-09 20:49:25 +0000334unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
335 // If we have a floating point constant we expect it in a floating point
336 // register.
337 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
338 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
339 TII.get(ARM::VMOVRS), MoveReg)
340 .addReg(SrcReg));
341 return MoveReg;
342}
343
Eric Christopher9ed58df2010-09-09 00:19:41 +0000344// For double width floating point we need to materialize two constants
345// (the high and the low) into integer registers then use a move to get
346// the combined constant into an FP reg.
347unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
348 const APFloat Val = CFP->getValueAPF();
349 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000350
Eric Christopher9ed58df2010-09-09 00:19:41 +0000351 // This checks to see if we can use VFP3 instructions to materialize
352 // a constant, otherwise we have to go through the constant pool.
353 if (TLI.isFPImmLegal(Val, VT)) {
354 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
355 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
357 DestReg)
358 .addFPImm(CFP));
359 return DestReg;
360 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000361
Eric Christopher9ed58df2010-09-09 00:19:41 +0000362 // No 64-bit at the moment.
363 if (is64bit) return 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000364
Eric Christopher9ed58df2010-09-09 00:19:41 +0000365 // Load this from the constant pool.
366 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000367
Eric Christopher9ed58df2010-09-09 00:19:41 +0000368 // If we have a floating point constant we expect it in a floating point
369 // register.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000370 return ARMMoveToFPReg(VT, DestReg);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000371}
372
373unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000374 // MachineConstantPool wants an explicit alignment.
375 unsigned Align = TD.getPrefTypeAlignment(C->getType());
376 if (Align == 0) {
377 // TODO: Figure out if this is correct.
378 Align = TD.getTypeAllocSize(C->getType());
379 }
380 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
381
Eric Christopher845c5752010-09-08 18:56:34 +0000382 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000383 if (isThumb)
384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
385 TII.get(ARM::t2LDRpci))
386 .addReg(DestReg).addConstantPoolIndex(Idx));
387 else
388 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
389 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000390 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000391 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000392
Eric Christopher56d2b722010-09-02 23:43:26 +0000393 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000394}
395
Eric Christopher9ed58df2010-09-09 00:19:41 +0000396unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
397 EVT VT = TLI.getValueType(C->getType(), true);
398
399 // Only handle simple types.
400 if (!VT.isSimple()) return 0;
401
402 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
403 return ARMMaterializeFP(CFP, VT);
404 return ARMMaterializeInt(C);
405}
406
Eric Christopherb1cc8482010-08-25 07:23:49 +0000407bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
408 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000409
Eric Christopherb1cc8482010-08-25 07:23:49 +0000410 // Only handle simple types.
411 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000412
Eric Christopherdc908042010-08-31 01:28:42 +0000413 // Handle all legal types, i.e. a register that will directly hold this
414 // value.
415 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000416}
417
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000418bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
419 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000420
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000421 // If this is a type than can be sign or zero-extended to a basic operation
422 // go ahead and accept it now.
423 if (VT == MVT::i8 || VT == MVT::i16)
424 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000425
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000426 return false;
427}
428
Eric Christophercb0b04b2010-08-24 00:07:24 +0000429// Computes the Reg+Offset to get to an object.
430bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000431 int &Offset) {
432 // Some boilerplate from the X86 FastISel.
433 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000434 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000435 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000436 // Don't walk into other basic blocks; it's possible we haven't
437 // visited them yet, so the instructions may not yet be assigned
438 // virtual registers.
439 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
440 return false;
441
442 Opcode = I->getOpcode();
443 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000444 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000445 Opcode = C->getOpcode();
446 U = C;
447 }
448
Eric Christophercb0b04b2010-08-24 00:07:24 +0000449 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000450 if (Ty->getAddressSpace() > 255)
451 // Fast instruction selection doesn't support the special
452 // address spaces.
453 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000454
Eric Christopher83007122010-08-23 21:44:12 +0000455 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000456 default:
Eric Christopher83007122010-08-23 21:44:12 +0000457 //errs() << "Failing Opcode is: " << *Op1 << "\n";
458 break;
459 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000460 assert(false && "Alloca should have been handled earlier!");
461 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000462 }
463 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000464
Eric Christophercb0b04b2010-08-24 00:07:24 +0000465 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
466 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000467 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000468 return false;
469 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000470
Eric Christophercb0b04b2010-08-24 00:07:24 +0000471 // Try to get this in a register if nothing else has worked.
472 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000473 if (Reg == 0) return false;
474
475 // Since the offset may be too large for the load instruction
476 // get the reg+offset into a register.
477 // TODO: Verify the additions work, otherwise we'll need to add the
478 // offset instead of 0 to the instructions and do all sorts of operand
479 // munging.
480 // TODO: Optimize this somewhat.
481 if (Offset != 0) {
482 ARMCC::CondCodes Pred = ARMCC::AL;
483 unsigned PredReg = 0;
484
Eric Christophereaa204b2010-09-02 01:39:14 +0000485 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000486 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
487 Reg, Reg, Offset, Pred, PredReg,
488 static_cast<const ARMBaseInstrInfo&>(TII));
489 else {
490 assert(AFI->isThumb2Function());
491 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
492 Reg, Reg, Offset, Pred, PredReg,
493 static_cast<const ARMBaseInstrInfo&>(TII));
494 }
495 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000496
Eric Christopher318b6ee2010-09-02 00:53:56 +0000497 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000498}
499
Eric Christopher30b66332010-09-08 21:49:50 +0000500bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000501 Value *Op0 = I->getOperand(0);
502
503 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000504 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
505 DenseMap<const AllocaInst*, int>::iterator SI =
506 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000507
Eric Christophere24d66f2010-08-24 22:07:27 +0000508 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000509 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000510 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000511 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000512 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000513 TM.getRegisterInfo());
514 UpdateValueMap(I, ResultReg);
515 return true;
516 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000517 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000518 return false;
519}
520
Eric Christopherb1cc8482010-08-25 07:23:49 +0000521bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
522 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000523
Eric Christopherb1cc8482010-08-25 07:23:49 +0000524 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000525 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000526
Eric Christopherb1cc8482010-08-25 07:23:49 +0000527 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000528 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000529 assert(false && "Trying to emit for an unhandled type!");
530 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000531 case MVT::i16:
532 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
533 VT = MVT::i32;
534 break;
535 case MVT::i8:
536 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
537 VT = MVT::i32;
538 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000539 case MVT::i32:
540 Opc = isThumb ? ARM::tLDR : ARM::LDR;
541 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000542 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000543
Eric Christopherdc908042010-08-31 01:28:42 +0000544 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000545
Eric Christopherdc908042010-08-31 01:28:42 +0000546 // TODO: Fix the Addressing modes so that these can share some code.
547 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
548 if (isThumb)
549 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
550 TII.get(Opc), ResultReg)
551 .addReg(Reg).addImm(Offset).addReg(0));
552 else
553 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
554 TII.get(Opc), ResultReg)
555 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000556 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000557}
558
Eric Christopher30b66332010-09-08 21:49:50 +0000559bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000560 Value *Op1 = I->getOperand(1);
561
562 // Verify it's an alloca.
563 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
564 DenseMap<const AllocaInst*, int>::iterator SI =
565 FuncInfo.StaticAllocaMap.find(AI);
566
567 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000568 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000569 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000570 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000571 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000572 TM.getRegisterInfo());
573 return true;
574 }
575 }
576 return false;
577}
578
Eric Christopher318b6ee2010-09-02 00:53:56 +0000579bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
580 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000581 unsigned StrOpc;
582 switch (VT.getSimpleVT().SimpleTy) {
583 default: return false;
584 case MVT::i1:
585 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
586 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
587 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000588 case MVT::f32:
589 if (!Subtarget->hasVFP2()) return false;
590 StrOpc = ARM::VSTRS;
591 break;
592 case MVT::f64:
593 if (!Subtarget->hasVFP2()) return false;
594 StrOpc = ARM::VSTRD;
595 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000596 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000597
Eric Christopher318b6ee2010-09-02 00:53:56 +0000598 if (isThumb)
599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
600 TII.get(StrOpc), SrcReg)
601 .addReg(DstReg).addImm(Offset).addReg(0));
602 else
603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
604 TII.get(StrOpc), SrcReg)
605 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000606
Eric Christopher318b6ee2010-09-02 00:53:56 +0000607 return true;
608}
609
610bool ARMFastISel::ARMSelectStore(const Instruction *I) {
611 Value *Op0 = I->getOperand(0);
612 unsigned SrcReg = 0;
613
Eric Christopher543cf052010-09-01 22:16:27 +0000614 // Yay type legalization
615 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000616 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000617 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000618
Eric Christopher1b61ef42010-09-02 01:48:11 +0000619 // Get the value to be stored into a register.
620 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000621 if (SrcReg == 0)
622 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000623
Eric Christopher318b6ee2010-09-02 00:53:56 +0000624 // If we're an alloca we know we have a frame index and can emit the store
625 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000626 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000627 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000628
Eric Christopher318b6ee2010-09-02 00:53:56 +0000629 // Our register and offset with innocuous defaults.
630 unsigned Reg = 0;
631 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000632
Eric Christopher318b6ee2010-09-02 00:53:56 +0000633 // See if we can handle this as Reg + Offset
634 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
635 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000636
Eric Christopher318b6ee2010-09-02 00:53:56 +0000637 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000638
Eric Christopher543cf052010-09-01 22:16:27 +0000639 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000640}
641
Eric Christopher83007122010-08-23 21:44:12 +0000642bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000643 // Verify we have a legal type before going any further.
644 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000645 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000646 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000647
Eric Christopher30b66332010-09-08 21:49:50 +0000648 // If we're an alloca we know we have a frame index and can emit the load
649 // directly in short order.
650 if (ARMLoadAlloca(I, VT))
651 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000652
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000653 // Our register and offset with innocuous defaults.
654 unsigned Reg = 0;
655 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000656
Eric Christopher83007122010-08-23 21:44:12 +0000657 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000658 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000659 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000660
Eric Christopherb1cc8482010-08-25 07:23:49 +0000661 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000662 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000663
Eric Christopherf06f3092010-08-24 00:50:47 +0000664 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000665 return true;
666}
667
Eric Christophere5734102010-09-03 00:35:47 +0000668bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
669 const BranchInst *BI = cast<BranchInst>(I);
670 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
671 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000672
Eric Christophere5734102010-09-03 00:35:47 +0000673 // Simple branch support.
674 unsigned CondReg = getRegForValue(BI->getCondition());
675 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000676
Eric Christophere5734102010-09-03 00:35:47 +0000677 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
678 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
679 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
680 .addReg(CondReg).addReg(CondReg));
681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
682 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
683 FastEmitBranch(FBB, DL);
684 FuncInfo.MBB->addSuccessor(TBB);
685 return true;
686}
687
Eric Christopherd43393a2010-09-08 23:13:45 +0000688bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
689 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000690
Eric Christopherd43393a2010-09-08 23:13:45 +0000691 EVT VT;
692 const Type *Ty = CI->getOperand(0)->getType();
693 if (!isTypeLegal(Ty, VT))
694 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000695
Eric Christopherd43393a2010-09-08 23:13:45 +0000696 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
697 if (isFloat && !Subtarget->hasVFP2())
698 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000699
Eric Christopherd43393a2010-09-08 23:13:45 +0000700 unsigned CmpOpc;
701 switch (VT.getSimpleVT().SimpleTy) {
702 default: return false;
703 // TODO: Verify compares.
704 case MVT::f32:
705 CmpOpc = ARM::VCMPES;
706 break;
707 case MVT::f64:
708 CmpOpc = ARM::VCMPED;
709 break;
710 case MVT::i32:
711 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
712 break;
713 }
714
715 unsigned Arg1 = getRegForValue(CI->getOperand(0));
716 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000717
Eric Christopherd43393a2010-09-08 23:13:45 +0000718 unsigned Arg2 = getRegForValue(CI->getOperand(1));
719 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000720
Eric Christopherd43393a2010-09-08 23:13:45 +0000721 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
722 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000723
Eric Christopherd43393a2010-09-08 23:13:45 +0000724 // For floating point we need to move the result to a register we can
725 // actually do something with.
726 if (isFloat)
727 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
728 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000729
730 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000731 return true;
732}
733
Eric Christopher46203602010-09-09 00:26:48 +0000734bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
735 // Make sure we have VFP and that we're extending float to double.
736 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000737
Eric Christopher46203602010-09-09 00:26:48 +0000738 Value *V = I->getOperand(0);
739 if (!I->getType()->isDoubleTy() ||
740 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000741
Eric Christopher46203602010-09-09 00:26:48 +0000742 unsigned Op = getRegForValue(V);
743 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000744
Eric Christopher46203602010-09-09 00:26:48 +0000745 unsigned Result = createResultReg(ARM::DPRRegisterClass);
746
Eric Christopherac1a19e2010-09-09 01:06:51 +0000747 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000748 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000749 .addReg(Op));
750 UpdateValueMap(I, Result);
751 return true;
752}
753
754bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
755 // Make sure we have VFP and that we're truncating double to float.
756 if (!Subtarget->hasVFP2()) return false;
757
758 Value *V = I->getOperand(0);
759 if (!I->getType()->isFloatTy() ||
760 !V->getType()->isDoubleTy()) return false;
761
762 unsigned Op = getRegForValue(V);
763 if (Op == 0) return false;
764
765 unsigned Result = createResultReg(ARM::SPRRegisterClass);
766
767 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000768 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000769 .addReg(Op));
770 UpdateValueMap(I, Result);
771 return true;
772}
773
Eric Christopher9a040492010-09-09 18:54:59 +0000774bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
775 // Make sure we have VFP.
776 if (!Subtarget->hasVFP2()) return false;
777
778 EVT VT;
779 const Type *Ty = I->getType();
780 if (!isTypeLegal(Ty, VT))
781 return false;
782
783 unsigned Op = getRegForValue(I->getOperand(0));
784 if (Op == 0) return false;
785
786 unsigned Opc;
787 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
788 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
789 else return 0;
790
791 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
792 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
793 ResultReg)
794 .addReg(Op));
Eric Christopherce07b542010-09-09 20:26:31 +0000795 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000796 return true;
797}
798
799bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
800 // Make sure we have VFP.
801 if (!Subtarget->hasVFP2()) return false;
802
803 EVT VT;
804 const Type *RetTy = I->getType();
805 if (!isTypeLegal(RetTy, VT))
806 return false;
807
808 unsigned Op = getRegForValue(I->getOperand(0));
809 if (Op == 0) return false;
810
811 unsigned Opc;
812 const Type *OpTy = I->getOperand(0)->getType();
813 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
814 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
815 else return 0;
816
817 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
818 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
819 ResultReg)
820 .addReg(Op));
Eric Christopherce07b542010-09-09 20:26:31 +0000821 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000822 return true;
823}
824
Eric Christopherbc39b822010-09-09 00:53:57 +0000825bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000826 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000827
Eric Christopherbc39b822010-09-09 00:53:57 +0000828 // We can get here in the case when we want to use NEON for our fp
829 // operations, but can't figure out how to. Just use the vfp instructions
830 // if we have them.
831 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000832 const Type *Ty = I->getType();
833 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
834 if (isFloat && !Subtarget->hasVFP2())
835 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000836
Eric Christopherbc39b822010-09-09 00:53:57 +0000837 unsigned Op1 = getRegForValue(I->getOperand(0));
838 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000839
Eric Christopherbc39b822010-09-09 00:53:57 +0000840 unsigned Op2 = getRegForValue(I->getOperand(1));
841 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000842
Eric Christopherbc39b822010-09-09 00:53:57 +0000843 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000844 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
845 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000846 switch (ISDOpcode) {
847 default: return false;
848 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000849 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000850 break;
851 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000852 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000853 break;
854 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000855 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000856 break;
857 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000858 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
860 TII.get(Opc), ResultReg)
861 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000862 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000863 return true;
864}
865
Eric Christopher56d2b722010-09-02 23:43:26 +0000866// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000867bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000868 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000869 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000870
Eric Christopherab695882010-07-21 22:26:11 +0000871 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000872 case Instruction::Load:
873 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000874 case Instruction::Store:
875 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000876 case Instruction::Br:
877 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000878 case Instruction::ICmp:
879 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000880 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000881 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000882 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +0000883 case Instruction::FPTrunc:
884 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000885 case Instruction::SIToFP:
886 return ARMSelectSIToFP(I);
887 case Instruction::FPToSI:
888 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000889 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000890 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000891 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000892 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000893 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000894 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000895 default: break;
896 }
897 return false;
898}
899
900namespace llvm {
901 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000902 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000903 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000904 }
905}