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 "ARMCallingConv.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMTargetMachine.h"
21 #include "ARMSubtarget.h"
22 #include "ARMConstantPoolValue.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/CodeGen/Analysis.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineConstantPool.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/PseudoSourceValue.h"
39 #include "llvm/Support/CallSite.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/GetElementPtrTypeIterator.h"
43 #include "llvm/Target/TargetData.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
51 DisableARMFastISel("disable-arm-fast-isel",
52 cl::desc("Turn off experimental ARM fast-isel support"),
53 cl::init(false), cl::Hidden);
57 class ARMFastISel : public FastISel {
59 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
60 /// make the right decision when generating code for different targets.
61 const ARMSubtarget *Subtarget;
62 const TargetMachine &TM;
63 const TargetInstrInfo &TII;
64 const TargetLowering &TLI;
67 // Convenience variables to avoid some queries.
72 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
74 TM(funcInfo.MF->getTarget()),
75 TII(*TM.getInstrInfo()),
76 TLI(*TM.getTargetLowering()) {
77 Subtarget = &TM.getSubtarget<ARMSubtarget>();
78 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
79 isThumb = AFI->isThumbFunction();
80 Context = &funcInfo.Fn->getContext();
83 // Code from FastISel.cpp.
84 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
85 const TargetRegisterClass *RC);
86 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
87 const TargetRegisterClass *RC,
88 unsigned Op0, bool Op0IsKill);
89 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
90 const TargetRegisterClass *RC,
91 unsigned Op0, bool Op0IsKill,
92 unsigned Op1, bool Op1IsKill);
93 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
94 const TargetRegisterClass *RC,
95 unsigned Op0, bool Op0IsKill,
97 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
98 const TargetRegisterClass *RC,
99 unsigned Op0, bool Op0IsKill,
100 const ConstantFP *FPImm);
101 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
102 const TargetRegisterClass *RC,
104 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC,
106 unsigned Op0, bool Op0IsKill,
107 unsigned Op1, bool Op1IsKill,
109 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
110 unsigned Op0, bool Op0IsKill,
113 // Backend specific FastISel code.
114 virtual bool TargetSelectInstruction(const Instruction *I);
115 virtual unsigned TargetMaterializeConstant(const Constant *C);
116 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
118 #include "ARMGenFastISel.inc"
120 // Instruction selection routines.
122 bool SelectLoad(const Instruction *I);
123 bool SelectStore(const Instruction *I);
124 bool SelectBranch(const Instruction *I);
125 bool SelectCmp(const Instruction *I);
126 bool SelectFPExt(const Instruction *I);
127 bool SelectFPTrunc(const Instruction *I);
128 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
129 bool SelectSIToFP(const Instruction *I);
130 bool SelectFPToSI(const Instruction *I);
131 bool SelectSDiv(const Instruction *I);
132 bool SelectSRem(const Instruction *I);
133 bool SelectCall(const Instruction *I);
134 bool SelectSelect(const Instruction *I);
135 bool SelectRet(const Instruction *I);
139 bool isTypeLegal(const Type *Ty, MVT &VT);
140 bool isLoadTypeLegal(const Type *Ty, MVT &VT);
141 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, unsigned Base, int Offset);
142 bool ARMEmitStore(EVT VT, unsigned SrcReg, unsigned Base, int Offset);
143 bool ARMComputeRegOffset(const Value *Obj, unsigned &Base, int &Offset);
144 void ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT);
145 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
146 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
147 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
148 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
149 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
151 // Call handling routines.
153 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
154 unsigned &ResultReg);
155 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
156 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
157 SmallVectorImpl<unsigned> &ArgRegs,
158 SmallVectorImpl<MVT> &ArgVTs,
159 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
160 SmallVectorImpl<unsigned> &RegArgs,
163 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
164 const Instruction *I, CallingConv::ID CC,
166 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
168 // OptionalDef handling routines.
170 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
171 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
174 } // end anonymous namespace
176 #include "ARMGenCallingConv.inc"
178 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
179 // we don't care about implicit defs here, just places we'll need to add a
180 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
181 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
182 const TargetInstrDesc &TID = MI->getDesc();
183 if (!TID.hasOptionalDef())
186 // Look to see if our OptionalDef is defining CPSR or CCR.
187 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
188 const MachineOperand &MO = MI->getOperand(i);
189 if (!MO.isReg() || !MO.isDef()) continue;
190 if (MO.getReg() == ARM::CPSR)
196 // If the machine is predicable go ahead and add the predicate operands, if
197 // it needs default CC operands add those.
198 // TODO: If we want to support thumb1 then we'll need to deal with optional
199 // CPSR defs that need to be added before the remaining operands. See s_cc_out
200 // for descriptions why.
201 const MachineInstrBuilder &
202 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
203 MachineInstr *MI = &*MIB;
205 // Do we use a predicate?
206 if (TII.isPredicable(MI))
209 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
210 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
212 if (DefinesOptionalPredicate(MI, &CPSR)) {
221 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
222 const TargetRegisterClass* RC) {
223 unsigned ResultReg = createResultReg(RC);
224 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
226 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
230 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
231 const TargetRegisterClass *RC,
232 unsigned Op0, bool Op0IsKill) {
233 unsigned ResultReg = createResultReg(RC);
234 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
236 if (II.getNumDefs() >= 1)
237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
238 .addReg(Op0, Op0IsKill * RegState::Kill));
240 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
241 .addReg(Op0, Op0IsKill * RegState::Kill));
242 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
243 TII.get(TargetOpcode::COPY), ResultReg)
244 .addReg(II.ImplicitDefs[0]));
249 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
250 const TargetRegisterClass *RC,
251 unsigned Op0, bool Op0IsKill,
252 unsigned Op1, bool Op1IsKill) {
253 unsigned ResultReg = createResultReg(RC);
254 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
256 if (II.getNumDefs() >= 1)
257 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
258 .addReg(Op0, Op0IsKill * RegState::Kill)
259 .addReg(Op1, Op1IsKill * RegState::Kill));
261 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
262 .addReg(Op0, Op0IsKill * RegState::Kill)
263 .addReg(Op1, Op1IsKill * RegState::Kill));
264 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
265 TII.get(TargetOpcode::COPY), ResultReg)
266 .addReg(II.ImplicitDefs[0]));
271 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
272 const TargetRegisterClass *RC,
273 unsigned Op0, bool Op0IsKill,
275 unsigned ResultReg = createResultReg(RC);
276 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
278 if (II.getNumDefs() >= 1)
279 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
280 .addReg(Op0, Op0IsKill * RegState::Kill)
283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
284 .addReg(Op0, Op0IsKill * RegState::Kill)
286 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
287 TII.get(TargetOpcode::COPY), ResultReg)
288 .addReg(II.ImplicitDefs[0]));
293 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
294 const TargetRegisterClass *RC,
295 unsigned Op0, bool Op0IsKill,
296 const ConstantFP *FPImm) {
297 unsigned ResultReg = createResultReg(RC);
298 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
300 if (II.getNumDefs() >= 1)
301 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
302 .addReg(Op0, Op0IsKill * RegState::Kill)
305 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
306 .addReg(Op0, Op0IsKill * RegState::Kill)
308 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
309 TII.get(TargetOpcode::COPY), ResultReg)
310 .addReg(II.ImplicitDefs[0]));
315 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 unsigned Op0, bool Op0IsKill,
318 unsigned Op1, bool Op1IsKill,
320 unsigned ResultReg = createResultReg(RC);
321 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
323 if (II.getNumDefs() >= 1)
324 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
325 .addReg(Op0, Op0IsKill * RegState::Kill)
326 .addReg(Op1, Op1IsKill * RegState::Kill)
329 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
330 .addReg(Op0, Op0IsKill * RegState::Kill)
331 .addReg(Op1, Op1IsKill * RegState::Kill)
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
340 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
343 unsigned ResultReg = createResultReg(RC);
344 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
346 if (II.getNumDefs() >= 1)
347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
350 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
353 TII.get(TargetOpcode::COPY), ResultReg)
354 .addReg(II.ImplicitDefs[0]));
359 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
360 unsigned Op0, bool Op0IsKill,
362 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
363 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
364 "Cannot yet extract from physregs");
365 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
366 DL, TII.get(TargetOpcode::COPY), ResultReg)
367 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
371 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
372 // checks from the various callers.
373 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
374 if (VT == MVT::f64) return 0;
376 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(ARM::VMOVRS), MoveReg)
383 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
384 if (VT == MVT::i64) return 0;
386 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
388 TII.get(ARM::VMOVSR), MoveReg)
393 // For double width floating point we need to materialize two constants
394 // (the high and the low) into integer registers then use a move to get
395 // the combined constant into an FP reg.
396 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
397 const APFloat Val = CFP->getValueAPF();
398 bool is64bit = VT == MVT::f64;
400 // This checks to see if we can use VFP3 instructions to materialize
401 // a constant, otherwise we have to go through the constant pool.
402 if (TLI.isFPImmLegal(Val, VT)) {
403 unsigned Opc = is64bit ? ARM::FCONSTD : ARM::FCONSTS;
404 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
405 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
411 // Require VFP2 for loading fp constants.
412 if (!Subtarget->hasVFP2()) return false;
414 // MachineConstantPool wants an explicit alignment.
415 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
417 // TODO: Figure out if this is correct.
418 Align = TD.getTypeAllocSize(CFP->getType());
420 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
421 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
422 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
424 // The extra reg is for addrmode5.
425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
427 .addConstantPoolIndex(Idx)
432 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
434 // For now 32-bit only.
435 if (VT != MVT::i32) return false;
437 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
439 // If we can do this in a single instruction without a constant pool entry
441 const ConstantInt *CI = cast<ConstantInt>(C);
442 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getSExtValue())) {
443 unsigned Opc = isThumb ? ARM::t2MOVi16 : ARM::MOVi16;
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
445 TII.get(Opc), DestReg)
446 .addImm(CI->getSExtValue()));
450 // MachineConstantPool wants an explicit alignment.
451 unsigned Align = TD.getPrefTypeAlignment(C->getType());
453 // TODO: Figure out if this is correct.
454 Align = TD.getTypeAllocSize(C->getType());
456 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
459 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
460 TII.get(ARM::t2LDRpci), DestReg)
461 .addConstantPoolIndex(Idx));
463 // The extra immediate is for addrmode2.
464 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
465 TII.get(ARM::LDRcp), DestReg)
466 .addConstantPoolIndex(Idx)
472 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
473 // For now 32-bit only.
474 if (VT != MVT::i32) return 0;
476 Reloc::Model RelocM = TM.getRelocationModel();
478 // TODO: No external globals for now.
479 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) return 0;
481 // TODO: Need more magic for ARM PIC.
482 if (!isThumb && (RelocM == Reloc::PIC_)) return 0;
484 // MachineConstantPool wants an explicit alignment.
485 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
487 // TODO: Figure out if this is correct.
488 Align = TD.getTypeAllocSize(GV->getType());
492 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
493 unsigned Id = AFI->createConstPoolEntryUId();
494 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, Id,
495 ARMCP::CPValue, PCAdj);
496 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
499 MachineInstrBuilder MIB;
500 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
502 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
503 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
504 .addConstantPoolIndex(Idx);
505 if (RelocM == Reloc::PIC_)
508 // The extra immediate is for addrmode2.
509 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
511 .addConstantPoolIndex(Idx)
514 AddOptionalDefs(MIB);
518 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
519 EVT VT = TLI.getValueType(C->getType(), true);
521 // Only handle simple types.
522 if (!VT.isSimple()) return 0;
524 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
525 return ARMMaterializeFP(CFP, VT);
526 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
527 return ARMMaterializeGV(GV, VT);
528 else if (isa<ConstantInt>(C))
529 return ARMMaterializeInt(C, VT);
534 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
535 // Don't handle dynamic allocas.
536 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
539 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
541 DenseMap<const AllocaInst*, int>::iterator SI =
542 FuncInfo.StaticAllocaMap.find(AI);
544 // This will get lowered later into the correct offsets and registers
545 // via rewriteXFrameIndex.
546 if (SI != FuncInfo.StaticAllocaMap.end()) {
547 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
548 unsigned ResultReg = createResultReg(RC);
549 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
550 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
551 TII.get(Opc), ResultReg)
552 .addFrameIndex(SI->second)
560 bool ARMFastISel::isTypeLegal(const Type *Ty, MVT &VT) {
561 EVT evt = TLI.getValueType(Ty, true);
563 // Only handle simple types.
564 if (evt == MVT::Other || !evt.isSimple()) return false;
565 VT = evt.getSimpleVT();
567 // Handle all legal types, i.e. a register that will directly hold this
569 return TLI.isTypeLegal(VT);
572 bool ARMFastISel::isLoadTypeLegal(const Type *Ty, MVT &VT) {
573 if (isTypeLegal(Ty, VT)) return true;
575 // If this is a type than can be sign or zero-extended to a basic operation
576 // go ahead and accept it now.
577 if (VT == MVT::i8 || VT == MVT::i16)
583 // Computes the Reg+Offset to get to an object.
584 bool ARMFastISel::ARMComputeRegOffset(const Value *Obj, unsigned &Base,
586 // Some boilerplate from the X86 FastISel.
587 const User *U = NULL;
588 unsigned Opcode = Instruction::UserOp1;
589 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
590 // Don't walk into other basic blocks; it's possible we haven't
591 // visited them yet, so the instructions may not yet be assigned
592 // virtual registers.
593 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
594 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
595 Opcode = I->getOpcode();
598 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
599 Opcode = C->getOpcode();
603 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
604 if (Ty->getAddressSpace() > 255)
605 // Fast instruction selection doesn't support the special
612 case Instruction::BitCast: {
613 // Look through bitcasts.
614 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
616 case Instruction::IntToPtr: {
617 // Look past no-op inttoptrs.
618 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
619 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
622 case Instruction::PtrToInt: {
623 // Look past no-op ptrtoints.
624 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
625 return ARMComputeRegOffset(U->getOperand(0), Base, Offset);
628 case Instruction::GetElementPtr: {
629 int SavedOffset = Offset;
630 unsigned SavedBase = Base;
631 int TmpOffset = Offset;
633 // Iterate through the GEP folding the constants into offsets where
635 gep_type_iterator GTI = gep_type_begin(U);
636 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
637 i != e; ++i, ++GTI) {
638 const Value *Op = *i;
639 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
640 const StructLayout *SL = TD.getStructLayout(STy);
641 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
642 TmpOffset += SL->getElementOffset(Idx);
644 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
645 SmallVector<const Value *, 4> Worklist;
646 Worklist.push_back(Op);
648 Op = Worklist.pop_back_val();
649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
650 // Constant-offset addressing.
651 TmpOffset += CI->getSExtValue() * S;
652 } else if (isa<AddOperator>(Op) &&
653 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
654 // An add with a constant operand. Fold the constant.
656 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
657 TmpOffset += CI->getSExtValue() * S;
658 // Add the other operand back to the work list.
659 Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
661 goto unsupported_gep;
662 } while (!Worklist.empty());
666 // Try to grab the base operand now.
668 if (ARMComputeRegOffset(U->getOperand(0), Base, Offset)) return true;
670 // We failed, restore everything and try the other options.
671 Offset = SavedOffset;
677 case Instruction::Alloca: {
678 const AllocaInst *AI = cast<AllocaInst>(Obj);
679 unsigned Reg = TargetMaterializeAlloca(AI);
681 if (Reg == 0) return false;
688 // Materialize the global variable's address into a reg which can
689 // then be used later to load the variable.
690 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
691 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
692 if (Tmp == 0) return false;
698 // Try to get this in a register if nothing else has worked.
699 if (Base == 0) Base = getRegForValue(Obj);
703 void ARMFastISel::ARMSimplifyRegOffset(unsigned &Base, int &Offset, EVT VT) {
705 assert(VT.isSimple() && "Non-simple types are invalid here!");
707 bool needsLowering = false;
708 switch (VT.getSimpleVT().SimpleTy) {
710 assert(false && "Unhandled load/store type!");
715 // Integer loads/stores handle 12-bit offsets.
716 needsLowering = ((Offset & 0xfff) != Offset);
720 // Floating point operands handle 8-bit offsets.
721 needsLowering = ((Offset & 0xff) != Offset);
725 // Since the offset is too large for the load/store instruction
726 // get the reg+offset into a register.
728 ARMCC::CondCodes Pred = ARMCC::AL;
729 unsigned PredReg = 0;
731 TargetRegisterClass *RC = isThumb ? ARM::tGPRRegisterClass :
732 ARM::GPRRegisterClass;
733 unsigned BaseReg = createResultReg(RC);
736 emitARMRegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
737 BaseReg, Base, Offset, Pred, PredReg,
738 static_cast<const ARMBaseInstrInfo&>(TII));
740 assert(AFI->isThumb2Function());
741 emitT2RegPlusImmediate(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
742 BaseReg, Base, Offset, Pred, PredReg,
743 static_cast<const ARMBaseInstrInfo&>(TII));
750 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg,
751 unsigned Base, int Offset) {
753 assert(VT.isSimple() && "Non-simple types are invalid here!");
755 TargetRegisterClass *RC;
756 bool isFloat = false;
757 switch (VT.getSimpleVT().SimpleTy) {
759 // This is mostly going to be Neon/vector support.
762 Opc = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
763 RC = ARM::GPRRegisterClass;
766 Opc = isThumb ? ARM::t2LDRBi12 : ARM::LDRBi12;
767 RC = ARM::GPRRegisterClass;
770 Opc = isThumb ? ARM::t2LDRi12 : ARM::LDRi12;
771 RC = ARM::GPRRegisterClass;
775 RC = TLI.getRegClassFor(VT);
780 RC = TLI.getRegClassFor(VT);
785 ResultReg = createResultReg(RC);
787 ARMSimplifyRegOffset(Base, Offset, VT);
789 // addrmode5 output depends on the selection dag addressing dividing the
790 // offset by 4 that it then later multiplies. Do this here as well.
794 // LDRH needs an additional operand.
795 if (!isThumb && VT.getSimpleVT().SimpleTy == MVT::i16)
796 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
797 TII.get(Opc), ResultReg)
798 .addReg(Base).addReg(0).addImm(Offset));
800 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
801 TII.get(Opc), ResultReg)
802 .addReg(Base).addImm(Offset));
806 bool ARMFastISel::SelectLoad(const Instruction *I) {
807 // Verify we have a legal type before going any further.
809 if (!isLoadTypeLegal(I->getType(), VT))
812 // Our register and offset with innocuous defaults.
816 // See if we can handle this as Reg + Offset
817 if (!ARMComputeRegOffset(I->getOperand(0), Base, Offset))
821 if (!ARMEmitLoad(VT, ResultReg, Base, Offset)) return false;
823 UpdateValueMap(I, ResultReg);
827 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg,
828 unsigned Base, int Offset) {
830 bool isFloat = false;
831 bool needReg0Op = false;
832 switch (VT.getSimpleVT().SimpleTy) {
833 default: return false;
835 unsigned Res = createResultReg(isThumb ? ARM::tGPRRegisterClass :
836 ARM::GPRRegisterClass);
837 unsigned Opc = isThumb ? ARM::t2ANDri : ARM::ANDri;
838 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
840 .addReg(SrcReg).addImm(1));
842 } // Fallthrough here.
844 StrOpc = isThumb ? ARM::t2STRBi12 : ARM::STRBi12;
847 StrOpc = isThumb ? ARM::t2STRHi12 : ARM::STRH;
851 StrOpc = isThumb ? ARM::t2STRi12 : ARM::STRi12;
854 if (!Subtarget->hasVFP2()) return false;
859 if (!Subtarget->hasVFP2()) return false;
865 ARMSimplifyRegOffset(Base, Offset, VT);
867 // addrmode5 output depends on the selection dag addressing dividing the
868 // offset by 4 that it then later multiplies. Do this here as well.
872 // FIXME: The 'needReg0Op' bit goes away once STRH is converted to
873 // not use the mega-addrmode stuff.
875 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
877 .addReg(SrcReg).addReg(Base).addImm(Offset));
879 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
881 .addReg(SrcReg).addReg(Base).addReg(0).addImm(Offset));
886 bool ARMFastISel::SelectStore(const Instruction *I) {
887 Value *Op0 = I->getOperand(0);
890 // Yay type legalization
892 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
895 // Get the value to be stored into a register.
896 SrcReg = getRegForValue(Op0);
900 // Our register and offset with innocuous defaults.
904 // See if we can handle this as Reg + Offset
905 if (!ARMComputeRegOffset(I->getOperand(1), Base, Offset))
908 if (!ARMEmitStore(VT, SrcReg, Base, Offset)) return false;
913 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
915 // Needs two compares...
916 case CmpInst::FCMP_ONE:
917 case CmpInst::FCMP_UEQ:
919 // AL is our "false" for now. The other two need more compares.
921 case CmpInst::ICMP_EQ:
922 case CmpInst::FCMP_OEQ:
924 case CmpInst::ICMP_SGT:
925 case CmpInst::FCMP_OGT:
927 case CmpInst::ICMP_SGE:
928 case CmpInst::FCMP_OGE:
930 case CmpInst::ICMP_UGT:
931 case CmpInst::FCMP_UGT:
933 case CmpInst::FCMP_OLT:
935 case CmpInst::ICMP_ULE:
936 case CmpInst::FCMP_OLE:
938 case CmpInst::FCMP_ORD:
940 case CmpInst::FCMP_UNO:
942 case CmpInst::FCMP_UGE:
944 case CmpInst::ICMP_SLT:
945 case CmpInst::FCMP_ULT:
947 case CmpInst::ICMP_SLE:
948 case CmpInst::FCMP_ULE:
950 case CmpInst::FCMP_UNE:
951 case CmpInst::ICMP_NE:
953 case CmpInst::ICMP_UGE:
955 case CmpInst::ICMP_ULT:
960 bool ARMFastISel::SelectBranch(const Instruction *I) {
961 const BranchInst *BI = cast<BranchInst>(I);
962 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
963 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
965 // Simple branch support.
967 // If we can, avoid recomputing the compare - redoing it could lead to wonky
969 // TODO: Factor this out.
970 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
971 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
973 const Type *Ty = CI->getOperand(0)->getType();
974 if (!isTypeLegal(Ty, VT))
977 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
978 if (isFloat && !Subtarget->hasVFP2())
983 switch (VT.SimpleTy) {
984 default: return false;
985 // TODO: Verify compares.
987 CmpOpc = ARM::VCMPES;
988 CondReg = ARM::FPSCR;
991 CmpOpc = ARM::VCMPED;
992 CondReg = ARM::FPSCR;
995 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1000 // Get the compare predicate.
1001 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1003 // We may not handle every CC for now.
1004 if (ARMPred == ARMCC::AL) return false;
1006 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1007 if (Arg1 == 0) return false;
1009 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1010 if (Arg2 == 0) return false;
1012 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1014 .addReg(Arg1).addReg(Arg2));
1016 // For floating point we need to move the result to a comparison register
1017 // that we can then use for branches.
1019 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1020 TII.get(ARM::FMSTAT)));
1022 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1024 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1025 FastEmitBranch(FBB, DL);
1026 FuncInfo.MBB->addSuccessor(TBB);
1031 unsigned CmpReg = getRegForValue(BI->getCondition());
1032 if (CmpReg == 0) return false;
1034 // Re-set the flags just in case.
1035 unsigned CmpOpc = isThumb ? ARM::t2CMPri : ARM::CMPri;
1036 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1037 .addReg(CmpReg).addImm(0));
1039 unsigned BrOpc = isThumb ? ARM::t2Bcc : ARM::Bcc;
1040 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1041 .addMBB(TBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
1042 FastEmitBranch(FBB, DL);
1043 FuncInfo.MBB->addSuccessor(TBB);
1047 bool ARMFastISel::SelectCmp(const Instruction *I) {
1048 const CmpInst *CI = cast<CmpInst>(I);
1051 const Type *Ty = CI->getOperand(0)->getType();
1052 if (!isTypeLegal(Ty, VT))
1055 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1056 if (isFloat && !Subtarget->hasVFP2())
1061 switch (VT.SimpleTy) {
1062 default: return false;
1063 // TODO: Verify compares.
1065 CmpOpc = ARM::VCMPES;
1066 CondReg = ARM::FPSCR;
1069 CmpOpc = ARM::VCMPED;
1070 CondReg = ARM::FPSCR;
1073 CmpOpc = isThumb ? ARM::t2CMPrr : ARM::CMPrr;
1074 CondReg = ARM::CPSR;
1078 // Get the compare predicate.
1079 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1081 // We may not handle every CC for now.
1082 if (ARMPred == ARMCC::AL) return false;
1084 unsigned Arg1 = getRegForValue(CI->getOperand(0));
1085 if (Arg1 == 0) return false;
1087 unsigned Arg2 = getRegForValue(CI->getOperand(1));
1088 if (Arg2 == 0) return false;
1090 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1091 .addReg(Arg1).addReg(Arg2));
1093 // For floating point we need to move the result to a comparison register
1094 // that we can then use for branches.
1096 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1097 TII.get(ARM::FMSTAT)));
1099 // Now set a register based on the comparison. Explicitly set the predicates
1101 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCi : ARM::MOVCCi;
1102 TargetRegisterClass *RC = isThumb ? ARM::rGPRRegisterClass
1103 : ARM::GPRRegisterClass;
1104 unsigned DestReg = createResultReg(RC);
1106 = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1107 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1109 .addReg(ZeroReg).addImm(1)
1110 .addImm(ARMPred).addReg(CondReg);
1112 UpdateValueMap(I, DestReg);
1116 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1117 // Make sure we have VFP and that we're extending float to double.
1118 if (!Subtarget->hasVFP2()) return false;
1120 Value *V = I->getOperand(0);
1121 if (!I->getType()->isDoubleTy() ||
1122 !V->getType()->isFloatTy()) return false;
1124 unsigned Op = getRegForValue(V);
1125 if (Op == 0) return false;
1127 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1128 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1129 TII.get(ARM::VCVTDS), Result)
1131 UpdateValueMap(I, Result);
1135 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1136 // Make sure we have VFP and that we're truncating double to float.
1137 if (!Subtarget->hasVFP2()) return false;
1139 Value *V = I->getOperand(0);
1140 if (!(I->getType()->isFloatTy() &&
1141 V->getType()->isDoubleTy())) return false;
1143 unsigned Op = getRegForValue(V);
1144 if (Op == 0) return false;
1146 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1147 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1148 TII.get(ARM::VCVTSD), Result)
1150 UpdateValueMap(I, Result);
1154 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1155 // Make sure we have VFP.
1156 if (!Subtarget->hasVFP2()) return false;
1159 const Type *Ty = I->getType();
1160 if (!isTypeLegal(Ty, DstVT))
1163 unsigned Op = getRegForValue(I->getOperand(0));
1164 if (Op == 0) return false;
1166 // The conversion routine works on fp-reg to fp-reg and the operand above
1167 // was an integer, move it to the fp registers if possible.
1168 unsigned FP = ARMMoveToFPReg(MVT::f32, Op);
1169 if (FP == 0) return false;
1172 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1173 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1176 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1177 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1180 UpdateValueMap(I, ResultReg);
1184 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1185 // Make sure we have VFP.
1186 if (!Subtarget->hasVFP2()) return false;
1189 const Type *RetTy = I->getType();
1190 if (!isTypeLegal(RetTy, DstVT))
1193 unsigned Op = getRegForValue(I->getOperand(0));
1194 if (Op == 0) return false;
1197 const Type *OpTy = I->getOperand(0)->getType();
1198 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1199 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1202 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1203 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1204 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1208 // This result needs to be in an integer register, but the conversion only
1209 // takes place in fp-regs.
1210 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1211 if (IntReg == 0) return false;
1213 UpdateValueMap(I, IntReg);
1217 bool ARMFastISel::SelectSelect(const Instruction *I) {
1219 if (!isTypeLegal(I->getType(), VT))
1222 // Things need to be register sized for register moves.
1223 if (VT != MVT::i32) return false;
1224 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1226 unsigned CondReg = getRegForValue(I->getOperand(0));
1227 if (CondReg == 0) return false;
1228 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1229 if (Op1Reg == 0) return false;
1230 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1231 if (Op2Reg == 0) return false;
1233 unsigned CmpOpc = isThumb ? ARM::t2TSTri : ARM::TSTri;
1234 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1235 .addReg(CondReg).addImm(1));
1236 unsigned ResultReg = createResultReg(RC);
1237 unsigned MovCCOpc = isThumb ? ARM::t2MOVCCr : ARM::MOVCCr;
1238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1239 .addReg(Op1Reg).addReg(Op2Reg)
1240 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
1241 UpdateValueMap(I, ResultReg);
1245 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1247 const Type *Ty = I->getType();
1248 if (!isTypeLegal(Ty, VT))
1251 // If we have integer div support we should have selected this automagically.
1252 // In case we have a real miss go ahead and return false and we'll pick
1254 if (Subtarget->hasDivide()) return false;
1256 // Otherwise emit a libcall.
1257 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1259 LC = RTLIB::SDIV_I8;
1260 else if (VT == MVT::i16)
1261 LC = RTLIB::SDIV_I16;
1262 else if (VT == MVT::i32)
1263 LC = RTLIB::SDIV_I32;
1264 else if (VT == MVT::i64)
1265 LC = RTLIB::SDIV_I64;
1266 else if (VT == MVT::i128)
1267 LC = RTLIB::SDIV_I128;
1268 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1270 return ARMEmitLibcall(I, LC);
1273 bool ARMFastISel::SelectSRem(const Instruction *I) {
1275 const Type *Ty = I->getType();
1276 if (!isTypeLegal(Ty, VT))
1279 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1281 LC = RTLIB::SREM_I8;
1282 else if (VT == MVT::i16)
1283 LC = RTLIB::SREM_I16;
1284 else if (VT == MVT::i32)
1285 LC = RTLIB::SREM_I32;
1286 else if (VT == MVT::i64)
1287 LC = RTLIB::SREM_I64;
1288 else if (VT == MVT::i128)
1289 LC = RTLIB::SREM_I128;
1290 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1292 return ARMEmitLibcall(I, LC);
1295 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1296 EVT VT = TLI.getValueType(I->getType(), true);
1298 // We can get here in the case when we want to use NEON for our fp
1299 // operations, but can't figure out how to. Just use the vfp instructions
1301 // FIXME: It'd be nice to use NEON instructions.
1302 const Type *Ty = I->getType();
1303 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1304 if (isFloat && !Subtarget->hasVFP2())
1307 unsigned Op1 = getRegForValue(I->getOperand(0));
1308 if (Op1 == 0) return false;
1310 unsigned Op2 = getRegForValue(I->getOperand(1));
1311 if (Op2 == 0) return false;
1314 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1315 switch (ISDOpcode) {
1316 default: return false;
1318 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1321 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1324 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1327 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1329 TII.get(Opc), ResultReg)
1330 .addReg(Op1).addReg(Op2));
1331 UpdateValueMap(I, ResultReg);
1335 // Call Handling Code
1337 bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1338 EVT SrcVT, unsigned &ResultReg) {
1339 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1340 Src, /*TODO: Kill=*/false);
1349 // This is largely taken directly from CCAssignFnForNode - we don't support
1350 // varargs in FastISel so that part has been removed.
1351 // TODO: We may not support all of this.
1352 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1355 llvm_unreachable("Unsupported calling convention");
1356 case CallingConv::Fast:
1357 // Ignore fastcc. Silence compiler warnings.
1358 (void)RetFastCC_ARM_APCS;
1359 (void)FastCC_ARM_APCS;
1361 case CallingConv::C:
1362 // Use target triple & subtarget features to do actual dispatch.
1363 if (Subtarget->isAAPCS_ABI()) {
1364 if (Subtarget->hasVFP2() &&
1365 FloatABIType == FloatABI::Hard)
1366 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1368 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1370 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1371 case CallingConv::ARM_AAPCS_VFP:
1372 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1373 case CallingConv::ARM_AAPCS:
1374 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1375 case CallingConv::ARM_APCS:
1376 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1380 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1381 SmallVectorImpl<unsigned> &ArgRegs,
1382 SmallVectorImpl<MVT> &ArgVTs,
1383 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1384 SmallVectorImpl<unsigned> &RegArgs,
1386 unsigned &NumBytes) {
1387 SmallVector<CCValAssign, 16> ArgLocs;
1388 CCState CCInfo(CC, false, TM, ArgLocs, *Context);
1389 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1391 // Get a count of how many bytes are to be pushed on the stack.
1392 NumBytes = CCInfo.getNextStackOffset();
1394 // Issue CALLSEQ_START
1395 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1397 TII.get(AdjStackDown))
1400 // Process the args.
1401 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1402 CCValAssign &VA = ArgLocs[i];
1403 unsigned Arg = ArgRegs[VA.getValNo()];
1404 MVT ArgVT = ArgVTs[VA.getValNo()];
1406 // We don't handle NEON parameters yet.
1407 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() > 64)
1410 // Handle arg promotion, etc.
1411 switch (VA.getLocInfo()) {
1412 case CCValAssign::Full: break;
1413 case CCValAssign::SExt: {
1414 bool Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1416 assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
1418 ArgVT = VA.getLocVT();
1421 case CCValAssign::ZExt: {
1422 bool Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1424 assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
1426 ArgVT = VA.getLocVT();
1429 case CCValAssign::AExt: {
1430 bool Emitted = FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1433 Emitted = FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1436 Emitted = FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1439 assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
1440 ArgVT = VA.getLocVT();
1443 case CCValAssign::BCvt: {
1444 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BIT_CONVERT, Arg,
1445 /*TODO: Kill=*/false);
1446 assert(BC != 0 && "Failed to emit a bitcast!");
1448 ArgVT = VA.getLocVT();
1451 default: llvm_unreachable("Unknown arg promotion!");
1454 // Now copy/store arg to correct locations.
1455 if (VA.isRegLoc() && !VA.needsCustom()) {
1456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1459 RegArgs.push_back(VA.getLocReg());
1460 } else if (VA.needsCustom()) {
1461 // TODO: We need custom lowering for vector (v2f64) args.
1462 if (VA.getLocVT() != MVT::f64) return false;
1464 CCValAssign &NextVA = ArgLocs[++i];
1466 // TODO: Only handle register args for now.
1467 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1469 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1470 TII.get(ARM::VMOVRRD), VA.getLocReg())
1471 .addReg(NextVA.getLocReg(), RegState::Define)
1473 RegArgs.push_back(VA.getLocReg());
1474 RegArgs.push_back(NextVA.getLocReg());
1476 assert(VA.isMemLoc());
1477 // Need to store on the stack.
1478 unsigned Base = ARM::SP;
1479 int Offset = VA.getLocMemOffset();
1481 if (!ARMEmitStore(ArgVT, Arg, Base, Offset)) return false;
1487 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1488 const Instruction *I, CallingConv::ID CC,
1489 unsigned &NumBytes) {
1490 // Issue CALLSEQ_END
1491 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1493 TII.get(AdjStackUp))
1494 .addImm(NumBytes).addImm(0));
1496 // Now the return value.
1497 if (RetVT != MVT::isVoid) {
1498 SmallVector<CCValAssign, 16> RVLocs;
1499 CCState CCInfo(CC, false, TM, RVLocs, *Context);
1500 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1502 // Copy all of the result registers out of their specified physreg.
1503 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1504 // For this move we copy into two registers and then move into the
1505 // double fp reg we want.
1506 EVT DestVT = RVLocs[0].getValVT();
1507 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1508 unsigned ResultReg = createResultReg(DstRC);
1509 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1510 TII.get(ARM::VMOVDRR), ResultReg)
1511 .addReg(RVLocs[0].getLocReg())
1512 .addReg(RVLocs[1].getLocReg()));
1514 UsedRegs.push_back(RVLocs[0].getLocReg());
1515 UsedRegs.push_back(RVLocs[1].getLocReg());
1517 // Finally update the result.
1518 UpdateValueMap(I, ResultReg);
1520 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1521 EVT CopyVT = RVLocs[0].getValVT();
1522 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1524 unsigned ResultReg = createResultReg(DstRC);
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1526 ResultReg).addReg(RVLocs[0].getLocReg());
1527 UsedRegs.push_back(RVLocs[0].getLocReg());
1529 // Finally update the result.
1530 UpdateValueMap(I, ResultReg);
1537 bool ARMFastISel::SelectRet(const Instruction *I) {
1538 const ReturnInst *Ret = cast<ReturnInst>(I);
1539 const Function &F = *I->getParent()->getParent();
1541 if (!FuncInfo.CanLowerReturn)
1547 CallingConv::ID CC = F.getCallingConv();
1548 if (Ret->getNumOperands() > 0) {
1549 SmallVector<ISD::OutputArg, 4> Outs;
1550 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1553 // Analyze operands of the call, assigning locations to each operand.
1554 SmallVector<CCValAssign, 16> ValLocs;
1555 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
1556 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1558 const Value *RV = Ret->getOperand(0);
1559 unsigned Reg = getRegForValue(RV);
1563 // Only handle a single return value for now.
1564 if (ValLocs.size() != 1)
1567 CCValAssign &VA = ValLocs[0];
1569 // Don't bother handling odd stuff for now.
1570 if (VA.getLocInfo() != CCValAssign::Full)
1572 // Only handle register returns for now.
1575 // TODO: For now, don't try to handle cases where getLocInfo()
1576 // says Full but the types don't match.
1577 if (TLI.getValueType(RV->getType()) != VA.getValVT())
1581 unsigned SrcReg = Reg + VA.getValNo();
1582 unsigned DstReg = VA.getLocReg();
1583 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1584 // Avoid a cross-class copy. This is very unlikely.
1585 if (!SrcRC->contains(DstReg))
1587 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1588 DstReg).addReg(SrcReg);
1590 // Mark the register as live out of the function.
1591 MRI.addLiveOut(VA.getLocReg());
1594 unsigned RetOpc = isThumb ? ARM::tBX_RET : ARM::BX_RET;
1595 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1600 // A quick function that will emit a call for a named libcall in F with the
1601 // vector of passed arguments for the Instruction in I. We can assume that we
1602 // can emit a call for any libcall we can produce. This is an abridged version
1603 // of the full call infrastructure since we won't need to worry about things
1604 // like computed function pointers or strange arguments at call sites.
1605 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1607 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1608 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1610 // Handle *simple* calls for now.
1611 const Type *RetTy = I->getType();
1613 if (RetTy->isVoidTy())
1614 RetVT = MVT::isVoid;
1615 else if (!isTypeLegal(RetTy, RetVT))
1618 // For now we're using BLX etc on the assumption that we have v5t ops.
1619 if (!Subtarget->hasV5TOps()) return false;
1621 // Set up the argument vectors.
1622 SmallVector<Value*, 8> Args;
1623 SmallVector<unsigned, 8> ArgRegs;
1624 SmallVector<MVT, 8> ArgVTs;
1625 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1626 Args.reserve(I->getNumOperands());
1627 ArgRegs.reserve(I->getNumOperands());
1628 ArgVTs.reserve(I->getNumOperands());
1629 ArgFlags.reserve(I->getNumOperands());
1630 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1631 Value *Op = I->getOperand(i);
1632 unsigned Arg = getRegForValue(Op);
1633 if (Arg == 0) return false;
1635 const Type *ArgTy = Op->getType();
1637 if (!isTypeLegal(ArgTy, ArgVT)) return false;
1639 ISD::ArgFlagsTy Flags;
1640 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1641 Flags.setOrigAlign(OriginalAlignment);
1644 ArgRegs.push_back(Arg);
1645 ArgVTs.push_back(ArgVT);
1646 ArgFlags.push_back(Flags);
1649 // Handle the arguments now that we've gotten them.
1650 SmallVector<unsigned, 4> RegArgs;
1652 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1655 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1656 // TODO: Turn this into the table of arm call ops.
1657 MachineInstrBuilder MIB;
1660 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1662 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1663 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1664 .addExternalSymbol(TLI.getLibcallName(Call));
1666 // Add implicit physical register uses to the call.
1667 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1668 MIB.addReg(RegArgs[i]);
1670 // Finish off the call including any return values.
1671 SmallVector<unsigned, 4> UsedRegs;
1672 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1674 // Set all unused physreg defs as dead.
1675 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1680 bool ARMFastISel::SelectCall(const Instruction *I) {
1681 const CallInst *CI = cast<CallInst>(I);
1682 const Value *Callee = CI->getCalledValue();
1684 // Can't handle inline asm or worry about intrinsics yet.
1685 if (isa<InlineAsm>(Callee) || isa<IntrinsicInst>(CI)) return false;
1687 // Only handle global variable Callees that are direct calls.
1688 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1689 if (!GV || Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel()))
1692 // Check the calling convention.
1693 ImmutableCallSite CS(CI);
1694 CallingConv::ID CC = CS.getCallingConv();
1696 // TODO: Avoid some calling conventions?
1698 // Let SDISel handle vararg functions.
1699 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1700 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1701 if (FTy->isVarArg())
1704 // Handle *simple* calls for now.
1705 const Type *RetTy = I->getType();
1707 if (RetTy->isVoidTy())
1708 RetVT = MVT::isVoid;
1709 else if (!isTypeLegal(RetTy, RetVT))
1712 // For now we're using BLX etc on the assumption that we have v5t ops.
1714 if (!Subtarget->hasV5TOps()) return false;
1716 // Set up the argument vectors.
1717 SmallVector<Value*, 8> Args;
1718 SmallVector<unsigned, 8> ArgRegs;
1719 SmallVector<MVT, 8> ArgVTs;
1720 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1721 Args.reserve(CS.arg_size());
1722 ArgRegs.reserve(CS.arg_size());
1723 ArgVTs.reserve(CS.arg_size());
1724 ArgFlags.reserve(CS.arg_size());
1725 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1727 unsigned Arg = getRegForValue(*i);
1731 ISD::ArgFlagsTy Flags;
1732 unsigned AttrInd = i - CS.arg_begin() + 1;
1733 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1735 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1738 // FIXME: Only handle *easy* calls for now.
1739 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1740 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1741 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1742 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1745 const Type *ArgTy = (*i)->getType();
1747 if (!isTypeLegal(ArgTy, ArgVT))
1749 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1750 Flags.setOrigAlign(OriginalAlignment);
1753 ArgRegs.push_back(Arg);
1754 ArgVTs.push_back(ArgVT);
1755 ArgFlags.push_back(Flags);
1758 // Handle the arguments now that we've gotten them.
1759 SmallVector<unsigned, 4> RegArgs;
1761 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1764 // Issue the call, BLXr9 for darwin, BLX otherwise. This uses V5 ops.
1765 // TODO: Turn this into the table of arm call ops.
1766 MachineInstrBuilder MIB;
1769 CallOpc = Subtarget->isTargetDarwin() ? ARM::tBLXi_r9 : ARM::tBLXi;
1771 CallOpc = Subtarget->isTargetDarwin() ? ARM::BLr9 : ARM::BL;
1772 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1773 .addGlobalAddress(GV, 0, 0);
1775 // Add implicit physical register uses to the call.
1776 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1777 MIB.addReg(RegArgs[i]);
1779 // Finish off the call including any return values.
1780 SmallVector<unsigned, 4> UsedRegs;
1781 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
1783 // Set all unused physreg defs as dead.
1784 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1790 // TODO: SoftFP support.
1791 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
1793 switch (I->getOpcode()) {
1794 case Instruction::Load:
1795 return SelectLoad(I);
1796 case Instruction::Store:
1797 return SelectStore(I);
1798 case Instruction::Br:
1799 return SelectBranch(I);
1800 case Instruction::ICmp:
1801 case Instruction::FCmp:
1802 return SelectCmp(I);
1803 case Instruction::FPExt:
1804 return SelectFPExt(I);
1805 case Instruction::FPTrunc:
1806 return SelectFPTrunc(I);
1807 case Instruction::SIToFP:
1808 return SelectSIToFP(I);
1809 case Instruction::FPToSI:
1810 return SelectFPToSI(I);
1811 case Instruction::FAdd:
1812 return SelectBinaryOp(I, ISD::FADD);
1813 case Instruction::FSub:
1814 return SelectBinaryOp(I, ISD::FSUB);
1815 case Instruction::FMul:
1816 return SelectBinaryOp(I, ISD::FMUL);
1817 case Instruction::SDiv:
1818 return SelectSDiv(I);
1819 case Instruction::SRem:
1820 return SelectSRem(I);
1821 case Instruction::Call:
1822 return SelectCall(I);
1823 case Instruction::Select:
1824 return SelectSelect(I);
1825 case Instruction::Ret:
1826 return SelectRet(I);
1833 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
1834 // Completely untested on non-darwin.
1835 const TargetMachine &TM = funcInfo.MF->getTarget();
1837 // Darwin and thumb1 only for now.
1838 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
1839 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
1840 !DisableARMFastISel)
1841 return new ARMFastISel(funcInfo);