blob: 7af9fc5291c06d2557afe33b5b38fca319cbf099 [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 Christopherbc39b822010-09-09 00:53:57 +0000118 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopher9a040492010-09-09 18:54:59 +0000119 virtual bool ARMSelectSIToFP(const Instruction *I);
120 virtual bool ARMSelectFPToSI(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000121
Eric Christopher83007122010-08-23 21:44:12 +0000122 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000123 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000124 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000125 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000126 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000127 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000128 bool ARMLoadAlloca(const Instruction *I, EVT VT);
129 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000130 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000131 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
132 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000133
Eric Christopher456144e2010-08-19 00:37:05 +0000134 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
135 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
136};
Eric Christopherab695882010-07-21 22:26:11 +0000137
138} // end anonymous namespace
139
140// #include "ARMGenCallingConv.inc"
141
Eric Christopher456144e2010-08-19 00:37:05 +0000142// DefinesOptionalPredicate - This is different from DefinesPredicate in that
143// we don't care about implicit defs here, just places we'll need to add a
144// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
145bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
146 const TargetInstrDesc &TID = MI->getDesc();
147 if (!TID.hasOptionalDef())
148 return false;
149
150 // Look to see if our OptionalDef is defining CPSR or CCR.
151 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
152 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000153 if (!MO.isReg() || !MO.isDef()) continue;
154 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000155 *CPSR = true;
156 }
157 return true;
158}
159
160// If the machine is predicable go ahead and add the predicate operands, if
161// it needs default CC operands add those.
162const MachineInstrBuilder &
163ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
164 MachineInstr *MI = &*MIB;
165
166 // Do we use a predicate?
167 if (TII.isPredicable(MI))
168 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000169
Eric Christopher456144e2010-08-19 00:37:05 +0000170 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
171 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000172 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000173 if (DefinesOptionalPredicate(MI, &CPSR)) {
174 if (CPSR)
175 AddDefaultT1CC(MIB);
176 else
177 AddDefaultCC(MIB);
178 }
179 return MIB;
180}
181
Eric Christopher0fe7d542010-08-17 01:25:29 +0000182unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
183 const TargetRegisterClass* RC) {
184 unsigned ResultReg = createResultReg(RC);
185 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
186
Eric Christopher456144e2010-08-19 00:37:05 +0000187 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000188 return ResultReg;
189}
190
191unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
192 const TargetRegisterClass *RC,
193 unsigned Op0, bool Op0IsKill) {
194 unsigned ResultReg = createResultReg(RC);
195 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
196
197 if (II.getNumDefs() >= 1)
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 .addReg(Op0, Op0IsKill * RegState::Kill));
200 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000202 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000203 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000204 TII.get(TargetOpcode::COPY), ResultReg)
205 .addReg(II.ImplicitDefs[0]));
206 }
207 return ResultReg;
208}
209
210unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
211 const TargetRegisterClass *RC,
212 unsigned Op0, bool Op0IsKill,
213 unsigned Op1, bool Op1IsKill) {
214 unsigned ResultReg = createResultReg(RC);
215 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
216
217 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000218 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000219 .addReg(Op0, Op0IsKill * RegState::Kill)
220 .addReg(Op1, Op1IsKill * RegState::Kill));
221 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000222 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000223 .addReg(Op0, Op0IsKill * RegState::Kill)
224 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000225 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000226 TII.get(TargetOpcode::COPY), ResultReg)
227 .addReg(II.ImplicitDefs[0]));
228 }
229 return ResultReg;
230}
231
232unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
233 const TargetRegisterClass *RC,
234 unsigned Op0, bool Op0IsKill,
235 uint64_t Imm) {
236 unsigned ResultReg = createResultReg(RC);
237 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
238
239 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000241 .addReg(Op0, Op0IsKill * RegState::Kill)
242 .addImm(Imm));
243 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000245 .addReg(Op0, Op0IsKill * RegState::Kill)
246 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000247 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000248 TII.get(TargetOpcode::COPY), ResultReg)
249 .addReg(II.ImplicitDefs[0]));
250 }
251 return ResultReg;
252}
253
254unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
255 const TargetRegisterClass *RC,
256 unsigned Op0, bool Op0IsKill,
257 const ConstantFP *FPImm) {
258 unsigned ResultReg = createResultReg(RC);
259 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
260
261 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000262 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000263 .addReg(Op0, Op0IsKill * RegState::Kill)
264 .addFPImm(FPImm));
265 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000267 .addReg(Op0, Op0IsKill * RegState::Kill)
268 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000270 TII.get(TargetOpcode::COPY), ResultReg)
271 .addReg(II.ImplicitDefs[0]));
272 }
273 return ResultReg;
274}
275
276unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
277 const TargetRegisterClass *RC,
278 unsigned Op0, bool Op0IsKill,
279 unsigned Op1, bool Op1IsKill,
280 uint64_t Imm) {
281 unsigned ResultReg = createResultReg(RC);
282 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
283
284 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000285 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000286 .addReg(Op0, Op0IsKill * RegState::Kill)
287 .addReg(Op1, Op1IsKill * RegState::Kill)
288 .addImm(Imm));
289 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000290 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000291 .addReg(Op0, Op0IsKill * RegState::Kill)
292 .addReg(Op1, Op1IsKill * RegState::Kill)
293 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 TII.get(TargetOpcode::COPY), ResultReg)
296 .addReg(II.ImplicitDefs[0]));
297 }
298 return ResultReg;
299}
300
301unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
302 const TargetRegisterClass *RC,
303 uint64_t Imm) {
304 unsigned ResultReg = createResultReg(RC);
305 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000306
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addImm(Imm));
310 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000313 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000314 TII.get(TargetOpcode::COPY), ResultReg)
315 .addReg(II.ImplicitDefs[0]));
316 }
317 return ResultReg;
318}
319
320unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
321 unsigned Op0, bool Op0IsKill,
322 uint32_t Idx) {
323 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
324 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
325 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000327 DL, TII.get(TargetOpcode::COPY), ResultReg)
328 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
329 return ResultReg;
330}
331
Eric Christopher9ed58df2010-09-09 00:19:41 +0000332// For double width floating point we need to materialize two constants
333// (the high and the low) into integer registers then use a move to get
334// the combined constant into an FP reg.
335unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
336 const APFloat Val = CFP->getValueAPF();
337 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000338
Eric Christopher9ed58df2010-09-09 00:19:41 +0000339 // This checks to see if we can use VFP3 instructions to materialize
340 // a constant, otherwise we have to go through the constant pool.
341 if (TLI.isFPImmLegal(Val, VT)) {
342 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
343 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
344 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
345 DestReg)
346 .addFPImm(CFP));
347 return DestReg;
348 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000349
Eric Christopher9ed58df2010-09-09 00:19:41 +0000350 // No 64-bit at the moment.
351 if (is64bit) return 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000352
Eric Christopher9ed58df2010-09-09 00:19:41 +0000353 // Load this from the constant pool.
354 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000355
Eric Christopher9ed58df2010-09-09 00:19:41 +0000356 // If we have a floating point constant we expect it in a floating point
357 // register.
358 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
360 TII.get(ARM::VMOVRS), MoveReg)
361 .addReg(DestReg));
362 return MoveReg;
363}
364
365unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000366 // MachineConstantPool wants an explicit alignment.
367 unsigned Align = TD.getPrefTypeAlignment(C->getType());
368 if (Align == 0) {
369 // TODO: Figure out if this is correct.
370 Align = TD.getTypeAllocSize(C->getType());
371 }
372 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
373
Eric Christopher845c5752010-09-08 18:56:34 +0000374 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000375 if (isThumb)
376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
377 TII.get(ARM::t2LDRpci))
378 .addReg(DestReg).addConstantPoolIndex(Idx));
379 else
380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
381 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000382 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000383 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000384
Eric Christopher56d2b722010-09-02 23:43:26 +0000385 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000386}
387
Eric Christopher9ed58df2010-09-09 00:19:41 +0000388unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
389 EVT VT = TLI.getValueType(C->getType(), true);
390
391 // Only handle simple types.
392 if (!VT.isSimple()) return 0;
393
394 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
395 return ARMMaterializeFP(CFP, VT);
396 return ARMMaterializeInt(C);
397}
398
Eric Christopherb1cc8482010-08-25 07:23:49 +0000399bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
400 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000401
Eric Christopherb1cc8482010-08-25 07:23:49 +0000402 // Only handle simple types.
403 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000404
Eric Christopherdc908042010-08-31 01:28:42 +0000405 // Handle all legal types, i.e. a register that will directly hold this
406 // value.
407 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000408}
409
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000410bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
411 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000412
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000413 // If this is a type than can be sign or zero-extended to a basic operation
414 // go ahead and accept it now.
415 if (VT == MVT::i8 || VT == MVT::i16)
416 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000417
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000418 return false;
419}
420
Eric Christophercb0b04b2010-08-24 00:07:24 +0000421// Computes the Reg+Offset to get to an object.
422bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000423 int &Offset) {
424 // Some boilerplate from the X86 FastISel.
425 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000426 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000427 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000428 // Don't walk into other basic blocks; it's possible we haven't
429 // visited them yet, so the instructions may not yet be assigned
430 // virtual registers.
431 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
432 return false;
433
434 Opcode = I->getOpcode();
435 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000436 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000437 Opcode = C->getOpcode();
438 U = C;
439 }
440
Eric Christophercb0b04b2010-08-24 00:07:24 +0000441 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000442 if (Ty->getAddressSpace() > 255)
443 // Fast instruction selection doesn't support the special
444 // address spaces.
445 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000446
Eric Christopher83007122010-08-23 21:44:12 +0000447 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000448 default:
Eric Christopher83007122010-08-23 21:44:12 +0000449 //errs() << "Failing Opcode is: " << *Op1 << "\n";
450 break;
451 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000452 assert(false && "Alloca should have been handled earlier!");
453 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000454 }
455 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000456
Eric Christophercb0b04b2010-08-24 00:07:24 +0000457 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
458 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000459 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000460 return false;
461 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000462
Eric Christophercb0b04b2010-08-24 00:07:24 +0000463 // Try to get this in a register if nothing else has worked.
464 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000465 if (Reg == 0) return false;
466
467 // Since the offset may be too large for the load instruction
468 // get the reg+offset into a register.
469 // TODO: Verify the additions work, otherwise we'll need to add the
470 // offset instead of 0 to the instructions and do all sorts of operand
471 // munging.
472 // TODO: Optimize this somewhat.
473 if (Offset != 0) {
474 ARMCC::CondCodes Pred = ARMCC::AL;
475 unsigned PredReg = 0;
476
Eric Christophereaa204b2010-09-02 01:39:14 +0000477 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000478 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
479 Reg, Reg, Offset, Pred, PredReg,
480 static_cast<const ARMBaseInstrInfo&>(TII));
481 else {
482 assert(AFI->isThumb2Function());
483 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
484 Reg, Reg, Offset, Pred, PredReg,
485 static_cast<const ARMBaseInstrInfo&>(TII));
486 }
487 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000488
Eric Christopher318b6ee2010-09-02 00:53:56 +0000489 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000490}
491
Eric Christopher30b66332010-09-08 21:49:50 +0000492bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000493 Value *Op0 = I->getOperand(0);
494
495 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000496 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
497 DenseMap<const AllocaInst*, int>::iterator SI =
498 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000499
Eric Christophere24d66f2010-08-24 22:07:27 +0000500 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000501 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000502 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000503 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000504 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000505 TM.getRegisterInfo());
506 UpdateValueMap(I, ResultReg);
507 return true;
508 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000509 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000510 return false;
511}
512
Eric Christopherb1cc8482010-08-25 07:23:49 +0000513bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
514 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000515
Eric Christopherb1cc8482010-08-25 07:23:49 +0000516 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000517 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000518
Eric Christopherb1cc8482010-08-25 07:23:49 +0000519 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000520 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000521 assert(false && "Trying to emit for an unhandled type!");
522 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000523 case MVT::i16:
524 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
525 VT = MVT::i32;
526 break;
527 case MVT::i8:
528 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
529 VT = MVT::i32;
530 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000531 case MVT::i32:
532 Opc = isThumb ? ARM::tLDR : ARM::LDR;
533 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000534 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000535
Eric Christopherdc908042010-08-31 01:28:42 +0000536 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000537
Eric Christopherdc908042010-08-31 01:28:42 +0000538 // TODO: Fix the Addressing modes so that these can share some code.
539 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
540 if (isThumb)
541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
542 TII.get(Opc), ResultReg)
543 .addReg(Reg).addImm(Offset).addReg(0));
544 else
545 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
546 TII.get(Opc), ResultReg)
547 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000548
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 Christopherac1a19e2010-09-09 01:06:51 +0000633
Eric Christopher543cf052010-09-01 22:16:27 +0000634}
635
Eric Christopher83007122010-08-23 21:44:12 +0000636bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000637 // Verify we have a legal type before going any further.
638 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000639 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000640 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000641
Eric Christopher30b66332010-09-08 21:49:50 +0000642 // If we're an alloca we know we have a frame index and can emit the load
643 // directly in short order.
644 if (ARMLoadAlloca(I, VT))
645 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000646
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000647 // Our register and offset with innocuous defaults.
648 unsigned Reg = 0;
649 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000650
Eric Christopher83007122010-08-23 21:44:12 +0000651 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000652 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000653 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000654
Eric Christopherb1cc8482010-08-25 07:23:49 +0000655 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000656 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000657
Eric Christopherf06f3092010-08-24 00:50:47 +0000658 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000659 return true;
660}
661
Eric Christophere5734102010-09-03 00:35:47 +0000662bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
663 const BranchInst *BI = cast<BranchInst>(I);
664 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
665 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000666
Eric Christophere5734102010-09-03 00:35:47 +0000667 // Simple branch support.
668 unsigned CondReg = getRegForValue(BI->getCondition());
669 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000670
Eric Christophere5734102010-09-03 00:35:47 +0000671 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
672 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
673 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
674 .addReg(CondReg).addReg(CondReg));
675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
676 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
677 FastEmitBranch(FBB, DL);
678 FuncInfo.MBB->addSuccessor(TBB);
679 return true;
680}
681
Eric Christopherd43393a2010-09-08 23:13:45 +0000682bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
683 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000684
Eric Christopherd43393a2010-09-08 23:13:45 +0000685 EVT VT;
686 const Type *Ty = CI->getOperand(0)->getType();
687 if (!isTypeLegal(Ty, VT))
688 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000689
Eric Christopherd43393a2010-09-08 23:13:45 +0000690 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
691 if (isFloat && !Subtarget->hasVFP2())
692 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000693
Eric Christopherd43393a2010-09-08 23:13:45 +0000694 unsigned CmpOpc;
695 switch (VT.getSimpleVT().SimpleTy) {
696 default: return false;
697 // TODO: Verify compares.
698 case MVT::f32:
699 CmpOpc = ARM::VCMPES;
700 break;
701 case MVT::f64:
702 CmpOpc = ARM::VCMPED;
703 break;
704 case MVT::i32:
705 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
706 break;
707 }
708
709 unsigned Arg1 = getRegForValue(CI->getOperand(0));
710 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000711
Eric Christopherd43393a2010-09-08 23:13:45 +0000712 unsigned Arg2 = getRegForValue(CI->getOperand(1));
713 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000714
Eric Christopherd43393a2010-09-08 23:13:45 +0000715 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
716 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000717
Eric Christopherd43393a2010-09-08 23:13:45 +0000718 // For floating point we need to move the result to a register we can
719 // actually do something with.
720 if (isFloat)
721 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
722 TII.get(ARM::FMSTAT)));
723 return true;
724}
725
Eric Christopher46203602010-09-09 00:26:48 +0000726bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
727 // Make sure we have VFP and that we're extending float to double.
728 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000729
Eric Christopher46203602010-09-09 00:26:48 +0000730 Value *V = I->getOperand(0);
731 if (!I->getType()->isDoubleTy() ||
732 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000733
Eric Christopher46203602010-09-09 00:26:48 +0000734 unsigned Op = getRegForValue(V);
735 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000736
Eric Christopher46203602010-09-09 00:26:48 +0000737 unsigned Result = createResultReg(ARM::DPRRegisterClass);
738
Eric Christopherac1a19e2010-09-09 01:06:51 +0000739 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher46203602010-09-09 00:26:48 +0000740 TII.get(ARM::VCVTDS), Result)
741 .addReg(Op));
742 UpdateValueMap(I, Result);
743 return true;
744}
745
Eric Christopher9a040492010-09-09 18:54:59 +0000746bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
747 // Make sure we have VFP.
748 if (!Subtarget->hasVFP2()) return false;
749
750 EVT VT;
751 const Type *Ty = I->getType();
752 if (!isTypeLegal(Ty, VT))
753 return false;
754
755 unsigned Op = getRegForValue(I->getOperand(0));
756 if (Op == 0) return false;
757
758 unsigned Opc;
759 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
760 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
761 else return 0;
762
763 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
764 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
765 ResultReg)
766 .addReg(Op));
767 return true;
768}
769
770bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
771 // Make sure we have VFP.
772 if (!Subtarget->hasVFP2()) return false;
773
774 EVT VT;
775 const Type *RetTy = I->getType();
776 if (!isTypeLegal(RetTy, VT))
777 return false;
778
779 unsigned Op = getRegForValue(I->getOperand(0));
780 if (Op == 0) return false;
781
782 unsigned Opc;
783 const Type *OpTy = I->getOperand(0)->getType();
784 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
785 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
786 else return 0;
787
788 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
789 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
790 ResultReg)
791 .addReg(Op));
792 return true;
793}
794
Eric Christopherbc39b822010-09-09 00:53:57 +0000795bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000796 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000797
Eric Christopherbc39b822010-09-09 00:53:57 +0000798 // We can get here in the case when we want to use NEON for our fp
799 // operations, but can't figure out how to. Just use the vfp instructions
800 // if we have them.
801 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000802 const Type *Ty = I->getType();
803 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
804 if (isFloat && !Subtarget->hasVFP2())
805 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000806
Eric Christopherbc39b822010-09-09 00:53:57 +0000807 unsigned Op1 = getRegForValue(I->getOperand(0));
808 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000809
Eric Christopherbc39b822010-09-09 00:53:57 +0000810 unsigned Op2 = getRegForValue(I->getOperand(1));
811 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000812
Eric Christopherbc39b822010-09-09 00:53:57 +0000813 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000814 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
815 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000816 switch (ISDOpcode) {
817 default: return false;
818 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000819 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000820 break;
821 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000822 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000823 break;
824 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000825 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000826 break;
827 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000828 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000829 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
830 TII.get(Opc), ResultReg)
831 .addReg(Op1).addReg(Op2));
832 return true;
833}
834
Eric Christopher56d2b722010-09-02 23:43:26 +0000835// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000836bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000837 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000838 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000839
Eric Christopherab695882010-07-21 22:26:11 +0000840 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000841 case Instruction::Load:
842 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000843 case Instruction::Store:
844 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000845 case Instruction::Br:
846 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000847 case Instruction::ICmp:
848 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000849 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000850 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000851 return ARMSelectFPExt(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000852 case Instruction::SIToFP:
853 return ARMSelectSIToFP(I);
854 case Instruction::FPToSI:
855 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000856 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000857 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000858 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000859 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000860 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000861 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000862 default: break;
863 }
864 return false;
865}
866
867namespace llvm {
868 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000869 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000870 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000871 }
872}