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;
62 // Convenience variable to avoid checking all the time.
66 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
68 TM(funcInfo.MF->getTarget()),
69 TII(*TM.getInstrInfo()),
70 TLI(*TM.getTargetLowering()) {
71 Subtarget = &TM.getSubtarget<ARMSubtarget>();
72 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
73 isThumb = AFI->isThumbFunction();
76 // Code from FastISel.cpp.
77 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,
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,
97 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 unsigned Op1, bool Op1IsKill,
102 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
103 unsigned Op0, bool Op0IsKill,
106 // Backend specific FastISel code.
107 virtual bool TargetSelectInstruction(const Instruction *I);
108 virtual unsigned TargetMaterializeConstant(const Constant *C);
110 #include "ARMGenFastISel.inc"
112 // Instruction selection routines.
114 virtual bool ARMSelectLoad(const Instruction *I);
115 virtual bool ARMSelectStore(const Instruction *I);
116 virtual bool ARMSelectBranch(const Instruction *I);
117 virtual bool ARMSelectCmp(const Instruction *I);
118 virtual bool ARMSelectFPExt(const Instruction *I);
119 virtual bool ARMSelectFPTrunc(const Instruction *I);
120 virtual bool ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
121 virtual bool ARMSelectSIToFP(const Instruction *I);
122 virtual bool ARMSelectFPToSI(const Instruction *I);
126 bool isTypeLegal(const Type *Ty, EVT &VT);
127 bool isLoadTypeLegal(const Type *Ty, EVT &VT);
128 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Reg, int Offset);
129 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Reg, int Offset);
130 bool ARMLoadAlloca(const Instruction *I, EVT VT);
131 bool ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT);
132 bool ARMComputeRegOffset(const Value *Obj, unsigned &Reg, int &Offset);
133 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
134 unsigned ARMMaterializeInt(const Constant *C);
135 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
136 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
138 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
139 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
142 } // end anonymous namespace
144 // #include "ARMGenCallingConv.inc"
146 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
147 // we don't care about implicit defs here, just places we'll need to add a
148 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
149 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
150 const TargetInstrDesc &TID = MI->getDesc();
151 if (!TID.hasOptionalDef())
154 // Look to see if our OptionalDef is defining CPSR or CCR.
155 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
156 const MachineOperand &MO = MI->getOperand(i);
157 if (!MO.isReg() || !MO.isDef()) continue;
158 if (MO.getReg() == ARM::CPSR)
164 // If the machine is predicable go ahead and add the predicate operands, if
165 // it needs default CC operands add those.
166 const MachineInstrBuilder &
167 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
168 MachineInstr *MI = &*MIB;
170 // Do we use a predicate?
171 if (TII.isPredicable(MI))
174 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
175 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
177 if (DefinesOptionalPredicate(MI, &CPSR)) {
186 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
187 const TargetRegisterClass* RC) {
188 unsigned ResultReg = createResultReg(RC);
189 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
191 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
195 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
196 const TargetRegisterClass *RC,
197 unsigned Op0, bool Op0IsKill) {
198 unsigned ResultReg = createResultReg(RC);
199 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
201 if (II.getNumDefs() >= 1)
202 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
203 .addReg(Op0, Op0IsKill * RegState::Kill));
205 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
206 .addReg(Op0, Op0IsKill * RegState::Kill));
207 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
208 TII.get(TargetOpcode::COPY), ResultReg)
209 .addReg(II.ImplicitDefs[0]));
214 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
215 const TargetRegisterClass *RC,
216 unsigned Op0, bool Op0IsKill,
217 unsigned Op1, bool Op1IsKill) {
218 unsigned ResultReg = createResultReg(RC);
219 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
221 if (II.getNumDefs() >= 1)
222 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
223 .addReg(Op0, Op0IsKill * RegState::Kill)
224 .addReg(Op1, Op1IsKill * RegState::Kill));
226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
227 .addReg(Op0, Op0IsKill * RegState::Kill)
228 .addReg(Op1, Op1IsKill * RegState::Kill));
229 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
230 TII.get(TargetOpcode::COPY), ResultReg)
231 .addReg(II.ImplicitDefs[0]));
236 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
237 const TargetRegisterClass *RC,
238 unsigned Op0, bool Op0IsKill,
240 unsigned ResultReg = createResultReg(RC);
241 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
243 if (II.getNumDefs() >= 1)
244 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
245 .addReg(Op0, Op0IsKill * RegState::Kill)
248 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
249 .addReg(Op0, Op0IsKill * RegState::Kill)
251 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
252 TII.get(TargetOpcode::COPY), ResultReg)
253 .addReg(II.ImplicitDefs[0]));
258 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
259 const TargetRegisterClass *RC,
260 unsigned Op0, bool Op0IsKill,
261 const ConstantFP *FPImm) {
262 unsigned ResultReg = createResultReg(RC);
263 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
265 if (II.getNumDefs() >= 1)
266 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
267 .addReg(Op0, Op0IsKill * RegState::Kill)
270 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
271 .addReg(Op0, Op0IsKill * RegState::Kill)
273 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(II.ImplicitDefs[0]));
280 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
281 const TargetRegisterClass *RC,
282 unsigned Op0, bool Op0IsKill,
283 unsigned Op1, bool Op1IsKill,
285 unsigned ResultReg = createResultReg(RC);
286 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
288 if (II.getNumDefs() >= 1)
289 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
290 .addReg(Op0, Op0IsKill * RegState::Kill)
291 .addReg(Op1, Op1IsKill * RegState::Kill)
294 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
295 .addReg(Op0, Op0IsKill * RegState::Kill)
296 .addReg(Op1, Op1IsKill * RegState::Kill)
298 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
299 TII.get(TargetOpcode::COPY), ResultReg)
300 .addReg(II.ImplicitDefs[0]));
305 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
306 const TargetRegisterClass *RC,
308 unsigned ResultReg = createResultReg(RC);
309 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
311 if (II.getNumDefs() >= 1)
312 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
315 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
317 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
318 TII.get(TargetOpcode::COPY), ResultReg)
319 .addReg(II.ImplicitDefs[0]));
324 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
325 unsigned Op0, bool Op0IsKill,
327 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
328 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
329 "Cannot yet extract from physregs");
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
331 DL, TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
336 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
337 // checks from the various callers.
338 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
339 if (VT.getSimpleVT().SimpleTy == MVT::f64) return 0;
341 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
342 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
343 TII.get(ARM::VMOVRS), MoveReg)
348 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
349 if (VT.getSimpleVT().SimpleTy == MVT::i64) return 0;
351 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
353 TII.get(ARM::VMOVSR), MoveReg)
358 // For double width floating point we need to materialize two constants
359 // (the high and the low) into integer registers then use a move to get
360 // the combined constant into an FP reg.
361 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
362 const APFloat Val = CFP->getValueAPF();
363 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64;
365 // This checks to see if we can use VFP3 instructions to materialize
366 // a constant, otherwise we have to go through the constant pool.
367 if (TLI.isFPImmLegal(Val, VT)) {
368 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
369 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
376 // Require VFP2 for loading fp constants.
377 if (!Subtarget->hasVFP2()) return false;
379 // MachineConstantPool wants an explicit alignment.
380 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
382 // TODO: Figure out if this is correct.
383 Align = TD.getTypeAllocSize(CFP->getType());
385 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
386 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
387 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
389 // The extra reg is for addrmode5.
390 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
391 .addReg(DestReg).addConstantPoolIndex(Idx)
396 // TODO: Verify 64-bit.
397 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C) {
398 // MachineConstantPool wants an explicit alignment.
399 unsigned Align = TD.getPrefTypeAlignment(C->getType());
401 // TODO: Figure out if this is correct.
402 Align = TD.getTypeAllocSize(C->getType());
404 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
405 unsigned DestReg = createResultReg(TLI.getRegClassFor(MVT::i32));
408 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
409 TII.get(ARM::t2LDRpci))
410 .addReg(DestReg).addConstantPoolIndex(Idx));
412 // The extra reg and immediate are for addrmode2.
413 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
415 .addReg(DestReg).addConstantPoolIndex(Idx)
416 .addReg(0).addImm(0));
421 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
422 EVT VT = TLI.getValueType(C->getType(), true);
424 // Only handle simple types.
425 if (!VT.isSimple()) return 0;
427 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
428 return ARMMaterializeFP(CFP, VT);
429 return ARMMaterializeInt(C);
432 bool ARMFastISel::isTypeLegal(const Type *Ty, EVT &VT) {
433 VT = TLI.getValueType(Ty, true);
435 // Only handle simple types.
436 if (VT == MVT::Other || !VT.isSimple()) return false;
438 // Handle all legal types, i.e. a register that will directly hold this
440 return TLI.isTypeLegal(VT);
443 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, EVT &VT) {
444 if (isTypeLegal(Ty, VT)) return true;
446 // If this is a type than can be sign or zero-extended to a basic operation
447 // go ahead and accept it now.
448 if (VT == MVT::i8 || VT == MVT::i16)
454 // Computes the Reg+Offset to get to an object.
455 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Reg,
457 // Some boilerplate from the X86 FastISel.
458 const User *U = NULL;
459 unsigned Opcode = Instruction::UserOp1;
460 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
461 // Don't walk into other basic blocks; it's possible we haven't
462 // visited them yet, so the instructions may not yet be assigned
463 // virtual registers.
464 if (FuncInfo.MBBMap[I->getParent()] != FuncInfo.MBB)
466 Opcode = I->getOpcode();
468 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
469 Opcode = C->getOpcode();
473 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
474 if (Ty->getAddressSpace() > 255)
475 // Fast instruction selection doesn't support the special
482 case Instruction::Alloca: {
483 assert(false && "Alloca should have been handled earlier!");
488 // FIXME: Handle global variables.
489 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
494 // Try to get this in a register if nothing else has worked.
495 Reg = getRegForValue(Obj);
496 if (Reg == 0) return false;
498 // Since the offset may be too large for the load instruction
499 // get the reg+offset into a register.
500 // TODO: Verify the additions work, otherwise we'll need to add the
501 // offset instead of 0 to the instructions and do all sorts of operand
503 // TODO: Optimize this somewhat.
505 ARMCC::CondCodes Pred = ARMCC::AL;
506 unsigned PredReg = 0;
509 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
510 Reg, Reg, Offset, Pred, PredReg,
511 static_cast<const ARMBaseInstrInfo&>(TII));
513 assert(AFI->isThumb2Function());
514 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
515 Reg, Reg, Offset, Pred, PredReg,
516 static_cast<const ARMBaseInstrInfo&>(TII));
522 bool ARMFastISel::ARMLoadAlloca(const Instruction *I, EVT VT) {
523 Value *Op0 = I->getOperand(0);
525 // Verify it's an alloca.
526 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op0)) {
527 DenseMap<const AllocaInst*, int>::iterator SI =
528 FuncInfo.StaticAllocaMap.find(AI);
530 if (SI != FuncInfo.StaticAllocaMap.end()) {
531 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
532 unsigned ResultReg = createResultReg(RC);
533 TII.loadRegFromStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
534 ResultReg, SI->second, RC,
535 TM.getRegisterInfo());
536 UpdateValueMap(I, ResultReg);
543 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
544 unsigned Reg, int Offset) {
546 assert(VT.isSimple() && "Non-simple types are invalid here!");
549 switch (VT.getSimpleVT().SimpleTy) {
551 assert(false && "Trying to emit for an unhandled type!");
554 Opc = isThumb ? ARM::tLDRH : ARM::LDRH;
558 Opc = isThumb ? ARM::tLDRB : ARM::LDRB;
562 Opc = isThumb ? ARM::tLDR : ARM::LDR;
566 ResultReg = createResultReg(TLI.getRegClassFor(VT));
568 // TODO: Fix the Addressing modes so that these can share some code.
569 // Since this is a Thumb1 load this will work in Thumb1 or 2 mode.
571 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
572 TII.get(Opc), ResultReg)
573 .addReg(Reg).addImm(Offset).addReg(0));
575 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
576 TII.get(Opc), ResultReg)
577 .addReg(Reg).addReg(0).addImm(Offset));
581 bool ARMFastISel::ARMSelectLoad(const Instruction *I) {
582 // Verify we have a legal type before going any further.
584 if (!isLoadTypeLegal(I->getType(), VT))
587 // If we're an alloca we know we have a frame index and can emit the load
588 // directly in short order.
589 if (ARMLoadAlloca(I, VT))
592 // Our register and offset with innocuous defaults.
596 // See if we can handle this as Reg + Offset
597 if (!ARMComputeRegOffset(I->getOperand(0), Reg, Offset))
601 if (!ARMEmitLoad(VT, ResultReg, Reg, Offset /* 0 */)) return false;
603 UpdateValueMap(I, ResultReg);
607 bool ARMFastISel::ARMStoreAlloca(const Instruction *I, unsigned SrcReg, EVT VT){
608 Value *Op1 = I->getOperand(1);
610 // Verify it's an alloca.
611 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
612 DenseMap<const AllocaInst*, int>::iterator SI =
613 FuncInfo.StaticAllocaMap.find(AI);
615 if (SI != FuncInfo.StaticAllocaMap.end()) {
616 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
617 assert(SrcReg != 0 && "Nothing to store!");
618 TII.storeRegToStackSlot(*FuncInfo.MBB, *FuncInfo.InsertPt,
619 SrcReg, true /*isKill*/, SI->second, RC,
620 TM.getRegisterInfo());
627 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
628 unsigned DstReg, int Offset) {
630 switch (VT.getSimpleVT().SimpleTy) {
631 default: return false;
633 case MVT::i8: StrOpc = isThumb ? ARM::tSTRB : ARM::STRB; break;
634 case MVT::i16: StrOpc = isThumb ? ARM::tSTRH : ARM::STRH; break;
635 case MVT::i32: StrOpc = isThumb ? ARM::tSTR : ARM::STR; break;
637 if (!Subtarget->hasVFP2()) return false;
641 if (!Subtarget->hasVFP2()) return false;
647 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
648 TII.get(StrOpc), SrcReg)
649 .addReg(DstReg).addImm(Offset).addReg(0));
651 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
652 TII.get(StrOpc), SrcReg)
653 .addReg(DstReg).addReg(0).addImm(Offset));
658 bool ARMFastISel::ARMSelectStore(const Instruction *I) {
659 Value *Op0 = I->getOperand(0);
662 // Yay type legalization
664 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
667 // Get the value to be stored into a register.
668 SrcReg = getRegForValue(Op0);
672 // If we're an alloca we know we have a frame index and can emit the store
674 if (ARMStoreAlloca(I, SrcReg, VT))
677 // Our register and offset with innocuous defaults.
681 // See if we can handle this as Reg + Offset
682 if (!ARMComputeRegOffset(I->getOperand(1), Reg, Offset))
685 if (!ARMEmitStore(VT, SrcReg, Reg, Offset /* 0 */)) return false;
690 bool ARMFastISel::ARMSelectBranch(const Instruction *I) {
691 const BranchInst *BI = cast<BranchInst>(I);
692 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
693 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
695 // Simple branch support.
696 unsigned CondReg = getRegForValue(BI->getCondition());
697 if (CondReg == 0) return false;
699 unsigned CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
700 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
701 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
702 .addReg(CondReg).addReg(CondReg));
703 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
704 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
705 FastEmitBranch(FBB, DL);
706 FuncInfo.MBB->addSuccessor(TBB);
710 bool ARMFastISel::ARMSelectCmp(const Instruction *I) {
711 const CmpInst *CI = cast<CmpInst>(I);
714 const Type *Ty = CI->getOperand(0)->getType();
715 if (!isTypeLegal(Ty, VT))
718 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
719 if (isFloat && !Subtarget->hasVFP2())
723 switch (VT.getSimpleVT().SimpleTy) {
724 default: return false;
725 // TODO: Verify compares.
727 CmpOpc = ARM::VCMPES;
730 CmpOpc = ARM::VCMPED;
733 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
737 unsigned Arg1 = getRegForValue(CI->getOperand(0));
738 if (Arg1 == 0) return false;
740 unsigned Arg2 = getRegForValue(CI->getOperand(1));
741 if (Arg2 == 0) return false;
743 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
744 .addReg(Arg1).addReg(Arg2));
746 // For floating point we need to move the result to a comparison register
747 // that we can then use for branches.
749 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
750 TII.get(ARM::FMSTAT)));
752 // TODO: How to update the value map when there's no result reg?
756 bool ARMFastISel::ARMSelectFPExt(const Instruction *I) {
757 // Make sure we have VFP and that we're extending float to double.
758 if (!Subtarget->hasVFP2()) return false;
760 Value *V = I->getOperand(0);
761 if (!I->getType()->isDoubleTy() ||
762 !V->getType()->isFloatTy()) return false;
764 unsigned Op = getRegForValue(V);
765 if (Op == 0) return false;
767 unsigned Result = createResultReg(ARM::DPRRegisterClass);
768 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
769 TII.get(ARM::VCVTDS), Result)
771 UpdateValueMap(I, Result);
775 bool ARMFastISel::ARMSelectFPTrunc(const Instruction *I) {
776 // Make sure we have VFP and that we're truncating double to float.
777 if (!Subtarget->hasVFP2()) return false;
779 Value *V = I->getOperand(0);
780 if (!I->getType()->isFloatTy() ||
781 !V->getType()->isDoubleTy()) return false;
783 unsigned Op = getRegForValue(V);
784 if (Op == 0) return false;
786 unsigned Result = createResultReg(ARM::SPRRegisterClass);
787 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
788 TII.get(ARM::VCVTSD), Result)
790 UpdateValueMap(I, Result);
794 bool ARMFastISel::ARMSelectSIToFP(const Instruction *I) {
795 // Make sure we have VFP.
796 if (!Subtarget->hasVFP2()) return false;
799 const Type *Ty = I->getType();
800 if (!isTypeLegal(Ty, DstVT))
803 unsigned Op = getRegForValue(I->getOperand(0));
804 if (Op == 0) return false;
806 // The conversion routine works on fp-reg to fp-reg and the operand above
807 // was an integer, move it to the fp registers if possible.
808 unsigned FP = ARMMoveToFPReg(DstVT, Op);
809 if (FP == 0) return false;
812 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
813 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
816 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
817 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
820 UpdateValueMap(I, ResultReg);
824 bool ARMFastISel::ARMSelectFPToSI(const Instruction *I) {
825 // Make sure we have VFP.
826 if (!Subtarget->hasVFP2()) return false;
829 const Type *RetTy = I->getType();
830 if (!isTypeLegal(RetTy, DstVT))
833 unsigned Op = getRegForValue(I->getOperand(0));
834 if (Op == 0) return false;
837 const Type *OpTy = I->getOperand(0)->getType();
838 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
839 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
841 EVT OpVT = TLI.getValueType(OpTy, true);
843 unsigned ResultReg = createResultReg(TLI.getRegClassFor(OpVT));
844 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
848 // This result needs to be in an integer register, but the conversion only
849 // takes place in fp-regs.
850 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
851 if (IntReg == 0) return false;
853 UpdateValueMap(I, IntReg);
857 bool ARMFastISel::ARMSelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
858 EVT VT = TLI.getValueType(I->getType(), true);
860 // We can get here in the case when we want to use NEON for our fp
861 // operations, but can't figure out how to. Just use the vfp instructions
863 // FIXME: It'd be nice to use NEON instructions.
864 const Type *Ty = I->getType();
865 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
866 if (isFloat && !Subtarget->hasVFP2())
869 unsigned Op1 = getRegForValue(I->getOperand(0));
870 if (Op1 == 0) return false;
872 unsigned Op2 = getRegForValue(I->getOperand(1));
873 if (Op2 == 0) return false;
876 bool is64bit = VT.getSimpleVT().SimpleTy == MVT::f64 ||
877 VT.getSimpleVT().SimpleTy == MVT::i64;
879 default: return false;
881 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
884 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
887 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
890 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
891 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
892 TII.get(Opc), ResultReg)
893 .addReg(Op1).addReg(Op2));
894 UpdateValueMap(I, ResultReg);
898 // TODO: SoftFP support.
899 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
900 // No Thumb-1 for now.
901 if (isThumb && !AFI->isThumb2Function()) return false;
903 switch (I->getOpcode()) {
904 case Instruction::Load:
905 return ARMSelectLoad(I);
906 case Instruction::Store:
907 return ARMSelectStore(I);
908 case Instruction::Br:
909 return ARMSelectBranch(I);
910 case Instruction::ICmp:
911 case Instruction::FCmp:
912 return ARMSelectCmp(I);
913 case Instruction::FPExt:
914 return ARMSelectFPExt(I);
915 case Instruction::FPTrunc:
916 return ARMSelectFPTrunc(I);
917 case Instruction::SIToFP:
918 return ARMSelectSIToFP(I);
919 case Instruction::FPToSI:
920 return ARMSelectFPToSI(I);
921 case Instruction::FAdd:
922 return ARMSelectBinaryOp(I, ISD::FADD);
923 case Instruction::FSub:
924 return ARMSelectBinaryOp(I, ISD::FSUB);
925 case Instruction::FMul:
926 return ARMSelectBinaryOp(I, ISD::FMUL);
933 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
934 if (EnableARMFastISel) return new ARMFastISel(funcInfo);