blob: 87af77b13e1b03b5b9110d5a168461a7fbe498ba [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
62 public:
Eric Christopher0fe7d542010-08-17 01:25:29 +000063 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
64 : FastISel(funcInfo),
65 TM(funcInfo.MF->getTarget()),
66 TII(*TM.getInstrInfo()),
67 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000068 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000069 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christopherab695882010-07-21 22:26:11 +000070 }
71
Eric Christophercb592292010-08-20 00:20:31 +000072 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000073 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
74 const TargetRegisterClass *RC);
75 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
76 const TargetRegisterClass *RC,
77 unsigned Op0, bool Op0IsKill);
78 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
79 const TargetRegisterClass *RC,
80 unsigned Op0, bool Op0IsKill,
81 unsigned Op1, bool Op1IsKill);
82 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC,
84 unsigned Op0, bool Op0IsKill,
85 uint64_t Imm);
86 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 const ConstantFP *FPImm);
90 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 uint64_t Imm);
93 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 unsigned Op1, bool Op1IsKill,
97 uint64_t Imm);
98 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
99 unsigned Op0, bool Op0IsKill,
100 uint32_t Idx);
Eric Christophercb592292010-08-20 00:20:31 +0000101
102 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000103 virtual bool TargetSelectInstruction(const Instruction *I);
104
105 #include "ARMGenFastISel.inc"
Eric Christopher83007122010-08-23 21:44:12 +0000106
107 // Instruction selection routines.
108 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000109
Eric Christopher83007122010-08-23 21:44:12 +0000110 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000111 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000112 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000113 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000114 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopherf06f3092010-08-24 00:50:47 +0000115 bool ARMLoadAlloca(const Instruction *I);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000116 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher83007122010-08-23 21:44:12 +0000117
Eric Christopher456144e2010-08-19 00:37:05 +0000118 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
119 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
120};
Eric Christopherab695882010-07-21 22:26:11 +0000121
122} // end anonymous namespace
123
124// #include "ARMGenCallingConv.inc"
125
Eric Christopher456144e2010-08-19 00:37:05 +0000126// DefinesOptionalPredicate - This is different from DefinesPredicate in that
127// we don't care about implicit defs here, just places we'll need to add a
128// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
129bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
130 const TargetInstrDesc &TID = MI->getDesc();
131 if (!TID.hasOptionalDef())
132 return false;
133
134 // Look to see if our OptionalDef is defining CPSR or CCR.
135 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
136 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000137 if (!MO.isReg() || !MO.isDef()) continue;
138 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000139 *CPSR = true;
140 }
141 return true;
142}
143
144// If the machine is predicable go ahead and add the predicate operands, if
145// it needs default CC operands add those.
146const MachineInstrBuilder &
147ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
148 MachineInstr *MI = &*MIB;
149
150 // Do we use a predicate?
151 if (TII.isPredicable(MI))
152 AddDefaultPred(MIB);
153
154 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
155 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000156 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000157 if (DefinesOptionalPredicate(MI, &CPSR)) {
158 if (CPSR)
159 AddDefaultT1CC(MIB);
160 else
161 AddDefaultCC(MIB);
162 }
163 return MIB;
164}
165
Eric Christopher0fe7d542010-08-17 01:25:29 +0000166unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
167 const TargetRegisterClass* RC) {
168 unsigned ResultReg = createResultReg(RC);
169 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
170
Eric Christopher456144e2010-08-19 00:37:05 +0000171 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000172 return ResultReg;
173}
174
175unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
176 const TargetRegisterClass *RC,
177 unsigned Op0, bool Op0IsKill) {
178 unsigned ResultReg = createResultReg(RC);
179 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
180
181 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000182 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000183 .addReg(Op0, Op0IsKill * RegState::Kill));
184 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000185 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000186 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000187 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000188 TII.get(TargetOpcode::COPY), ResultReg)
189 .addReg(II.ImplicitDefs[0]));
190 }
191 return ResultReg;
192}
193
194unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
195 const TargetRegisterClass *RC,
196 unsigned Op0, bool Op0IsKill,
197 unsigned Op1, bool Op1IsKill) {
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 .addReg(Op1, Op1IsKill * RegState::Kill));
205 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000206 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000207 .addReg(Op0, Op0IsKill * RegState::Kill)
208 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000209 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000210 TII.get(TargetOpcode::COPY), ResultReg)
211 .addReg(II.ImplicitDefs[0]));
212 }
213 return ResultReg;
214}
215
216unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
217 const TargetRegisterClass *RC,
218 unsigned Op0, bool Op0IsKill,
219 uint64_t Imm) {
220 unsigned ResultReg = createResultReg(RC);
221 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222
223 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000224 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000225 .addReg(Op0, Op0IsKill * RegState::Kill)
226 .addImm(Imm));
227 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000228 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000229 .addReg(Op0, Op0IsKill * RegState::Kill)
230 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000231 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000232 TII.get(TargetOpcode::COPY), ResultReg)
233 .addReg(II.ImplicitDefs[0]));
234 }
235 return ResultReg;
236}
237
238unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
239 const TargetRegisterClass *RC,
240 unsigned Op0, bool Op0IsKill,
241 const ConstantFP *FPImm) {
242 unsigned ResultReg = createResultReg(RC);
243 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
244
245 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000246 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000247 .addReg(Op0, Op0IsKill * RegState::Kill)
248 .addFPImm(FPImm));
249 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000250 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000251 .addReg(Op0, Op0IsKill * RegState::Kill)
252 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000253 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000254 TII.get(TargetOpcode::COPY), ResultReg)
255 .addReg(II.ImplicitDefs[0]));
256 }
257 return ResultReg;
258}
259
260unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
261 const TargetRegisterClass *RC,
262 unsigned Op0, bool Op0IsKill,
263 unsigned Op1, bool Op1IsKill,
264 uint64_t Imm) {
265 unsigned ResultReg = createResultReg(RC);
266 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
267
268 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000269 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000270 .addReg(Op0, Op0IsKill * RegState::Kill)
271 .addReg(Op1, Op1IsKill * RegState::Kill)
272 .addImm(Imm));
273 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000274 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000275 .addReg(Op0, Op0IsKill * RegState::Kill)
276 .addReg(Op1, Op1IsKill * RegState::Kill)
277 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 TII.get(TargetOpcode::COPY), ResultReg)
280 .addReg(II.ImplicitDefs[0]));
281 }
282 return ResultReg;
283}
284
285unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
286 const TargetRegisterClass *RC,
287 uint64_t Imm) {
288 unsigned ResultReg = createResultReg(RC);
289 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
290
291 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 .addImm(Imm));
294 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000296 .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_extractsubreg(MVT RetVT,
305 unsigned Op0, bool Op0IsKill,
306 uint32_t Idx) {
307 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
308 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
309 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000310 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000311 DL, TII.get(TargetOpcode::COPY), ResultReg)
312 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
313 return ResultReg;
314}
315
Eric Christopherb1cc8482010-08-25 07:23:49 +0000316bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
317 VT = TLI.getValueType(Ty, true);
318
319 // Only handle simple types.
320 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000321
Eric Christopherdc908042010-08-31 01:28:42 +0000322 // Handle all legal types, i.e. a register that will directly hold this
323 // value.
324 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000325}
326
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000327bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
328 if (isTypeLegal(Ty, VT)) return true;
329
330 // If this is a type than can be sign or zero-extended to a basic operation
331 // go ahead and accept it now.
332 if (VT == MVT::i8 || VT == MVT::i16)
333 return true;
334
335 return false;
336}
337
Eric Christophercb0b04b2010-08-24 00:07:24 +0000338// Computes the Reg+Offset to get to an object.
339bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000340 int &Offset) {
341 // Some boilerplate from the X86 FastISel.
342 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000343 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000344 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000345 // Don't walk into other basic blocks; it's possible we haven't
346 // visited them yet, so the instructions may not yet be assigned
347 // virtual registers.
348 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
349 return false;
350
351 Opcode = I->getOpcode();
352 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000353 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000354 Opcode = C->getOpcode();
355 U = C;
356 }
357
Eric Christophercb0b04b2010-08-24 00:07:24 +0000358 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000359 if (Ty->getAddressSpace() > 255)
360 // Fast instruction selection doesn't support the special
361 // address spaces.
362 return false;
363
364 switch (Opcode) {
365 default:
366 //errs() << "Failing Opcode is: " << *Op1 << "\n";
367 break;
368 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000369 assert(false && "Alloca should have been handled earlier!");
370 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000371 }
372 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000373
374 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
375 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000376 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000377 return false;
378 }
379
380 // Try to get this in a register if nothing else has worked.
381 Reg = getRegForValue(Obj);
382 return Reg != 0;
Eric Christopher83007122010-08-23 21:44:12 +0000383}
384
Eric Christopherf06f3092010-08-24 00:50:47 +0000385bool ARMFastISel::ARMLoadAlloca(const Instruction *I) {
386 Value *Op0 = I->getOperand(0);
387
388 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000389 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
390 DenseMap<const AllocaInst*, int>::iterator SI =
391 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000392
Eric Christophere24d66f2010-08-24 22:07:27 +0000393 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000394 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
395 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000396 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000397 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000398 TM.getRegisterInfo());
399 UpdateValueMap(I, ResultReg);
400 return true;
401 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000402 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000403 return false;
404}
405
Eric Christopherb1cc8482010-08-25 07:23:49 +0000406bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
407 unsigned Reg, int Offset) {
408
409 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000410
411 bool isThumb = AFI->isThumbFunction();
412 unsigned Opc;
413
Eric Christopherb1cc8482010-08-25 07:23:49 +0000414 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000415 default:
416 assert(false && "Trying to emit for an unhandled type!");
417 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000418 case MVT::i16:
419 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
420 VT = MVT::i32;
421 break;
422 case MVT::i8:
423 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
424 VT = MVT::i32;
425 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000426 case MVT::i32:
427 Opc = isThumb ? ARM::tLDR : ARM::LDR;
428 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000429 }
Eric Christopherdc908042010-08-31 01:28:42 +0000430
431 ResultReg = createResultReg(TLI.getRegClassFor(VT));
432
433 // TODO: Fix the Addressing modes so that these can share some code.
434 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
435 if (isThumb)
436 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
437 TII.get(Opc), ResultReg)
438 .addReg(Reg).addImm(Offset).addReg(0));
439 else
440 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
441 TII.get(Opc), ResultReg)
442 .addReg(Reg).addReg(0).addImm(Offset));
443
444 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000445}
446
Eric Christopher83007122010-08-23 21:44:12 +0000447bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher882d62e2010-08-24 01:10:52 +0000448 // If we're an alloca we know we have a frame index and can emit the load
449 // directly in short order.
Eric Christopherf06f3092010-08-24 00:50:47 +0000450 if (ARMLoadAlloca(I))
451 return true;
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000452
453 // Verify we have a legal type before going any further.
454 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000455 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000456 return false;
457
458 // Our register and offset with innocuous defaults.
459 unsigned Reg = 0;
460 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000461
Eric Christopher83007122010-08-23 21:44:12 +0000462 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000463 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000464 return false;
465
Eric Christopher8654c712010-08-23 23:14:31 +0000466 // Since the offset may be too large for the load instruction
467 // get the reg+offset into a register.
468 // TODO: Optimize this somewhat.
Eric Christopher8654c712010-08-23 23:14:31 +0000469 ARMCC::CondCodes Pred = ARMCC::AL;
470 unsigned PredReg = 0;
471
472 if (!AFI->isThumbFunction())
473 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000474 Reg, Reg, Offset, Pred, PredReg,
Eric Christopher8654c712010-08-23 23:14:31 +0000475 static_cast<const ARMBaseInstrInfo&>(TII));
476 else {
477 assert(AFI->isThumb2Function());
478 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000479 Reg, Reg, Offset, Pred, PredReg,
Eric Christopher8654c712010-08-23 23:14:31 +0000480 static_cast<const ARMBaseInstrInfo&>(TII));
481 }
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000482
Eric Christopherb1cc8482010-08-25 07:23:49 +0000483 unsigned ResultReg;
Eric Christopher2012c7b2010-08-24 01:10:04 +0000484 // TODO: Verify the additions above work, otherwise we'll need to add the
485 // offset instead of 0 and do all sorts of operand munging.
Eric Christopherb1cc8482010-08-25 07:23:49 +0000486 if (!ARMEmitLoad(VT, ResultReg, Reg, 0)) return false;
487
Eric Christopherf06f3092010-08-24 00:50:47 +0000488 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000489 return true;
490}
491
Eric Christopherab695882010-07-21 22:26:11 +0000492bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000493 // No Thumb-1 for now.
494 if (AFI->isThumbFunction() && !AFI->isThumb2Function()) return false;
495
Eric Christopherab695882010-07-21 22:26:11 +0000496 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000497 case Instruction::Load:
498 return ARMSelectLoad(I);
Eric Christopherab695882010-07-21 22:26:11 +0000499 default: break;
500 }
501 return false;
502}
503
504namespace llvm {
505 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000506 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000507 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000508 }
509}