blob: 5cbf29601412e6fec6a7bf71f290fc6f321db135 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
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/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/SelectionDAG.h"
30#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng491fcdb2007-01-31 08:40:13 +000031#include "llvm/Target/TargetOptions.h"
Evan Cheng10043e22007-01-19 07:51:42 +000032#include "llvm/ADT/VectorExtras.h"
33using namespace llvm;
34
35ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
36 : TargetLowering(TM), ARMPCLabelIndex(0) {
37 Subtarget = &TM.getSubtarget<ARMSubtarget>();
38
39 // Uses VFP for Thumb libfuncs if available.
Evan Cheng491fcdb2007-01-31 08:40:13 +000040 if (!UseSoftFloat && Subtarget->isThumb() && Subtarget->hasVFP2()) {
Evan Cheng10043e22007-01-19 07:51:42 +000041 // Single-precision floating-point arithmetic.
42 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
43 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
44 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
45 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
46
47 // Double-precision floating-point arithmetic.
48 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
49 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
50 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
51 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
52
53 // Single-precision comparisons.
54 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
55 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
56 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
57 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
58 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
59 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
60 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
61
62 // Double-precision comparisons.
63 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
64 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
65 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
66 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
67 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
68 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
69 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
70
71 // Floating-point to integer conversions.
72 // i64 conversions are done via library routines even when generating VFP
73 // instructions, so use the same ones.
74 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
75 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
76 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
77 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
78
79 // Conversions between floating types.
80 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
81 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
82
83 // Integer to floating-point conversions.
84 // i64 conversions are done via library routines even when generating VFP
85 // instructions, so use the same ones.
86 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
87 // __floatunsidf vs. __floatunssidfvfp.
88 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
89 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
90 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
91 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
92 }
93
94 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Cheng491fcdb2007-01-31 08:40:13 +000095 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Cheng10043e22007-01-19 07:51:42 +000096 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
97 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
98 }
99
100 // ARM does not have f32 extending load.
101 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
102
103 // ARM supports all 4 flavors of integer indexed load / store.
104 for (unsigned im = (unsigned)ISD::PRE_INC;
105 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
106 setIndexedLoadAction(im, MVT::i1, Legal);
107 setIndexedLoadAction(im, MVT::i8, Legal);
108 setIndexedLoadAction(im, MVT::i16, Legal);
109 setIndexedLoadAction(im, MVT::i32, Legal);
110 setIndexedStoreAction(im, MVT::i1, Legal);
111 setIndexedStoreAction(im, MVT::i8, Legal);
112 setIndexedStoreAction(im, MVT::i16, Legal);
113 setIndexedStoreAction(im, MVT::i32, Legal);
114 }
115
116 // i64 operation support.
117 if (Subtarget->isThumb()) {
118 setOperationAction(ISD::MUL, MVT::i64, Expand);
119 setOperationAction(ISD::MULHU, MVT::i32, Expand);
120 setOperationAction(ISD::MULHS, MVT::i32, Expand);
121 } else {
122 setOperationAction(ISD::MUL, MVT::i64, Custom);
123 setOperationAction(ISD::MULHU, MVT::i32, Custom);
124 if (!Subtarget->hasV6Ops())
125 setOperationAction(ISD::MULHS, MVT::i32, Custom);
126 }
127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
130 setOperationAction(ISD::SRL, MVT::i64, Custom);
131 setOperationAction(ISD::SRA, MVT::i64, Custom);
132
133 // ARM does not have ROTL.
134 setOperationAction(ISD::ROTL, MVT::i32, Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
136 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
137 if (!Subtarget->hasV5TOps())
138 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
139
140 // These are expanded into libcalls.
141 setOperationAction(ISD::SDIV, MVT::i32, Expand);
142 setOperationAction(ISD::UDIV, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i32, Expand);
144 setOperationAction(ISD::UREM, MVT::i32, Expand);
145
146 // Support label based line numbers.
147 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
148 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
149 // FIXME - use subtarget debug flags
Evan Chengbf216c32007-01-19 19:28:01 +0000150 if (Subtarget->isTargetDarwin())
Jim Laskeyf9e54452007-01-26 14:34:52 +0000151 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000152
153 setOperationAction(ISD::RET, MVT::Other, Custom);
154 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
155 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
156
157 // Expand mem operations genericly.
158 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
159 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
160 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
161
162 // Use the default implementation.
163 setOperationAction(ISD::VASTART , MVT::Other, Expand);
164 setOperationAction(ISD::VAARG , MVT::Other, Expand);
165 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
166 setOperationAction(ISD::VAEND , MVT::Other, Expand);
167 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
168 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
169 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
170
171 if (!Subtarget->hasV6Ops()) {
172 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
173 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
174 }
175 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
176
Evan Cheng491fcdb2007-01-31 08:40:13 +0000177 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Cheng10043e22007-01-19 07:51:42 +0000178 // Turn f64->i64 into FMRRD iff target supports vfp2.
179 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
180
181 setOperationAction(ISD::SETCC , MVT::i32, Expand);
182 setOperationAction(ISD::SETCC , MVT::f32, Expand);
183 setOperationAction(ISD::SETCC , MVT::f64, Expand);
184 setOperationAction(ISD::SELECT , MVT::i32, Expand);
185 setOperationAction(ISD::SELECT , MVT::f32, Expand);
186 setOperationAction(ISD::SELECT , MVT::f64, Expand);
187 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
188 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
189 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
190
191 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
192 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
193 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
194 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
195 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
196
197 setOperationAction(ISD::VASTART, MVT::Other, Custom);
198 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
199 setOperationAction(ISD::VAEND, MVT::Other, Expand);
200 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
201 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
202
203 // FP Constants can't be immediates.
204 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
205 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
206
207 // We don't support sin/cos/fmod/copysign
208 setOperationAction(ISD::FSIN , MVT::f64, Expand);
209 setOperationAction(ISD::FSIN , MVT::f32, Expand);
210 setOperationAction(ISD::FCOS , MVT::f32, Expand);
211 setOperationAction(ISD::FCOS , MVT::f64, Expand);
212 setOperationAction(ISD::FREM , MVT::f64, Expand);
213 setOperationAction(ISD::FREM , MVT::f32, Expand);
214 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
215 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
216
217 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
218 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
219 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
220 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
221 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
222
223 setStackPointerRegisterToSaveRestore(ARM::SP);
224
225 setSchedulingPreference(SchedulingForRegPressure);
226 computeRegisterProperties();
227}
228
229
230const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
231 switch (Opcode) {
232 default: return 0;
233 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng10043e22007-01-19 07:51:42 +0000234 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
235 case ARMISD::CALL: return "ARMISD::CALL";
236 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
237 case ARMISD::tCALL: return "ARMISD::tCALL";
238 case ARMISD::BRCOND: return "ARMISD::BRCOND";
239 case ARMISD::BR_JT: return "ARMISD::BR_JT";
240 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
241 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
242 case ARMISD::CMP: return "ARMISD::CMP";
243 case ARMISD::CMPFP: return "ARMISD::CMPFP";
244 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
245 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
246 case ARMISD::CMOV: return "ARMISD::CMOV";
247 case ARMISD::CNEG: return "ARMISD::CNEG";
248
249 case ARMISD::FTOSI: return "ARMISD::FTOSI";
250 case ARMISD::FTOUI: return "ARMISD::FTOUI";
251 case ARMISD::SITOF: return "ARMISD::SITOF";
252 case ARMISD::UITOF: return "ARMISD::UITOF";
253 case ARMISD::MULHILOU: return "ARMISD::MULHILOU";
254 case ARMISD::MULHILOS: return "ARMISD::MULHILOS";
255
256 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
257 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
258 case ARMISD::RRX: return "ARMISD::RRX";
259
260 case ARMISD::FMRRD: return "ARMISD::FMRRD";
261 case ARMISD::FMDRR: return "ARMISD::FMDRR";
262 }
263}
264
265//===----------------------------------------------------------------------===//
266// Lowering Code
267//===----------------------------------------------------------------------===//
268
269
270/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
271static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
272 switch (CC) {
273 default: assert(0 && "Unknown condition code!");
274 case ISD::SETNE: return ARMCC::NE;
275 case ISD::SETEQ: return ARMCC::EQ;
276 case ISD::SETGT: return ARMCC::GT;
277 case ISD::SETGE: return ARMCC::GE;
278 case ISD::SETLT: return ARMCC::LT;
279 case ISD::SETLE: return ARMCC::LE;
280 case ISD::SETUGT: return ARMCC::HI;
281 case ISD::SETUGE: return ARMCC::HS;
282 case ISD::SETULT: return ARMCC::LO;
283 case ISD::SETULE: return ARMCC::LS;
284 }
285}
286
287/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
288/// returns true if the operands should be inverted to form the proper
289/// comparison.
290static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
291 ARMCC::CondCodes &CondCode2) {
292 bool Invert = false;
293 CondCode2 = ARMCC::AL;
294 switch (CC) {
295 default: assert(0 && "Unknown FP condition!");
296 case ISD::SETEQ:
297 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
298 case ISD::SETGT:
299 case ISD::SETOGT: CondCode = ARMCC::GT; break;
300 case ISD::SETGE:
301 case ISD::SETOGE: CondCode = ARMCC::GE; break;
302 case ISD::SETOLT: CondCode = ARMCC::MI; break;
303 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
304 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
305 case ISD::SETO: CondCode = ARMCC::VC; break;
306 case ISD::SETUO: CondCode = ARMCC::VS; break;
307 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
308 case ISD::SETUGT: CondCode = ARMCC::HI; break;
309 case ISD::SETUGE: CondCode = ARMCC::PL; break;
310 case ISD::SETLT:
311 case ISD::SETULT: CondCode = ARMCC::LT; break;
312 case ISD::SETLE:
313 case ISD::SETULE: CondCode = ARMCC::LE; break;
314 case ISD::SETNE:
315 case ISD::SETUNE: CondCode = ARMCC::NE; break;
316 }
317 return Invert;
318}
319
320static void
321HowToPassArgument(MVT::ValueType ObjectVT,
322 unsigned NumGPRs, unsigned &ObjSize, unsigned &ObjGPRs) {
323 ObjSize = 0;
324 ObjGPRs = 0;
325
326 switch (ObjectVT) {
327 default: assert(0 && "Unhandled argument type!");
328 case MVT::i32:
329 case MVT::f32:
330 if (NumGPRs < 4)
331 ObjGPRs = 1;
332 else
333 ObjSize = 4;
334 break;
335 case MVT::i64:
336 case MVT::f64:
337 if (NumGPRs < 3)
338 ObjGPRs = 2;
339 else if (NumGPRs == 3) {
340 ObjGPRs = 1;
341 ObjSize = 4;
342 } else
343 ObjSize = 8;
344 }
345}
346
347// This transforms a ISD::CALL node into a
348// callseq_star <- ARMISD:CALL <- callseq_end
349// chain
350SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
351 MVT::ValueType RetVT= Op.Val->getValueType(0);
352 SDOperand Chain = Op.getOperand(0);
353 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
354 assert((CallConv == CallingConv::C ||
Evan Cheng10043e22007-01-19 07:51:42 +0000355 CallConv == CallingConv::Fast) && "unknown calling convention");
356 SDOperand Callee = Op.getOperand(4);
357 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
358 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
359 unsigned NumGPRs = 0; // GPRs used for parameter passing.
360
361 // Count how many bytes are to be pushed on the stack.
362 unsigned NumBytes = 0;
363
364 // Add up all the space actually used.
365 for (unsigned i = 0; i < NumOps; ++i) {
366 unsigned ObjSize = 0;
367 unsigned ObjGPRs = 0;
368 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
369 HowToPassArgument(ObjectVT, NumGPRs, ObjSize, ObjGPRs);
370 NumBytes += ObjSize;
371 NumGPRs += ObjGPRs;
372 }
373
374 // Adjust the stack pointer for the new arguments...
375 // These operations are automatically eliminated by the prolog/epilog pass
376 Chain = DAG.getCALLSEQ_START(Chain,
377 DAG.getConstant(NumBytes, MVT::i32));
378
379 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
380
381 static const unsigned GPRArgRegs[] = {
382 ARM::R0, ARM::R1, ARM::R2, ARM::R3
383 };
384
385 NumGPRs = 0;
386 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
387 std::vector<SDOperand> MemOpChains;
388 for (unsigned i = 0; i != NumOps; ++i) {
389 SDOperand Arg = Op.getOperand(5+2*i);
390 MVT::ValueType ArgVT = Arg.getValueType();
391
392 unsigned ObjSize = 0;
393 unsigned ObjGPRs = 0;
394 HowToPassArgument(ArgVT, NumGPRs, ObjSize, ObjGPRs);
395 if (ObjGPRs > 0) {
396 switch (ArgVT) {
397 default: assert(0 && "Unexpected ValueType for argument!");
398 case MVT::i32:
399 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
400 break;
401 case MVT::f32:
402 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
403 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
404 break;
405 case MVT::i64: {
406 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
407 DAG.getConstant(0, getPointerTy()));
408 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
409 DAG.getConstant(1, getPointerTy()));
410 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
411 if (ObjGPRs == 2)
412 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
413 else {
414 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
415 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
416 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
417 }
418 break;
419 }
420 case MVT::f64: {
421 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
422 DAG.getVTList(MVT::i32, MVT::i32),
423 &Arg, 1);
424 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
425 if (ObjGPRs == 2)
426 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
427 Cvt.getValue(1)));
428 else {
429 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
430 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
431 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
432 NULL, 0));
433 }
434 break;
435 }
436 }
437 } else {
438 assert(ObjSize != 0);
439 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
440 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
441 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
442 }
443
444 NumGPRs += ObjGPRs;
445 ArgOffset += ObjSize;
446 }
447
448 if (!MemOpChains.empty())
449 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
450 &MemOpChains[0], MemOpChains.size());
451
452 // Build a sequence of copy-to-reg nodes chained together with token chain
453 // and flag operands which copy the outgoing args into the appropriate regs.
454 SDOperand InFlag;
455 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
456 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
457 InFlag);
458 InFlag = Chain.getValue(1);
459 }
460
461 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
462 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
463 // node so that legalize doesn't hack it.
464 bool isDirect = false;
465 bool isARMFunc = false;
466 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
467 GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +0000468 isDirect = true;
Reid Spencer5301e7c2007-01-30 20:08:39 +0000469 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Cheng10043e22007-01-19 07:51:42 +0000470 GV->hasLinkOnceLinkage());
Evan Chengbf216c32007-01-19 19:28:01 +0000471 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Cheng10043e22007-01-19 07:51:42 +0000472 getTargetMachine().getRelocationModel() != Reloc::Static;
473 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +0000474 // tBX takes a register source operand.
475 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
476 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
477 ARMCP::CPStub, 4);
478 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
479 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
480 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
481 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
482 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
483 } else
484 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000485 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000486 isDirect = true;
Evan Chengbf216c32007-01-19 19:28:01 +0000487 bool isStub = Subtarget->isTargetDarwin() &&
Evan Cheng10043e22007-01-19 07:51:42 +0000488 getTargetMachine().getRelocationModel() != Reloc::Static;
489 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +0000490 // tBX takes a register source operand.
491 const char *Sym = S->getSymbol();
492 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
493 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
494 ARMCP::CPStub, 4);
495 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
496 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
497 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
498 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
499 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
500 } else
501 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +0000502 }
503
504 std::vector<MVT::ValueType> NodeTys;
505 NodeTys.push_back(MVT::Other); // Returns a chain
506 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
507
508 std::vector<SDOperand> Ops;
509 Ops.push_back(Chain);
510 Ops.push_back(Callee);
511
512 // Add argument registers to the end of the list so that they are known live
513 // into the call.
514 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
515 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
516 RegsToPass[i].second.getValueType()));
517
518 // FIXME: handle tail calls differently.
519 unsigned CallOpc;
520 if (Subtarget->isThumb()) {
521 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
522 CallOpc = ARMISD::CALL_NOLINK;
523 else
524 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
525 } else {
526 CallOpc = (isDirect || Subtarget->hasV5TOps())
527 ? ARMISD::CALL : ARMISD::CALL_NOLINK;
528 }
529 if (InFlag.Val)
530 Ops.push_back(InFlag);
531 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
532 InFlag = Chain.getValue(1);
533
534 SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag };
535 Chain = DAG.getNode(ISD::CALLSEQ_END,
536 DAG.getNodeValueTypes(MVT::Other, MVT::Flag),
537 ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3);
538 if (RetVT != MVT::Other)
539 InFlag = Chain.getValue(1);
540
541 std::vector<SDOperand> ResultVals;
542 NodeTys.clear();
543
544 // If the call has results, copy the values out of the ret val registers.
545 switch (RetVT) {
546 default: assert(0 && "Unexpected ret value!");
547 case MVT::Other:
548 break;
549 case MVT::i32:
550 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
551 ResultVals.push_back(Chain.getValue(0));
552 if (Op.Val->getValueType(1) == MVT::i32) {
553 // Returns a i64 value.
554 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
555 Chain.getValue(2)).getValue(1);
556 ResultVals.push_back(Chain.getValue(0));
557 NodeTys.push_back(MVT::i32);
558 }
559 NodeTys.push_back(MVT::i32);
560 break;
561 case MVT::f32:
562 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
563 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
564 Chain.getValue(0)));
565 NodeTys.push_back(MVT::f32);
566 break;
567 case MVT::f64: {
568 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
569 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
570 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
571 NodeTys.push_back(MVT::f64);
572 break;
573 }
574 }
575
576 NodeTys.push_back(MVT::Other);
577
578 if (ResultVals.empty())
579 return Chain;
580
581 ResultVals.push_back(Chain);
582 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
583 ResultVals.size());
584 return Res.getValue(Op.ResNo);
585}
586
587static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
588 SDOperand Copy;
589 SDOperand Chain = Op.getOperand(0);
590 switch(Op.getNumOperands()) {
591 default:
592 assert(0 && "Do not know how to return this many arguments!");
593 abort();
594 case 1: {
595 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
596 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
597 }
598 case 3:
599 Op = Op.getOperand(1);
600 if (Op.getValueType() == MVT::f32) {
601 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
602 } else if (Op.getValueType() == MVT::f64) {
603 // Recursively legalize f64 -> i64.
604 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Op);
605 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op,
606 DAG.getConstant(0, MVT::i32));
607 }
608 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
609 if (DAG.getMachineFunction().liveout_empty())
610 DAG.getMachineFunction().addLiveOut(ARM::R0);
611 break;
612 case 5:
613 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
614 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
615 // If we haven't noted the R0+R1 are live out, do so now.
616 if (DAG.getMachineFunction().liveout_empty()) {
617 DAG.getMachineFunction().addLiveOut(ARM::R0);
618 DAG.getMachineFunction().addLiveOut(ARM::R1);
619 }
620 break;
621 }
622
623 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
624 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
625}
626
627// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
628// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
629// one of the above mentioned nodes. It has to be wrapped because otherwise
630// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
631// be used to form addressing mode. These wrapped nodes will be selected
632// into MOVri.
633static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
634 MVT::ValueType PtrVT = Op.getValueType();
635 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
636 SDOperand Res;
637 if (CP->isMachineConstantPoolEntry())
638 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
639 CP->getAlignment());
640 else
641 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
642 CP->getAlignment());
643 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
644}
645
646/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
647/// even in dynamic-no-pic mode.
648static bool GVIsIndirectSymbol(GlobalValue *GV) {
649 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Reid Spencer5301e7c2007-01-30 20:08:39 +0000650 (GV->isDeclaration() && !GV->hasNotBeenReadFromBytecode()));
Evan Cheng10043e22007-01-19 07:51:42 +0000651}
652
653SDOperand ARMTargetLowering::LowerGlobalAddress(SDOperand Op,
654 SelectionDAG &DAG) {
655 MVT::ValueType PtrVT = getPointerTy();
656 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
657 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengbf216c32007-01-19 19:28:01 +0000658 bool IsIndirect = Subtarget->isTargetDarwin() && GVIsIndirectSymbol(GV);
Evan Cheng10043e22007-01-19 07:51:42 +0000659 SDOperand CPAddr;
660 if (RelocM == Reloc::Static)
661 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
662 else {
663 unsigned PCAdj = (RelocM != Reloc::PIC_)
664 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Cheng83f35172007-01-30 20:37:08 +0000665 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
666 : ARMCP::CPValue;
Evan Cheng10043e22007-01-19 07:51:42 +0000667 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Cheng83f35172007-01-30 20:37:08 +0000668 Kind, PCAdj);
Evan Cheng10043e22007-01-19 07:51:42 +0000669 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
670 }
671 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
672
673 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
674 SDOperand Chain = Result.getValue(1);
675
676 if (RelocM == Reloc::PIC_) {
677 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
678 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
679 }
680 if (IsIndirect)
681 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
682
683 return Result;
684}
685
686static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
687 unsigned VarArgsFrameIndex) {
688 // vastart just stores the address of the VarArgsFrameIndex slot into the
689 // memory location argument.
690 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
691 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
692 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
693 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
694 SV->getOffset());
695}
696
697static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
698 unsigned *vRegs, unsigned ArgNo,
699 unsigned &NumGPRs, unsigned &ArgOffset) {
700 MachineFunction &MF = DAG.getMachineFunction();
701 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
702 SDOperand Root = Op.getOperand(0);
703 std::vector<SDOperand> ArgValues;
704 SSARegMap *RegMap = MF.getSSARegMap();
705
706 static const unsigned GPRArgRegs[] = {
707 ARM::R0, ARM::R1, ARM::R2, ARM::R3
708 };
709
710 unsigned ObjSize = 0;
711 unsigned ObjGPRs = 0;
712 HowToPassArgument(ObjectVT, NumGPRs, ObjSize, ObjGPRs);
713
714 SDOperand ArgValue;
715 if (ObjGPRs == 1) {
716 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
717 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
718 vRegs[NumGPRs] = VReg;
719 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
720 if (ObjectVT == MVT::f32)
721 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
722 } else if (ObjGPRs == 2) {
723 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
724 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
725 vRegs[NumGPRs] = VReg;
726 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
727
728 VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
729 MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
730 vRegs[NumGPRs+1] = VReg;
731 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
732
733 if (ObjectVT == MVT::i64)
734 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
735 else
736 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
737 }
738 NumGPRs += ObjGPRs;
739
740 if (ObjSize) {
741 // If the argument is actually used, emit a load from the right stack
742 // slot.
743 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
744 MachineFrameInfo *MFI = MF.getFrameInfo();
745 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
746 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
747 if (ObjGPRs == 0)
748 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
749 else {
750 SDOperand ArgValue2 =
751 DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
752 if (ObjectVT == MVT::i64)
753 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
754 else
755 ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
756 }
757 } else {
758 // Don't emit a dead load.
759 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
760 }
761
762 ArgOffset += ObjSize; // Move on to the next argument.
763 }
764
765 return ArgValue;
766}
767
768SDOperand
769ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
770 std::vector<SDOperand> ArgValues;
771 SDOperand Root = Op.getOperand(0);
772 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
773 unsigned NumGPRs = 0; // GPRs used for parameter passing.
774 unsigned VRegs[4];
775
776 unsigned NumArgs = Op.Val->getNumValues()-1;
777 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
778 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
779 NumGPRs, ArgOffset));
780
781 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
782 if (isVarArg) {
783 static const unsigned GPRArgRegs[] = {
784 ARM::R0, ARM::R1, ARM::R2, ARM::R3
785 };
786
787 MachineFunction &MF = DAG.getMachineFunction();
788 SSARegMap *RegMap = MF.getSSARegMap();
789 MachineFrameInfo *MFI = MF.getFrameInfo();
790 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
791 unsigned VARegSaveSize = (4 - NumGPRs) * 4;
792 if (VARegSaveSize) {
793 // If this function is vararg, store any remaining integer argument regs
794 // to their spots on the stack so that they may be loaded by deferencing
795 // the result of va_next.
796 AFI->setVarArgsRegSaveSize(VARegSaveSize);
797 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset);
798 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
799
800 SmallVector<SDOperand, 4> MemOps;
801 for (; NumGPRs < 4; ++NumGPRs) {
802 unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass);
803 MF.addLiveIn(GPRArgRegs[NumGPRs], VReg);
804 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
805 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
806 MemOps.push_back(Store);
807 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
808 DAG.getConstant(4, getPointerTy()));
809 }
810 if (!MemOps.empty())
811 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
812 &MemOps[0], MemOps.size());
813 } else
814 // This will point to the next argument passed via stack.
815 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
816 }
817
818 ArgValues.push_back(Root);
819
820 // Return the new list of results.
821 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
822 Op.Val->value_end());
823 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
824}
825
826/// isFloatingPointZero - Return true if this is +0.0.
827static bool isFloatingPointZero(SDOperand Op) {
828 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
829 return CFP->isExactlyValue(0.0);
830 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
831 // Maybe this has already been legalized into the constant pool?
832 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
833 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
834 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
835 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
836 return CFP->isExactlyValue(0.0);
837 }
838 }
839 return false;
840}
841
842static bool isLegalCmpImmediate(int C, bool isThumb) {
843 return ( isThumb && (C & ~255U) == 0) ||
844 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
845}
846
847/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
848/// the given operands.
849static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
850 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
851 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
852 int C = (int)RHSC->getValue();
853 if (!isLegalCmpImmediate(C, isThumb)) {
854 // Constant does not fit, try adjusting it by one?
855 switch (CC) {
856 default: break;
857 case ISD::SETLT:
858 case ISD::SETULT:
859 case ISD::SETGE:
860 case ISD::SETUGE:
861 if (isLegalCmpImmediate(C-1, isThumb)) {
862 switch (CC) {
863 default: break;
864 case ISD::SETLT: CC = ISD::SETLE; break;
865 case ISD::SETULT: CC = ISD::SETULE; break;
866 case ISD::SETGE: CC = ISD::SETGT; break;
867 case ISD::SETUGE: CC = ISD::SETUGT; break;
868 }
869 RHS = DAG.getConstant(C-1, MVT::i32);
870 }
871 break;
872 case ISD::SETLE:
873 case ISD::SETULE:
874 case ISD::SETGT:
875 case ISD::SETUGT:
876 if (isLegalCmpImmediate(C+1, isThumb)) {
877 switch (CC) {
878 default: break;
879 case ISD::SETLE: CC = ISD::SETLT; break;
880 case ISD::SETULE: CC = ISD::SETULT; break;
881 case ISD::SETGT: CC = ISD::SETGE; break;
882 case ISD::SETUGT: CC = ISD::SETUGE; break;
883 }
884 RHS = DAG.getConstant(C+1, MVT::i32);
885 }
886 break;
887 }
888 }
889 }
890
891 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
892 ARMCC = DAG.getConstant(CondCode, MVT::i32);
893 return DAG.getNode(ARMISD::CMP, MVT::Flag, LHS, RHS);
894}
895
896/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
897static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
898 SDOperand Cmp;
899 if (!isFloatingPointZero(RHS))
900 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
901 else
902 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
903 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
904}
905
906static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
907 const ARMSubtarget *ST) {
908 MVT::ValueType VT = Op.getValueType();
909 SDOperand LHS = Op.getOperand(0);
910 SDOperand RHS = Op.getOperand(1);
911 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
912 SDOperand TrueVal = Op.getOperand(2);
913 SDOperand FalseVal = Op.getOperand(3);
914
915 if (LHS.getValueType() == MVT::i32) {
916 SDOperand ARMCC;
917 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
918 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, Cmp);
919 }
920
921 ARMCC::CondCodes CondCode, CondCode2;
922 if (FPCCToARMCC(CC, CondCode, CondCode2))
923 std::swap(TrueVal, FalseVal);
924
925 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
926 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
927 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
928 ARMCC, Cmp);
929 if (CondCode2 != ARMCC::AL) {
930 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
931 // FIXME: Needs another CMP because flag can have but one use.
932 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
933 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, Cmp2);
934 }
935 return Result;
936}
937
938static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
939 const ARMSubtarget *ST) {
940 SDOperand Chain = Op.getOperand(0);
941 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
942 SDOperand LHS = Op.getOperand(2);
943 SDOperand RHS = Op.getOperand(3);
944 SDOperand Dest = Op.getOperand(4);
945
946 if (LHS.getValueType() == MVT::i32) {
947 SDOperand ARMCC;
948 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
949 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, Cmp);
950 }
951
952 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
953 ARMCC::CondCodes CondCode, CondCode2;
954 if (FPCCToARMCC(CC, CondCode, CondCode2))
955 // Swap the LHS/RHS of the comparison if needed.
956 std::swap(LHS, RHS);
957
958 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
959 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
960 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
961 SDOperand Ops[] = { Chain, Dest, ARMCC, Cmp };
962 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
963 if (CondCode2 != ARMCC::AL) {
964 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
965 SDOperand Ops[] = { Res, Dest, ARMCC, Res.getValue(1) };
966 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 4);
967 }
968 return Res;
969}
970
971SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
972 SDOperand Chain = Op.getOperand(0);
973 SDOperand Table = Op.getOperand(1);
974 SDOperand Index = Op.getOperand(2);
975
976 MVT::ValueType PTy = getPointerTy();
977 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
978 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
979 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
980 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
981 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
982 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
983 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
984 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
985 Addr = DAG.getLoad(isPIC ? MVT::i32 : PTy, Chain, Addr, NULL, 0);
986 Chain = Addr.getValue(1);
987 if (isPIC)
988 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
989 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
990}
991
992static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
993 unsigned Opc =
994 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
995 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
996 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
997}
998
999static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1000 MVT::ValueType VT = Op.getValueType();
1001 unsigned Opc =
1002 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1003
1004 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1005 return DAG.getNode(Opc, VT, Op);
1006}
1007
1008static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1009 // Implement fcopysign with a fabs and a conditional fneg.
1010 SDOperand Tmp0 = Op.getOperand(0);
1011 SDOperand Tmp1 = Op.getOperand(1);
1012 MVT::ValueType VT = Op.getValueType();
1013 MVT::ValueType SrcVT = Tmp1.getValueType();
1014 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1015 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1016 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1017 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, Cmp);
1018}
1019
1020static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) {
1021 // Turn f64->i64 into FMRRD.
1022 assert(Op.getValueType() == MVT::i64 &&
1023 Op.getOperand(0).getValueType() == MVT::f64);
1024
1025 Op = Op.getOperand(0);
1026 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1027 &Op, 1);
1028
1029 // Merge the pieces into a single i64 value.
1030 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1031}
1032
1033static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG) {
1034 // FIXME: All this code is target-independent. Create a new target-indep
1035 // MULHILO node and move this code to the legalizer.
1036 //
1037 assert(Op.getValueType() == MVT::i64 && "Only handles i64 expand right now!");
1038
1039 SDOperand LL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1040 DAG.getConstant(0, MVT::i32));
1041 SDOperand RL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1042 DAG.getConstant(0, MVT::i32));
1043
1044 const TargetLowering &TL = DAG.getTargetLoweringInfo();
1045 unsigned LHSSB = TL.ComputeNumSignBits(Op.getOperand(0));
1046 unsigned RHSSB = TL.ComputeNumSignBits(Op.getOperand(1));
1047
1048 SDOperand Lo, Hi;
1049 // Figure out how to lower this multiply.
1050 if (LHSSB >= 33 && RHSSB >= 33) {
1051 // If the input values are both sign extended, we can emit a mulhs+mul.
1052 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1053 Hi = DAG.getNode(ISD::MULHS, MVT::i32, LL, RL);
1054 } else if (LHSSB == 32 && RHSSB == 32 &&
1055 TL.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
1056 TL.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
1057 // If the inputs are zero extended, use mulhu.
1058 Lo = DAG.getNode(ISD::MUL, MVT::i32, LL, RL);
1059 Hi = DAG.getNode(ISD::MULHU, MVT::i32, LL, RL);
1060 } else {
1061 SDOperand LH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1062 DAG.getConstant(1, MVT::i32));
1063 SDOperand RH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(1),
1064 DAG.getConstant(1, MVT::i32));
1065
1066 // Lo,Hi = umul LHS, RHS.
1067 SDOperand Ops[] = { LL, RL };
1068 SDOperand UMul64 = DAG.getNode(ARMISD::MULHILOU,
1069 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2);
1070 Lo = UMul64;
1071 Hi = UMul64.getValue(1);
1072 RH = DAG.getNode(ISD::MUL, MVT::i32, LL, RH);
1073 LH = DAG.getNode(ISD::MUL, MVT::i32, LH, RL);
1074 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, RH);
1075 Hi = DAG.getNode(ISD::ADD, MVT::i32, Hi, LH);
1076 }
1077
1078 // Merge the pieces into a single i64 value.
1079 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1080}
1081
1082static SDOperand LowerMULHU(SDOperand Op, SelectionDAG &DAG) {
1083 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1084 return DAG.getNode(ARMISD::MULHILOU,
1085 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1086}
1087
1088static SDOperand LowerMULHS(SDOperand Op, SelectionDAG &DAG) {
1089 SDOperand Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1090 return DAG.getNode(ARMISD::MULHILOS,
1091 DAG.getVTList(MVT::i32, MVT::i32), Ops, 2).getValue(1);
1092}
1093
1094static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG,
1095 const ARMSubtarget *ST) {
1096 assert(Op.getValueType() == MVT::i64 &&
1097 (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
1098 "Unknown shift to lower!");
1099
1100 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1101 if (!isa<ConstantSDNode>(Op.getOperand(1)) ||
1102 cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1)
1103 return SDOperand();
1104
1105 // If we are in thumb mode, we don't have RRX.
1106 if (ST->isThumb()) return SDOperand();
1107
1108 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1109 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1110 DAG.getConstant(0, MVT::i32));
1111 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
1112 DAG.getConstant(1, MVT::i32));
1113
1114 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1115 // captures the result into a carry flag.
1116 unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1117 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1118
1119 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1120 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1121
1122 // Merge the pieces into a single i64 value.
1123 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1124}
1125
1126SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1127 switch (Op.getOpcode()) {
1128 default: assert(0 && "Don't know how to custom lower this!"); abort();
1129 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1130 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1131 case ISD::CALL: return LowerCALL(Op, DAG);
1132 case ISD::RET: return LowerRET(Op, DAG);
1133 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1134 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1135 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1136 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1137 case ISD::SINT_TO_FP:
1138 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1139 case ISD::FP_TO_SINT:
1140 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1141 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1142 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
1143 case ISD::MUL: return LowerMUL(Op, DAG);
1144 case ISD::MULHU: return LowerMULHU(Op, DAG);
1145 case ISD::MULHS: return LowerMULHS(Op, DAG);
1146 case ISD::SRL:
1147 case ISD::SRA: return LowerSRx(Op, DAG, Subtarget);
1148 case ISD::FORMAL_ARGUMENTS:
1149 return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00001150 case ISD::RETURNADDR: break;
1151 case ISD::FRAMEADDR: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001152 }
Nate Begemaneda59972007-01-29 22:58:52 +00001153 return SDOperand();
Evan Cheng10043e22007-01-19 07:51:42 +00001154}
1155
1156//===----------------------------------------------------------------------===//
1157// ARM Scheduler Hooks
1158//===----------------------------------------------------------------------===//
1159
1160MachineBasicBlock *
1161ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1162 MachineBasicBlock *BB) {
1163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1164 switch (MI->getOpcode()) {
1165 default: assert(false && "Unexpected instr type to insert");
1166 case ARM::tMOVCCr: {
1167 // To "insert" a SELECT_CC instruction, we actually have to insert the
1168 // diamond control-flow pattern. The incoming instruction knows the
1169 // destination vreg to set, the condition code register to branch on, the
1170 // true/false values to select between, and a branch opcode to use.
1171 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1172 ilist<MachineBasicBlock>::iterator It = BB;
1173 ++It;
1174
1175 // thisMBB:
1176 // ...
1177 // TrueVal = ...
1178 // cmpTY ccX, r1, r2
1179 // bCC copy1MBB
1180 // fallthrough --> copy0MBB
1181 MachineBasicBlock *thisMBB = BB;
1182 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1183 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1184 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1185 .addImm(MI->getOperand(3).getImm());
1186 MachineFunction *F = BB->getParent();
1187 F->getBasicBlockList().insert(It, copy0MBB);
1188 F->getBasicBlockList().insert(It, sinkMBB);
1189 // Update machine-CFG edges by first adding all successors of the current
1190 // block to the new block which will contain the Phi node for the select.
1191 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1192 e = BB->succ_end(); i != e; ++i)
1193 sinkMBB->addSuccessor(*i);
1194 // Next, remove all successors of the current block, and add the true
1195 // and fallthrough blocks as its successors.
1196 while(!BB->succ_empty())
1197 BB->removeSuccessor(BB->succ_begin());
1198 BB->addSuccessor(copy0MBB);
1199 BB->addSuccessor(sinkMBB);
1200
1201 // copy0MBB:
1202 // %FalseValue = ...
1203 // # fallthrough to sinkMBB
1204 BB = copy0MBB;
1205
1206 // Update machine-CFG edges
1207 BB->addSuccessor(sinkMBB);
1208
1209 // sinkMBB:
1210 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1211 // ...
1212 BB = sinkMBB;
1213 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1214 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1215 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1216
1217 delete MI; // The pseudo instruction is gone now.
1218 return BB;
1219 }
1220 }
1221}
1222
1223//===----------------------------------------------------------------------===//
1224// ARM Optimization Hooks
1225//===----------------------------------------------------------------------===//
1226
1227/// isLegalAddressImmediate - Return true if the integer value or
1228/// GlobalValue can be used as the offset of the target addressing mode.
1229bool ARMTargetLowering::isLegalAddressImmediate(int64_t V) const {
1230 // ARM allows a 12-bit immediate field.
1231 return V == V & ((1LL << 12) - 1);
1232}
1233
1234bool ARMTargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
1235 return false;
1236}
1237
1238static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1239 bool isSEXTLoad, SDOperand &Base,
1240 SDOperand &Offset, bool &isInc,
1241 SelectionDAG &DAG) {
1242 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1243 return false;
1244
1245 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1246 // AddressingMode 3
1247 Base = Ptr->getOperand(0);
1248 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1249 int RHSC = (int)RHS->getValue();
1250 if (RHSC < 0 && RHSC > -256) {
1251 isInc = false;
1252 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1253 return true;
1254 }
1255 }
1256 isInc = (Ptr->getOpcode() == ISD::ADD);
1257 Offset = Ptr->getOperand(1);
1258 return true;
1259 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1260 // AddressingMode 2
1261 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1262 int RHSC = (int)RHS->getValue();
1263 if (RHSC < 0 && RHSC > -0x1000) {
1264 isInc = false;
1265 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1266 Base = Ptr->getOperand(0);
1267 return true;
1268 }
1269 }
1270
1271 if (Ptr->getOpcode() == ISD::ADD) {
1272 isInc = true;
1273 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1274 if (ShOpcVal != ARM_AM::no_shift) {
1275 Base = Ptr->getOperand(1);
1276 Offset = Ptr->getOperand(0);
1277 } else {
1278 Base = Ptr->getOperand(0);
1279 Offset = Ptr->getOperand(1);
1280 }
1281 return true;
1282 }
1283
1284 isInc = (Ptr->getOpcode() == ISD::ADD);
1285 Base = Ptr->getOperand(0);
1286 Offset = Ptr->getOperand(1);
1287 return true;
1288 }
1289
1290 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1291 return false;
1292}
1293
1294/// getPreIndexedAddressParts - returns true by value, base pointer and
1295/// offset pointer and addressing mode by reference if the node's address
1296/// can be legally represented as pre-indexed load / store address.
1297bool
1298ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1299 SDOperand &Offset,
1300 ISD::MemIndexedMode &AM,
1301 SelectionDAG &DAG) {
1302 if (Subtarget->isThumb())
1303 return false;
1304
1305 MVT::ValueType VT;
1306 SDOperand Ptr;
1307 bool isSEXTLoad = false;
1308 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1309 Ptr = LD->getBasePtr();
1310 VT = LD->getLoadedVT();
1311 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1312 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1313 Ptr = ST->getBasePtr();
1314 VT = ST->getStoredVT();
1315 } else
1316 return false;
1317
1318 bool isInc;
1319 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1320 isInc, DAG);
1321 if (isLegal) {
1322 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1323 return true;
1324 }
1325 return false;
1326}
1327
1328/// getPostIndexedAddressParts - returns true by value, base pointer and
1329/// offset pointer and addressing mode by reference if this node can be
1330/// combined with a load / store to form a post-indexed load / store.
1331bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1332 SDOperand &Base,
1333 SDOperand &Offset,
1334 ISD::MemIndexedMode &AM,
1335 SelectionDAG &DAG) {
1336 if (Subtarget->isThumb())
1337 return false;
1338
1339 MVT::ValueType VT;
1340 SDOperand Ptr;
1341 bool isSEXTLoad = false;
1342 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1343 VT = LD->getLoadedVT();
1344 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1345 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1346 VT = ST->getStoredVT();
1347 } else
1348 return false;
1349
1350 bool isInc;
1351 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1352 isInc, DAG);
1353 if (isLegal) {
1354 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1355 return true;
1356 }
1357 return false;
1358}
1359
1360void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1361 uint64_t Mask,
1362 uint64_t &KnownZero,
1363 uint64_t &KnownOne,
1364 unsigned Depth) const {
1365 KnownZero = 0;
1366 KnownOne = 0;
1367 switch (Op.getOpcode()) {
1368 default: break;
1369 case ARMISD::CMOV: {
1370 // Bits are known zero/one if known on the LHS and RHS.
1371 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1372 if (KnownZero == 0 && KnownOne == 0) return;
1373
1374 uint64_t KnownZeroRHS, KnownOneRHS;
1375 ComputeMaskedBits(Op.getOperand(1), Mask,
1376 KnownZeroRHS, KnownOneRHS, Depth+1);
1377 KnownZero &= KnownZeroRHS;
1378 KnownOne &= KnownOneRHS;
1379 return;
1380 }
1381 }
1382}
1383
1384//===----------------------------------------------------------------------===//
1385// ARM Inline Assembly Support
1386//===----------------------------------------------------------------------===//
1387
1388/// getConstraintType - Given a constraint letter, return the type of
1389/// constraint it is for this target.
1390ARMTargetLowering::ConstraintType
1391ARMTargetLowering::getConstraintType(char ConstraintLetter) const {
1392 switch (ConstraintLetter) {
1393 case 'l':
1394 return C_RegisterClass;
1395 default: return TargetLowering::getConstraintType(ConstraintLetter);
1396 }
1397}
1398
1399std::pair<unsigned, const TargetRegisterClass*>
1400ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1401 MVT::ValueType VT) const {
1402 if (Constraint.size() == 1) {
1403 // GCC RS6000 Constraint Letters
1404 switch (Constraint[0]) {
1405 case 'l':
1406 // FIXME: in thumb mode, 'l' is only low-regs.
1407 // FALL THROUGH.
1408 case 'r':
1409 return std::make_pair(0U, ARM::GPRRegisterClass);
1410 break;
1411 }
1412 }
1413 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1414}
1415
1416std::vector<unsigned> ARMTargetLowering::
1417getRegClassForInlineAsmConstraint(const std::string &Constraint,
1418 MVT::ValueType VT) const {
1419 if (Constraint.size() != 1)
1420 return std::vector<unsigned>();
1421
1422 switch (Constraint[0]) { // GCC ARM Constraint Letters
1423 default: break;
1424 case 'l':
1425 case 'r':
1426 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1427 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1428 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1429 ARM::R12, ARM::LR, 0);
1430 }
1431
1432 return std::vector<unsigned>();
1433}