blob: bdaf6b2fc506697d75006853e313a00f9e048ca3 [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()) {
44 // Don't have these.
45 setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL);
46 setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL);
Evan Chenga8e29892007-01-19 07:51:42 +000047
Evan Chengb1df8f22007-04-27 08:15:43 +000048 // Uses VFP for Thumb libfuncs if available.
49 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
50 // Single-precision floating-point arithmetic.
51 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
52 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
53 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
54 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000055
Evan Chengb1df8f22007-04-27 08:15:43 +000056 // Double-precision floating-point arithmetic.
57 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
58 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
59 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
60 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000061
Evan Chengb1df8f22007-04-27 08:15:43 +000062 // Single-precision comparisons.
63 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
64 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
65 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
66 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
67 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
68 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
69 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
70 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000071
Evan Chengb1df8f22007-04-27 08:15:43 +000072 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
73 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
74 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
75 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
76 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
77 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
78 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
79 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +000080
Evan Chengb1df8f22007-04-27 08:15:43 +000081 // Double-precision comparisons.
82 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
83 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
84 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
85 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
86 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
87 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
88 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
89 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000090
Evan Chengb1df8f22007-04-27 08:15:43 +000091 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
92 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
97 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
98 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +000099
Evan Chengb1df8f22007-04-27 08:15:43 +0000100 // Floating-point to integer conversions.
101 // i64 conversions are done via library routines even when generating VFP
102 // instructions, so use the same ones.
103 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
104 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
105 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
106 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000107
Evan Chengb1df8f22007-04-27 08:15:43 +0000108 // Conversions between floating types.
109 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
110 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
111
112 // Integer to floating-point conversions.
113 // i64 conversions are done via library routines even when generating VFP
114 // instructions, so use the same ones.
115 // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
116 // __floatunsidf vs. __floatunssidfvfp.
117 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
118 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
119 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
120 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
121 }
Evan Chenga8e29892007-01-19 07:51:42 +0000122 }
123
124 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000125 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000126 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
127 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Chris Lattnerddf89562008-01-17 19:59:44 +0000128
129 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000130 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000131 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000132
133 // ARM does not have f32 extending load.
134 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
135
136 // ARM supports all 4 flavors of integer indexed load / store.
137 for (unsigned im = (unsigned)ISD::PRE_INC;
138 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
139 setIndexedLoadAction(im, MVT::i1, Legal);
140 setIndexedLoadAction(im, MVT::i8, Legal);
141 setIndexedLoadAction(im, MVT::i16, Legal);
142 setIndexedLoadAction(im, MVT::i32, Legal);
143 setIndexedStoreAction(im, MVT::i1, Legal);
144 setIndexedStoreAction(im, MVT::i8, Legal);
145 setIndexedStoreAction(im, MVT::i16, Legal);
146 setIndexedStoreAction(im, MVT::i32, Legal);
147 }
148
149 // i64 operation support.
150 if (Subtarget->isThumb()) {
151 setOperationAction(ISD::MUL, MVT::i64, Expand);
152 setOperationAction(ISD::MULHU, MVT::i32, Expand);
153 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000154 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
155 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000156 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000157 setOperationAction(ISD::MUL, MVT::i64, Expand);
158 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000159 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000160 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000161 }
162 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
163 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
164 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
165 setOperationAction(ISD::SRL, MVT::i64, Custom);
166 setOperationAction(ISD::SRA, MVT::i64, Custom);
167
168 // ARM does not have ROTL.
169 setOperationAction(ISD::ROTL, MVT::i32, Expand);
170 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
171 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000172 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000173 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
174
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000175 // Only ARMv6 has BSWAP.
176 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000177 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000178
Evan Chenga8e29892007-01-19 07:51:42 +0000179 // These are expanded into libcalls.
180 setOperationAction(ISD::SDIV, MVT::i32, Expand);
181 setOperationAction(ISD::UDIV, MVT::i32, Expand);
182 setOperationAction(ISD::SREM, MVT::i32, Expand);
183 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000184 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
185 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000186
187 // Support label based line numbers.
188 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
189 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000190
191 setOperationAction(ISD::RET, MVT::Other, Custom);
192 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
193 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000194 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000195 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000196
197 // Expand mem operations genericly.
198 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000199 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000200 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Duncan Sands36397f52007-07-27 12:58:54 +0000201
Evan Chenga8e29892007-01-19 07:51:42 +0000202 // Use the default implementation.
203 setOperationAction(ISD::VASTART , MVT::Other, Expand);
204 setOperationAction(ISD::VAARG , MVT::Other, Expand);
205 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
206 setOperationAction(ISD::VAEND , MVT::Other, Expand);
207 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
208 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
209 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
210
211 if (!Subtarget->hasV6Ops()) {
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
214 }
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
216
Evan Chengb6ab2542007-01-31 08:40:13 +0000217 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000218 // Turn f64->i64 into FMRRD iff target supports vfp2.
219 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000220
221 // We want to custom lower some of our intrinsics.
222 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
223
Evan Chenga8e29892007-01-19 07:51:42 +0000224 setOperationAction(ISD::SETCC , MVT::i32, Expand);
225 setOperationAction(ISD::SETCC , MVT::f32, Expand);
226 setOperationAction(ISD::SETCC , MVT::f64, Expand);
227 setOperationAction(ISD::SELECT , MVT::i32, Expand);
228 setOperationAction(ISD::SELECT , MVT::f32, Expand);
229 setOperationAction(ISD::SELECT , MVT::f64, Expand);
230 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
231 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
232 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
233
234 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
235 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
236 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
237 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
238 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
239
240 setOperationAction(ISD::VASTART, MVT::Other, Custom);
241 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
242 setOperationAction(ISD::VAEND, MVT::Other, Expand);
243 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
244 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
245
246 // FP Constants can't be immediates.
247 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
248 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
249
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000250 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000251 setOperationAction(ISD::FSIN , MVT::f64, Expand);
252 setOperationAction(ISD::FSIN , MVT::f32, Expand);
253 setOperationAction(ISD::FCOS , MVT::f32, Expand);
254 setOperationAction(ISD::FCOS , MVT::f64, Expand);
255 setOperationAction(ISD::FREM , MVT::f64, Expand);
256 setOperationAction(ISD::FREM , MVT::f32, Expand);
257 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
258 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000259 setOperationAction(ISD::FPOW , MVT::f64, Expand);
260 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000261
262 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
263 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
264 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
265 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
266 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
267
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000268 // We have target-specific dag combine patterns for the following nodes:
269 // ARMISD::FMRRD - No need to call setTargetDAGCombine
270
Evan Chenga8e29892007-01-19 07:51:42 +0000271 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000272 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000273 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000274 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000275
276 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000277}
278
279
280const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
281 switch (Opcode) {
282 default: return 0;
283 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000284 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
285 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000286 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000287 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
288 case ARMISD::tCALL: return "ARMISD::tCALL";
289 case ARMISD::BRCOND: return "ARMISD::BRCOND";
290 case ARMISD::BR_JT: return "ARMISD::BR_JT";
291 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
292 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
293 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000294 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000295 case ARMISD::CMPFP: return "ARMISD::CMPFP";
296 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
297 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
298 case ARMISD::CMOV: return "ARMISD::CMOV";
299 case ARMISD::CNEG: return "ARMISD::CNEG";
300
301 case ARMISD::FTOSI: return "ARMISD::FTOSI";
302 case ARMISD::FTOUI: return "ARMISD::FTOUI";
303 case ARMISD::SITOF: return "ARMISD::SITOF";
304 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000305
306 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
307 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
308 case ARMISD::RRX: return "ARMISD::RRX";
309
310 case ARMISD::FMRRD: return "ARMISD::FMRRD";
311 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000312
313 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000314 }
315}
316
317//===----------------------------------------------------------------------===//
318// Lowering Code
319//===----------------------------------------------------------------------===//
320
321
322/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
323static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
324 switch (CC) {
325 default: assert(0 && "Unknown condition code!");
326 case ISD::SETNE: return ARMCC::NE;
327 case ISD::SETEQ: return ARMCC::EQ;
328 case ISD::SETGT: return ARMCC::GT;
329 case ISD::SETGE: return ARMCC::GE;
330 case ISD::SETLT: return ARMCC::LT;
331 case ISD::SETLE: return ARMCC::LE;
332 case ISD::SETUGT: return ARMCC::HI;
333 case ISD::SETUGE: return ARMCC::HS;
334 case ISD::SETULT: return ARMCC::LO;
335 case ISD::SETULE: return ARMCC::LS;
336 }
337}
338
339/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
340/// returns true if the operands should be inverted to form the proper
341/// comparison.
342static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
343 ARMCC::CondCodes &CondCode2) {
344 bool Invert = false;
345 CondCode2 = ARMCC::AL;
346 switch (CC) {
347 default: assert(0 && "Unknown FP condition!");
348 case ISD::SETEQ:
349 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
350 case ISD::SETGT:
351 case ISD::SETOGT: CondCode = ARMCC::GT; break;
352 case ISD::SETGE:
353 case ISD::SETOGE: CondCode = ARMCC::GE; break;
354 case ISD::SETOLT: CondCode = ARMCC::MI; break;
355 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
356 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
357 case ISD::SETO: CondCode = ARMCC::VC; break;
358 case ISD::SETUO: CondCode = ARMCC::VS; break;
359 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
360 case ISD::SETUGT: CondCode = ARMCC::HI; break;
361 case ISD::SETUGE: CondCode = ARMCC::PL; break;
362 case ISD::SETLT:
363 case ISD::SETULT: CondCode = ARMCC::LT; break;
364 case ISD::SETLE:
365 case ISD::SETULE: CondCode = ARMCC::LE; break;
366 case ISD::SETNE:
367 case ISD::SETUNE: CondCode = ARMCC::NE; break;
368 }
369 return Invert;
370}
371
372static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000373HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
374 unsigned StackOffset, unsigned &NeededGPRs,
375 unsigned &NeededStackSize, unsigned &GPRPad,
376 unsigned &StackPad, unsigned Flags) {
377 NeededStackSize = 0;
378 NeededGPRs = 0;
379 StackPad = 0;
380 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000381 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000382 GPRPad = NumGPRs % ((align + 3)/4);
383 StackPad = StackOffset % align;
384 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000385 switch (ObjectVT) {
386 default: assert(0 && "Unhandled argument type!");
387 case MVT::i32:
388 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000389 if (firstGPR < 4)
390 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000391 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000392 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000393 break;
394 case MVT::i64:
395 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000396 if (firstGPR < 3)
397 NeededGPRs = 2;
398 else if (firstGPR == 3) {
399 NeededGPRs = 1;
400 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000401 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000402 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000403 }
404}
405
Evan Chengfc403422007-02-03 08:53:01 +0000406/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
407/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
408/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000409SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
410 MVT::ValueType RetVT= Op.Val->getValueType(0);
411 SDOperand Chain = Op.getOperand(0);
412 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
413 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000414 CallConv == CallingConv::Fast) && "unknown calling convention");
415 SDOperand Callee = Op.getOperand(4);
416 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
417 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
418 unsigned NumGPRs = 0; // GPRs used for parameter passing.
419
420 // Count how many bytes are to be pushed on the stack.
421 unsigned NumBytes = 0;
422
423 // Add up all the space actually used.
424 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000425 unsigned ObjSize;
426 unsigned ObjGPRs;
427 unsigned StackPad;
428 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000429 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000430 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
431 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
432 GPRPad, StackPad, Flags);
433 NumBytes += ObjSize + StackPad;
434 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000435 }
436
437 // Adjust the stack pointer for the new arguments...
438 // These operations are automatically eliminated by the prolog/epilog pass
439 Chain = DAG.getCALLSEQ_START(Chain,
440 DAG.getConstant(NumBytes, MVT::i32));
441
442 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
443
444 static const unsigned GPRArgRegs[] = {
445 ARM::R0, ARM::R1, ARM::R2, ARM::R3
446 };
447
448 NumGPRs = 0;
449 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
450 std::vector<SDOperand> MemOpChains;
451 for (unsigned i = 0; i != NumOps; ++i) {
452 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000453 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000454 MVT::ValueType ArgVT = Arg.getValueType();
455
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000456 unsigned ObjSize;
457 unsigned ObjGPRs;
458 unsigned GPRPad;
459 unsigned StackPad;
460 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
461 ObjSize, GPRPad, StackPad, Flags);
462 NumGPRs += GPRPad;
463 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000464 if (ObjGPRs > 0) {
465 switch (ArgVT) {
466 default: assert(0 && "Unexpected ValueType for argument!");
467 case MVT::i32:
468 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
469 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000470 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000471 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
472 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
473 break;
474 case MVT::i64: {
475 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
476 DAG.getConstant(0, getPointerTy()));
477 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
478 DAG.getConstant(1, getPointerTy()));
479 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
480 if (ObjGPRs == 2)
481 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
482 else {
483 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
484 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
485 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
486 }
487 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000488 }
Evan Chenga8e29892007-01-19 07:51:42 +0000489 case MVT::f64: {
490 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
491 DAG.getVTList(MVT::i32, MVT::i32),
492 &Arg, 1);
493 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
494 if (ObjGPRs == 2)
495 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
496 Cvt.getValue(1)));
497 else {
498 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
499 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
500 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
501 NULL, 0));
502 }
503 break;
504 }
505 }
506 } else {
507 assert(ObjSize != 0);
508 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
509 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
510 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
511 }
512
513 NumGPRs += ObjGPRs;
514 ArgOffset += ObjSize;
515 }
516
517 if (!MemOpChains.empty())
518 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
519 &MemOpChains[0], MemOpChains.size());
520
521 // Build a sequence of copy-to-reg nodes chained together with token chain
522 // and flag operands which copy the outgoing args into the appropriate regs.
523 SDOperand InFlag;
524 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
525 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
526 InFlag);
527 InFlag = Chain.getValue(1);
528 }
529
530 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
531 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
532 // node so that legalize doesn't hack it.
533 bool isDirect = false;
534 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000535 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000536 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
537 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000538 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000539 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000540 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000541 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000542 getTargetMachine().getRelocationModel() != Reloc::Static;
543 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000544 // ARM call to a local ARM function is predicable.
545 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000546 // tBX takes a register source operand.
547 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
548 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
549 ARMCP::CPStub, 4);
550 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
551 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
552 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
553 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
554 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
555 } else
556 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000557 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000558 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000559 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000560 getTargetMachine().getRelocationModel() != Reloc::Static;
561 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000562 // tBX takes a register source operand.
563 const char *Sym = S->getSymbol();
564 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
565 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
566 ARMCP::CPStub, 4);
567 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
568 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
569 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
570 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
571 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
572 } else
573 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000574 }
575
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000576 // FIXME: handle tail calls differently.
577 unsigned CallOpc;
578 if (Subtarget->isThumb()) {
579 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
580 CallOpc = ARMISD::CALL_NOLINK;
581 else
582 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
583 } else {
584 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000585 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
586 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000587 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000588 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
589 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000590 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000591 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000592 InFlag = Chain.getValue(1);
593 }
594
Evan Chenga8e29892007-01-19 07:51:42 +0000595 std::vector<MVT::ValueType> NodeTys;
596 NodeTys.push_back(MVT::Other); // Returns a chain
597 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
598
599 std::vector<SDOperand> Ops;
600 Ops.push_back(Chain);
601 Ops.push_back(Callee);
602
603 // Add argument registers to the end of the list so that they are known live
604 // into the call.
605 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
606 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
607 RegsToPass[i].second.getValueType()));
608
Evan Chenga8e29892007-01-19 07:51:42 +0000609 if (InFlag.Val)
610 Ops.push_back(InFlag);
611 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
612 InFlag = Chain.getValue(1);
613
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000614 Chain = DAG.getCALLSEQ_END(Chain,
615 DAG.getConstant(NumBytes, MVT::i32),
616 DAG.getConstant(0, MVT::i32),
617 InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000618 if (RetVT != MVT::Other)
619 InFlag = Chain.getValue(1);
620
621 std::vector<SDOperand> ResultVals;
622 NodeTys.clear();
623
624 // If the call has results, copy the values out of the ret val registers.
625 switch (RetVT) {
626 default: assert(0 && "Unexpected ret value!");
627 case MVT::Other:
628 break;
629 case MVT::i32:
630 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
631 ResultVals.push_back(Chain.getValue(0));
632 if (Op.Val->getValueType(1) == MVT::i32) {
633 // Returns a i64 value.
634 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
635 Chain.getValue(2)).getValue(1);
636 ResultVals.push_back(Chain.getValue(0));
637 NodeTys.push_back(MVT::i32);
638 }
639 NodeTys.push_back(MVT::i32);
640 break;
641 case MVT::f32:
642 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
643 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
644 Chain.getValue(0)));
645 NodeTys.push_back(MVT::f32);
646 break;
647 case MVT::f64: {
648 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
649 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
650 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
651 NodeTys.push_back(MVT::f64);
652 break;
653 }
654 }
655
656 NodeTys.push_back(MVT::Other);
657
658 if (ResultVals.empty())
659 return Chain;
660
661 ResultVals.push_back(Chain);
662 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
663 ResultVals.size());
664 return Res.getValue(Op.ResNo);
665}
666
667static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
668 SDOperand Copy;
669 SDOperand Chain = Op.getOperand(0);
670 switch(Op.getNumOperands()) {
671 default:
672 assert(0 && "Do not know how to return this many arguments!");
673 abort();
674 case 1: {
675 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
676 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
677 }
678 case 3:
679 Op = Op.getOperand(1);
680 if (Op.getValueType() == MVT::f32) {
681 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
682 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000683 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
684 // available.
685 Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
686 SDOperand Sign = DAG.getConstant(0, MVT::i32);
687 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign,
688 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000689 }
690 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
Chris Lattner84bc5422007-12-31 04:13:23 +0000691 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
692 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
Evan Chenga8e29892007-01-19 07:51:42 +0000693 break;
694 case 5:
695 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
696 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
697 // If we haven't noted the R0+R1 are live out, do so now.
Chris Lattner84bc5422007-12-31 04:13:23 +0000698 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
699 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
700 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
Evan Chenga8e29892007-01-19 07:51:42 +0000701 }
702 break;
703 }
704
705 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
706 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
707}
708
709// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
710// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
711// one of the above mentioned nodes. It has to be wrapped because otherwise
712// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
713// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000714// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000715static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
716 MVT::ValueType PtrVT = Op.getValueType();
717 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
718 SDOperand Res;
719 if (CP->isMachineConstantPoolEntry())
720 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
721 CP->getAlignment());
722 else
723 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
724 CP->getAlignment());
725 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
726}
727
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000728// Lower ISD::GlobalTLSAddress using the "general dynamic" model
729SDOperand
730ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
731 SelectionDAG &DAG) {
732 MVT::ValueType PtrVT = getPointerTy();
733 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
734 ARMConstantPoolValue *CPV =
735 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
736 PCAdj, "tlsgd", true);
737 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
738 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
739 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
740 SDOperand Chain = Argument.getValue(1);
741
742 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
743 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
744
745 // call __tls_get_addr.
746 ArgListTy Args;
747 ArgListEntry Entry;
748 Entry.Node = Argument;
749 Entry.Ty = (const Type *) Type::Int32Ty;
750 Args.push_back(Entry);
751 std::pair<SDOperand, SDOperand> CallResult =
752 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
753 CallingConv::C, false,
754 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
755 return CallResult.first;
756}
757
758// Lower ISD::GlobalTLSAddress using the "initial exec" or
759// "local exec" model.
760SDOperand
761ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
762 SelectionDAG &DAG) {
763 GlobalValue *GV = GA->getGlobal();
764 SDOperand Offset;
765 SDOperand Chain = DAG.getEntryNode();
766 MVT::ValueType PtrVT = getPointerTy();
767 // Get the Thread Pointer
768 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
769
770 if (GV->isDeclaration()){
771 // initial exec model
772 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
773 ARMConstantPoolValue *CPV =
774 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
775 PCAdj, "gottpoff", true);
776 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
777 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
778 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
779 Chain = Offset.getValue(1);
780
781 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
782 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
783
784 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
785 } else {
786 // local exec model
787 ARMConstantPoolValue *CPV =
788 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
789 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
790 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
791 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
792 }
793
794 // The address of the thread local variable is the add of the thread
795 // pointer with the offset of the variable.
796 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
797}
798
799SDOperand
800ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
801 // TODO: implement the "local dynamic" model
802 assert(Subtarget->isTargetELF() &&
803 "TLS not implemented for non-ELF targets");
804 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
805 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
806 // otherwise use the "Local Exec" TLS Model
807 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
808 return LowerToTLSGeneralDynamicModel(GA, DAG);
809 else
810 return LowerToTLSExecModels(GA, DAG);
811}
812
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000813SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
814 SelectionDAG &DAG) {
815 MVT::ValueType PtrVT = getPointerTy();
816 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
817 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
818 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000819 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000820 ARMConstantPoolValue *CPV =
821 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
822 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
823 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
824 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
825 SDOperand Chain = Result.getValue(1);
826 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
827 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
828 if (!UseGOTOFF)
829 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
830 return Result;
831 } else {
832 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
833 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
834 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
835 }
836}
837
Evan Chenga8e29892007-01-19 07:51:42 +0000838/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000839/// even in non-static mode.
840static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
841 return RelocM != Reloc::Static &&
842 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Gabor Greifa99be512007-07-05 17:07:56 +0000843 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000844}
845
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000846SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
847 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000848 MVT::ValueType PtrVT = getPointerTy();
849 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
850 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000851 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Evan Chenga8e29892007-01-19 07:51:42 +0000852 SDOperand CPAddr;
853 if (RelocM == Reloc::Static)
854 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
855 else {
856 unsigned PCAdj = (RelocM != Reloc::PIC_)
857 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000858 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
859 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000860 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000861 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000862 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
863 }
864 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
865
866 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
867 SDOperand Chain = Result.getValue(1);
868
869 if (RelocM == Reloc::PIC_) {
870 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
871 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
872 }
873 if (IsIndirect)
874 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
875
876 return Result;
877}
878
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000879SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
880 SelectionDAG &DAG){
881 assert(Subtarget->isTargetELF() &&
882 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
883 MVT::ValueType PtrVT = getPointerTy();
884 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
885 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
886 ARMPCLabelIndex,
887 ARMCP::CPValue, PCAdj);
888 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
889 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
890 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
891 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
892 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
893}
894
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000895static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
896 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
897 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
898 switch (IntNo) {
899 default: return SDOperand(); // Don't custom lower most intrinsics.
900 case Intrinsic::arm_thread_pointer:
901 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
902 }
903}
904
Evan Chenga8e29892007-01-19 07:51:42 +0000905static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
906 unsigned VarArgsFrameIndex) {
907 // vastart just stores the address of the VarArgsFrameIndex slot into the
908 // memory location argument.
909 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
910 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
911 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
912 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(),
913 SV->getOffset());
914}
915
916static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000917 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000918 unsigned &NumGPRs, unsigned &ArgOffset) {
919 MachineFunction &MF = DAG.getMachineFunction();
920 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
921 SDOperand Root = Op.getOperand(0);
922 std::vector<SDOperand> ArgValues;
Chris Lattner84bc5422007-12-31 04:13:23 +0000923 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000924
925 static const unsigned GPRArgRegs[] = {
926 ARM::R0, ARM::R1, ARM::R2, ARM::R3
927 };
928
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000929 unsigned ObjSize;
930 unsigned ObjGPRs;
931 unsigned GPRPad;
932 unsigned StackPad;
933 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
934 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
935 ObjSize, GPRPad, StackPad, Flags);
936 NumGPRs += GPRPad;
937 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000938
939 SDOperand ArgValue;
940 if (ObjGPRs == 1) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000941 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
942 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000943 vRegs[NumGPRs] = VReg;
944 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
945 if (ObjectVT == MVT::f32)
946 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
947 } else if (ObjGPRs == 2) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000948 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
949 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000950 vRegs[NumGPRs] = VReg;
951 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
952
Chris Lattner84bc5422007-12-31 04:13:23 +0000953 VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
954 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000955 vRegs[NumGPRs+1] = VReg;
956 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
957
Chris Lattner27a6c732007-11-24 07:07:01 +0000958 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
959 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000960 }
961 NumGPRs += ObjGPRs;
962
963 if (ObjSize) {
964 // If the argument is actually used, emit a load from the right stack
965 // slot.
966 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
967 MachineFrameInfo *MFI = MF.getFrameInfo();
968 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
969 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
970 if (ObjGPRs == 0)
971 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
972 else {
Chris Lattner27a6c732007-11-24 07:07:01 +0000973 SDOperand ArgValue2 = DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
974 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
975 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000976 }
977 } else {
978 // Don't emit a dead load.
979 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
980 }
981
982 ArgOffset += ObjSize; // Move on to the next argument.
983 }
984
985 return ArgValue;
986}
987
988SDOperand
989ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
990 std::vector<SDOperand> ArgValues;
991 SDOperand Root = Op.getOperand(0);
992 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
993 unsigned NumGPRs = 0; // GPRs used for parameter passing.
994 unsigned VRegs[4];
995
996 unsigned NumArgs = Op.Val->getNumValues()-1;
997 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
998 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
999 NumGPRs, ArgOffset));
1000
1001 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1002 if (isVarArg) {
1003 static const unsigned GPRArgRegs[] = {
1004 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1005 };
1006
1007 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001008 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +00001009 MachineFrameInfo *MFI = MF.getFrameInfo();
1010 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001011 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1012 unsigned VARegSize = (4 - NumGPRs) * 4;
1013 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001014 if (VARegSaveSize) {
1015 // If this function is vararg, store any remaining integer argument regs
1016 // to their spots on the stack so that they may be loaded by deferencing
1017 // the result of va_next.
1018 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001019 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1020 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +00001021 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1022
1023 SmallVector<SDOperand, 4> MemOps;
1024 for (; NumGPRs < 4; ++NumGPRs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001025 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1026 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +00001027 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1028 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1029 MemOps.push_back(Store);
1030 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1031 DAG.getConstant(4, getPointerTy()));
1032 }
1033 if (!MemOps.empty())
1034 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1035 &MemOps[0], MemOps.size());
1036 } else
1037 // This will point to the next argument passed via stack.
1038 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1039 }
1040
1041 ArgValues.push_back(Root);
1042
1043 // Return the new list of results.
1044 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1045 Op.Val->value_end());
1046 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1047}
1048
1049/// isFloatingPointZero - Return true if this is +0.0.
1050static bool isFloatingPointZero(SDOperand Op) {
1051 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001052 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001053 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1054 // Maybe this has already been legalized into the constant pool?
1055 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1056 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1057 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1058 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001059 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001060 }
1061 }
1062 return false;
1063}
1064
Evan Cheng9a2ef952007-02-02 01:53:26 +00001065static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001066 return ( isThumb && (C & ~255U) == 0) ||
1067 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1068}
1069
1070/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1071/// the given operands.
1072static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1073 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1074 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001075 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001076 if (!isLegalCmpImmediate(C, isThumb)) {
1077 // Constant does not fit, try adjusting it by one?
1078 switch (CC) {
1079 default: break;
1080 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001081 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001082 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001083 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1084 RHS = DAG.getConstant(C-1, MVT::i32);
1085 }
1086 break;
1087 case ISD::SETULT:
1088 case ISD::SETUGE:
1089 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1090 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001091 RHS = DAG.getConstant(C-1, MVT::i32);
1092 }
1093 break;
1094 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001095 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001096 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001097 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1098 RHS = DAG.getConstant(C+1, MVT::i32);
1099 }
1100 break;
1101 case ISD::SETULE:
1102 case ISD::SETUGT:
1103 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1104 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001105 RHS = DAG.getConstant(C+1, MVT::i32);
1106 }
1107 break;
1108 }
1109 }
1110 }
1111
1112 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001113 ARMISD::NodeType CompareType;
1114 switch (CondCode) {
1115 default:
1116 CompareType = ARMISD::CMP;
1117 break;
1118 case ARMCC::EQ:
1119 case ARMCC::NE:
1120 case ARMCC::MI:
1121 case ARMCC::PL:
1122 // Uses only N and Z Flags
1123 CompareType = ARMISD::CMPNZ;
1124 break;
1125 }
Evan Chenga8e29892007-01-19 07:51:42 +00001126 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001127 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001128}
1129
1130/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1131static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1132 SDOperand Cmp;
1133 if (!isFloatingPointZero(RHS))
1134 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1135 else
1136 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1137 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1138}
1139
1140static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1141 const ARMSubtarget *ST) {
1142 MVT::ValueType VT = Op.getValueType();
1143 SDOperand LHS = Op.getOperand(0);
1144 SDOperand RHS = Op.getOperand(1);
1145 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1146 SDOperand TrueVal = Op.getOperand(2);
1147 SDOperand FalseVal = Op.getOperand(3);
1148
1149 if (LHS.getValueType() == MVT::i32) {
1150 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001151 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001152 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001153 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001154 }
1155
1156 ARMCC::CondCodes CondCode, CondCode2;
1157 if (FPCCToARMCC(CC, CondCode, CondCode2))
1158 std::swap(TrueVal, FalseVal);
1159
1160 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001161 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001162 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1163 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001164 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001165 if (CondCode2 != ARMCC::AL) {
1166 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1167 // FIXME: Needs another CMP because flag can have but one use.
1168 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001169 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001170 }
1171 return Result;
1172}
1173
1174static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1175 const ARMSubtarget *ST) {
1176 SDOperand Chain = Op.getOperand(0);
1177 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1178 SDOperand LHS = Op.getOperand(2);
1179 SDOperand RHS = Op.getOperand(3);
1180 SDOperand Dest = Op.getOperand(4);
1181
1182 if (LHS.getValueType() == MVT::i32) {
1183 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001184 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001185 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001186 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001187 }
1188
1189 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1190 ARMCC::CondCodes CondCode, CondCode2;
1191 if (FPCCToARMCC(CC, CondCode, CondCode2))
1192 // Swap the LHS/RHS of the comparison if needed.
1193 std::swap(LHS, RHS);
1194
1195 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1196 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001197 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001198 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001199 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1200 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001201 if (CondCode2 != ARMCC::AL) {
1202 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001203 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1204 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001205 }
1206 return Res;
1207}
1208
1209SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1210 SDOperand Chain = Op.getOperand(0);
1211 SDOperand Table = Op.getOperand(1);
1212 SDOperand Index = Op.getOperand(2);
1213
1214 MVT::ValueType PTy = getPointerTy();
1215 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1216 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1217 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1218 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1219 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1220 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1221 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1222 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Evan Chenge2446c62007-06-26 18:31:22 +00001223 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1224 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001225 Chain = Addr.getValue(1);
1226 if (isPIC)
1227 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1228 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1229}
1230
1231static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1232 unsigned Opc =
1233 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1234 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1235 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1236}
1237
1238static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1239 MVT::ValueType VT = Op.getValueType();
1240 unsigned Opc =
1241 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1242
1243 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1244 return DAG.getNode(Opc, VT, Op);
1245}
1246
1247static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1248 // Implement fcopysign with a fabs and a conditional fneg.
1249 SDOperand Tmp0 = Op.getOperand(0);
1250 SDOperand Tmp1 = Op.getOperand(1);
1251 MVT::ValueType VT = Op.getValueType();
1252 MVT::ValueType SrcVT = Tmp1.getValueType();
1253 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1254 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1255 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001256 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1257 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001258}
1259
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001260SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain,
1261 SDOperand Dest,
1262 SDOperand Source,
1263 unsigned Size,
1264 unsigned Align,
1265 SelectionDAG &DAG) {
Evan Cheng4102eb52007-10-22 22:11:27 +00001266 // Do repeated 4-byte loads and stores. To be improved.
1267 assert((Align & 3) == 0 && "Expected 4-byte aligned addresses!");
1268 unsigned BytesLeft = Size & 3;
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001269 unsigned NumMemOps = Size >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001270 unsigned EmittedNumMemOps = 0;
1271 unsigned SrcOff = 0, DstOff = 0;
1272 MVT::ValueType VT = MVT::i32;
1273 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001274 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001275 const unsigned MAX_LOADS_IN_LDM = 6;
Evan Cheng4102eb52007-10-22 22:11:27 +00001276 SDOperand TFOps[MAX_LOADS_IN_LDM];
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001277 SDOperand Loads[MAX_LOADS_IN_LDM];
1278
Evan Cheng4102eb52007-10-22 22:11:27 +00001279 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1280 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001281 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001282 while (EmittedNumMemOps < NumMemOps) {
1283 for (i = 0;
1284 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001285 Loads[i] = DAG.getLoad(VT, Chain,
Evan Cheng4102eb52007-10-22 22:11:27 +00001286 DAG.getNode(ISD::ADD, MVT::i32, Source,
1287 DAG.getConstant(SrcOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001288 NULL, 0);
Evan Cheng4102eb52007-10-22 22:11:27 +00001289 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001290 SrcOff += VTSize;
1291 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001292 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001293
Evan Cheng4102eb52007-10-22 22:11:27 +00001294 for (i = 0;
1295 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1296 TFOps[i] = DAG.getStore(Chain, Loads[i],
1297 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1298 DAG.getConstant(DstOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001299 NULL, 0);
1300 DstOff += VTSize;
1301 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001302 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1303
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001304 EmittedNumMemOps += i;
1305 }
1306
Evan Cheng4102eb52007-10-22 22:11:27 +00001307 if (BytesLeft == 0)
1308 return Chain;
1309
1310 // Issue loads / stores for the trailing (1 - 3) bytes.
1311 unsigned BytesLeftSave = BytesLeft;
1312 i = 0;
1313 while (BytesLeft) {
1314 if (BytesLeft >= 2) {
1315 VT = MVT::i16;
1316 VTSize = 2;
1317 } else {
1318 VT = MVT::i8;
1319 VTSize = 1;
1320 }
1321
1322 Loads[i] = DAG.getLoad(VT, Chain,
1323 DAG.getNode(ISD::ADD, MVT::i32, Source,
1324 DAG.getConstant(SrcOff, MVT::i32)),
1325 NULL, 0);
1326 TFOps[i] = Loads[i].getValue(1);
1327 ++i;
1328 SrcOff += VTSize;
1329 BytesLeft -= VTSize;
1330 }
1331 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1332
1333 i = 0;
1334 BytesLeft = BytesLeftSave;
1335 while (BytesLeft) {
1336 if (BytesLeft >= 2) {
1337 VT = MVT::i16;
1338 VTSize = 2;
1339 } else {
1340 VT = MVT::i8;
1341 VTSize = 1;
1342 }
1343
1344 TFOps[i] = DAG.getStore(Chain, Loads[i],
1345 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1346 DAG.getConstant(DstOff, MVT::i32)),
1347 NULL, 0);
1348 ++i;
1349 DstOff += VTSize;
1350 BytesLeft -= VTSize;
1351 }
1352 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001353}
1354
Chris Lattner27a6c732007-11-24 07:07:01 +00001355static SDNode *ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1356 // Turn f64->i64 into FMRRD.
1357 assert(N->getValueType(0) == MVT::i64 &&
1358 N->getOperand(0).getValueType() == MVT::f64);
1359
1360 SDOperand Op = N->getOperand(0);
1361 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1362 &Op, 1);
1363
1364 // Merge the pieces into a single i64 value.
1365 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)).Val;
1366}
1367
1368static SDNode *ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1369 assert(N->getValueType(0) == MVT::i64 &&
1370 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1371 "Unknown shift to lower!");
1372
1373 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1374 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1375 cast<ConstantSDNode>(N->getOperand(1))->getValue() != 1)
1376 return 0;
1377
1378 // If we are in thumb mode, we don't have RRX.
1379 if (ST->isThumb()) return 0;
1380
1381 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1382 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1383 DAG.getConstant(0, MVT::i32));
1384 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1385 DAG.getConstant(1, MVT::i32));
1386
1387 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1388 // captures the result into a carry flag.
1389 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1390 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1391
1392 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1393 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1394
1395 // Merge the pieces into a single i64 value.
1396 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi).Val;
1397}
1398
1399
Evan Chenga8e29892007-01-19 07:51:42 +00001400SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1401 switch (Op.getOpcode()) {
1402 default: assert(0 && "Don't know how to custom lower this!"); abort();
1403 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001404 case ISD::GlobalAddress:
1405 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1406 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001407 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001408 case ISD::CALL: return LowerCALL(Op, DAG);
1409 case ISD::RET: return LowerRET(Op, DAG);
1410 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1411 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1412 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1413 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1414 case ISD::SINT_TO_FP:
1415 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1416 case ISD::FP_TO_SINT:
1417 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1418 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001419 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001420 case ISD::RETURNADDR: break;
1421 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001422 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Rafael Espindolae0703c82007-10-31 14:39:58 +00001423 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001424 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001425
1426
1427 // FIXME: Remove these when LegalizeDAGTypes lands.
1428 case ISD::BIT_CONVERT: return SDOperand(ExpandBIT_CONVERT(Op.Val, DAG), 0);
1429 case ISD::SRL:
1430 case ISD::SRA: return SDOperand(ExpandSRx(Op.Val, DAG,Subtarget),0);
Evan Chenga8e29892007-01-19 07:51:42 +00001431 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001432 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001433}
1434
Chris Lattner27a6c732007-11-24 07:07:01 +00001435
1436/// ExpandOperationResult - Provide custom lowering hooks for expanding
1437/// operations.
1438SDNode *ARMTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
1439 switch (N->getOpcode()) {
1440 default: assert(0 && "Don't know how to custom expand this!"); abort();
1441 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(N, DAG);
1442 case ISD::SRL:
1443 case ISD::SRA: return ExpandSRx(N, DAG, Subtarget);
1444 }
1445}
1446
1447
Evan Chenga8e29892007-01-19 07:51:42 +00001448//===----------------------------------------------------------------------===//
1449// ARM Scheduler Hooks
1450//===----------------------------------------------------------------------===//
1451
1452MachineBasicBlock *
1453ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1454 MachineBasicBlock *BB) {
1455 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1456 switch (MI->getOpcode()) {
1457 default: assert(false && "Unexpected instr type to insert");
1458 case ARM::tMOVCCr: {
1459 // To "insert" a SELECT_CC instruction, we actually have to insert the
1460 // diamond control-flow pattern. The incoming instruction knows the
1461 // destination vreg to set, the condition code register to branch on, the
1462 // true/false values to select between, and a branch opcode to use.
1463 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1464 ilist<MachineBasicBlock>::iterator It = BB;
1465 ++It;
1466
1467 // thisMBB:
1468 // ...
1469 // TrueVal = ...
1470 // cmpTY ccX, r1, r2
1471 // bCC copy1MBB
1472 // fallthrough --> copy0MBB
1473 MachineBasicBlock *thisMBB = BB;
1474 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1475 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1476 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001477 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001478 MachineFunction *F = BB->getParent();
1479 F->getBasicBlockList().insert(It, copy0MBB);
1480 F->getBasicBlockList().insert(It, sinkMBB);
1481 // Update machine-CFG edges by first adding all successors of the current
1482 // block to the new block which will contain the Phi node for the select.
1483 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1484 e = BB->succ_end(); i != e; ++i)
1485 sinkMBB->addSuccessor(*i);
1486 // Next, remove all successors of the current block, and add the true
1487 // and fallthrough blocks as its successors.
1488 while(!BB->succ_empty())
1489 BB->removeSuccessor(BB->succ_begin());
1490 BB->addSuccessor(copy0MBB);
1491 BB->addSuccessor(sinkMBB);
1492
1493 // copy0MBB:
1494 // %FalseValue = ...
1495 // # fallthrough to sinkMBB
1496 BB = copy0MBB;
1497
1498 // Update machine-CFG edges
1499 BB->addSuccessor(sinkMBB);
1500
1501 // sinkMBB:
1502 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1503 // ...
1504 BB = sinkMBB;
1505 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1506 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1507 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1508
1509 delete MI; // The pseudo instruction is gone now.
1510 return BB;
1511 }
1512 }
1513}
1514
1515//===----------------------------------------------------------------------===//
1516// ARM Optimization Hooks
1517//===----------------------------------------------------------------------===//
1518
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001519/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
1520static SDOperand PerformFMRRDCombine(SDNode *N,
1521 TargetLowering::DAGCombinerInfo &DCI) {
1522 // fmrrd(fmdrr x, y) -> x,y
1523 SDOperand InDouble = N->getOperand(0);
1524 if (InDouble.getOpcode() == ARMISD::FMDRR)
1525 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
1526 return SDOperand();
1527}
1528
1529SDOperand ARMTargetLowering::PerformDAGCombine(SDNode *N,
1530 DAGCombinerInfo &DCI) const {
1531 switch (N->getOpcode()) {
1532 default: break;
1533 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1534 }
1535
1536 return SDOperand();
1537}
1538
1539
Evan Chengb01fad62007-03-12 23:30:29 +00001540/// isLegalAddressImmediate - Return true if the integer value can be used
1541/// as the offset of the target addressing mode for load / store of the
1542/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001543static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1544 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001545 if (V == 0)
1546 return true;
1547
Evan Chengb01fad62007-03-12 23:30:29 +00001548 if (Subtarget->isThumb()) {
1549 if (V < 0)
1550 return false;
1551
1552 unsigned Scale = 1;
1553 switch (VT) {
1554 default: return false;
1555 case MVT::i1:
1556 case MVT::i8:
1557 // Scale == 1;
1558 break;
1559 case MVT::i16:
1560 // Scale == 2;
1561 Scale = 2;
1562 break;
1563 case MVT::i32:
1564 // Scale == 4;
1565 Scale = 4;
1566 break;
1567 }
1568
1569 if ((V & (Scale - 1)) != 0)
1570 return false;
1571 V /= Scale;
1572 return V == V & ((1LL << 5) - 1);
1573 }
1574
1575 if (V < 0)
1576 V = - V;
1577 switch (VT) {
1578 default: return false;
1579 case MVT::i1:
1580 case MVT::i8:
1581 case MVT::i32:
1582 // +- imm12
1583 return V == V & ((1LL << 12) - 1);
1584 case MVT::i16:
1585 // +- imm8
1586 return V == V & ((1LL << 8) - 1);
1587 case MVT::f32:
1588 case MVT::f64:
1589 if (!Subtarget->hasVFP2())
1590 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001591 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001592 return false;
1593 V >>= 2;
1594 return V == V & ((1LL << 8) - 1);
1595 }
Evan Chenga8e29892007-01-19 07:51:42 +00001596}
1597
Chris Lattner37caf8c2007-04-09 23:33:39 +00001598/// isLegalAddressingMode - Return true if the addressing mode represented
1599/// by AM is legal for this target, for a load/store of the specified type.
1600bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1601 const Type *Ty) const {
1602 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001603 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001604
1605 // Can never fold addr of global into load/store.
1606 if (AM.BaseGV)
1607 return false;
1608
1609 switch (AM.Scale) {
1610 case 0: // no scale reg, must be "r+i" or "r", or "i".
1611 break;
1612 case 1:
1613 if (Subtarget->isThumb())
1614 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001615 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001616 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001617 // ARM doesn't support any R+R*scale+imm addr modes.
1618 if (AM.BaseOffs)
1619 return false;
1620
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001621 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001622 switch (getValueType(Ty)) {
1623 default: return false;
1624 case MVT::i1:
1625 case MVT::i8:
1626 case MVT::i32:
1627 case MVT::i64:
1628 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1629 // ldrd / strd are used, then its address mode is same as i16.
1630 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001631 if (Scale < 0) Scale = -Scale;
1632 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001633 return true;
1634 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001635 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001636 case MVT::i16:
1637 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001638 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001639 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001640 return false;
1641
Chris Lattner37caf8c2007-04-09 23:33:39 +00001642 case MVT::isVoid:
1643 // Note, we allow "void" uses (basically, uses that aren't loads or
1644 // stores), because arm allows folding a scale into many arithmetic
1645 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001646
Chris Lattner37caf8c2007-04-09 23:33:39 +00001647 // Allow r << imm, but the imm has to be a multiple of two.
1648 if (AM.Scale & 1) return false;
1649 return isPowerOf2_32(AM.Scale);
1650 }
1651 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001652 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001653 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001654}
1655
Chris Lattner37caf8c2007-04-09 23:33:39 +00001656
Evan Chenga8e29892007-01-19 07:51:42 +00001657static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1658 bool isSEXTLoad, SDOperand &Base,
1659 SDOperand &Offset, bool &isInc,
1660 SelectionDAG &DAG) {
1661 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1662 return false;
1663
1664 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1665 // AddressingMode 3
1666 Base = Ptr->getOperand(0);
1667 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1668 int RHSC = (int)RHS->getValue();
1669 if (RHSC < 0 && RHSC > -256) {
1670 isInc = false;
1671 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1672 return true;
1673 }
1674 }
1675 isInc = (Ptr->getOpcode() == ISD::ADD);
1676 Offset = Ptr->getOperand(1);
1677 return true;
1678 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1679 // AddressingMode 2
1680 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1681 int RHSC = (int)RHS->getValue();
1682 if (RHSC < 0 && RHSC > -0x1000) {
1683 isInc = false;
1684 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1685 Base = Ptr->getOperand(0);
1686 return true;
1687 }
1688 }
1689
1690 if (Ptr->getOpcode() == ISD::ADD) {
1691 isInc = true;
1692 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1693 if (ShOpcVal != ARM_AM::no_shift) {
1694 Base = Ptr->getOperand(1);
1695 Offset = Ptr->getOperand(0);
1696 } else {
1697 Base = Ptr->getOperand(0);
1698 Offset = Ptr->getOperand(1);
1699 }
1700 return true;
1701 }
1702
1703 isInc = (Ptr->getOpcode() == ISD::ADD);
1704 Base = Ptr->getOperand(0);
1705 Offset = Ptr->getOperand(1);
1706 return true;
1707 }
1708
1709 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1710 return false;
1711}
1712
1713/// getPreIndexedAddressParts - returns true by value, base pointer and
1714/// offset pointer and addressing mode by reference if the node's address
1715/// can be legally represented as pre-indexed load / store address.
1716bool
1717ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1718 SDOperand &Offset,
1719 ISD::MemIndexedMode &AM,
1720 SelectionDAG &DAG) {
1721 if (Subtarget->isThumb())
1722 return false;
1723
1724 MVT::ValueType VT;
1725 SDOperand Ptr;
1726 bool isSEXTLoad = false;
1727 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1728 Ptr = LD->getBasePtr();
1729 VT = LD->getLoadedVT();
1730 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1731 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1732 Ptr = ST->getBasePtr();
1733 VT = ST->getStoredVT();
1734 } else
1735 return false;
1736
1737 bool isInc;
1738 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1739 isInc, DAG);
1740 if (isLegal) {
1741 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1742 return true;
1743 }
1744 return false;
1745}
1746
1747/// getPostIndexedAddressParts - returns true by value, base pointer and
1748/// offset pointer and addressing mode by reference if this node can be
1749/// combined with a load / store to form a post-indexed load / store.
1750bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1751 SDOperand &Base,
1752 SDOperand &Offset,
1753 ISD::MemIndexedMode &AM,
1754 SelectionDAG &DAG) {
1755 if (Subtarget->isThumb())
1756 return false;
1757
1758 MVT::ValueType VT;
1759 SDOperand Ptr;
1760 bool isSEXTLoad = false;
1761 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1762 VT = LD->getLoadedVT();
1763 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1764 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1765 VT = ST->getStoredVT();
1766 } else
1767 return false;
1768
1769 bool isInc;
1770 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1771 isInc, DAG);
1772 if (isLegal) {
1773 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1774 return true;
1775 }
1776 return false;
1777}
1778
1779void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1780 uint64_t Mask,
1781 uint64_t &KnownZero,
1782 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001783 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001784 unsigned Depth) const {
1785 KnownZero = 0;
1786 KnownOne = 0;
1787 switch (Op.getOpcode()) {
1788 default: break;
1789 case ARMISD::CMOV: {
1790 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001791 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001792 if (KnownZero == 0 && KnownOne == 0) return;
1793
1794 uint64_t KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001795 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1796 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001797 KnownZero &= KnownZeroRHS;
1798 KnownOne &= KnownOneRHS;
1799 return;
1800 }
1801 }
1802}
1803
1804//===----------------------------------------------------------------------===//
1805// ARM Inline Assembly Support
1806//===----------------------------------------------------------------------===//
1807
1808/// getConstraintType - Given a constraint letter, return the type of
1809/// constraint it is for this target.
1810ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001811ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1812 if (Constraint.size() == 1) {
1813 switch (Constraint[0]) {
1814 default: break;
1815 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001816 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001817 }
Evan Chenga8e29892007-01-19 07:51:42 +00001818 }
Chris Lattner4234f572007-03-25 02:14:49 +00001819 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001820}
1821
1822std::pair<unsigned, const TargetRegisterClass*>
1823ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1824 MVT::ValueType VT) const {
1825 if (Constraint.size() == 1) {
1826 // GCC RS6000 Constraint Letters
1827 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001828 case 'l':
1829 // FIXME: in thumb mode, 'l' is only low-regs.
1830 // FALL THROUGH.
1831 case 'r':
1832 return std::make_pair(0U, ARM::GPRRegisterClass);
1833 case 'w':
1834 if (VT == MVT::f32)
1835 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001836 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001837 return std::make_pair(0U, ARM::DPRRegisterClass);
1838 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001839 }
1840 }
1841 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1842}
1843
1844std::vector<unsigned> ARMTargetLowering::
1845getRegClassForInlineAsmConstraint(const std::string &Constraint,
1846 MVT::ValueType VT) const {
1847 if (Constraint.size() != 1)
1848 return std::vector<unsigned>();
1849
1850 switch (Constraint[0]) { // GCC ARM Constraint Letters
1851 default: break;
1852 case 'l':
1853 case 'r':
1854 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1855 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1856 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1857 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001858 case 'w':
1859 if (VT == MVT::f32)
1860 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1861 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1862 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1863 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1864 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1865 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1866 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1867 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1868 if (VT == MVT::f64)
1869 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1870 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1871 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1872 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1873 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001874 }
1875
1876 return std::vector<unsigned>();
1877}