blob: 5c6dd566a2bf18da4a940a9fa58091704a437b36 [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 Christopher0fe7d542010-08-17 01:25:29 +000066 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
67 : 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 Christophercb592292010-08-20 00:20:31 +0000105
106 // 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 Christopher83007122010-08-23 21:44:12 +0000111
112 // 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 Christopherab695882010-07-21 22:26:11 +0000118
Eric Christopher83007122010-08-23 21:44:12 +0000119 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000120 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000121 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000122 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000123 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000124 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000125 bool ARMLoadAlloca(const Instruction *I, EVT VT);
126 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000127 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000128 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
129 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopher83007122010-08-23 21:44:12 +0000130
Eric Christopher456144e2010-08-19 00:37:05 +0000131 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
132 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
133};
Eric Christopherab695882010-07-21 22:26:11 +0000134
135} // end anonymous namespace
136
137// #include "ARMGenCallingConv.inc"
138
Eric Christopher456144e2010-08-19 00:37:05 +0000139// DefinesOptionalPredicate - This is different from DefinesPredicate in that
140// we don't care about implicit defs here, just places we'll need to add a
141// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
142bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
143 const TargetInstrDesc &TID = MI->getDesc();
144 if (!TID.hasOptionalDef())
145 return false;
146
147 // Look to see if our OptionalDef is defining CPSR or CCR.
148 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
149 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000150 if (!MO.isReg() || !MO.isDef()) continue;
151 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000152 *CPSR = true;
153 }
154 return true;
155}
156
157// If the machine is predicable go ahead and add the predicate operands, if
158// it needs default CC operands add those.
159const MachineInstrBuilder &
160ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
161 MachineInstr *MI = &*MIB;
162
163 // Do we use a predicate?
164 if (TII.isPredicable(MI))
165 AddDefaultPred(MIB);
166
167 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
168 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000169 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000170 if (DefinesOptionalPredicate(MI, &CPSR)) {
171 if (CPSR)
172 AddDefaultT1CC(MIB);
173 else
174 AddDefaultCC(MIB);
175 }
176 return MIB;
177}
178
Eric Christopher0fe7d542010-08-17 01:25:29 +0000179unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
180 const TargetRegisterClass* RC) {
181 unsigned ResultReg = createResultReg(RC);
182 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
183
Eric Christopher456144e2010-08-19 00:37:05 +0000184 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000185 return ResultReg;
186}
187
188unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
189 const TargetRegisterClass *RC,
190 unsigned Op0, bool Op0IsKill) {
191 unsigned ResultReg = createResultReg(RC);
192 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
193
194 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000195 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000196 .addReg(Op0, Op0IsKill * RegState::Kill));
197 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000198 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000199 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000200 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000201 TII.get(TargetOpcode::COPY), ResultReg)
202 .addReg(II.ImplicitDefs[0]));
203 }
204 return ResultReg;
205}
206
207unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
208 const TargetRegisterClass *RC,
209 unsigned Op0, bool Op0IsKill,
210 unsigned Op1, bool Op1IsKill) {
211 unsigned ResultReg = createResultReg(RC);
212 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
213
214 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000215 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000216 .addReg(Op0, Op0IsKill * RegState::Kill)
217 .addReg(Op1, Op1IsKill * RegState::Kill));
218 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000219 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000220 .addReg(Op0, Op0IsKill * RegState::Kill)
221 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000222 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000223 TII.get(TargetOpcode::COPY), ResultReg)
224 .addReg(II.ImplicitDefs[0]));
225 }
226 return ResultReg;
227}
228
229unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
230 const TargetRegisterClass *RC,
231 unsigned Op0, bool Op0IsKill,
232 uint64_t Imm) {
233 unsigned ResultReg = createResultReg(RC);
234 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
235
236 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill)
239 .addImm(Imm));
240 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000241 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000242 .addReg(Op0, Op0IsKill * RegState::Kill)
243 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000245 TII.get(TargetOpcode::COPY), ResultReg)
246 .addReg(II.ImplicitDefs[0]));
247 }
248 return ResultReg;
249}
250
251unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
252 const TargetRegisterClass *RC,
253 unsigned Op0, bool Op0IsKill,
254 const ConstantFP *FPImm) {
255 unsigned ResultReg = createResultReg(RC);
256 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
257
258 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000259 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000260 .addReg(Op0, Op0IsKill * RegState::Kill)
261 .addFPImm(FPImm));
262 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000263 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000264 .addReg(Op0, Op0IsKill * RegState::Kill)
265 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000267 TII.get(TargetOpcode::COPY), ResultReg)
268 .addReg(II.ImplicitDefs[0]));
269 }
270 return ResultReg;
271}
272
273unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
274 const TargetRegisterClass *RC,
275 unsigned Op0, bool Op0IsKill,
276 unsigned Op1, bool Op1IsKill,
277 uint64_t Imm) {
278 unsigned ResultReg = createResultReg(RC);
279 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
280
281 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addReg(Op1, Op1IsKill * RegState::Kill)
285 .addImm(Imm));
286 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 .addReg(Op0, Op0IsKill * RegState::Kill)
289 .addReg(Op1, Op1IsKill * RegState::Kill)
290 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000292 TII.get(TargetOpcode::COPY), ResultReg)
293 .addReg(II.ImplicitDefs[0]));
294 }
295 return ResultReg;
296}
297
298unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
299 const TargetRegisterClass *RC,
300 uint64_t Imm) {
301 unsigned ResultReg = createResultReg(RC);
302 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
303
304 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 .addImm(Imm));
307 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000309 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 TII.get(TargetOpcode::COPY), ResultReg)
312 .addReg(II.ImplicitDefs[0]));
313 }
314 return ResultReg;
315}
316
317unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
318 unsigned Op0, bool Op0IsKill,
319 uint32_t Idx) {
320 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
321 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
322 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000324 DL, TII.get(TargetOpcode::COPY), ResultReg)
325 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
326 return ResultReg;
327}
328
Eric Christopher9ed58df2010-09-09 00:19:41 +0000329// For double width floating point we need to materialize two constants
330// (the high and the low) into integer registers then use a move to get
331// the combined constant into an FP reg.
332unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
333 const APFloat Val = CFP->getValueAPF();
334 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopher56d2b722010-09-02 23:43:26 +0000335
Eric Christopher9ed58df2010-09-09 00:19:41 +0000336 // This checks to see if we can use VFP3 instructions to materialize
337 // a constant, otherwise we have to go through the constant pool.
338 if (TLI.isFPImmLegal(Val, VT)) {
339 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
340 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
341 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
342 DestReg)
343 .addFPImm(CFP));
344 return DestReg;
345 }
346
347 // No 64-bit at the moment.
348 if (is64bit) return 0;
349
350 // Load this from the constant pool.
351 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000352
Eric Christopher9ed58df2010-09-09 00:19:41 +0000353 // If we have a floating point constant we expect it in a floating point
354 // register.
355 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
357 TII.get(ARM::VMOVRS), MoveReg)
358 .addReg(DestReg));
359 return MoveReg;
360}
361
362unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000363 // MachineConstantPool wants an explicit alignment.
364 unsigned Align = TD.getPrefTypeAlignment(C->getType());
365 if (Align == 0) {
366 // TODO: Figure out if this is correct.
367 Align = TD.getTypeAllocSize(C->getType());
368 }
369 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
370
Eric Christopher845c5752010-09-08 18:56:34 +0000371 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000372 if (isThumb)
373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
374 TII.get(ARM::t2LDRpci))
375 .addReg(DestReg).addConstantPoolIndex(Idx));
376 else
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000379 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000380 .addReg(0).addImm(0));
381
382 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000383}
384
Eric Christopher9ed58df2010-09-09 00:19:41 +0000385unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
386 EVT VT = TLI.getValueType(C->getType(), true);
387
388 // Only handle simple types.
389 if (!VT.isSimple()) return 0;
390
391 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
392 return ARMMaterializeFP(CFP, VT);
393 return ARMMaterializeInt(C);
394}
395
Eric Christopherb1cc8482010-08-25 07:23:49 +0000396bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
397 VT = TLI.getValueType(Ty, true);
398
399 // Only handle simple types.
400 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000401
Eric Christopherdc908042010-08-31 01:28:42 +0000402 // Handle all legal types, i.e. a register that will directly hold this
403 // value.
404 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000405}
406
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000407bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
408 if (isTypeLegal(Ty, VT)) return true;
409
410 // If this is a type than can be sign or zero-extended to a basic operation
411 // go ahead and accept it now.
412 if (VT == MVT::i8 || VT == MVT::i16)
413 return true;
414
415 return false;
416}
417
Eric Christophercb0b04b2010-08-24 00:07:24 +0000418// Computes the Reg+Offset to get to an object.
419bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000420 int &Offset) {
421 // Some boilerplate from the X86 FastISel.
422 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000423 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000424 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000425 // Don't walk into other basic blocks; it's possible we haven't
426 // visited them yet, so the instructions may not yet be assigned
427 // virtual registers.
428 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
429 return false;
430
431 Opcode = I->getOpcode();
432 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000433 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000434 Opcode = C->getOpcode();
435 U = C;
436 }
437
Eric Christophercb0b04b2010-08-24 00:07:24 +0000438 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000439 if (Ty->getAddressSpace() > 255)
440 // Fast instruction selection doesn't support the special
441 // address spaces.
442 return false;
443
444 switch (Opcode) {
445 default:
446 //errs() << "Failing Opcode is: " << *Op1 << "\n";
447 break;
448 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000449 assert(false && "Alloca should have been handled earlier!");
450 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000451 }
452 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000453
454 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
455 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000456 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000457 return false;
458 }
459
460 // Try to get this in a register if nothing else has worked.
461 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000462 if (Reg == 0) return false;
463
464 // Since the offset may be too large for the load instruction
465 // get the reg+offset into a register.
466 // TODO: Verify the additions work, otherwise we'll need to add the
467 // offset instead of 0 to the instructions and do all sorts of operand
468 // munging.
469 // TODO: Optimize this somewhat.
470 if (Offset != 0) {
471 ARMCC::CondCodes Pred = ARMCC::AL;
472 unsigned PredReg = 0;
473
Eric Christophereaa204b2010-09-02 01:39:14 +0000474 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000475 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
476 Reg, Reg, Offset, Pred, PredReg,
477 static_cast<const ARMBaseInstrInfo&>(TII));
478 else {
479 assert(AFI->isThumb2Function());
480 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
481 Reg, Reg, Offset, Pred, PredReg,
482 static_cast<const ARMBaseInstrInfo&>(TII));
483 }
484 }
485
486 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000487}
488
Eric Christopher30b66332010-09-08 21:49:50 +0000489bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000490 Value *Op0 = I->getOperand(0);
491
492 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000493 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
494 DenseMap<const AllocaInst*, int>::iterator SI =
495 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000496
Eric Christophere24d66f2010-08-24 22:07:27 +0000497 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000498 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000499 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000500 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000501 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000502 TM.getRegisterInfo());
503 UpdateValueMap(I, ResultReg);
504 return true;
505 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000506 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000507 return false;
508}
509
Eric Christopherb1cc8482010-08-25 07:23:49 +0000510bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
511 unsigned Reg, int Offset) {
512
513 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000514 unsigned Opc;
515
Eric Christopherb1cc8482010-08-25 07:23:49 +0000516 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000517 default:
518 assert(false && "Trying to emit for an unhandled type!");
519 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000520 case MVT::i16:
521 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
522 VT = MVT::i32;
523 break;
524 case MVT::i8:
525 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
526 VT = MVT::i32;
527 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000528 case MVT::i32:
529 Opc = isThumb ? ARM::tLDR : ARM::LDR;
530 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000531 }
Eric Christopherdc908042010-08-31 01:28:42 +0000532
533 ResultReg = createResultReg(TLI.getRegClassFor(VT));
534
535 // TODO: Fix the Addressing modes so that these can share some code.
536 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
537 if (isThumb)
538 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
539 TII.get(Opc), ResultReg)
540 .addReg(Reg).addImm(Offset).addReg(0));
541 else
542 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
543 TII.get(Opc), ResultReg)
544 .addReg(Reg).addReg(0).addImm(Offset));
545
546 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000547}
548
Eric Christopher30b66332010-09-08 21:49:50 +0000549bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000550 Value *Op1 = I->getOperand(1);
551
552 // Verify it's an alloca.
553 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
554 DenseMap<const AllocaInst*, int>::iterator SI =
555 FuncInfo.StaticAllocaMap.find(AI);
556
557 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000558 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000559 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000560 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000561 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000562 TM.getRegisterInfo());
563 return true;
564 }
565 }
566 return false;
567}
568
Eric Christopher318b6ee2010-09-02 00:53:56 +0000569bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
570 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000571 unsigned StrOpc;
572 switch (VT.getSimpleVT().SimpleTy) {
573 default: return false;
574 case MVT::i1:
575 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
576 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
577 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000578 case MVT::f32:
579 if (!Subtarget->hasVFP2()) return false;
580 StrOpc = ARM::VSTRS;
581 break;
582 case MVT::f64:
583 if (!Subtarget->hasVFP2()) return false;
584 StrOpc = ARM::VSTRD;
585 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000586 }
587
588 if (isThumb)
589 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
590 TII.get(StrOpc), SrcReg)
591 .addReg(DstReg).addImm(Offset).addReg(0));
592 else
593 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
594 TII.get(StrOpc), SrcReg)
595 .addReg(DstReg).addReg(0).addImm(Offset));
596
597 return true;
598}
599
600bool ARMFastISel::ARMSelectStore(const Instruction *I) {
601 Value *Op0 = I->getOperand(0);
602 unsigned SrcReg = 0;
603
Eric Christopher543cf052010-09-01 22:16:27 +0000604 // Yay type legalization
605 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000606 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000607 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000608
Eric Christopher1b61ef42010-09-02 01:48:11 +0000609 // Get the value to be stored into a register.
610 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000611 if (SrcReg == 0)
612 return false;
613
614 // If we're an alloca we know we have a frame index and can emit the store
615 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000616 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000617 return true;
618
619 // Our register and offset with innocuous defaults.
620 unsigned Reg = 0;
621 int Offset = 0;
622
623 // See if we can handle this as Reg + Offset
624 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
625 return false;
626
627 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
628
Eric Christopher543cf052010-09-01 22:16:27 +0000629 return false;
630
631}
632
Eric Christopher83007122010-08-23 21:44:12 +0000633bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000634 // Verify we have a legal type before going any further.
635 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000636 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000637 return false;
638
Eric Christopher30b66332010-09-08 21:49:50 +0000639 // If we're an alloca we know we have a frame index and can emit the load
640 // directly in short order.
641 if (ARMLoadAlloca(I, VT))
642 return true;
643
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000644 // Our register and offset with innocuous defaults.
645 unsigned Reg = 0;
646 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000647
Eric Christopher83007122010-08-23 21:44:12 +0000648 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000649 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000650 return false;
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000651
Eric Christopherb1cc8482010-08-25 07:23:49 +0000652 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000653 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000654
Eric Christopherf06f3092010-08-24 00:50:47 +0000655 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000656 return true;
657}
658
Eric Christophere5734102010-09-03 00:35:47 +0000659bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
660 const BranchInst *BI = cast<BranchInst>(I);
661 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
662 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
663
664 // Simple branch support.
665 unsigned CondReg = getRegForValue(BI->getCondition());
666 if (CondReg == 0) return false;
667
668 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
669 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
670 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
671 .addReg(CondReg).addReg(CondReg));
672 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
673 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
674 FastEmitBranch(FBB, DL);
675 FuncInfo.MBB->addSuccessor(TBB);
676 return true;
677}
678
Eric Christopherd43393a2010-09-08 23:13:45 +0000679bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
680 const CmpInst *CI = cast<CmpInst>(I);
681
682 EVT VT;
683 const Type *Ty = CI->getOperand(0)->getType();
684 if (!isTypeLegal(Ty, VT))
685 return false;
686
687 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
688 if (isFloat && !Subtarget->hasVFP2())
689 return false;
690
691 unsigned CmpOpc;
692 switch (VT.getSimpleVT().SimpleTy) {
693 default: return false;
694 // TODO: Verify compares.
695 case MVT::f32:
696 CmpOpc = ARM::VCMPES;
697 break;
698 case MVT::f64:
699 CmpOpc = ARM::VCMPED;
700 break;
701 case MVT::i32:
702 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
703 break;
704 }
705
706 unsigned Arg1 = getRegForValue(CI->getOperand(0));
707 if (Arg1 == 0) return false;
708
709 unsigned Arg2 = getRegForValue(CI->getOperand(1));
710 if (Arg2 == 0) return false;
711
712 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
713 .addReg(Arg1).addReg(Arg2));
714
715 // For floating point we need to move the result to a register we can
716 // actually do something with.
717 if (isFloat)
718 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
719 TII.get(ARM::FMSTAT)));
720 return true;
721}
722
Eric Christopher46203602010-09-09 00:26:48 +0000723bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
724 // Make sure we have VFP and that we're extending float to double.
725 if (!Subtarget->hasVFP2()) return false;
726
727 Value *V = I->getOperand(0);
728 if (!I->getType()->isDoubleTy() ||
729 !V->getType()->isFloatTy()) return false;
730
731 unsigned Op = getRegForValue(V);
732 if (Op == 0) return false;
733
734 unsigned Result = createResultReg(ARM::DPRRegisterClass);
735
736 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
737 TII.get(ARM::VCVTDS), Result)
738 .addReg(Op));
739 UpdateValueMap(I, Result);
740 return true;
741}
742
Eric Christopher56d2b722010-09-02 23:43:26 +0000743// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000744bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000745 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000746 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopher7fe55b72010-08-23 22:32:45 +0000747
Eric Christopherab695882010-07-21 22:26:11 +0000748 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000749 case Instruction::Load:
750 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000751 case Instruction::Store:
752 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000753 case Instruction::Br:
754 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000755 case Instruction::ICmp:
756 case Instruction::FCmp:
757 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000758 case Instruction::FPExt:
759 return ARMSelectFPExt(I);
Eric Christopherab695882010-07-21 22:26:11 +0000760 default: break;
761 }
762 return false;
763}
764
765namespace llvm {
766 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000767 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000768 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000769 }
770}