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 "ARMTargetMachine.h"
20 #include "ARMSubtarget.h"
21 #include "ARMConstantPoolValue.h"
22 #include "MCTargetDesc/ARMAddressingModes.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Module.h"
29 #include "llvm/Operator.h"
30 #include "llvm/CodeGen/Analysis.h"
31 #include "llvm/CodeGen/FastISel.h"
32 #include "llvm/CodeGen/FunctionLoweringInfo.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineConstantPool.h"
36 #include "llvm/CodeGen/MachineFrameInfo.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/Support/CallSite.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/GetElementPtrTypeIterator.h"
43 #include "llvm/Target/TargetData.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
50 extern cl::opt<bool> EnableARMLongCalls;
54 // All possible address modes, plus some.
55 typedef struct Address {
68 // Innocuous defaults for our address.
70 : BaseType(RegBase), Offset(0) {
75 class ARMFastISel : public FastISel {
77 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
78 /// make the right decision when generating code for different targets.
79 const ARMSubtarget *Subtarget;
80 const TargetMachine &TM;
81 const TargetInstrInfo &TII;
82 const TargetLowering &TLI;
85 // Convenience variables to avoid some queries.
90 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
91 const TargetLibraryInfo *libInfo)
92 : FastISel(funcInfo, libInfo),
93 TM(funcInfo.MF->getTarget()),
94 TII(*TM.getInstrInfo()),
95 TLI(*TM.getTargetLowering()) {
96 Subtarget = &TM.getSubtarget<ARMSubtarget>();
97 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
98 isThumb2 = AFI->isThumbFunction();
99 Context = &funcInfo.Fn->getContext();
102 // Code from FastISel.cpp.
104 unsigned FastEmitInst_(unsigned MachineInstOpcode,
105 const TargetRegisterClass *RC);
106 unsigned FastEmitInst_r(unsigned MachineInstOpcode,
107 const TargetRegisterClass *RC,
108 unsigned Op0, bool Op0IsKill);
109 unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
110 const TargetRegisterClass *RC,
111 unsigned Op0, bool Op0IsKill,
112 unsigned Op1, bool Op1IsKill);
113 unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
114 const TargetRegisterClass *RC,
115 unsigned Op0, bool Op0IsKill,
116 unsigned Op1, bool Op1IsKill,
117 unsigned Op2, bool Op2IsKill);
118 unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
119 const TargetRegisterClass *RC,
120 unsigned Op0, bool Op0IsKill,
122 unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
123 const TargetRegisterClass *RC,
124 unsigned Op0, bool Op0IsKill,
125 const ConstantFP *FPImm);
126 unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
127 const TargetRegisterClass *RC,
128 unsigned Op0, bool Op0IsKill,
129 unsigned Op1, bool Op1IsKill,
131 unsigned FastEmitInst_i(unsigned MachineInstOpcode,
132 const TargetRegisterClass *RC,
134 unsigned FastEmitInst_ii(unsigned MachineInstOpcode,
135 const TargetRegisterClass *RC,
136 uint64_t Imm1, uint64_t Imm2);
138 unsigned FastEmitInst_extractsubreg(MVT RetVT,
139 unsigned Op0, bool Op0IsKill,
142 // Backend specific FastISel code.
144 virtual bool TargetSelectInstruction(const Instruction *I);
145 virtual unsigned TargetMaterializeConstant(const Constant *C);
146 virtual unsigned TargetMaterializeAlloca(const AllocaInst *AI);
147 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
150 #include "ARMGenFastISel.inc"
152 // Instruction selection routines.
154 bool SelectLoad(const Instruction *I);
155 bool SelectStore(const Instruction *I);
156 bool SelectBranch(const Instruction *I);
157 bool SelectIndirectBr(const Instruction *I);
158 bool SelectCmp(const Instruction *I);
159 bool SelectFPExt(const Instruction *I);
160 bool SelectFPTrunc(const Instruction *I);
161 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
162 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
163 bool SelectIToFP(const Instruction *I, bool isSigned);
164 bool SelectFPToI(const Instruction *I, bool isSigned);
165 bool SelectDiv(const Instruction *I, bool isSigned);
166 bool SelectRem(const Instruction *I, bool isSigned);
167 bool SelectCall(const Instruction *I, const char *IntrMemName);
168 bool SelectIntrinsicCall(const IntrinsicInst &I);
169 bool SelectSelect(const Instruction *I);
170 bool SelectRet(const Instruction *I);
171 bool SelectTrunc(const Instruction *I);
172 bool SelectIntExt(const Instruction *I);
173 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
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);
184 bool ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
185 unsigned Alignment = 0);
186 bool ARMComputeAddress(const Value *Obj, Address &Addr);
187 void ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3);
188 bool ARMIsMemCpySmall(uint64_t Len);
189 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len);
190 unsigned ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT, bool isZExt);
191 unsigned ARMMaterializeFP(const ConstantFP *CFP, EVT VT);
192 unsigned ARMMaterializeInt(const Constant *C, EVT VT);
193 unsigned ARMMaterializeGV(const GlobalValue *GV, EVT VT);
194 unsigned ARMMoveToFPReg(EVT VT, unsigned SrcReg);
195 unsigned ARMMoveToIntReg(EVT VT, unsigned SrcReg);
196 unsigned ARMSelectCallOp(bool UseReg);
198 // Call handling routines.
200 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
203 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
204 SmallVectorImpl<unsigned> &ArgRegs,
205 SmallVectorImpl<MVT> &ArgVTs,
206 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
207 SmallVectorImpl<unsigned> &RegArgs,
211 unsigned getLibcallReg(const Twine &Name);
212 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
213 const Instruction *I, CallingConv::ID CC,
214 unsigned &NumBytes, bool isVarArg);
215 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
217 // OptionalDef handling routines.
219 bool isARMNEONPred(const MachineInstr *MI);
220 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
221 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
222 void AddLoadStoreOperands(EVT VT, Address &Addr,
223 const MachineInstrBuilder &MIB,
224 unsigned Flags, bool useAM3);
227 } // end anonymous namespace
229 #include "ARMGenCallingConv.inc"
231 // DefinesOptionalPredicate - This is different from DefinesPredicate in that
232 // we don't care about implicit defs here, just places we'll need to add a
233 // default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
234 bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
235 if (!MI->hasOptionalDef())
238 // Look to see if our OptionalDef is defining CPSR or CCR.
239 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
240 const MachineOperand &MO = MI->getOperand(i);
241 if (!MO.isReg() || !MO.isDef()) continue;
242 if (MO.getReg() == ARM::CPSR)
248 bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
249 const MCInstrDesc &MCID = MI->getDesc();
251 // If we're a thumb2 or not NEON function we were handled via isPredicable.
252 if ((MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainNEON ||
253 AFI->isThumb2Function())
256 for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
257 if (MCID.OpInfo[i].isPredicate())
263 // If the machine is predicable go ahead and add the predicate operands, if
264 // it needs default CC operands add those.
265 // TODO: If we want to support thumb1 then we'll need to deal with optional
266 // CPSR defs that need to be added before the remaining operands. See s_cc_out
267 // for descriptions why.
268 const MachineInstrBuilder &
269 ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
270 MachineInstr *MI = &*MIB;
272 // Do we use a predicate? or...
273 // Are we NEON in ARM mode and have a predicate operand? If so, I know
274 // we're not predicable but add it anyways.
275 if (TII.isPredicable(MI) || isARMNEONPred(MI))
278 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
279 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
281 if (DefinesOptionalPredicate(MI, &CPSR)) {
290 unsigned ARMFastISel::FastEmitInst_(unsigned MachineInstOpcode,
291 const TargetRegisterClass* RC) {
292 unsigned ResultReg = createResultReg(RC);
293 const MCInstrDesc &II = TII.get(MachineInstOpcode);
295 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg));
299 unsigned ARMFastISel::FastEmitInst_r(unsigned MachineInstOpcode,
300 const TargetRegisterClass *RC,
301 unsigned Op0, bool Op0IsKill) {
302 unsigned ResultReg = createResultReg(RC);
303 const MCInstrDesc &II = TII.get(MachineInstOpcode);
305 if (II.getNumDefs() >= 1) {
306 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
307 .addReg(Op0, Op0IsKill * RegState::Kill));
309 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
310 .addReg(Op0, Op0IsKill * RegState::Kill));
311 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
312 TII.get(TargetOpcode::COPY), ResultReg)
313 .addReg(II.ImplicitDefs[0]));
318 unsigned ARMFastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
319 const TargetRegisterClass *RC,
320 unsigned Op0, bool Op0IsKill,
321 unsigned Op1, bool Op1IsKill) {
322 unsigned ResultReg = createResultReg(RC);
323 const MCInstrDesc &II = TII.get(MachineInstOpcode);
325 if (II.getNumDefs() >= 1) {
326 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
327 .addReg(Op0, Op0IsKill * RegState::Kill)
328 .addReg(Op1, Op1IsKill * RegState::Kill));
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
331 .addReg(Op0, Op0IsKill * RegState::Kill)
332 .addReg(Op1, Op1IsKill * RegState::Kill));
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
334 TII.get(TargetOpcode::COPY), ResultReg)
335 .addReg(II.ImplicitDefs[0]));
340 unsigned ARMFastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
341 const TargetRegisterClass *RC,
342 unsigned Op0, bool Op0IsKill,
343 unsigned Op1, bool Op1IsKill,
344 unsigned Op2, bool Op2IsKill) {
345 unsigned ResultReg = createResultReg(RC);
346 const MCInstrDesc &II = TII.get(MachineInstOpcode);
348 if (II.getNumDefs() >= 1) {
349 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
350 .addReg(Op0, Op0IsKill * RegState::Kill)
351 .addReg(Op1, Op1IsKill * RegState::Kill)
352 .addReg(Op2, Op2IsKill * RegState::Kill));
354 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
355 .addReg(Op0, Op0IsKill * RegState::Kill)
356 .addReg(Op1, Op1IsKill * RegState::Kill)
357 .addReg(Op2, Op2IsKill * RegState::Kill));
358 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
359 TII.get(TargetOpcode::COPY), ResultReg)
360 .addReg(II.ImplicitDefs[0]));
365 unsigned ARMFastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
366 const TargetRegisterClass *RC,
367 unsigned Op0, bool Op0IsKill,
369 unsigned ResultReg = createResultReg(RC);
370 const MCInstrDesc &II = TII.get(MachineInstOpcode);
372 if (II.getNumDefs() >= 1) {
373 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
374 .addReg(Op0, Op0IsKill * RegState::Kill)
377 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
378 .addReg(Op0, Op0IsKill * RegState::Kill)
380 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
381 TII.get(TargetOpcode::COPY), ResultReg)
382 .addReg(II.ImplicitDefs[0]));
387 unsigned ARMFastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
388 const TargetRegisterClass *RC,
389 unsigned Op0, bool Op0IsKill,
390 const ConstantFP *FPImm) {
391 unsigned ResultReg = createResultReg(RC);
392 const MCInstrDesc &II = TII.get(MachineInstOpcode);
394 if (II.getNumDefs() >= 1) {
395 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
396 .addReg(Op0, Op0IsKill * RegState::Kill)
399 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
400 .addReg(Op0, Op0IsKill * RegState::Kill)
402 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
403 TII.get(TargetOpcode::COPY), ResultReg)
404 .addReg(II.ImplicitDefs[0]));
409 unsigned ARMFastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
410 const TargetRegisterClass *RC,
411 unsigned Op0, bool Op0IsKill,
412 unsigned Op1, bool Op1IsKill,
414 unsigned ResultReg = createResultReg(RC);
415 const MCInstrDesc &II = TII.get(MachineInstOpcode);
417 if (II.getNumDefs() >= 1) {
418 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
419 .addReg(Op0, Op0IsKill * RegState::Kill)
420 .addReg(Op1, Op1IsKill * RegState::Kill)
423 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
424 .addReg(Op0, Op0IsKill * RegState::Kill)
425 .addReg(Op1, Op1IsKill * RegState::Kill)
427 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
428 TII.get(TargetOpcode::COPY), ResultReg)
429 .addReg(II.ImplicitDefs[0]));
434 unsigned ARMFastISel::FastEmitInst_i(unsigned MachineInstOpcode,
435 const TargetRegisterClass *RC,
437 unsigned ResultReg = createResultReg(RC);
438 const MCInstrDesc &II = TII.get(MachineInstOpcode);
440 if (II.getNumDefs() >= 1) {
441 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
444 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
446 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
447 TII.get(TargetOpcode::COPY), ResultReg)
448 .addReg(II.ImplicitDefs[0]));
453 unsigned ARMFastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
454 const TargetRegisterClass *RC,
455 uint64_t Imm1, uint64_t Imm2) {
456 unsigned ResultReg = createResultReg(RC);
457 const MCInstrDesc &II = TII.get(MachineInstOpcode);
459 if (II.getNumDefs() >= 1) {
460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
461 .addImm(Imm1).addImm(Imm2));
463 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
464 .addImm(Imm1).addImm(Imm2));
465 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
466 TII.get(TargetOpcode::COPY),
468 .addReg(II.ImplicitDefs[0]));
473 unsigned ARMFastISel::FastEmitInst_extractsubreg(MVT RetVT,
474 unsigned Op0, bool Op0IsKill,
476 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
477 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
478 "Cannot yet extract from physregs");
480 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
481 DL, TII.get(TargetOpcode::COPY), ResultReg)
482 .addReg(Op0, getKillRegState(Op0IsKill), Idx));
486 // TODO: Don't worry about 64-bit now, but when this is fixed remove the
487 // checks from the various callers.
488 unsigned ARMFastISel::ARMMoveToFPReg(EVT VT, unsigned SrcReg) {
489 if (VT == MVT::f64) return 0;
491 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
492 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
493 TII.get(ARM::VMOVSR), MoveReg)
498 unsigned ARMFastISel::ARMMoveToIntReg(EVT VT, unsigned SrcReg) {
499 if (VT == MVT::i64) return 0;
501 unsigned MoveReg = createResultReg(TLI.getRegClassFor(VT));
502 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
503 TII.get(ARM::VMOVRS), MoveReg)
508 // For double width floating point we need to materialize two constants
509 // (the high and the low) into integer registers then use a move to get
510 // the combined constant into an FP reg.
511 unsigned ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, EVT VT) {
512 const APFloat Val = CFP->getValueAPF();
513 bool is64bit = VT == MVT::f64;
515 // This checks to see if we can use VFP3 instructions to materialize
516 // a constant, otherwise we have to go through the constant pool.
517 if (TLI.isFPImmLegal(Val, VT)) {
521 Imm = ARM_AM::getFP64Imm(Val);
524 Imm = ARM_AM::getFP32Imm(Val);
527 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
528 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
534 // Require VFP2 for loading fp constants.
535 if (!Subtarget->hasVFP2()) return false;
537 // MachineConstantPool wants an explicit alignment.
538 unsigned Align = TD.getPrefTypeAlignment(CFP->getType());
540 // TODO: Figure out if this is correct.
541 Align = TD.getTypeAllocSize(CFP->getType());
543 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
544 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
545 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
547 // The extra reg is for addrmode5.
548 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
550 .addConstantPoolIndex(Idx)
555 unsigned ARMFastISel::ARMMaterializeInt(const Constant *C, EVT VT) {
557 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
560 // If we can do this in a single instruction without a constant pool entry
562 const ConstantInt *CI = cast<ConstantInt>(C);
563 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
564 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
565 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
566 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
567 TII.get(Opc), ImmReg)
568 .addImm(CI->getZExtValue()));
572 // Use MVN to emit negative constants.
573 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
574 unsigned Imm = (unsigned)~(CI->getSExtValue());
575 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
576 (ARM_AM::getSOImmVal(Imm) != -1);
578 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
579 unsigned ImmReg = createResultReg(TLI.getRegClassFor(MVT::i32));
580 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
581 TII.get(Opc), ImmReg)
587 // Load from constant pool. For now 32-bit only.
591 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
593 // MachineConstantPool wants an explicit alignment.
594 unsigned Align = TD.getPrefTypeAlignment(C->getType());
596 // TODO: Figure out if this is correct.
597 Align = TD.getTypeAllocSize(C->getType());
599 unsigned Idx = MCP.getConstantPoolIndex(C, Align);
602 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
603 TII.get(ARM::t2LDRpci), DestReg)
604 .addConstantPoolIndex(Idx));
606 // The extra immediate is for addrmode2.
607 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
608 TII.get(ARM::LDRcp), DestReg)
609 .addConstantPoolIndex(Idx)
615 unsigned ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, EVT VT) {
616 // For now 32-bit only.
617 if (VT != MVT::i32) return 0;
619 Reloc::Model RelocM = TM.getRelocationModel();
620 bool IsIndirect = Subtarget->GVIsIndirectSymbol(GV, RelocM);
621 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
623 // Use movw+movt when possible, it avoids constant pool entries.
624 // Darwin targets don't support movt with Reloc::Static, see
625 // ARMTargetLowering::LowerGlobalAddressDarwin. Other targets only support
626 // static movt relocations.
627 if (Subtarget->useMovt() &&
628 Subtarget->isTargetDarwin() == (RelocM != Reloc::Static)) {
632 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
634 case Reloc::DynamicNoPIC:
635 Opc = isThumb2 ? ARM::t2MOV_ga_dyn : ARM::MOV_ga_dyn;
638 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
641 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
642 DestReg).addGlobalAddress(GV));
644 // MachineConstantPool wants an explicit alignment.
645 unsigned Align = TD.getPrefTypeAlignment(GV->getType());
647 // TODO: Figure out if this is correct.
648 Align = TD.getTypeAllocSize(GV->getType());
652 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 :
653 (Subtarget->isThumb() ? 4 : 8);
654 unsigned Id = AFI->createPICLabelUId();
655 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
658 unsigned Idx = MCP.getConstantPoolIndex(CPV, Align);
661 MachineInstrBuilder MIB;
663 unsigned Opc = (RelocM!=Reloc::PIC_) ? ARM::t2LDRpci : ARM::t2LDRpci_pic;
664 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), DestReg)
665 .addConstantPoolIndex(Idx);
666 if (RelocM == Reloc::PIC_)
668 AddOptionalDefs(MIB);
670 // The extra immediate is for addrmode2.
671 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRcp),
673 .addConstantPoolIndex(Idx)
675 AddOptionalDefs(MIB);
677 if (RelocM == Reloc::PIC_) {
678 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
679 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
681 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
682 DL, TII.get(Opc), NewDestReg)
685 AddOptionalDefs(MIB);
692 MachineInstrBuilder MIB;
693 unsigned NewDestReg = createResultReg(TLI.getRegClassFor(VT));
695 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
696 TII.get(ARM::t2LDRi12), NewDestReg)
700 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::LDRi12),
704 DestReg = NewDestReg;
705 AddOptionalDefs(MIB);
711 unsigned ARMFastISel::TargetMaterializeConstant(const Constant *C) {
712 EVT VT = TLI.getValueType(C->getType(), true);
714 // Only handle simple types.
715 if (!VT.isSimple()) return 0;
717 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
718 return ARMMaterializeFP(CFP, VT);
719 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
720 return ARMMaterializeGV(GV, VT);
721 else if (isa<ConstantInt>(C))
722 return ARMMaterializeInt(C, VT);
727 // TODO: unsigned ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
729 unsigned ARMFastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
730 // Don't handle dynamic allocas.
731 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
734 if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
736 DenseMap<const AllocaInst*, int>::iterator SI =
737 FuncInfo.StaticAllocaMap.find(AI);
739 // This will get lowered later into the correct offsets and registers
740 // via rewriteXFrameIndex.
741 if (SI != FuncInfo.StaticAllocaMap.end()) {
742 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
743 unsigned ResultReg = createResultReg(RC);
744 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
745 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
746 TII.get(Opc), ResultReg)
747 .addFrameIndex(SI->second)
755 bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
756 EVT evt = TLI.getValueType(Ty, true);
758 // Only handle simple types.
759 if (evt == MVT::Other || !evt.isSimple()) return false;
760 VT = evt.getSimpleVT();
762 // Handle all legal types, i.e. a register that will directly hold this
764 return TLI.isTypeLegal(VT);
767 bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
768 if (isTypeLegal(Ty, VT)) return true;
770 // If this is a type than can be sign or zero-extended to a basic operation
771 // go ahead and accept it now.
772 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
778 // Computes the address to get to an object.
779 bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
780 // Some boilerplate from the X86 FastISel.
781 const User *U = NULL;
782 unsigned Opcode = Instruction::UserOp1;
783 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
784 // Don't walk into other basic blocks unless the object is an alloca from
785 // another block, otherwise it may not have a virtual register assigned.
786 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
787 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
788 Opcode = I->getOpcode();
791 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
792 Opcode = C->getOpcode();
796 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
797 if (Ty->getAddressSpace() > 255)
798 // Fast instruction selection doesn't support the special
805 case Instruction::BitCast: {
806 // Look through bitcasts.
807 return ARMComputeAddress(U->getOperand(0), Addr);
809 case Instruction::IntToPtr: {
810 // Look past no-op inttoptrs.
811 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
812 return ARMComputeAddress(U->getOperand(0), Addr);
815 case Instruction::PtrToInt: {
816 // Look past no-op ptrtoints.
817 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
818 return ARMComputeAddress(U->getOperand(0), Addr);
821 case Instruction::GetElementPtr: {
822 Address SavedAddr = Addr;
823 int TmpOffset = Addr.Offset;
825 // Iterate through the GEP folding the constants into offsets where
827 gep_type_iterator GTI = gep_type_begin(U);
828 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
829 i != e; ++i, ++GTI) {
830 const Value *Op = *i;
831 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
832 const StructLayout *SL = TD.getStructLayout(STy);
833 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
834 TmpOffset += SL->getElementOffset(Idx);
836 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
838 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
839 // Constant-offset addressing.
840 TmpOffset += CI->getSExtValue() * S;
843 if (isa<AddOperator>(Op) &&
844 (!isa<Instruction>(Op) ||
845 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
847 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
848 // An add (in the same block) with a constant operand. Fold the
851 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
852 TmpOffset += CI->getSExtValue() * S;
853 // Iterate on the other operand.
854 Op = cast<AddOperator>(Op)->getOperand(0);
858 goto unsupported_gep;
863 // Try to grab the base operand now.
864 Addr.Offset = TmpOffset;
865 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
867 // We failed, restore everything and try the other options.
873 case Instruction::Alloca: {
874 const AllocaInst *AI = cast<AllocaInst>(Obj);
875 DenseMap<const AllocaInst*, int>::iterator SI =
876 FuncInfo.StaticAllocaMap.find(AI);
877 if (SI != FuncInfo.StaticAllocaMap.end()) {
878 Addr.BaseType = Address::FrameIndexBase;
879 Addr.Base.FI = SI->second;
886 // Try to get this in a register if nothing else has worked.
887 if (Addr.Base.Reg == 0) Addr.Base.Reg = getRegForValue(Obj);
888 return Addr.Base.Reg != 0;
891 void ARMFastISel::ARMSimplifyAddress(Address &Addr, EVT VT, bool useAM3) {
893 assert(VT.isSimple() && "Non-simple types are invalid here!");
895 bool needsLowering = false;
896 switch (VT.getSimpleVT().SimpleTy) {
897 default: llvm_unreachable("Unhandled load/store type!");
903 // Integer loads/stores handle 12-bit offsets.
904 needsLowering = ((Addr.Offset & 0xfff) != Addr.Offset);
905 // Handle negative offsets.
906 if (needsLowering && isThumb2)
907 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.Offset < 0 &&
910 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
911 needsLowering = (Addr.Offset > 255 || Addr.Offset < -255);
916 // Floating point operands handle 8-bit offsets.
917 needsLowering = ((Addr.Offset & 0xff) != Addr.Offset);
921 // If this is a stack pointer and the offset needs to be simplified then
922 // put the alloca address into a register, set the base type back to
923 // register and continue. This should almost never happen.
924 if (needsLowering && Addr.BaseType == Address::FrameIndexBase) {
925 const TargetRegisterClass *RC = isThumb2 ?
926 (const TargetRegisterClass*)&ARM::tGPRRegClass :
927 (const TargetRegisterClass*)&ARM::GPRRegClass;
928 unsigned ResultReg = createResultReg(RC);
929 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
930 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
931 TII.get(Opc), ResultReg)
932 .addFrameIndex(Addr.Base.FI)
934 Addr.Base.Reg = ResultReg;
935 Addr.BaseType = Address::RegBase;
938 // Since the offset is too large for the load/store instruction
939 // get the reg+offset into a register.
941 Addr.Base.Reg = FastEmit_ri_(MVT::i32, ISD::ADD, Addr.Base.Reg,
942 /*Op0IsKill*/false, Addr.Offset, MVT::i32);
947 void ARMFastISel::AddLoadStoreOperands(EVT VT, Address &Addr,
948 const MachineInstrBuilder &MIB,
949 unsigned Flags, bool useAM3) {
950 // addrmode5 output depends on the selection dag addressing dividing the
951 // offset by 4 that it then later multiplies. Do this here as well.
952 if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
953 VT.getSimpleVT().SimpleTy == MVT::f64)
956 // Frame base works a bit differently. Handle it separately.
957 if (Addr.BaseType == Address::FrameIndexBase) {
958 int FI = Addr.Base.FI;
959 int Offset = Addr.Offset;
960 MachineMemOperand *MMO =
961 FuncInfo.MF->getMachineMemOperand(
962 MachinePointerInfo::getFixedStack(FI, Offset),
964 MFI.getObjectSize(FI),
965 MFI.getObjectAlignment(FI));
966 // Now add the rest of the operands.
967 MIB.addFrameIndex(FI);
969 // ARM halfword load/stores and signed byte loads need an additional
972 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
976 MIB.addImm(Addr.Offset);
978 MIB.addMemOperand(MMO);
980 // Now add the rest of the operands.
981 MIB.addReg(Addr.Base.Reg);
983 // ARM halfword load/stores and signed byte loads need an additional
986 signed Imm = (Addr.Offset < 0) ? (0x100 | -Addr.Offset) : Addr.Offset;
990 MIB.addImm(Addr.Offset);
993 AddOptionalDefs(MIB);
996 bool ARMFastISel::ARMEmitLoad(EVT VT, unsigned &ResultReg, Address &Addr,
997 unsigned Alignment, bool isZExt, bool allocReg) {
998 assert(VT.isSimple() && "Non-simple types are invalid here!");
1000 bool useAM3 = false;
1001 bool needVMOV = false;
1002 const TargetRegisterClass *RC;
1003 switch (VT.getSimpleVT().SimpleTy) {
1004 // This is mostly going to be Neon/vector support.
1005 default: return false;
1009 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1010 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
1012 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
1021 RC = &ARM::GPRRegClass;
1024 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
1028 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1029 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
1031 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
1033 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
1036 RC = &ARM::GPRRegClass;
1040 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1043 Opc = ARM::t2LDRi12;
1047 RC = &ARM::GPRRegClass;
1050 if (!Subtarget->hasVFP2()) return false;
1051 // Unaligned loads need special handling. Floats require word-alignment.
1052 if (Alignment && Alignment < 4) {
1055 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1056 RC = &ARM::GPRRegClass;
1059 RC = TLI.getRegClassFor(VT);
1063 if (!Subtarget->hasVFP2()) return false;
1064 // FIXME: Unaligned loads need special handling. Doublewords require
1066 if (Alignment && Alignment < 4)
1070 RC = TLI.getRegClassFor(VT);
1073 // Simplify this down to something we can handle.
1074 ARMSimplifyAddress(Addr, VT, useAM3);
1076 // Create the base instruction, then add the operands.
1078 ResultReg = createResultReg(RC);
1079 assert (ResultReg > 255 && "Expected an allocated virtual register.");
1080 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1081 TII.get(Opc), ResultReg);
1082 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1084 // If we had an unaligned load of a float we've converted it to an regular
1085 // load. Now we must move from the GRP to the FP register.
1087 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1088 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1089 TII.get(ARM::VMOVSR), MoveReg)
1090 .addReg(ResultReg));
1091 ResultReg = MoveReg;
1096 bool ARMFastISel::SelectLoad(const Instruction *I) {
1097 // Atomic loads need special handling.
1098 if (cast<LoadInst>(I)->isAtomic())
1101 // Verify we have a legal type before going any further.
1103 if (!isLoadTypeLegal(I->getType(), VT))
1106 // See if we can handle this address.
1108 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1111 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
1113 UpdateValueMap(I, ResultReg);
1117 bool ARMFastISel::ARMEmitStore(EVT VT, unsigned SrcReg, Address &Addr,
1118 unsigned Alignment) {
1120 bool useAM3 = false;
1121 switch (VT.getSimpleVT().SimpleTy) {
1122 // This is mostly going to be Neon/vector support.
1123 default: return false;
1125 unsigned Res = createResultReg(isThumb2 ?
1126 (const TargetRegisterClass*)&ARM::tGPRRegClass :
1127 (const TargetRegisterClass*)&ARM::GPRRegClass);
1128 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1129 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1131 .addReg(SrcReg).addImm(1));
1133 } // Fallthrough here.
1136 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1137 StrOpc = ARM::t2STRBi8;
1139 StrOpc = ARM::t2STRBi12;
1141 StrOpc = ARM::STRBi12;
1145 if (Alignment && Alignment < 2 && !Subtarget->allowsUnalignedMem())
1149 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1150 StrOpc = ARM::t2STRHi8;
1152 StrOpc = ARM::t2STRHi12;
1160 if (Addr.Offset < 0 && Addr.Offset > -256 && Subtarget->hasV6T2Ops())
1161 StrOpc = ARM::t2STRi8;
1163 StrOpc = ARM::t2STRi12;
1165 StrOpc = ARM::STRi12;
1169 if (!Subtarget->hasVFP2()) return false;
1170 // Unaligned stores need special handling. Floats require word-alignment.
1171 if (Alignment && Alignment < 4) {
1172 unsigned MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1173 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1174 TII.get(ARM::VMOVRS), MoveReg)
1178 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1180 StrOpc = ARM::VSTRS;
1184 if (!Subtarget->hasVFP2()) return false;
1185 // FIXME: Unaligned stores need special handling. Doublewords require
1187 if (Alignment && Alignment < 4)
1190 StrOpc = ARM::VSTRD;
1193 // Simplify this down to something we can handle.
1194 ARMSimplifyAddress(Addr, VT, useAM3);
1196 // Create the base instruction, then add the operands.
1197 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1200 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1204 bool ARMFastISel::SelectStore(const Instruction *I) {
1205 Value *Op0 = I->getOperand(0);
1206 unsigned SrcReg = 0;
1208 // Atomic stores need special handling.
1209 if (cast<StoreInst>(I)->isAtomic())
1212 // Verify we have a legal type before going any further.
1214 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1217 // Get the value to be stored into a register.
1218 SrcReg = getRegForValue(Op0);
1219 if (SrcReg == 0) return false;
1221 // See if we can handle this address.
1223 if (!ARMComputeAddress(I->getOperand(1), Addr))
1226 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
1231 static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred) {
1233 // Needs two compares...
1234 case CmpInst::FCMP_ONE:
1235 case CmpInst::FCMP_UEQ:
1237 // AL is our "false" for now. The other two need more compares.
1239 case CmpInst::ICMP_EQ:
1240 case CmpInst::FCMP_OEQ:
1242 case CmpInst::ICMP_SGT:
1243 case CmpInst::FCMP_OGT:
1245 case CmpInst::ICMP_SGE:
1246 case CmpInst::FCMP_OGE:
1248 case CmpInst::ICMP_UGT:
1249 case CmpInst::FCMP_UGT:
1251 case CmpInst::FCMP_OLT:
1253 case CmpInst::ICMP_ULE:
1254 case CmpInst::FCMP_OLE:
1256 case CmpInst::FCMP_ORD:
1258 case CmpInst::FCMP_UNO:
1260 case CmpInst::FCMP_UGE:
1262 case CmpInst::ICMP_SLT:
1263 case CmpInst::FCMP_ULT:
1265 case CmpInst::ICMP_SLE:
1266 case CmpInst::FCMP_ULE:
1268 case CmpInst::FCMP_UNE:
1269 case CmpInst::ICMP_NE:
1271 case CmpInst::ICMP_UGE:
1273 case CmpInst::ICMP_ULT:
1278 bool ARMFastISel::SelectBranch(const Instruction *I) {
1279 const BranchInst *BI = cast<BranchInst>(I);
1280 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1281 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1283 // Simple branch support.
1285 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1287 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1288 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1290 // Get the compare predicate.
1291 // Try to take advantage of fallthrough opportunities.
1292 CmpInst::Predicate Predicate = CI->getPredicate();
1293 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1294 std::swap(TBB, FBB);
1295 Predicate = CmpInst::getInversePredicate(Predicate);
1298 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1300 // We may not handle every CC for now.
1301 if (ARMPred == ARMCC::AL) return false;
1303 // Emit the compare.
1304 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1307 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1309 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1310 FastEmitBranch(FBB, DL);
1311 FuncInfo.MBB->addSuccessor(TBB);
1314 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1316 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1317 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1318 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1319 unsigned OpReg = getRegForValue(TI->getOperand(0));
1320 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1322 .addReg(OpReg).addImm(1));
1324 unsigned CCMode = ARMCC::NE;
1325 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1326 std::swap(TBB, FBB);
1330 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1331 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1332 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1334 FastEmitBranch(FBB, DL);
1335 FuncInfo.MBB->addSuccessor(TBB);
1338 } else if (const ConstantInt *CI =
1339 dyn_cast<ConstantInt>(BI->getCondition())) {
1340 uint64_t Imm = CI->getZExtValue();
1341 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1342 FastEmitBranch(Target, DL);
1346 unsigned CmpReg = getRegForValue(BI->getCondition());
1347 if (CmpReg == 0) return false;
1349 // We've been divorced from our compare! Our block was split, and
1350 // now our compare lives in a predecessor block. We musn't
1351 // re-compare here, as the children of the compare aren't guaranteed
1352 // live across the block boundary (we *could* check for this).
1353 // Regardless, the compare has been done in the predecessor block,
1354 // and it left a value for us in a virtual register. Ergo, we test
1355 // the one-bit value left in the virtual register.
1356 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1357 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TstOpc))
1358 .addReg(CmpReg).addImm(1));
1360 unsigned CCMode = ARMCC::NE;
1361 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1362 std::swap(TBB, FBB);
1366 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BrOpc))
1368 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1369 FastEmitBranch(FBB, DL);
1370 FuncInfo.MBB->addSuccessor(TBB);
1374 bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1375 unsigned AddrReg = getRegForValue(I->getOperand(0));
1376 if (AddrReg == 0) return false;
1378 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1379 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc))
1384 bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1386 Type *Ty = Src1Value->getType();
1387 EVT SrcVT = TLI.getValueType(Ty, true);
1388 if (!SrcVT.isSimple()) return false;
1390 bool isFloat = (Ty->isFloatTy() || Ty->isDoubleTy());
1391 if (isFloat && !Subtarget->hasVFP2())
1394 // Check to see if the 2nd operand is a constant that we can encode directly
1397 bool UseImm = false;
1398 bool isNegativeImm = false;
1399 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1400 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1401 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1402 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1404 const APInt &CIVal = ConstInt->getValue();
1405 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1406 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1407 // then a cmn, because there is no way to represent 2147483648 as a
1408 // signed 32-bit int.
1409 if (Imm < 0 && Imm != (int)0x80000000) {
1410 isNegativeImm = true;
1413 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1414 (ARM_AM::getSOImmVal(Imm) != -1);
1416 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1417 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1418 if (ConstFP->isZero() && !ConstFP->isNegative())
1424 bool needsExt = false;
1425 switch (SrcVT.getSimpleVT().SimpleTy) {
1426 default: return false;
1427 // TODO: Verify compares.
1430 CmpOpc = UseImm ? ARM::VCMPEZS : ARM::VCMPES;
1434 CmpOpc = UseImm ? ARM::VCMPEZD : ARM::VCMPED;
1440 // Intentional fall-through.
1444 CmpOpc = ARM::t2CMPrr;
1446 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
1449 CmpOpc = ARM::CMPrr;
1451 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
1456 unsigned SrcReg1 = getRegForValue(Src1Value);
1457 if (SrcReg1 == 0) return false;
1459 unsigned SrcReg2 = 0;
1461 SrcReg2 = getRegForValue(Src2Value);
1462 if (SrcReg2 == 0) return false;
1465 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1467 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1468 if (SrcReg1 == 0) return false;
1470 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1471 if (SrcReg2 == 0) return false;
1476 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1478 .addReg(SrcReg1).addReg(SrcReg2));
1480 MachineInstrBuilder MIB;
1481 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1484 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1487 AddOptionalDefs(MIB);
1490 // For floating point we need to move the result to a comparison register
1491 // that we can then use for branches.
1492 if (Ty->isFloatTy() || Ty->isDoubleTy())
1493 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1494 TII.get(ARM::FMSTAT)));
1498 bool ARMFastISel::SelectCmp(const Instruction *I) {
1499 const CmpInst *CI = cast<CmpInst>(I);
1501 // Get the compare predicate.
1502 ARMCC::CondCodes ARMPred = getComparePred(CI->getPredicate());
1504 // We may not handle every CC for now.
1505 if (ARMPred == ARMCC::AL) return false;
1507 // Emit the compare.
1508 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1511 // Now set a register based on the comparison. Explicitly set the predicates
1513 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1514 const TargetRegisterClass *RC = isThumb2 ?
1515 (const TargetRegisterClass*)&ARM::rGPRRegClass :
1516 (const TargetRegisterClass*)&ARM::GPRRegClass;
1517 unsigned DestReg = createResultReg(RC);
1518 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1519 unsigned ZeroReg = TargetMaterializeConstant(Zero);
1520 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
1521 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), DestReg)
1522 .addReg(ZeroReg).addImm(1)
1523 .addImm(ARMPred).addReg(ARM::CPSR);
1525 UpdateValueMap(I, DestReg);
1529 bool ARMFastISel::SelectFPExt(const Instruction *I) {
1530 // Make sure we have VFP and that we're extending float to double.
1531 if (!Subtarget->hasVFP2()) return false;
1533 Value *V = I->getOperand(0);
1534 if (!I->getType()->isDoubleTy() ||
1535 !V->getType()->isFloatTy()) return false;
1537 unsigned Op = getRegForValue(V);
1538 if (Op == 0) return false;
1540 unsigned Result = createResultReg(&ARM::DPRRegClass);
1541 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1542 TII.get(ARM::VCVTDS), Result)
1544 UpdateValueMap(I, Result);
1548 bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1549 // Make sure we have VFP and that we're truncating double to float.
1550 if (!Subtarget->hasVFP2()) return false;
1552 Value *V = I->getOperand(0);
1553 if (!(I->getType()->isFloatTy() &&
1554 V->getType()->isDoubleTy())) return false;
1556 unsigned Op = getRegForValue(V);
1557 if (Op == 0) return false;
1559 unsigned Result = createResultReg(&ARM::SPRRegClass);
1560 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1561 TII.get(ARM::VCVTSD), Result)
1563 UpdateValueMap(I, Result);
1567 bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1568 // Make sure we have VFP.
1569 if (!Subtarget->hasVFP2()) return false;
1572 Type *Ty = I->getType();
1573 if (!isTypeLegal(Ty, DstVT))
1576 Value *Src = I->getOperand(0);
1577 EVT SrcVT = TLI.getValueType(Src->getType(), true);
1578 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1581 unsigned SrcReg = getRegForValue(Src);
1582 if (SrcReg == 0) return false;
1584 // Handle sign-extension.
1585 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1586 EVT DestVT = MVT::i32;
1587 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT,
1588 /*isZExt*/!isSigned);
1589 if (SrcReg == 0) return false;
1592 // The conversion routine works on fp-reg to fp-reg and the operand above
1593 // was an integer, move it to the fp registers if possible.
1594 unsigned FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1595 if (FP == 0) return false;
1598 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1599 else if (Ty->isDoubleTy()) Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1602 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1603 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1606 UpdateValueMap(I, ResultReg);
1610 bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1611 // Make sure we have VFP.
1612 if (!Subtarget->hasVFP2()) return false;
1615 Type *RetTy = I->getType();
1616 if (!isTypeLegal(RetTy, DstVT))
1619 unsigned Op = getRegForValue(I->getOperand(0));
1620 if (Op == 0) return false;
1623 Type *OpTy = I->getOperand(0)->getType();
1624 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1625 else if (OpTy->isDoubleTy()) Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1628 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1629 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1630 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc),
1634 // This result needs to be in an integer register, but the conversion only
1635 // takes place in fp-regs.
1636 unsigned IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1637 if (IntReg == 0) return false;
1639 UpdateValueMap(I, IntReg);
1643 bool ARMFastISel::SelectSelect(const Instruction *I) {
1645 if (!isTypeLegal(I->getType(), VT))
1648 // Things need to be register sized for register moves.
1649 if (VT != MVT::i32) return false;
1650 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1652 unsigned CondReg = getRegForValue(I->getOperand(0));
1653 if (CondReg == 0) return false;
1654 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1655 if (Op1Reg == 0) return false;
1657 // Check to see if we can use an immediate in the conditional move.
1659 bool UseImm = false;
1660 bool isNegativeImm = false;
1661 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1662 assert (VT == MVT::i32 && "Expecting an i32.");
1663 Imm = (int)ConstInt->getValue().getZExtValue();
1665 isNegativeImm = true;
1668 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1669 (ARM_AM::getSOImmVal(Imm) != -1);
1672 unsigned Op2Reg = 0;
1674 Op2Reg = getRegForValue(I->getOperand(2));
1675 if (Op2Reg == 0) return false;
1678 unsigned CmpOpc = isThumb2 ? ARM::t2CMPri : ARM::CMPri;
1679 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CmpOpc))
1680 .addReg(CondReg).addImm(0));
1684 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1686 if (!isNegativeImm) {
1687 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1689 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1692 unsigned ResultReg = createResultReg(RC);
1694 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1695 .addReg(Op2Reg).addReg(Op1Reg).addImm(ARMCC::NE).addReg(ARM::CPSR);
1697 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovCCOpc), ResultReg)
1698 .addReg(Op1Reg).addImm(Imm).addImm(ARMCC::EQ).addReg(ARM::CPSR);
1699 UpdateValueMap(I, ResultReg);
1703 bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1705 Type *Ty = I->getType();
1706 if (!isTypeLegal(Ty, VT))
1709 // If we have integer div support we should have selected this automagically.
1710 // In case we have a real miss go ahead and return false and we'll pick
1712 if (Subtarget->hasDivide()) return false;
1714 // Otherwise emit a libcall.
1715 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1717 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1718 else if (VT == MVT::i16)
1719 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1720 else if (VT == MVT::i32)
1721 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1722 else if (VT == MVT::i64)
1723 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1724 else if (VT == MVT::i128)
1725 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1726 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1728 return ARMEmitLibcall(I, LC);
1731 bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1733 Type *Ty = I->getType();
1734 if (!isTypeLegal(Ty, VT))
1737 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1739 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1740 else if (VT == MVT::i16)
1741 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1742 else if (VT == MVT::i32)
1743 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1744 else if (VT == MVT::i64)
1745 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1746 else if (VT == MVT::i128)
1747 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1748 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1750 return ARMEmitLibcall(I, LC);
1753 bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1754 EVT DestVT = TLI.getValueType(I->getType(), true);
1756 // We can get here in the case when we have a binary operation on a non-legal
1757 // type and the target independent selector doesn't know how to handle it.
1758 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1762 switch (ISDOpcode) {
1763 default: return false;
1765 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1768 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1771 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1775 unsigned SrcReg1 = getRegForValue(I->getOperand(0));
1776 if (SrcReg1 == 0) return false;
1778 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1779 // in the instruction, rather then materializing the value in a register.
1780 unsigned SrcReg2 = getRegForValue(I->getOperand(1));
1781 if (SrcReg2 == 0) return false;
1783 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1784 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1785 TII.get(Opc), ResultReg)
1786 .addReg(SrcReg1).addReg(SrcReg2));
1787 UpdateValueMap(I, ResultReg);
1791 bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1792 EVT VT = TLI.getValueType(I->getType(), true);
1794 // We can get here in the case when we want to use NEON for our fp
1795 // operations, but can't figure out how to. Just use the vfp instructions
1797 // FIXME: It'd be nice to use NEON instructions.
1798 Type *Ty = I->getType();
1799 bool isFloat = (Ty->isDoubleTy() || Ty->isFloatTy());
1800 if (isFloat && !Subtarget->hasVFP2())
1804 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1805 switch (ISDOpcode) {
1806 default: return false;
1808 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1811 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1814 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1817 unsigned Op1 = getRegForValue(I->getOperand(0));
1818 if (Op1 == 0) return false;
1820 unsigned Op2 = getRegForValue(I->getOperand(1));
1821 if (Op2 == 0) return false;
1823 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1824 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1825 TII.get(Opc), ResultReg)
1826 .addReg(Op1).addReg(Op2));
1827 UpdateValueMap(I, ResultReg);
1831 // Call Handling Code
1833 // This is largely taken directly from CCAssignFnForNode
1834 // TODO: We may not support all of this.
1835 CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1840 llvm_unreachable("Unsupported calling convention");
1841 case CallingConv::Fast:
1842 if (Subtarget->hasVFP2() && !isVarArg) {
1843 if (!Subtarget->isAAPCS_ABI())
1844 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1845 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1846 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1849 case CallingConv::C:
1850 // Use target triple & subtarget features to do actual dispatch.
1851 if (Subtarget->isAAPCS_ABI()) {
1852 if (Subtarget->hasVFP2() &&
1853 TM.Options.FloatABIType == FloatABI::Hard && !isVarArg)
1854 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1856 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1858 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1859 case CallingConv::ARM_AAPCS_VFP:
1861 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1862 // Fall through to soft float variant, variadic functions don't
1863 // use hard floating point ABI.
1864 case CallingConv::ARM_AAPCS:
1865 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1866 case CallingConv::ARM_APCS:
1867 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1868 case CallingConv::GHC:
1870 llvm_unreachable("Can't return in GHC call convention");
1872 return CC_ARM_APCS_GHC;
1876 bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1877 SmallVectorImpl<unsigned> &ArgRegs,
1878 SmallVectorImpl<MVT> &ArgVTs,
1879 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1880 SmallVectorImpl<unsigned> &RegArgs,
1884 SmallVector<CCValAssign, 16> ArgLocs;
1885 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs, *Context);
1886 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags,
1887 CCAssignFnForCall(CC, false, isVarArg));
1889 // Check that we can handle all of the arguments. If we can't, then bail out
1890 // now before we add code to the MBB.
1891 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1892 CCValAssign &VA = ArgLocs[i];
1893 MVT ArgVT = ArgVTs[VA.getValNo()];
1895 // We don't handle NEON/vector parameters yet.
1896 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1899 // Now copy/store arg to correct locations.
1900 if (VA.isRegLoc() && !VA.needsCustom()) {
1902 } else if (VA.needsCustom()) {
1903 // TODO: We need custom lowering for vector (v2f64) args.
1904 if (VA.getLocVT() != MVT::f64 ||
1905 // TODO: Only handle register args for now.
1906 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1909 switch (static_cast<EVT>(ArgVT).getSimpleVT().SimpleTy) {
1918 if (!Subtarget->hasVFP2())
1922 if (!Subtarget->hasVFP2())
1929 // At the point, we are able to handle the call's arguments in fast isel.
1931 // Get a count of how many bytes are to be pushed on the stack.
1932 NumBytes = CCInfo.getNextStackOffset();
1934 // Issue CALLSEQ_START
1935 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1936 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1937 TII.get(AdjStackDown))
1940 // Process the args.
1941 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1942 CCValAssign &VA = ArgLocs[i];
1943 unsigned Arg = ArgRegs[VA.getValNo()];
1944 MVT ArgVT = ArgVTs[VA.getValNo()];
1946 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
1947 "We don't handle NEON/vector parameters yet.");
1949 // Handle arg promotion, etc.
1950 switch (VA.getLocInfo()) {
1951 case CCValAssign::Full: break;
1952 case CCValAssign::SExt: {
1953 MVT DestVT = VA.getLocVT();
1954 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
1955 assert (Arg != 0 && "Failed to emit a sext");
1959 case CCValAssign::AExt:
1960 // Intentional fall-through. Handle AExt and ZExt.
1961 case CCValAssign::ZExt: {
1962 MVT DestVT = VA.getLocVT();
1963 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
1964 assert (Arg != 0 && "Failed to emit a sext");
1968 case CCValAssign::BCvt: {
1969 unsigned BC = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg,
1970 /*TODO: Kill=*/false);
1971 assert(BC != 0 && "Failed to emit a bitcast!");
1973 ArgVT = VA.getLocVT();
1976 default: llvm_unreachable("Unknown arg promotion!");
1979 // Now copy/store arg to correct locations.
1980 if (VA.isRegLoc() && !VA.needsCustom()) {
1981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1984 RegArgs.push_back(VA.getLocReg());
1985 } else if (VA.needsCustom()) {
1986 // TODO: We need custom lowering for vector (v2f64) args.
1987 assert(VA.getLocVT() == MVT::f64 &&
1988 "Custom lowering for v2f64 args not available");
1990 CCValAssign &NextVA = ArgLocs[++i];
1992 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
1993 "We only handle register args!");
1995 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1996 TII.get(ARM::VMOVRRD), VA.getLocReg())
1997 .addReg(NextVA.getLocReg(), RegState::Define)
1999 RegArgs.push_back(VA.getLocReg());
2000 RegArgs.push_back(NextVA.getLocReg());
2002 assert(VA.isMemLoc());
2003 // Need to store on the stack.
2005 Addr.BaseType = Address::RegBase;
2006 Addr.Base.Reg = ARM::SP;
2007 Addr.Offset = VA.getLocMemOffset();
2009 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2010 assert(EmitRet && "Could not emit a store for argument!");
2017 bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
2018 const Instruction *I, CallingConv::ID CC,
2019 unsigned &NumBytes, bool isVarArg) {
2020 // Issue CALLSEQ_END
2021 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2022 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2023 TII.get(AdjStackUp))
2024 .addImm(NumBytes).addImm(0));
2026 // Now the return value.
2027 if (RetVT != MVT::isVoid) {
2028 SmallVector<CCValAssign, 16> RVLocs;
2029 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2030 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2032 // Copy all of the result registers out of their specified physreg.
2033 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
2034 // For this move we copy into two registers and then move into the
2035 // double fp reg we want.
2036 EVT DestVT = RVLocs[0].getValVT();
2037 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
2038 unsigned ResultReg = createResultReg(DstRC);
2039 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2040 TII.get(ARM::VMOVDRR), ResultReg)
2041 .addReg(RVLocs[0].getLocReg())
2042 .addReg(RVLocs[1].getLocReg()));
2044 UsedRegs.push_back(RVLocs[0].getLocReg());
2045 UsedRegs.push_back(RVLocs[1].getLocReg());
2047 // Finally update the result.
2048 UpdateValueMap(I, ResultReg);
2050 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
2051 EVT CopyVT = RVLocs[0].getValVT();
2053 // Special handling for extended integers.
2054 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2057 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
2059 unsigned ResultReg = createResultReg(DstRC);
2060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2061 ResultReg).addReg(RVLocs[0].getLocReg());
2062 UsedRegs.push_back(RVLocs[0].getLocReg());
2064 // Finally update the result.
2065 UpdateValueMap(I, ResultReg);
2072 bool ARMFastISel::SelectRet(const Instruction *I) {
2073 const ReturnInst *Ret = cast<ReturnInst>(I);
2074 const Function &F = *I->getParent()->getParent();
2076 if (!FuncInfo.CanLowerReturn)
2079 CallingConv::ID CC = F.getCallingConv();
2080 if (Ret->getNumOperands() > 0) {
2081 SmallVector<ISD::OutputArg, 4> Outs;
2082 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
2085 // Analyze operands of the call, assigning locations to each operand.
2086 SmallVector<CCValAssign, 16> ValLocs;
2087 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,I->getContext());
2088 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2091 const Value *RV = Ret->getOperand(0);
2092 unsigned Reg = getRegForValue(RV);
2096 // Only handle a single return value for now.
2097 if (ValLocs.size() != 1)
2100 CCValAssign &VA = ValLocs[0];
2102 // Don't bother handling odd stuff for now.
2103 if (VA.getLocInfo() != CCValAssign::Full)
2105 // Only handle register returns for now.
2109 unsigned SrcReg = Reg + VA.getValNo();
2110 EVT RVVT = TLI.getValueType(RV->getType());
2111 EVT DestVT = VA.getValVT();
2112 // Special handling for extended integers.
2113 if (RVVT != DestVT) {
2114 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2117 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2119 // Perform extension if flagged as either zext or sext. Otherwise, do
2121 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2122 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2123 if (SrcReg == 0) return false;
2128 unsigned DstReg = VA.getLocReg();
2129 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2130 // Avoid a cross-class copy. This is very unlikely.
2131 if (!SrcRC->contains(DstReg))
2133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2134 DstReg).addReg(SrcReg);
2136 // Mark the register as live out of the function.
2137 MRI.addLiveOut(VA.getLocReg());
2140 unsigned RetOpc = isThumb2 ? ARM::tBX_RET : ARM::BX_RET;
2141 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2146 unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2148 return isThumb2 ? ARM::tBLXr : ARM::BLX;
2150 return isThumb2 ? ARM::tBL : ARM::BL;
2153 unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
2154 GlobalValue *GV = new GlobalVariable(Type::getInt32Ty(*Context), false,
2155 GlobalValue::ExternalLinkage, 0, Name);
2156 return ARMMaterializeGV(GV, TLI.getValueType(GV->getType()));
2159 // A quick function that will emit a call for a named libcall in F with the
2160 // vector of passed arguments for the Instruction in I. We can assume that we
2161 // can emit a call for any libcall we can produce. This is an abridged version
2162 // of the full call infrastructure since we won't need to worry about things
2163 // like computed function pointers or strange arguments at call sites.
2164 // TODO: Try to unify this and the normal call bits for ARM, then try to unify
2166 bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2167 CallingConv::ID CC = TLI.getLibcallCallingConv(Call);
2169 // Handle *simple* calls for now.
2170 Type *RetTy = I->getType();
2172 if (RetTy->isVoidTy())
2173 RetVT = MVT::isVoid;
2174 else if (!isTypeLegal(RetTy, RetVT))
2177 // Can't handle non-double multi-reg retvals.
2178 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
2179 SmallVector<CCValAssign, 16> RVLocs;
2180 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
2181 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, false));
2182 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2186 // Set up the argument vectors.
2187 SmallVector<Value*, 8> Args;
2188 SmallVector<unsigned, 8> ArgRegs;
2189 SmallVector<MVT, 8> ArgVTs;
2190 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2191 Args.reserve(I->getNumOperands());
2192 ArgRegs.reserve(I->getNumOperands());
2193 ArgVTs.reserve(I->getNumOperands());
2194 ArgFlags.reserve(I->getNumOperands());
2195 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
2196 Value *Op = I->getOperand(i);
2197 unsigned Arg = getRegForValue(Op);
2198 if (Arg == 0) return false;
2200 Type *ArgTy = Op->getType();
2202 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2204 ISD::ArgFlagsTy Flags;
2205 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2206 Flags.setOrigAlign(OriginalAlignment);
2209 ArgRegs.push_back(Arg);
2210 ArgVTs.push_back(ArgVT);
2211 ArgFlags.push_back(Flags);
2214 // Handle the arguments now that we've gotten them.
2215 SmallVector<unsigned, 4> RegArgs;
2217 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2218 RegArgs, CC, NumBytes, false))
2221 unsigned CalleeReg = 0;
2222 if (EnableARMLongCalls) {
2223 CalleeReg = getLibcallReg(TLI.getLibcallName(Call));
2224 if (CalleeReg == 0) return false;
2228 unsigned CallOpc = ARMSelectCallOp(EnableARMLongCalls);
2229 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2230 DL, TII.get(CallOpc));
2231 // BL / BLX don't take a predicate, but tBL / tBLX do.
2233 AddDefaultPred(MIB);
2234 if (EnableARMLongCalls)
2235 MIB.addReg(CalleeReg);
2237 MIB.addExternalSymbol(TLI.getLibcallName(Call));
2239 // Add implicit physical register uses to the call.
2240 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2241 MIB.addReg(RegArgs[i], RegState::Implicit);
2243 // Add a register mask with the call-preserved registers.
2244 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2245 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2247 // Finish off the call including any return values.
2248 SmallVector<unsigned, 4> UsedRegs;
2249 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
2251 // Set all unused physreg defs as dead.
2252 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2257 bool ARMFastISel::SelectCall(const Instruction *I,
2258 const char *IntrMemName = 0) {
2259 const CallInst *CI = cast<CallInst>(I);
2260 const Value *Callee = CI->getCalledValue();
2262 // Can't handle inline asm.
2263 if (isa<InlineAsm>(Callee)) return false;
2265 // Check the calling convention.
2266 ImmutableCallSite CS(CI);
2267 CallingConv::ID CC = CS.getCallingConv();
2269 // TODO: Avoid some calling conventions?
2271 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2272 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
2273 bool isVarArg = FTy->isVarArg();
2275 // Handle *simple* calls for now.
2276 Type *RetTy = I->getType();
2278 if (RetTy->isVoidTy())
2279 RetVT = MVT::isVoid;
2280 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2281 RetVT != MVT::i8 && RetVT != MVT::i1)
2284 // Can't handle non-double multi-reg retvals.
2285 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2286 RetVT != MVT::i16 && RetVT != MVT::i32) {
2287 SmallVector<CCValAssign, 16> RVLocs;
2288 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, RVLocs, *Context);
2289 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC, true, isVarArg));
2290 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2294 // Set up the argument vectors.
2295 SmallVector<Value*, 8> Args;
2296 SmallVector<unsigned, 8> ArgRegs;
2297 SmallVector<MVT, 8> ArgVTs;
2298 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
2299 unsigned arg_size = CS.arg_size();
2300 Args.reserve(arg_size);
2301 ArgRegs.reserve(arg_size);
2302 ArgVTs.reserve(arg_size);
2303 ArgFlags.reserve(arg_size);
2304 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
2306 // If we're lowering a memory intrinsic instead of a regular call, skip the
2307 // last two arguments, which shouldn't be passed to the underlying function.
2308 if (IntrMemName && e-i <= 2)
2311 ISD::ArgFlagsTy Flags;
2312 unsigned AttrInd = i - CS.arg_begin() + 1;
2313 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
2315 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
2318 // FIXME: Only handle *easy* calls for now.
2319 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
2320 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
2321 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
2322 CS.paramHasAttr(AttrInd, Attribute::ByVal))
2325 Type *ArgTy = (*i)->getType();
2327 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2331 unsigned Arg = getRegForValue(*i);
2335 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2336 Flags.setOrigAlign(OriginalAlignment);
2339 ArgRegs.push_back(Arg);
2340 ArgVTs.push_back(ArgVT);
2341 ArgFlags.push_back(Flags);
2344 // Handle the arguments now that we've gotten them.
2345 SmallVector<unsigned, 4> RegArgs;
2347 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2348 RegArgs, CC, NumBytes, isVarArg))
2351 bool UseReg = false;
2352 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2353 if (!GV || EnableARMLongCalls) UseReg = true;
2355 unsigned CalleeReg = 0;
2358 CalleeReg = getLibcallReg(IntrMemName);
2360 CalleeReg = getRegForValue(Callee);
2362 if (CalleeReg == 0) return false;
2366 unsigned CallOpc = ARMSelectCallOp(UseReg);
2367 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2368 DL, TII.get(CallOpc));
2370 // ARM calls don't take a predicate, but tBL / tBLX do.
2372 AddDefaultPred(MIB);
2374 MIB.addReg(CalleeReg);
2375 else if (!IntrMemName)
2376 MIB.addGlobalAddress(GV, 0, 0);
2378 MIB.addExternalSymbol(IntrMemName, 0);
2380 // Add implicit physical register uses to the call.
2381 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2382 MIB.addReg(RegArgs[i], RegState::Implicit);
2384 // Add a register mask with the call-preserved registers.
2385 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2386 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2388 // Finish off the call including any return values.
2389 SmallVector<unsigned, 4> UsedRegs;
2390 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2393 // Set all unused physreg defs as dead.
2394 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2399 bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2403 bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src,
2405 // Make sure we don't bloat code by inlining very large memcpy's.
2406 if (!ARMIsMemCpySmall(Len))
2409 // We don't care about alignment here since we just emit integer accesses.
2423 RV = ARMEmitLoad(VT, ResultReg, Src);
2424 assert (RV == true && "Should be able to handle this load.");
2425 RV = ARMEmitStore(VT, ResultReg, Dest);
2426 assert (RV == true && "Should be able to handle this store.");
2429 unsigned Size = VT.getSizeInBits()/8;
2431 Dest.Offset += Size;
2438 bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2439 // FIXME: Handle more intrinsics.
2440 switch (I.getIntrinsicID()) {
2441 default: return false;
2442 case Intrinsic::frameaddress: {
2443 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2444 MFI->setFrameAddressIsTaken(true);
2447 const TargetRegisterClass *RC;
2449 LdrOpc = ARM::t2LDRi12;
2450 RC = (const TargetRegisterClass*)&ARM::tGPRRegClass;
2452 LdrOpc = ARM::LDRi12;
2453 RC = (const TargetRegisterClass*)&ARM::GPRRegClass;
2456 const ARMBaseRegisterInfo *RegInfo =
2457 static_cast<const ARMBaseRegisterInfo*>(TM.getRegisterInfo());
2458 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2459 unsigned SrcReg = FramePtr;
2461 // Recursively load frame address
2467 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2469 DestReg = createResultReg(RC);
2470 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2471 TII.get(LdrOpc), DestReg)
2472 .addReg(SrcReg).addImm(0));
2475 UpdateValueMap(&I, SrcReg);
2478 case Intrinsic::memcpy:
2479 case Intrinsic::memmove: {
2480 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2481 // Don't handle volatile.
2482 if (MTI.isVolatile())
2485 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2486 // we would emit dead code because we don't currently handle memmoves.
2487 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2488 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2489 // Small memcpy's are common enough that we want to do them without a call
2491 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2492 if (ARMIsMemCpySmall(Len)) {
2494 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2495 !ARMComputeAddress(MTI.getRawSource(), Src))
2497 if (ARMTryEmitSmallMemCpy(Dest, Src, Len))
2502 if (!MTI.getLength()->getType()->isIntegerTy(32))
2505 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2508 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2509 return SelectCall(&I, IntrMemName);
2511 case Intrinsic::memset: {
2512 const MemSetInst &MSI = cast<MemSetInst>(I);
2513 // Don't handle volatile.
2514 if (MSI.isVolatile())
2517 if (!MSI.getLength()->getType()->isIntegerTy(32))
2520 if (MSI.getDestAddressSpace() > 255)
2523 return SelectCall(&I, "memset");
2525 case Intrinsic::trap: {
2526 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(ARM::TRAP));
2532 bool ARMFastISel::SelectTrunc(const Instruction *I) {
2533 // The high bits for a type smaller than the register size are assumed to be
2535 Value *Op = I->getOperand(0);
2538 SrcVT = TLI.getValueType(Op->getType(), true);
2539 DestVT = TLI.getValueType(I->getType(), true);
2541 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2543 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2546 unsigned SrcReg = getRegForValue(Op);
2547 if (!SrcReg) return false;
2549 // Because the high bits are undefined, a truncate doesn't generate
2551 UpdateValueMap(I, SrcReg);
2555 unsigned ARMFastISel::ARMEmitIntExt(EVT SrcVT, unsigned SrcReg, EVT DestVT,
2557 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2561 bool isBoolZext = false;
2562 if (!SrcVT.isSimple()) return 0;
2563 switch (SrcVT.getSimpleVT().SimpleTy) {
2566 if (!Subtarget->hasV6Ops()) return 0;
2568 Opc = isThumb2 ? ARM::t2UXTH : ARM::UXTH;
2570 Opc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
2573 if (!Subtarget->hasV6Ops()) return 0;
2575 Opc = isThumb2 ? ARM::t2UXTB : ARM::UXTB;
2577 Opc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
2581 Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
2588 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2589 MachineInstrBuilder MIB;
2590 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
2596 AddOptionalDefs(MIB);
2600 bool ARMFastISel::SelectIntExt(const Instruction *I) {
2601 // On ARM, in general, integer casts don't involve legal types; this code
2602 // handles promotable integers.
2603 Type *DestTy = I->getType();
2604 Value *Src = I->getOperand(0);
2605 Type *SrcTy = Src->getType();
2608 SrcVT = TLI.getValueType(SrcTy, true);
2609 DestVT = TLI.getValueType(DestTy, true);
2611 bool isZExt = isa<ZExtInst>(I);
2612 unsigned SrcReg = getRegForValue(Src);
2613 if (!SrcReg) return false;
2615 unsigned ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2616 if (ResultReg == 0) return false;
2617 UpdateValueMap(I, ResultReg);
2621 bool ARMFastISel::SelectShift(const Instruction *I,
2622 ARM_AM::ShiftOpc ShiftTy) {
2623 // We handle thumb2 mode by target independent selector
2624 // or SelectionDAG ISel.
2628 // Only handle i32 now.
2629 EVT DestVT = TLI.getValueType(I->getType(), true);
2630 if (DestVT != MVT::i32)
2633 unsigned Opc = ARM::MOVsr;
2635 Value *Src2Value = I->getOperand(1);
2636 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2637 ShiftImm = CI->getZExtValue();
2639 // Fall back to selection DAG isel if the shift amount
2640 // is zero or greater than the width of the value type.
2641 if (ShiftImm == 0 || ShiftImm >=32)
2647 Value *Src1Value = I->getOperand(0);
2648 unsigned Reg1 = getRegForValue(Src1Value);
2649 if (Reg1 == 0) return false;
2652 if (Opc == ARM::MOVsr) {
2653 Reg2 = getRegForValue(Src2Value);
2654 if (Reg2 == 0) return false;
2657 unsigned ResultReg = createResultReg(TLI.getRegClassFor(MVT::i32));
2658 if(ResultReg == 0) return false;
2660 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2661 TII.get(Opc), ResultReg)
2664 if (Opc == ARM::MOVsi)
2665 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2666 else if (Opc == ARM::MOVsr) {
2668 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2671 AddOptionalDefs(MIB);
2672 UpdateValueMap(I, ResultReg);
2676 // TODO: SoftFP support.
2677 bool ARMFastISel::TargetSelectInstruction(const Instruction *I) {
2679 switch (I->getOpcode()) {
2680 case Instruction::Load:
2681 return SelectLoad(I);
2682 case Instruction::Store:
2683 return SelectStore(I);
2684 case Instruction::Br:
2685 return SelectBranch(I);
2686 case Instruction::IndirectBr:
2687 return SelectIndirectBr(I);
2688 case Instruction::ICmp:
2689 case Instruction::FCmp:
2690 return SelectCmp(I);
2691 case Instruction::FPExt:
2692 return SelectFPExt(I);
2693 case Instruction::FPTrunc:
2694 return SelectFPTrunc(I);
2695 case Instruction::SIToFP:
2696 return SelectIToFP(I, /*isSigned*/ true);
2697 case Instruction::UIToFP:
2698 return SelectIToFP(I, /*isSigned*/ false);
2699 case Instruction::FPToSI:
2700 return SelectFPToI(I, /*isSigned*/ true);
2701 case Instruction::FPToUI:
2702 return SelectFPToI(I, /*isSigned*/ false);
2703 case Instruction::Add:
2704 return SelectBinaryIntOp(I, ISD::ADD);
2705 case Instruction::Or:
2706 return SelectBinaryIntOp(I, ISD::OR);
2707 case Instruction::Sub:
2708 return SelectBinaryIntOp(I, ISD::SUB);
2709 case Instruction::FAdd:
2710 return SelectBinaryFPOp(I, ISD::FADD);
2711 case Instruction::FSub:
2712 return SelectBinaryFPOp(I, ISD::FSUB);
2713 case Instruction::FMul:
2714 return SelectBinaryFPOp(I, ISD::FMUL);
2715 case Instruction::SDiv:
2716 return SelectDiv(I, /*isSigned*/ true);
2717 case Instruction::UDiv:
2718 return SelectDiv(I, /*isSigned*/ false);
2719 case Instruction::SRem:
2720 return SelectRem(I, /*isSigned*/ true);
2721 case Instruction::URem:
2722 return SelectRem(I, /*isSigned*/ false);
2723 case Instruction::Call:
2724 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2725 return SelectIntrinsicCall(*II);
2726 return SelectCall(I);
2727 case Instruction::Select:
2728 return SelectSelect(I);
2729 case Instruction::Ret:
2730 return SelectRet(I);
2731 case Instruction::Trunc:
2732 return SelectTrunc(I);
2733 case Instruction::ZExt:
2734 case Instruction::SExt:
2735 return SelectIntExt(I);
2736 case Instruction::Shl:
2737 return SelectShift(I, ARM_AM::lsl);
2738 case Instruction::LShr:
2739 return SelectShift(I, ARM_AM::lsr);
2740 case Instruction::AShr:
2741 return SelectShift(I, ARM_AM::asr);
2747 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2748 /// vreg is being provided by the specified load instruction. If possible,
2749 /// try to fold the load as an operand to the instruction, returning true if
2751 bool ARMFastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2752 const LoadInst *LI) {
2753 // Verify we have a legal type before going any further.
2755 if (!isLoadTypeLegal(LI->getType(), VT))
2758 // Combine load followed by zero- or sign-extend.
2759 // ldrb r1, [r0] ldrb r1, [r0]
2761 // mov r3, r2 mov r3, r1
2763 switch(MI->getOpcode()) {
2764 default: return false;
2782 // See if we can handle this address.
2784 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
2786 unsigned ResultReg = MI->getOperand(0).getReg();
2787 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlignment(), isZExt, false))
2789 MI->eraseFromParent();
2794 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
2795 const TargetLibraryInfo *libInfo) {
2796 // Completely untested on non-iOS.
2797 const TargetMachine &TM = funcInfo.MF->getTarget();
2799 // Darwin and thumb1 only for now.
2800 const ARMSubtarget *Subtarget = &TM.getSubtarget<ARMSubtarget>();
2801 if (Subtarget->isTargetIOS() && !Subtarget->isThumb1Only())
2802 return new ARMFastISel(funcInfo, libInfo);