blob: e484ce4038e475cd2d82a23b21b260b4168916a0 [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 Christopher46203602010-09-09 00:26:48 +0000117 virtual bool ARMSelectFPExt(const Instruction *I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000118 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
Eric Christopherab695882010-07-21 22:26:11 +0000119
Eric Christopher83007122010-08-23 21:44:12 +0000120 // Utility routines.
Eric Christopher456144e2010-08-19 00:37:05 +0000121 private:
Eric Christopherb1cc8482010-08-25 07:23:49 +0000122 bool isTypeLegal(const Type *Ty, EVT &VT);
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000123 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000124 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000125 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
Eric Christopher30b66332010-09-08 21:49:50 +0000126 bool ARMLoadAlloca(const Instruction *I, EVT VT);
127 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
Eric Christophercb0b04b2010-08-24 00:07:24 +0000128 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
Eric Christopher9ed58df2010-09-09 00:19:41 +0000129 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
130 unsigned ARMMaterializeInt(const Constant *C);
Eric Christopher83007122010-08-23 21:44:12 +0000131
Eric Christopher456144e2010-08-19 00:37:05 +0000132 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
133 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
134};
Eric Christopherab695882010-07-21 22:26:11 +0000135
136} // end anonymous namespace
137
138// #include "ARMGenCallingConv.inc"
139
Eric Christopher456144e2010-08-19 00:37:05 +0000140// DefinesOptionalPredicate - This is different from DefinesPredicate in that
141// we don't care about implicit defs here, just places we'll need to add a
142// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
143bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
144 const TargetInstrDesc &TID = MI->getDesc();
145 if (!TID.hasOptionalDef())
146 return false;
147
148 // Look to see if our OptionalDef is defining CPSR or CCR.
149 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
150 const MachineOperand &MO = MI->getOperand(i);
Eric Christopherf762fbe2010-08-20 00:36:24 +0000151 if (!MO.isReg() || !MO.isDef()) continue;
152 if (MO.getReg() == ARM::CPSR)
Eric Christopher456144e2010-08-19 00:37:05 +0000153 *CPSR = true;
154 }
155 return true;
156}
157
158// If the machine is predicable go ahead and add the predicate operands, if
159// it needs default CC operands add those.
160const MachineInstrBuilder &
161ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
162 MachineInstr *MI = &*MIB;
163
164 // Do we use a predicate?
165 if (TII.isPredicable(MI))
166 AddDefaultPred(MIB);
167
168 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
169 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
Eric Christopher979e0a12010-08-19 15:35:27 +0000170 bool CPSR = false;
Eric Christopher456144e2010-08-19 00:37:05 +0000171 if (DefinesOptionalPredicate(MI, &CPSR)) {
172 if (CPSR)
173 AddDefaultT1CC(MIB);
174 else
175 AddDefaultCC(MIB);
176 }
177 return MIB;
178}
179
Eric Christopher0fe7d542010-08-17 01:25:29 +0000180unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
181 const TargetRegisterClass* RC) {
182 unsigned ResultReg = createResultReg(RC);
183 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
184
Eric Christopher456144e2010-08-19 00:37:05 +0000185 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
Eric Christopher0fe7d542010-08-17 01:25:29 +0000186 return ResultReg;
187}
188
189unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
190 const TargetRegisterClass *RC,
191 unsigned Op0, bool Op0IsKill) {
192 unsigned ResultReg = createResultReg(RC);
193 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
194
195 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000196 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000197 .addReg(Op0, Op0IsKill * RegState::Kill));
198 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000199 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000200 .addReg(Op0, Op0IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000202 TII.get(TargetOpcode::COPY), ResultReg)
203 .addReg(II.ImplicitDefs[0]));
204 }
205 return ResultReg;
206}
207
208unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
209 const TargetRegisterClass *RC,
210 unsigned Op0, bool Op0IsKill,
211 unsigned Op1, bool Op1IsKill) {
212 unsigned ResultReg = createResultReg(RC);
213 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
214
215 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000216 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000217 .addReg(Op0, Op0IsKill * RegState::Kill)
218 .addReg(Op1, Op1IsKill * RegState::Kill));
219 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000220 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000221 .addReg(Op0, Op0IsKill * RegState::Kill)
222 .addReg(Op1, Op1IsKill * RegState::Kill));
Eric Christopher456144e2010-08-19 00:37:05 +0000223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000224 TII.get(TargetOpcode::COPY), ResultReg)
225 .addReg(II.ImplicitDefs[0]));
226 }
227 return ResultReg;
228}
229
230unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
231 const TargetRegisterClass *RC,
232 unsigned Op0, bool Op0IsKill,
233 uint64_t Imm) {
234 unsigned ResultReg = createResultReg(RC);
235 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
236
237 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000238 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000239 .addReg(Op0, Op0IsKill * RegState::Kill)
240 .addImm(Imm));
241 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000243 .addReg(Op0, Op0IsKill * RegState::Kill)
244 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000245 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000246 TII.get(TargetOpcode::COPY), ResultReg)
247 .addReg(II.ImplicitDefs[0]));
248 }
249 return ResultReg;
250}
251
252unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
253 const TargetRegisterClass *RC,
254 unsigned Op0, bool Op0IsKill,
255 const ConstantFP *FPImm) {
256 unsigned ResultReg = createResultReg(RC);
257 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
258
259 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000260 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000261 .addReg(Op0, Op0IsKill * RegState::Kill)
262 .addFPImm(FPImm));
263 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000265 .addReg(Op0, Op0IsKill * RegState::Kill)
266 .addFPImm(FPImm));
Eric Christopher456144e2010-08-19 00:37:05 +0000267 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000268 TII.get(TargetOpcode::COPY), ResultReg)
269 .addReg(II.ImplicitDefs[0]));
270 }
271 return ResultReg;
272}
273
274unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
275 const TargetRegisterClass *RC,
276 unsigned Op0, bool Op0IsKill,
277 unsigned Op1, bool Op1IsKill,
278 uint64_t Imm) {
279 unsigned ResultReg = createResultReg(RC);
280 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
281
282 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000284 .addReg(Op0, Op0IsKill * RegState::Kill)
285 .addReg(Op1, Op1IsKill * RegState::Kill)
286 .addImm(Imm));
287 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000288 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000289 .addReg(Op0, Op0IsKill * RegState::Kill)
290 .addReg(Op1, Op1IsKill * RegState::Kill)
291 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000293 TII.get(TargetOpcode::COPY), ResultReg)
294 .addReg(II.ImplicitDefs[0]));
295 }
296 return ResultReg;
297}
298
299unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
300 const TargetRegisterClass *RC,
301 uint64_t Imm) {
302 unsigned ResultReg = createResultReg(RC);
303 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
304
305 if (II.getNumDefs() >= 1)
Eric Christopher456144e2010-08-19 00:37:05 +0000306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000307 .addImm(Imm));
308 else {
Eric Christopher456144e2010-08-19 00:37:05 +0000309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Eric Christopher0fe7d542010-08-17 01:25:29 +0000310 .addImm(Imm));
Eric Christopher456144e2010-08-19 00:37:05 +0000311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
314 }
315 return ResultReg;
316}
317
318unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
319 unsigned Op0, bool Op0IsKill,
320 uint32_t Idx) {
321 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
322 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
323 "Cannot yet extract from physregs");
Eric Christopher456144e2010-08-19 00:37:05 +0000324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Eric Christopher0fe7d542010-08-17 01:25:29 +0000325 DL, TII.get(TargetOpcode::COPY), ResultReg)
326 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
327 return ResultReg;
328}
329
Eric Christopher9ed58df2010-09-09 00:19:41 +0000330// For double width floating point we need to materialize two constants
331// (the high and the low) into integer registers then use a move to get
332// the combined constant into an FP reg.
333unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
334 const APFloat Val = CFP->getValueAPF();
335 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
Eric Christopher56d2b722010-09-02 23:43:26 +0000336
Eric Christopher9ed58df2010-09-09 00:19:41 +0000337 // This checks to see if we can use VFP3 instructions to materialize
338 // a constant, otherwise we have to go through the constant pool.
339 if (TLI.isFPImmLegal(Val, VT)) {
340 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
341 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
343 DestReg)
344 .addFPImm(CFP));
345 return DestReg;
346 }
347
348 // No 64-bit at the moment.
349 if (is64bit) return 0;
350
351 // Load this from the constant pool.
352 unsigned DestReg = ARMMaterializeInt(cast<Constant>(CFP));
Eric Christopher56d2b722010-09-02 23:43:26 +0000353
Eric Christopher9ed58df2010-09-09 00:19:41 +0000354 // If we have a floating point constant we expect it in a floating point
355 // register.
356 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
358 TII.get(ARM::VMOVRS), MoveReg)
359 .addReg(DestReg));
360 return MoveReg;
361}
362
363unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
Eric Christopher56d2b722010-09-02 23:43:26 +0000364 // MachineConstantPool wants an explicit alignment.
365 unsigned Align = TD.getPrefTypeAlignment(C->getType());
366 if (Align == 0) {
367 // TODO: Figure out if this is correct.
368 Align = TD.getTypeAllocSize(C->getType());
369 }
370 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
371
Eric Christopher845c5752010-09-08 18:56:34 +0000372 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
Eric Christopher56d2b722010-09-02 23:43:26 +0000373 if (isThumb)
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
375 TII.get(ARM::t2LDRpci))
376 .addReg(DestReg).addConstantPoolIndex(Idx));
377 else
378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
379 TII.get(ARM::LDRcp))
Eric Christopher845c5752010-09-08 18:56:34 +0000380 .addReg(DestReg).addConstantPoolIndex(Idx)
Eric Christopher56d2b722010-09-02 23:43:26 +0000381 .addReg(0).addImm(0));
382
383 return DestReg;
Eric Christopher1b61ef42010-09-02 01:48:11 +0000384}
385
Eric Christopher9ed58df2010-09-09 00:19:41 +0000386unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
387 EVT VT = TLI.getValueType(C->getType(), true);
388
389 // Only handle simple types.
390 if (!VT.isSimple()) return 0;
391
392 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
393 return ARMMaterializeFP(CFP, VT);
394 return ARMMaterializeInt(C);
395}
396
Eric Christopherb1cc8482010-08-25 07:23:49 +0000397bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
398 VT = TLI.getValueType(Ty, true);
399
400 // Only handle simple types.
401 if (VT == MVT::Other || !VT.isSimple()) return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000402
Eric Christopherdc908042010-08-31 01:28:42 +0000403 // Handle all legal types, i.e. a register that will directly hold this
404 // value.
405 return TLI.isTypeLegal(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000406}
407
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000408bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
409 if (isTypeLegal(Ty, VT)) return true;
410
411 // If this is a type than can be sign or zero-extended to a basic operation
412 // go ahead and accept it now.
413 if (VT == MVT::i8 || VT == MVT::i16)
414 return true;
415
416 return false;
417}
418
Eric Christophercb0b04b2010-08-24 00:07:24 +0000419// Computes the Reg+Offset to get to an object.
420bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
Eric Christopher83007122010-08-23 21:44:12 +0000421 int &Offset) {
422 // Some boilerplate from the X86 FastISel.
423 const User *U = NULL;
Eric Christopher83007122010-08-23 21:44:12 +0000424 unsigned Opcode = Instruction::UserOp1;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000425 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000426 // Don't walk into other basic blocks; it's possible we haven't
427 // visited them yet, so the instructions may not yet be assigned
428 // virtual registers.
429 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
430 return false;
431
432 Opcode = I->getOpcode();
433 U = I;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000434 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
Eric Christopher83007122010-08-23 21:44:12 +0000435 Opcode = C->getOpcode();
436 U = C;
437 }
438
Eric Christophercb0b04b2010-08-24 00:07:24 +0000439 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
Eric Christopher83007122010-08-23 21:44:12 +0000440 if (Ty->getAddressSpace() > 255)
441 // Fast instruction selection doesn't support the special
442 // address spaces.
443 return false;
444
445 switch (Opcode) {
446 default:
447 //errs() << "Failing Opcode is: " << *Op1 << "\n";
448 break;
449 case Instruction::Alloca: {
Eric Christopherf06f3092010-08-24 00:50:47 +0000450 assert(false && "Alloca should have been handled earlier!");
451 return false;
Eric Christopher83007122010-08-23 21:44:12 +0000452 }
453 }
Eric Christophercb0b04b2010-08-24 00:07:24 +0000454
455 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
456 //errs() << "Failing GV is: " << GV << "\n";
Eric Christopherf06f3092010-08-24 00:50:47 +0000457 (void)GV;
Eric Christophercb0b04b2010-08-24 00:07:24 +0000458 return false;
459 }
460
461 // Try to get this in a register if nothing else has worked.
462 Reg = getRegForValue(Obj);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000463 if (Reg == 0) return false;
464
465 // Since the offset may be too large for the load instruction
466 // get the reg+offset into a register.
467 // TODO: Verify the additions work, otherwise we'll need to add the
468 // offset instead of 0 to the instructions and do all sorts of operand
469 // munging.
470 // TODO: Optimize this somewhat.
471 if (Offset != 0) {
472 ARMCC::CondCodes Pred = ARMCC::AL;
473 unsigned PredReg = 0;
474
Eric Christophereaa204b2010-09-02 01:39:14 +0000475 if (!isThumb)
Eric Christopher318b6ee2010-09-02 00:53:56 +0000476 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
477 Reg, Reg, Offset, Pred, PredReg,
478 static_cast<const ARMBaseInstrInfo&>(TII));
479 else {
480 assert(AFI->isThumb2Function());
481 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
482 Reg, Reg, Offset, Pred, PredReg,
483 static_cast<const ARMBaseInstrInfo&>(TII));
484 }
485 }
486
487 return true;
Eric Christopher83007122010-08-23 21:44:12 +0000488}
489
Eric Christopher30b66332010-09-08 21:49:50 +0000490bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
Eric Christopherf06f3092010-08-24 00:50:47 +0000491 Value *Op0 = I->getOperand(0);
492
493 // Verify it's an alloca.
Eric Christophere24d66f2010-08-24 22:07:27 +0000494 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
495 DenseMap<const AllocaInst*, int>::iterator SI =
496 FuncInfo.StaticAllocaMap.find(AI);
Eric Christopherf06f3092010-08-24 00:50:47 +0000497
Eric Christophere24d66f2010-08-24 22:07:27 +0000498 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000499 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopherb1cc8482010-08-25 07:23:49 +0000500 unsigned ResultReg = createResultReg(RC);
Eric Christophere24d66f2010-08-24 22:07:27 +0000501 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopherb1cc8482010-08-25 07:23:49 +0000502 ResultReg, SI->second, RC,
Eric Christophere24d66f2010-08-24 22:07:27 +0000503 TM.getRegisterInfo());
504 UpdateValueMap(I, ResultReg);
505 return true;
506 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000507 }
Eric Christopherf06f3092010-08-24 00:50:47 +0000508 return false;
509}
510
Eric Christopherb1cc8482010-08-25 07:23:49 +0000511bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
512 unsigned Reg, int Offset) {
513
514 assert(VT.isSimple() && "Non-simple types are invalid here!");
Eric Christopherdc908042010-08-31 01:28:42 +0000515 unsigned Opc;
516
Eric Christopherb1cc8482010-08-25 07:23:49 +0000517 switch (VT.getSimpleVT().SimpleTy) {
Eric Christopher548d1bb2010-08-30 23:48:26 +0000518 default:
519 assert(false && "Trying to emit for an unhandled type!");
520 return false;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000521 case MVT::i16:
522 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
523 VT = MVT::i32;
524 break;
525 case MVT::i8:
526 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
527 VT = MVT::i32;
528 break;
Eric Christopherdc908042010-08-31 01:28:42 +0000529 case MVT::i32:
530 Opc = isThumb ? ARM::tLDR : ARM::LDR;
531 break;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000532 }
Eric Christopherdc908042010-08-31 01:28:42 +0000533
534 ResultReg = createResultReg(TLI.getRegClassFor(VT));
535
536 // TODO: Fix the Addressing modes so that these can share some code.
537 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
538 if (isThumb)
539 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
540 TII.get(Opc), ResultReg)
541 .addReg(Reg).addImm(Offset).addReg(0));
542 else
543 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
544 TII.get(Opc), ResultReg)
545 .addReg(Reg).addReg(0).addImm(Offset));
546
547 return true;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000548}
549
Eric Christopher30b66332010-09-08 21:49:50 +0000550bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
Eric Christopher543cf052010-09-01 22:16:27 +0000551 Value *Op1 = I->getOperand(1);
552
553 // Verify it's an alloca.
554 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
555 DenseMap<const AllocaInst*, int>::iterator SI =
556 FuncInfo.StaticAllocaMap.find(AI);
557
558 if (SI != FuncInfo.StaticAllocaMap.end()) {
Eric Christopher30b66332010-09-08 21:49:50 +0000559 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000560 assert(SrcReg != 0 && "Nothing to store!");
Eric Christopher543cf052010-09-01 22:16:27 +0000561 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
Eric Christopher318b6ee2010-09-02 00:53:56 +0000562 SrcReg, true /*isKill*/, SI->second, RC,
Eric Christopher543cf052010-09-01 22:16:27 +0000563 TM.getRegisterInfo());
564 return true;
565 }
566 }
567 return false;
568}
569
Eric Christopher318b6ee2010-09-02 00:53:56 +0000570bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
571 unsigned DstReg, int Offset) {
Eric Christopher318b6ee2010-09-02 00:53:56 +0000572 unsigned StrOpc;
573 switch (VT.getSimpleVT().SimpleTy) {
574 default: return false;
575 case MVT::i1:
576 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
577 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
578 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
Eric Christopher56d2b722010-09-02 23:43:26 +0000579 case MVT::f32:
580 if (!Subtarget->hasVFP2()) return false;
581 StrOpc = ARM::VSTRS;
582 break;
583 case MVT::f64:
584 if (!Subtarget->hasVFP2()) return false;
585 StrOpc = ARM::VSTRD;
586 break;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000587 }
588
589 if (isThumb)
590 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
591 TII.get(StrOpc), SrcReg)
592 .addReg(DstReg).addImm(Offset).addReg(0));
593 else
594 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
595 TII.get(StrOpc), SrcReg)
596 .addReg(DstReg).addReg(0).addImm(Offset));
597
598 return true;
599}
600
601bool ARMFastISel::ARMSelectStore(const Instruction *I) {
602 Value *Op0 = I->getOperand(0);
603 unsigned SrcReg = 0;
604
Eric Christopher543cf052010-09-01 22:16:27 +0000605 // Yay type legalization
606 EVT VT;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000607 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
Eric Christopher543cf052010-09-01 22:16:27 +0000608 return false;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000609
Eric Christopher1b61ef42010-09-02 01:48:11 +0000610 // Get the value to be stored into a register.
611 SrcReg = getRegForValue(Op0);
Eric Christopher318b6ee2010-09-02 00:53:56 +0000612 if (SrcReg == 0)
613 return false;
614
615 // If we're an alloca we know we have a frame index and can emit the store
616 // quickly.
Eric Christopher30b66332010-09-08 21:49:50 +0000617 if (ARMStoreAlloca(I, SrcReg, VT))
Eric Christopher318b6ee2010-09-02 00:53:56 +0000618 return true;
619
620 // Our register and offset with innocuous defaults.
621 unsigned Reg = 0;
622 int Offset = 0;
623
624 // See if we can handle this as Reg + Offset
625 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
626 return false;
627
628 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
629
Eric Christopher543cf052010-09-01 22:16:27 +0000630 return false;
631
632}
633
Eric Christopher83007122010-08-23 21:44:12 +0000634bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000635 // Verify we have a legal type before going any further.
636 EVT VT;
Eric Christopher4e68c7c2010-09-01 18:01:32 +0000637 if (!isLoadTypeLegal(I->getType(), VT))
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000638 return false;
639
Eric Christopher30b66332010-09-08 21:49:50 +0000640 // If we're an alloca we know we have a frame index and can emit the load
641 // directly in short order.
642 if (ARMLoadAlloca(I, VT))
643 return true;
644
Eric Christopher61c3f9a2010-08-25 08:43:57 +0000645 // Our register and offset with innocuous defaults.
646 unsigned Reg = 0;
647 int Offset = 0;
Eric Christopher8654c712010-08-23 23:14:31 +0000648
Eric Christopher83007122010-08-23 21:44:12 +0000649 // See if we can handle this as Reg + Offset
Eric Christophercb0b04b2010-08-24 00:07:24 +0000650 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
Eric Christopher83007122010-08-23 21:44:12 +0000651 return false;
Eric Christopher1dfb4d32010-08-23 23:28:04 +0000652
Eric Christopherb1cc8482010-08-25 07:23:49 +0000653 unsigned ResultReg;
Eric Christopher318b6ee2010-09-02 00:53:56 +0000654 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
Eric Christopherb1cc8482010-08-25 07:23:49 +0000655
Eric Christopherf06f3092010-08-24 00:50:47 +0000656 UpdateValueMap(I, ResultReg);
Eric Christopher83007122010-08-23 21:44:12 +0000657 return true;
658}
659
Eric Christophere5734102010-09-03 00:35:47 +0000660bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
661 const BranchInst *BI = cast<BranchInst>(I);
662 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
663 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
664
665 // Simple branch support.
666 unsigned CondReg = getRegForValue(BI->getCondition());
667 if (CondReg == 0) return false;
668
669 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
670 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
671 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
672 .addReg(CondReg).addReg(CondReg));
673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
674 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
675 FastEmitBranch(FBB, DL);
676 FuncInfo.MBB->addSuccessor(TBB);
677 return true;
678}
679
Eric Christopherd43393a2010-09-08 23:13:45 +0000680bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
681 const CmpInst *CI = cast<CmpInst>(I);
682
683 EVT VT;
684 const Type *Ty = CI->getOperand(0)->getType();
685 if (!isTypeLegal(Ty, VT))
686 return false;
687
688 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
689 if (isFloat && !Subtarget->hasVFP2())
690 return false;
691
692 unsigned CmpOpc;
693 switch (VT.getSimpleVT().SimpleTy) {
694 default: return false;
695 // TODO: Verify compares.
696 case MVT::f32:
697 CmpOpc = ARM::VCMPES;
698 break;
699 case MVT::f64:
700 CmpOpc = ARM::VCMPED;
701 break;
702 case MVT::i32:
703 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
704 break;
705 }
706
707 unsigned Arg1 = getRegForValue(CI->getOperand(0));
708 if (Arg1 == 0) return false;
709
710 unsigned Arg2 = getRegForValue(CI->getOperand(1));
711 if (Arg2 == 0) return false;
712
713 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
714 .addReg(Arg1).addReg(Arg2));
715
716 // For floating point we need to move the result to a register we can
717 // actually do something with.
718 if (isFloat)
719 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
720 TII.get(ARM::FMSTAT)));
721 return true;
722}
723
Eric Christopher46203602010-09-09 00:26:48 +0000724bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
725 // Make sure we have VFP and that we're extending float to double.
726 if (!Subtarget->hasVFP2()) return false;
727
728 Value *V = I->getOperand(0);
729 if (!I->getType()->isDoubleTy() ||
730 !V->getType()->isFloatTy()) return false;
731
732 unsigned Op = getRegForValue(V);
733 if (Op == 0) return false;
734
735 unsigned Result = createResultReg(ARM::DPRRegisterClass);
736
737 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
738 TII.get(ARM::VCVTDS), Result)
739 .addReg(Op));
740 UpdateValueMap(I, Result);
741 return true;
742}
743
Eric Christopherbc39b822010-09-09 00:53:57 +0000744bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
745 // We can get here in the case when we want to use NEON for our fp
746 // operations, but can't figure out how to. Just use the vfp instructions
747 // if we have them.
748 // FIXME: It'd be nice to use NEON instructions.
749 if (!Subtarget->hasVFP2()) return false;
750
751 EVT VT = TLI.getValueType(I->getType(), true);
752
753 // In this case make extra sure we have a 32-bit floating point add.
754 if (VT != MVT::f32) return false;
755
756 unsigned Op1 = getRegForValue(I->getOperand(0));
757 if (Op1 == 0) return false;
758
759 unsigned Op2 = getRegForValue(I->getOperand(1));
760 if (Op2 == 0) return false;
761
762 unsigned Opc;
763 switch (ISDOpcode) {
764 default: return false;
765 case ISD::FADD:
766 Opc = ARM::VADDS;
767 break;
768 case ISD::FSUB:
769 Opc = ARM::VSUBS;
770 break;
771 case ISD::FMUL:
772 Opc = ARM::VMULS;
773 break;
774 }
775 unsigned ResultReg = createResultReg(ARM::SPRRegisterClass);
776 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
777 TII.get(Opc), ResultReg)
778 .addReg(Op1).addReg(Op2));
779 return true;
780}
781
Eric Christopher56d2b722010-09-02 23:43:26 +0000782// TODO: SoftFP support.
Eric Christopherab695882010-07-21 22:26:11 +0000783bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
Eric Christopher7fe55b72010-08-23 22:32:45 +0000784 // No Thumb-1 for now.
Eric Christophereaa204b2010-09-02 01:39:14 +0000785 if (isThumb && !AFI->isThumb2Function()) return false;
Eric Christopher7fe55b72010-08-23 22:32:45 +0000786
Eric Christopherab695882010-07-21 22:26:11 +0000787 switch (I->getOpcode()) {
Eric Christopher83007122010-08-23 21:44:12 +0000788 case Instruction::Load:
789 return ARMSelectLoad(I);
Eric Christopher543cf052010-09-01 22:16:27 +0000790 case Instruction::Store:
791 return ARMSelectStore(I);
Eric Christophere5734102010-09-03 00:35:47 +0000792 case Instruction::Br:
793 return ARMSelectBranch(I);
Eric Christopherd43393a2010-09-08 23:13:45 +0000794 case Instruction::ICmp:
795 case Instruction::FCmp:
796 return ARMSelectCmp(I);
Eric Christopher46203602010-09-09 00:26:48 +0000797 case Instruction::FPExt:
798 return ARMSelectFPExt(I);
Eric Christopherbc39b822010-09-09 00:53:57 +0000799 case Instruction::FAdd:
800 return ARMSelectBinaryOp(I, ISD::FADD);
801 case Instruction::FSub:
802 return ARMSelectBinaryOp(I, ISD::FSUB);
803 case Instruction::FMul:
804 return ARMSelectBinaryOp(I, ISD::FMUL);
Eric Christopherab695882010-07-21 22:26:11 +0000805 default: break;
806 }
807 return false;
808}
809
810namespace llvm {
811 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
Eric Christopher038fea52010-08-17 00:46:57 +0000812 if (EnableARMFastISel) return new ARMFastISel(funcInfo);
Evan Cheng09447952010-07-26 18:32:55 +0000813 return 0;
Eric Christopherab695882010-07-21 22:26:11 +0000814 }
815}