1 //===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that ARM uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "ARMAddressingModes.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMISelLowering.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMRegisterInfo.h"
21 #include "ARMSubtarget.h"
22 #include "ARMTargetMachine.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Instruction.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/SelectionDAG.h"
32 #include "llvm/CodeGen/SSARegMap.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/ADT/VectorExtras.h"
35 #include "llvm/Support/MathExtras.h"
38 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
39 : TargetLowering(TM), ARMPCLabelIndex(0) {
40 Subtarget = &TM.getSubtarget<ARMSubtarget>();
42 if (Subtarget->isTargetDarwin()) {
44 setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL);
45 setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL);
47 // Uses VFP for Thumb libfuncs if available.
48 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
49 // Single-precision floating-point arithmetic.
50 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
51 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
52 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
53 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
55 // Double-precision floating-point arithmetic.
56 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
57 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
58 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
59 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
61 // Single-precision comparisons.
62 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
63 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
64 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
65 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
66 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
67 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
68 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
69 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
71 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
72 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
76 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
77 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
78 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
80 // Double-precision comparisons.
81 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
82 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
83 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
84 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
85 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
86 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
87 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
88 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
90 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
97 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
99 // Floating-point to integer conversions.
100 // i64 conversions are done via library routines even when generating VFP
101 // instructions, so use the same ones.
102 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
103 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
104 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
105 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
107 // Conversions between floating types.
108 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
109 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
111 // Integer to floating-point conversions.
112 // i64 conversions are done via library routines even when generating VFP
113 // instructions, so use the same ones.
114 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
115 // __floatunsidf vs. __floatunssidfvfp.
116 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
117 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
118 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
119 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
123 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
124 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
125 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
126 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
128 computeRegisterProperties();
130 // ARM does not have f32 extending load.
131 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
133 // ARM supports all 4 flavors of integer indexed load / store.
134 for (unsigned im = (unsigned)ISD::PRE_INC;
135 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
136 setIndexedLoadAction(im, MVT::i1, Legal);
137 setIndexedLoadAction(im, MVT::i8, Legal);
138 setIndexedLoadAction(im, MVT::i16, Legal);
139 setIndexedLoadAction(im, MVT::i32, Legal);
140 setIndexedStoreAction(im, MVT::i1, Legal);
141 setIndexedStoreAction(im, MVT::i8, Legal);
142 setIndexedStoreAction(im, MVT::i16, Legal);
143 setIndexedStoreAction(im, MVT::i32, Legal);
146 // i64 operation support.
147 if (Subtarget->isThumb()) {
148 setOperationAction(ISD::MUL, MVT::i64, Expand);
149 setOperationAction(ISD::MULHU, MVT::i32, Expand);
150 setOperationAction(ISD::MULHS, MVT::i32, Expand);
151 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
152 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
154 setOperationAction(ISD::MUL, MVT::i64, Expand);
155 setOperationAction(ISD::MULHU, MVT::i32, Expand);
156 if (!Subtarget->hasV6Ops())
157 setOperationAction(ISD::MULHS, MVT::i32, Expand);
159 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
160 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
161 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
162 setOperationAction(ISD::SRL, MVT::i64, Custom);
163 setOperationAction(ISD::SRA, MVT::i64, Custom);
165 // ARM does not have ROTL.
166 setOperationAction(ISD::ROTL, MVT::i32, Expand);
167 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
168 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
169 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
170 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
172 // Only ARMv6 has BSWAP.
173 if (!Subtarget->hasV6Ops())
174 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
176 // These are expanded into libcalls.
177 setOperationAction(ISD::SDIV, MVT::i32, Expand);
178 setOperationAction(ISD::UDIV, MVT::i32, Expand);
179 setOperationAction(ISD::SREM, MVT::i32, Expand);
180 setOperationAction(ISD::UREM, MVT::i32, Expand);
181 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
182 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
184 // Support label based line numbers.
185 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
186 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
188 setOperationAction(ISD::RET, MVT::Other, Custom);
189 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
190 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
191 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
192 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
194 // Expand mem operations genericly.
195 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
196 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
197 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
199 // Use the default implementation.
200 setOperationAction(ISD::VASTART , MVT::Other, Expand);
201 setOperationAction(ISD::VAARG , MVT::Other, Expand);
202 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
203 setOperationAction(ISD::VAEND , MVT::Other, Expand);
204 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
205 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
206 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
208 if (!Subtarget->hasV6Ops()) {
209 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
214 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
215 // Turn f64->i64 into FMRRD iff target supports vfp2.
216 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
218 // We want to custom lower some of our intrinsics.
219 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
221 setOperationAction(ISD::SETCC , MVT::i32, Expand);
222 setOperationAction(ISD::SETCC , MVT::f32, Expand);
223 setOperationAction(ISD::SETCC , MVT::f64, Expand);
224 setOperationAction(ISD::SELECT , MVT::i32, Expand);
225 setOperationAction(ISD::SELECT , MVT::f32, Expand);
226 setOperationAction(ISD::SELECT , MVT::f64, Expand);
227 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
228 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
229 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
231 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
232 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
233 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
234 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
235 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
237 setOperationAction(ISD::VASTART, MVT::Other, Custom);
238 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
239 setOperationAction(ISD::VAEND, MVT::Other, Expand);
240 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
241 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
243 // FP Constants can't be immediates.
244 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
245 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
247 // We don't support sin/cos/fmod/copysign/pow
248 setOperationAction(ISD::FSIN , MVT::f64, Expand);
249 setOperationAction(ISD::FSIN , MVT::f32, Expand);
250 setOperationAction(ISD::FCOS , MVT::f32, Expand);
251 setOperationAction(ISD::FCOS , MVT::f64, Expand);
252 setOperationAction(ISD::FREM , MVT::f64, Expand);
253 setOperationAction(ISD::FREM , MVT::f32, Expand);
254 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
255 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
256 setOperationAction(ISD::FPOW , MVT::f64, Expand);
257 setOperationAction(ISD::FPOW , MVT::f32, Expand);
259 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
260 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
261 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
262 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
263 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
265 setStackPointerRegisterToSaveRestore(ARM::SP);
266 setSchedulingPreference(SchedulingForRegPressure);
267 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
268 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
270 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
274 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
277 case ARMISD::Wrapper: return "ARMISD::Wrapper";
278 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
279 case ARMISD::CALL: return "ARMISD::CALL";
280 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
281 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
282 case ARMISD::tCALL: return "ARMISD::tCALL";
283 case ARMISD::BRCOND: return "ARMISD::BRCOND";
284 case ARMISD::BR_JT: return "ARMISD::BR_JT";
285 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
286 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
287 case ARMISD::CMP: return "ARMISD::CMP";
288 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
289 case ARMISD::CMPFP: return "ARMISD::CMPFP";
290 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
291 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
292 case ARMISD::CMOV: return "ARMISD::CMOV";
293 case ARMISD::CNEG: return "ARMISD::CNEG";
295 case ARMISD::FTOSI: return "ARMISD::FTOSI";
296 case ARMISD::FTOUI: return "ARMISD::FTOUI";
297 case ARMISD::SITOF: return "ARMISD::SITOF";
298 case ARMISD::UITOF: return "ARMISD::UITOF";
300 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
301 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
302 case ARMISD::RRX: return "ARMISD::RRX";
304 case ARMISD::FMRRD: return "ARMISD::FMRRD";
305 case ARMISD::FMDRR: return "ARMISD::FMDRR";
307 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
311 //===----------------------------------------------------------------------===//
313 //===----------------------------------------------------------------------===//
316 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
317 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
319 default: assert(0 && "Unknown condition code!");
320 case ISD::SETNE: return ARMCC::NE;
321 case ISD::SETEQ: return ARMCC::EQ;
322 case ISD::SETGT: return ARMCC::GT;
323 case ISD::SETGE: return ARMCC::GE;
324 case ISD::SETLT: return ARMCC::LT;
325 case ISD::SETLE: return ARMCC::LE;
326 case ISD::SETUGT: return ARMCC::HI;
327 case ISD::SETUGE: return ARMCC::HS;
328 case ISD::SETULT: return ARMCC::LO;
329 case ISD::SETULE: return ARMCC::LS;
333 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
334 /// returns true if the operands should be inverted to form the proper
336 static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
337 ARMCC::CondCodes &CondCode2) {
339 CondCode2 = ARMCC::AL;
341 default: assert(0 && "Unknown FP condition!");
343 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
345 case ISD::SETOGT: CondCode = ARMCC::GT; break;
347 case ISD::SETOGE: CondCode = ARMCC::GE; break;
348 case ISD::SETOLT: CondCode = ARMCC::MI; break;
349 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
350 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
351 case ISD::SETO: CondCode = ARMCC::VC; break;
352 case ISD::SETUO: CondCode = ARMCC::VS; break;
353 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
354 case ISD::SETUGT: CondCode = ARMCC::HI; break;
355 case ISD::SETUGE: CondCode = ARMCC::PL; break;
357 case ISD::SETULT: CondCode = ARMCC::LT; break;
359 case ISD::SETULE: CondCode = ARMCC::LE; break;
361 case ISD::SETUNE: CondCode = ARMCC::NE; break;
367 HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
368 unsigned StackOffset, unsigned &NeededGPRs,
369 unsigned &NeededStackSize, unsigned &GPRPad,
370 unsigned &StackPad, unsigned Flags) {
375 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
376 GPRPad = NumGPRs % ((align + 3)/4);
377 StackPad = StackOffset % align;
378 unsigned firstGPR = NumGPRs + GPRPad;
380 default: assert(0 && "Unhandled argument type!");
392 else if (firstGPR == 3) {
400 /// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
401 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
403 SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
404 MVT::ValueType RetVT= Op.Val->getValueType(0);
405 SDOperand Chain = Op.getOperand(0);
406 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
407 assert((CallConv == CallingConv::C ||
408 CallConv == CallingConv::Fast) && "unknown calling convention");
409 SDOperand Callee = Op.getOperand(4);
410 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
411 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
412 unsigned NumGPRs = 0; // GPRs used for parameter passing.
414 // Count how many bytes are to be pushed on the stack.
415 unsigned NumBytes = 0;
417 // Add up all the space actually used.
418 for (unsigned i = 0; i < NumOps; ++i) {
423 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
424 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
425 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
426 GPRPad, StackPad, Flags);
427 NumBytes += ObjSize + StackPad;
428 NumGPRs += ObjGPRs + GPRPad;
431 // Adjust the stack pointer for the new arguments...
432 // These operations are automatically eliminated by the prolog/epilog pass
433 Chain = DAG.getCALLSEQ_START(Chain,
434 DAG.getConstant(NumBytes, MVT::i32));
436 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
438 static const unsigned GPRArgRegs[] = {
439 ARM::R0, ARM::R1, ARM::R2, ARM::R3
443 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
444 std::vector<SDOperand> MemOpChains;
445 for (unsigned i = 0; i != NumOps; ++i) {
446 SDOperand Arg = Op.getOperand(5+2*i);
447 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
448 MVT::ValueType ArgVT = Arg.getValueType();
454 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
455 ObjSize, GPRPad, StackPad, Flags);
457 ArgOffset += StackPad;
460 default: assert(0 && "Unexpected ValueType for argument!");
462 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
465 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
466 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
469 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
470 DAG.getConstant(0, getPointerTy()));
471 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
472 DAG.getConstant(1, getPointerTy()));
473 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
475 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
477 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
478 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
479 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
484 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
485 DAG.getVTList(MVT::i32, MVT::i32),
487 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
489 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
492 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
493 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
494 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
501 assert(ObjSize != 0);
502 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
503 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
504 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
508 ArgOffset += ObjSize;
511 if (!MemOpChains.empty())
512 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
513 &MemOpChains[0], MemOpChains.size());
515 // Build a sequence of copy-to-reg nodes chained together with token chain
516 // and flag operands which copy the outgoing args into the appropriate regs.
518 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
519 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
521 InFlag = Chain.getValue(1);
524 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
525 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
526 // node so that legalize doesn't hack it.
527 bool isDirect = false;
528 bool isARMFunc = false;
529 bool isLocalARMFunc = false;
530 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
531 GlobalValue *GV = G->getGlobal();
533 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
534 GV->hasLinkOnceLinkage());
535 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
536 getTargetMachine().getRelocationModel() != Reloc::Static;
537 isARMFunc = !Subtarget->isThumb() || isStub;
538 // ARM call to a local ARM function is predicable.
539 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
540 // tBX takes a register source operand.
541 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
542 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
544 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
545 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
546 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
547 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
548 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
550 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
551 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
553 bool isStub = Subtarget->isTargetDarwin() &&
554 getTargetMachine().getRelocationModel() != Reloc::Static;
555 isARMFunc = !Subtarget->isThumb() || isStub;
556 // tBX takes a register source operand.
557 const char *Sym = S->getSymbol();
558 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
559 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
561 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
562 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
563 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
564 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
565 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
567 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
570 // FIXME: handle tail calls differently.
572 if (Subtarget->isThumb()) {
573 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
574 CallOpc = ARMISD::CALL_NOLINK;
576 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
578 CallOpc = (isDirect || Subtarget->hasV5TOps())
579 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
580 : ARMISD::CALL_NOLINK;
582 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
583 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
584 Chain = DAG.getCopyToReg(Chain, ARM::LR,
585 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
586 InFlag = Chain.getValue(1);
589 std::vector<MVT::ValueType> NodeTys;
590 NodeTys.push_back(MVT::Other); // Returns a chain
591 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
593 std::vector<SDOperand> Ops;
594 Ops.push_back(Chain);
595 Ops.push_back(Callee);
597 // Add argument registers to the end of the list so that they are known live
599 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
600 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
601 RegsToPass[i].second.getValueType()));
604 Ops.push_back(InFlag);
605 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
606 InFlag = Chain.getValue(1);
608 Chain = DAG.getCALLSEQ_END(Chain,
609 DAG.getConstant(NumBytes, MVT::i32),
610 DAG.getConstant(0, MVT::i32),
612 if (RetVT != MVT::Other)
613 InFlag = Chain.getValue(1);
615 std::vector<SDOperand> ResultVals;
618 // If the call has results, copy the values out of the ret val registers.
620 default: assert(0 && "Unexpected ret value!");
624 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
625 ResultVals.push_back(Chain.getValue(0));
626 if (Op.Val->getValueType(1) == MVT::i32) {
627 // Returns a i64 value.
628 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
629 Chain.getValue(2)).getValue(1);
630 ResultVals.push_back(Chain.getValue(0));
631 NodeTys.push_back(MVT::i32);
633 NodeTys.push_back(MVT::i32);
636 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
637 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
639 NodeTys.push_back(MVT::f32);
642 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
643 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
644 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
645 NodeTys.push_back(MVT::f64);
650 NodeTys.push_back(MVT::Other);
652 if (ResultVals.empty())
655 ResultVals.push_back(Chain);
656 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
658 return Res.getValue(Op.ResNo);
661 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
663 SDOperand Chain = Op.getOperand(0);
664 switch(Op.getNumOperands()) {
666 assert(0 && "Do not know how to return this many arguments!");
669 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
670 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
673 Op = Op.getOperand(1);
674 if (Op.getValueType() == MVT::f32) {
675 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
676 } else if (Op.getValueType() == MVT::f64) {
677 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
679 Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
680 SDOperand Sign = DAG.getConstant(0, MVT::i32);
681 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign,
682 Op.getValue(1), Sign);
684 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
685 if (DAG.getMachineFunction().liveout_empty())
686 DAG.getMachineFunction().addLiveOut(ARM::R0);
689 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
690 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
691 // If we haven't noted the R0+R1 are live out, do so now.
692 if (DAG.getMachineFunction().liveout_empty()) {
693 DAG.getMachineFunction().addLiveOut(ARM::R0);
694 DAG.getMachineFunction().addLiveOut(ARM::R1);
699 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
700 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
703 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
704 // their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
705 // one of the above mentioned nodes. It has to be wrapped because otherwise
706 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
707 // be used to form addressing mode. These wrapped nodes will be selected
709 static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
710 MVT::ValueType PtrVT = Op.getValueType();
711 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
713 if (CP->isMachineConstantPoolEntry())
714 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
717 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
719 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
722 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
724 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
726 MVT::ValueType PtrVT = getPointerTy();
727 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
728 ARMConstantPoolValue *CPV =
729 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
730 PCAdj, "tlsgd", true);
731 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
732 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
733 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
734 SDOperand Chain = Argument.getValue(1);
736 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
737 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
739 // call __tls_get_addr.
742 Entry.Node = Argument;
743 Entry.Ty = (const Type *) Type::Int32Ty;
744 Args.push_back(Entry);
745 std::pair<SDOperand, SDOperand> CallResult =
746 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
747 CallingConv::C, false,
748 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
749 return CallResult.first;
752 // Lower ISD::GlobalTLSAddress using the "initial exec" or
753 // "local exec" model.
755 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
757 GlobalValue *GV = GA->getGlobal();
759 SDOperand Chain = DAG.getEntryNode();
760 MVT::ValueType PtrVT = getPointerTy();
761 // Get the Thread Pointer
762 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
764 if (GV->isDeclaration()){
765 // initial exec model
766 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
767 ARMConstantPoolValue *CPV =
768 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
769 PCAdj, "gottpoff", true);
770 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
771 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
772 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
773 Chain = Offset.getValue(1);
775 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
776 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
778 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
781 ARMConstantPoolValue *CPV =
782 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
783 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
784 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
785 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
788 // The address of the thread local variable is the add of the thread
789 // pointer with the offset of the variable.
790 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
794 ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
795 // TODO: implement the "local dynamic" model
796 assert(Subtarget->isTargetELF() &&
797 "TLS not implemented for non-ELF targets");
798 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
799 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
800 // otherwise use the "Local Exec" TLS Model
801 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
802 return LowerToTLSGeneralDynamicModel(GA, DAG);
804 return LowerToTLSExecModels(GA, DAG);
807 SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
809 MVT::ValueType PtrVT = getPointerTy();
810 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
811 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
812 if (RelocM == Reloc::PIC_) {
813 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
814 ARMConstantPoolValue *CPV =
815 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
816 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
817 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
818 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
819 SDOperand Chain = Result.getValue(1);
820 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
821 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
823 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
826 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
827 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
828 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
832 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
833 /// even in non-static mode.
834 static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
835 return RelocM != Reloc::Static &&
836 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
837 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
840 SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
842 MVT::ValueType PtrVT = getPointerTy();
843 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
844 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
845 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
847 if (RelocM == Reloc::Static)
848 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
850 unsigned PCAdj = (RelocM != Reloc::PIC_)
851 ? 0 : (Subtarget->isThumb() ? 4 : 8);
852 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
854 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
856 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
858 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
860 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
861 SDOperand Chain = Result.getValue(1);
863 if (RelocM == Reloc::PIC_) {
864 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
865 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
868 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
873 SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
875 assert(Subtarget->isTargetELF() &&
876 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
877 MVT::ValueType PtrVT = getPointerTy();
878 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
879 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
881 ARMCP::CPValue, PCAdj);
882 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
883 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
884 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
885 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
886 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
889 static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
890 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
891 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
893 default: return SDOperand(); // Don't custom lower most intrinsics.
894 case Intrinsic::arm_thread_pointer:
895 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
899 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
900 unsigned VarArgsFrameIndex) {
901 // vastart just stores the address of the VarArgsFrameIndex slot into the
902 // memory location argument.
903 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
904 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
905 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
906 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
910 static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
911 unsigned *vRegs, unsigned ArgNo,
912 unsigned &NumGPRs, unsigned &ArgOffset) {
913 MachineFunction &MF = DAG.getMachineFunction();
914 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
915 SDOperand Root = Op.getOperand(0);
916 std::vector<SDOperand> ArgValues;
917 SSARegMap *RegMap = MF.getSSARegMap();
919 static const unsigned GPRArgRegs[] = {
920 ARM::R0, ARM::R1, ARM::R2, ARM::R3
927 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
928 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
929 ObjSize, GPRPad, StackPad, Flags);
931 ArgOffset += StackPad;
935 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
936 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
937 vRegs[NumGPRs] = VReg;
938 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
939 if (ObjectVT == MVT::f32)
940 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
941 } else if (ObjGPRs == 2) {
942 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
943 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
944 vRegs[NumGPRs] = VReg;
945 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
947 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
948 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
949 vRegs[NumGPRs+1] = VReg;
950 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
952 if (ObjectVT == MVT::i64)
953 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
955 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
960 // If the argument is actually used, emit a load from the right stack
962 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
963 MachineFrameInfo *MFI = MF.getFrameInfo();
964 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
965 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
967 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
969 SDOperand ArgValue2 =
970 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
971 if (ObjectVT == MVT::i64)
972 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
974 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
977 // Don't emit a dead load.
978 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
981 ArgOffset += ObjSize; // Move on to the next argument.
988 ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
989 std::vector<SDOperand> ArgValues;
990 SDOperand Root = Op.getOperand(0);
991 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
992 unsigned NumGPRs = 0; // GPRs used for parameter passing.
995 unsigned NumArgs = Op.Val->getNumValues()-1;
996 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
997 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
998 NumGPRs, ArgOffset));
1000 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1002 static const unsigned GPRArgRegs[] = {
1003 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1006 MachineFunction &MF = DAG.getMachineFunction();
1007 SSARegMap *RegMap = MF.getSSARegMap();
1008 MachineFrameInfo *MFI = MF.getFrameInfo();
1009 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1010 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1011 unsigned VARegSize = (4 - NumGPRs) * 4;
1012 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1013 if (VARegSaveSize) {
1014 // If this function is vararg, store any remaining integer argument regs
1015 // to their spots on the stack so that they may be loaded by deferencing
1016 // the result of va_next.
1017 AFI->setVarArgsRegSaveSize(VARegSaveSize);
1018 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1019 VARegSaveSize - VARegSize);
1020 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1022 SmallVector<SDOperand, 4> MemOps;
1023 for (; NumGPRs < 4; ++NumGPRs) {
1024 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
1025 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
1026 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1027 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1028 MemOps.push_back(Store);
1029 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1030 DAG.getConstant(4, getPointerTy()));
1032 if (!MemOps.empty())
1033 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1034 &MemOps[0], MemOps.size());
1036 // This will point to the next argument passed via stack.
1037 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1040 ArgValues.push_back(Root);
1042 // Return the new list of results.
1043 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1044 Op.Val->value_end());
1045 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1048 /// isFloatingPointZero - Return true if this is +0.0.
1049 static bool isFloatingPointZero(SDOperand Op) {
1050 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1051 return CFP->getValueAPF().isPosZero();
1052 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1053 // Maybe this has already been legalized into the constant pool?
1054 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1055 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1056 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1057 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1058 return CFP->getValueAPF().isPosZero();
1064 static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
1065 return ( isThumb && (C & ~255U) == 0) ||
1066 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1069 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1070 /// the given operands.
1071 static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1072 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1073 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
1074 unsigned C = RHSC->getValue();
1075 if (!isLegalCmpImmediate(C, isThumb)) {
1076 // Constant does not fit, try adjusting it by one?
1081 if (isLegalCmpImmediate(C-1, isThumb)) {
1082 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1083 RHS = DAG.getConstant(C-1, MVT::i32);
1088 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1089 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1090 RHS = DAG.getConstant(C-1, MVT::i32);
1095 if (isLegalCmpImmediate(C+1, isThumb)) {
1096 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1097 RHS = DAG.getConstant(C+1, MVT::i32);
1102 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1103 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1104 RHS = DAG.getConstant(C+1, MVT::i32);
1111 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1112 ARMISD::NodeType CompareType;
1115 CompareType = ARMISD::CMP;
1121 // Uses only N and Z Flags
1122 CompareType = ARMISD::CMPNZ;
1125 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1126 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
1129 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1130 static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1132 if (!isFloatingPointZero(RHS))
1133 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1135 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1136 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1139 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1140 const ARMSubtarget *ST) {
1141 MVT::ValueType VT = Op.getValueType();
1142 SDOperand LHS = Op.getOperand(0);
1143 SDOperand RHS = Op.getOperand(1);
1144 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1145 SDOperand TrueVal = Op.getOperand(2);
1146 SDOperand FalseVal = Op.getOperand(3);
1148 if (LHS.getValueType() == MVT::i32) {
1150 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1151 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1152 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
1155 ARMCC::CondCodes CondCode, CondCode2;
1156 if (FPCCToARMCC(CC, CondCode, CondCode2))
1157 std::swap(TrueVal, FalseVal);
1159 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1160 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1161 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1162 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
1164 if (CondCode2 != ARMCC::AL) {
1165 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1166 // FIXME: Needs another CMP because flag can have but one use.
1167 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
1168 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
1173 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1174 const ARMSubtarget *ST) {
1175 SDOperand Chain = Op.getOperand(0);
1176 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1177 SDOperand LHS = Op.getOperand(2);
1178 SDOperand RHS = Op.getOperand(3);
1179 SDOperand Dest = Op.getOperand(4);
1181 if (LHS.getValueType() == MVT::i32) {
1183 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1184 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1185 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
1188 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1189 ARMCC::CondCodes CondCode, CondCode2;
1190 if (FPCCToARMCC(CC, CondCode, CondCode2))
1191 // Swap the LHS/RHS of the comparison if needed.
1192 std::swap(LHS, RHS);
1194 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1195 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
1196 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1197 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1198 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1199 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
1200 if (CondCode2 != ARMCC::AL) {
1201 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1202 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1203 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
1208 SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1209 SDOperand Chain = Op.getOperand(0);
1210 SDOperand Table = Op.getOperand(1);
1211 SDOperand Index = Op.getOperand(2);
1213 MVT::ValueType PTy = getPointerTy();
1214 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1215 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1216 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1217 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1218 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1219 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1220 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1221 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1222 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1223 Chain, Addr, NULL, 0);
1224 Chain = Addr.getValue(1);
1226 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1227 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1230 static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1232 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1233 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1234 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1237 static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1238 MVT::ValueType VT = Op.getValueType();
1240 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1242 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1243 return DAG.getNode(Opc, VT, Op);
1246 static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1247 // Implement fcopysign with a fabs and a conditional fneg.
1248 SDOperand Tmp0 = Op.getOperand(0);
1249 SDOperand Tmp1 = Op.getOperand(1);
1250 MVT::ValueType VT = Op.getValueType();
1251 MVT::ValueType SrcVT = Tmp1.getValueType();
1252 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1253 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1254 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1255 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1256 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
1259 static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1260 // Turn f64->i64 into FMRRD.
1261 assert(Op.getValueType() == MVT::i64 &&
1262 Op.getOperand(0).getValueType() == MVT::f64);
1264 Op = Op.getOperand(0);
1265 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1268 // Merge the pieces into a single i64 value.
1269 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1272 static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1273 const ARMSubtarget *ST) {
1274 assert(Op.getValueType() == MVT::i64 &&
1275 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1276 "Unknown shift to lower!");
1278 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1279 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1280 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1283 // If we are in thumb mode, we don't have RRX.
1284 if (ST->isThumb()) return SDOperand();
1286 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1287 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1288 DAG.getConstant(0, MVT::i32));
1289 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1290 DAG.getConstant(1, MVT::i32));
1292 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1293 // captures the result into a carry flag.
1294 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1295 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1297 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1298 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1300 // Merge the pieces into a single i64 value.
1301 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1304 SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain,
1309 SelectionDAG &DAG) {
1310 // Do repeated 4-byte loads and stores. To be improved.
1311 assert((Align & 3) == 0 && "Expected 4-byte aligned addresses!");
1312 unsigned BytesLeft = Size & 3;
1313 unsigned NumMemOps = Size >> 2;
1314 unsigned EmittedNumMemOps = 0;
1315 unsigned SrcOff = 0, DstOff = 0;
1316 MVT::ValueType VT = MVT::i32;
1317 unsigned VTSize = 4;
1319 const unsigned MAX_LOADS_IN_LDM = 6;
1320 SDOperand TFOps[MAX_LOADS_IN_LDM];
1321 SDOperand Loads[MAX_LOADS_IN_LDM];
1323 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1324 // same number of stores. The loads and stores will get combined into
1325 // ldm/stm later on.
1326 while (EmittedNumMemOps < NumMemOps) {
1328 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1329 Loads[i] = DAG.getLoad(VT, Chain,
1330 DAG.getNode(ISD::ADD, MVT::i32, Source,
1331 DAG.getConstant(SrcOff, MVT::i32)),
1333 TFOps[i] = Loads[i].getValue(1);
1336 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1339 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1340 TFOps[i] = DAG.getStore(Chain, Loads[i],
1341 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1342 DAG.getConstant(DstOff, MVT::i32)),
1346 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1348 EmittedNumMemOps += i;
1354 // Issue loads / stores for the trailing (1 - 3) bytes.
1355 unsigned BytesLeftSave = BytesLeft;
1358 if (BytesLeft >= 2) {
1366 Loads[i] = DAG.getLoad(VT, Chain,
1367 DAG.getNode(ISD::ADD, MVT::i32, Source,
1368 DAG.getConstant(SrcOff, MVT::i32)),
1370 TFOps[i] = Loads[i].getValue(1);
1373 BytesLeft -= VTSize;
1375 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1378 BytesLeft = BytesLeftSave;
1380 if (BytesLeft >= 2) {
1388 TFOps[i] = DAG.getStore(Chain, Loads[i],
1389 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1390 DAG.getConstant(DstOff, MVT::i32)),
1394 BytesLeft -= VTSize;
1396 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1399 SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1400 switch (Op.getOpcode()) {
1401 default: assert(0 && "Don't know how to custom lower this!"); abort();
1402 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1403 case ISD::GlobalAddress:
1404 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1405 LowerGlobalAddressELF(Op, DAG);
1406 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
1407 case ISD::CALL: return LowerCALL(Op, DAG);
1408 case ISD::RET: return LowerRET(Op, DAG);
1409 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1410 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1411 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1412 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1413 case ISD::SINT_TO_FP:
1414 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1415 case ISD::FP_TO_SINT:
1416 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1417 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1418 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1420 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1421 case ISD::FORMAL_ARGUMENTS:
1422 return LowerFORMAL_ARGUMENTS(Op, DAG);
1423 case ISD::RETURNADDR: break;
1424 case ISD::FRAMEADDR: break;
1425 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
1426 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
1427 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1432 //===----------------------------------------------------------------------===//
1433 // ARM Scheduler Hooks
1434 //===----------------------------------------------------------------------===//
1437 ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1438 MachineBasicBlock *BB) {
1439 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1440 switch (MI->getOpcode()) {
1441 default: assert(false && "Unexpected instr type to insert");
1442 case ARM::tMOVCCr: {
1443 // To "insert" a SELECT_CC instruction, we actually have to insert the
1444 // diamond control-flow pattern. The incoming instruction knows the
1445 // destination vreg to set, the condition code register to branch on, the
1446 // true/false values to select between, and a branch opcode to use.
1447 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1448 ilist<MachineBasicBlock>::iterator It = BB;
1454 // cmpTY ccX, r1, r2
1456 // fallthrough --> copy0MBB
1457 MachineBasicBlock *thisMBB = BB;
1458 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1459 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1460 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1461 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
1462 MachineFunction *F = BB->getParent();
1463 F->getBasicBlockList().insert(It, copy0MBB);
1464 F->getBasicBlockList().insert(It, sinkMBB);
1465 // Update machine-CFG edges by first adding all successors of the current
1466 // block to the new block which will contain the Phi node for the select.
1467 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1468 e = BB->succ_end(); i != e; ++i)
1469 sinkMBB->addSuccessor(*i);
1470 // Next, remove all successors of the current block, and add the true
1471 // and fallthrough blocks as its successors.
1472 while(!BB->succ_empty())
1473 BB->removeSuccessor(BB->succ_begin());
1474 BB->addSuccessor(copy0MBB);
1475 BB->addSuccessor(sinkMBB);
1478 // %FalseValue = ...
1479 // # fallthrough to sinkMBB
1482 // Update machine-CFG edges
1483 BB->addSuccessor(sinkMBB);
1486 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1489 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1490 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1491 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1493 delete MI; // The pseudo instruction is gone now.
1499 //===----------------------------------------------------------------------===//
1500 // ARM Optimization Hooks
1501 //===----------------------------------------------------------------------===//
1503 /// isLegalAddressImmediate - Return true if the integer value can be used
1504 /// as the offset of the target addressing mode for load / store of the
1506 static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1507 const ARMSubtarget *Subtarget) {
1511 if (Subtarget->isThumb()) {
1517 default: return false;
1532 if ((V & (Scale - 1)) != 0)
1535 return V == V & ((1LL << 5) - 1);
1541 default: return false;
1546 return V == V & ((1LL << 12) - 1);
1549 return V == V & ((1LL << 8) - 1);
1552 if (!Subtarget->hasVFP2())
1557 return V == V & ((1LL << 8) - 1);
1561 /// isLegalAddressingMode - Return true if the addressing mode represented
1562 /// by AM is legal for this target, for a load/store of the specified type.
1563 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1564 const Type *Ty) const {
1565 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
1568 // Can never fold addr of global into load/store.
1573 case 0: // no scale reg, must be "r+i" or "r", or "i".
1576 if (Subtarget->isThumb())
1580 // ARM doesn't support any R+R*scale+imm addr modes.
1584 int Scale = AM.Scale;
1585 switch (getValueType(Ty)) {
1586 default: return false;
1591 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1592 // ldrd / strd are used, then its address mode is same as i16.
1594 if (Scale < 0) Scale = -Scale;
1598 return isPowerOf2_32(Scale & ~1);
1601 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
1606 // Note, we allow "void" uses (basically, uses that aren't loads or
1607 // stores), because arm allows folding a scale into many arithmetic
1608 // operations. This should be made more precise and revisited later.
1610 // Allow r << imm, but the imm has to be a multiple of two.
1611 if (AM.Scale & 1) return false;
1612 return isPowerOf2_32(AM.Scale);
1620 static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1621 bool isSEXTLoad, SDOperand &Base,
1622 SDOperand &Offset, bool &isInc,
1623 SelectionDAG &DAG) {
1624 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1627 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1629 Base = Ptr->getOperand(0);
1630 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1631 int RHSC = (int)RHS->getValue();
1632 if (RHSC < 0 && RHSC > -256) {
1634 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1638 isInc = (Ptr->getOpcode() == ISD::ADD);
1639 Offset = Ptr->getOperand(1);
1641 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1643 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1644 int RHSC = (int)RHS->getValue();
1645 if (RHSC < 0 && RHSC > -0x1000) {
1647 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1648 Base = Ptr->getOperand(0);
1653 if (Ptr->getOpcode() == ISD::ADD) {
1655 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1656 if (ShOpcVal != ARM_AM::no_shift) {
1657 Base = Ptr->getOperand(1);
1658 Offset = Ptr->getOperand(0);
1660 Base = Ptr->getOperand(0);
1661 Offset = Ptr->getOperand(1);
1666 isInc = (Ptr->getOpcode() == ISD::ADD);
1667 Base = Ptr->getOperand(0);
1668 Offset = Ptr->getOperand(1);
1672 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1676 /// getPreIndexedAddressParts - returns true by value, base pointer and
1677 /// offset pointer and addressing mode by reference if the node's address
1678 /// can be legally represented as pre-indexed load / store address.
1680 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1682 ISD::MemIndexedMode &AM,
1683 SelectionDAG &DAG) {
1684 if (Subtarget->isThumb())
1689 bool isSEXTLoad = false;
1690 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1691 Ptr = LD->getBasePtr();
1692 VT = LD->getLoadedVT();
1693 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1694 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1695 Ptr = ST->getBasePtr();
1696 VT = ST->getStoredVT();
1701 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1704 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1710 /// getPostIndexedAddressParts - returns true by value, base pointer and
1711 /// offset pointer and addressing mode by reference if this node can be
1712 /// combined with a load / store to form a post-indexed load / store.
1713 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1716 ISD::MemIndexedMode &AM,
1717 SelectionDAG &DAG) {
1718 if (Subtarget->isThumb())
1723 bool isSEXTLoad = false;
1724 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1725 VT = LD->getLoadedVT();
1726 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1727 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1728 VT = ST->getStoredVT();
1733 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1736 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1742 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1744 uint64_t &KnownZero,
1746 const SelectionDAG &DAG,
1747 unsigned Depth) const {
1750 switch (Op.getOpcode()) {
1752 case ARMISD::CMOV: {
1753 // Bits are known zero/one if known on the LHS and RHS.
1754 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1755 if (KnownZero == 0 && KnownOne == 0) return;
1757 uint64_t KnownZeroRHS, KnownOneRHS;
1758 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1759 KnownZeroRHS, KnownOneRHS, Depth+1);
1760 KnownZero &= KnownZeroRHS;
1761 KnownOne &= KnownOneRHS;
1767 //===----------------------------------------------------------------------===//
1768 // ARM Inline Assembly Support
1769 //===----------------------------------------------------------------------===//
1771 /// getConstraintType - Given a constraint letter, return the type of
1772 /// constraint it is for this target.
1773 ARMTargetLowering::ConstraintType
1774 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1775 if (Constraint.size() == 1) {
1776 switch (Constraint[0]) {
1778 case 'l': return C_RegisterClass;
1779 case 'w': return C_RegisterClass;
1782 return TargetLowering::getConstraintType(Constraint);
1785 std::pair<unsigned, const TargetRegisterClass*>
1786 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1787 MVT::ValueType VT) const {
1788 if (Constraint.size() == 1) {
1789 // GCC RS6000 Constraint Letters
1790 switch (Constraint[0]) {
1792 // FIXME: in thumb mode, 'l' is only low-regs.
1795 return std::make_pair(0U, ARM::GPRRegisterClass);
1798 return std::make_pair(0U, ARM::SPRRegisterClass);
1800 return std::make_pair(0U, ARM::DPRRegisterClass);
1804 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1807 std::vector<unsigned> ARMTargetLowering::
1808 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1809 MVT::ValueType VT) const {
1810 if (Constraint.size() != 1)
1811 return std::vector<unsigned>();
1813 switch (Constraint[0]) { // GCC ARM Constraint Letters
1817 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1818 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1819 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1820 ARM::R12, ARM::LR, 0);
1823 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1824 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1825 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1826 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1827 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1828 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1829 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1830 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1832 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1833 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1834 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1835 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1839 return std::vector<unsigned>();