blob: 79a75863487f628fc471c007c334cad2183835c5 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
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"
Evan Cheng27707472007-03-16 08:43:56 +000025#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000026#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/GlobalValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000034#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000035#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000036#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037using namespace llvm;
38
39ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
40 : TargetLowering(TM), ARMPCLabelIndex(0) {
41 Subtarget = &TM.getSubtarget<ARMSubtarget>();
42
Evan Chengb1df8f22007-04-27 08:15:43 +000043 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +000044 // Uses VFP for Thumb libfuncs if available.
45 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
46 // Single-precision floating-point arithmetic.
47 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
48 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
49 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
50 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000051
Evan Chengb1df8f22007-04-27 08:15:43 +000052 // Double-precision floating-point arithmetic.
53 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
54 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
55 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
56 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000057
Evan Chengb1df8f22007-04-27 08:15:43 +000058 // Single-precision comparisons.
59 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
60 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
61 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
62 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
63 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
64 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
65 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
66 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000067
Evan Chengb1df8f22007-04-27 08:15:43 +000068 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
69 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
70 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
71 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
72 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000076
Evan Chengb1df8f22007-04-27 08:15:43 +000077 // Double-precision comparisons.
78 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
79 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
80 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
81 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
82 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
83 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
84 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
85 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000086
Evan Chengb1df8f22007-04-27 08:15:43 +000087 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
88 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
89 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
90 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
91 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000095
Evan Chengb1df8f22007-04-27 08:15:43 +000096 // Floating-point to integer conversions.
97 // i64 conversions are done via library routines even when generating VFP
98 // instructions, so use the same ones.
99 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
100 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
101 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
102 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000103
Evan Chengb1df8f22007-04-27 08:15:43 +0000104 // Conversions between floating types.
105 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
106 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
107
108 // Integer to floating-point conversions.
109 // i64 conversions are done via library routines even when generating VFP
110 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000111 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
112 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000113 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
114 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
115 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
116 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
117 }
Evan Chenga8e29892007-01-19 07:51:42 +0000118 }
119
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000120 if (Subtarget->isThumb())
121 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
122 else
123 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000124 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000125 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
126 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000127
Chris Lattnerddf89562008-01-17 19:59:44 +0000128 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000129 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000130 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000131
132 // ARM does not have f32 extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000133 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000134
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000135 // ARM does not have i1 sign extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000136 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000137
Evan Chenga8e29892007-01-19 07:51:42 +0000138 // ARM supports all 4 flavors of integer indexed load / store.
139 for (unsigned im = (unsigned)ISD::PRE_INC;
140 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
141 setIndexedLoadAction(im, MVT::i1, Legal);
142 setIndexedLoadAction(im, MVT::i8, Legal);
143 setIndexedLoadAction(im, MVT::i16, Legal);
144 setIndexedLoadAction(im, MVT::i32, Legal);
145 setIndexedStoreAction(im, MVT::i1, Legal);
146 setIndexedStoreAction(im, MVT::i8, Legal);
147 setIndexedStoreAction(im, MVT::i16, Legal);
148 setIndexedStoreAction(im, MVT::i32, Legal);
149 }
150
151 // i64 operation support.
152 if (Subtarget->isThumb()) {
153 setOperationAction(ISD::MUL, MVT::i64, Expand);
154 setOperationAction(ISD::MULHU, MVT::i32, Expand);
155 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000156 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
157 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000158 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000159 setOperationAction(ISD::MUL, MVT::i64, Expand);
160 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000161 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000162 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000163 }
164 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
165 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
166 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRL, MVT::i64, Custom);
168 setOperationAction(ISD::SRA, MVT::i64, Custom);
169
170 // ARM does not have ROTL.
171 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000172 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000173 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000174 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000175 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
176
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000177 // Only ARMv6 has BSWAP.
178 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000179 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000180
Evan Chenga8e29892007-01-19 07:51:42 +0000181 // These are expanded into libcalls.
182 setOperationAction(ISD::SDIV, MVT::i32, Expand);
183 setOperationAction(ISD::UDIV, MVT::i32, Expand);
184 setOperationAction(ISD::SREM, MVT::i32, Expand);
185 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000186 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
187 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000188
Evan Chenga8e29892007-01-19 07:51:42 +0000189 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000190 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000191 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000192
193 setOperationAction(ISD::RET, MVT::Other, Custom);
194 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
195 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000196 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000197 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000198
Evan Chenga8e29892007-01-19 07:51:42 +0000199 // Use the default implementation.
Bob Wilson2dc4f542009-03-20 22:42:55 +0000200 setOperationAction(ISD::VASTART, MVT::Other, Custom);
201 setOperationAction(ISD::VAARG, MVT::Other, Expand);
202 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
203 setOperationAction(ISD::VAEND, MVT::Other, Expand);
204 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000205 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000206 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
207 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000208
209 if (!Subtarget->hasV6Ops()) {
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
212 }
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
214
Evan Chengb6ab2542007-01-31 08:40:13 +0000215 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chengc7c77292008-11-04 19:57:48 +0000216 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Evan Chenga8e29892007-01-19 07:51:42 +0000217 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000218
219 // We want to custom lower some of our intrinsics.
220 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
221
Bob Wilson2dc4f542009-03-20 22:42:55 +0000222 setOperationAction(ISD::SETCC, MVT::i32, Expand);
223 setOperationAction(ISD::SETCC, MVT::f32, Expand);
224 setOperationAction(ISD::SETCC, MVT::f64, Expand);
225 setOperationAction(ISD::SELECT, MVT::i32, Expand);
226 setOperationAction(ISD::SELECT, MVT::f32, Expand);
227 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000228 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
229 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
230 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
231
Bob Wilson2dc4f542009-03-20 22:42:55 +0000232 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
233 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
234 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
235 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
236 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000237
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000238 // We don't support sin/cos/fmod/copysign/pow
Bob Wilson2dc4f542009-03-20 22:42:55 +0000239 setOperationAction(ISD::FSIN, MVT::f64, Expand);
240 setOperationAction(ISD::FSIN, MVT::f32, Expand);
241 setOperationAction(ISD::FCOS, MVT::f32, Expand);
242 setOperationAction(ISD::FCOS, MVT::f64, Expand);
243 setOperationAction(ISD::FREM, MVT::f64, Expand);
244 setOperationAction(ISD::FREM, MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000245 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
246 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
247 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
248 }
Bob Wilson2dc4f542009-03-20 22:42:55 +0000249 setOperationAction(ISD::FPOW, MVT::f64, Expand);
250 setOperationAction(ISD::FPOW, MVT::f32, Expand);
251
Evan Chenga8e29892007-01-19 07:51:42 +0000252 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000253 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
254 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
255 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
256 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
257 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
258 }
Evan Chenga8e29892007-01-19 07:51:42 +0000259
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000260 // We have target-specific dag combine patterns for the following nodes:
261 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000262 setTargetDAGCombine(ISD::ADD);
263 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000264
Evan Chenga8e29892007-01-19 07:51:42 +0000265 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000266 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000267 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000268 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000269
270 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000271}
272
Evan Chenga8e29892007-01-19 07:51:42 +0000273const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
274 switch (Opcode) {
275 default: return 0;
276 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000277 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
278 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000279 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000280 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
281 case ARMISD::tCALL: return "ARMISD::tCALL";
282 case ARMISD::BRCOND: return "ARMISD::BRCOND";
283 case ARMISD::BR_JT: return "ARMISD::BR_JT";
284 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
285 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
286 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000287 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000288 case ARMISD::CMPFP: return "ARMISD::CMPFP";
289 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
290 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
291 case ARMISD::CMOV: return "ARMISD::CMOV";
292 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000293
Evan Chenga8e29892007-01-19 07:51:42 +0000294 case ARMISD::FTOSI: return "ARMISD::FTOSI";
295 case ARMISD::FTOUI: return "ARMISD::FTOUI";
296 case ARMISD::SITOF: return "ARMISD::SITOF";
297 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000298
299 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
300 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
301 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000302
Evan Chenga8e29892007-01-19 07:51:42 +0000303 case ARMISD::FMRRD: return "ARMISD::FMRRD";
304 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000305
306 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000307 }
308}
309
310//===----------------------------------------------------------------------===//
311// Lowering Code
312//===----------------------------------------------------------------------===//
313
Evan Chenga8e29892007-01-19 07:51:42 +0000314/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
315static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
316 switch (CC) {
317 default: assert(0 && "Unknown condition code!");
318 case ISD::SETNE: return ARMCC::NE;
319 case ISD::SETEQ: return ARMCC::EQ;
320 case ISD::SETGT: return ARMCC::GT;
321 case ISD::SETGE: return ARMCC::GE;
322 case ISD::SETLT: return ARMCC::LT;
323 case ISD::SETLE: return ARMCC::LE;
324 case ISD::SETUGT: return ARMCC::HI;
325 case ISD::SETUGE: return ARMCC::HS;
326 case ISD::SETULT: return ARMCC::LO;
327 case ISD::SETULE: return ARMCC::LS;
328 }
329}
330
331/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
332/// returns true if the operands should be inverted to form the proper
333/// comparison.
334static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
335 ARMCC::CondCodes &CondCode2) {
336 bool Invert = false;
337 CondCode2 = ARMCC::AL;
338 switch (CC) {
339 default: assert(0 && "Unknown FP condition!");
340 case ISD::SETEQ:
341 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
342 case ISD::SETGT:
343 case ISD::SETOGT: CondCode = ARMCC::GT; break;
344 case ISD::SETGE:
345 case ISD::SETOGE: CondCode = ARMCC::GE; break;
346 case ISD::SETOLT: CondCode = ARMCC::MI; break;
347 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
348 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
349 case ISD::SETO: CondCode = ARMCC::VC; break;
350 case ISD::SETUO: CondCode = ARMCC::VS; break;
351 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
352 case ISD::SETUGT: CondCode = ARMCC::HI; break;
353 case ISD::SETUGE: CondCode = ARMCC::PL; break;
354 case ISD::SETLT:
355 case ISD::SETULT: CondCode = ARMCC::LT; break;
356 case ISD::SETLE:
357 case ISD::SETULE: CondCode = ARMCC::LE; break;
358 case ISD::SETNE:
359 case ISD::SETUNE: CondCode = ARMCC::NE; break;
360 }
361 return Invert;
362}
363
364static void
Duncan Sands83ec4b62008-06-06 12:08:01 +0000365HowToPassArgument(MVT ObjectVT, unsigned NumGPRs,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000366 unsigned StackOffset, unsigned &NeededGPRs,
367 unsigned &NeededStackSize, unsigned &GPRPad,
Duncan Sands276dcbd2008-03-21 09:14:45 +0000368 unsigned &StackPad, ISD::ArgFlagsTy Flags) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000369 NeededStackSize = 0;
370 NeededGPRs = 0;
371 StackPad = 0;
372 GPRPad = 0;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000373 unsigned align = Flags.getOrigAlign();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000374 GPRPad = NumGPRs % ((align + 3)/4);
375 StackPad = StackOffset % align;
376 unsigned firstGPR = NumGPRs + GPRPad;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000377 switch (ObjectVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000378 default: assert(0 && "Unhandled argument type!");
379 case MVT::i32:
380 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000381 if (firstGPR < 4)
382 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000383 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000384 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000385 break;
386 case MVT::i64:
387 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000388 if (firstGPR < 3)
389 NeededGPRs = 2;
390 else if (firstGPR == 3) {
391 NeededGPRs = 1;
392 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000393 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000394 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000395 }
396}
397
Evan Chengfc403422007-02-03 08:53:01 +0000398/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
399/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
400/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000401SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000402 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
403 MVT RetVT = TheCall->getRetValType(0);
404 SDValue Chain = TheCall->getChain();
Chris Lattner4469c532009-01-25 23:08:00 +0000405 assert((TheCall->getCallingConv() == CallingConv::C ||
406 TheCall->getCallingConv() == CallingConv::Fast) &&
407 "unknown calling convention");
Dan Gohman095cc292008-09-13 01:54:27 +0000408 SDValue Callee = TheCall->getCallee();
Bob Wilson2a14c522009-03-20 23:16:43 +0000409 unsigned NumOps = TheCall->getNumArgs();
410 DebugLoc dl = TheCall->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000411 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
412 unsigned NumGPRs = 0; // GPRs used for parameter passing.
413
414 // Count how many bytes are to be pushed on the stack.
415 unsigned NumBytes = 0;
416
417 // Add up all the space actually used.
418 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000419 unsigned ObjSize;
420 unsigned ObjGPRs;
421 unsigned StackPad;
422 unsigned GPRPad;
Dan Gohman095cc292008-09-13 01:54:27 +0000423 MVT ObjectVT = TheCall->getArg(i).getValueType();
424 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000425 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
426 GPRPad, StackPad, Flags);
427 NumBytes += ObjSize + StackPad;
428 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000429 }
430
431 // Adjust the stack pointer for the new arguments...
432 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000433 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000434
Dan Gohman475871a2008-07-27 21:46:04 +0000435 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000436
437 static const unsigned GPRArgRegs[] = {
438 ARM::R0, ARM::R1, ARM::R2, ARM::R3
439 };
440
441 NumGPRs = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000442 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
443 std::vector<SDValue> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000444 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +0000445 SDValue Arg = TheCall->getArg(i);
446 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000447 MVT ArgVT = Arg.getValueType();
Evan Chenga8e29892007-01-19 07:51:42 +0000448
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000449 unsigned ObjSize;
450 unsigned ObjGPRs;
451 unsigned GPRPad;
452 unsigned StackPad;
453 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
454 ObjSize, GPRPad, StackPad, Flags);
455 NumGPRs += GPRPad;
456 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000457 if (ObjGPRs > 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000458 switch (ArgVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000459 default: assert(0 && "Unexpected ValueType for argument!");
460 case MVT::i32:
461 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
462 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000463 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000464 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
Dale Johannesen33c960f2009-02-04 20:06:27 +0000465 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg)));
Evan Chenga8e29892007-01-19 07:51:42 +0000466 break;
467 case MVT::i64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000468 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bob Wilson2a14c522009-03-20 23:16:43 +0000469 DAG.getConstant(0, getPointerTy()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000470 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bob Wilson2a14c522009-03-20 23:16:43 +0000471 DAG.getConstant(1, getPointerTy()));
Evan Chenga8e29892007-01-19 07:51:42 +0000472 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
473 if (ObjGPRs == 2)
474 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
475 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000476 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000477 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
478 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff, NULL, 0));
Evan Chenga8e29892007-01-19 07:51:42 +0000479 }
480 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000481 }
Evan Chenga8e29892007-01-19 07:51:42 +0000482 case MVT::f64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000483 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Bob Wilson2a14c522009-03-20 23:16:43 +0000484 DAG.getVTList(MVT::i32, MVT::i32),
485 &Arg, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000486 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
487 if (ObjGPRs == 2)
488 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
489 Cvt.getValue(1)));
490 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000491 SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000492 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
493 MemOpChains.push_back(DAG.getStore(Chain, dl, Cvt.getValue(1), PtrOff,
Evan Chenga8e29892007-01-19 07:51:42 +0000494 NULL, 0));
495 }
496 break;
497 }
498 }
499 } else {
500 assert(ObjSize != 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000501 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000502 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
503 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Evan Chenga8e29892007-01-19 07:51:42 +0000504 }
505
506 NumGPRs += ObjGPRs;
507 ArgOffset += ObjSize;
508 }
509
510 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000511 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000512 &MemOpChains[0], MemOpChains.size());
513
514 // Build a sequence of copy-to-reg nodes chained together with token chain
515 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000516 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000517 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000518 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000519 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 InFlag = Chain.getValue(1);
521 }
522
Bill Wendling056292f2008-09-16 21:48:12 +0000523 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
524 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
525 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000526 bool isDirect = false;
527 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000528 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000529 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
530 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000531 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000532 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000533 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000534 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000535 getTargetMachine().getRelocationModel() != Reloc::Static;
536 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000537 // ARM call to a local ARM function is predicable.
538 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000539 // tBX takes a register source operand.
540 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
541 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
542 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000543 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000544 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000545 Callee = DAG.getLoad(getPointerTy(), dl,
546 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000547 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000548 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000549 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000550 } else
551 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000552 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000553 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000554 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000555 getTargetMachine().getRelocationModel() != Reloc::Static;
556 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000557 // tBX takes a register source operand.
558 const char *Sym = S->getSymbol();
559 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
560 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
561 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000562 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000563 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000564 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000565 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000566 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000567 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000568 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000569 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000570 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000571 }
572
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000573 // FIXME: handle tail calls differently.
574 unsigned CallOpc;
575 if (Subtarget->isThumb()) {
576 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
577 CallOpc = ARMISD::CALL_NOLINK;
578 else
579 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
580 } else {
581 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000582 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
583 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000584 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000585 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
586 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Dale Johannesene8d72302009-02-06 23:05:02 +0000587 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000588 InFlag = Chain.getValue(1);
589 }
590
Dan Gohman475871a2008-07-27 21:46:04 +0000591 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000592 Ops.push_back(Chain);
593 Ops.push_back(Callee);
594
595 // Add argument registers to the end of the list so that they are known live
596 // into the call.
597 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
598 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
599 RegsToPass[i].second.getValueType()));
600
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000602 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000603 // Returns a chain and a flag for retval copy to use.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000604 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +0000605 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000606 InFlag = Chain.getValue(1);
607
Chris Lattnere563bbc2008-10-11 22:08:30 +0000608 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
609 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000610 if (RetVT != MVT::Other)
611 InFlag = Chain.getValue(1);
612
Dan Gohman475871a2008-07-27 21:46:04 +0000613 std::vector<SDValue> ResultVals;
Evan Chenga8e29892007-01-19 07:51:42 +0000614
615 // If the call has results, copy the values out of the ret val registers.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000616 switch (RetVT.getSimpleVT()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000617 default: assert(0 && "Unexpected ret value!");
618 case MVT::Other:
619 break;
620 case MVT::i32:
Bob Wilson2dc4f542009-03-20 22:42:55 +0000621 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000622 MVT::i32, InFlag).getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +0000623 ResultVals.push_back(Chain.getValue(0));
Dan Gohman095cc292008-09-13 01:54:27 +0000624 if (TheCall->getNumRetVals() > 1 &&
625 TheCall->getRetValType(1) == MVT::i32) {
Evan Chenga8e29892007-01-19 07:51:42 +0000626 // Returns a i64 value.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000627 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R1, MVT::i32,
Evan Chenga8e29892007-01-19 07:51:42 +0000628 Chain.getValue(2)).getValue(1);
629 ResultVals.push_back(Chain.getValue(0));
Evan Chenga8e29892007-01-19 07:51:42 +0000630 }
Evan Chenga8e29892007-01-19 07:51:42 +0000631 break;
632 case MVT::f32:
Bob Wilson2dc4f542009-03-20 22:42:55 +0000633 Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000634 MVT::i32, InFlag).getValue(1);
635 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32,
Evan Chenga8e29892007-01-19 07:51:42 +0000636 Chain.getValue(0)));
Evan Chenga8e29892007-01-19 07:51:42 +0000637 break;
638 case MVT::f64: {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000639 SDValue Lo = DAG.getCopyFromReg(Chain, dl, ARM::R0, MVT::i32, InFlag);
640 SDValue Hi = DAG.getCopyFromReg(Lo, dl, ARM::R1, MVT::i32, Lo.getValue(2));
641 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi));
Evan Chenga8e29892007-01-19 07:51:42 +0000642 break;
643 }
644 }
645
Evan Chenga8e29892007-01-19 07:51:42 +0000646 if (ResultVals.empty())
647 return Chain;
648
649 ResultVals.push_back(Chain);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000650 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000651 return Res.getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000652}
653
Dan Gohman475871a2008-07-27 21:46:04 +0000654static SDValue LowerRET(SDValue Op, SelectionDAG &DAG) {
655 SDValue Copy;
656 SDValue Chain = Op.getOperand(0);
Dale Johannesena05dca42009-02-04 23:02:30 +0000657 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000658 switch(Op.getNumOperands()) {
659 default:
660 assert(0 && "Do not know how to return this many arguments!");
661 abort();
662 case 1: {
Dan Gohman475871a2008-07-27 21:46:04 +0000663 SDValue LR = DAG.getRegister(ARM::LR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +0000664 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Evan Chenga8e29892007-01-19 07:51:42 +0000665 }
666 case 3:
667 Op = Op.getOperand(1);
668 if (Op.getValueType() == MVT::f32) {
Dale Johannesende064702009-02-06 21:50:26 +0000669 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +0000670 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000671 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
672 // available.
Bob Wilson2dc4f542009-03-20 22:42:55 +0000673 Op = DAG.getNode(ARMISD::FMRRD, dl,
Dale Johannesende064702009-02-06 21:50:26 +0000674 DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
Dan Gohman475871a2008-07-27 21:46:04 +0000675 SDValue Sign = DAG.getConstant(0, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000676 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain, Op, Sign,
Chris Lattner65a33232007-10-18 06:17:07 +0000677 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000678 }
Dale Johannesena05dca42009-02-04 23:02:30 +0000679 Copy = DAG.getCopyToReg(Chain, dl, ARM::R0, Op, SDValue());
Chris Lattner84bc5422007-12-31 04:13:23 +0000680 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
681 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
Evan Chenga8e29892007-01-19 07:51:42 +0000682 break;
683 case 5:
Dale Johannesena05dca42009-02-04 23:02:30 +0000684 Copy = DAG.getCopyToReg(Chain, dl, ARM::R1, Op.getOperand(3), SDValue());
Bob Wilson2dc4f542009-03-20 22:42:55 +0000685 Copy = DAG.getCopyToReg(Copy, dl, ARM::R0, Op.getOperand(1),
Dale Johannesena05dca42009-02-04 23:02:30 +0000686 Copy.getValue(1));
Evan Chenga8e29892007-01-19 07:51:42 +0000687 // If we haven't noted the R0+R1 are live out, do so now.
Chris Lattner84bc5422007-12-31 04:13:23 +0000688 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
689 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
690 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
Evan Chenga8e29892007-01-19 07:51:42 +0000691 }
692 break;
Chris Lattner78d60452008-07-11 20:53:00 +0000693 case 9: // i128 -> 4 regs
Dale Johannesena05dca42009-02-04 23:02:30 +0000694 Copy = DAG.getCopyToReg(Chain, dl, ARM::R3, Op.getOperand(7), SDValue());
Bob Wilson2dc4f542009-03-20 22:42:55 +0000695 Copy = DAG.getCopyToReg(Copy, dl, ARM::R2, Op.getOperand(5),
Dale Johannesena05dca42009-02-04 23:02:30 +0000696 Copy.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +0000697 Copy = DAG.getCopyToReg(Copy, dl, ARM::R1, Op.getOperand(3),
Dale Johannesena05dca42009-02-04 23:02:30 +0000698 Copy.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +0000699 Copy = DAG.getCopyToReg(Copy, dl, ARM::R0, Op.getOperand(1),
Dale Johannesena05dca42009-02-04 23:02:30 +0000700 Copy.getValue(1));
Chris Lattner78d60452008-07-11 20:53:00 +0000701 // If we haven't noted the R0+R1 are live out, do so now.
702 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
703 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
704 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
705 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R2);
706 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R3);
707 }
708 break;
Bob Wilson2dc4f542009-03-20 22:42:55 +0000709
Evan Chenga8e29892007-01-19 07:51:42 +0000710 }
711
712 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
Dale Johannesende064702009-02-06 21:50:26 +0000713 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Copy, Copy.getValue(1));
Evan Chenga8e29892007-01-19 07:51:42 +0000714}
715
Bob Wilson2dc4f542009-03-20 22:42:55 +0000716// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bill Wendling056292f2008-09-16 21:48:12 +0000717// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
718// one of the above mentioned nodes. It has to be wrapped because otherwise
719// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
720// be used to form addressing mode. These wrapped nodes will be selected
721// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000722static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 MVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000724 // FIXME there is no actual debug info here
725 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000726 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000728 if (CP->isMachineConstantPoolEntry())
729 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
730 CP->getAlignment());
731 else
732 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
733 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000734 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +0000735}
736
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000737// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000738SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000739ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
740 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000741 DebugLoc dl = GA->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000742 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000743 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
744 ARMConstantPoolValue *CPV =
745 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
746 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000747 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000748 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000749 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000751
Dan Gohman475871a2008-07-27 21:46:04 +0000752 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000753 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000754
755 // call __tls_get_addr.
756 ArgListTy Args;
757 ArgListEntry Entry;
758 Entry.Node = Argument;
759 Entry.Ty = (const Type *) Type::Int32Ty;
760 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000761 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +0000762 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000763 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000764 CallingConv::C, false,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000765 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000766 return CallResult.first;
767}
768
769// Lower ISD::GlobalTLSAddress using the "initial exec" or
770// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000771SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000772ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000773 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000774 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000775 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000776 SDValue Offset;
777 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000779 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000780 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000781
782 if (GV->isDeclaration()){
783 // initial exec model
784 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
785 ARMConstantPoolValue *CPV =
786 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
787 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000788 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000789 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000790 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000791 Chain = Offset.getValue(1);
792
Dan Gohman475871a2008-07-27 21:46:04 +0000793 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000794 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000795
Dale Johannesen33c960f2009-02-04 20:06:27 +0000796 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000797 } else {
798 // local exec model
799 ARMConstantPoolValue *CPV =
800 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000801 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000802 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000803 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000804 }
805
806 // The address of the thread local variable is the add of the thread
807 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000808 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000809}
810
Dan Gohman475871a2008-07-27 21:46:04 +0000811SDValue
812ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000813 // TODO: implement the "local dynamic" model
814 assert(Subtarget->isTargetELF() &&
815 "TLS not implemented for non-ELF targets");
816 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
817 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
818 // otherwise use the "Local Exec" TLS Model
819 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
820 return LowerToTLSGeneralDynamicModel(GA, DAG);
821 else
822 return LowerToTLSExecModels(GA, DAG);
823}
824
Dan Gohman475871a2008-07-27 21:46:04 +0000825SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000826 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000827 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000828 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000829 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
830 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
831 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000832 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000833 ARMConstantPoolValue *CPV =
834 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000835 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000836 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000837 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +0000838 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000839 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000840 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000841 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000842 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000843 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000844 return Result;
845 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000846 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000847 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000848 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000849 }
850}
851
Evan Chenga8e29892007-01-19 07:51:42 +0000852/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000853/// even in non-static mode.
854static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +0000855 // If symbol visibility is hidden, the extra load is not needed if
856 // the symbol is definitely defined in the current translation unit.
857 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
858 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
859 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +0000860 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +0000861}
862
Dan Gohman475871a2008-07-27 21:46:04 +0000863SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000864 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000865 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000866 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000867 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
868 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000869 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +0000870 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +0000871 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +0000872 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000873 else {
874 unsigned PCAdj = (RelocM != Reloc::PIC_)
875 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000876 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
877 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000878 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000879 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000880 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000881 }
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000882 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +0000883
Dale Johannesen33c960f2009-02-04 20:06:27 +0000884 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000885 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +0000886
887 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +0000888 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000889 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +0000890 }
891 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000892 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000893
894 return Result;
895}
896
Dan Gohman475871a2008-07-27 21:46:04 +0000897SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000898 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000899 assert(Subtarget->isTargetELF() &&
900 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000901 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000902 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000903 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
904 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
905 ARMPCLabelIndex,
906 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000907 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000908 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000909 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000910 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000911 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000912}
913
Dan Gohman475871a2008-07-27 21:46:04 +0000914static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000915 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000916 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000917 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000918 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000919 case Intrinsic::arm_thread_pointer:
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000920 return DAG.getNode(ARMISD::THREAD_POINTER, DebugLoc::getUnknownLoc(),
921 PtrVT);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000922 }
923}
924
Dan Gohman475871a2008-07-27 21:46:04 +0000925static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000926 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +0000927 // vastart just stores the address of the VarArgsFrameIndex slot into the
928 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000929 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000930 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000931 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +0000932 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000933 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000934}
935
Dan Gohman475871a2008-07-27 21:46:04 +0000936static SDValue LowerFORMAL_ARGUMENT(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000937 unsigned ArgNo, unsigned &NumGPRs,
938 unsigned &ArgOffset, DebugLoc dl) {
Evan Chenga8e29892007-01-19 07:51:42 +0000939 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000940 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000941 SDValue Root = Op.getOperand(0);
Chris Lattner84bc5422007-12-31 04:13:23 +0000942 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000943 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000944
945 static const unsigned GPRArgRegs[] = {
946 ARM::R0, ARM::R1, ARM::R2, ARM::R3
947 };
948
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000949 unsigned ObjSize;
950 unsigned ObjGPRs;
951 unsigned GPRPad;
952 unsigned StackPad;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000953 ISD::ArgFlagsTy Flags =
954 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo + 3))->getArgFlags();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000955 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
956 ObjSize, GPRPad, StackPad, Flags);
957 NumGPRs += GPRPad;
958 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000959
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SDValue ArgValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000961 if (ObjGPRs == 1) {
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000962 unsigned VReg;
963 if (AFI->isThumbFunction())
964 VReg = RegInfo.createVirtualRegister(ARM::tGPRRegisterClass);
965 else
966 VReg = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
Chris Lattner84bc5422007-12-31 04:13:23 +0000967 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000968 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000969 if (ObjectVT == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000970 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
Evan Chenga8e29892007-01-19 07:51:42 +0000971 } else if (ObjGPRs == 2) {
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000972 unsigned VReg;
973 if (AFI->isThumbFunction())
974 VReg = RegInfo.createVirtualRegister(ARM::tGPRRegisterClass);
975 else
976 VReg = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
Chris Lattner84bc5422007-12-31 04:13:23 +0000977 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000978 ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000979
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000980 if (AFI->isThumbFunction())
981 VReg = RegInfo.createVirtualRegister(ARM::tGPRRegisterClass);
982 else
983 VReg = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
Chris Lattner84bc5422007-12-31 04:13:23 +0000984 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000985 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000986
Chris Lattner27a6c732007-11-24 07:07:01 +0000987 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
Dale Johannesen33c960f2009-02-04 20:06:27 +0000988 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000989 }
990 NumGPRs += ObjGPRs;
991
992 if (ObjSize) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000993 MachineFrameInfo *MFI = MF.getFrameInfo();
994 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000995 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000996 if (ObjGPRs == 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000997 ArgValue = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000998 else {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000999 SDValue ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001000 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
Dale Johannesen33c960f2009-02-04 20:06:27 +00001001 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +00001002 }
1003
1004 ArgOffset += ObjSize; // Move on to the next argument.
1005 }
1006
1007 return ArgValue;
1008}
1009
Dan Gohman475871a2008-07-27 21:46:04 +00001010SDValue
1011ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
1012 std::vector<SDValue> ArgValues;
1013 SDValue Root = Op.getOperand(0);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001014 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001015 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1016 unsigned NumGPRs = 0; // GPRs used for parameter passing.
Evan Chenga8e29892007-01-19 07:51:42 +00001017
Gabor Greifba36cb52008-08-28 21:40:38 +00001018 unsigned NumArgs = Op.getNode()->getNumValues()-1;
Evan Chenga8e29892007-01-19 07:51:42 +00001019 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
Nate Begemanbf1caa92008-02-12 22:54:40 +00001020 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, ArgNo,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001021 NumGPRs, ArgOffset, dl));
Evan Chenga8e29892007-01-19 07:51:42 +00001022
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001023 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001024 if (isVarArg) {
1025 static const unsigned GPRArgRegs[] = {
1026 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1027 };
1028
1029 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001030 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +00001031 MachineFrameInfo *MFI = MF.getFrameInfo();
1032 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001033 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1034 unsigned VARegSize = (4 - NumGPRs) * 4;
1035 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001036 if (VARegSaveSize) {
1037 // If this function is vararg, store any remaining integer argument regs
1038 // to their spots on the stack so that they may be loaded by deferencing
1039 // the result of va_next.
1040 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001041 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1042 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001043 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001044
Dan Gohman475871a2008-07-27 21:46:04 +00001045 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001046 for (; NumGPRs < 4; ++NumGPRs) {
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001047 unsigned VReg;
1048 if (AFI->isThumbFunction())
1049 VReg = RegInfo.createVirtualRegister(ARM::tGPRRegisterClass);
1050 else
1051 VReg = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
Chris Lattner84bc5422007-12-31 04:13:23 +00001052 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001053 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1054 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001055 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001056 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001057 DAG.getConstant(4, getPointerTy()));
1058 }
1059 if (!MemOps.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +00001060 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001061 &MemOps[0], MemOps.size());
1062 } else
1063 // This will point to the next argument passed via stack.
1064 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1065 }
1066
1067 ArgValues.push_back(Root);
1068
1069 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001070 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001071 &ArgValues[0], ArgValues.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001072}
1073
1074/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001075static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001076 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001077 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001078 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001079 // Maybe this has already been legalized into the constant pool?
1080 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001082 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1083 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001084 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001085 }
1086 }
1087 return false;
1088}
1089
Evan Cheng9a2ef952007-02-02 01:53:26 +00001090static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001091 return ( isThumb && (C & ~255U) == 0) ||
1092 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1093}
1094
1095/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1096/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001097static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dale Johannesende064702009-02-06 21:50:26 +00001098 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb,
1099 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001100 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001101 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001102 if (!isLegalCmpImmediate(C, isThumb)) {
1103 // Constant does not fit, try adjusting it by one?
1104 switch (CC) {
1105 default: break;
1106 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001107 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001108 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001109 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1110 RHS = DAG.getConstant(C-1, MVT::i32);
1111 }
1112 break;
1113 case ISD::SETULT:
1114 case ISD::SETUGE:
1115 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1116 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001117 RHS = DAG.getConstant(C-1, MVT::i32);
1118 }
1119 break;
1120 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001121 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001122 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001123 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1124 RHS = DAG.getConstant(C+1, MVT::i32);
1125 }
1126 break;
1127 case ISD::SETULE:
1128 case ISD::SETUGT:
1129 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1130 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001131 RHS = DAG.getConstant(C+1, MVT::i32);
1132 }
1133 break;
1134 }
1135 }
1136 }
1137
1138 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001139 ARMISD::NodeType CompareType;
1140 switch (CondCode) {
1141 default:
1142 CompareType = ARMISD::CMP;
1143 break;
1144 case ARMCC::EQ:
1145 case ARMCC::NE:
1146 case ARMCC::MI:
1147 case ARMCC::PL:
1148 // Uses only N and Z Flags
1149 CompareType = ARMISD::CMPNZ;
1150 break;
1151 }
Evan Chenga8e29892007-01-19 07:51:42 +00001152 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001153 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001154}
1155
1156/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001157static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001158 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001160 if (!isFloatingPointZero(RHS))
Dale Johannesende064702009-02-06 21:50:26 +00001161 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001162 else
Dale Johannesende064702009-02-06 21:50:26 +00001163 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1164 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001165}
1166
Dan Gohman475871a2008-07-27 21:46:04 +00001167static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001168 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001169 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001170 SDValue LHS = Op.getOperand(0);
1171 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001172 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue TrueVal = Op.getOperand(2);
1174 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001175 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001176
1177 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue ARMCC;
1179 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001180 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1181 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001182 }
1183
1184 ARMCC::CondCodes CondCode, CondCode2;
1185 if (FPCCToARMCC(CC, CondCode, CondCode2))
1186 std::swap(TrueVal, FalseVal);
1187
Dan Gohman475871a2008-07-27 21:46:04 +00001188 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1189 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001190 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1191 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001192 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001193 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001194 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001195 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001196 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001197 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001198 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001199 }
1200 return Result;
1201}
1202
Dan Gohman475871a2008-07-27 21:46:04 +00001203static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001204 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001206 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001207 SDValue LHS = Op.getOperand(2);
1208 SDValue RHS = Op.getOperand(3);
1209 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001210 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001211
1212 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001213 SDValue ARMCC;
1214 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001215 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001216 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001217 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001218 }
1219
1220 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1221 ARMCC::CondCodes CondCode, CondCode2;
1222 if (FPCCToARMCC(CC, CondCode, CondCode2))
1223 // Swap the LHS/RHS of the comparison if needed.
1224 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001225
Dale Johannesende064702009-02-06 21:50:26 +00001226 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1228 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001229 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001230 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001231 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001232 if (CondCode2 != ARMCC::AL) {
1233 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001234 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001235 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001236 }
1237 return Res;
1238}
1239
Dan Gohman475871a2008-07-27 21:46:04 +00001240SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1241 SDValue Chain = Op.getOperand(0);
1242 SDValue Table = Op.getOperand(1);
1243 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001244 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001245
Duncan Sands83ec4b62008-06-06 12:08:01 +00001246 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001247 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1248 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001249 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1250 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Dale Johannesende064702009-02-06 21:50:26 +00001251 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001252 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1253 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001254 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001255 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
Evan Chenge2446c62007-06-26 18:31:22 +00001256 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001257 Chain = Addr.getValue(1);
1258 if (isPIC)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001259 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1260 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chenga8e29892007-01-19 07:51:42 +00001261}
1262
Dan Gohman475871a2008-07-27 21:46:04 +00001263static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001264 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001265 unsigned Opc =
1266 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Dale Johannesende064702009-02-06 21:50:26 +00001267 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1268 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001269}
1270
Dan Gohman475871a2008-07-27 21:46:04 +00001271static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001272 MVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001273 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001274 unsigned Opc =
1275 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1276
Dale Johannesende064702009-02-06 21:50:26 +00001277 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1278 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001279}
1280
Dan Gohman475871a2008-07-27 21:46:04 +00001281static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001282 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SDValue Tmp0 = Op.getOperand(0);
1284 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001285 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001286 MVT VT = Op.getValueType();
1287 MVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001288 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1289 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001290 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1291 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001292 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001293}
1294
Dan Gohman475871a2008-07-27 21:46:04 +00001295SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001296ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001297 SDValue Chain,
1298 SDValue Dst, SDValue Src,
1299 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001300 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001301 const Value *DstSV, uint64_t DstSVOff,
1302 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001303 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001304 // This requires 4-byte alignment.
1305 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001306 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001307 // This requires the copy size to be a constant, preferrably
1308 // within a subtarget-specific limit.
1309 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1310 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001311 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001312 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001313 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001314 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001315
1316 unsigned BytesLeft = SizeVal & 3;
1317 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001318 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001319 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001320 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001321 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001322 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue TFOps[MAX_LOADS_IN_LDM];
1324 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001325 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001326
Evan Cheng4102eb52007-10-22 22:11:27 +00001327 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1328 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001329 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001330 while (EmittedNumMemOps < NumMemOps) {
1331 for (i = 0;
1332 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001333 Loads[i] = DAG.getLoad(VT, dl, Chain,
1334 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001335 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001336 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001337 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001338 SrcOff += VTSize;
1339 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001340 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001341
Evan Cheng4102eb52007-10-22 22:11:27 +00001342 for (i = 0;
1343 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001344 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001345 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001346 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001347 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001348 DstOff += VTSize;
1349 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001350 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001351
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001352 EmittedNumMemOps += i;
1353 }
1354
Bob Wilson2dc4f542009-03-20 22:42:55 +00001355 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001356 return Chain;
1357
1358 // Issue loads / stores for the trailing (1 - 3) bytes.
1359 unsigned BytesLeftSave = BytesLeft;
1360 i = 0;
1361 while (BytesLeft) {
1362 if (BytesLeft >= 2) {
1363 VT = MVT::i16;
1364 VTSize = 2;
1365 } else {
1366 VT = MVT::i8;
1367 VTSize = 1;
1368 }
1369
Dale Johannesen0f502f62009-02-03 22:26:09 +00001370 Loads[i] = DAG.getLoad(VT, dl, Chain,
1371 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001372 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001373 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001374 TFOps[i] = Loads[i].getValue(1);
1375 ++i;
1376 SrcOff += VTSize;
1377 BytesLeft -= VTSize;
1378 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001379 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001380
1381 i = 0;
1382 BytesLeft = BytesLeftSave;
1383 while (BytesLeft) {
1384 if (BytesLeft >= 2) {
1385 VT = MVT::i16;
1386 VTSize = 2;
1387 } else {
1388 VT = MVT::i8;
1389 VTSize = 1;
1390 }
1391
Dale Johannesen0f502f62009-02-03 22:26:09 +00001392 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001393 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001394 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001395 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001396 ++i;
1397 DstOff += VTSize;
1398 BytesLeft -= VTSize;
1399 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001400 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001401}
1402
Duncan Sands1607f052008-12-01 11:39:25 +00001403static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001404 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00001405 DebugLoc dl = N->getDebugLoc();
Evan Chengc7c77292008-11-04 19:57:48 +00001406 if (N->getValueType(0) == MVT::f64) {
1407 // Turn i64->f64 into FMDRR.
Dale Johannesende064702009-02-06 21:50:26 +00001408 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001409 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001410 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001411 DAG.getConstant(1, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001412 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00001413 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001414
Evan Chengc7c77292008-11-04 19:57:48 +00001415 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001416 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Dale Johannesende064702009-02-06 21:50:26 +00001417 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001418
Chris Lattner27a6c732007-11-24 07:07:01 +00001419 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001420 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001421}
1422
Duncan Sands1607f052008-12-01 11:39:25 +00001423static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001424 assert(N->getValueType(0) == MVT::i64 &&
1425 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1426 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00001427
Chris Lattner27a6c732007-11-24 07:07:01 +00001428 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1429 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001430 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00001431 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001432
Chris Lattner27a6c732007-11-24 07:07:01 +00001433 // If we are in thumb mode, we don't have RRX.
Duncan Sands1607f052008-12-01 11:39:25 +00001434 if (ST->isThumb()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001435
Chris Lattner27a6c732007-11-24 07:07:01 +00001436 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dale Johannesende064702009-02-06 21:50:26 +00001437 DebugLoc dl = N->getDebugLoc();
1438 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001439 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001440 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001441 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001442
Chris Lattner27a6c732007-11-24 07:07:01 +00001443 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1444 // captures the result into a carry flag.
1445 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Dale Johannesende064702009-02-06 21:50:26 +00001446 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001447
Chris Lattner27a6c732007-11-24 07:07:01 +00001448 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Dale Johannesende064702009-02-06 21:50:26 +00001449 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001450
Chris Lattner27a6c732007-11-24 07:07:01 +00001451 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001452 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00001453}
1454
Dan Gohman475871a2008-07-27 21:46:04 +00001455SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001456 switch (Op.getOpcode()) {
1457 default: assert(0 && "Don't know how to custom lower this!"); abort();
1458 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001459 case ISD::GlobalAddress:
1460 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1461 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001462 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001463 case ISD::CALL: return LowerCALL(Op, DAG);
1464 case ISD::RET: return LowerRET(Op, DAG);
1465 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1466 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1467 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1468 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1469 case ISD::SINT_TO_FP:
1470 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1471 case ISD::FP_TO_SINT:
1472 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1473 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001474 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001475 case ISD::RETURNADDR: break;
1476 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001477 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001478 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00001479 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001480 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001481 case ISD::SRA: return ExpandSRx(Op.getNode(), DAG,Subtarget);
Evan Chenga8e29892007-01-19 07:51:42 +00001482 }
Dan Gohman475871a2008-07-27 21:46:04 +00001483 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001484}
1485
Duncan Sands1607f052008-12-01 11:39:25 +00001486/// ReplaceNodeResults - Replace the results of node with an illegal result
1487/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00001488void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1489 SmallVectorImpl<SDValue>&Results,
1490 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001491 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00001492 default:
1493 assert(0 && "Don't know how to custom expand this!");
1494 return;
1495 case ISD::BIT_CONVERT:
1496 Results.push_back(ExpandBIT_CONVERT(N, DAG));
1497 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00001498 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001499 case ISD::SRA: {
1500 SDValue Res = ExpandSRx(N, DAG, Subtarget);
1501 if (Res.getNode())
1502 Results.push_back(Res);
1503 return;
1504 }
Chris Lattner27a6c732007-11-24 07:07:01 +00001505 }
1506}
Chris Lattner27a6c732007-11-24 07:07:01 +00001507
Evan Chenga8e29892007-01-19 07:51:42 +00001508//===----------------------------------------------------------------------===//
1509// ARM Scheduler Hooks
1510//===----------------------------------------------------------------------===//
1511
1512MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001513ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001514 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001515 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00001516 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001517 switch (MI->getOpcode()) {
1518 default: assert(false && "Unexpected instr type to insert");
1519 case ARM::tMOVCCr: {
1520 // To "insert" a SELECT_CC instruction, we actually have to insert the
1521 // diamond control-flow pattern. The incoming instruction knows the
1522 // destination vreg to set, the condition code register to branch on, the
1523 // true/false values to select between, and a branch opcode to use.
1524 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001525 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001526 ++It;
1527
1528 // thisMBB:
1529 // ...
1530 // TrueVal = ...
1531 // cmpTY ccX, r1, r2
1532 // bCC copy1MBB
1533 // fallthrough --> copy0MBB
1534 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001535 MachineFunction *F = BB->getParent();
1536 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1537 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00001538 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001539 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001540 F->insert(It, copy0MBB);
1541 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001542 // Update machine-CFG edges by first adding all successors of the current
1543 // block to the new block which will contain the Phi node for the select.
1544 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1545 e = BB->succ_end(); i != e; ++i)
1546 sinkMBB->addSuccessor(*i);
1547 // Next, remove all successors of the current block, and add the true
1548 // and fallthrough blocks as its successors.
1549 while(!BB->succ_empty())
1550 BB->removeSuccessor(BB->succ_begin());
1551 BB->addSuccessor(copy0MBB);
1552 BB->addSuccessor(sinkMBB);
1553
1554 // copy0MBB:
1555 // %FalseValue = ...
1556 // # fallthrough to sinkMBB
1557 BB = copy0MBB;
1558
1559 // Update machine-CFG edges
1560 BB->addSuccessor(sinkMBB);
1561
1562 // sinkMBB:
1563 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1564 // ...
1565 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00001566 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00001567 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1568 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1569
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001570 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001571 return BB;
1572 }
1573 }
1574}
1575
1576//===----------------------------------------------------------------------===//
1577// ARM Optimization Hooks
1578//===----------------------------------------------------------------------===//
1579
Chris Lattnerd1980a52009-03-12 06:52:53 +00001580static
1581SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1582 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00001583 SelectionDAG &DAG = DCI.DAG;
1584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1585 MVT VT = N->getValueType(0);
1586 unsigned Opc = N->getOpcode();
1587 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
1588 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
1589 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
1590 ISD::CondCode CC = ISD::SETCC_INVALID;
1591
1592 if (isSlctCC) {
1593 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
1594 } else {
1595 SDValue CCOp = Slct.getOperand(0);
1596 if (CCOp.getOpcode() == ISD::SETCC)
1597 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
1598 }
1599
1600 bool DoXform = false;
1601 bool InvCC = false;
1602 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
1603 "Bad input!");
1604
1605 if (LHS.getOpcode() == ISD::Constant &&
1606 cast<ConstantSDNode>(LHS)->isNullValue()) {
1607 DoXform = true;
1608 } else if (CC != ISD::SETCC_INVALID &&
1609 RHS.getOpcode() == ISD::Constant &&
1610 cast<ConstantSDNode>(RHS)->isNullValue()) {
1611 std::swap(LHS, RHS);
1612 SDValue Op0 = Slct.getOperand(0);
1613 MVT OpVT = isSlctCC ? Op0.getValueType() :
1614 Op0.getOperand(0).getValueType();
1615 bool isInt = OpVT.isInteger();
1616 CC = ISD::getSetCCInverse(CC, isInt);
1617
1618 if (!TLI.isCondCodeLegal(CC, OpVT))
1619 return SDValue(); // Inverse operator isn't legal.
1620
1621 DoXform = true;
1622 InvCC = true;
1623 }
1624
1625 if (DoXform) {
1626 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
1627 if (isSlctCC)
1628 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
1629 Slct.getOperand(0), Slct.getOperand(1), CC);
1630 SDValue CCOp = Slct.getOperand(0);
1631 if (InvCC)
1632 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
1633 CCOp.getOperand(0), CCOp.getOperand(1), CC);
1634 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
1635 CCOp, OtherOp, Result);
1636 }
1637 return SDValue();
1638}
1639
1640/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
1641static SDValue PerformADDCombine(SDNode *N,
1642 TargetLowering::DAGCombinerInfo &DCI) {
1643 // added by evan in r37685 with no testcase.
1644 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001645
Chris Lattnerd1980a52009-03-12 06:52:53 +00001646 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1647 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
1648 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
1649 if (Result.getNode()) return Result;
1650 }
1651 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1652 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1653 if (Result.getNode()) return Result;
1654 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001655
Chris Lattnerd1980a52009-03-12 06:52:53 +00001656 return SDValue();
1657}
1658
1659/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1660static SDValue PerformSUBCombine(SDNode *N,
1661 TargetLowering::DAGCombinerInfo &DCI) {
1662 // added by evan in r37685 with no testcase.
1663 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001664
Chris Lattnerd1980a52009-03-12 06:52:53 +00001665 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1666 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1667 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1668 if (Result.getNode()) return Result;
1669 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001670
Chris Lattnerd1980a52009-03-12 06:52:53 +00001671 return SDValue();
1672}
1673
1674
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001675/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001676static SDValue PerformFMRRDCombine(SDNode *N,
1677 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001678 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001679 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001680 if (InDouble.getOpcode() == ARMISD::FMDRR)
1681 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001682 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001683}
1684
Dan Gohman475871a2008-07-27 21:46:04 +00001685SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001686 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001687 switch (N->getOpcode()) {
1688 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00001689 case ISD::ADD: return PerformADDCombine(N, DCI);
1690 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001691 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1692 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001693
Dan Gohman475871a2008-07-27 21:46:04 +00001694 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001695}
1696
Evan Chengb01fad62007-03-12 23:30:29 +00001697/// isLegalAddressImmediate - Return true if the integer value can be used
1698/// as the offset of the target addressing mode for load / store of the
1699/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001700static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001701 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001702 if (V == 0)
1703 return true;
1704
Evan Cheng65011532009-03-09 19:15:00 +00001705 if (!VT.isSimple())
1706 return false;
1707
Evan Chengb01fad62007-03-12 23:30:29 +00001708 if (Subtarget->isThumb()) {
1709 if (V < 0)
1710 return false;
1711
1712 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001714 default: return false;
1715 case MVT::i1:
1716 case MVT::i8:
1717 // Scale == 1;
1718 break;
1719 case MVT::i16:
1720 // Scale == 2;
1721 Scale = 2;
1722 break;
1723 case MVT::i32:
1724 // Scale == 4;
1725 Scale = 4;
1726 break;
1727 }
1728
1729 if ((V & (Scale - 1)) != 0)
1730 return false;
1731 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001732 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001733 }
1734
1735 if (V < 0)
1736 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001737 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001738 default: return false;
1739 case MVT::i1:
1740 case MVT::i8:
1741 case MVT::i32:
1742 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001743 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001744 case MVT::i16:
1745 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001746 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001747 case MVT::f32:
1748 case MVT::f64:
1749 if (!Subtarget->hasVFP2())
1750 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001751 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001752 return false;
1753 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001754 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001755 }
Evan Chenga8e29892007-01-19 07:51:42 +00001756}
1757
Chris Lattner37caf8c2007-04-09 23:33:39 +00001758/// isLegalAddressingMode - Return true if the addressing mode represented
1759/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001760bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001761 const Type *Ty) const {
Evan Chengd1b3da62008-07-25 00:55:17 +00001762 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty, true), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001763 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001764
Chris Lattner37caf8c2007-04-09 23:33:39 +00001765 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001766 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001767 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001768
Chris Lattner37caf8c2007-04-09 23:33:39 +00001769 switch (AM.Scale) {
1770 case 0: // no scale reg, must be "r+i" or "r", or "i".
1771 break;
1772 case 1:
1773 if (Subtarget->isThumb())
1774 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001775 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001776 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001777 // ARM doesn't support any R+R*scale+imm addr modes.
1778 if (AM.BaseOffs)
1779 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001780
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001781 int Scale = AM.Scale;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001782 switch (getValueType(Ty).getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001783 default: return false;
1784 case MVT::i1:
1785 case MVT::i8:
1786 case MVT::i32:
1787 case MVT::i64:
1788 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1789 // ldrd / strd are used, then its address mode is same as i16.
1790 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001791 if (Scale < 0) Scale = -Scale;
1792 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001793 return true;
1794 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001795 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001796 case MVT::i16:
1797 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001798 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001799 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001800 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001801
Chris Lattner37caf8c2007-04-09 23:33:39 +00001802 case MVT::isVoid:
1803 // Note, we allow "void" uses (basically, uses that aren't loads or
1804 // stores), because arm allows folding a scale into many arithmetic
1805 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001806
Chris Lattner37caf8c2007-04-09 23:33:39 +00001807 // Allow r << imm, but the imm has to be a multiple of two.
1808 if (AM.Scale & 1) return false;
1809 return isPowerOf2_32(AM.Scale);
1810 }
1811 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001812 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001813 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001814}
1815
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001817 bool isSEXTLoad, SDValue &Base,
1818 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001819 SelectionDAG &DAG) {
1820 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1821 return false;
1822
1823 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1824 // AddressingMode 3
1825 Base = Ptr->getOperand(0);
1826 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001827 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001828 if (RHSC < 0 && RHSC > -256) {
1829 isInc = false;
1830 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1831 return true;
1832 }
1833 }
1834 isInc = (Ptr->getOpcode() == ISD::ADD);
1835 Offset = Ptr->getOperand(1);
1836 return true;
1837 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1838 // AddressingMode 2
1839 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001840 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001841 if (RHSC < 0 && RHSC > -0x1000) {
1842 isInc = false;
1843 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1844 Base = Ptr->getOperand(0);
1845 return true;
1846 }
1847 }
1848
1849 if (Ptr->getOpcode() == ISD::ADD) {
1850 isInc = true;
1851 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1852 if (ShOpcVal != ARM_AM::no_shift) {
1853 Base = Ptr->getOperand(1);
1854 Offset = Ptr->getOperand(0);
1855 } else {
1856 Base = Ptr->getOperand(0);
1857 Offset = Ptr->getOperand(1);
1858 }
1859 return true;
1860 }
1861
1862 isInc = (Ptr->getOpcode() == ISD::ADD);
1863 Base = Ptr->getOperand(0);
1864 Offset = Ptr->getOperand(1);
1865 return true;
1866 }
1867
1868 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1869 return false;
1870}
1871
1872/// getPreIndexedAddressParts - returns true by value, base pointer and
1873/// offset pointer and addressing mode by reference if the node's address
1874/// can be legally represented as pre-indexed load / store address.
1875bool
Dan Gohman475871a2008-07-27 21:46:04 +00001876ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1877 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001878 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001879 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001880 if (Subtarget->isThumb())
1881 return false;
1882
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001884 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001885 bool isSEXTLoad = false;
1886 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1887 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001888 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001889 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1890 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1891 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001892 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001893 } else
1894 return false;
1895
1896 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00001897 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001898 isInc, DAG);
1899 if (isLegal) {
1900 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1901 return true;
1902 }
1903 return false;
1904}
1905
1906/// getPostIndexedAddressParts - returns true by value, base pointer and
1907/// offset pointer and addressing mode by reference if this node can be
1908/// combined with a load / store to form a post-indexed load / store.
1909bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue &Base,
1911 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00001912 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001913 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001914 if (Subtarget->isThumb())
1915 return false;
1916
Duncan Sands83ec4b62008-06-06 12:08:01 +00001917 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00001919 bool isSEXTLoad = false;
1920 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001921 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001922 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1923 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001924 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001925 } else
1926 return false;
1927
1928 bool isInc;
1929 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1930 isInc, DAG);
1931 if (isLegal) {
1932 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1933 return true;
1934 }
1935 return false;
1936}
1937
Dan Gohman475871a2008-07-27 21:46:04 +00001938void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001939 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001940 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001941 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001942 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001943 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001944 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001945 switch (Op.getOpcode()) {
1946 default: break;
1947 case ARMISD::CMOV: {
1948 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001950 if (KnownZero == 0 && KnownOne == 0) return;
1951
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001952 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1954 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001955 KnownZero &= KnownZeroRHS;
1956 KnownOne &= KnownOneRHS;
1957 return;
1958 }
1959 }
1960}
1961
1962//===----------------------------------------------------------------------===//
1963// ARM Inline Assembly Support
1964//===----------------------------------------------------------------------===//
1965
1966/// getConstraintType - Given a constraint letter, return the type of
1967/// constraint it is for this target.
1968ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001969ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1970 if (Constraint.size() == 1) {
1971 switch (Constraint[0]) {
1972 default: break;
1973 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001974 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001975 }
Evan Chenga8e29892007-01-19 07:51:42 +00001976 }
Chris Lattner4234f572007-03-25 02:14:49 +00001977 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001978}
1979
Bob Wilson2dc4f542009-03-20 22:42:55 +00001980std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00001981ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001983 if (Constraint.size() == 1) {
1984 // GCC RS6000 Constraint Letters
1985 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001986 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001987 if (Subtarget->isThumb())
1988 return std::make_pair(0U, ARM::tGPRRegisterClass);
1989 else
1990 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001991 case 'r':
1992 return std::make_pair(0U, ARM::GPRRegisterClass);
1993 case 'w':
1994 if (VT == MVT::f32)
1995 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001996 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001997 return std::make_pair(0U, ARM::DPRRegisterClass);
1998 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001999 }
2000 }
2001 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2002}
2003
2004std::vector<unsigned> ARMTargetLowering::
2005getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002006 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002007 if (Constraint.size() != 1)
2008 return std::vector<unsigned>();
2009
2010 switch (Constraint[0]) { // GCC ARM Constraint Letters
2011 default: break;
2012 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002013 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2014 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2015 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002016 case 'r':
2017 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2018 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2019 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
2020 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002021 case 'w':
2022 if (VT == MVT::f32)
2023 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
2024 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
2025 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
2026 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
2027 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
2028 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
2029 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
2030 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
2031 if (VT == MVT::f64)
2032 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
2033 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
2034 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
2035 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
2036 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002037 }
2038
2039 return std::vector<unsigned>();
2040}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00002041
2042/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2043/// vector. If it is invalid, don't add anything to Ops.
2044void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2045 char Constraint,
2046 bool hasMemory,
2047 std::vector<SDValue>&Ops,
2048 SelectionDAG &DAG) const {
2049 SDValue Result(0, 0);
2050
2051 switch (Constraint) {
2052 default: break;
2053 case 'I': case 'J': case 'K': case 'L':
2054 case 'M': case 'N': case 'O':
2055 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2056 if (!C)
2057 return;
2058
2059 int64_t CVal64 = C->getSExtValue();
2060 int CVal = (int) CVal64;
2061 // None of these constraints allow values larger than 32 bits. Check
2062 // that the value fits in an int.
2063 if (CVal != CVal64)
2064 return;
2065
2066 switch (Constraint) {
2067 case 'I':
2068 if (Subtarget->isThumb()) {
2069 // This must be a constant between 0 and 255, for ADD immediates.
2070 if (CVal >= 0 && CVal <= 255)
2071 break;
2072 } else {
2073 // A constant that can be used as an immediate value in a
2074 // data-processing instruction.
2075 if (ARM_AM::getSOImmVal(CVal) != -1)
2076 break;
2077 }
2078 return;
2079
2080 case 'J':
2081 if (Subtarget->isThumb()) {
2082 // This must be a constant between -255 and -1, for negated ADD
2083 // immediates. This can be used in GCC with an "n" modifier that
2084 // prints the negated value, for use with SUB instructions. It is
2085 // not useful otherwise but is implemented for compatibility.
2086 if (CVal >= -255 && CVal <= -1)
2087 break;
2088 } else {
2089 // This must be a constant between -4095 and 4095. It is not clear
2090 // what this constraint is intended for. Implemented for
2091 // compatibility with GCC.
2092 if (CVal >= -4095 && CVal <= 4095)
2093 break;
2094 }
2095 return;
2096
2097 case 'K':
2098 if (Subtarget->isThumb()) {
2099 // A 32-bit value where only one byte has a nonzero value. Exclude
2100 // zero to match GCC. This constraint is used by GCC internally for
2101 // constants that can be loaded with a move/shift combination.
2102 // It is not useful otherwise but is implemented for compatibility.
2103 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
2104 break;
2105 } else {
2106 // A constant whose bitwise inverse can be used as an immediate
2107 // value in a data-processing instruction. This can be used in GCC
2108 // with a "B" modifier that prints the inverted value, for use with
2109 // BIC and MVN instructions. It is not useful otherwise but is
2110 // implemented for compatibility.
2111 if (ARM_AM::getSOImmVal(~CVal) != -1)
2112 break;
2113 }
2114 return;
2115
2116 case 'L':
2117 if (Subtarget->isThumb()) {
2118 // This must be a constant between -7 and 7,
2119 // for 3-operand ADD/SUB immediate instructions.
2120 if (CVal >= -7 && CVal < 7)
2121 break;
2122 } else {
2123 // A constant whose negation can be used as an immediate value in a
2124 // data-processing instruction. This can be used in GCC with an "n"
2125 // modifier that prints the negated value, for use with SUB
2126 // instructions. It is not useful otherwise but is implemented for
2127 // compatibility.
2128 if (ARM_AM::getSOImmVal(-CVal) != -1)
2129 break;
2130 }
2131 return;
2132
2133 case 'M':
2134 if (Subtarget->isThumb()) {
2135 // This must be a multiple of 4 between 0 and 1020, for
2136 // ADD sp + immediate.
2137 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
2138 break;
2139 } else {
2140 // A power of two or a constant between 0 and 32. This is used in
2141 // GCC for the shift amount on shifted register operands, but it is
2142 // useful in general for any shift amounts.
2143 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
2144 break;
2145 }
2146 return;
2147
2148 case 'N':
2149 if (Subtarget->isThumb()) {
2150 // This must be a constant between 0 and 31, for shift amounts.
2151 if (CVal >= 0 && CVal <= 31)
2152 break;
2153 }
2154 return;
2155
2156 case 'O':
2157 if (Subtarget->isThumb()) {
2158 // This must be a multiple of 4 between -508 and 508, for
2159 // ADD/SUB sp = sp + immediate.
2160 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
2161 break;
2162 }
2163 return;
2164 }
2165 Result = DAG.getTargetConstant(CVal, Op.getValueType());
2166 break;
2167 }
2168
2169 if (Result.getNode()) {
2170 Ops.push_back(Result);
2171 return;
2172 }
2173 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
2174 Ops, DAG);
2175}