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