blob: 866b14fab3a8ff9f25515e42276bce14e179e1db [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 Christopherab695882010-07-21 22:26:11 +0000115
Eric Christopher83007122010-08-23 21:44:12 +0000116 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000117 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000118 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000119 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000120 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000121 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopherf06f3092010-08-24 00:50:47 +0000122 bool ARMLoadAlloca(const Instruction *I);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000123 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000124 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000125 bool ARMMaterializeConstant(const ConstantInt *Val, unsigned &Reg);
Eric Christopher83007122010-08-23 21:44:12 +0000126
Eric Christopher456144e2010-08-19 00:37:05 +0000127 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
128 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
129};
Eric Christopherab695882010-07-21 22:26:11 +0000130
131} // end anonymous namespace
132
133// #include "ARMGenCallingConv.inc"
134
Eric Christopher456144e2010-08-19 00:37:05 +0000135// DefinesOptionalPredicate - This is different from DefinesPredicate in that
136// we don't care about implicit defs here, just places we'll need to add a
137// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
138bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
139 const TargetInstrDesc &TID = MI->getDesc();
140 if (!TID.hasOptionalDef())
141 return false;
142
143 // Look to see if our OptionalDef is defining CPSR or CCR.
144 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
145 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000146 if (!MO.isReg() || !MO.isDef()) continue;
147 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000148 *CPSR = true;
149 }
150 return true;
151}
152
153// If the machine is predicable go ahead and add the predicate operands, if
154// it needs default CC operands add those.
155const MachineInstrBuilder &
156ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
157 MachineInstr *MI = &*MIB;
158
159 // Do we use a predicate?
160 if (TII.isPredicable(MI))
161 AddDefaultPred(MIB);
162
163 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
164 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000165 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000166 if (DefinesOptionalPredicate(MI, &CPSR)) {
167 if (CPSR)
168 AddDefaultT1CC(MIB);
169 else
170 AddDefaultCC(MIB);
171 }
172 return MIB;
173}
174
Eric Christopher0fe7d542010-08-17 01:25:29 +0000175unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
176 const TargetRegisterClass* RC) {
177 unsigned ResultReg = createResultReg(RC);
178 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
179
Eric Christopher456144e2010-08-19 00:37:05 +0000180 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000181 return ResultReg;
182}
183
184unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
185 const TargetRegisterClass *RC,
186 unsigned Op0, bool Op0IsKill) {
187 unsigned ResultReg = createResultReg(RC);
188 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
189
190 if (II.getNumDefs() >= 1)
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 .addReg(Op0, Op0IsKill * RegState::Kill));
193 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000194 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000195 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000196 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000197 TII.get(TargetOpcode::COPY), ResultReg)
198 .addReg(II.ImplicitDefs[0]));
199 }
200 return ResultReg;
201}
202
203unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
204 const TargetRegisterClass *RC,
205 unsigned Op0, bool Op0IsKill,
206 unsigned Op1, bool Op1IsKill) {
207 unsigned ResultReg = createResultReg(RC);
208 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
209
210 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000211 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000212 .addReg(Op0, Op0IsKill * RegState::Kill)
213 .addReg(Op1, Op1IsKill * RegState::Kill));
214 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000215 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000216 .addReg(Op0, Op0IsKill * RegState::Kill)
217 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000218 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000219 TII.get(TargetOpcode::COPY), ResultReg)
220 .addReg(II.ImplicitDefs[0]));
221 }
222 return ResultReg;
223}
224
225unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
226 const TargetRegisterClass *RC,
227 unsigned Op0, bool Op0IsKill,
228 uint64_t Imm) {
229 unsigned ResultReg = createResultReg(RC);
230 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
231
232 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000233 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000234 .addReg(Op0, Op0IsKill * RegState::Kill)
235 .addImm(Imm));
236 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000238 .addReg(Op0, Op0IsKill * RegState::Kill)
239 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000241 TII.get(TargetOpcode::COPY), ResultReg)
242 .addReg(II.ImplicitDefs[0]));
243 }
244 return ResultReg;
245}
246
247unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
248 const TargetRegisterClass *RC,
249 unsigned Op0, bool Op0IsKill,
250 const ConstantFP *FPImm) {
251 unsigned ResultReg = createResultReg(RC);
252 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
253
254 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000255 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000256 .addReg(Op0, Op0IsKill * RegState::Kill)
257 .addFPImm(FPImm));
258 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000259 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000260 .addReg(Op0, Op0IsKill * RegState::Kill)
261 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000262 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000263 TII.get(TargetOpcode::COPY), ResultReg)
264 .addReg(II.ImplicitDefs[0]));
265 }
266 return ResultReg;
267}
268
269unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
270 const TargetRegisterClass *RC,
271 unsigned Op0, bool Op0IsKill,
272 unsigned Op1, bool Op1IsKill,
273 uint64_t Imm) {
274 unsigned ResultReg = createResultReg(RC);
275 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
276
277 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000278 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000279 .addReg(Op0, Op0IsKill * RegState::Kill)
280 .addReg(Op1, Op1IsKill * RegState::Kill)
281 .addImm(Imm));
282 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 .addReg(Op0, Op0IsKill * RegState::Kill)
285 .addReg(Op1, Op1IsKill * RegState::Kill)
286 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000287 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000288 TII.get(TargetOpcode::COPY), ResultReg)
289 .addReg(II.ImplicitDefs[0]));
290 }
291 return ResultReg;
292}
293
294unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
295 const TargetRegisterClass *RC,
296 uint64_t Imm) {
297 unsigned ResultReg = createResultReg(RC);
298 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
299
300 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000302 .addImm(Imm));
303 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 TII.get(TargetOpcode::COPY), ResultReg)
308 .addReg(II.ImplicitDefs[0]));
309 }
310 return ResultReg;
311}
312
313unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
314 unsigned Op0, bool Op0IsKill,
315 uint32_t Idx) {
316 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
317 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
318 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000319 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000320 DL, TII.get(TargetOpcode::COPY), ResultReg)
321 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
322 return ResultReg;
323}
324
Eric Christopher1b61ef42010-09-02 01:48:11 +0000325unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
326 const ConstantInt *CI = dyn_cast<ConstantInt>(C);
327 if (!CI) return 0;
328
329 unsigned Opc;
330 bool Signed = true;
331 EVT VT = TLI.getValueType(CI->getType(), true);
332
333 switch (VT.getSimpleVT().SimpleTy) {
334 default: return 0;
335 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
336 case MVT::i8:
337 case MVT::i16:
338 case MVT::i32:
339 Opc = isThumb ? ARM::t2MOVi32imm : ARM::MOVi32imm; break;
340 }
341
342 unsigned Reg = createResultReg(TLI.getRegClassFor(VT));
343 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
344 Reg)
345 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
346 CI->getZExtValue()));
347 return Reg;
348}
349
Eric Christopherb1cc8482010-08-25 07:23:49 +0000350bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
351 VT = TLI.getValueType(Ty, true);
352
353 // Only handle simple types.
354 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000355
Eric Christopherdc908042010-08-31 01:28:42 +0000356 // Handle all legal types, i.e. a register that will directly hold this
357 // value.
358 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000359}
360
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000361bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
362 if (isTypeLegal(Ty, VT)) return true;
363
364 // If this is a type than can be sign or zero-extended to a basic operation
365 // go ahead and accept it now.
366 if (VT == MVT::i8 || VT == MVT::i16)
367 return true;
368
369 return false;
370}
371
Eric Christophercb0b04b2010-08-24 00:07:24 +0000372// Computes the Reg+Offset to get to an object.
373bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000374 int &Offset) {
375 // Some boilerplate from the X86 FastISel.
376 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000377 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000378 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000379 // Don't walk into other basic blocks; it's possible we haven't
380 // visited them yet, so the instructions may not yet be assigned
381 // virtual registers.
382 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
383 return false;
384
385 Opcode = I->getOpcode();
386 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000387 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000388 Opcode = C->getOpcode();
389 U = C;
390 }
391
Eric Christophercb0b04b2010-08-24 00:07:24 +0000392 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000393 if (Ty->getAddressSpace() > 255)
394 // Fast instruction selection doesn't support the special
395 // address spaces.
396 return false;
397
398 switch (Opcode) {
399 default:
400 //errs() << "Failing Opcode is: " << *Op1 << "\n";
401 break;
402 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000403 assert(false && "Alloca should have been handled earlier!");
404 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000405 }
406 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000407
408 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
409 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000410 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000411 return false;
412 }
413
414 // Try to get this in a register if nothing else has worked.
415 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000416 if (Reg == 0) return false;
417
418 // Since the offset may be too large for the load instruction
419 // get the reg+offset into a register.
420 // TODO: Verify the additions work, otherwise we'll need to add the
421 // offset instead of 0 to the instructions and do all sorts of operand
422 // munging.
423 // TODO: Optimize this somewhat.
424 if (Offset != 0) {
425 ARMCC::CondCodes Pred = ARMCC::AL;
426 unsigned PredReg = 0;
427
Eric Christophereaa204b2010-09-02 01:39:14 +0000428 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000429 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
430 Reg, Reg, Offset, Pred, PredReg,
431 static_cast<const ARMBaseInstrInfo&>(TII));
432 else {
433 assert(AFI->isThumb2Function());
434 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
435 Reg, Reg, Offset, Pred, PredReg,
436 static_cast<const ARMBaseInstrInfo&>(TII));
437 }
438 }
439
440 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000441}
442
Eric Christopherf06f3092010-08-24 00:50:47 +0000443bool ARMFastISel::ARMLoadAlloca(const Instruction *I) {
444 Value *Op0 = I->getOperand(0);
445
446 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000447 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
448 DenseMap<const AllocaInst*, int>::iterator SI =
449 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000450
Eric Christophere24d66f2010-08-24 22:07:27 +0000451 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000452 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
453 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000454 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000455 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000456 TM.getRegisterInfo());
457 UpdateValueMap(I, ResultReg);
458 return true;
459 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000460 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000461 return false;
462}
463
Eric Christopherb1cc8482010-08-25 07:23:49 +0000464bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
465 unsigned Reg, int Offset) {
466
467 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000468 unsigned Opc;
469
Eric Christopherb1cc8482010-08-25 07:23:49 +0000470 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000471 default:
472 assert(false && "Trying to emit for an unhandled type!");
473 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000474 case MVT::i16:
475 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
476 VT = MVT::i32;
477 break;
478 case MVT::i8:
479 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
480 VT = MVT::i32;
481 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000482 case MVT::i32:
483 Opc = isThumb ? ARM::tLDR : ARM::LDR;
484 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000485 }
Eric Christopherdc908042010-08-31 01:28:42 +0000486
487 ResultReg = createResultReg(TLI.getRegClassFor(VT));
488
489 // TODO: Fix the Addressing modes so that these can share some code.
490 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
491 if (isThumb)
492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
493 TII.get(Opc), ResultReg)
494 .addReg(Reg).addImm(Offset).addReg(0));
495 else
496 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
497 TII.get(Opc), ResultReg)
498 .addReg(Reg).addReg(0).addImm(Offset));
499
500 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000501}
502
Eric Christopher318b6ee2010-09-02 00:53:56 +0000503bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg) {
Eric Christopher543cf052010-09-01 22:16:27 +0000504 Value *Op1 = I->getOperand(1);
505
506 // Verify it's an alloca.
507 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
508 DenseMap<const AllocaInst*, int>::iterator SI =
509 FuncInfo.StaticAllocaMap.find(AI);
510
511 if (SI != FuncInfo.StaticAllocaMap.end()) {
512 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Eric Christopher318b6ee2010-09-02 00:53:56 +0000513 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000514 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000515 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000516 TM.getRegisterInfo());
517 return true;
518 }
519 }
520 return false;
521}
522
Eric Christopher318b6ee2010-09-02 00:53:56 +0000523bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
524 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000525 unsigned StrOpc;
526 switch (VT.getSimpleVT().SimpleTy) {
527 default: return false;
528 case MVT::i1:
529 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
530 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
531 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
532 }
533
534 if (isThumb)
535 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
536 TII.get(StrOpc), SrcReg)
537 .addReg(DstReg).addImm(Offset).addReg(0));
538 else
539 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
540 TII.get(StrOpc), SrcReg)
541 .addReg(DstReg).addReg(0).addImm(Offset));
542
543 return true;
544}
545
546bool ARMFastISel::ARMSelectStore(const Instruction *I) {
547 Value *Op0 = I->getOperand(0);
548 unsigned SrcReg = 0;
549
Eric Christopher543cf052010-09-01 22:16:27 +0000550 // Yay type legalization
551 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000552 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000553 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000554
Eric Christopher1b61ef42010-09-02 01:48:11 +0000555 // Get the value to be stored into a register.
556 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000557 if (SrcReg == 0)
558 return false;
559
560 // If we're an alloca we know we have a frame index and can emit the store
561 // quickly.
562 if (ARMStoreAlloca(I, SrcReg))
563 return true;
564
565 // Our register and offset with innocuous defaults.
566 unsigned Reg = 0;
567 int Offset = 0;
568
569 // See if we can handle this as Reg + Offset
570 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
571 return false;
572
573 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
574
Eric Christopher543cf052010-09-01 22:16:27 +0000575 return false;
576
577}
578
Eric Christopher83007122010-08-23 21:44:12 +0000579bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher882d62e2010-08-24 01:10:52 +0000580 // If we're an alloca we know we have a frame index and can emit the load
581 // directly in short order.
Eric Christopherf06f3092010-08-24 00:50:47 +0000582 if (ARMLoadAlloca(I))
583 return true;
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000584
585 // Verify we have a legal type before going any further.
586 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000587 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000588 return false;
589
590 // Our register and offset with innocuous defaults.
591 unsigned Reg = 0;
592 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000593
Eric Christopher83007122010-08-23 21:44:12 +0000594 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000595 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000596 return false;
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000597
Eric Christopherb1cc8482010-08-25 07:23:49 +0000598 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000599 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000600
Eric Christopherf06f3092010-08-24 00:50:47 +0000601 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000602 return true;
603}
604
Eric Christopherab695882010-07-21 22:26:11 +0000605bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000606 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000607 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopher7fe55b72010-08-23 22:32:45 +0000608
Eric Christopherab695882010-07-21 22:26:11 +0000609 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000610 case Instruction::Load:
611 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000612 case Instruction::Store:
613 return ARMSelectStore(I);
Eric Christopherab695882010-07-21 22:26:11 +0000614 default: break;
615 }
616 return false;
617}
618
619namespace llvm {
620 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000621 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000622 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000623 }
624}