blob: 7ec62afc70cea5220f8f02acd8a5d4ee0a1fe264 [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 Christopher9ee4ce22010-09-09 21:44:45 +0000135 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000136
Eric Christopher456144e2010-08-19 00:37:05 +0000137 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
138 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
139};
Eric Christopherab695882010-07-21 22:26:11 +0000140
141} // end anonymous namespace
142
143// #include "ARMGenCallingConv.inc"
144
Eric Christopher456144e2010-08-19 00:37:05 +0000145// DefinesOptionalPredicate - This is different from DefinesPredicate in that
146// we don't care about implicit defs here, just places we'll need to add a
147// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
148bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
149 const TargetInstrDesc &TID = MI->getDesc();
150 if (!TID.hasOptionalDef())
151 return false;
152
153 // Look to see if our OptionalDef is defining CPSR or CCR.
154 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
155 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000156 if (!MO.isReg() || !MO.isDef()) continue;
157 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000158 *CPSR = true;
159 }
160 return true;
161}
162
163// If the machine is predicable go ahead and add the predicate operands, if
164// it needs default CC operands add those.
165const MachineInstrBuilder &
166ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
167 MachineInstr *MI = &*MIB;
168
169 // Do we use a predicate?
170 if (TII.isPredicable(MI))
171 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000172
Eric Christopher456144e2010-08-19 00:37:05 +0000173 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
174 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000175 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000176 if (DefinesOptionalPredicate(MI, &CPSR)) {
177 if (CPSR)
178 AddDefaultT1CC(MIB);
179 else
180 AddDefaultCC(MIB);
181 }
182 return MIB;
183}
184
Eric Christopher0fe7d542010-08-17 01:25:29 +0000185unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
186 const TargetRegisterClass* RC) {
187 unsigned ResultReg = createResultReg(RC);
188 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
189
Eric Christopher456144e2010-08-19 00:37:05 +0000190 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000191 return ResultReg;
192}
193
194unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
195 const TargetRegisterClass *RC,
196 unsigned Op0, bool Op0IsKill) {
197 unsigned ResultReg = createResultReg(RC);
198 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
199
200 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000202 .addReg(Op0, Op0IsKill * RegState::Kill));
203 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000205 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000206 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000207 TII.get(TargetOpcode::COPY), ResultReg)
208 .addReg(II.ImplicitDefs[0]));
209 }
210 return ResultReg;
211}
212
213unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
214 const TargetRegisterClass *RC,
215 unsigned Op0, bool Op0IsKill,
216 unsigned Op1, bool Op1IsKill) {
217 unsigned ResultReg = createResultReg(RC);
218 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
219
220 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000221 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000222 .addReg(Op0, Op0IsKill * RegState::Kill)
223 .addReg(Op1, Op1IsKill * RegState::Kill));
224 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000225 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000226 .addReg(Op0, Op0IsKill * RegState::Kill)
227 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000228 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000229 TII.get(TargetOpcode::COPY), ResultReg)
230 .addReg(II.ImplicitDefs[0]));
231 }
232 return ResultReg;
233}
234
235unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
236 const TargetRegisterClass *RC,
237 unsigned Op0, bool Op0IsKill,
238 uint64_t Imm) {
239 unsigned ResultReg = createResultReg(RC);
240 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
241
242 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000243 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000244 .addReg(Op0, Op0IsKill * RegState::Kill)
245 .addImm(Imm));
246 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000247 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000248 .addReg(Op0, Op0IsKill * RegState::Kill)
249 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000250 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000251 TII.get(TargetOpcode::COPY), ResultReg)
252 .addReg(II.ImplicitDefs[0]));
253 }
254 return ResultReg;
255}
256
257unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
258 const TargetRegisterClass *RC,
259 unsigned Op0, bool Op0IsKill,
260 const ConstantFP *FPImm) {
261 unsigned ResultReg = createResultReg(RC);
262 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
263
264 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000266 .addReg(Op0, Op0IsKill * RegState::Kill)
267 .addFPImm(FPImm));
268 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000270 .addReg(Op0, Op0IsKill * RegState::Kill)
271 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000272 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000273 TII.get(TargetOpcode::COPY), ResultReg)
274 .addReg(II.ImplicitDefs[0]));
275 }
276 return ResultReg;
277}
278
279unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
280 const TargetRegisterClass *RC,
281 unsigned Op0, bool Op0IsKill,
282 unsigned Op1, bool Op1IsKill,
283 uint64_t Imm) {
284 unsigned ResultReg = createResultReg(RC);
285 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
286
287 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289 .addReg(Op0, Op0IsKill * RegState::Kill)
290 .addReg(Op1, Op1IsKill * RegState::Kill)
291 .addImm(Imm));
292 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000294 .addReg(Op0, Op0IsKill * RegState::Kill)
295 .addReg(Op1, Op1IsKill * RegState::Kill)
296 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 TII.get(TargetOpcode::COPY), ResultReg)
299 .addReg(II.ImplicitDefs[0]));
300 }
301 return ResultReg;
302}
303
304unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
305 const TargetRegisterClass *RC,
306 uint64_t Imm) {
307 unsigned ResultReg = createResultReg(RC);
308 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000309
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 .addImm(Imm));
313 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000314 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000315 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000316 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000317 TII.get(TargetOpcode::COPY), ResultReg)
318 .addReg(II.ImplicitDefs[0]));
319 }
320 return ResultReg;
321}
322
323unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
324 unsigned Op0, bool Op0IsKill,
325 uint32_t Idx) {
326 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
327 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
328 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000330 DL, TII.get(TargetOpcode::COPY), ResultReg)
331 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
332 return ResultReg;
333}
334
Eric Christopheraa3ace12010-09-09 20:49:25 +0000335unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000336 // Don't worry about 64-bit now.
337 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
338
339 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
341 TII.get(ARM::VMOVRS), MoveReg)
342 .addReg(SrcReg));
343 return MoveReg;
344}
345
346unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
347 // Don't worry about 64-bit now.
348 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
349
Eric Christopheraa3ace12010-09-09 20:49:25 +0000350 // If we have a floating point constant we expect it in a floating point
351 // register.
352 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
353 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000354 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000355 .addReg(SrcReg));
356 return MoveReg;
357}
358
Eric Christopher9ed58df2010-09-09 00:19:41 +0000359// For double width floating point we need to materialize two constants
360// (the high and the low) into integer registers then use a move to get
361// the combined constant into an FP reg.
362unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
363 const APFloat Val = CFP->getValueAPF();
364 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000365
Eric Christopher9ed58df2010-09-09 00:19:41 +0000366 // This checks to see if we can use VFP3 instructions to materialize
367 // a constant, otherwise we have to go through the constant pool.
368 if (TLI.isFPImmLegal(Val, VT)) {
369 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
370 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
372 DestReg)
373 .addFPImm(CFP));
374 return DestReg;
375 }
Eric Christopher238bb162010-09-09 23:50:00 +0000376
377 // Require VFP2 for this.
378 if (!Subtarget->hasVFP2()) return false;
379
380 // MachineConstantPool wants an explicit alignment.
381 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
382 if (Align == 0) {
383 // TODO: Figure out if this is correct.
384 Align = TD.getTypeAllocSize(CFP->getType());
385 }
386 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
387 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
388 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
389
390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
391 .addReg(DestReg).addConstantPoolIndex(Idx)
392 .addReg(0));
393 return DestReg;
Eric Christopher9ed58df2010-09-09 00:19:41 +0000394}
395
396unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000397 // MachineConstantPool wants an explicit alignment.
398 unsigned Align = TD.getPrefTypeAlignment(C->getType());
399 if (Align == 0) {
400 // TODO: Figure out if this is correct.
401 Align = TD.getTypeAllocSize(C->getType());
402 }
403 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
404
Eric Christopher845c5752010-09-08 18:56:34 +0000405 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000406 if (isThumb)
407 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
408 TII.get(ARM::t2LDRpci))
409 .addReg(DestReg).addConstantPoolIndex(Idx));
410 else
411 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
412 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000413 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000414 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000415
Eric Christopher56d2b722010-09-02 23:43:26 +0000416 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000417}
418
Eric Christopher9ed58df2010-09-09 00:19:41 +0000419unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
420 EVT VT = TLI.getValueType(C->getType(), true);
421
422 // Only handle simple types.
423 if (!VT.isSimple()) return 0;
424
425 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
426 return ARMMaterializeFP(CFP, VT);
427 return ARMMaterializeInt(C);
428}
429
Eric Christopherb1cc8482010-08-25 07:23:49 +0000430bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
431 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000432
Eric Christopherb1cc8482010-08-25 07:23:49 +0000433 // Only handle simple types.
434 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000435
Eric Christopherdc908042010-08-31 01:28:42 +0000436 // Handle all legal types, i.e. a register that will directly hold this
437 // value.
438 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000439}
440
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000441bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
442 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000443
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000444 // If this is a type than can be sign or zero-extended to a basic operation
445 // go ahead and accept it now.
446 if (VT == MVT::i8 || VT == MVT::i16)
447 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000448
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000449 return false;
450}
451
Eric Christophercb0b04b2010-08-24 00:07:24 +0000452// Computes the Reg+Offset to get to an object.
453bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000454 int &Offset) {
455 // Some boilerplate from the X86 FastISel.
456 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000457 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000458 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000459 // Don't walk into other basic blocks; it's possible we haven't
460 // visited them yet, so the instructions may not yet be assigned
461 // virtual registers.
462 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
463 return false;
464
465 Opcode = I->getOpcode();
466 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000467 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000468 Opcode = C->getOpcode();
469 U = C;
470 }
471
Eric Christophercb0b04b2010-08-24 00:07:24 +0000472 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000473 if (Ty->getAddressSpace() > 255)
474 // Fast instruction selection doesn't support the special
475 // address spaces.
476 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000477
Eric Christopher83007122010-08-23 21:44:12 +0000478 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000479 default:
Eric Christopher83007122010-08-23 21:44:12 +0000480 //errs() << "Failing Opcode is: " << *Op1 << "\n";
481 break;
482 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000483 assert(false && "Alloca should have been handled earlier!");
484 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000485 }
486 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000487
Eric Christophercb0b04b2010-08-24 00:07:24 +0000488 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
489 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000490 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000491 return false;
492 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000493
Eric Christophercb0b04b2010-08-24 00:07:24 +0000494 // Try to get this in a register if nothing else has worked.
495 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000496 if (Reg == 0) return false;
497
498 // Since the offset may be too large for the load instruction
499 // get the reg+offset into a register.
500 // TODO: Verify the additions work, otherwise we'll need to add the
501 // offset instead of 0 to the instructions and do all sorts of operand
502 // munging.
503 // TODO: Optimize this somewhat.
504 if (Offset != 0) {
505 ARMCC::CondCodes Pred = ARMCC::AL;
506 unsigned PredReg = 0;
507
Eric Christophereaa204b2010-09-02 01:39:14 +0000508 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000509 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
510 Reg, Reg, Offset, Pred, PredReg,
511 static_cast<const ARMBaseInstrInfo&>(TII));
512 else {
513 assert(AFI->isThumb2Function());
514 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
515 Reg, Reg, Offset, Pred, PredReg,
516 static_cast<const ARMBaseInstrInfo&>(TII));
517 }
518 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000519
Eric Christopher318b6ee2010-09-02 00:53:56 +0000520 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000521}
522
Eric Christopher30b66332010-09-08 21:49:50 +0000523bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000524 Value *Op0 = I->getOperand(0);
525
526 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000527 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
528 DenseMap<const AllocaInst*, int>::iterator SI =
529 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000530
Eric Christophere24d66f2010-08-24 22:07:27 +0000531 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000532 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000533 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000534 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000535 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000536 TM.getRegisterInfo());
537 UpdateValueMap(I, ResultReg);
538 return true;
539 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000540 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000541 return false;
542}
543
Eric Christopherb1cc8482010-08-25 07:23:49 +0000544bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
545 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000546
Eric Christopherb1cc8482010-08-25 07:23:49 +0000547 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000548 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000549
Eric Christopherb1cc8482010-08-25 07:23:49 +0000550 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000551 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000552 assert(false && "Trying to emit for an unhandled type!");
553 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000554 case MVT::i16:
555 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
556 VT = MVT::i32;
557 break;
558 case MVT::i8:
559 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
560 VT = MVT::i32;
561 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000562 case MVT::i32:
563 Opc = isThumb ? ARM::tLDR : ARM::LDR;
564 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000565 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000566
Eric Christopherdc908042010-08-31 01:28:42 +0000567 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000568
Eric Christopherdc908042010-08-31 01:28:42 +0000569 // TODO: Fix the Addressing modes so that these can share some code.
570 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
571 if (isThumb)
572 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
573 TII.get(Opc), ResultReg)
574 .addReg(Reg).addImm(Offset).addReg(0));
575 else
576 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
577 TII.get(Opc), ResultReg)
578 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000579 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000580}
581
Eric Christopher30b66332010-09-08 21:49:50 +0000582bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000583 Value *Op1 = I->getOperand(1);
584
585 // Verify it's an alloca.
586 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
587 DenseMap<const AllocaInst*, int>::iterator SI =
588 FuncInfo.StaticAllocaMap.find(AI);
589
590 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000591 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000592 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000593 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000594 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000595 TM.getRegisterInfo());
596 return true;
597 }
598 }
599 return false;
600}
601
Eric Christopher318b6ee2010-09-02 00:53:56 +0000602bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
603 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000604 unsigned StrOpc;
605 switch (VT.getSimpleVT().SimpleTy) {
606 default: return false;
607 case MVT::i1:
608 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
609 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
610 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000611 case MVT::f32:
612 if (!Subtarget->hasVFP2()) return false;
613 StrOpc = ARM::VSTRS;
614 break;
615 case MVT::f64:
616 if (!Subtarget->hasVFP2()) return false;
617 StrOpc = ARM::VSTRD;
618 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000619 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000620
Eric Christopher318b6ee2010-09-02 00:53:56 +0000621 if (isThumb)
622 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
623 TII.get(StrOpc), SrcReg)
624 .addReg(DstReg).addImm(Offset).addReg(0));
625 else
626 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
627 TII.get(StrOpc), SrcReg)
628 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000629
Eric Christopher318b6ee2010-09-02 00:53:56 +0000630 return true;
631}
632
633bool ARMFastISel::ARMSelectStore(const Instruction *I) {
634 Value *Op0 = I->getOperand(0);
635 unsigned SrcReg = 0;
636
Eric Christopher543cf052010-09-01 22:16:27 +0000637 // Yay type legalization
638 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000639 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000640 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000641
Eric Christopher1b61ef42010-09-02 01:48:11 +0000642 // Get the value to be stored into a register.
643 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000644 if (SrcReg == 0)
645 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000646
Eric Christopher318b6ee2010-09-02 00:53:56 +0000647 // If we're an alloca we know we have a frame index and can emit the store
648 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000649 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000650 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000651
Eric Christopher318b6ee2010-09-02 00:53:56 +0000652 // Our register and offset with innocuous defaults.
653 unsigned Reg = 0;
654 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000655
Eric Christopher318b6ee2010-09-02 00:53:56 +0000656 // See if we can handle this as Reg + Offset
657 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
658 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000659
Eric Christopher318b6ee2010-09-02 00:53:56 +0000660 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000661
Eric Christopher543cf052010-09-01 22:16:27 +0000662 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000663}
664
Eric Christopher83007122010-08-23 21:44:12 +0000665bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000666 // Verify we have a legal type before going any further.
667 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000668 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000669 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000670
Eric Christopher30b66332010-09-08 21:49:50 +0000671 // If we're an alloca we know we have a frame index and can emit the load
672 // directly in short order.
673 if (ARMLoadAlloca(I, VT))
674 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000675
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000676 // Our register and offset with innocuous defaults.
677 unsigned Reg = 0;
678 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000679
Eric Christopher83007122010-08-23 21:44:12 +0000680 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000681 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000682 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000683
Eric Christopherb1cc8482010-08-25 07:23:49 +0000684 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000685 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000686
Eric Christopherf06f3092010-08-24 00:50:47 +0000687 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000688 return true;
689}
690
Eric Christophere5734102010-09-03 00:35:47 +0000691bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
692 const BranchInst *BI = cast<BranchInst>(I);
693 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
694 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000695
Eric Christophere5734102010-09-03 00:35:47 +0000696 // Simple branch support.
697 unsigned CondReg = getRegForValue(BI->getCondition());
698 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000699
Eric Christophere5734102010-09-03 00:35:47 +0000700 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
701 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
702 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
703 .addReg(CondReg).addReg(CondReg));
704 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
705 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
706 FastEmitBranch(FBB, DL);
707 FuncInfo.MBB->addSuccessor(TBB);
708 return true;
709}
710
Eric Christopherd43393a2010-09-08 23:13:45 +0000711bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
712 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000713
Eric Christopherd43393a2010-09-08 23:13:45 +0000714 EVT VT;
715 const Type *Ty = CI->getOperand(0)->getType();
716 if (!isTypeLegal(Ty, VT))
717 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000718
Eric Christopherd43393a2010-09-08 23:13:45 +0000719 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
720 if (isFloat && !Subtarget->hasVFP2())
721 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000722
Eric Christopherd43393a2010-09-08 23:13:45 +0000723 unsigned CmpOpc;
724 switch (VT.getSimpleVT().SimpleTy) {
725 default: return false;
726 // TODO: Verify compares.
727 case MVT::f32:
728 CmpOpc = ARM::VCMPES;
729 break;
730 case MVT::f64:
731 CmpOpc = ARM::VCMPED;
732 break;
733 case MVT::i32:
734 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
735 break;
736 }
737
738 unsigned Arg1 = getRegForValue(CI->getOperand(0));
739 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000740
Eric Christopherd43393a2010-09-08 23:13:45 +0000741 unsigned Arg2 = getRegForValue(CI->getOperand(1));
742 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000743
Eric Christopherd43393a2010-09-08 23:13:45 +0000744 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
745 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000746
Eric Christopherd43393a2010-09-08 23:13:45 +0000747 // For floating point we need to move the result to a register we can
748 // actually do something with.
749 if (isFloat)
750 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
751 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000752
753 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000754 return true;
755}
756
Eric Christopher46203602010-09-09 00:26:48 +0000757bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
758 // Make sure we have VFP and that we're extending float to double.
759 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000760
Eric Christopher46203602010-09-09 00:26:48 +0000761 Value *V = I->getOperand(0);
762 if (!I->getType()->isDoubleTy() ||
763 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000764
Eric Christopher46203602010-09-09 00:26:48 +0000765 unsigned Op = getRegForValue(V);
766 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000767
Eric Christopher46203602010-09-09 00:26:48 +0000768 unsigned Result = createResultReg(ARM::DPRRegisterClass);
769
Eric Christopherac1a19e2010-09-09 01:06:51 +0000770 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000771 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000772 .addReg(Op));
773 UpdateValueMap(I, Result);
774 return true;
775}
776
777bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
778 // Make sure we have VFP and that we're truncating double to float.
779 if (!Subtarget->hasVFP2()) return false;
780
781 Value *V = I->getOperand(0);
782 if (!I->getType()->isFloatTy() ||
783 !V->getType()->isDoubleTy()) return false;
784
785 unsigned Op = getRegForValue(V);
786 if (Op == 0) return false;
787
788 unsigned Result = createResultReg(ARM::SPRRegisterClass);
789
790 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000791 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000792 .addReg(Op));
793 UpdateValueMap(I, Result);
794 return true;
795}
796
Eric Christopher9a040492010-09-09 18:54:59 +0000797bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
798 // Make sure we have VFP.
799 if (!Subtarget->hasVFP2()) return false;
800
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000801 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000802 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000803 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000804 return false;
805
806 unsigned Op = getRegForValue(I->getOperand(0));
807 if (Op == 0) return false;
808
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000809 // The conversion routine works on fp-reg to fp-reg.
810 unsigned FP = ARMMoveToFPReg(DstVT, Op);
811 if (FP == 0) return false;
812
Eric Christopher9a040492010-09-09 18:54:59 +0000813 unsigned Opc;
814 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
815 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
816 else return 0;
817
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000818 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000819 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
820 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000821 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000822 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000823 return true;
824}
825
826bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
827 // Make sure we have VFP.
828 if (!Subtarget->hasVFP2()) return false;
829
830 EVT VT;
831 const Type *RetTy = I->getType();
832 if (!isTypeLegal(RetTy, VT))
833 return false;
834
835 unsigned Op = getRegForValue(I->getOperand(0));
836 if (Op == 0) return false;
837
838 unsigned Opc;
839 const Type *OpTy = I->getOperand(0)->getType();
840 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
841 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
842 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000843 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000844
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000845 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000846 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
847 ResultReg)
848 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000849
850 // This result needs to be in an integer register, but the conversion only
851 // takes place in fp-regs.
852 unsigned IntReg = ARMMoveToIntReg(VT, ResultReg);
853 if (IntReg == 0) return false;
854
855 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000856 return true;
857}
858
Eric Christopherbc39b822010-09-09 00:53:57 +0000859bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000860 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000861
Eric Christopherbc39b822010-09-09 00:53:57 +0000862 // We can get here in the case when we want to use NEON for our fp
863 // operations, but can't figure out how to. Just use the vfp instructions
864 // if we have them.
865 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000866 const Type *Ty = I->getType();
867 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
868 if (isFloat && !Subtarget->hasVFP2())
869 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000870
Eric Christopherbc39b822010-09-09 00:53:57 +0000871 unsigned Op1 = getRegForValue(I->getOperand(0));
872 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000873
Eric Christopherbc39b822010-09-09 00:53:57 +0000874 unsigned Op2 = getRegForValue(I->getOperand(1));
875 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000876
Eric Christopherbc39b822010-09-09 00:53:57 +0000877 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000878 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
879 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000880 switch (ISDOpcode) {
881 default: return false;
882 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000883 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000884 break;
885 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000886 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000887 break;
888 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000889 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000890 break;
891 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000892 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000893 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
894 TII.get(Opc), ResultReg)
895 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000896 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000897 return true;
898}
899
Eric Christopher56d2b722010-09-02 23:43:26 +0000900// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000901bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000902 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000903 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000904
Eric Christopherab695882010-07-21 22:26:11 +0000905 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000906 case Instruction::Load:
907 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000908 case Instruction::Store:
909 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000910 case Instruction::Br:
911 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000912 case Instruction::ICmp:
913 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000914 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000915 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000916 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +0000917 case Instruction::FPTrunc:
918 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000919 case Instruction::SIToFP:
920 return ARMSelectSIToFP(I);
921 case Instruction::FPToSI:
922 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000923 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000924 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000925 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000926 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000927 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000928 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000929 default: break;
930 }
931 return false;
932}
933
934namespace llvm {
935 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000936 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000937 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000938 }
939}