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 "MCTargetDesc/ARMAddressingModes.h"
24 #include "llvm/CallingConv.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/GlobalVariable.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/IntrinsicInst.h"
29 #include "llvm/Module.h"
30 #include "llvm/Operator.h"
31 #include "llvm/CodeGen/Analysis.h"
32 #include "llvm/CodeGen/FastISel.h"
33 #include "llvm/CodeGen/FunctionLoweringInfo.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineModuleInfo.h"
36 #include "llvm/CodeGen/MachineConstantPool.h"
37 #include "llvm/CodeGen/MachineFrameInfo.h"
38 #include "llvm/CodeGen/MachineMemOperand.h"
39 #include "llvm/CodeGen/MachineRegisterInfo.h"
40 #include "llvm/Support/CallSite.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/GetElementPtrTypeIterator.h"
44 #include "llvm/Target/TargetData.h"
45 #include "llvm/Target/TargetInstrInfo.h"
46 #include "llvm/Target/TargetLowering.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Target/TargetOptions.h"
52 DisableARMFastISel("disable-arm-fast-isel",
53 cl::desc("Turn off experimental ARM fast-isel support"),
54 cl::init(false), cl::Hidden);
56 extern cl::opt<bool> EnableARMLongCalls;
60 // All possible address modes, plus some.
61 typedef struct Address {
74 // Innocuous defaults for our address.
76 : BaseType(RegBase), Offset(0) {
81 class ARMFastISel : public FastISel {
83 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
84 /// make the right decision when generating code for different targets.
85 const ARMSubtarget *Subtarget;
86 const TargetMachine &TM;
87 const TargetInstrInfo &TII;
88 const TargetLowering &TLI;
91 // Convenience variables to avoid some queries.
96 explicit ARMFastISel(FunctionLoweringInfo &funcInfo)
98 TM(funcInfo.MF->getTarget()),
99 TII(*TM.getInstrInfo()),
100 TLI(*TM.getTargetLowering()) {
101 Subtarget = &TM.getSubtarget<ARMSubtarget>();
102 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
103 isThumb2 = AFI->isThumbFunction();
104 Context = &funcInfo.Fn->getContext();
107 // Code from FastISel.cpp.
108 virtual unsigned FastEmitInst_(unsigned MachineInstOpcode,
109 const TargetRegisterClass *RC);
110 virtual unsigned FastEmitInst_r(unsigned MachineInstOpcode,
111 const TargetRegisterClass *RC,
112 unsigned Op0, bool Op0IsKill);
113 virtual unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill);
117 virtual unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
118 const TargetRegisterClass *RC,
119 unsigned Op0, bool Op0IsKill,
120 unsigned Op1, bool Op1IsKill,
121 unsigned Op2, bool Op2IsKill);
122 virtual unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
126 virtual unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 const ConstantFP *FPImm);
130 virtual unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
131 const TargetRegisterClass *RC,
132 unsigned Op0, bool Op0IsKill,
133 unsigned Op1, bool Op1IsKill,
135 virtual unsigned FastEmitInst_i(unsigned MachineInstOpcode,
136 const TargetRegisterClass *RC,
138 virtual unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
139 const TargetRegisterClass *RC,
140 uint64_t Imm1, uint64_t Imm2);
142 virtual unsigned FastEmitInst_extractsubreg(MVT RetVT,
143 unsigned Op0, bool Op0IsKill,
146 // Backend specific FastISel code.
147 virtual bool TargetSelectInstruction(const Instruction *I);
148 virtual unsigned TargetMaterializeConstant(const Constant *C);
149 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
150 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
153 #include "ARMGenFastISel.inc"
155 // Instruction selection routines.
157 bool SelectLoad(const Instruction *I);
158 bool SelectStore(const Instruction *I);
159 bool SelectBranch(const Instruction *I);
160 bool SelectCmp(const Instruction *I);
161 bool SelectFPExt(const Instruction *I);
162 bool SelectFPTrunc(const Instruction *I);
163 bool SelectBinaryOp(const Instruction *I, unsigned ISDOpcode);
164 bool SelectSIToFP(const Instruction *I);
165 bool SelectFPToSI(const Instruction *I);
166 bool SelectSDiv(const Instruction *I);
167 bool SelectSRem(const Instruction *I);
168 bool SelectCall(const Instruction *I, const char *IntrMemName);
169 bool SelectIntrinsicCall(const IntrinsicInst &I);
170 bool SelectSelect(const Instruction *I);
171 bool SelectRet(const Instruction *I);
172 bool SelectTrunc(const Instruction *I);
173 bool SelectIntExt(const Instruction *I);
177 bool isTypeLegal(Type *Ty, MVT &VT);
178 bool isLoadTypeLegal(Type *Ty, MVT &VT);
179 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
181 bool ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr, bool isZExt,
184 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr);
185 bool ARMComputeAddress(const Value *Obj, Address &Addr);
186 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
187 bool ARMIsMemCpySmall(uint64_t Len);
188 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
189 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
190 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
191 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
192 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
193 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
194 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
195 unsigned ARMSelectCallOp(const GlobalValue *GV);
197 // Call handling routines.
199 bool FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
200 unsigned &ResultReg);
201 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool Return);
202 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
203 SmallVectorImpl<unsigned> &ArgRegs,
204 SmallVectorImpl<MVT> &ArgVTs,
205 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
206 SmallVectorImpl<unsigned> &RegArgs,
209 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
210 const Instruction *I, CallingConv::ID CC,
212 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
214 // OptionalDef handling routines.
216 bool isARMNEONPred(const MachineInstr *MI);
217 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
218 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
219 void AddLoadStoreOperands(EVT VT, Address &Addr,
220 const MachineInstrBuilder &MIB,
221 unsigned Flags, bool useAM3);
224 } // end anonymous namespace
226 #include "ARMGenCallingConv.inc"
228 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
229 // we don't care about implicit defs here, just places we'll need to add a
230 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
231 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
232 const MCInstrDesc &MCID = MI->getDesc();
233 if (!MCID.hasOptionalDef())
236 // Look to see if our OptionalDef is defining CPSR or CCR.
237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238 const MachineOperand &MO = MI->getOperand(i);
239 if (!MO.isReg() || !MO.isDef()) continue;
240 if (MO.getReg() == ARM::CPSR)
246 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
247 const MCInstrDesc &MCID = MI->getDesc();
249 // If we're a thumb2 or not NEON function we were handled via isPredicable.
250 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
251 AFI->isThumb2Function())
254 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
255 if (MCID.OpInfo[i].isPredicate())
261 // If the machine is predicable go ahead and add the predicate operands, if
262 // it needs default CC operands add those.
263 // TODO: If we want to support thumb1 then we'll need to deal with optional
264 // CPSR defs that need to be added before the remaining operands. See s_cc_out
265 // for descriptions why.
266 const MachineInstrBuilder &
267 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
268 MachineInstr *MI = &*MIB;
270 // Do we use a predicate? or...
271 // Are we NEON in ARM mode and have a predicate operand? If so, I know
272 // we're not predicable but add it anyways.
273 if (TII.isPredicable(MI) || isARMNEONPred(MI))
276 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
277 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
279 if (DefinesOptionalPredicate(MI, &CPSR)) {
288 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
289 const TargetRegisterClass* RC) {
290 unsigned ResultReg = createResultReg(RC);
291 const MCInstrDesc &II = TII.get(MachineInstOpcode);
293 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
297 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
298 const TargetRegisterClass *RC,
299 unsigned Op0, bool Op0IsKill) {
300 unsigned ResultReg = createResultReg(RC);
301 const MCInstrDesc &II = TII.get(MachineInstOpcode);
303 if (II.getNumDefs() >= 1)
304 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
305 .addReg(Op0, Op0IsKill * RegState::Kill));
307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
308 .addReg(Op0, Op0IsKill * RegState::Kill));
309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
310 TII.get(TargetOpcode::COPY), ResultReg)
311 .addReg(II.ImplicitDefs[0]));
316 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
317 const TargetRegisterClass *RC,
318 unsigned Op0, bool Op0IsKill,
319 unsigned Op1, bool Op1IsKill) {
320 unsigned ResultReg = createResultReg(RC);
321 const MCInstrDesc &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));
328 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
329 .addReg(Op0, Op0IsKill * RegState::Kill)
330 .addReg(Op1, Op1IsKill * RegState::Kill));
331 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
332 TII.get(TargetOpcode::COPY), ResultReg)
333 .addReg(II.ImplicitDefs[0]));
338 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
339 const TargetRegisterClass *RC,
340 unsigned Op0, bool Op0IsKill,
341 unsigned Op1, bool Op1IsKill,
342 unsigned Op2, bool Op2IsKill) {
343 unsigned ResultReg = createResultReg(RC);
344 const MCInstrDesc &II = TII.get(MachineInstOpcode);
346 if (II.getNumDefs() >= 1)
347 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
348 .addReg(Op0, Op0IsKill * RegState::Kill)
349 .addReg(Op1, Op1IsKill * RegState::Kill)
350 .addReg(Op2, Op2IsKill * RegState::Kill));
352 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
353 .addReg(Op0, Op0IsKill * RegState::Kill)
354 .addReg(Op1, Op1IsKill * RegState::Kill)
355 .addReg(Op2, Op2IsKill * RegState::Kill));
356 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
357 TII.get(TargetOpcode::COPY), ResultReg)
358 .addReg(II.ImplicitDefs[0]));
363 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
364 const TargetRegisterClass *RC,
365 unsigned Op0, bool Op0IsKill,
367 unsigned ResultReg = createResultReg(RC);
368 const MCInstrDesc &II = TII.get(MachineInstOpcode);
370 if (II.getNumDefs() >= 1)
371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
372 .addReg(Op0, Op0IsKill * RegState::Kill)
375 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
376 .addReg(Op0, Op0IsKill * RegState::Kill)
378 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
379 TII.get(TargetOpcode::COPY), ResultReg)
380 .addReg(II.ImplicitDefs[0]));
385 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
386 const TargetRegisterClass *RC,
387 unsigned Op0, bool Op0IsKill,
388 const ConstantFP *FPImm) {
389 unsigned ResultReg = createResultReg(RC);
390 const MCInstrDesc &II = TII.get(MachineInstOpcode);
392 if (II.getNumDefs() >= 1)
393 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
394 .addReg(Op0, Op0IsKill * RegState::Kill)
397 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
398 .addReg(Op0, Op0IsKill * RegState::Kill)
400 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
401 TII.get(TargetOpcode::COPY), ResultReg)
402 .addReg(II.ImplicitDefs[0]));
407 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
408 const TargetRegisterClass *RC,
409 unsigned Op0, bool Op0IsKill,
410 unsigned Op1, bool Op1IsKill,
412 unsigned ResultReg = createResultReg(RC);
413 const MCInstrDesc &II = TII.get(MachineInstOpcode);
415 if (II.getNumDefs() >= 1)
416 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
417 .addReg(Op0, Op0IsKill * RegState::Kill)
418 .addReg(Op1, Op1IsKill * RegState::Kill)
421 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
422 .addReg(Op0, Op0IsKill * RegState::Kill)
423 .addReg(Op1, Op1IsKill * RegState::Kill)
425 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
426 TII.get(TargetOpcode::COPY), ResultReg)
427 .addReg(II.ImplicitDefs[0]));
432 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
433 const TargetRegisterClass *RC,
435 unsigned ResultReg = createResultReg(RC);
436 const MCInstrDesc &II = TII.get(MachineInstOpcode);
438 if (II.getNumDefs() >= 1)
439 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
442 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
445 TII.get(TargetOpcode::COPY), ResultReg)
446 .addReg(II.ImplicitDefs[0]));
451 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
452 const TargetRegisterClass *RC,
453 uint64_t Imm1, uint64_t Imm2) {
454 unsigned ResultReg = createResultReg(RC);
455 const MCInstrDesc &II = TII.get(MachineInstOpcode);
457 if (II.getNumDefs() >= 1)
458 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
459 .addImm(Imm1).addImm(Imm2));
461 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
462 .addImm(Imm1).addImm(Imm2));
463 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
464 TII.get(TargetOpcode::COPY),
466 .addReg(II.ImplicitDefs[0]));
471 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
472 unsigned Op0, bool Op0IsKill,
474 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
475 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
476 "Cannot yet extract from physregs");
477 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
478 DL, TII.get(TargetOpcode::COPY), ResultReg)
479 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
483 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
484 // checks from the various callers.
485 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
486 if (VT == MVT::f64) return 0;
488 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
489 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
490 TII.get(ARM::VMOVRS), MoveReg)
495 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
496 if (VT == MVT::i64) return 0;
498 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
499 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
500 TII.get(ARM::VMOVSR), MoveReg)
505 // For double width floating point we need to materialize two constants
506 // (the high and the low) into integer registers then use a move to get
507 // the combined constant into an FP reg.
508 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
509 const APFloat Val = CFP->getValueAPF();
510 bool is64bit = VT == MVT::f64;
512 // This checks to see if we can use VFP3 instructions to materialize
513 // a constant, otherwise we have to go through the constant pool.
514 if (TLI.isFPImmLegal(Val, VT)) {
518 Imm = ARM_AM::getFP64Imm(Val);
521 Imm = ARM_AM::getFP32Imm(Val);
524 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
525 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
531 // Require VFP2 for loading fp constants.
532 if (!Subtarget->hasVFP2()) return false;
534 // MachineConstantPool wants an explicit alignment.
535 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
537 // TODO: Figure out if this is correct.
538 Align = TD.getTypeAllocSize(CFP->getType());
540 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
541 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
542 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
544 // The extra reg is for addrmode5.
545 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
547 .addConstantPoolIndex(Idx)
552 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
554 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
557 // If we can do this in a single instruction without a constant pool entry
559 const ConstantInt *CI = cast<ConstantInt>(C);
560 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
561 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
562 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
563 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
564 TII.get(Opc), ImmReg)
565 .addImm(CI->getZExtValue()));
569 // Use MVN to emit negative constants.
570 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
571 unsigned Imm = (unsigned)~(CI->getSExtValue());
572 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
573 (ARM_AM::getSOImmVal(Imm) != -1);
575 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
576 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
577 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
578 TII.get(Opc), ImmReg)
584 // Load from constant pool. For now 32-bit only.
588 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
590 // MachineConstantPool wants an explicit alignment.
591 unsigned Align = TD.getPrefTypeAlignment(C->getType());
593 // TODO: Figure out if this is correct.
594 Align = TD.getTypeAllocSize(C->getType());
596 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
599 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
600 TII.get(ARM::t2LDRpci), DestReg)
601 .addConstantPoolIndex(Idx));
603 // The extra immediate is for addrmode2.
604 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
605 TII.get(ARM::LDRcp), DestReg)
606 .addConstantPoolIndex(Idx)
612 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
613 // For now 32-bit only.
614 if (VT != MVT::i32) return 0;
616 Reloc::Model RelocM = TM.getRelocationModel();
618 // TODO: Need more magic for ARM PIC.
619 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
621 // MachineConstantPool wants an explicit alignment.
622 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
624 // TODO: Figure out if this is correct.
625 Align = TD.getTypeAllocSize(GV->getType());
629 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
630 unsigned Id = AFI->createPICLabelUId();
631 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
634 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
637 MachineInstrBuilder MIB;
638 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
640 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
641 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
642 .addConstantPoolIndex(Idx);
643 if (RelocM == Reloc::PIC_)
646 // The extra immediate is for addrmode2.
647 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
649 .addConstantPoolIndex(Idx)
652 AddOptionalDefs(MIB);
654 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
655 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
657 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
658 TII.get(ARM::t2LDRi12), NewDestReg)
662 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
666 DestReg = NewDestReg;
667 AddOptionalDefs(MIB);
673 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
674 EVT VT = TLI.getValueType(C->getType(), true);
676 // Only handle simple types.
677 if (!VT.isSimple()) return 0;
679 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
680 return ARMMaterializeFP(CFP, VT);
681 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
682 return ARMMaterializeGV(GV, VT);
683 else if (isa<ConstantInt>(C))
684 return ARMMaterializeInt(C, VT);
689 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
690 // Don't handle dynamic allocas.
691 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
694 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
696 DenseMap<const AllocaInst*, int>::iterator SI =
697 FuncInfo.StaticAllocaMap.find(AI);
699 // This will get lowered later into the correct offsets and registers
700 // via rewriteXFrameIndex.
701 if (SI != FuncInfo.StaticAllocaMap.end()) {
702 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
703 unsigned ResultReg = createResultReg(RC);
704 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
705 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
706 TII.get(Opc), ResultReg)
707 .addFrameIndex(SI->second)
715 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
716 EVT evt = TLI.getValueType(Ty, true);
718 // Only handle simple types.
719 if (evt == MVT::Other || !evt.isSimple()) return false;
720 VT = evt.getSimpleVT();
722 // Handle all legal types, i.e. a register that will directly hold this
724 return TLI.isTypeLegal(VT);
727 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
728 if (isTypeLegal(Ty, VT)) return true;
730 // If this is a type than can be sign or zero-extended to a basic operation
731 // go ahead and accept it now.
732 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
738 // Computes the address to get to an object.
739 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
740 // Some boilerplate from the X86 FastISel.
741 const User *U = NULL;
742 unsigned Opcode = Instruction::UserOp1;
743 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
744 // Don't walk into other basic blocks unless the object is an alloca from
745 // another block, otherwise it may not have a virtual register assigned.
746 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
747 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
748 Opcode = I->getOpcode();
751 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
752 Opcode = C->getOpcode();
756 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
757 if (Ty->getAddressSpace() > 255)
758 // Fast instruction selection doesn't support the special
765 case Instruction::BitCast: {
766 // Look through bitcasts.
767 return ARMComputeAddress(U->getOperand(0), Addr);
769 case Instruction::IntToPtr: {
770 // Look past no-op inttoptrs.
771 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
772 return ARMComputeAddress(U->getOperand(0), Addr);
775 case Instruction::PtrToInt: {
776 // Look past no-op ptrtoints.
777 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
778 return ARMComputeAddress(U->getOperand(0), Addr);
781 case Instruction::GetElementPtr: {
782 Address SavedAddr = Addr;
783 int TmpOffset = Addr.Offset;
785 // Iterate through the GEP folding the constants into offsets where
787 gep_type_iterator GTI = gep_type_begin(U);
788 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
789 i != e; ++i, ++GTI) {
790 const Value *Op = *i;
791 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
792 const StructLayout *SL = TD.getStructLayout(STy);
793 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
794 TmpOffset += SL->getElementOffset(Idx);
796 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
798 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
799 // Constant-offset addressing.
800 TmpOffset += CI->getSExtValue() * S;
803 if (isa<AddOperator>(Op) &&
804 (!isa<Instruction>(Op) ||
805 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
807 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
808 // An add (in the same block) with a constant operand. Fold the
811 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
812 TmpOffset += CI->getSExtValue() * S;
813 // Iterate on the other operand.
814 Op = cast<AddOperator>(Op)->getOperand(0);
818 goto unsupported_gep;
823 // Try to grab the base operand now.
824 Addr.Offset = TmpOffset;
825 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
827 // We failed, restore everything and try the other options.
833 case Instruction::Alloca: {
834 const AllocaInst *AI = cast<AllocaInst>(Obj);
835 DenseMap<const AllocaInst*, int>::iterator SI =
836 FuncInfo.StaticAllocaMap.find(AI);
837 if (SI != FuncInfo.StaticAllocaMap.end()) {
838 Addr.BaseType = Address::FrameIndexBase;
839 Addr.Base.FI = SI->second;
846 // Materialize the global variable's address into a reg which can
847 // then be used later to load the variable.
848 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
849 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
850 if (Tmp == 0) return false;
856 // Try to get this in a register if nothing else has worked.
857 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
858 return Addr.Base.Reg != 0;
861 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
863 assert(VT.isSimple() && "Non-simple types are invalid here!");
865 bool needsLowering = false;
866 switch (VT.getSimpleVT().SimpleTy) {
868 assert(false && "Unhandled load/store type!");
875 // Integer loads/stores handle 12-bit offsets.
876 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
877 // Handle negative offsets.
878 if (needsLowering && isThumb2)
879 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
882 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
883 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
888 // Floating point operands handle 8-bit offsets.
889 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
893 // If this is a stack pointer and the offset needs to be simplified then
894 // put the alloca address into a register, set the base type back to
895 // register and continue. This should almost never happen.
896 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
897 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
898 ARM::GPRRegisterClass;
899 unsigned ResultReg = createResultReg(RC);
900 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
901 AddOptionalDefs(BuildMI(*FuncInfo.MBB, *FuncInfo.InsertPt, DL,
902 TII.get(Opc), ResultReg)
903 .addFrameIndex(Addr.Base.FI)
905 Addr.Base.Reg = ResultReg;
906 Addr.BaseType = Address::RegBase;
909 // Since the offset is too large for the load/store instruction
910 // get the reg+offset into a register.
912 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
913 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
918 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
919 const MachineInstrBuilder &MIB,
920 unsigned Flags, bool useAM3) {
921 // addrmode5 output depends on the selection dag addressing dividing the
922 // offset by 4 that it then later multiplies. Do this here as well.
923 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
924 VT.getSimpleVT().SimpleTy == MVT::f64)
927 // Frame base works a bit differently. Handle it separately.
928 if (Addr.BaseType == Address::FrameIndexBase) {
929 int FI = Addr.Base.FI;
930 int Offset = Addr.Offset;
931 MachineMemOperand *MMO =
932 FuncInfo.MF->getMachineMemOperand(
933 MachinePointerInfo::getFixedStack(FI, Offset),
935 MFI.getObjectSize(FI),
936 MFI.getObjectAlignment(FI));
937 // Now add the rest of the operands.
938 MIB.addFrameIndex(FI);
940 // ARM halfword load/stores and signed byte loads need an additional operand.
942 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
946 MIB.addImm(Addr.Offset);
948 MIB.addMemOperand(MMO);
950 // Now add the rest of the operands.
951 MIB.addReg(Addr.Base.Reg);
953 // ARM halfword load/stores and signed byte loads need an additional operand.
955 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
959 MIB.addImm(Addr.Offset);
962 AddOptionalDefs(MIB);
965 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
966 bool isZExt = true, bool allocReg = true) {
967 assert(VT.isSimple() && "Non-simple types are invalid here!");
970 TargetRegisterClass *RC;
971 switch (VT.getSimpleVT().SimpleTy) {
972 // This is mostly going to be Neon/vector support.
973 default: return false;
977 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
978 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
980 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
989 RC = ARM::GPRRegisterClass;
993 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
994 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
996 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
998 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1001 RC = ARM::GPRRegisterClass;
1005 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1008 Opc = ARM::t2LDRi12;
1012 RC = ARM::GPRRegisterClass;
1016 RC = TLI.getRegClassFor(VT);
1020 RC = TLI.getRegClassFor(VT);
1023 // Simplify this down to something we can handle.
1024 ARMSimplifyAddress(Addr, VT, useAM3);
1026 // Create the base instruction, then add the operands.
1028 ResultReg = createResultReg(RC);
1029 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1030 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1031 TII.get(Opc), ResultReg);
1032 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1036 bool ARMFastISel::SelectLoad(const Instruction *I) {
1037 // Atomic loads need special handling.
1038 if (cast<LoadInst>(I)->isAtomic())
1041 // Verify we have a legal type before going any further.
1043 if (!isLoadTypeLegal(I->getType(), VT))
1046 // See if we can handle this address.
1048 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1051 if (!ARMEmitLoad(VT, ResultReg, Addr)) return false;
1052 UpdateValueMap(I, ResultReg);
1056 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr) {
1058 bool useAM3 = false;
1059 switch (VT.getSimpleVT().SimpleTy) {
1060 // This is mostly going to be Neon/vector support.
1061 default: return false;
1063 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
1064 ARM::GPRRegisterClass);
1065 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1066 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1068 .addReg(SrcReg).addImm(1));
1070 } // Fallthrough here.
1073 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1074 StrOpc = ARM::t2STRBi8;
1076 StrOpc = ARM::t2STRBi12;
1078 StrOpc = ARM::STRBi12;
1083 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1084 StrOpc = ARM::t2STRHi8;
1086 StrOpc = ARM::t2STRHi12;
1094 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1095 StrOpc = ARM::t2STRi8;
1097 StrOpc = ARM::t2STRi12;
1099 StrOpc = ARM::STRi12;
1103 if (!Subtarget->hasVFP2()) return false;
1104 StrOpc = ARM::VSTRS;
1107 if (!Subtarget->hasVFP2()) return false;
1108 StrOpc = ARM::VSTRD;
1111 // Simplify this down to something we can handle.
1112 ARMSimplifyAddress(Addr, VT, useAM3);
1114 // Create the base instruction, then add the operands.
1115 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1117 .addReg(SrcReg, getKillRegState(true));
1118 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1122 bool ARMFastISel::SelectStore(const Instruction *I) {
1123 Value *Op0 = I->getOperand(0);
1124 unsigned SrcReg = 0;
1126 // Atomic stores need special handling.
1127 if (cast<StoreInst>(I)->isAtomic())
1130 // Verify we have a legal type before going any further.
1132 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1135 // Get the value to be stored into a register.
1136 SrcReg = getRegForValue(Op0);
1137 if (SrcReg == 0) return false;
1139 // See if we can handle this address.
1141 if (!ARMComputeAddress(I->getOperand(1), Addr))
1144 if (!ARMEmitStore(VT, SrcReg, Addr)) return false;
1148 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1150 // Needs two compares...
1151 case CmpInst::FCMP_ONE:
1152 case CmpInst::FCMP_UEQ:
1154 // AL is our "false" for now. The other two need more compares.
1156 case CmpInst::ICMP_EQ:
1157 case CmpInst::FCMP_OEQ:
1159 case CmpInst::ICMP_SGT:
1160 case CmpInst::FCMP_OGT:
1162 case CmpInst::ICMP_SGE:
1163 case CmpInst::FCMP_OGE:
1165 case CmpInst::ICMP_UGT:
1166 case CmpInst::FCMP_UGT:
1168 case CmpInst::FCMP_OLT:
1170 case CmpInst::ICMP_ULE:
1171 case CmpInst::FCMP_OLE:
1173 case CmpInst::FCMP_ORD:
1175 case CmpInst::FCMP_UNO:
1177 case CmpInst::FCMP_UGE:
1179 case CmpInst::ICMP_SLT:
1180 case CmpInst::FCMP_ULT:
1182 case CmpInst::ICMP_SLE:
1183 case CmpInst::FCMP_ULE:
1185 case CmpInst::FCMP_UNE:
1186 case CmpInst::ICMP_NE:
1188 case CmpInst::ICMP_UGE:
1190 case CmpInst::ICMP_ULT:
1195 bool ARMFastISel::SelectBranch(const Instruction *I) {
1196 const BranchInst *BI = cast<BranchInst>(I);
1197 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1198 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1200 // Simple branch support.
1202 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1204 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1205 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1207 // Get the compare predicate.
1208 // Try to take advantage of fallthrough opportunities.
1209 CmpInst::Predicate Predicate = CI->getPredicate();
1210 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1211 std::swap(TBB, FBB);
1212 Predicate = CmpInst::getInversePredicate(Predicate);
1215 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1217 // We may not handle every CC for now.
1218 if (ARMPred == ARMCC::AL) return false;
1220 // Emit the compare.
1221 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1224 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1226 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1227 FastEmitBranch(FBB, DL);
1228 FuncInfo.MBB->addSuccessor(TBB);
1231 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1233 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1234 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1235 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1236 unsigned OpReg = getRegForValue(TI->getOperand(0));
1237 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1239 .addReg(OpReg).addImm(1));
1241 unsigned CCMode = ARMCC::NE;
1242 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1243 std::swap(TBB, FBB);
1247 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1249 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1251 FastEmitBranch(FBB, DL);
1252 FuncInfo.MBB->addSuccessor(TBB);
1255 } else if (const ConstantInt *CI =
1256 dyn_cast<ConstantInt>(BI->getCondition())) {
1257 uint64_t Imm = CI->getZExtValue();
1258 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1259 FastEmitBranch(Target, DL);
1263 unsigned CmpReg = getRegForValue(BI->getCondition());
1264 if (CmpReg == 0) return false;
1266 // We've been divorced from our compare! Our block was split, and
1267 // now our compare lives in a predecessor block. We musn't
1268 // re-compare here, as the children of the compare aren't guaranteed
1269 // live across the block boundary (we *could* check for this).
1270 // Regardless, the compare has been done in the predecessor block,
1271 // and it left a value for us in a virtual register. Ergo, we test
1272 // the one-bit value left in the virtual register.
1273 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1274 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1275 .addReg(CmpReg).addImm(1));
1277 unsigned CCMode = ARMCC::NE;
1278 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1279 std::swap(TBB, FBB);
1283 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1285 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1286 FastEmitBranch(FBB, DL);
1287 FuncInfo.MBB->addSuccessor(TBB);
1291 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1293 Type *Ty = Src1Value->getType();
1294 EVT SrcVT = TLI.getValueType(Ty, true);
1295 if (!SrcVT.isSimple()) return false;
1297 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1298 if (isFloat && !Subtarget->hasVFP2())
1301 // Check to see if the 2nd operand is a constant that we can encode directly
1304 bool UseImm = false;
1305 bool isNegativeImm = false;
1306 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1307 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1309 const APInt &CIVal = ConstInt->getValue();
1310 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1312 isNegativeImm = true;
1315 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1316 (ARM_AM::getSOImmVal(Imm) != -1);
1318 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1319 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1320 if (ConstFP->isZero() && !ConstFP->isNegative())
1326 bool needsExt = false;
1327 switch (SrcVT.getSimpleVT().SimpleTy) {
1328 default: return false;
1329 // TODO: Verify compares.
1332 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1336 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1342 // Intentional fall-through.
1346 CmpOpc = ARM::t2CMPrr;
1348 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1351 CmpOpc = ARM::CMPrr;
1353 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1358 unsigned SrcReg1 = getRegForValue(Src1Value);
1359 if (SrcReg1 == 0) return false;
1363 SrcReg2 = getRegForValue(Src2Value);
1364 if (SrcReg2 == 0) return false;
1367 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1370 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1371 if (ResultReg == 0) return false;
1372 SrcReg1 = ResultReg;
1374 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1375 if (ResultReg == 0) return false;
1376 SrcReg2 = ResultReg;
1381 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1383 .addReg(SrcReg1).addReg(SrcReg2));
1385 MachineInstrBuilder MIB;
1386 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1389 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1392 AddOptionalDefs(MIB);
1395 // For floating point we need to move the result to a comparison register
1396 // that we can then use for branches.
1397 if (Ty->isFloatTy() || Ty->isDoubleTy())
1398 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1399 TII.get(ARM::FMSTAT)));
1403 bool ARMFastISel::SelectCmp(const Instruction *I) {
1404 const CmpInst *CI = cast<CmpInst>(I);
1405 Type *Ty = CI->getOperand(0)->getType();
1407 // Get the compare predicate.
1408 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1410 // We may not handle every CC for now.
1411 if (ARMPred == ARMCC::AL) return false;
1413 // Emit the compare.
1414 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1417 // Now set a register based on the comparison. Explicitly set the predicates
1419 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1420 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
1421 : ARM::GPRRegisterClass;
1422 unsigned DestReg = createResultReg(RC);
1423 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1424 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1425 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1426 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
1427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1428 .addReg(ZeroReg).addImm(1)
1429 .addImm(ARMPred).addReg(CondReg);
1431 UpdateValueMap(I, DestReg);
1435 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1436 // Make sure we have VFP and that we're extending float to double.
1437 if (!Subtarget->hasVFP2()) return false;
1439 Value *V = I->getOperand(0);
1440 if (!I->getType()->isDoubleTy() ||
1441 !V->getType()->isFloatTy()) return false;
1443 unsigned Op = getRegForValue(V);
1444 if (Op == 0) return false;
1446 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1447 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1448 TII.get(ARM::VCVTDS), Result)
1450 UpdateValueMap(I, Result);
1454 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1455 // Make sure we have VFP and that we're truncating double to float.
1456 if (!Subtarget->hasVFP2()) return false;
1458 Value *V = I->getOperand(0);
1459 if (!(I->getType()->isFloatTy() &&
1460 V->getType()->isDoubleTy())) return false;
1462 unsigned Op = getRegForValue(V);
1463 if (Op == 0) return false;
1465 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1466 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1467 TII.get(ARM::VCVTSD), Result)
1469 UpdateValueMap(I, Result);
1473 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1474 // Make sure we have VFP.
1475 if (!Subtarget->hasVFP2()) return false;
1478 Type *Ty = I->getType();
1479 if (!isTypeLegal(Ty, DstVT))
1482 Value *Src = I->getOperand(0);
1483 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1484 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1487 unsigned SrcReg = getRegForValue(Src);
1488 if (SrcReg == 0) return false;
1490 // Handle sign-extension.
1491 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1492 EVT DestVT = MVT::i32;
1493 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1494 if (ResultReg == 0) return false;
1498 // The conversion routine works on fp-reg to fp-reg and the operand above
1499 // was an integer, move it to the fp registers if possible.
1500 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1501 if (FP == 0) return false;
1504 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1505 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1508 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1509 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1512 UpdateValueMap(I, ResultReg);
1516 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1517 // Make sure we have VFP.
1518 if (!Subtarget->hasVFP2()) return false;
1521 Type *RetTy = I->getType();
1522 if (!isTypeLegal(RetTy, DstVT))
1525 unsigned Op = getRegForValue(I->getOperand(0));
1526 if (Op == 0) return false;
1529 Type *OpTy = I->getOperand(0)->getType();
1530 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1531 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1534 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1535 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1536 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1540 // This result needs to be in an integer register, but the conversion only
1541 // takes place in fp-regs.
1542 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1543 if (IntReg == 0) return false;
1545 UpdateValueMap(I, IntReg);
1549 bool ARMFastISel::SelectSelect(const Instruction *I) {
1551 if (!isTypeLegal(I->getType(), VT))
1554 // Things need to be register sized for register moves.
1555 if (VT != MVT::i32) return false;
1556 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1558 unsigned CondReg = getRegForValue(I->getOperand(0));
1559 if (CondReg == 0) return false;
1560 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1561 if (Op1Reg == 0) return false;
1563 // Check to see if we can use an immediate in the conditional move.
1565 bool UseImm = false;
1566 bool isNegativeImm = false;
1567 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1568 assert (VT == MVT::i32 && "Expecting an i32.");
1569 Imm = (int)ConstInt->getValue().getZExtValue();
1571 isNegativeImm = true;
1574 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1575 (ARM_AM::getSOImmVal(Imm) != -1);
1580 Op2Reg = getRegForValue(I->getOperand(2));
1581 if (Op2Reg == 0) return false;
1584 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1586 .addReg(CondReg).addImm(0));
1590 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1592 if (!isNegativeImm) {
1593 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1595 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1598 unsigned ResultReg = createResultReg(RC);
1600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1601 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1603 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1604 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1605 UpdateValueMap(I, ResultReg);
1609 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1611 Type *Ty = I->getType();
1612 if (!isTypeLegal(Ty, VT))
1615 // If we have integer div support we should have selected this automagically.
1616 // In case we have a real miss go ahead and return false and we'll pick
1618 if (Subtarget->hasDivide()) return false;
1620 // Otherwise emit a libcall.
1621 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1623 LC = RTLIB::SDIV_I8;
1624 else if (VT == MVT::i16)
1625 LC = RTLIB::SDIV_I16;
1626 else if (VT == MVT::i32)
1627 LC = RTLIB::SDIV_I32;
1628 else if (VT == MVT::i64)
1629 LC = RTLIB::SDIV_I64;
1630 else if (VT == MVT::i128)
1631 LC = RTLIB::SDIV_I128;
1632 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1634 return ARMEmitLibcall(I, LC);
1637 bool ARMFastISel::SelectSRem(const Instruction *I) {
1639 Type *Ty = I->getType();
1640 if (!isTypeLegal(Ty, VT))
1643 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1645 LC = RTLIB::SREM_I8;
1646 else if (VT == MVT::i16)
1647 LC = RTLIB::SREM_I16;
1648 else if (VT == MVT::i32)
1649 LC = RTLIB::SREM_I32;
1650 else if (VT == MVT::i64)
1651 LC = RTLIB::SREM_I64;
1652 else if (VT == MVT::i128)
1653 LC = RTLIB::SREM_I128;
1654 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1656 return ARMEmitLibcall(I, LC);
1659 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1660 EVT VT = TLI.getValueType(I->getType(), true);
1662 // We can get here in the case when we want to use NEON for our fp
1663 // operations, but can't figure out how to. Just use the vfp instructions
1665 // FIXME: It'd be nice to use NEON instructions.
1666 Type *Ty = I->getType();
1667 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1668 if (isFloat && !Subtarget->hasVFP2())
1671 unsigned Op1 = getRegForValue(I->getOperand(0));
1672 if (Op1 == 0) return false;
1674 unsigned Op2 = getRegForValue(I->getOperand(1));
1675 if (Op2 == 0) return false;
1678 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1679 switch (ISDOpcode) {
1680 default: return false;
1682 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1685 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1688 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1691 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1692 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1693 TII.get(Opc), ResultReg)
1694 .addReg(Op1).addReg(Op2));
1695 UpdateValueMap(I, ResultReg);
1699 // Call Handling Code
1701 bool ARMFastISel::FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src,
1702 EVT SrcVT, unsigned &ResultReg) {
1703 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
1704 Src, /*TODO: Kill=*/false);
1713 // This is largely taken directly from CCAssignFnForNode - we don't support
1714 // varargs in FastISel so that part has been removed.
1715 // TODO: We may not support all of this.
1716 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1719 llvm_unreachable("Unsupported calling convention");
1720 case CallingConv::Fast:
1721 // Ignore fastcc. Silence compiler warnings.
1722 (void)RetFastCC_ARM_APCS;
1723 (void)FastCC_ARM_APCS;
1725 case CallingConv::C:
1726 // Use target triple & subtarget features to do actual dispatch.
1727 if (Subtarget->isAAPCS_ABI()) {
1728 if (Subtarget->hasVFP2() &&
1729 FloatABIType == FloatABI::Hard)
1730 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1732 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1734 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1735 case CallingConv::ARM_AAPCS_VFP:
1736 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1737 case CallingConv::ARM_AAPCS:
1738 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1739 case CallingConv::ARM_APCS:
1740 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1744 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1745 SmallVectorImpl<unsigned> &ArgRegs,
1746 SmallVectorImpl<MVT> &ArgVTs,
1747 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1748 SmallVectorImpl<unsigned> &RegArgs,
1750 unsigned &NumBytes) {
1751 SmallVector<CCValAssign, 16> ArgLocs;
1752 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1753 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1755 // Get a count of how many bytes are to be pushed on the stack.
1756 NumBytes = CCInfo.getNextStackOffset();
1758 // Issue CALLSEQ_START
1759 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1760 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1761 TII.get(AdjStackDown))
1764 // Process the args.
1765 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1766 CCValAssign &VA = ArgLocs[i];
1767 unsigned Arg = ArgRegs[VA.getValNo()];
1768 MVT ArgVT = ArgVTs[VA.getValNo()];
1770 // We don't handle NEON/vector parameters yet.
1771 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1774 // Handle arg promotion, etc.
1775 switch (VA.getLocInfo()) {
1776 case CCValAssign::Full: break;
1777 case CCValAssign::SExt: {
1778 EVT DestVT = VA.getLocVT();
1779 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1781 assert (ResultReg != 0 && "Failed to emit a sext");
1785 case CCValAssign::AExt:
1786 // Intentional fall-through. Handle AExt and ZExt.
1787 case CCValAssign::ZExt: {
1788 EVT DestVT = VA.getLocVT();
1789 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1791 assert (ResultReg != 0 && "Failed to emit a sext");
1795 case CCValAssign::BCvt: {
1796 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1797 /*TODO: Kill=*/false);
1798 assert(BC != 0 && "Failed to emit a bitcast!");
1800 ArgVT = VA.getLocVT();
1803 default: llvm_unreachable("Unknown arg promotion!");
1806 // Now copy/store arg to correct locations.
1807 if (VA.isRegLoc() && !VA.needsCustom()) {
1808 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1811 RegArgs.push_back(VA.getLocReg());
1812 } else if (VA.needsCustom()) {
1813 // TODO: We need custom lowering for vector (v2f64) args.
1814 if (VA.getLocVT() != MVT::f64) return false;
1816 CCValAssign &NextVA = ArgLocs[++i];
1818 // TODO: Only handle register args for now.
1819 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1821 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1822 TII.get(ARM::VMOVRRD), VA.getLocReg())
1823 .addReg(NextVA.getLocReg(), RegState::Define)
1825 RegArgs.push_back(VA.getLocReg());
1826 RegArgs.push_back(NextVA.getLocReg());
1828 assert(VA.isMemLoc());
1829 // Need to store on the stack.
1831 Addr.BaseType = Address::RegBase;
1832 Addr.Base.Reg = ARM::SP;
1833 Addr.Offset = VA.getLocMemOffset();
1835 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
1841 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1842 const Instruction *I, CallingConv::ID CC,
1843 unsigned &NumBytes) {
1844 // Issue CALLSEQ_END
1845 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1846 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1847 TII.get(AdjStackUp))
1848 .addImm(NumBytes).addImm(0));
1850 // Now the return value.
1851 if (RetVT != MVT::isVoid) {
1852 SmallVector<CCValAssign, 16> RVLocs;
1853 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1854 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1856 // Copy all of the result registers out of their specified physreg.
1857 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1858 // For this move we copy into two registers and then move into the
1859 // double fp reg we want.
1860 EVT DestVT = RVLocs[0].getValVT();
1861 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1862 unsigned ResultReg = createResultReg(DstRC);
1863 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1864 TII.get(ARM::VMOVDRR), ResultReg)
1865 .addReg(RVLocs[0].getLocReg())
1866 .addReg(RVLocs[1].getLocReg()));
1868 UsedRegs.push_back(RVLocs[0].getLocReg());
1869 UsedRegs.push_back(RVLocs[1].getLocReg());
1871 // Finally update the result.
1872 UpdateValueMap(I, ResultReg);
1874 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1875 EVT CopyVT = RVLocs[0].getValVT();
1877 // Special handling for extended integers.
1878 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1881 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1883 unsigned ResultReg = createResultReg(DstRC);
1884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1885 ResultReg).addReg(RVLocs[0].getLocReg());
1886 UsedRegs.push_back(RVLocs[0].getLocReg());
1888 // Finally update the result.
1889 UpdateValueMap(I, ResultReg);
1896 bool ARMFastISel::SelectRet(const Instruction *I) {
1897 const ReturnInst *Ret = cast<ReturnInst>(I);
1898 const Function &F = *I->getParent()->getParent();
1900 if (!FuncInfo.CanLowerReturn)
1906 CallingConv::ID CC = F.getCallingConv();
1907 if (Ret->getNumOperands() > 0) {
1908 SmallVector<ISD::OutputArg, 4> Outs;
1909 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1912 // Analyze operands of the call, assigning locations to each operand.
1913 SmallVector<CCValAssign, 16> ValLocs;
1914 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
1915 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1917 const Value *RV = Ret->getOperand(0);
1918 unsigned Reg = getRegForValue(RV);
1922 // Only handle a single return value for now.
1923 if (ValLocs.size() != 1)
1926 CCValAssign &VA = ValLocs[0];
1928 // Don't bother handling odd stuff for now.
1929 if (VA.getLocInfo() != CCValAssign::Full)
1931 // Only handle register returns for now.
1935 unsigned SrcReg = Reg + VA.getValNo();
1936 EVT RVVT = TLI.getValueType(RV->getType());
1937 EVT DestVT = VA.getValVT();
1938 // Special handling for extended integers.
1939 if (RVVT != DestVT) {
1940 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1943 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1946 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1948 bool isZExt = Outs[0].Flags.isZExt();
1949 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1950 if (ResultReg == 0) return false;
1955 unsigned DstReg = VA.getLocReg();
1956 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1957 // Avoid a cross-class copy. This is very unlikely.
1958 if (!SrcRC->contains(DstReg))
1960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1961 DstReg).addReg(SrcReg);
1963 // Mark the register as live out of the function.
1964 MRI.addLiveOut(VA.getLocReg());
1967 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
1968 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1973 unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
1975 // Darwin needs the r9 versions of the opcodes.
1976 bool isDarwin = Subtarget->isTargetDarwin();
1978 return isDarwin ? ARM::tBLr9 : ARM::tBL;
1980 return isDarwin ? ARM::BLr9 : ARM::BL;
1984 // A quick function that will emit a call for a named libcall in F with the
1985 // vector of passed arguments for the Instruction in I. We can assume that we
1986 // can emit a call for any libcall we can produce. This is an abridged version
1987 // of the full call infrastructure since we won't need to worry about things
1988 // like computed function pointers or strange arguments at call sites.
1989 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
1991 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
1992 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
1994 // Handle *simple* calls for now.
1995 Type *RetTy = I->getType();
1997 if (RetTy->isVoidTy())
1998 RetVT = MVT::isVoid;
1999 else if (!isTypeLegal(RetTy, RetVT))
2002 // TODO: For now if we have long calls specified we don't handle the call.
2003 if (EnableARMLongCalls) return false;
2005 // Set up the argument vectors.
2006 SmallVector<Value*, 8> Args;
2007 SmallVector<unsigned, 8> ArgRegs;
2008 SmallVector<MVT, 8> ArgVTs;
2009 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2010 Args.reserve(I->getNumOperands());
2011 ArgRegs.reserve(I->getNumOperands());
2012 ArgVTs.reserve(I->getNumOperands());
2013 ArgFlags.reserve(I->getNumOperands());
2014 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2015 Value *Op = I->getOperand(i);
2016 unsigned Arg = getRegForValue(Op);
2017 if (Arg == 0) return false;
2019 Type *ArgTy = Op->getType();
2021 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2023 ISD::ArgFlagsTy Flags;
2024 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2025 Flags.setOrigAlign(OriginalAlignment);
2028 ArgRegs.push_back(Arg);
2029 ArgVTs.push_back(ArgVT);
2030 ArgFlags.push_back(Flags);
2033 // Handle the arguments now that we've gotten them.
2034 SmallVector<unsigned, 4> RegArgs;
2036 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2039 // Issue the call, BLr9 for darwin, BL otherwise.
2040 // TODO: Turn this into the table of arm call ops.
2041 MachineInstrBuilder MIB;
2042 unsigned CallOpc = ARMSelectCallOp(NULL);
2044 // Explicitly adding the predicate here.
2045 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2047 .addExternalSymbol(TLI.getLibcallName(Call));
2049 // Explicitly adding the predicate here.
2050 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2052 .addExternalSymbol(TLI.getLibcallName(Call)));
2054 // Add implicit physical register uses to the call.
2055 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2056 MIB.addReg(RegArgs[i]);
2058 // Finish off the call including any return values.
2059 SmallVector<unsigned, 4> UsedRegs;
2060 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2062 // Set all unused physreg defs as dead.
2063 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2068 bool ARMFastISel::SelectCall(const Instruction *I,
2069 const char *IntrMemName = 0) {
2070 const CallInst *CI = cast<CallInst>(I);
2071 const Value *Callee = CI->getCalledValue();
2073 // Can't handle inline asm.
2074 if (isa<InlineAsm>(Callee)) return false;
2076 // Only handle global variable Callees.
2077 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2081 // Check the calling convention.
2082 ImmutableCallSite CS(CI);
2083 CallingConv::ID CC = CS.getCallingConv();
2085 // TODO: Avoid some calling conventions?
2087 // Let SDISel handle vararg functions.
2088 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2089 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2090 if (FTy->isVarArg())
2093 // Handle *simple* calls for now.
2094 Type *RetTy = I->getType();
2096 if (RetTy->isVoidTy())
2097 RetVT = MVT::isVoid;
2098 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2099 RetVT != MVT::i8 && RetVT != MVT::i1)
2102 // TODO: For now if we have long calls specified we don't handle the call.
2103 if (EnableARMLongCalls) return false;
2105 // Set up the argument vectors.
2106 SmallVector<Value*, 8> Args;
2107 SmallVector<unsigned, 8> ArgRegs;
2108 SmallVector<MVT, 8> ArgVTs;
2109 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2110 Args.reserve(CS.arg_size());
2111 ArgRegs.reserve(CS.arg_size());
2112 ArgVTs.reserve(CS.arg_size());
2113 ArgFlags.reserve(CS.arg_size());
2114 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2116 // If we're lowering a memory intrinsic instead of a regular call, skip the
2117 // last two arguments, which shouldn't be passed to the underlying function.
2118 if (IntrMemName && e-i <= 2)
2121 unsigned Arg = getRegForValue(*i);
2124 ISD::ArgFlagsTy Flags;
2125 unsigned AttrInd = i - CS.arg_begin() + 1;
2126 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2128 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2131 // FIXME: Only handle *easy* calls for now.
2132 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2133 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2134 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2135 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2138 Type *ArgTy = (*i)->getType();
2140 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2143 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2144 Flags.setOrigAlign(OriginalAlignment);
2147 ArgRegs.push_back(Arg);
2148 ArgVTs.push_back(ArgVT);
2149 ArgFlags.push_back(Flags);
2152 // Handle the arguments now that we've gotten them.
2153 SmallVector<unsigned, 4> RegArgs;
2155 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2158 // Issue the call, BLr9 for darwin, BL otherwise.
2159 // TODO: Turn this into the table of arm call ops.
2160 MachineInstrBuilder MIB;
2161 unsigned CallOpc = ARMSelectCallOp(GV);
2162 // Explicitly adding the predicate here.
2164 // Explicitly adding the predicate here.
2165 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2168 MIB.addGlobalAddress(GV, 0, 0);
2170 MIB.addExternalSymbol(IntrMemName, 0);
2173 // Explicitly adding the predicate here.
2174 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2176 .addGlobalAddress(GV, 0, 0));
2178 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2180 .addExternalSymbol(IntrMemName, 0));
2183 // Add implicit physical register uses to the call.
2184 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2185 MIB.addReg(RegArgs[i]);
2187 // Finish off the call including any return values.
2188 SmallVector<unsigned, 4> UsedRegs;
2189 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2191 // Set all unused physreg defs as dead.
2192 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2197 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2201 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
2202 // Make sure we don't bloat code by inlining very large memcpy's.
2203 if (!ARMIsMemCpySmall(Len))
2206 // We don't care about alignment here since we just emit integer accesses.
2220 RV = ARMEmitLoad(VT, ResultReg, Src);
2221 assert (RV = true && "Should be able to handle this load.");
2222 RV = ARMEmitStore(VT, ResultReg, Dest);
2223 assert (RV = true && "Should be able to handle this store.");
2225 unsigned Size = VT.getSizeInBits()/8;
2227 Dest.Offset += Size;
2234 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2235 // FIXME: Handle more intrinsics.
2236 switch (I.getIntrinsicID()) {
2237 default: return false;
2238 case Intrinsic::memcpy:
2239 case Intrinsic::memmove: {
2240 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2241 // Don't handle volatile.
2242 if (MTI.isVolatile())
2245 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2246 // we would emit dead code because we don't currently handle memmoves.
2247 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2248 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2249 // Small memcpy's are common enough that we want to do them without a call
2251 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2252 if (ARMIsMemCpySmall(Len)) {
2254 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2255 !ARMComputeAddress(MTI.getRawSource(), Src))
2257 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2262 if (!MTI.getLength()->getType()->isIntegerTy(32))
2265 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2268 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2269 return SelectCall(&I, IntrMemName);
2271 case Intrinsic::memset: {
2272 const MemSetInst &MSI = cast<MemSetInst>(I);
2273 // Don't handle volatile.
2274 if (MSI.isVolatile())
2277 if (!MSI.getLength()->getType()->isIntegerTy(32))
2280 if (MSI.getDestAddressSpace() > 255)
2283 return SelectCall(&I, "memset");
2289 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2290 // The high bits for a type smaller than the register size are assumed to be
2292 Value *Op = I->getOperand(0);
2295 SrcVT = TLI.getValueType(Op->getType(), true);
2296 DestVT = TLI.getValueType(I->getType(), true);
2298 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2300 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2303 unsigned SrcReg = getRegForValue(Op);
2304 if (!SrcReg) return false;
2306 // Because the high bits are undefined, a truncate doesn't generate
2308 UpdateValueMap(I, SrcReg);
2312 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2314 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2318 bool isBoolZext = false;
2319 if (!SrcVT.isSimple()) return 0;
2320 switch (SrcVT.getSimpleVT().SimpleTy) {
2323 if (!Subtarget->hasV6Ops()) return 0;
2325 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2327 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2330 if (!Subtarget->hasV6Ops()) return 0;
2332 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2334 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2338 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2345 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2346 MachineInstrBuilder MIB;
2347 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2353 AddOptionalDefs(MIB);
2357 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2358 // On ARM, in general, integer casts don't involve legal types; this code
2359 // handles promotable integers.
2360 Type *DestTy = I->getType();
2361 Value *Src = I->getOperand(0);
2362 Type *SrcTy = Src->getType();
2365 SrcVT = TLI.getValueType(SrcTy, true);
2366 DestVT = TLI.getValueType(DestTy, true);
2368 bool isZExt = isa<ZExtInst>(I);
2369 unsigned SrcReg = getRegForValue(Src);
2370 if (!SrcReg) return false;
2372 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2373 if (ResultReg == 0) return false;
2374 UpdateValueMap(I, ResultReg);
2378 // TODO: SoftFP support.
2379 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2381 switch (I->getOpcode()) {
2382 case Instruction::Load:
2383 return SelectLoad(I);
2384 case Instruction::Store:
2385 return SelectStore(I);
2386 case Instruction::Br:
2387 return SelectBranch(I);
2388 case Instruction::ICmp:
2389 case Instruction::FCmp:
2390 return SelectCmp(I);
2391 case Instruction::FPExt:
2392 return SelectFPExt(I);
2393 case Instruction::FPTrunc:
2394 return SelectFPTrunc(I);
2395 case Instruction::SIToFP:
2396 return SelectSIToFP(I);
2397 case Instruction::FPToSI:
2398 return SelectFPToSI(I);
2399 case Instruction::FAdd:
2400 return SelectBinaryOp(I, ISD::FADD);
2401 case Instruction::FSub:
2402 return SelectBinaryOp(I, ISD::FSUB);
2403 case Instruction::FMul:
2404 return SelectBinaryOp(I, ISD::FMUL);
2405 case Instruction::SDiv:
2406 return SelectSDiv(I);
2407 case Instruction::SRem:
2408 return SelectSRem(I);
2409 case Instruction::Call:
2410 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2411 return SelectIntrinsicCall(*II);
2412 return SelectCall(I);
2413 case Instruction::Select:
2414 return SelectSelect(I);
2415 case Instruction::Ret:
2416 return SelectRet(I);
2417 case Instruction::Trunc:
2418 return SelectTrunc(I);
2419 case Instruction::ZExt:
2420 case Instruction::SExt:
2421 return SelectIntExt(I);
2427 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2428 /// vreg is being provided by the specified load instruction. If possible,
2429 /// try to fold the load as an operand to the instruction, returning true if
2431 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2432 const LoadInst *LI) {
2433 // Verify we have a legal type before going any further.
2435 if (!isLoadTypeLegal(LI->getType(), VT))
2438 // Combine load followed by zero- or sign-extend.
2439 // ldrb r1, [r0] ldrb r1, [r0]
2441 // mov r3, r2 mov r3, r1
2443 switch(MI->getOpcode()) {
2444 default: return false;
2462 // See if we can handle this address.
2464 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2466 unsigned ResultReg = MI->getOperand(0).getReg();
2467 if (!ARMEmitLoad(VT, ResultReg, Addr, isZExt, false))
2469 MI->eraseFromParent();
2474 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
2475 // Completely untested on non-darwin.
2476 const TargetMachine &TM = funcInfo.MF->getTarget();
2478 // Darwin and thumb1 only for now.
2479 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2480 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
2481 !DisableARMFastISel)
2482 return new ARMFastISel(funcInfo);