blob: 9b41afb4d4ceca9c3a73b991dbd461e871ae1792 [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 Christopher543cf052010-09-01 22:16:27 +0000109 virtual bool ARMSelectStore(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000110
Eric Christopher83007122010-08-23 21:44:12 +0000111 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000112 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000113 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000114 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000115 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopherf06f3092010-08-24 00:50:47 +0000116 bool ARMLoadAlloca(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000117 bool ARMStoreAlloca(const Instruction *I);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000118 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher83007122010-08-23 21:44:12 +0000119
Eric Christopher456144e2010-08-19 00:37:05 +0000120 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
121 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
122};
Eric Christopherab695882010-07-21 22:26:11 +0000123
124} // end anonymous namespace
125
126// #include "ARMGenCallingConv.inc"
127
Eric Christopher456144e2010-08-19 00:37:05 +0000128// DefinesOptionalPredicate - This is different from DefinesPredicate in that
129// we don't care about implicit defs here, just places we'll need to add a
130// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
131bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
132 const TargetInstrDesc &TID = MI->getDesc();
133 if (!TID.hasOptionalDef())
134 return false;
135
136 // Look to see if our OptionalDef is defining CPSR or CCR.
137 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
138 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000139 if (!MO.isReg() || !MO.isDef()) continue;
140 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000141 *CPSR = true;
142 }
143 return true;
144}
145
146// If the machine is predicable go ahead and add the predicate operands, if
147// it needs default CC operands add those.
148const MachineInstrBuilder &
149ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
150 MachineInstr *MI = &*MIB;
151
152 // Do we use a predicate?
153 if (TII.isPredicable(MI))
154 AddDefaultPred(MIB);
155
156 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
157 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000158 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000159 if (DefinesOptionalPredicate(MI, &CPSR)) {
160 if (CPSR)
161 AddDefaultT1CC(MIB);
162 else
163 AddDefaultCC(MIB);
164 }
165 return MIB;
166}
167
Eric Christopher0fe7d542010-08-17 01:25:29 +0000168unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
169 const TargetRegisterClass* RC) {
170 unsigned ResultReg = createResultReg(RC);
171 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
172
Eric Christopher456144e2010-08-19 00:37:05 +0000173 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000174 return ResultReg;
175}
176
177unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
178 const TargetRegisterClass *RC,
179 unsigned Op0, bool Op0IsKill) {
180 unsigned ResultReg = createResultReg(RC);
181 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
182
183 if (II.getNumDefs() >= 1)
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 .addReg(Op0, Op0IsKill * RegState::Kill));
186 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000187 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000188 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000189 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000190 TII.get(TargetOpcode::COPY), ResultReg)
191 .addReg(II.ImplicitDefs[0]));
192 }
193 return ResultReg;
194}
195
196unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
197 const TargetRegisterClass *RC,
198 unsigned Op0, bool Op0IsKill,
199 unsigned Op1, bool Op1IsKill) {
200 unsigned ResultReg = createResultReg(RC);
201 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
202
203 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000205 .addReg(Op0, Op0IsKill * RegState::Kill)
206 .addReg(Op1, Op1IsKill * RegState::Kill));
207 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000208 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000209 .addReg(Op0, Op0IsKill * RegState::Kill)
210 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000211 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000212 TII.get(TargetOpcode::COPY), ResultReg)
213 .addReg(II.ImplicitDefs[0]));
214 }
215 return ResultReg;
216}
217
218unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
219 const TargetRegisterClass *RC,
220 unsigned Op0, bool Op0IsKill,
221 uint64_t Imm) {
222 unsigned ResultReg = createResultReg(RC);
223 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
224
225 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000227 .addReg(Op0, Op0IsKill * RegState::Kill)
228 .addImm(Imm));
229 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000230 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000231 .addReg(Op0, Op0IsKill * RegState::Kill)
232 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 TII.get(TargetOpcode::COPY), ResultReg)
235 .addReg(II.ImplicitDefs[0]));
236 }
237 return ResultReg;
238}
239
240unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
241 const TargetRegisterClass *RC,
242 unsigned Op0, bool Op0IsKill,
243 const ConstantFP *FPImm) {
244 unsigned ResultReg = createResultReg(RC);
245 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
246
247 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000249 .addReg(Op0, Op0IsKill * RegState::Kill)
250 .addFPImm(FPImm));
251 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000252 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000253 .addReg(Op0, Op0IsKill * RegState::Kill)
254 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000255 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000256 TII.get(TargetOpcode::COPY), ResultReg)
257 .addReg(II.ImplicitDefs[0]));
258 }
259 return ResultReg;
260}
261
262unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
263 const TargetRegisterClass *RC,
264 unsigned Op0, bool Op0IsKill,
265 unsigned Op1, bool Op1IsKill,
266 uint64_t Imm) {
267 unsigned ResultReg = createResultReg(RC);
268 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
269
270 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000271 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000272 .addReg(Op0, Op0IsKill * RegState::Kill)
273 .addReg(Op1, Op1IsKill * RegState::Kill)
274 .addImm(Imm));
275 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000276 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000277 .addReg(Op0, Op0IsKill * RegState::Kill)
278 .addReg(Op1, Op1IsKill * RegState::Kill)
279 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000280 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000281 TII.get(TargetOpcode::COPY), ResultReg)
282 .addReg(II.ImplicitDefs[0]));
283 }
284 return ResultReg;
285}
286
287unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
288 const TargetRegisterClass *RC,
289 uint64_t Imm) {
290 unsigned ResultReg = createResultReg(RC);
291 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
292
293 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000295 .addImm(Imm));
296 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000297 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000298 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000299 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000300 TII.get(TargetOpcode::COPY), ResultReg)
301 .addReg(II.ImplicitDefs[0]));
302 }
303 return ResultReg;
304}
305
306unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
307 unsigned Op0, bool Op0IsKill,
308 uint32_t Idx) {
309 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
310 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
311 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000313 DL, TII.get(TargetOpcode::COPY), ResultReg)
314 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
315 return ResultReg;
316}
317
Eric Christopherb1cc8482010-08-25 07:23:49 +0000318bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
319 VT = TLI.getValueType(Ty, true);
320
321 // Only handle simple types.
322 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000323
Eric Christopherdc908042010-08-31 01:28:42 +0000324 // Handle all legal types, i.e. a register that will directly hold this
325 // value.
326 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000327}
328
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000329bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
330 if (isTypeLegal(Ty, VT)) return true;
331
332 // If this is a type than can be sign or zero-extended to a basic operation
333 // go ahead and accept it now.
334 if (VT == MVT::i8 || VT == MVT::i16)
335 return true;
336
337 return false;
338}
339
Eric Christophercb0b04b2010-08-24 00:07:24 +0000340// Computes the Reg+Offset to get to an object.
341bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000342 int &Offset) {
343 // Some boilerplate from the X86 FastISel.
344 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000345 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000346 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000347 // Don't walk into other basic blocks; it's possible we haven't
348 // visited them yet, so the instructions may not yet be assigned
349 // virtual registers.
350 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
351 return false;
352
353 Opcode = I->getOpcode();
354 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000355 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000356 Opcode = C->getOpcode();
357 U = C;
358 }
359
Eric Christophercb0b04b2010-08-24 00:07:24 +0000360 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000361 if (Ty->getAddressSpace() > 255)
362 // Fast instruction selection doesn't support the special
363 // address spaces.
364 return false;
365
366 switch (Opcode) {
367 default:
368 //errs() << "Failing Opcode is: " << *Op1 << "\n";
369 break;
370 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000371 assert(false && "Alloca should have been handled earlier!");
372 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000373 }
374 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000375
376 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
377 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000378 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000379 return false;
380 }
381
382 // Try to get this in a register if nothing else has worked.
383 Reg = getRegForValue(Obj);
384 return Reg != 0;
Eric Christopher83007122010-08-23 21:44:12 +0000385}
386
Eric Christopherf06f3092010-08-24 00:50:47 +0000387bool ARMFastISel::ARMLoadAlloca(const Instruction *I) {
388 Value *Op0 = I->getOperand(0);
389
390 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000391 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
392 DenseMap<const AllocaInst*, int>::iterator SI =
393 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000394
Eric Christophere24d66f2010-08-24 22:07:27 +0000395 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000396 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
397 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000398 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000399 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000400 TM.getRegisterInfo());
401 UpdateValueMap(I, ResultReg);
402 return true;
403 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000404 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000405 return false;
406}
407
Eric Christopherb1cc8482010-08-25 07:23:49 +0000408bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
409 unsigned Reg, int Offset) {
410
411 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000412
413 bool isThumb = AFI->isThumbFunction();
414 unsigned Opc;
415
Eric Christopherb1cc8482010-08-25 07:23:49 +0000416 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000417 default:
418 assert(false && "Trying to emit for an unhandled type!");
419 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000420 case MVT::i16:
421 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
422 VT = MVT::i32;
423 break;
424 case MVT::i8:
425 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
426 VT = MVT::i32;
427 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000428 case MVT::i32:
429 Opc = isThumb ? ARM::tLDR : ARM::LDR;
430 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000431 }
Eric Christopherdc908042010-08-31 01:28:42 +0000432
433 ResultReg = createResultReg(TLI.getRegClassFor(VT));
434
435 // TODO: Fix the Addressing modes so that these can share some code.
436 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
437 if (isThumb)
438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
439 TII.get(Opc), ResultReg)
440 .addReg(Reg).addImm(Offset).addReg(0));
441 else
442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
443 TII.get(Opc), ResultReg)
444 .addReg(Reg).addReg(0).addImm(Offset));
445
446 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000447}
448
Eric Christopher543cf052010-09-01 22:16:27 +0000449bool ARMFastISel::ARMStoreAlloca(const Instruction *I) {
450 Value *Op1 = I->getOperand(1);
451
452 // Verify it's an alloca.
453 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
454 DenseMap<const AllocaInst*, int>::iterator SI =
455 FuncInfo.StaticAllocaMap.find(AI);
456
457 if (SI != FuncInfo.StaticAllocaMap.end()) {
458 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
459 unsigned Reg = getRegForValue(I->getOperand(0));
460 // Make sure we can get this into a register.
461 if (Reg == 0) return false;
462 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
463 Reg, true /*isKill*/, SI->second, RC,
464 TM.getRegisterInfo());
465 return true;
466 }
467 }
468 return false;
469}
470
471bool ARMFastISel::ARMSelectStore(const Instruction *I) {
472 // If we're an alloca we know we have a frame index and can emit the store
473 // quickly.
474 if (ARMStoreAlloca(I))
475 return true;
476
477 // Yay type legalization
478 EVT VT;
479 if (!isLoadTypeLegal(I->getType(), VT))
480 return false;
481
482 return false;
483
484}
485
Eric Christopher83007122010-08-23 21:44:12 +0000486bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher882d62e2010-08-24 01:10:52 +0000487 // If we're an alloca we know we have a frame index and can emit the load
488 // directly in short order.
Eric Christopherf06f3092010-08-24 00:50:47 +0000489 if (ARMLoadAlloca(I))
490 return true;
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000491
492 // Verify we have a legal type before going any further.
493 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000494 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000495 return false;
496
497 // Our register and offset with innocuous defaults.
498 unsigned Reg = 0;
499 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000500
Eric Christopher83007122010-08-23 21:44:12 +0000501 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000502 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000503 return false;
504
Eric Christopher8654c712010-08-23 23:14:31 +0000505 // Since the offset may be too large for the load instruction
506 // get the reg+offset into a register.
507 // TODO: Optimize this somewhat.
Eric Christopher8654c712010-08-23 23:14:31 +0000508 ARMCC::CondCodes Pred = ARMCC::AL;
509 unsigned PredReg = 0;
510
511 if (!AFI->isThumbFunction())
512 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000513 Reg, Reg, Offset, Pred, PredReg,
Eric Christopher8654c712010-08-23 23:14:31 +0000514 static_cast<const ARMBaseInstrInfo&>(TII));
515 else {
516 assert(AFI->isThumb2Function());
517 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000518 Reg, Reg, Offset, Pred, PredReg,
Eric Christopher8654c712010-08-23 23:14:31 +0000519 static_cast<const ARMBaseInstrInfo&>(TII));
520 }
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000521
Eric Christopherb1cc8482010-08-25 07:23:49 +0000522 unsigned ResultReg;
Eric Christopher2012c7b2010-08-24 01:10:04 +0000523 // TODO: Verify the additions above work, otherwise we'll need to add the
524 // offset instead of 0 and do all sorts of operand munging.
Eric Christopherb1cc8482010-08-25 07:23:49 +0000525 if (!ARMEmitLoad(VT, ResultReg, Reg, 0)) return false;
526
Eric Christopherf06f3092010-08-24 00:50:47 +0000527 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000528 return true;
529}
530
Eric Christopherab695882010-07-21 22:26:11 +0000531bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000532 // No Thumb-1 for now.
533 if (AFI->isThumbFunction() && !AFI->isThumb2Function()) return false;
534
Eric Christopherab695882010-07-21 22:26:11 +0000535 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000536 case Instruction::Load:
537 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000538 case Instruction::Store:
539 return ARMSelectStore(I);
Eric Christopherab695882010-07-21 22:26:11 +0000540 default: break;
541 }
542 return false;
543}
544
545namespace llvm {
546 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000547 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000548 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000549 }
550}