1 //===-- ARMFastISel.cpp - ARM FastISel implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
17 #include "ARMBaseInstrInfo.h"
18 #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"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineConstantPool.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/Support/CallSite.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetLowering.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
46 EnableARMFastISel("arm-fast-isel",
47 cl::desc("Turn on experimental ARM fast-isel support"),
48 cl::init(false), cl::Hidden);
52 class ARMFastISel : public FastISel {
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;
57 const TargetMachine &TM;
58 const TargetInstrInfo &TII;
59 const TargetLowering &TLI;
60 const ARMFunctionInfo *AFI;
63 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
65 TM(funcInfo.MF->getTarget()),
66 TII(*TM.getInstrInfo()),
67 TLI(*TM.getTargetLowering()) {
68 Subtarget = &TM.getSubtarget<ARMSubtarget>();
69 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
72 // Code from FastISel.cpp.
73 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
74 const TargetRegisterClass *RC);
75 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
76 const TargetRegisterClass *RC,
77 unsigned Op0, bool Op0IsKill);
78 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
79 const TargetRegisterClass *RC,
80 unsigned Op0, bool Op0IsKill,
81 unsigned Op1, bool Op1IsKill);
82 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
83 const TargetRegisterClass *RC,
84 unsigned Op0, bool Op0IsKill,
86 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill,
89 const ConstantFP *FPImm);
90 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
91 const TargetRegisterClass *RC,
93 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
96 unsigned Op1, bool Op1IsKill,
98 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
99 unsigned Op0, bool Op0IsKill,
102 // Backend specific FastISel code.
103 virtual bool TargetSelectInstruction(const Instruction *I);
105 #include "ARMGenFastISel.inc"
107 // Instruction selection routines.
108 virtual bool ARMSelectLoad(const Instruction *I);
112 bool isTypeLegal(const Type *Ty, EVT &VT);
113 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
114 bool ARMLoadAlloca(const Instruction *I);
115 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
117 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
118 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
121 } // end anonymous namespace
123 // #include "ARMGenCallingConv.inc"
125 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
126 // we don't care about implicit defs here, just places we'll need to add a
127 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
128 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
129 const TargetInstrDesc &TID = MI->getDesc();
130 if (!TID.hasOptionalDef())
133 // Look to see if our OptionalDef is defining CPSR or CCR.
134 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
135 const MachineOperand &MO = MI->getOperand(i);
136 if (!MO.isReg() || !MO.isDef()) continue;
137 if (MO.getReg() == ARM::CPSR)
143 // If the machine is predicable go ahead and add the predicate operands, if
144 // it needs default CC operands add those.
145 const MachineInstrBuilder &
146 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
147 MachineInstr *MI = &*MIB;
149 // Do we use a predicate?
150 if (TII.isPredicable(MI))
153 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
154 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
156 if (DefinesOptionalPredicate(MI, &CPSR)) {
165 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
166 const TargetRegisterClass* RC) {
167 unsigned ResultReg = createResultReg(RC);
168 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
170 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
174 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
175 const TargetRegisterClass *RC,
176 unsigned Op0, bool Op0IsKill) {
177 unsigned ResultReg = createResultReg(RC);
178 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
180 if (II.getNumDefs() >= 1)
181 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
182 .addReg(Op0, Op0IsKill * RegState::Kill));
184 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
185 .addReg(Op0, Op0IsKill * RegState::Kill));
186 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
187 TII.get(TargetOpcode::COPY), ResultReg)
188 .addReg(II.ImplicitDefs[0]));
193 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
194 const TargetRegisterClass *RC,
195 unsigned Op0, bool Op0IsKill,
196 unsigned Op1, bool Op1IsKill) {
197 unsigned ResultReg = createResultReg(RC);
198 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
200 if (II.getNumDefs() >= 1)
201 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
202 .addReg(Op0, Op0IsKill * RegState::Kill)
203 .addReg(Op1, Op1IsKill * RegState::Kill));
205 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
206 .addReg(Op0, Op0IsKill * RegState::Kill)
207 .addReg(Op1, Op1IsKill * RegState::Kill));
208 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
209 TII.get(TargetOpcode::COPY), ResultReg)
210 .addReg(II.ImplicitDefs[0]));
215 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
216 const TargetRegisterClass *RC,
217 unsigned Op0, bool Op0IsKill,
219 unsigned ResultReg = createResultReg(RC);
220 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
222 if (II.getNumDefs() >= 1)
223 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
224 .addReg(Op0, Op0IsKill * RegState::Kill)
227 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
228 .addReg(Op0, Op0IsKill * RegState::Kill)
230 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
231 TII.get(TargetOpcode::COPY), ResultReg)
232 .addReg(II.ImplicitDefs[0]));
237 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
238 const TargetRegisterClass *RC,
239 unsigned Op0, bool Op0IsKill,
240 const ConstantFP *FPImm) {
241 unsigned ResultReg = createResultReg(RC);
242 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
244 if (II.getNumDefs() >= 1)
245 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
246 .addReg(Op0, Op0IsKill * RegState::Kill)
249 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
250 .addReg(Op0, Op0IsKill * RegState::Kill)
252 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
253 TII.get(TargetOpcode::COPY), ResultReg)
254 .addReg(II.ImplicitDefs[0]));
259 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
260 const TargetRegisterClass *RC,
261 unsigned Op0, bool Op0IsKill,
262 unsigned Op1, bool Op1IsKill,
264 unsigned ResultReg = createResultReg(RC);
265 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
267 if (II.getNumDefs() >= 1)
268 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
269 .addReg(Op0, Op0IsKill * RegState::Kill)
270 .addReg(Op1, Op1IsKill * RegState::Kill)
273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
274 .addReg(Op0, Op0IsKill * RegState::Kill)
275 .addReg(Op1, Op1IsKill * RegState::Kill)
277 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
278 TII.get(TargetOpcode::COPY), ResultReg)
279 .addReg(II.ImplicitDefs[0]));
284 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
285 const TargetRegisterClass *RC,
287 unsigned ResultReg = createResultReg(RC);
288 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
290 if (II.getNumDefs() >= 1)
291 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
296 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
297 TII.get(TargetOpcode::COPY), ResultReg)
298 .addReg(II.ImplicitDefs[0]));
303 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
304 unsigned Op0, bool Op0IsKill,
306 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
307 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
308 "Cannot yet extract from physregs");
309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
310 DL, TII.get(TargetOpcode::COPY), ResultReg)
311 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
315 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
316 VT = TLI.getValueType(Ty, true);
318 // Only handle simple types.
319 if (VT == MVT::Other || !VT.isSimple()) return false;
321 // Handle all legal types, i.e. a register that will directly hold this
323 return TLI.isTypeLegal(VT);
326 // Computes the Reg+Offset to get to an object.
327 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
329 // Some boilerplate from the X86 FastISel.
330 const User *U = NULL;
331 unsigned Opcode = Instruction::UserOp1;
332 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
333 // Don't walk into other basic blocks; it's possible we haven't
334 // visited them yet, so the instructions may not yet be assigned
335 // virtual registers.
336 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
339 Opcode = I->getOpcode();
341 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
342 Opcode = C->getOpcode();
346 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
347 if (Ty->getAddressSpace() > 255)
348 // Fast instruction selection doesn't support the special
354 //errs() << "Failing Opcode is: " << *Op1 << "\n";
356 case Instruction::Alloca: {
357 assert(false && "Alloca should have been handled earlier!");
362 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
363 //errs() << "Failing GV is: " << GV << "\n";
368 // Try to get this in a register if nothing else has worked.
369 Reg = getRegForValue(Obj);
373 bool ARMFastISel::ARMLoadAlloca(const Instruction *I) {
374 Value *Op0 = I->getOperand(0);
376 // Verify it's an alloca.
377 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
378 DenseMap<const AllocaInst*, int>::iterator SI =
379 FuncInfo.StaticAllocaMap.find(AI);
381 if (SI != FuncInfo.StaticAllocaMap.end()) {
382 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
383 unsigned ResultReg = createResultReg(RC);
384 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
385 ResultReg, SI->second, RC,
386 TM.getRegisterInfo());
387 UpdateValueMap(I, ResultReg);
394 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
395 unsigned Reg, int Offset) {
397 assert(VT.isSimple() && "Non-simple types are invalid here!");
399 bool isThumb = AFI->isThumbFunction();
402 switch (VT.getSimpleVT().SimpleTy) {
404 assert(false && "Trying to emit for an unhandled type!");
407 Opc = isThumb ? ARM::tLDR : ARM::LDR;
411 ResultReg = createResultReg(TLI.getRegClassFor(VT));
413 // TODO: Fix the Addressing modes so that these can share some code.
414 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
417 TII.get(Opc), ResultReg)
418 .addReg(Reg).addImm(Offset).addReg(0));
420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
421 TII.get(Opc), ResultReg)
422 .addReg(Reg).addReg(0).addImm(Offset));
427 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
428 // If we're an alloca we know we have a frame index and can emit the load
429 // directly in short order.
430 if (ARMLoadAlloca(I))
433 // Verify we have a legal type before going any further.
435 if (!isTypeLegal(I->getType(), VT))
438 // Our register and offset with innocuous defaults.
442 // See if we can handle this as Reg + Offset
443 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
446 // Since the offset may be too large for the load instruction
447 // get the reg+offset into a register.
448 // TODO: Optimize this somewhat.
449 ARMCC::CondCodes Pred = ARMCC::AL;
450 unsigned PredReg = 0;
452 if (!AFI->isThumbFunction())
453 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
454 Reg, Reg, Offset, Pred, PredReg,
455 static_cast<const ARMBaseInstrInfo&>(TII));
457 assert(AFI->isThumb2Function());
458 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
459 Reg, Reg, Offset, Pred, PredReg,
460 static_cast<const ARMBaseInstrInfo&>(TII));
464 // TODO: Verify the additions above work, otherwise we'll need to add the
465 // offset instead of 0 and do all sorts of operand munging.
466 if (!ARMEmitLoad(VT, ResultReg, Reg, 0)) return false;
468 UpdateValueMap(I, ResultReg);
472 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
473 // No Thumb-1 for now.
474 if (AFI->isThumbFunction() && !AFI->isThumb2Function()) return false;
476 switch (I->getOpcode()) {
477 case Instruction::Load:
478 return ARMSelectLoad(I);
485 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
486 if (EnableARMFastISel) return new ARMFastISel(funcInfo);