blob: 3d17acf7f345e8bd8458ad732643f81656b36314 [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.
Eric Christopher44bff902010-09-10 23:10:30 +0000113 private:
Eric Christopher83007122010-08-23 21:44:12 +0000114 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000115 virtual bool ARMSelectStore(const Instruction *I);
Eric Christophere5734102010-09-03 00:35:47 +0000116 virtual bool ARMSelectBranch(const Instruction *I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000117 virtual bool ARMSelectCmp(const Instruction *I);
Eric Christopher46203602010-09-09 00:26:48 +0000118 virtual bool ARMSelectFPExt(const Instruction *I);
Eric Christopherce07b542010-09-09 20:26:31 +0000119 virtual bool ARMSelectFPTrunc(const Instruction *I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000120 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopher9a040492010-09-09 18:54:59 +0000121 virtual bool ARMSelectSIToFP(const Instruction *I);
122 virtual bool ARMSelectFPToSI(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000123
Eric Christopher83007122010-08-23 21:44:12 +0000124 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000125 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000126 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000127 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000128 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000129 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000130 bool ARMLoadAlloca(const Instruction *I, EVT VT);
131 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000132 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000133 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
134 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopheraa3ace12010-09-09 20:49:25 +0000135 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000136 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000137
Eric Christopher456144e2010-08-19 00:37:05 +0000138 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
139 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
140};
Eric Christopherab695882010-07-21 22:26:11 +0000141
142} // end anonymous namespace
143
144// #include "ARMGenCallingConv.inc"
145
Eric Christopher456144e2010-08-19 00:37:05 +0000146// DefinesOptionalPredicate - This is different from DefinesPredicate in that
147// we don't care about implicit defs here, just places we'll need to add a
148// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
149bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
150 const TargetInstrDesc &TID = MI->getDesc();
151 if (!TID.hasOptionalDef())
152 return false;
153
154 // Look to see if our OptionalDef is defining CPSR or CCR.
155 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000157 if (!MO.isReg() || !MO.isDef()) continue;
158 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000159 *CPSR = true;
160 }
161 return true;
162}
163
164// If the machine is predicable go ahead and add the predicate operands, if
165// it needs default CC operands add those.
166const MachineInstrBuilder &
167ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
168 MachineInstr *MI = &*MIB;
169
170 // Do we use a predicate?
171 if (TII.isPredicable(MI))
172 AddDefaultPred(MIB);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000173
Eric Christopher456144e2010-08-19 00:37:05 +0000174 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
175 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000176 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000177 if (DefinesOptionalPredicate(MI, &CPSR)) {
178 if (CPSR)
179 AddDefaultT1CC(MIB);
180 else
181 AddDefaultCC(MIB);
182 }
183 return MIB;
184}
185
Eric Christopher0fe7d542010-08-17 01:25:29 +0000186unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
187 const TargetRegisterClass* RC) {
188 unsigned ResultReg = createResultReg(RC);
189 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
190
Eric Christopher456144e2010-08-19 00:37:05 +0000191 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000192 return ResultReg;
193}
194
195unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
196 const TargetRegisterClass *RC,
197 unsigned Op0, bool Op0IsKill) {
198 unsigned ResultReg = createResultReg(RC);
199 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
200
201 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000202 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000203 .addReg(Op0, Op0IsKill * RegState::Kill));
204 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000205 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000206 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000207 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000208 TII.get(TargetOpcode::COPY), ResultReg)
209 .addReg(II.ImplicitDefs[0]));
210 }
211 return ResultReg;
212}
213
214unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
215 const TargetRegisterClass *RC,
216 unsigned Op0, bool Op0IsKill,
217 unsigned Op1, bool Op1IsKill) {
218 unsigned ResultReg = createResultReg(RC);
219 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
220
221 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000222 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000223 .addReg(Op0, Op0IsKill * RegState::Kill)
224 .addReg(Op1, Op1IsKill * RegState::Kill));
225 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000227 .addReg(Op0, Op0IsKill * RegState::Kill)
228 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000230 TII.get(TargetOpcode::COPY), ResultReg)
231 .addReg(II.ImplicitDefs[0]));
232 }
233 return ResultReg;
234}
235
236unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
237 const TargetRegisterClass *RC,
238 unsigned Op0, bool Op0IsKill,
239 uint64_t Imm) {
240 unsigned ResultReg = createResultReg(RC);
241 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
242
243 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000245 .addReg(Op0, Op0IsKill * RegState::Kill)
246 .addImm(Imm));
247 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249 .addReg(Op0, Op0IsKill * RegState::Kill)
250 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000252 TII.get(TargetOpcode::COPY), ResultReg)
253 .addReg(II.ImplicitDefs[0]));
254 }
255 return ResultReg;
256}
257
258unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
259 const TargetRegisterClass *RC,
260 unsigned Op0, bool Op0IsKill,
261 const ConstantFP *FPImm) {
262 unsigned ResultReg = createResultReg(RC);
263 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
264
265 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000267 .addReg(Op0, Op0IsKill * RegState::Kill)
268 .addFPImm(FPImm));
269 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000271 .addReg(Op0, Op0IsKill * RegState::Kill)
272 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(II.ImplicitDefs[0]));
276 }
277 return ResultReg;
278}
279
280unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
281 const TargetRegisterClass *RC,
282 unsigned Op0, bool Op0IsKill,
283 unsigned Op1, bool Op1IsKill,
284 uint64_t Imm) {
285 unsigned ResultReg = createResultReg(RC);
286 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
287
288 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000290 .addReg(Op0, Op0IsKill * RegState::Kill)
291 .addReg(Op1, Op1IsKill * RegState::Kill)
292 .addImm(Imm));
293 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 .addReg(Op0, Op0IsKill * RegState::Kill)
296 .addReg(Op1, Op1IsKill * RegState::Kill)
297 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000299 TII.get(TargetOpcode::COPY), ResultReg)
300 .addReg(II.ImplicitDefs[0]));
301 }
302 return ResultReg;
303}
304
305unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
306 const TargetRegisterClass *RC,
307 uint64_t Imm) {
308 unsigned ResultReg = createResultReg(RC);
309 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000310
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000313 .addImm(Imm));
314 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000316 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000317 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000318 TII.get(TargetOpcode::COPY), ResultReg)
319 .addReg(II.ImplicitDefs[0]));
320 }
321 return ResultReg;
322}
323
324unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
325 unsigned Op0, bool Op0IsKill,
326 uint32_t Idx) {
327 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
328 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
329 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000331 DL, TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
333 return ResultReg;
334}
335
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000336// TODO: Don't worry about 64-bit now, but when this is fixed remove the
337// checks from the various callers.
Eric Christopheraa3ace12010-09-09 20:49:25 +0000338unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000339 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
340
341 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
343 TII.get(ARM::VMOVRS), MoveReg)
344 .addReg(SrcReg));
345 return MoveReg;
346}
347
348unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000349 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
350
Eric Christopheraa3ace12010-09-09 20:49:25 +0000351 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000353 TII.get(ARM::VMOVSR), MoveReg)
Eric Christopheraa3ace12010-09-09 20:49:25 +0000354 .addReg(SrcReg));
355 return MoveReg;
356}
357
Eric Christopher9ed58df2010-09-09 00:19:41 +0000358// For double width floating point we need to materialize two constants
359// (the high and the low) into integer registers then use a move to get
360// the combined constant into an FP reg.
361unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
362 const APFloat Val = CFP->getValueAPF();
363 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000364
Eric Christopher9ed58df2010-09-09 00:19:41 +0000365 // This checks to see if we can use VFP3 instructions to materialize
366 // a constant, otherwise we have to go through the constant pool.
367 if (TLI.isFPImmLegal(Val, VT)) {
368 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
369 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
371 DestReg)
372 .addFPImm(CFP));
373 return DestReg;
374 }
Eric Christopher238bb162010-09-09 23:50:00 +0000375
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000376 // Require VFP2 for loading fp constants.
Eric Christopher238bb162010-09-09 23:50:00 +0000377 if (!Subtarget->hasVFP2()) return false;
378
379 // MachineConstantPool wants an explicit alignment.
380 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
381 if (Align == 0) {
382 // TODO: Figure out if this is correct.
383 Align = TD.getTypeAllocSize(CFP->getType());
384 }
385 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
386 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
387 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
388
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000389 // The extra reg is for addrmode5.
Eric Christopher238bb162010-09-09 23:50:00 +0000390 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
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000396// TODO: Verify 64-bit.
Eric Christopher9ed58df2010-09-09 00:19:41 +0000397unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000398 // MachineConstantPool wants an explicit alignment.
399 unsigned Align = TD.getPrefTypeAlignment(C->getType());
400 if (Align == 0) {
401 // TODO: Figure out if this is correct.
402 Align = TD.getTypeAllocSize(C->getType());
403 }
404 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
Eric Christopher845c5752010-09-08 18:56:34 +0000405 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000406
Eric Christopher56d2b722010-09-02 23:43:26 +0000407 if (isThumb)
408 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
409 TII.get(ARM::t2LDRpci))
410 .addReg(DestReg).addConstantPoolIndex(Idx));
411 else
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000412 // The extra reg and immediate are for addrmode2.
Eric Christopher56d2b722010-09-02 23:43:26 +0000413 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
414 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000415 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000416 .addReg(0).addImm(0));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000417
Eric Christopher56d2b722010-09-02 23:43:26 +0000418 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000419}
420
Eric Christopher9ed58df2010-09-09 00:19:41 +0000421unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
422 EVT VT = TLI.getValueType(C->getType(), true);
423
424 // Only handle simple types.
425 if (!VT.isSimple()) return 0;
426
427 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
428 return ARMMaterializeFP(CFP, VT);
429 return ARMMaterializeInt(C);
430}
431
Eric Christopherb1cc8482010-08-25 07:23:49 +0000432bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
433 VT = TLI.getValueType(Ty, true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000434
Eric Christopherb1cc8482010-08-25 07:23:49 +0000435 // Only handle simple types.
436 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000437
Eric Christopherdc908042010-08-31 01:28:42 +0000438 // Handle all legal types, i.e. a register that will directly hold this
439 // value.
440 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000441}
442
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000443bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
444 if (isTypeLegal(Ty, VT)) return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000445
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000446 // If this is a type than can be sign or zero-extended to a basic operation
447 // go ahead and accept it now.
448 if (VT == MVT::i8 || VT == MVT::i16)
449 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000450
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000451 return false;
452}
453
Eric Christophercb0b04b2010-08-24 00:07:24 +0000454// Computes the Reg+Offset to get to an object.
455bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000456 int &Offset) {
457 // Some boilerplate from the X86 FastISel.
458 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000459 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000460 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000461 // Don't walk into other basic blocks; it's possible we haven't
462 // visited them yet, so the instructions may not yet be assigned
463 // virtual registers.
464 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
465 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000466 Opcode = I->getOpcode();
467 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000468 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000469 Opcode = C->getOpcode();
470 U = C;
471 }
472
Eric Christophercb0b04b2010-08-24 00:07:24 +0000473 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000474 if (Ty->getAddressSpace() > 255)
475 // Fast instruction selection doesn't support the special
476 // address spaces.
477 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000478
Eric Christopher83007122010-08-23 21:44:12 +0000479 switch (Opcode) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000480 default:
Eric Christopher83007122010-08-23 21:44:12 +0000481 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 Christopherdb12b2b2010-09-10 00:34:35 +0000488 // FIXME: Handle global variables.
Eric Christophercb0b04b2010-08-24 00:07:24 +0000489 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
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 Christopher318b6ee2010-09-02 00:53:56 +0000519 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000520}
521
Eric Christopher30b66332010-09-08 21:49:50 +0000522bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000523 Value *Op0 = I->getOperand(0);
524
525 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000526 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
527 DenseMap<const AllocaInst*, int>::iterator SI =
528 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000529
Eric Christophere24d66f2010-08-24 22:07:27 +0000530 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000531 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000532 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000533 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000534 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000535 TM.getRegisterInfo());
536 UpdateValueMap(I, ResultReg);
537 return true;
538 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000539 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000540 return false;
541}
542
Eric Christopherb1cc8482010-08-25 07:23:49 +0000543bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
544 unsigned Reg, int Offset) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000545
Eric Christopherb1cc8482010-08-25 07:23:49 +0000546 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000547 unsigned Opc;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000548
Eric Christopherb1cc8482010-08-25 07:23:49 +0000549 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopherac1a19e2010-09-09 01:06:51 +0000550 default:
Eric Christopher548d1bb2010-08-30 23:48:26 +0000551 assert(false && "Trying to emit for an unhandled type!");
552 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000553 case MVT::i16:
554 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
555 VT = MVT::i32;
556 break;
557 case MVT::i8:
558 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
559 VT = MVT::i32;
560 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000561 case MVT::i32:
562 Opc = isThumb ? ARM::tLDR : ARM::LDR;
563 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000564 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000565
Eric Christopherdc908042010-08-31 01:28:42 +0000566 ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000567
Eric Christopherdc908042010-08-31 01:28:42 +0000568 // TODO: Fix the Addressing modes so that these can share some code.
569 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
570 if (isThumb)
571 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
572 TII.get(Opc), ResultReg)
573 .addReg(Reg).addImm(Offset).addReg(0));
574 else
575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
576 TII.get(Opc), ResultReg)
577 .addReg(Reg).addReg(0).addImm(Offset));
Eric Christopherdc908042010-08-31 01:28:42 +0000578 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000579}
580
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000581bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
582 // Verify we have a legal type before going any further.
583 EVT VT;
584 if (!isLoadTypeLegal(I->getType(), VT))
585 return false;
586
587 // If we're an alloca we know we have a frame index and can emit the load
588 // directly in short order.
589 if (ARMLoadAlloca(I, VT))
590 return true;
591
592 // Our register and offset with innocuous defaults.
593 unsigned Reg = 0;
594 int Offset = 0;
595
596 // See if we can handle this as Reg + Offset
597 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
598 return false;
599
600 unsigned ResultReg;
601 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
602
603 UpdateValueMap(I, ResultReg);
604 return true;
605}
606
Eric Christopher30b66332010-09-08 21:49:50 +0000607bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000608 Value *Op1 = I->getOperand(1);
609
610 // Verify it's an alloca.
611 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
612 DenseMap<const AllocaInst*, int>::iterator SI =
613 FuncInfo.StaticAllocaMap.find(AI);
614
615 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000616 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000617 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000618 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000619 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000620 TM.getRegisterInfo());
621 return true;
622 }
623 }
624 return false;
625}
626
Eric Christopher318b6ee2010-09-02 00:53:56 +0000627bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
628 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000629 unsigned StrOpc;
630 switch (VT.getSimpleVT().SimpleTy) {
631 default: return false;
632 case MVT::i1:
633 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
634 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
635 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000636 case MVT::f32:
637 if (!Subtarget->hasVFP2()) return false;
638 StrOpc = ARM::VSTRS;
639 break;
640 case MVT::f64:
641 if (!Subtarget->hasVFP2()) return false;
642 StrOpc = ARM::VSTRD;
643 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000644 }
Eric Christopherac1a19e2010-09-09 01:06:51 +0000645
Eric Christopher318b6ee2010-09-02 00:53:56 +0000646 if (isThumb)
647 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
648 TII.get(StrOpc), SrcReg)
649 .addReg(DstReg).addImm(Offset).addReg(0));
650 else
651 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
652 TII.get(StrOpc), SrcReg)
653 .addReg(DstReg).addReg(0).addImm(Offset));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000654
Eric Christopher318b6ee2010-09-02 00:53:56 +0000655 return true;
656}
657
658bool ARMFastISel::ARMSelectStore(const Instruction *I) {
659 Value *Op0 = I->getOperand(0);
660 unsigned SrcReg = 0;
661
Eric Christopher543cf052010-09-01 22:16:27 +0000662 // Yay type legalization
663 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000664 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000665 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000666
Eric Christopher1b61ef42010-09-02 01:48:11 +0000667 // Get the value to be stored into a register.
668 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000669 if (SrcReg == 0)
670 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000671
Eric Christopher318b6ee2010-09-02 00:53:56 +0000672 // If we're an alloca we know we have a frame index and can emit the store
673 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000674 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000675 return true;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000676
Eric Christopher318b6ee2010-09-02 00:53:56 +0000677 // Our register and offset with innocuous defaults.
678 unsigned Reg = 0;
679 int Offset = 0;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000680
Eric Christopher318b6ee2010-09-02 00:53:56 +0000681 // See if we can handle this as Reg + Offset
682 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
683 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000684
Eric Christopher318b6ee2010-09-02 00:53:56 +0000685 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000686
Eric Christopher543cf052010-09-01 22:16:27 +0000687 return false;
Eric Christopher543cf052010-09-01 22:16:27 +0000688}
689
Eric Christophere5734102010-09-03 00:35:47 +0000690bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
691 const BranchInst *BI = cast<BranchInst>(I);
692 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
693 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Eric Christopherac1a19e2010-09-09 01:06:51 +0000694
Eric Christophere5734102010-09-03 00:35:47 +0000695 // Simple branch support.
696 unsigned CondReg = getRegForValue(BI->getCondition());
697 if (CondReg == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000698
Eric Christophere5734102010-09-03 00:35:47 +0000699 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
700 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
701 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
702 .addReg(CondReg).addReg(CondReg));
703 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
704 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
705 FastEmitBranch(FBB, DL);
706 FuncInfo.MBB->addSuccessor(TBB);
707 return true;
708}
709
Eric Christopherd43393a2010-09-08 23:13:45 +0000710bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
711 const CmpInst *CI = cast<CmpInst>(I);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000712
Eric Christopherd43393a2010-09-08 23:13:45 +0000713 EVT VT;
714 const Type *Ty = CI->getOperand(0)->getType();
715 if (!isTypeLegal(Ty, VT))
716 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000717
Eric Christopherd43393a2010-09-08 23:13:45 +0000718 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
719 if (isFloat && !Subtarget->hasVFP2())
720 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000721
Eric Christopherd43393a2010-09-08 23:13:45 +0000722 unsigned CmpOpc;
723 switch (VT.getSimpleVT().SimpleTy) {
724 default: return false;
725 // TODO: Verify compares.
726 case MVT::f32:
727 CmpOpc = ARM::VCMPES;
728 break;
729 case MVT::f64:
730 CmpOpc = ARM::VCMPED;
731 break;
732 case MVT::i32:
733 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
734 break;
735 }
736
737 unsigned Arg1 = getRegForValue(CI->getOperand(0));
738 if (Arg1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000739
Eric Christopherd43393a2010-09-08 23:13:45 +0000740 unsigned Arg2 = getRegForValue(CI->getOperand(1));
741 if (Arg2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000742
Eric Christopherd43393a2010-09-08 23:13:45 +0000743 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
744 .addReg(Arg1).addReg(Arg2));
Eric Christopherac1a19e2010-09-09 01:06:51 +0000745
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000746 // For floating point we need to move the result to a comparison register
747 // that we can then use for branches.
Eric Christopherd43393a2010-09-08 23:13:45 +0000748 if (isFloat)
749 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
750 TII.get(ARM::FMSTAT)));
Eric Christopherce07b542010-09-09 20:26:31 +0000751
752 // TODO: How to update the value map when there's no result reg?
Eric Christopherd43393a2010-09-08 23:13:45 +0000753 return true;
754}
755
Eric Christopher46203602010-09-09 00:26:48 +0000756bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
757 // Make sure we have VFP and that we're extending float to double.
758 if (!Subtarget->hasVFP2()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000759
Eric Christopher46203602010-09-09 00:26:48 +0000760 Value *V = I->getOperand(0);
761 if (!I->getType()->isDoubleTy() ||
762 !V->getType()->isFloatTy()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000763
Eric Christopher46203602010-09-09 00:26:48 +0000764 unsigned Op = getRegForValue(V);
765 if (Op == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000766
Eric Christopher46203602010-09-09 00:26:48 +0000767 unsigned Result = createResultReg(ARM::DPRRegisterClass);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000768 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000769 TII.get(ARM::VCVTDS), Result)
Eric Christopherce07b542010-09-09 20:26:31 +0000770 .addReg(Op));
771 UpdateValueMap(I, Result);
772 return true;
773}
774
775bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
776 // Make sure we have VFP and that we're truncating double to float.
777 if (!Subtarget->hasVFP2()) return false;
778
779 Value *V = I->getOperand(0);
780 if (!I->getType()->isFloatTy() ||
781 !V->getType()->isDoubleTy()) return false;
782
783 unsigned Op = getRegForValue(V);
784 if (Op == 0) return false;
785
786 unsigned Result = createResultReg(ARM::SPRRegisterClass);
Eric Christopherce07b542010-09-09 20:26:31 +0000787 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopheref2fdd22010-09-09 20:36:19 +0000788 TII.get(ARM::VCVTSD), Result)
Eric Christopher46203602010-09-09 00:26:48 +0000789 .addReg(Op));
790 UpdateValueMap(I, Result);
791 return true;
792}
793
Eric Christopher9a040492010-09-09 18:54:59 +0000794bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
795 // Make sure we have VFP.
796 if (!Subtarget->hasVFP2()) return false;
797
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000798 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000799 const Type *Ty = I->getType();
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000800 if (!isTypeLegal(Ty, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000801 return false;
802
803 unsigned Op = getRegForValue(I->getOperand(0));
804 if (Op == 0) return false;
805
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000806 // The conversion routine works on fp-reg to fp-reg and the operand above
807 // was an integer, move it to the fp registers if possible.
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000808 unsigned FP = ARMMoveToFPReg(DstVT, Op);
809 if (FP == 0) return false;
810
Eric Christopher9a040492010-09-09 18:54:59 +0000811 unsigned Opc;
812 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
813 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
814 else return 0;
815
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000816 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000817 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
818 ResultReg)
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000819 .addReg(FP));
Eric Christopherce07b542010-09-09 20:26:31 +0000820 UpdateValueMap(I, ResultReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000821 return true;
822}
823
824bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
825 // Make sure we have VFP.
826 if (!Subtarget->hasVFP2()) return false;
827
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000828 EVT DstVT;
Eric Christopher9a040492010-09-09 18:54:59 +0000829 const Type *RetTy = I->getType();
Eric Christopher920a2082010-09-10 00:35:09 +0000830 if (!isTypeLegal(RetTy, DstVT))
Eric Christopher9a040492010-09-09 18:54:59 +0000831 return false;
832
833 unsigned Op = getRegForValue(I->getOperand(0));
834 if (Op == 0) return false;
835
836 unsigned Opc;
837 const Type *OpTy = I->getOperand(0)->getType();
838 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
839 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
840 else return 0;
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000841 EVT OpVT = TLI.getValueType(OpTy, true);
Eric Christopher9a040492010-09-09 18:54:59 +0000842
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000843 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
Eric Christopher9a040492010-09-09 18:54:59 +0000844 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
845 ResultReg)
846 .addReg(Op));
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000847
848 // This result needs to be in an integer register, but the conversion only
849 // takes place in fp-regs.
Eric Christopherdb12b2b2010-09-10 00:34:35 +0000850 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
Eric Christopher9ee4ce22010-09-09 21:44:45 +0000851 if (IntReg == 0) return false;
852
853 UpdateValueMap(I, IntReg);
Eric Christopher9a040492010-09-09 18:54:59 +0000854 return true;
855}
856
Eric Christopherbc39b822010-09-09 00:53:57 +0000857bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
Eric Christopherbd6bf082010-09-09 01:02:03 +0000858 EVT VT = TLI.getValueType(I->getType(), true);
Eric Christopherac1a19e2010-09-09 01:06:51 +0000859
Eric Christopherbc39b822010-09-09 00:53:57 +0000860 // We can get here in the case when we want to use NEON for our fp
861 // operations, but can't figure out how to. Just use the vfp instructions
862 // if we have them.
863 // FIXME: It'd be nice to use NEON instructions.
Eric Christopherbd6bf082010-09-09 01:02:03 +0000864 const Type *Ty = I->getType();
865 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
866 if (isFloat && !Subtarget->hasVFP2())
867 return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000868
Eric Christopherbc39b822010-09-09 00:53:57 +0000869 unsigned Op1 = getRegForValue(I->getOperand(0));
870 if (Op1 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000871
Eric Christopherbc39b822010-09-09 00:53:57 +0000872 unsigned Op2 = getRegForValue(I->getOperand(1));
873 if (Op2 == 0) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000874
Eric Christopherbc39b822010-09-09 00:53:57 +0000875 unsigned Opc;
Eric Christopherbd6bf082010-09-09 01:02:03 +0000876 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
877 VT.getSimpleVT().SimpleTy == MVT::i64;
Eric Christopherbc39b822010-09-09 00:53:57 +0000878 switch (ISDOpcode) {
879 default: return false;
880 case ISD::FADD:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000881 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000882 break;
883 case ISD::FSUB:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000884 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000885 break;
886 case ISD::FMUL:
Eric Christopherbd6bf082010-09-09 01:02:03 +0000887 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
Eric Christopherbc39b822010-09-09 00:53:57 +0000888 break;
889 }
Eric Christopherbd6bf082010-09-09 01:02:03 +0000890 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Eric Christopherbc39b822010-09-09 00:53:57 +0000891 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
892 TII.get(Opc), ResultReg)
893 .addReg(Op1).addReg(Op2));
Eric Christopherce07b542010-09-09 20:26:31 +0000894 UpdateValueMap(I, ResultReg);
Eric Christopherbc39b822010-09-09 00:53:57 +0000895 return true;
896}
897
Eric Christopher56d2b722010-09-02 23:43:26 +0000898// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000899bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000900 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000901 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopherac1a19e2010-09-09 01:06:51 +0000902
Eric Christopherab695882010-07-21 22:26:11 +0000903 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000904 case Instruction::Load:
905 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000906 case Instruction::Store:
907 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000908 case Instruction::Br:
909 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000910 case Instruction::ICmp:
911 case Instruction::FCmp:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000912 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000913 case Instruction::FPExt:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000914 return ARMSelectFPExt(I);
Eric Christopherce07b542010-09-09 20:26:31 +0000915 case Instruction::FPTrunc:
916 return ARMSelectFPTrunc(I);
Eric Christopher9a040492010-09-09 18:54:59 +0000917 case Instruction::SIToFP:
918 return ARMSelectSIToFP(I);
919 case Instruction::FPToSI:
920 return ARMSelectFPToSI(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000921 case Instruction::FAdd:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000922 return ARMSelectBinaryOp(I, ISD::FADD);
Eric Christopherbc39b822010-09-09 00:53:57 +0000923 case Instruction::FSub:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000924 return ARMSelectBinaryOp(I, ISD::FSUB);
Eric Christopherbc39b822010-09-09 00:53:57 +0000925 case Instruction::FMul:
Eric Christopherac1a19e2010-09-09 01:06:51 +0000926 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000927 default: break;
928 }
929 return false;
930}
931
932namespace llvm {
933 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000934 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000935 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000936 }
937}