blob: ca42079eb808f3218e00503b93906666c654d045 [file] [log] [blame]
Eric Christopherab695882010-07-21 22:26:11 +00001//===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ARM-specific support for the FastISel class. Some
11// of the target-specific code is generated by tablegen in the file
12// ARMGenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
Eric Christopher456144e2010-08-19 00:37:05 +000017#include "ARMBaseInstrInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000018#include "ARMRegisterInfo.h"
19#include "ARMTargetMachine.h"
20#include "ARMSubtarget.h"
21#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Instructions.h"
25#include "llvm/IntrinsicInst.h"
26#include "llvm/CodeGen/Analysis.h"
27#include "llvm/CodeGen/FastISel.h"
28#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
Eric Christopherab695882010-07-21 22:26:11 +000031#include "llvm/CodeGen/MachineConstantPool.h"
32#include "llvm/CodeGen/MachineFrameInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/Support/CallSite.h"
Eric Christopher038fea52010-08-17 00:46:57 +000035#include "llvm/Support/CommandLine.h"
Eric Christopherab695882010-07-21 22:26:11 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/GetElementPtrTypeIterator.h"
Eric Christopher0fe7d542010-08-17 01:25:29 +000038#include "llvm/Target/TargetData.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetLowering.h"
41#include "llvm/Target/TargetMachine.h"
Eric Christopherab695882010-07-21 22:26:11 +000042#include "llvm/Target/TargetOptions.h"
43using namespace llvm;
44
Eric Christopher038fea52010-08-17 00:46:57 +000045static cl::opt<bool>
46EnableARMFastISel("arm-fast-isel",
47 cl::desc("Turn on experimental ARM fast-isel support"),
48 cl::init(false), cl::Hidden);
49
Eric Christopherab695882010-07-21 22:26:11 +000050namespace {
51
52class ARMFastISel : public FastISel {
53
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
56 const ARMSubtarget *Subtarget;
Eric Christopher0fe7d542010-08-17 01:25:29 +000057 const TargetMachine &TM;
58 const TargetInstrInfo &TII;
59 const TargetLowering &TLI;
Eric Christopher7fe55b72010-08-23 22:32:45 +000060 const ARMFunctionInfo *AFI;
Eric Christopherab695882010-07-21 22:26:11 +000061
Eric Christophereaa204b2010-09-02 01:39:14 +000062 // Convenience variable to avoid checking all the time.
63 bool isThumb;
64
Eric Christopherab695882010-07-21 22:26:11 +000065 public:
Eric Christopher0fe7d542010-08-17 01:25:29 +000066 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
67 : FastISel(funcInfo),
68 TM(funcInfo.MF->getTarget()),
69 TII(*TM.getInstrInfo()),
70 TLI(*TM.getTargetLowering()) {
Eric Christopherab695882010-07-21 22:26:11 +000071 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopher7fe55b72010-08-23 22:32:45 +000072 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
Eric Christophereaa204b2010-09-02 01:39:14 +000073 isThumb = AFI->isThumbFunction();
Eric Christopherab695882010-07-21 22:26:11 +000074 }
75
Eric Christophercb592292010-08-20 00:20:31 +000076 // Code from FastISel.cpp.
Eric Christopher0fe7d542010-08-17 01:25:29 +000077 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
78 const TargetRegisterClass *RC);
79 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
80 const TargetRegisterClass *RC,
81 unsigned Op0, bool Op0IsKill);
82 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC,
84 unsigned Op0, bool Op0IsKill,
85 unsigned Op1, bool Op1IsKill);
86 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 uint64_t Imm);
90 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
92 unsigned Op0, bool Op0IsKill,
93 const ConstantFP *FPImm);
94 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
95 const TargetRegisterClass *RC,
96 uint64_t Imm);
97 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 unsigned Op1, bool Op1IsKill,
101 uint64_t Imm);
102 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
103 unsigned Op0, bool Op0IsKill,
104 uint32_t Idx);
Eric Christophercb592292010-08-20 00:20:31 +0000105
106 // Backend specific FastISel code.
Eric Christopherab695882010-07-21 22:26:11 +0000107 virtual bool TargetSelectInstruction(const Instruction *I);
Eric Christopher1b61ef42010-09-02 01:48:11 +0000108 virtual unsigned TargetMaterializeConstant(const Constant *C);
Eric Christopherab695882010-07-21 22:26:11 +0000109
110 #include "ARMGenFastISel.inc"
Eric Christopher83007122010-08-23 21:44:12 +0000111
112 // Instruction selection routines.
113 virtual bool ARMSelectLoad(const Instruction *I);
Eric Christopher543cf052010-09-01 22:16:27 +0000114 virtual bool ARMSelectStore(const Instruction *I);
Eric Christophere5734102010-09-03 00:35:47 +0000115 virtual bool ARMSelectBranch(const Instruction *I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000116 virtual bool ARMSelectCmp(const Instruction *I);
Eric Christopherab695882010-07-21 22:26:11 +0000117
Eric Christopher83007122010-08-23 21:44:12 +0000118 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000119 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000120 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000121 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000122 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000123 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000124 bool ARMLoadAlloca(const Instruction *I, EVT VT);
125 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000126 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000127 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
128 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopher83007122010-08-23 21:44:12 +0000129
Eric Christopher456144e2010-08-19 00:37:05 +0000130 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
131 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
132};
Eric Christopherab695882010-07-21 22:26:11 +0000133
134} // end anonymous namespace
135
136// #include "ARMGenCallingConv.inc"
137
Eric Christopher456144e2010-08-19 00:37:05 +0000138// DefinesOptionalPredicate - This is different from DefinesPredicate in that
139// we don't care about implicit defs here, just places we'll need to add a
140// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
141bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
142 const TargetInstrDesc &TID = MI->getDesc();
143 if (!TID.hasOptionalDef())
144 return false;
145
146 // Look to see if our OptionalDef is defining CPSR or CCR.
147 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
148 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000149 if (!MO.isReg() || !MO.isDef()) continue;
150 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000151 *CPSR = true;
152 }
153 return true;
154}
155
156// If the machine is predicable go ahead and add the predicate operands, if
157// it needs default CC operands add those.
158const MachineInstrBuilder &
159ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
160 MachineInstr *MI = &*MIB;
161
162 // Do we use a predicate?
163 if (TII.isPredicable(MI))
164 AddDefaultPred(MIB);
165
166 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
167 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000168 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000169 if (DefinesOptionalPredicate(MI, &CPSR)) {
170 if (CPSR)
171 AddDefaultT1CC(MIB);
172 else
173 AddDefaultCC(MIB);
174 }
175 return MIB;
176}
177
Eric Christopher0fe7d542010-08-17 01:25:29 +0000178unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
179 const TargetRegisterClass* RC) {
180 unsigned ResultReg = createResultReg(RC);
181 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
182
Eric Christopher456144e2010-08-19 00:37:05 +0000183 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000184 return ResultReg;
185}
186
187unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
188 const TargetRegisterClass *RC,
189 unsigned Op0, bool Op0IsKill) {
190 unsigned ResultReg = createResultReg(RC);
191 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
192
193 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000194 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000195 .addReg(Op0, Op0IsKill * RegState::Kill));
196 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000197 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000198 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000199 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000200 TII.get(TargetOpcode::COPY), ResultReg)
201 .addReg(II.ImplicitDefs[0]));
202 }
203 return ResultReg;
204}
205
206unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
207 const TargetRegisterClass *RC,
208 unsigned Op0, bool Op0IsKill,
209 unsigned Op1, bool Op1IsKill) {
210 unsigned ResultReg = createResultReg(RC);
211 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
212
213 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000214 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000215 .addReg(Op0, Op0IsKill * RegState::Kill)
216 .addReg(Op1, Op1IsKill * RegState::Kill));
217 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000218 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000219 .addReg(Op0, Op0IsKill * RegState::Kill)
220 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000221 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000222 TII.get(TargetOpcode::COPY), ResultReg)
223 .addReg(II.ImplicitDefs[0]));
224 }
225 return ResultReg;
226}
227
228unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
229 const TargetRegisterClass *RC,
230 unsigned Op0, bool Op0IsKill,
231 uint64_t Imm) {
232 unsigned ResultReg = createResultReg(RC);
233 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
234
235 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000236 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000237 .addReg(Op0, Op0IsKill * RegState::Kill)
238 .addImm(Imm));
239 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000241 .addReg(Op0, Op0IsKill * RegState::Kill)
242 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000243 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000244 TII.get(TargetOpcode::COPY), ResultReg)
245 .addReg(II.ImplicitDefs[0]));
246 }
247 return ResultReg;
248}
249
250unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
251 const TargetRegisterClass *RC,
252 unsigned Op0, bool Op0IsKill,
253 const ConstantFP *FPImm) {
254 unsigned ResultReg = createResultReg(RC);
255 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
256
257 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000258 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000259 .addReg(Op0, Op0IsKill * RegState::Kill)
260 .addFPImm(FPImm));
261 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000262 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000263 .addReg(Op0, Op0IsKill * RegState::Kill)
264 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000265 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000266 TII.get(TargetOpcode::COPY), ResultReg)
267 .addReg(II.ImplicitDefs[0]));
268 }
269 return ResultReg;
270}
271
272unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
273 const TargetRegisterClass *RC,
274 unsigned Op0, bool Op0IsKill,
275 unsigned Op1, bool Op1IsKill,
276 uint64_t Imm) {
277 unsigned ResultReg = createResultReg(RC);
278 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
279
280 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000281 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000282 .addReg(Op0, Op0IsKill * RegState::Kill)
283 .addReg(Op1, Op1IsKill * RegState::Kill)
284 .addImm(Imm));
285 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000287 .addReg(Op0, Op0IsKill * RegState::Kill)
288 .addReg(Op1, Op1IsKill * RegState::Kill)
289 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000290 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000291 TII.get(TargetOpcode::COPY), ResultReg)
292 .addReg(II.ImplicitDefs[0]));
293 }
294 return ResultReg;
295}
296
297unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
298 const TargetRegisterClass *RC,
299 uint64_t Imm) {
300 unsigned ResultReg = createResultReg(RC);
301 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
302
303 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000305 .addImm(Imm));
306 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000308 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 TII.get(TargetOpcode::COPY), ResultReg)
311 .addReg(II.ImplicitDefs[0]));
312 }
313 return ResultReg;
314}
315
316unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
317 unsigned Op0, bool Op0IsKill,
318 uint32_t Idx) {
319 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
320 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
321 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000322 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000323 DL, TII.get(TargetOpcode::COPY), ResultReg)
324 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
325 return ResultReg;
326}
327
Eric Christopher9ed58df2010-09-09 00:19:41 +0000328// For double width floating point we need to materialize two constants
329// (the high and the low) into integer registers then use a move to get
330// the combined constant into an FP reg.
331unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
332 const APFloat Val = CFP->getValueAPF();
333 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopher56d2b722010-09-02 23:43:26 +0000334
Eric Christopher9ed58df2010-09-09 00:19:41 +0000335 // This checks to see if we can use VFP3 instructions to materialize
336 // a constant, otherwise we have to go through the constant pool.
337 if (TLI.isFPImmLegal(Val, VT)) {
338 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
339 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
340 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
341 DestReg)
342 .addFPImm(CFP));
343 return DestReg;
344 }
345
346 // No 64-bit at the moment.
347 if (is64bit) return 0;
348
349 // Load this from the constant pool.
350 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000351
Eric Christopher9ed58df2010-09-09 00:19:41 +0000352 // If we have a floating point constant we expect it in a floating point
353 // register.
354 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
356 TII.get(ARM::VMOVRS), MoveReg)
357 .addReg(DestReg));
358 return MoveReg;
359}
360
361unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000362 // MachineConstantPool wants an explicit alignment.
363 unsigned Align = TD.getPrefTypeAlignment(C->getType());
364 if (Align == 0) {
365 // TODO: Figure out if this is correct.
366 Align = TD.getTypeAllocSize(C->getType());
367 }
368 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
369
Eric Christopher845c5752010-09-08 18:56:34 +0000370 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000371 if (isThumb)
372 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
373 TII.get(ARM::t2LDRpci))
374 .addReg(DestReg).addConstantPoolIndex(Idx));
375 else
376 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
377 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000378 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000379 .addReg(0).addImm(0));
380
381 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000382}
383
Eric Christopher9ed58df2010-09-09 00:19:41 +0000384unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
385 EVT VT = TLI.getValueType(C->getType(), true);
386
387 // Only handle simple types.
388 if (!VT.isSimple()) return 0;
389
390 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
391 return ARMMaterializeFP(CFP, VT);
392 return ARMMaterializeInt(C);
393}
394
Eric Christopherb1cc8482010-08-25 07:23:49 +0000395bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
396 VT = TLI.getValueType(Ty, true);
397
398 // Only handle simple types.
399 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000400
Eric Christopherdc908042010-08-31 01:28:42 +0000401 // Handle all legal types, i.e. a register that will directly hold this
402 // value.
403 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000404}
405
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000406bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
407 if (isTypeLegal(Ty, VT)) return true;
408
409 // If this is a type than can be sign or zero-extended to a basic operation
410 // go ahead and accept it now.
411 if (VT == MVT::i8 || VT == MVT::i16)
412 return true;
413
414 return false;
415}
416
Eric Christophercb0b04b2010-08-24 00:07:24 +0000417// Computes the Reg+Offset to get to an object.
418bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000419 int &Offset) {
420 // Some boilerplate from the X86 FastISel.
421 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000422 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000423 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000424 // Don't walk into other basic blocks; it's possible we haven't
425 // visited them yet, so the instructions may not yet be assigned
426 // virtual registers.
427 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
428 return false;
429
430 Opcode = I->getOpcode();
431 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000432 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000433 Opcode = C->getOpcode();
434 U = C;
435 }
436
Eric Christophercb0b04b2010-08-24 00:07:24 +0000437 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000438 if (Ty->getAddressSpace() > 255)
439 // Fast instruction selection doesn't support the special
440 // address spaces.
441 return false;
442
443 switch (Opcode) {
444 default:
445 //errs() << "Failing Opcode is: " << *Op1 << "\n";
446 break;
447 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000448 assert(false && "Alloca should have been handled earlier!");
449 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000450 }
451 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000452
453 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
454 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000455 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000456 return false;
457 }
458
459 // Try to get this in a register if nothing else has worked.
460 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000461 if (Reg == 0) return false;
462
463 // Since the offset may be too large for the load instruction
464 // get the reg+offset into a register.
465 // TODO: Verify the additions work, otherwise we'll need to add the
466 // offset instead of 0 to the instructions and do all sorts of operand
467 // munging.
468 // TODO: Optimize this somewhat.
469 if (Offset != 0) {
470 ARMCC::CondCodes Pred = ARMCC::AL;
471 unsigned PredReg = 0;
472
Eric Christophereaa204b2010-09-02 01:39:14 +0000473 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000474 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
475 Reg, Reg, Offset, Pred, PredReg,
476 static_cast<const ARMBaseInstrInfo&>(TII));
477 else {
478 assert(AFI->isThumb2Function());
479 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
480 Reg, Reg, Offset, Pred, PredReg,
481 static_cast<const ARMBaseInstrInfo&>(TII));
482 }
483 }
484
485 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000486}
487
Eric Christopher30b66332010-09-08 21:49:50 +0000488bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000489 Value *Op0 = I->getOperand(0);
490
491 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000492 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
493 DenseMap<const AllocaInst*, int>::iterator SI =
494 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000495
Eric Christophere24d66f2010-08-24 22:07:27 +0000496 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000497 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000498 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000499 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000500 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000501 TM.getRegisterInfo());
502 UpdateValueMap(I, ResultReg);
503 return true;
504 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000505 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000506 return false;
507}
508
Eric Christopherb1cc8482010-08-25 07:23:49 +0000509bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
510 unsigned Reg, int Offset) {
511
512 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000513 unsigned Opc;
514
Eric Christopherb1cc8482010-08-25 07:23:49 +0000515 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000516 default:
517 assert(false && "Trying to emit for an unhandled type!");
518 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000519 case MVT::i16:
520 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
521 VT = MVT::i32;
522 break;
523 case MVT::i8:
524 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
525 VT = MVT::i32;
526 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000527 case MVT::i32:
528 Opc = isThumb ? ARM::tLDR : ARM::LDR;
529 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000530 }
Eric Christopherdc908042010-08-31 01:28:42 +0000531
532 ResultReg = createResultReg(TLI.getRegClassFor(VT));
533
534 // TODO: Fix the Addressing modes so that these can share some code.
535 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
536 if (isThumb)
537 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
538 TII.get(Opc), ResultReg)
539 .addReg(Reg).addImm(Offset).addReg(0));
540 else
541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
542 TII.get(Opc), ResultReg)
543 .addReg(Reg).addReg(0).addImm(Offset));
544
545 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000546}
547
Eric Christopher30b66332010-09-08 21:49:50 +0000548bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000549 Value *Op1 = I->getOperand(1);
550
551 // Verify it's an alloca.
552 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
553 DenseMap<const AllocaInst*, int>::iterator SI =
554 FuncInfo.StaticAllocaMap.find(AI);
555
556 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000557 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000558 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000559 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000560 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000561 TM.getRegisterInfo());
562 return true;
563 }
564 }
565 return false;
566}
567
Eric Christopher318b6ee2010-09-02 00:53:56 +0000568bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
569 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000570 unsigned StrOpc;
571 switch (VT.getSimpleVT().SimpleTy) {
572 default: return false;
573 case MVT::i1:
574 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
575 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
576 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000577 case MVT::f32:
578 if (!Subtarget->hasVFP2()) return false;
579 StrOpc = ARM::VSTRS;
580 break;
581 case MVT::f64:
582 if (!Subtarget->hasVFP2()) return false;
583 StrOpc = ARM::VSTRD;
584 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000585 }
586
587 if (isThumb)
588 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
589 TII.get(StrOpc), SrcReg)
590 .addReg(DstReg).addImm(Offset).addReg(0));
591 else
592 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
593 TII.get(StrOpc), SrcReg)
594 .addReg(DstReg).addReg(0).addImm(Offset));
595
596 return true;
597}
598
599bool ARMFastISel::ARMSelectStore(const Instruction *I) {
600 Value *Op0 = I->getOperand(0);
601 unsigned SrcReg = 0;
602
Eric Christopher543cf052010-09-01 22:16:27 +0000603 // Yay type legalization
604 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000605 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000606 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000607
Eric Christopher1b61ef42010-09-02 01:48:11 +0000608 // Get the value to be stored into a register.
609 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000610 if (SrcReg == 0)
611 return false;
612
613 // If we're an alloca we know we have a frame index and can emit the store
614 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000615 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000616 return true;
617
618 // Our register and offset with innocuous defaults.
619 unsigned Reg = 0;
620 int Offset = 0;
621
622 // See if we can handle this as Reg + Offset
623 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
624 return false;
625
626 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
627
Eric Christopher543cf052010-09-01 22:16:27 +0000628 return false;
629
630}
631
Eric Christopher83007122010-08-23 21:44:12 +0000632bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000633 // Verify we have a legal type before going any further.
634 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000635 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000636 return false;
637
Eric Christopher30b66332010-09-08 21:49:50 +0000638 // If we're an alloca we know we have a frame index and can emit the load
639 // directly in short order.
640 if (ARMLoadAlloca(I, VT))
641 return true;
642
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000643 // Our register and offset with innocuous defaults.
644 unsigned Reg = 0;
645 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000646
Eric Christopher83007122010-08-23 21:44:12 +0000647 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000648 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000649 return false;
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000650
Eric Christopherb1cc8482010-08-25 07:23:49 +0000651 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000652 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000653
Eric Christopherf06f3092010-08-24 00:50:47 +0000654 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000655 return true;
656}
657
Eric Christophere5734102010-09-03 00:35:47 +0000658bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
659 const BranchInst *BI = cast<BranchInst>(I);
660 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
661 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
662
663 // Simple branch support.
664 unsigned CondReg = getRegForValue(BI->getCondition());
665 if (CondReg == 0) return false;
666
667 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
668 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
669 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
670 .addReg(CondReg).addReg(CondReg));
671 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
672 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
673 FastEmitBranch(FBB, DL);
674 FuncInfo.MBB->addSuccessor(TBB);
675 return true;
676}
677
Eric Christopherd43393a2010-09-08 23:13:45 +0000678bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
679 const CmpInst *CI = cast<CmpInst>(I);
680
681 EVT VT;
682 const Type *Ty = CI->getOperand(0)->getType();
683 if (!isTypeLegal(Ty, VT))
684 return false;
685
686 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
687 if (isFloat && !Subtarget->hasVFP2())
688 return false;
689
690 unsigned CmpOpc;
691 switch (VT.getSimpleVT().SimpleTy) {
692 default: return false;
693 // TODO: Verify compares.
694 case MVT::f32:
695 CmpOpc = ARM::VCMPES;
696 break;
697 case MVT::f64:
698 CmpOpc = ARM::VCMPED;
699 break;
700 case MVT::i32:
701 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
702 break;
703 }
704
705 unsigned Arg1 = getRegForValue(CI->getOperand(0));
706 if (Arg1 == 0) return false;
707
708 unsigned Arg2 = getRegForValue(CI->getOperand(1));
709 if (Arg2 == 0) return false;
710
711 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
712 .addReg(Arg1).addReg(Arg2));
713
714 // For floating point we need to move the result to a register we can
715 // actually do something with.
716 if (isFloat)
717 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
718 TII.get(ARM::FMSTAT)));
719 return true;
720}
721
Eric Christopher56d2b722010-09-02 23:43:26 +0000722// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000723bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000724 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000725 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopher7fe55b72010-08-23 22:32:45 +0000726
Eric Christopherab695882010-07-21 22:26:11 +0000727 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000728 case Instruction::Load:
729 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000730 case Instruction::Store:
731 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000732 case Instruction::Br:
733 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000734 case Instruction::ICmp:
735 case Instruction::FCmp:
736 return ARMSelectCmp(I);
Eric Christopherab695882010-07-21 22:26:11 +0000737 default: break;
738 }
739 return false;
740}
741
742namespace llvm {
743 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000744 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000745 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000746 }
747}