blob: 64ab2467d8277ac047174f1b879758983c51760b [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);
108
109 #include "ARMGenFastISel.inc"
Eric Christopher83007122010-08-23 21:44:12 +0000110
111 // Instruction selection routines.
112 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000113 virtual bool ARMSelectStore(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000114
Eric Christopher83007122010-08-23 21:44:12 +0000115 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000116 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000117 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000118 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000119 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000120 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopherf06f3092010-08-24 00:50:47 +0000121 bool ARMLoadAlloca(const Instruction *I);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000122 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000123 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000124 bool ARMMaterializeConstant(const ConstantInt *Val, unsigned &Reg);
Eric Christopher83007122010-08-23 21:44:12 +0000125
Eric Christopher456144e2010-08-19 00:37:05 +0000126 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
127 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
128};
Eric Christopherab695882010-07-21 22:26:11 +0000129
130} // end anonymous namespace
131
132// #include "ARMGenCallingConv.inc"
133
Eric Christopher456144e2010-08-19 00:37:05 +0000134// DefinesOptionalPredicate - This is different from DefinesPredicate in that
135// we don't care about implicit defs here, just places we'll need to add a
136// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
137bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
138 const TargetInstrDesc &TID = MI->getDesc();
139 if (!TID.hasOptionalDef())
140 return false;
141
142 // Look to see if our OptionalDef is defining CPSR or CCR.
143 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
144 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000145 if (!MO.isReg() || !MO.isDef()) continue;
146 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000147 *CPSR = true;
148 }
149 return true;
150}
151
152// If the machine is predicable go ahead and add the predicate operands, if
153// it needs default CC operands add those.
154const MachineInstrBuilder &
155ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
156 MachineInstr *MI = &*MIB;
157
158 // Do we use a predicate?
159 if (TII.isPredicable(MI))
160 AddDefaultPred(MIB);
161
162 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
163 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000164 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000165 if (DefinesOptionalPredicate(MI, &CPSR)) {
166 if (CPSR)
167 AddDefaultT1CC(MIB);
168 else
169 AddDefaultCC(MIB);
170 }
171 return MIB;
172}
173
Eric Christopher0fe7d542010-08-17 01:25:29 +0000174unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
175 const TargetRegisterClass* RC) {
176 unsigned ResultReg = createResultReg(RC);
177 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
178
Eric Christopher456144e2010-08-19 00:37:05 +0000179 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000180 return ResultReg;
181}
182
183unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
184 const TargetRegisterClass *RC,
185 unsigned Op0, bool Op0IsKill) {
186 unsigned ResultReg = createResultReg(RC);
187 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
188
189 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000190 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000191 .addReg(Op0, Op0IsKill * RegState::Kill));
192 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000193 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000194 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000195 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000196 TII.get(TargetOpcode::COPY), ResultReg)
197 .addReg(II.ImplicitDefs[0]));
198 }
199 return ResultReg;
200}
201
202unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
203 const TargetRegisterClass *RC,
204 unsigned Op0, bool Op0IsKill,
205 unsigned Op1, bool Op1IsKill) {
206 unsigned ResultReg = createResultReg(RC);
207 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
208
209 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000210 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000211 .addReg(Op0, Op0IsKill * RegState::Kill)
212 .addReg(Op1, Op1IsKill * RegState::Kill));
213 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000215 .addReg(Op0, Op0IsKill * RegState::Kill)
216 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000217 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000218 TII.get(TargetOpcode::COPY), ResultReg)
219 .addReg(II.ImplicitDefs[0]));
220 }
221 return ResultReg;
222}
223
224unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
225 const TargetRegisterClass *RC,
226 unsigned Op0, bool Op0IsKill,
227 uint64_t Imm) {
228 unsigned ResultReg = createResultReg(RC);
229 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
230
231 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000232 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000233 .addReg(Op0, Op0IsKill * RegState::Kill)
234 .addImm(Imm));
235 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 .addReg(Op0, Op0IsKill * RegState::Kill)
238 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000239 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000240 TII.get(TargetOpcode::COPY), ResultReg)
241 .addReg(II.ImplicitDefs[0]));
242 }
243 return ResultReg;
244}
245
246unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
247 const TargetRegisterClass *RC,
248 unsigned Op0, bool Op0IsKill,
249 const ConstantFP *FPImm) {
250 unsigned ResultReg = createResultReg(RC);
251 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
252
253 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000254 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000255 .addReg(Op0, Op0IsKill * RegState::Kill)
256 .addFPImm(FPImm));
257 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000259 .addReg(Op0, Op0IsKill * RegState::Kill)
260 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000262 TII.get(TargetOpcode::COPY), ResultReg)
263 .addReg(II.ImplicitDefs[0]));
264 }
265 return ResultReg;
266}
267
268unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
269 const TargetRegisterClass *RC,
270 unsigned Op0, bool Op0IsKill,
271 unsigned Op1, bool Op1IsKill,
272 uint64_t Imm) {
273 unsigned ResultReg = createResultReg(RC);
274 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
275
276 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000277 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000278 .addReg(Op0, Op0IsKill * RegState::Kill)
279 .addReg(Op1, Op1IsKill * RegState::Kill)
280 .addImm(Imm));
281 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000282 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000283 .addReg(Op0, Op0IsKill * RegState::Kill)
284 .addReg(Op1, Op1IsKill * RegState::Kill)
285 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 TII.get(TargetOpcode::COPY), ResultReg)
288 .addReg(II.ImplicitDefs[0]));
289 }
290 return ResultReg;
291}
292
293unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
294 const TargetRegisterClass *RC,
295 uint64_t Imm) {
296 unsigned ResultReg = createResultReg(RC);
297 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
298
299 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000300 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000301 .addImm(Imm));
302 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000304 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000306 TII.get(TargetOpcode::COPY), ResultReg)
307 .addReg(II.ImplicitDefs[0]));
308 }
309 return ResultReg;
310}
311
312unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
313 unsigned Op0, bool Op0IsKill,
314 uint32_t Idx) {
315 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
316 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
317 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000318 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000319 DL, TII.get(TargetOpcode::COPY), ResultReg)
320 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
321 return ResultReg;
322}
323
Eric Christopherb1cc8482010-08-25 07:23:49 +0000324bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
325 VT = TLI.getValueType(Ty, true);
326
327 // Only handle simple types.
328 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000329
Eric Christopherdc908042010-08-31 01:28:42 +0000330 // Handle all legal types, i.e. a register that will directly hold this
331 // value.
332 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000333}
334
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000335bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
336 if (isTypeLegal(Ty, VT)) return true;
337
338 // If this is a type than can be sign or zero-extended to a basic operation
339 // go ahead and accept it now.
340 if (VT == MVT::i8 || VT == MVT::i16)
341 return true;
342
343 return false;
344}
345
Eric Christophercb0b04b2010-08-24 00:07:24 +0000346// Computes the Reg+Offset to get to an object.
347bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000348 int &Offset) {
349 // Some boilerplate from the X86 FastISel.
350 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000351 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000352 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000353 // Don't walk into other basic blocks; it's possible we haven't
354 // visited them yet, so the instructions may not yet be assigned
355 // virtual registers.
356 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
357 return false;
358
359 Opcode = I->getOpcode();
360 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000361 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000362 Opcode = C->getOpcode();
363 U = C;
364 }
365
Eric Christophercb0b04b2010-08-24 00:07:24 +0000366 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000367 if (Ty->getAddressSpace() > 255)
368 // Fast instruction selection doesn't support the special
369 // address spaces.
370 return false;
371
372 switch (Opcode) {
373 default:
374 //errs() << "Failing Opcode is: " << *Op1 << "\n";
375 break;
376 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000377 assert(false && "Alloca should have been handled earlier!");
378 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000379 }
380 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000381
382 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
383 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000384 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000385 return false;
386 }
387
388 // Try to get this in a register if nothing else has worked.
389 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000390 if (Reg == 0) return false;
391
392 // Since the offset may be too large for the load instruction
393 // get the reg+offset into a register.
394 // TODO: Verify the additions work, otherwise we'll need to add the
395 // offset instead of 0 to the instructions and do all sorts of operand
396 // munging.
397 // TODO: Optimize this somewhat.
398 if (Offset != 0) {
399 ARMCC::CondCodes Pred = ARMCC::AL;
400 unsigned PredReg = 0;
401
Eric Christophereaa204b2010-09-02 01:39:14 +0000402 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000403 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
404 Reg, Reg, Offset, Pred, PredReg,
405 static_cast<const ARMBaseInstrInfo&>(TII));
406 else {
407 assert(AFI->isThumb2Function());
408 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
409 Reg, Reg, Offset, Pred, PredReg,
410 static_cast<const ARMBaseInstrInfo&>(TII));
411 }
412 }
413
414 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000415}
416
Eric Christopherf06f3092010-08-24 00:50:47 +0000417bool ARMFastISel::ARMLoadAlloca(const Instruction *I) {
418 Value *Op0 = I->getOperand(0);
419
420 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000421 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
422 DenseMap<const AllocaInst*, int>::iterator SI =
423 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000424
Eric Christophere24d66f2010-08-24 22:07:27 +0000425 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopherb1cc8482010-08-25 07:23:49 +0000426 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
427 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000428 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000429 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000430 TM.getRegisterInfo());
431 UpdateValueMap(I, ResultReg);
432 return true;
433 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000434 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000435 return false;
436}
437
Eric Christopherb1cc8482010-08-25 07:23:49 +0000438bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
439 unsigned Reg, int Offset) {
440
441 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000442 unsigned Opc;
443
Eric Christopherb1cc8482010-08-25 07:23:49 +0000444 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000445 default:
446 assert(false && "Trying to emit for an unhandled type!");
447 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000448 case MVT::i16:
449 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
450 VT = MVT::i32;
451 break;
452 case MVT::i8:
453 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
454 VT = MVT::i32;
455 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000456 case MVT::i32:
457 Opc = isThumb ? ARM::tLDR : ARM::LDR;
458 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000459 }
Eric Christopherdc908042010-08-31 01:28:42 +0000460
461 ResultReg = createResultReg(TLI.getRegClassFor(VT));
462
463 // TODO: Fix the Addressing modes so that these can share some code.
464 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
465 if (isThumb)
466 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
467 TII.get(Opc), ResultReg)
468 .addReg(Reg).addImm(Offset).addReg(0));
469 else
470 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
471 TII.get(Opc), ResultReg)
472 .addReg(Reg).addReg(0).addImm(Offset));
473
474 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000475}
476
Eric Christopher318b6ee2010-09-02 00:53:56 +0000477bool ARMFastISel::ARMMaterializeConstant(const ConstantInt *CI, unsigned &Reg) {
478 unsigned Opc;
479 bool Signed = true;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000480 EVT VT = TLI.getValueType(CI->getType(), true);
481
482 switch (VT.getSimpleVT().SimpleTy) {
483 default: return false;
484 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
485 case MVT::i8:
486 case MVT::i16:
487 case MVT::i32:
488 Opc = isThumb ? ARM::t2MOVi32imm : ARM::MOVi32imm; break;
489 }
490
491 Reg = createResultReg(TLI.getRegClassFor(VT));
492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
493 Reg)
494 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
495 CI->getZExtValue()));
496
497 return true;
498}
499
500bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg) {
Eric Christopher543cf052010-09-01 22:16:27 +0000501 Value *Op1 = I->getOperand(1);
502
503 // Verify it's an alloca.
504 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
505 DenseMap<const AllocaInst*, int>::iterator SI =
506 FuncInfo.StaticAllocaMap.find(AI);
507
508 if (SI != FuncInfo.StaticAllocaMap.end()) {
509 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Eric Christopher318b6ee2010-09-02 00:53:56 +0000510 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000511 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000512 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000513 TM.getRegisterInfo());
514 return true;
515 }
516 }
517 return false;
518}
519
Eric Christopher318b6ee2010-09-02 00:53:56 +0000520bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
521 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000522 unsigned StrOpc;
523 switch (VT.getSimpleVT().SimpleTy) {
524 default: return false;
525 case MVT::i1:
526 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
527 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
528 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
529 }
530
531 if (isThumb)
532 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
533 TII.get(StrOpc), SrcReg)
534 .addReg(DstReg).addImm(Offset).addReg(0));
535 else
536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
537 TII.get(StrOpc), SrcReg)
538 .addReg(DstReg).addReg(0).addImm(Offset));
539
540 return true;
541}
542
543bool ARMFastISel::ARMSelectStore(const Instruction *I) {
544 Value *Op0 = I->getOperand(0);
545 unsigned SrcReg = 0;
546
Eric Christopher543cf052010-09-01 22:16:27 +0000547 // Yay type legalization
548 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000549 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000550 return false;
551
Eric Christopher318b6ee2010-09-02 00:53:56 +0000552 // First see if we're a constant that we want to store, we'll need to
553 // materialize that into a register.
554 // Handle 'null' like i32/i64 0.
555 if (isa<ConstantPointerNull>(Op0))
556 Op0 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
557
558 // If this is a store of a simple constant, materialize the constant into
559 // a register then emit the store into the location.
560 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
561 if (!ARMMaterializeConstant(CI, SrcReg))
562 return false;
563
564 // If Reg is still 0, try to get the value into a register.
565 if (SrcReg == 0)
566 SrcReg = getRegForValue(Op0);
567 if (SrcReg == 0)
568 return false;
569
570 // If we're an alloca we know we have a frame index and can emit the store
571 // quickly.
572 if (ARMStoreAlloca(I, SrcReg))
573 return true;
574
575 // Our register and offset with innocuous defaults.
576 unsigned Reg = 0;
577 int Offset = 0;
578
579 // See if we can handle this as Reg + Offset
580 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
581 return false;
582
583 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
584
Eric Christopher543cf052010-09-01 22:16:27 +0000585 return false;
586
587}
588
Eric Christopher83007122010-08-23 21:44:12 +0000589bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher882d62e2010-08-24 01:10:52 +0000590 // If we're an alloca we know we have a frame index and can emit the load
591 // directly in short order.
Eric Christopherf06f3092010-08-24 00:50:47 +0000592 if (ARMLoadAlloca(I))
593 return true;
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000594
595 // Verify we have a legal type before going any further.
596 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000597 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000598 return false;
599
600 // Our register and offset with innocuous defaults.
601 unsigned Reg = 0;
602 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000603
Eric Christopher83007122010-08-23 21:44:12 +0000604 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000605 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000606 return false;
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000607
Eric Christopherb1cc8482010-08-25 07:23:49 +0000608 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000609 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000610
Eric Christopherf06f3092010-08-24 00:50:47 +0000611 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000612 return true;
613}
614
Eric Christopherab695882010-07-21 22:26:11 +0000615bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000616 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000617 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopher7fe55b72010-08-23 22:32:45 +0000618
Eric Christopherab695882010-07-21 22:26:11 +0000619 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000620 case Instruction::Load:
621 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000622 case Instruction::Store:
623 return ARMSelectStore(I);
Eric Christopherab695882010-07-21 22:26:11 +0000624 default: break;
625 }
626 return false;
627}
628
629namespace llvm {
630 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000631 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000632 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000633 }
634}