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,
182 unsigned Alignment = 0, bool isZExt = true,
183 bool allocReg = true);
185 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
186 unsigned Alignment = 0);
187 bool ARMComputeAddress(const Value *Obj, Address &Addr);
188 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
189 bool ARMIsMemCpySmall(uint64_t Len);
190 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
191 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
192 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
193 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
194 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
195 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
196 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
197 unsigned ARMSelectCallOp(const GlobalValue *GV);
199 // Call handling routines.
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 if (!MI->hasOptionalDef())
235 // Look to see if our OptionalDef is defining CPSR or CCR.
236 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
237 const MachineOperand &MO = MI->getOperand(i);
238 if (!MO.isReg() || !MO.isDef()) continue;
239 if (MO.getReg() == ARM::CPSR)
245 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
246 const MCInstrDesc &MCID = MI->getDesc();
248 // If we're a thumb2 or not NEON function we were handled via isPredicable.
249 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
250 AFI->isThumb2Function())
253 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
254 if (MCID.OpInfo[i].isPredicate())
260 // If the machine is predicable go ahead and add the predicate operands, if
261 // it needs default CC operands add those.
262 // TODO: If we want to support thumb1 then we'll need to deal with optional
263 // CPSR defs that need to be added before the remaining operands. See s_cc_out
264 // for descriptions why.
265 const MachineInstrBuilder &
266 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
267 MachineInstr *MI = &*MIB;
269 // Do we use a predicate? or...
270 // Are we NEON in ARM mode and have a predicate operand? If so, I know
271 // we're not predicable but add it anyways.
272 if (TII.isPredicable(MI) || isARMNEONPred(MI))
275 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
276 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
278 if (DefinesOptionalPredicate(MI, &CPSR)) {
287 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
288 const TargetRegisterClass* RC) {
289 unsigned ResultReg = createResultReg(RC);
290 const MCInstrDesc &II = TII.get(MachineInstOpcode);
292 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
296 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
297 const TargetRegisterClass *RC,
298 unsigned Op0, bool Op0IsKill) {
299 unsigned ResultReg = createResultReg(RC);
300 const MCInstrDesc &II = TII.get(MachineInstOpcode);
302 if (II.getNumDefs() >= 1)
303 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
304 .addReg(Op0, Op0IsKill * RegState::Kill));
306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
307 .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_rr(unsigned MachineInstOpcode,
316 const TargetRegisterClass *RC,
317 unsigned Op0, bool Op0IsKill,
318 unsigned Op1, bool Op1IsKill) {
319 unsigned ResultReg = createResultReg(RC);
320 const MCInstrDesc &II = TII.get(MachineInstOpcode);
322 if (II.getNumDefs() >= 1)
323 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
324 .addReg(Op0, Op0IsKill * RegState::Kill)
325 .addReg(Op1, Op1IsKill * RegState::Kill));
327 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
328 .addReg(Op0, Op0IsKill * RegState::Kill)
329 .addReg(Op1, Op1IsKill * RegState::Kill));
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
331 TII.get(TargetOpcode::COPY), ResultReg)
332 .addReg(II.ImplicitDefs[0]));
337 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
338 const TargetRegisterClass *RC,
339 unsigned Op0, bool Op0IsKill,
340 unsigned Op1, bool Op1IsKill,
341 unsigned Op2, bool Op2IsKill) {
342 unsigned ResultReg = createResultReg(RC);
343 const MCInstrDesc &II = TII.get(MachineInstOpcode);
345 if (II.getNumDefs() >= 1)
346 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
347 .addReg(Op0, Op0IsKill * RegState::Kill)
348 .addReg(Op1, Op1IsKill * RegState::Kill)
349 .addReg(Op2, Op2IsKill * RegState::Kill));
351 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
352 .addReg(Op0, Op0IsKill * RegState::Kill)
353 .addReg(Op1, Op1IsKill * RegState::Kill)
354 .addReg(Op2, Op2IsKill * RegState::Kill));
355 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
356 TII.get(TargetOpcode::COPY), ResultReg)
357 .addReg(II.ImplicitDefs[0]));
362 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
363 const TargetRegisterClass *RC,
364 unsigned Op0, bool Op0IsKill,
366 unsigned ResultReg = createResultReg(RC);
367 const MCInstrDesc &II = TII.get(MachineInstOpcode);
369 if (II.getNumDefs() >= 1)
370 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
371 .addReg(Op0, Op0IsKill * RegState::Kill)
374 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
375 .addReg(Op0, Op0IsKill * RegState::Kill)
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
378 TII.get(TargetOpcode::COPY), ResultReg)
379 .addReg(II.ImplicitDefs[0]));
384 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
385 const TargetRegisterClass *RC,
386 unsigned Op0, bool Op0IsKill,
387 const ConstantFP *FPImm) {
388 unsigned ResultReg = createResultReg(RC);
389 const MCInstrDesc &II = TII.get(MachineInstOpcode);
391 if (II.getNumDefs() >= 1)
392 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
393 .addReg(Op0, Op0IsKill * RegState::Kill)
396 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
397 .addReg(Op0, Op0IsKill * RegState::Kill)
399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
400 TII.get(TargetOpcode::COPY), ResultReg)
401 .addReg(II.ImplicitDefs[0]));
406 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
407 const TargetRegisterClass *RC,
408 unsigned Op0, bool Op0IsKill,
409 unsigned Op1, bool Op1IsKill,
411 unsigned ResultReg = createResultReg(RC);
412 const MCInstrDesc &II = TII.get(MachineInstOpcode);
414 if (II.getNumDefs() >= 1)
415 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
416 .addReg(Op0, Op0IsKill * RegState::Kill)
417 .addReg(Op1, Op1IsKill * RegState::Kill)
420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
421 .addReg(Op0, Op0IsKill * RegState::Kill)
422 .addReg(Op1, Op1IsKill * RegState::Kill)
424 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
425 TII.get(TargetOpcode::COPY), ResultReg)
426 .addReg(II.ImplicitDefs[0]));
431 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
432 const TargetRegisterClass *RC,
434 unsigned ResultReg = createResultReg(RC);
435 const MCInstrDesc &II = TII.get(MachineInstOpcode);
437 if (II.getNumDefs() >= 1)
438 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
441 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
443 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
444 TII.get(TargetOpcode::COPY), ResultReg)
445 .addReg(II.ImplicitDefs[0]));
450 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
451 const TargetRegisterClass *RC,
452 uint64_t Imm1, uint64_t Imm2) {
453 unsigned ResultReg = createResultReg(RC);
454 const MCInstrDesc &II = TII.get(MachineInstOpcode);
456 if (II.getNumDefs() >= 1)
457 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
458 .addImm(Imm1).addImm(Imm2));
460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
461 .addImm(Imm1).addImm(Imm2));
462 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
463 TII.get(TargetOpcode::COPY),
465 .addReg(II.ImplicitDefs[0]));
470 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
471 unsigned Op0, bool Op0IsKill,
473 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
474 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
475 "Cannot yet extract from physregs");
476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
477 DL, TII.get(TargetOpcode::COPY), ResultReg)
478 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
482 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
483 // checks from the various callers.
484 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
485 if (VT == MVT::f64) return 0;
487 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
488 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
489 TII.get(ARM::VMOVRS), MoveReg)
494 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
495 if (VT == MVT::i64) return 0;
497 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
498 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
499 TII.get(ARM::VMOVSR), MoveReg)
504 // For double width floating point we need to materialize two constants
505 // (the high and the low) into integer registers then use a move to get
506 // the combined constant into an FP reg.
507 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
508 const APFloat Val = CFP->getValueAPF();
509 bool is64bit = VT == MVT::f64;
511 // This checks to see if we can use VFP3 instructions to materialize
512 // a constant, otherwise we have to go through the constant pool.
513 if (TLI.isFPImmLegal(Val, VT)) {
517 Imm = ARM_AM::getFP64Imm(Val);
520 Imm = ARM_AM::getFP32Imm(Val);
523 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
524 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
530 // Require VFP2 for loading fp constants.
531 if (!Subtarget->hasVFP2()) return false;
533 // MachineConstantPool wants an explicit alignment.
534 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
536 // TODO: Figure out if this is correct.
537 Align = TD.getTypeAllocSize(CFP->getType());
539 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
540 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
541 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
543 // The extra reg is for addrmode5.
544 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
546 .addConstantPoolIndex(Idx)
551 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
553 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
556 // If we can do this in a single instruction without a constant pool entry
558 const ConstantInt *CI = cast<ConstantInt>(C);
559 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
560 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
561 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
562 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
563 TII.get(Opc), ImmReg)
564 .addImm(CI->getZExtValue()));
568 // Use MVN to emit negative constants.
569 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
570 unsigned Imm = (unsigned)~(CI->getSExtValue());
571 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
572 (ARM_AM::getSOImmVal(Imm) != -1);
574 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
575 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
576 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
577 TII.get(Opc), ImmReg)
583 // Load from constant pool. For now 32-bit only.
587 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
589 // MachineConstantPool wants an explicit alignment.
590 unsigned Align = TD.getPrefTypeAlignment(C->getType());
592 // TODO: Figure out if this is correct.
593 Align = TD.getTypeAllocSize(C->getType());
595 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
598 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
599 TII.get(ARM::t2LDRpci), DestReg)
600 .addConstantPoolIndex(Idx));
602 // The extra immediate is for addrmode2.
603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
604 TII.get(ARM::LDRcp), DestReg)
605 .addConstantPoolIndex(Idx)
611 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
612 // For now 32-bit only.
613 if (VT != MVT::i32) return 0;
615 Reloc::Model RelocM = TM.getRelocationModel();
617 // TODO: Need more magic for ARM PIC.
618 if (!isThumb2 && (RelocM == Reloc::PIC_)) return 0;
620 // MachineConstantPool wants an explicit alignment.
621 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
623 // TODO: Figure out if this is correct.
624 Align = TD.getTypeAllocSize(GV->getType());
628 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb() ? 4 : 8);
629 unsigned Id = AFI->createPICLabelUId();
630 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
633 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
636 MachineInstrBuilder MIB;
637 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
639 unsigned Opc = (RelocM != Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
640 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
641 .addConstantPoolIndex(Idx);
642 if (RelocM == Reloc::PIC_)
645 // The extra immediate is for addrmode2.
646 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
648 .addConstantPoolIndex(Idx)
651 AddOptionalDefs(MIB);
653 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
654 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
656 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
657 TII.get(ARM::t2LDRi12), NewDestReg)
661 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
665 DestReg = NewDestReg;
666 AddOptionalDefs(MIB);
672 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
673 EVT VT = TLI.getValueType(C->getType(), true);
675 // Only handle simple types.
676 if (!VT.isSimple()) return 0;
678 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
679 return ARMMaterializeFP(CFP, VT);
680 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
681 return ARMMaterializeGV(GV, VT);
682 else if (isa<ConstantInt>(C))
683 return ARMMaterializeInt(C, VT);
688 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
690 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
691 // Don't handle dynamic allocas.
692 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
695 if (!isLoadTypeLegal(AI->getType(), VT)) return false;
697 DenseMap<const AllocaInst*, int>::iterator SI =
698 FuncInfo.StaticAllocaMap.find(AI);
700 // This will get lowered later into the correct offsets and registers
701 // via rewriteXFrameIndex.
702 if (SI != FuncInfo.StaticAllocaMap.end()) {
703 TargetRegisterClass* RC = TLI.getRegClassFor(VT);
704 unsigned ResultReg = createResultReg(RC);
705 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
706 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
707 TII.get(Opc), ResultReg)
708 .addFrameIndex(SI->second)
716 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
717 EVT evt = TLI.getValueType(Ty, true);
719 // Only handle simple types.
720 if (evt == MVT::Other || !evt.isSimple()) return false;
721 VT = evt.getSimpleVT();
723 // Handle all legal types, i.e. a register that will directly hold this
725 return TLI.isTypeLegal(VT);
728 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
729 if (isTypeLegal(Ty, VT)) return true;
731 // If this is a type than can be sign or zero-extended to a basic operation
732 // go ahead and accept it now.
733 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
739 // Computes the address to get to an object.
740 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
741 // Some boilerplate from the X86 FastISel.
742 const User *U = NULL;
743 unsigned Opcode = Instruction::UserOp1;
744 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
745 // Don't walk into other basic blocks unless the object is an alloca from
746 // another block, otherwise it may not have a virtual register assigned.
747 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
748 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
749 Opcode = I->getOpcode();
752 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
753 Opcode = C->getOpcode();
757 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
758 if (Ty->getAddressSpace() > 255)
759 // Fast instruction selection doesn't support the special
766 case Instruction::BitCast: {
767 // Look through bitcasts.
768 return ARMComputeAddress(U->getOperand(0), Addr);
770 case Instruction::IntToPtr: {
771 // Look past no-op inttoptrs.
772 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
773 return ARMComputeAddress(U->getOperand(0), Addr);
776 case Instruction::PtrToInt: {
777 // Look past no-op ptrtoints.
778 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
779 return ARMComputeAddress(U->getOperand(0), Addr);
782 case Instruction::GetElementPtr: {
783 Address SavedAddr = Addr;
784 int TmpOffset = Addr.Offset;
786 // Iterate through the GEP folding the constants into offsets where
788 gep_type_iterator GTI = gep_type_begin(U);
789 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
790 i != e; ++i, ++GTI) {
791 const Value *Op = *i;
792 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
793 const StructLayout *SL = TD.getStructLayout(STy);
794 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
795 TmpOffset += SL->getElementOffset(Idx);
797 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
799 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
800 // Constant-offset addressing.
801 TmpOffset += CI->getSExtValue() * S;
804 if (isa<AddOperator>(Op) &&
805 (!isa<Instruction>(Op) ||
806 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
808 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
809 // An add (in the same block) with a constant operand. Fold the
812 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
813 TmpOffset += CI->getSExtValue() * S;
814 // Iterate on the other operand.
815 Op = cast<AddOperator>(Op)->getOperand(0);
819 goto unsupported_gep;
824 // Try to grab the base operand now.
825 Addr.Offset = TmpOffset;
826 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
828 // We failed, restore everything and try the other options.
834 case Instruction::Alloca: {
835 const AllocaInst *AI = cast<AllocaInst>(Obj);
836 DenseMap<const AllocaInst*, int>::iterator SI =
837 FuncInfo.StaticAllocaMap.find(AI);
838 if (SI != FuncInfo.StaticAllocaMap.end()) {
839 Addr.BaseType = Address::FrameIndexBase;
840 Addr.Base.FI = SI->second;
847 // Materialize the global variable's address into a reg which can
848 // then be used later to load the variable.
849 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Obj)) {
850 unsigned Tmp = ARMMaterializeGV(GV, TLI.getValueType(Obj->getType()));
851 if (Tmp == 0) return false;
857 // Try to get this in a register if nothing else has worked.
858 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
859 return Addr.Base.Reg != 0;
862 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
864 assert(VT.isSimple() && "Non-simple types are invalid here!");
866 bool needsLowering = false;
867 switch (VT.getSimpleVT().SimpleTy) {
869 assert(false && "Unhandled load/store type!");
876 // Integer loads/stores handle 12-bit offsets.
877 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
878 // Handle negative offsets.
879 if (needsLowering && isThumb2)
880 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
883 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
884 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
889 // Floating point operands handle 8-bit offsets.
890 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
894 // If this is a stack pointer and the offset needs to be simplified then
895 // put the alloca address into a register, set the base type back to
896 // register and continue. This should almost never happen.
897 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
898 TargetRegisterClass *RC = isThumb2 ? ARM::tGPRRegisterClass :
899 ARM::GPRRegisterClass;
900 unsigned ResultReg = createResultReg(RC);
901 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
902 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
903 TII.get(Opc), ResultReg)
904 .addFrameIndex(Addr.Base.FI)
906 Addr.Base.Reg = ResultReg;
907 Addr.BaseType = Address::RegBase;
910 // Since the offset is too large for the load/store instruction
911 // get the reg+offset into a register.
913 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
914 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
919 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
920 const MachineInstrBuilder &MIB,
921 unsigned Flags, bool useAM3) {
922 // addrmode5 output depends on the selection dag addressing dividing the
923 // offset by 4 that it then later multiplies. Do this here as well.
924 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
925 VT.getSimpleVT().SimpleTy == MVT::f64)
928 // Frame base works a bit differently. Handle it separately.
929 if (Addr.BaseType == Address::FrameIndexBase) {
930 int FI = Addr.Base.FI;
931 int Offset = Addr.Offset;
932 MachineMemOperand *MMO =
933 FuncInfo.MF->getMachineMemOperand(
934 MachinePointerInfo::getFixedStack(FI, Offset),
936 MFI.getObjectSize(FI),
937 MFI.getObjectAlignment(FI));
938 // Now add the rest of the operands.
939 MIB.addFrameIndex(FI);
941 // ARM halfword load/stores and signed byte loads need an additional
944 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
948 MIB.addImm(Addr.Offset);
950 MIB.addMemOperand(MMO);
952 // Now add the rest of the operands.
953 MIB.addReg(Addr.Base.Reg);
955 // ARM halfword load/stores and signed byte loads need an additional
958 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
962 MIB.addImm(Addr.Offset);
965 AddOptionalDefs(MIB);
968 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
969 unsigned Alignment, bool isZExt, bool allocReg) {
970 assert(VT.isSimple() && "Non-simple types are invalid here!");
973 bool needVMOV = false;
974 TargetRegisterClass *RC;
975 switch (VT.getSimpleVT().SimpleTy) {
976 // This is mostly going to be Neon/vector support.
977 default: return false;
981 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
982 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
984 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
993 RC = ARM::GPRRegisterClass;
997 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
998 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1000 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1002 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1005 RC = ARM::GPRRegisterClass;
1009 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1012 Opc = ARM::t2LDRi12;
1016 RC = ARM::GPRRegisterClass;
1019 // Unaligned loads need special handling. Floats require word-alignment.
1020 if (Alignment && Alignment < 4) {
1023 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1024 RC = ARM::GPRRegisterClass;
1027 RC = TLI.getRegClassFor(VT);
1031 // FIXME: Unaligned loads need special handling. Doublewords require
1033 if (Alignment && Alignment < 4)
1037 RC = TLI.getRegClassFor(VT);
1040 // Simplify this down to something we can handle.
1041 ARMSimplifyAddress(Addr, VT, useAM3);
1043 // Create the base instruction, then add the operands.
1045 ResultReg = createResultReg(RC);
1046 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1047 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1048 TII.get(Opc), ResultReg);
1049 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1051 // If we had an unaligned load of a float we've converted it to an regular
1052 // load. Now we must move from the GRP to the FP register.
1054 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1055 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1056 TII.get(ARM::VMOVSR), MoveReg)
1057 .addReg(ResultReg));
1058 ResultReg = MoveReg;
1063 bool ARMFastISel::SelectLoad(const Instruction *I) {
1064 // Atomic loads need special handling.
1065 if (cast<LoadInst>(I)->isAtomic())
1068 // Verify we have a legal type before going any further.
1070 if (!isLoadTypeLegal(I->getType(), VT))
1073 // See if we can handle this address.
1075 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1078 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1080 UpdateValueMap(I, ResultReg);
1084 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
1085 unsigned Alignment) {
1087 bool useAM3 = false;
1088 switch (VT.getSimpleVT().SimpleTy) {
1089 // This is mostly going to be Neon/vector support.
1090 default: return false;
1092 unsigned Res = createResultReg(isThumb2 ? ARM::tGPRRegisterClass :
1093 ARM::GPRRegisterClass);
1094 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1095 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1097 .addReg(SrcReg).addImm(1));
1099 } // Fallthrough here.
1102 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1103 StrOpc = ARM::t2STRBi8;
1105 StrOpc = ARM::t2STRBi12;
1107 StrOpc = ARM::STRBi12;
1112 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1113 StrOpc = ARM::t2STRHi8;
1115 StrOpc = ARM::t2STRHi12;
1123 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1124 StrOpc = ARM::t2STRi8;
1126 StrOpc = ARM::t2STRi12;
1128 StrOpc = ARM::STRi12;
1132 if (!Subtarget->hasVFP2()) return false;
1133 // Unaligned stores need special handling. Floats require word-alignment.
1134 if (Alignment && Alignment < 4) {
1135 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1136 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1137 TII.get(ARM::VMOVRS), MoveReg)
1141 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1143 StrOpc = ARM::VSTRS;
1147 if (!Subtarget->hasVFP2()) return false;
1148 // FIXME: Unaligned stores need special handling. Doublewords require
1150 if (Alignment && Alignment < 4)
1153 StrOpc = ARM::VSTRD;
1156 // Simplify this down to something we can handle.
1157 ARMSimplifyAddress(Addr, VT, useAM3);
1159 // Create the base instruction, then add the operands.
1160 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1163 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1167 bool ARMFastISel::SelectStore(const Instruction *I) {
1168 Value *Op0 = I->getOperand(0);
1169 unsigned SrcReg = 0;
1171 // Atomic stores need special handling.
1172 if (cast<StoreInst>(I)->isAtomic())
1175 // Verify we have a legal type before going any further.
1177 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1180 // Get the value to be stored into a register.
1181 SrcReg = getRegForValue(Op0);
1182 if (SrcReg == 0) return false;
1184 // See if we can handle this address.
1186 if (!ARMComputeAddress(I->getOperand(1), Addr))
1189 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1194 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1196 // Needs two compares...
1197 case CmpInst::FCMP_ONE:
1198 case CmpInst::FCMP_UEQ:
1200 // AL is our "false" for now. The other two need more compares.
1202 case CmpInst::ICMP_EQ:
1203 case CmpInst::FCMP_OEQ:
1205 case CmpInst::ICMP_SGT:
1206 case CmpInst::FCMP_OGT:
1208 case CmpInst::ICMP_SGE:
1209 case CmpInst::FCMP_OGE:
1211 case CmpInst::ICMP_UGT:
1212 case CmpInst::FCMP_UGT:
1214 case CmpInst::FCMP_OLT:
1216 case CmpInst::ICMP_ULE:
1217 case CmpInst::FCMP_OLE:
1219 case CmpInst::FCMP_ORD:
1221 case CmpInst::FCMP_UNO:
1223 case CmpInst::FCMP_UGE:
1225 case CmpInst::ICMP_SLT:
1226 case CmpInst::FCMP_ULT:
1228 case CmpInst::ICMP_SLE:
1229 case CmpInst::FCMP_ULE:
1231 case CmpInst::FCMP_UNE:
1232 case CmpInst::ICMP_NE:
1234 case CmpInst::ICMP_UGE:
1236 case CmpInst::ICMP_ULT:
1241 bool ARMFastISel::SelectBranch(const Instruction *I) {
1242 const BranchInst *BI = cast<BranchInst>(I);
1243 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1244 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1246 // Simple branch support.
1248 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1250 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1251 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1253 // Get the compare predicate.
1254 // Try to take advantage of fallthrough opportunities.
1255 CmpInst::Predicate Predicate = CI->getPredicate();
1256 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1257 std::swap(TBB, FBB);
1258 Predicate = CmpInst::getInversePredicate(Predicate);
1261 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1263 // We may not handle every CC for now.
1264 if (ARMPred == ARMCC::AL) return false;
1266 // Emit the compare.
1267 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1270 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1272 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1273 FastEmitBranch(FBB, DL);
1274 FuncInfo.MBB->addSuccessor(TBB);
1277 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1279 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1280 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1281 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1282 unsigned OpReg = getRegForValue(TI->getOperand(0));
1283 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1285 .addReg(OpReg).addImm(1));
1287 unsigned CCMode = ARMCC::NE;
1288 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1289 std::swap(TBB, FBB);
1293 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1295 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1297 FastEmitBranch(FBB, DL);
1298 FuncInfo.MBB->addSuccessor(TBB);
1301 } else if (const ConstantInt *CI =
1302 dyn_cast<ConstantInt>(BI->getCondition())) {
1303 uint64_t Imm = CI->getZExtValue();
1304 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1305 FastEmitBranch(Target, DL);
1309 unsigned CmpReg = getRegForValue(BI->getCondition());
1310 if (CmpReg == 0) return false;
1312 // We've been divorced from our compare! Our block was split, and
1313 // now our compare lives in a predecessor block. We musn't
1314 // re-compare here, as the children of the compare aren't guaranteed
1315 // live across the block boundary (we *could* check for this).
1316 // Regardless, the compare has been done in the predecessor block,
1317 // and it left a value for us in a virtual register. Ergo, we test
1318 // the one-bit value left in the virtual register.
1319 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1321 .addReg(CmpReg).addImm(1));
1323 unsigned CCMode = ARMCC::NE;
1324 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1325 std::swap(TBB, FBB);
1329 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1331 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1332 FastEmitBranch(FBB, DL);
1333 FuncInfo.MBB->addSuccessor(TBB);
1337 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1339 Type *Ty = Src1Value->getType();
1340 EVT SrcVT = TLI.getValueType(Ty, true);
1341 if (!SrcVT.isSimple()) return false;
1343 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1344 if (isFloat && !Subtarget->hasVFP2())
1347 // Check to see if the 2nd operand is a constant that we can encode directly
1350 bool UseImm = false;
1351 bool isNegativeImm = false;
1352 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1353 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1354 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1355 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1357 const APInt &CIVal = ConstInt->getValue();
1358 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1360 isNegativeImm = true;
1363 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1364 (ARM_AM::getSOImmVal(Imm) != -1);
1366 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1367 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1368 if (ConstFP->isZero() && !ConstFP->isNegative())
1374 bool needsExt = false;
1375 switch (SrcVT.getSimpleVT().SimpleTy) {
1376 default: return false;
1377 // TODO: Verify compares.
1380 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1384 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1390 // Intentional fall-through.
1394 CmpOpc = ARM::t2CMPrr;
1396 CmpOpc = isNegativeImm ? ARM::t2CMNzri : ARM::t2CMPri;
1399 CmpOpc = ARM::CMPrr;
1401 CmpOpc = isNegativeImm ? ARM::CMNzri : ARM::CMPri;
1406 unsigned SrcReg1 = getRegForValue(Src1Value);
1407 if (SrcReg1 == 0) return false;
1409 unsigned SrcReg2 = 0;
1411 SrcReg2 = getRegForValue(Src2Value);
1412 if (SrcReg2 == 0) return false;
1415 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1418 ResultReg = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1419 if (ResultReg == 0) return false;
1420 SrcReg1 = ResultReg;
1422 ResultReg = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1423 if (ResultReg == 0) return false;
1424 SrcReg2 = ResultReg;
1429 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1431 .addReg(SrcReg1).addReg(SrcReg2));
1433 MachineInstrBuilder MIB;
1434 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1437 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1440 AddOptionalDefs(MIB);
1443 // For floating point we need to move the result to a comparison register
1444 // that we can then use for branches.
1445 if (Ty->isFloatTy() || Ty->isDoubleTy())
1446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1447 TII.get(ARM::FMSTAT)));
1451 bool ARMFastISel::SelectCmp(const Instruction *I) {
1452 const CmpInst *CI = cast<CmpInst>(I);
1453 Type *Ty = CI->getOperand(0)->getType();
1455 // Get the compare predicate.
1456 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1458 // We may not handle every CC for now.
1459 if (ARMPred == ARMCC::AL) return false;
1461 // Emit the compare.
1462 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1465 // Now set a register based on the comparison. Explicitly set the predicates
1467 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1468 TargetRegisterClass *RC = isThumb2 ? ARM::rGPRRegisterClass
1469 : ARM::GPRRegisterClass;
1470 unsigned DestReg = createResultReg(RC);
1471 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1472 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1473 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1474 unsigned CondReg = isFloat ? ARM::FPSCR : ARM::CPSR;
1475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1476 .addReg(ZeroReg).addImm(1)
1477 .addImm(ARMPred).addReg(CondReg);
1479 UpdateValueMap(I, DestReg);
1483 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1484 // Make sure we have VFP and that we're extending float to double.
1485 if (!Subtarget->hasVFP2()) return false;
1487 Value *V = I->getOperand(0);
1488 if (!I->getType()->isDoubleTy() ||
1489 !V->getType()->isFloatTy()) return false;
1491 unsigned Op = getRegForValue(V);
1492 if (Op == 0) return false;
1494 unsigned Result = createResultReg(ARM::DPRRegisterClass);
1495 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1496 TII.get(ARM::VCVTDS), Result)
1498 UpdateValueMap(I, Result);
1502 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1503 // Make sure we have VFP and that we're truncating double to float.
1504 if (!Subtarget->hasVFP2()) return false;
1506 Value *V = I->getOperand(0);
1507 if (!(I->getType()->isFloatTy() &&
1508 V->getType()->isDoubleTy())) return false;
1510 unsigned Op = getRegForValue(V);
1511 if (Op == 0) return false;
1513 unsigned Result = createResultReg(ARM::SPRRegisterClass);
1514 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1515 TII.get(ARM::VCVTSD), Result)
1517 UpdateValueMap(I, Result);
1521 bool ARMFastISel::SelectSIToFP(const Instruction *I) {
1522 // Make sure we have VFP.
1523 if (!Subtarget->hasVFP2()) return false;
1526 Type *Ty = I->getType();
1527 if (!isTypeLegal(Ty, DstVT))
1530 Value *Src = I->getOperand(0);
1531 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1532 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1535 unsigned SrcReg = getRegForValue(Src);
1536 if (SrcReg == 0) return false;
1538 // Handle sign-extension.
1539 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1540 EVT DestVT = MVT::i32;
1541 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, /*isZExt*/ false);
1542 if (ResultReg == 0) return false;
1546 // The conversion routine works on fp-reg to fp-reg and the operand above
1547 // was an integer, move it to the fp registers if possible.
1548 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1549 if (FP == 0) return false;
1552 if (Ty->isFloatTy()) Opc = ARM::VSITOS;
1553 else if (Ty->isDoubleTy()) Opc = ARM::VSITOD;
1556 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1557 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1560 UpdateValueMap(I, ResultReg);
1564 bool ARMFastISel::SelectFPToSI(const Instruction *I) {
1565 // Make sure we have VFP.
1566 if (!Subtarget->hasVFP2()) return false;
1569 Type *RetTy = I->getType();
1570 if (!isTypeLegal(RetTy, DstVT))
1573 unsigned Op = getRegForValue(I->getOperand(0));
1574 if (Op == 0) return false;
1577 Type *OpTy = I->getOperand(0)->getType();
1578 if (OpTy->isFloatTy()) Opc = ARM::VTOSIZS;
1579 else if (OpTy->isDoubleTy()) Opc = ARM::VTOSIZD;
1582 // f64->s32 or f32->s32 both need an intermediate f32 reg.
1583 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1584 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1588 // This result needs to be in an integer register, but the conversion only
1589 // takes place in fp-regs.
1590 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1591 if (IntReg == 0) return false;
1593 UpdateValueMap(I, IntReg);
1597 bool ARMFastISel::SelectSelect(const Instruction *I) {
1599 if (!isTypeLegal(I->getType(), VT))
1602 // Things need to be register sized for register moves.
1603 if (VT != MVT::i32) return false;
1604 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1606 unsigned CondReg = getRegForValue(I->getOperand(0));
1607 if (CondReg == 0) return false;
1608 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1609 if (Op1Reg == 0) return false;
1611 // Check to see if we can use an immediate in the conditional move.
1613 bool UseImm = false;
1614 bool isNegativeImm = false;
1615 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1616 assert (VT == MVT::i32 && "Expecting an i32.");
1617 Imm = (int)ConstInt->getValue().getZExtValue();
1619 isNegativeImm = true;
1622 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1623 (ARM_AM::getSOImmVal(Imm) != -1);
1626 unsigned Op2Reg = 0;
1628 Op2Reg = getRegForValue(I->getOperand(2));
1629 if (Op2Reg == 0) return false;
1632 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1633 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1634 .addReg(CondReg).addImm(0));
1638 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1640 if (!isNegativeImm) {
1641 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1643 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1646 unsigned ResultReg = createResultReg(RC);
1648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1649 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1651 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1652 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1653 UpdateValueMap(I, ResultReg);
1657 bool ARMFastISel::SelectSDiv(const Instruction *I) {
1659 Type *Ty = I->getType();
1660 if (!isTypeLegal(Ty, VT))
1663 // If we have integer div support we should have selected this automagically.
1664 // In case we have a real miss go ahead and return false and we'll pick
1666 if (Subtarget->hasDivide()) return false;
1668 // Otherwise emit a libcall.
1669 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1671 LC = RTLIB::SDIV_I8;
1672 else if (VT == MVT::i16)
1673 LC = RTLIB::SDIV_I16;
1674 else if (VT == MVT::i32)
1675 LC = RTLIB::SDIV_I32;
1676 else if (VT == MVT::i64)
1677 LC = RTLIB::SDIV_I64;
1678 else if (VT == MVT::i128)
1679 LC = RTLIB::SDIV_I128;
1680 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1682 return ARMEmitLibcall(I, LC);
1685 bool ARMFastISel::SelectSRem(const Instruction *I) {
1687 Type *Ty = I->getType();
1688 if (!isTypeLegal(Ty, VT))
1691 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1693 LC = RTLIB::SREM_I8;
1694 else if (VT == MVT::i16)
1695 LC = RTLIB::SREM_I16;
1696 else if (VT == MVT::i32)
1697 LC = RTLIB::SREM_I32;
1698 else if (VT == MVT::i64)
1699 LC = RTLIB::SREM_I64;
1700 else if (VT == MVT::i128)
1701 LC = RTLIB::SREM_I128;
1702 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1704 return ARMEmitLibcall(I, LC);
1707 bool ARMFastISel::SelectBinaryOp(const Instruction *I, unsigned ISDOpcode) {
1708 EVT VT = TLI.getValueType(I->getType(), true);
1710 // We can get here in the case when we want to use NEON for our fp
1711 // operations, but can't figure out how to. Just use the vfp instructions
1713 // FIXME: It'd be nice to use NEON instructions.
1714 Type *Ty = I->getType();
1715 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1716 if (isFloat && !Subtarget->hasVFP2())
1720 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1721 switch (ISDOpcode) {
1722 default: return false;
1724 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1727 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1730 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1733 unsigned Op1 = getRegForValue(I->getOperand(0));
1734 if (Op1 == 0) return false;
1736 unsigned Op2 = getRegForValue(I->getOperand(1));
1737 if (Op2 == 0) return false;
1739 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1740 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1741 TII.get(Opc), ResultReg)
1742 .addReg(Op1).addReg(Op2));
1743 UpdateValueMap(I, ResultReg);
1747 // Call Handling Code
1749 // This is largely taken directly from CCAssignFnForNode - we don't support
1750 // varargs in FastISel so that part has been removed.
1751 // TODO: We may not support all of this.
1752 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC, bool Return) {
1755 llvm_unreachable("Unsupported calling convention");
1756 case CallingConv::Fast:
1757 // Ignore fastcc. Silence compiler warnings.
1758 (void)RetFastCC_ARM_APCS;
1759 (void)FastCC_ARM_APCS;
1761 case CallingConv::C:
1762 // Use target triple & subtarget features to do actual dispatch.
1763 if (Subtarget->isAAPCS_ABI()) {
1764 if (Subtarget->hasVFP2() &&
1765 TM.Options.FloatABIType == FloatABI::Hard)
1766 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1768 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1770 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1771 case CallingConv::ARM_AAPCS_VFP:
1772 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1773 case CallingConv::ARM_AAPCS:
1774 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1775 case CallingConv::ARM_APCS:
1776 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1780 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1781 SmallVectorImpl<unsigned> &ArgRegs,
1782 SmallVectorImpl<MVT> &ArgVTs,
1783 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1784 SmallVectorImpl<unsigned> &RegArgs,
1786 unsigned &NumBytes) {
1787 SmallVector<CCValAssign, 16> ArgLocs;
1788 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1789 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC, false));
1791 // Get a count of how many bytes are to be pushed on the stack.
1792 NumBytes = CCInfo.getNextStackOffset();
1794 // Issue CALLSEQ_START
1795 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1796 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1797 TII.get(AdjStackDown))
1800 // Process the args.
1801 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1802 CCValAssign &VA = ArgLocs[i];
1803 unsigned Arg = ArgRegs[VA.getValNo()];
1804 MVT ArgVT = ArgVTs[VA.getValNo()];
1806 // We don't handle NEON/vector parameters yet.
1807 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1810 // Handle arg promotion, etc.
1811 switch (VA.getLocInfo()) {
1812 case CCValAssign::Full: break;
1813 case CCValAssign::SExt: {
1814 MVT DestVT = VA.getLocVT();
1815 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1817 assert (ResultReg != 0 && "Failed to emit a sext");
1822 case CCValAssign::AExt:
1823 // Intentional fall-through. Handle AExt and ZExt.
1824 case CCValAssign::ZExt: {
1825 MVT DestVT = VA.getLocVT();
1826 unsigned ResultReg = ARMEmitIntExt(ArgVT, Arg, DestVT,
1828 assert (ResultReg != 0 && "Failed to emit a sext");
1833 case CCValAssign::BCvt: {
1834 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1835 /*TODO: Kill=*/false);
1836 assert(BC != 0 && "Failed to emit a bitcast!");
1838 ArgVT = VA.getLocVT();
1841 default: llvm_unreachable("Unknown arg promotion!");
1844 // Now copy/store arg to correct locations.
1845 if (VA.isRegLoc() && !VA.needsCustom()) {
1846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1849 RegArgs.push_back(VA.getLocReg());
1850 } else if (VA.needsCustom()) {
1851 // TODO: We need custom lowering for vector (v2f64) args.
1852 if (VA.getLocVT() != MVT::f64) return false;
1854 CCValAssign &NextVA = ArgLocs[++i];
1856 // TODO: Only handle register args for now.
1857 if(!(VA.isRegLoc() && NextVA.isRegLoc())) return false;
1859 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1860 TII.get(ARM::VMOVRRD), VA.getLocReg())
1861 .addReg(NextVA.getLocReg(), RegState::Define)
1863 RegArgs.push_back(VA.getLocReg());
1864 RegArgs.push_back(NextVA.getLocReg());
1866 assert(VA.isMemLoc());
1867 // Need to store on the stack.
1869 Addr.BaseType = Address::RegBase;
1870 Addr.Base.Reg = ARM::SP;
1871 Addr.Offset = VA.getLocMemOffset();
1873 if (!ARMEmitStore(ArgVT, Arg, Addr)) return false;
1879 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1880 const Instruction *I, CallingConv::ID CC,
1881 unsigned &NumBytes) {
1882 // Issue CALLSEQ_END
1883 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1884 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1885 TII.get(AdjStackUp))
1886 .addImm(NumBytes).addImm(0));
1888 // Now the return value.
1889 if (RetVT != MVT::isVoid) {
1890 SmallVector<CCValAssign, 16> RVLocs;
1891 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1892 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true));
1894 // Copy all of the result registers out of their specified physreg.
1895 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
1896 // For this move we copy into two registers and then move into the
1897 // double fp reg we want.
1898 EVT DestVT = RVLocs[0].getValVT();
1899 TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
1900 unsigned ResultReg = createResultReg(DstRC);
1901 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1902 TII.get(ARM::VMOVDRR), ResultReg)
1903 .addReg(RVLocs[0].getLocReg())
1904 .addReg(RVLocs[1].getLocReg()));
1906 UsedRegs.push_back(RVLocs[0].getLocReg());
1907 UsedRegs.push_back(RVLocs[1].getLocReg());
1909 // Finally update the result.
1910 UpdateValueMap(I, ResultReg);
1912 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
1913 EVT CopyVT = RVLocs[0].getValVT();
1915 // Special handling for extended integers.
1916 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1919 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1921 unsigned ResultReg = createResultReg(DstRC);
1922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1923 ResultReg).addReg(RVLocs[0].getLocReg());
1924 UsedRegs.push_back(RVLocs[0].getLocReg());
1926 // Finally update the result.
1927 UpdateValueMap(I, ResultReg);
1934 bool ARMFastISel::SelectRet(const Instruction *I) {
1935 const ReturnInst *Ret = cast<ReturnInst>(I);
1936 const Function &F = *I->getParent()->getParent();
1938 if (!FuncInfo.CanLowerReturn)
1944 CallingConv::ID CC = F.getCallingConv();
1945 if (Ret->getNumOperands() > 0) {
1946 SmallVector<ISD::OutputArg, 4> Outs;
1947 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
1950 // Analyze operands of the call, assigning locations to each operand.
1951 SmallVector<CCValAssign, 16> ValLocs;
1952 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
1953 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */));
1955 const Value *RV = Ret->getOperand(0);
1956 unsigned Reg = getRegForValue(RV);
1960 // Only handle a single return value for now.
1961 if (ValLocs.size() != 1)
1964 CCValAssign &VA = ValLocs[0];
1966 // Don't bother handling odd stuff for now.
1967 if (VA.getLocInfo() != CCValAssign::Full)
1969 // Only handle register returns for now.
1973 unsigned SrcReg = Reg + VA.getValNo();
1974 EVT RVVT = TLI.getValueType(RV->getType());
1975 EVT DestVT = VA.getValVT();
1976 // Special handling for extended integers.
1977 if (RVVT != DestVT) {
1978 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1981 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1984 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
1986 bool isZExt = Outs[0].Flags.isZExt();
1987 unsigned ResultReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1988 if (ResultReg == 0) return false;
1993 unsigned DstReg = VA.getLocReg();
1994 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
1995 // Avoid a cross-class copy. This is very unlikely.
1996 if (!SrcRC->contains(DstReg))
1998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1999 DstReg).addReg(SrcReg);
2001 // Mark the register as live out of the function.
2002 MRI.addLiveOut(VA.getLocReg());
2005 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2006 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2011 unsigned ARMFastISel::ARMSelectCallOp(const GlobalValue *GV) {
2013 // Darwin needs the r9 versions of the opcodes.
2014 bool isDarwin = Subtarget->isTargetDarwin();
2016 return isDarwin ? ARM::tBLr9 : ARM::tBL;
2018 return isDarwin ? ARM::BLr9 : ARM::BL;
2022 // A quick function that will emit a call for a named libcall in F with the
2023 // vector of passed arguments for the Instruction in I. We can assume that we
2024 // can emit a call for any libcall we can produce. This is an abridged version
2025 // of the full call infrastructure since we won't need to worry about things
2026 // like computed function pointers or strange arguments at call sites.
2027 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2029 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2030 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2032 // Handle *simple* calls for now.
2033 Type *RetTy = I->getType();
2035 if (RetTy->isVoidTy())
2036 RetVT = MVT::isVoid;
2037 else if (!isTypeLegal(RetTy, RetVT))
2040 // TODO: For now if we have long calls specified we don't handle the call.
2041 if (EnableARMLongCalls) return false;
2043 // Set up the argument vectors.
2044 SmallVector<Value*, 8> Args;
2045 SmallVector<unsigned, 8> ArgRegs;
2046 SmallVector<MVT, 8> ArgVTs;
2047 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2048 Args.reserve(I->getNumOperands());
2049 ArgRegs.reserve(I->getNumOperands());
2050 ArgVTs.reserve(I->getNumOperands());
2051 ArgFlags.reserve(I->getNumOperands());
2052 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2053 Value *Op = I->getOperand(i);
2054 unsigned Arg = getRegForValue(Op);
2055 if (Arg == 0) return false;
2057 Type *ArgTy = Op->getType();
2059 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2061 ISD::ArgFlagsTy Flags;
2062 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2063 Flags.setOrigAlign(OriginalAlignment);
2066 ArgRegs.push_back(Arg);
2067 ArgVTs.push_back(ArgVT);
2068 ArgFlags.push_back(Flags);
2071 // Handle the arguments now that we've gotten them.
2072 SmallVector<unsigned, 4> RegArgs;
2074 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2077 // Issue the call, BLr9 for darwin, BL otherwise.
2078 // TODO: Turn this into the table of arm call ops.
2079 MachineInstrBuilder MIB;
2080 unsigned CallOpc = ARMSelectCallOp(NULL);
2082 // Explicitly adding the predicate here.
2083 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2085 .addExternalSymbol(TLI.getLibcallName(Call));
2087 // Explicitly adding the predicate here.
2088 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2090 .addExternalSymbol(TLI.getLibcallName(Call)));
2092 // Add implicit physical register uses to the call.
2093 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2094 MIB.addReg(RegArgs[i]);
2096 // Finish off the call including any return values.
2097 SmallVector<unsigned, 4> UsedRegs;
2098 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2100 // Set all unused physreg defs as dead.
2101 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2106 bool ARMFastISel::SelectCall(const Instruction *I,
2107 const char *IntrMemName = 0) {
2108 const CallInst *CI = cast<CallInst>(I);
2109 const Value *Callee = CI->getCalledValue();
2111 // Can't handle inline asm.
2112 if (isa<InlineAsm>(Callee)) return false;
2114 // Only handle global variable Callees.
2115 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2119 // Check the calling convention.
2120 ImmutableCallSite CS(CI);
2121 CallingConv::ID CC = CS.getCallingConv();
2123 // TODO: Avoid some calling conventions?
2125 // Let SDISel handle vararg functions.
2126 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2127 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2128 if (FTy->isVarArg())
2131 // Handle *simple* calls for now.
2132 Type *RetTy = I->getType();
2134 if (RetTy->isVoidTy())
2135 RetVT = MVT::isVoid;
2136 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2137 RetVT != MVT::i8 && RetVT != MVT::i1)
2140 // TODO: For now if we have long calls specified we don't handle the call.
2141 if (EnableARMLongCalls) return false;
2143 // Set up the argument vectors.
2144 SmallVector<Value*, 8> Args;
2145 SmallVector<unsigned, 8> ArgRegs;
2146 SmallVector<MVT, 8> ArgVTs;
2147 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2148 Args.reserve(CS.arg_size());
2149 ArgRegs.reserve(CS.arg_size());
2150 ArgVTs.reserve(CS.arg_size());
2151 ArgFlags.reserve(CS.arg_size());
2152 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2154 // If we're lowering a memory intrinsic instead of a regular call, skip the
2155 // last two arguments, which shouldn't be passed to the underlying function.
2156 if (IntrMemName && e-i <= 2)
2159 ISD::ArgFlagsTy Flags;
2160 unsigned AttrInd = i - CS.arg_begin() + 1;
2161 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2163 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2166 // FIXME: Only handle *easy* calls for now.
2167 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2168 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2169 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2170 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2173 Type *ArgTy = (*i)->getType();
2175 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2179 unsigned Arg = getRegForValue(*i);
2183 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2184 Flags.setOrigAlign(OriginalAlignment);
2187 ArgRegs.push_back(Arg);
2188 ArgVTs.push_back(ArgVT);
2189 ArgFlags.push_back(Flags);
2192 // Handle the arguments now that we've gotten them.
2193 SmallVector<unsigned, 4> RegArgs;
2195 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
2198 // Issue the call, BLr9 for darwin, BL otherwise.
2199 // TODO: Turn this into the table of arm call ops.
2200 MachineInstrBuilder MIB;
2201 unsigned CallOpc = ARMSelectCallOp(GV);
2202 // Explicitly adding the predicate here.
2204 // Explicitly adding the predicate here.
2205 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2208 MIB.addGlobalAddress(GV, 0, 0);
2210 MIB.addExternalSymbol(IntrMemName, 0);
2213 // Explicitly adding the predicate here.
2214 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2216 .addGlobalAddress(GV, 0, 0));
2218 MIB = AddDefaultPred(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2220 .addExternalSymbol(IntrMemName, 0));
2223 // Add implicit physical register uses to the call.
2224 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2225 MIB.addReg(RegArgs[i]);
2227 // Finish off the call including any return values.
2228 SmallVector<unsigned, 4> UsedRegs;
2229 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes)) return false;
2231 // Set all unused physreg defs as dead.
2232 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2237 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2241 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len) {
2242 // Make sure we don't bloat code by inlining very large memcpy's.
2243 if (!ARMIsMemCpySmall(Len))
2246 // We don't care about alignment here since we just emit integer accesses.
2260 RV = ARMEmitLoad(VT, ResultReg, Src);
2261 assert (RV = true && "Should be able to handle this load.");
2262 RV = ARMEmitStore(VT, ResultReg, Dest);
2263 assert (RV = true && "Should be able to handle this store.");
2265 unsigned Size = VT.getSizeInBits()/8;
2267 Dest.Offset += Size;
2274 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2275 // FIXME: Handle more intrinsics.
2276 switch (I.getIntrinsicID()) {
2277 default: return false;
2278 case Intrinsic::memcpy:
2279 case Intrinsic::memmove: {
2280 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2281 // Don't handle volatile.
2282 if (MTI.isVolatile())
2285 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2286 // we would emit dead code because we don't currently handle memmoves.
2287 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2288 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2289 // Small memcpy's are common enough that we want to do them without a call
2291 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2292 if (ARMIsMemCpySmall(Len)) {
2294 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2295 !ARMComputeAddress(MTI.getRawSource(), Src))
2297 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2302 if (!MTI.getLength()->getType()->isIntegerTy(32))
2305 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2308 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2309 return SelectCall(&I, IntrMemName);
2311 case Intrinsic::memset: {
2312 const MemSetInst &MSI = cast<MemSetInst>(I);
2313 // Don't handle volatile.
2314 if (MSI.isVolatile())
2317 if (!MSI.getLength()->getType()->isIntegerTy(32))
2320 if (MSI.getDestAddressSpace() > 255)
2323 return SelectCall(&I, "memset");
2329 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2330 // The high bits for a type smaller than the register size are assumed to be
2332 Value *Op = I->getOperand(0);
2335 SrcVT = TLI.getValueType(Op->getType(), true);
2336 DestVT = TLI.getValueType(I->getType(), true);
2338 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2340 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2343 unsigned SrcReg = getRegForValue(Op);
2344 if (!SrcReg) return false;
2346 // Because the high bits are undefined, a truncate doesn't generate
2348 UpdateValueMap(I, SrcReg);
2352 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2354 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2358 bool isBoolZext = false;
2359 if (!SrcVT.isSimple()) return 0;
2360 switch (SrcVT.getSimpleVT().SimpleTy) {
2363 if (!Subtarget->hasV6Ops()) return 0;
2365 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2367 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2370 if (!Subtarget->hasV6Ops()) return 0;
2372 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2374 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2378 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2385 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2386 MachineInstrBuilder MIB;
2387 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2393 AddOptionalDefs(MIB);
2397 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2398 // On ARM, in general, integer casts don't involve legal types; this code
2399 // handles promotable integers.
2400 Type *DestTy = I->getType();
2401 Value *Src = I->getOperand(0);
2402 Type *SrcTy = Src->getType();
2405 SrcVT = TLI.getValueType(SrcTy, true);
2406 DestVT = TLI.getValueType(DestTy, true);
2408 bool isZExt = isa<ZExtInst>(I);
2409 unsigned SrcReg = getRegForValue(Src);
2410 if (!SrcReg) return false;
2412 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2413 if (ResultReg == 0) return false;
2414 UpdateValueMap(I, ResultReg);
2418 // TODO: SoftFP support.
2419 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2421 switch (I->getOpcode()) {
2422 case Instruction::Load:
2423 return SelectLoad(I);
2424 case Instruction::Store:
2425 return SelectStore(I);
2426 case Instruction::Br:
2427 return SelectBranch(I);
2428 case Instruction::ICmp:
2429 case Instruction::FCmp:
2430 return SelectCmp(I);
2431 case Instruction::FPExt:
2432 return SelectFPExt(I);
2433 case Instruction::FPTrunc:
2434 return SelectFPTrunc(I);
2435 case Instruction::SIToFP:
2436 return SelectSIToFP(I);
2437 case Instruction::FPToSI:
2438 return SelectFPToSI(I);
2439 case Instruction::FAdd:
2440 return SelectBinaryOp(I, ISD::FADD);
2441 case Instruction::FSub:
2442 return SelectBinaryOp(I, ISD::FSUB);
2443 case Instruction::FMul:
2444 return SelectBinaryOp(I, ISD::FMUL);
2445 case Instruction::SDiv:
2446 return SelectSDiv(I);
2447 case Instruction::SRem:
2448 return SelectSRem(I);
2449 case Instruction::Call:
2450 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2451 return SelectIntrinsicCall(*II);
2452 return SelectCall(I);
2453 case Instruction::Select:
2454 return SelectSelect(I);
2455 case Instruction::Ret:
2456 return SelectRet(I);
2457 case Instruction::Trunc:
2458 return SelectTrunc(I);
2459 case Instruction::ZExt:
2460 case Instruction::SExt:
2461 return SelectIntExt(I);
2467 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2468 /// vreg is being provided by the specified load instruction. If possible,
2469 /// try to fold the load as an operand to the instruction, returning true if
2471 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2472 const LoadInst *LI) {
2473 // Verify we have a legal type before going any further.
2475 if (!isLoadTypeLegal(LI->getType(), VT))
2478 // Combine load followed by zero- or sign-extend.
2479 // ldrb r1, [r0] ldrb r1, [r0]
2481 // mov r3, r2 mov r3, r1
2483 switch(MI->getOpcode()) {
2484 default: return false;
2502 // See if we can handle this address.
2504 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2506 unsigned ResultReg = MI->getOperand(0).getReg();
2507 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2509 MI->eraseFromParent();
2514 llvm::FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo) {
2515 // Completely untested on non-darwin.
2516 const TargetMachine &TM = funcInfo.MF->getTarget();
2518 // Darwin and thumb1 only for now.
2519 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2520 if (Subtarget->isTargetDarwin() && !Subtarget->isThumb1Only() &&
2521 !DisableARMFastISel)
2522 return new ARMFastISel(funcInfo);