blob: 65df5e8273f9d20cec84f1cfe53ef0e8b0df8932 [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
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000136 // ARM does not have i1 sign extending load.
137 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
138
Evan Chenga8e29892007-01-19 07:51:42 +0000139 // ARM supports all 4 flavors of integer indexed load / store.
140 for (unsigned im = (unsigned)ISD::PRE_INC;
141 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
142 setIndexedLoadAction(im, MVT::i1, Legal);
143 setIndexedLoadAction(im, MVT::i8, Legal);
144 setIndexedLoadAction(im, MVT::i16, Legal);
145 setIndexedLoadAction(im, MVT::i32, Legal);
146 setIndexedStoreAction(im, MVT::i1, Legal);
147 setIndexedStoreAction(im, MVT::i8, Legal);
148 setIndexedStoreAction(im, MVT::i16, Legal);
149 setIndexedStoreAction(im, MVT::i32, Legal);
150 }
151
152 // i64 operation support.
153 if (Subtarget->isThumb()) {
154 setOperationAction(ISD::MUL, MVT::i64, Expand);
155 setOperationAction(ISD::MULHU, MVT::i32, Expand);
156 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000157 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
158 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000159 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000160 setOperationAction(ISD::MUL, MVT::i64, Expand);
161 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000162 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000163 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000164 }
165 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
166 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
168 setOperationAction(ISD::SRL, MVT::i64, Custom);
169 setOperationAction(ISD::SRA, MVT::i64, Custom);
170
171 // ARM does not have ROTL.
172 setOperationAction(ISD::ROTL, MVT::i32, Expand);
173 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
174 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000175 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000176 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
177
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000178 // Only ARMv6 has BSWAP.
179 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000180 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000181
Evan Chenga8e29892007-01-19 07:51:42 +0000182 // These are expanded into libcalls.
183 setOperationAction(ISD::SDIV, MVT::i32, Expand);
184 setOperationAction(ISD::UDIV, MVT::i32, Expand);
185 setOperationAction(ISD::SREM, MVT::i32, Expand);
186 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000187 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
188 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000189
190 // Support label based line numbers.
191 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
192 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000193
194 setOperationAction(ISD::RET, MVT::Other, Custom);
195 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
196 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000197 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000198 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000199
200 // Expand mem operations genericly.
201 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000202 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000203 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Duncan Sands36397f52007-07-27 12:58:54 +0000204
Evan Chenga8e29892007-01-19 07:51:42 +0000205 // Use the default implementation.
206 setOperationAction(ISD::VASTART , MVT::Other, Expand);
207 setOperationAction(ISD::VAARG , MVT::Other, Expand);
208 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
209 setOperationAction(ISD::VAEND , MVT::Other, Expand);
210 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
211 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
212 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
213
214 if (!Subtarget->hasV6Ops()) {
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
217 }
218 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
219
Evan Chengb6ab2542007-01-31 08:40:13 +0000220 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000221 // Turn f64->i64 into FMRRD iff target supports vfp2.
222 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000223
224 // We want to custom lower some of our intrinsics.
225 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
226
Evan Chenga8e29892007-01-19 07:51:42 +0000227 setOperationAction(ISD::SETCC , MVT::i32, Expand);
228 setOperationAction(ISD::SETCC , MVT::f32, Expand);
229 setOperationAction(ISD::SETCC , MVT::f64, Expand);
230 setOperationAction(ISD::SELECT , MVT::i32, Expand);
231 setOperationAction(ISD::SELECT , MVT::f32, Expand);
232 setOperationAction(ISD::SELECT , MVT::f64, Expand);
233 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
234 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
235 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
236
237 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
238 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
239 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
240 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
241 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
242
243 setOperationAction(ISD::VASTART, MVT::Other, Custom);
244 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
245 setOperationAction(ISD::VAEND, MVT::Other, Expand);
246 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
247 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
248
249 // FP Constants can't be immediates.
250 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
251 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
252
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000253 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000254 setOperationAction(ISD::FSIN , MVT::f64, Expand);
255 setOperationAction(ISD::FSIN , MVT::f32, Expand);
256 setOperationAction(ISD::FCOS , MVT::f32, Expand);
257 setOperationAction(ISD::FCOS , MVT::f64, Expand);
258 setOperationAction(ISD::FREM , MVT::f64, Expand);
259 setOperationAction(ISD::FREM , MVT::f32, Expand);
260 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
261 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000262 setOperationAction(ISD::FPOW , MVT::f64, Expand);
263 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000264
265 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
266 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
267 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
268 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
269 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
270
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000271 // We have target-specific dag combine patterns for the following nodes:
272 // ARMISD::FMRRD - No need to call setTargetDAGCombine
273
Evan Chenga8e29892007-01-19 07:51:42 +0000274 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000275 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000276 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000277 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000278
279 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000280}
281
282
283const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
284 switch (Opcode) {
285 default: return 0;
286 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000287 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
288 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000289 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000290 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
291 case ARMISD::tCALL: return "ARMISD::tCALL";
292 case ARMISD::BRCOND: return "ARMISD::BRCOND";
293 case ARMISD::BR_JT: return "ARMISD::BR_JT";
294 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
295 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
296 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000297 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000298 case ARMISD::CMPFP: return "ARMISD::CMPFP";
299 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
300 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
301 case ARMISD::CMOV: return "ARMISD::CMOV";
302 case ARMISD::CNEG: return "ARMISD::CNEG";
303
304 case ARMISD::FTOSI: return "ARMISD::FTOSI";
305 case ARMISD::FTOUI: return "ARMISD::FTOUI";
306 case ARMISD::SITOF: return "ARMISD::SITOF";
307 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000308
309 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
310 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
311 case ARMISD::RRX: return "ARMISD::RRX";
312
313 case ARMISD::FMRRD: return "ARMISD::FMRRD";
314 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000315
316 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000317 }
318}
319
320//===----------------------------------------------------------------------===//
321// Lowering Code
322//===----------------------------------------------------------------------===//
323
324
325/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
326static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
327 switch (CC) {
328 default: assert(0 && "Unknown condition code!");
329 case ISD::SETNE: return ARMCC::NE;
330 case ISD::SETEQ: return ARMCC::EQ;
331 case ISD::SETGT: return ARMCC::GT;
332 case ISD::SETGE: return ARMCC::GE;
333 case ISD::SETLT: return ARMCC::LT;
334 case ISD::SETLE: return ARMCC::LE;
335 case ISD::SETUGT: return ARMCC::HI;
336 case ISD::SETUGE: return ARMCC::HS;
337 case ISD::SETULT: return ARMCC::LO;
338 case ISD::SETULE: return ARMCC::LS;
339 }
340}
341
342/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
343/// returns true if the operands should be inverted to form the proper
344/// comparison.
345static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
346 ARMCC::CondCodes &CondCode2) {
347 bool Invert = false;
348 CondCode2 = ARMCC::AL;
349 switch (CC) {
350 default: assert(0 && "Unknown FP condition!");
351 case ISD::SETEQ:
352 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
353 case ISD::SETGT:
354 case ISD::SETOGT: CondCode = ARMCC::GT; break;
355 case ISD::SETGE:
356 case ISD::SETOGE: CondCode = ARMCC::GE; break;
357 case ISD::SETOLT: CondCode = ARMCC::MI; break;
358 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
359 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
360 case ISD::SETO: CondCode = ARMCC::VC; break;
361 case ISD::SETUO: CondCode = ARMCC::VS; break;
362 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
363 case ISD::SETUGT: CondCode = ARMCC::HI; break;
364 case ISD::SETUGE: CondCode = ARMCC::PL; break;
365 case ISD::SETLT:
366 case ISD::SETULT: CondCode = ARMCC::LT; break;
367 case ISD::SETLE:
368 case ISD::SETULE: CondCode = ARMCC::LE; break;
369 case ISD::SETNE:
370 case ISD::SETUNE: CondCode = ARMCC::NE; break;
371 }
372 return Invert;
373}
374
375static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000376HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
377 unsigned StackOffset, unsigned &NeededGPRs,
378 unsigned &NeededStackSize, unsigned &GPRPad,
379 unsigned &StackPad, unsigned Flags) {
380 NeededStackSize = 0;
381 NeededGPRs = 0;
382 StackPad = 0;
383 GPRPad = 0;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000384 unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000385 GPRPad = NumGPRs % ((align + 3)/4);
386 StackPad = StackOffset % align;
387 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000388 switch (ObjectVT) {
389 default: assert(0 && "Unhandled argument type!");
390 case MVT::i32:
391 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000392 if (firstGPR < 4)
393 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000394 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000395 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000396 break;
397 case MVT::i64:
398 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000399 if (firstGPR < 3)
400 NeededGPRs = 2;
401 else if (firstGPR == 3) {
402 NeededGPRs = 1;
403 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000404 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000405 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000406 }
407}
408
Evan Chengfc403422007-02-03 08:53:01 +0000409/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
410/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
411/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000412SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
413 MVT::ValueType RetVT= Op.Val->getValueType(0);
414 SDOperand Chain = Op.getOperand(0);
415 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
416 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000417 CallConv == CallingConv::Fast) && "unknown calling convention");
418 SDOperand Callee = Op.getOperand(4);
419 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
420 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
421 unsigned NumGPRs = 0; // GPRs used for parameter passing.
422
423 // Count how many bytes are to be pushed on the stack.
424 unsigned NumBytes = 0;
425
426 // Add up all the space actually used.
427 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000428 unsigned ObjSize;
429 unsigned ObjGPRs;
430 unsigned StackPad;
431 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000432 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000433 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
434 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
435 GPRPad, StackPad, Flags);
436 NumBytes += ObjSize + StackPad;
437 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000438 }
439
440 // Adjust the stack pointer for the new arguments...
441 // These operations are automatically eliminated by the prolog/epilog pass
442 Chain = DAG.getCALLSEQ_START(Chain,
443 DAG.getConstant(NumBytes, MVT::i32));
444
445 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
446
447 static const unsigned GPRArgRegs[] = {
448 ARM::R0, ARM::R1, ARM::R2, ARM::R3
449 };
450
451 NumGPRs = 0;
452 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
453 std::vector<SDOperand> MemOpChains;
454 for (unsigned i = 0; i != NumOps; ++i) {
455 SDOperand Arg = Op.getOperand(5+2*i);
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000456 unsigned Flags = Op.getConstantOperandVal(5+2*i+1);
Evan Chenga8e29892007-01-19 07:51:42 +0000457 MVT::ValueType ArgVT = Arg.getValueType();
458
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000459 unsigned ObjSize;
460 unsigned ObjGPRs;
461 unsigned GPRPad;
462 unsigned StackPad;
463 HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
464 ObjSize, GPRPad, StackPad, Flags);
465 NumGPRs += GPRPad;
466 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000467 if (ObjGPRs > 0) {
468 switch (ArgVT) {
469 default: assert(0 && "Unexpected ValueType for argument!");
470 case MVT::i32:
471 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
472 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000473 case MVT::f32:
Evan Chenga8e29892007-01-19 07:51:42 +0000474 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
475 DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg)));
476 break;
477 case MVT::i64: {
478 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
479 DAG.getConstant(0, getPointerTy()));
480 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg,
481 DAG.getConstant(1, getPointerTy()));
482 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
483 if (ObjGPRs == 2)
484 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
485 else {
486 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
487 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
488 MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0));
489 }
490 break;
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000491 }
Evan Chenga8e29892007-01-19 07:51:42 +0000492 case MVT::f64: {
493 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD,
494 DAG.getVTList(MVT::i32, MVT::i32),
495 &Arg, 1);
496 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
497 if (ObjGPRs == 2)
498 RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
499 Cvt.getValue(1)));
500 else {
501 SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
502 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
503 MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff,
504 NULL, 0));
505 }
506 break;
507 }
508 }
509 } else {
510 assert(ObjSize != 0);
511 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
512 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
513 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
514 }
515
516 NumGPRs += ObjGPRs;
517 ArgOffset += ObjSize;
518 }
519
520 if (!MemOpChains.empty())
521 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
522 &MemOpChains[0], MemOpChains.size());
523
524 // Build a sequence of copy-to-reg nodes chained together with token chain
525 // and flag operands which copy the outgoing args into the appropriate regs.
526 SDOperand InFlag;
527 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
528 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
529 InFlag);
530 InFlag = Chain.getValue(1);
531 }
532
533 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
534 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
535 // node so that legalize doesn't hack it.
536 bool isDirect = false;
537 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000538 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000539 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
540 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000541 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000542 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000543 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000544 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000545 getTargetMachine().getRelocationModel() != Reloc::Static;
546 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000547 // ARM call to a local ARM function is predicable.
548 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000549 // tBX takes a register source operand.
550 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
551 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
552 ARMCP::CPStub, 4);
553 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
554 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
555 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
556 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
557 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
558 } else
559 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000560 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000561 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000562 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000563 getTargetMachine().getRelocationModel() != Reloc::Static;
564 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000565 // tBX takes a register source operand.
566 const char *Sym = S->getSymbol();
567 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
568 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
569 ARMCP::CPStub, 4);
570 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
571 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
572 Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0);
573 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
574 Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel);
575 } else
576 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000577 }
578
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000579 // FIXME: handle tail calls differently.
580 unsigned CallOpc;
581 if (Subtarget->isThumb()) {
582 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
583 CallOpc = ARMISD::CALL_NOLINK;
584 else
585 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
586 } else {
587 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000588 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
589 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000590 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000591 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
592 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000593 Chain = DAG.getCopyToReg(Chain, ARM::LR,
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000594 DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000595 InFlag = Chain.getValue(1);
596 }
597
Evan Chenga8e29892007-01-19 07:51:42 +0000598 std::vector<MVT::ValueType> NodeTys;
599 NodeTys.push_back(MVT::Other); // Returns a chain
600 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
601
602 std::vector<SDOperand> Ops;
603 Ops.push_back(Chain);
604 Ops.push_back(Callee);
605
606 // Add argument registers to the end of the list so that they are known live
607 // into the call.
608 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
609 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
610 RegsToPass[i].second.getValueType()));
611
Evan Chenga8e29892007-01-19 07:51:42 +0000612 if (InFlag.Val)
613 Ops.push_back(InFlag);
614 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
615 InFlag = Chain.getValue(1);
616
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000617 Chain = DAG.getCALLSEQ_END(Chain,
618 DAG.getConstant(NumBytes, MVT::i32),
619 DAG.getConstant(0, MVT::i32),
620 InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000621 if (RetVT != MVT::Other)
622 InFlag = Chain.getValue(1);
623
624 std::vector<SDOperand> ResultVals;
625 NodeTys.clear();
626
627 // If the call has results, copy the values out of the ret val registers.
628 switch (RetVT) {
629 default: assert(0 && "Unexpected ret value!");
630 case MVT::Other:
631 break;
632 case MVT::i32:
633 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
634 ResultVals.push_back(Chain.getValue(0));
635 if (Op.Val->getValueType(1) == MVT::i32) {
636 // Returns a i64 value.
637 Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32,
638 Chain.getValue(2)).getValue(1);
639 ResultVals.push_back(Chain.getValue(0));
640 NodeTys.push_back(MVT::i32);
641 }
642 NodeTys.push_back(MVT::i32);
643 break;
644 case MVT::f32:
645 Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1);
646 ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32,
647 Chain.getValue(0)));
648 NodeTys.push_back(MVT::f32);
649 break;
650 case MVT::f64: {
651 SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag);
652 SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2));
653 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi));
654 NodeTys.push_back(MVT::f64);
655 break;
656 }
657 }
658
659 NodeTys.push_back(MVT::Other);
660
661 if (ResultVals.empty())
662 return Chain;
663
664 ResultVals.push_back(Chain);
665 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0],
666 ResultVals.size());
667 return Res.getValue(Op.ResNo);
668}
669
670static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
671 SDOperand Copy;
672 SDOperand Chain = Op.getOperand(0);
673 switch(Op.getNumOperands()) {
674 default:
675 assert(0 && "Do not know how to return this many arguments!");
676 abort();
677 case 1: {
678 SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32);
679 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
680 }
681 case 3:
682 Op = Op.getOperand(1);
683 if (Op.getValueType() == MVT::f32) {
684 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
685 } else if (Op.getValueType() == MVT::f64) {
Chris Lattner65a33232007-10-18 06:17:07 +0000686 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
687 // available.
688 Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
689 SDOperand Sign = DAG.getConstant(0, MVT::i32);
690 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign,
691 Op.getValue(1), Sign);
Evan Chenga8e29892007-01-19 07:51:42 +0000692 }
693 Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand());
Chris Lattner84bc5422007-12-31 04:13:23 +0000694 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
695 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
Evan Chenga8e29892007-01-19 07:51:42 +0000696 break;
697 case 5:
698 Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand());
699 Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1));
700 // If we haven't noted the R0+R1 are live out, do so now.
Chris Lattner84bc5422007-12-31 04:13:23 +0000701 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
702 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
703 DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
Evan Chenga8e29892007-01-19 07:51:42 +0000704 }
705 break;
706 }
707
708 //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
709 return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
710}
711
712// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
713// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
714// one of the above mentioned nodes. It has to be wrapped because otherwise
715// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
716// be used to form addressing mode. These wrapped nodes will be selected
Evan Cheng9f6636f2007-03-19 07:48:02 +0000717// into MOVi.
Evan Chenga8e29892007-01-19 07:51:42 +0000718static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
719 MVT::ValueType PtrVT = Op.getValueType();
720 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
721 SDOperand Res;
722 if (CP->isMachineConstantPoolEntry())
723 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
724 CP->getAlignment());
725 else
726 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
727 CP->getAlignment());
728 return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
729}
730
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000731// Lower ISD::GlobalTLSAddress using the "general dynamic" model
732SDOperand
733ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
734 SelectionDAG &DAG) {
735 MVT::ValueType PtrVT = getPointerTy();
736 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
737 ARMConstantPoolValue *CPV =
738 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
739 PCAdj, "tlsgd", true);
740 SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
741 Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
742 Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0);
743 SDOperand Chain = Argument.getValue(1);
744
745 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
746 Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel);
747
748 // call __tls_get_addr.
749 ArgListTy Args;
750 ArgListEntry Entry;
751 Entry.Node = Argument;
752 Entry.Ty = (const Type *) Type::Int32Ty;
753 Args.push_back(Entry);
754 std::pair<SDOperand, SDOperand> CallResult =
755 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false,
756 CallingConv::C, false,
757 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG);
758 return CallResult.first;
759}
760
761// Lower ISD::GlobalTLSAddress using the "initial exec" or
762// "local exec" model.
763SDOperand
764ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
765 SelectionDAG &DAG) {
766 GlobalValue *GV = GA->getGlobal();
767 SDOperand Offset;
768 SDOperand Chain = DAG.getEntryNode();
769 MVT::ValueType PtrVT = getPointerTy();
770 // Get the Thread Pointer
771 SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
772
773 if (GV->isDeclaration()){
774 // initial exec model
775 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
776 ARMConstantPoolValue *CPV =
777 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
778 PCAdj, "gottpoff", true);
779 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
780 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
781 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
782 Chain = Offset.getValue(1);
783
784 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
785 Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel);
786
787 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
788 } else {
789 // local exec model
790 ARMConstantPoolValue *CPV =
791 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
792 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
793 Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
794 Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0);
795 }
796
797 // The address of the thread local variable is the add of the thread
798 // pointer with the offset of the variable.
799 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
800}
801
802SDOperand
803ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
804 // TODO: implement the "local dynamic" model
805 assert(Subtarget->isTargetELF() &&
806 "TLS not implemented for non-ELF targets");
807 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
808 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
809 // otherwise use the "Local Exec" TLS Model
810 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
811 return LowerToTLSGeneralDynamicModel(GA, DAG);
812 else
813 return LowerToTLSExecModels(GA, DAG);
814}
815
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000816SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op,
817 SelectionDAG &DAG) {
818 MVT::ValueType PtrVT = getPointerTy();
819 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
820 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
821 if (RelocM == Reloc::PIC_) {
Lauro Ramos Venancio5d3d44a2007-05-14 23:20:21 +0000822 bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000823 ARMConstantPoolValue *CPV =
824 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
825 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
826 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
827 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
828 SDOperand Chain = Result.getValue(1);
829 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
830 Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT);
831 if (!UseGOTOFF)
832 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
833 return Result;
834 } else {
835 SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
836 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
837 return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
838 }
839}
840
Evan Chenga8e29892007-01-19 07:51:42 +0000841/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000842/// even in non-static mode.
843static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
844 return RelocM != Reloc::Static &&
845 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Gabor Greifa99be512007-07-05 17:07:56 +0000846 (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode()));
Evan Chenga8e29892007-01-19 07:51:42 +0000847}
848
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000849SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op,
850 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +0000851 MVT::ValueType PtrVT = getPointerTy();
852 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
853 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +0000854 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Evan Chenga8e29892007-01-19 07:51:42 +0000855 SDOperand CPAddr;
856 if (RelocM == Reloc::Static)
857 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
858 else {
859 unsigned PCAdj = (RelocM != Reloc::PIC_)
860 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +0000861 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
862 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +0000863 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +0000864 Kind, PCAdj);
Evan Chenga8e29892007-01-19 07:51:42 +0000865 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
866 }
867 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
868
869 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
870 SDOperand Chain = Result.getValue(1);
871
872 if (RelocM == Reloc::PIC_) {
873 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
874 Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
875 }
876 if (IsIndirect)
877 Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0);
878
879 return Result;
880}
881
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000882SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op,
883 SelectionDAG &DAG){
884 assert(Subtarget->isTargetELF() &&
885 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
886 MVT::ValueType PtrVT = getPointerTy();
887 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
888 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
889 ARMPCLabelIndex,
890 ARMCP::CPValue, PCAdj);
891 SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
892 CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
893 SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0);
894 SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
895 return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel);
896}
897
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000898static SDOperand LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
899 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
900 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
901 switch (IntNo) {
902 default: return SDOperand(); // Don't custom lower most intrinsics.
903 case Intrinsic::arm_thread_pointer:
904 return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
905 }
906}
907
Evan Chenga8e29892007-01-19 07:51:42 +0000908static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
909 unsigned VarArgsFrameIndex) {
910 // vastart just stores the address of the VarArgsFrameIndex slot into the
911 // memory location argument.
912 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
913 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000914 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
915 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000916}
917
918static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000919 unsigned *vRegs, unsigned ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000920 unsigned &NumGPRs, unsigned &ArgOffset) {
921 MachineFunction &MF = DAG.getMachineFunction();
922 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
923 SDOperand Root = Op.getOperand(0);
924 std::vector<SDOperand> ArgValues;
Chris Lattner84bc5422007-12-31 04:13:23 +0000925 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000926
927 static const unsigned GPRArgRegs[] = {
928 ARM::R0, ARM::R1, ARM::R2, ARM::R3
929 };
930
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000931 unsigned ObjSize;
932 unsigned ObjGPRs;
933 unsigned GPRPad;
934 unsigned StackPad;
935 unsigned Flags = Op.getConstantOperandVal(ArgNo + 3);
936 HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
937 ObjSize, GPRPad, StackPad, Flags);
938 NumGPRs += GPRPad;
939 ArgOffset += StackPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000940
941 SDOperand ArgValue;
942 if (ObjGPRs == 1) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000943 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
944 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000945 vRegs[NumGPRs] = VReg;
946 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
947 if (ObjectVT == MVT::f32)
948 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
949 } else if (ObjGPRs == 2) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000950 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
951 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000952 vRegs[NumGPRs] = VReg;
953 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
954
Chris Lattner84bc5422007-12-31 04:13:23 +0000955 VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
956 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000957 vRegs[NumGPRs+1] = VReg;
958 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
959
Chris Lattner27a6c732007-11-24 07:07:01 +0000960 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
961 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000962 }
963 NumGPRs += ObjGPRs;
964
965 if (ObjSize) {
966 // If the argument is actually used, emit a load from the right stack
967 // slot.
968 if (!Op.Val->hasNUsesOfValue(0, ArgNo)) {
969 MachineFrameInfo *MFI = MF.getFrameInfo();
970 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
971 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
972 if (ObjGPRs == 0)
973 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
974 else {
Chris Lattner27a6c732007-11-24 07:07:01 +0000975 SDOperand ArgValue2 = DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
976 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
977 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000978 }
979 } else {
980 // Don't emit a dead load.
981 ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT);
982 }
983
984 ArgOffset += ObjSize; // Move on to the next argument.
985 }
986
987 return ArgValue;
988}
989
990SDOperand
991ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
992 std::vector<SDOperand> ArgValues;
993 SDOperand Root = Op.getOperand(0);
994 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
995 unsigned NumGPRs = 0; // GPRs used for parameter passing.
996 unsigned VRegs[4];
997
998 unsigned NumArgs = Op.Val->getNumValues()-1;
999 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
1000 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo,
1001 NumGPRs, ArgOffset));
1002
1003 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1004 if (isVarArg) {
1005 static const unsigned GPRArgRegs[] = {
1006 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1007 };
1008
1009 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001010 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +00001011 MachineFrameInfo *MFI = MF.getFrameInfo();
1012 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001013 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1014 unsigned VARegSize = (4 - NumGPRs) * 4;
1015 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001016 if (VARegSaveSize) {
1017 // If this function is vararg, store any remaining integer argument regs
1018 // to their spots on the stack so that they may be loaded by deferencing
1019 // the result of va_next.
1020 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001021 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1022 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +00001023 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1024
1025 SmallVector<SDOperand, 4> MemOps;
1026 for (; NumGPRs < 4; ++NumGPRs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001027 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1028 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +00001029 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1030 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1031 MemOps.push_back(Store);
1032 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1033 DAG.getConstant(4, getPointerTy()));
1034 }
1035 if (!MemOps.empty())
1036 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1037 &MemOps[0], MemOps.size());
1038 } else
1039 // This will point to the next argument passed via stack.
1040 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1041 }
1042
1043 ArgValues.push_back(Root);
1044
1045 // Return the new list of results.
1046 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1047 Op.Val->value_end());
1048 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1049}
1050
1051/// isFloatingPointZero - Return true if this is +0.0.
1052static bool isFloatingPointZero(SDOperand Op) {
1053 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001054 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001055 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1056 // Maybe this has already been legalized into the constant pool?
1057 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1058 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1059 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1060 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001061 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001062 }
1063 }
1064 return false;
1065}
1066
Evan Cheng9a2ef952007-02-02 01:53:26 +00001067static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001068 return ( isThumb && (C & ~255U) == 0) ||
1069 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1070}
1071
1072/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1073/// the given operands.
1074static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1075 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1076 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001077 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001078 if (!isLegalCmpImmediate(C, isThumb)) {
1079 // Constant does not fit, try adjusting it by one?
1080 switch (CC) {
1081 default: break;
1082 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001083 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001084 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001085 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1086 RHS = DAG.getConstant(C-1, MVT::i32);
1087 }
1088 break;
1089 case ISD::SETULT:
1090 case ISD::SETUGE:
1091 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1092 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001093 RHS = DAG.getConstant(C-1, MVT::i32);
1094 }
1095 break;
1096 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001097 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001098 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001099 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1100 RHS = DAG.getConstant(C+1, MVT::i32);
1101 }
1102 break;
1103 case ISD::SETULE:
1104 case ISD::SETUGT:
1105 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1106 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001107 RHS = DAG.getConstant(C+1, MVT::i32);
1108 }
1109 break;
1110 }
1111 }
1112 }
1113
1114 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001115 ARMISD::NodeType CompareType;
1116 switch (CondCode) {
1117 default:
1118 CompareType = ARMISD::CMP;
1119 break;
1120 case ARMCC::EQ:
1121 case ARMCC::NE:
1122 case ARMCC::MI:
1123 case ARMCC::PL:
1124 // Uses only N and Z Flags
1125 CompareType = ARMISD::CMPNZ;
1126 break;
1127 }
Evan Chenga8e29892007-01-19 07:51:42 +00001128 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001129 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001130}
1131
1132/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1133static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1134 SDOperand Cmp;
1135 if (!isFloatingPointZero(RHS))
1136 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1137 else
1138 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1139 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1140}
1141
1142static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1143 const ARMSubtarget *ST) {
1144 MVT::ValueType VT = Op.getValueType();
1145 SDOperand LHS = Op.getOperand(0);
1146 SDOperand RHS = Op.getOperand(1);
1147 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1148 SDOperand TrueVal = Op.getOperand(2);
1149 SDOperand FalseVal = Op.getOperand(3);
1150
1151 if (LHS.getValueType() == MVT::i32) {
1152 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001153 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001154 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001155 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001156 }
1157
1158 ARMCC::CondCodes CondCode, CondCode2;
1159 if (FPCCToARMCC(CC, CondCode, CondCode2))
1160 std::swap(TrueVal, FalseVal);
1161
1162 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001163 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001164 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1165 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001166 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001167 if (CondCode2 != ARMCC::AL) {
1168 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1169 // FIXME: Needs another CMP because flag can have but one use.
1170 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001171 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001172 }
1173 return Result;
1174}
1175
1176static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1177 const ARMSubtarget *ST) {
1178 SDOperand Chain = Op.getOperand(0);
1179 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1180 SDOperand LHS = Op.getOperand(2);
1181 SDOperand RHS = Op.getOperand(3);
1182 SDOperand Dest = Op.getOperand(4);
1183
1184 if (LHS.getValueType() == MVT::i32) {
1185 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001186 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001187 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001188 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001189 }
1190
1191 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1192 ARMCC::CondCodes CondCode, CondCode2;
1193 if (FPCCToARMCC(CC, CondCode, CondCode2))
1194 // Swap the LHS/RHS of the comparison if needed.
1195 std::swap(LHS, RHS);
1196
1197 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1198 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001199 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001200 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001201 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1202 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001203 if (CondCode2 != ARMCC::AL) {
1204 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001205 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1206 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001207 }
1208 return Res;
1209}
1210
1211SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1212 SDOperand Chain = Op.getOperand(0);
1213 SDOperand Table = Op.getOperand(1);
1214 SDOperand Index = Op.getOperand(2);
1215
1216 MVT::ValueType PTy = getPointerTy();
1217 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1218 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1219 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1220 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1221 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1222 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1223 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1224 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Evan Chenge2446c62007-06-26 18:31:22 +00001225 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1226 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001227 Chain = Addr.getValue(1);
1228 if (isPIC)
1229 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1230 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1231}
1232
1233static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1234 unsigned Opc =
1235 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1236 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1237 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1238}
1239
1240static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1241 MVT::ValueType VT = Op.getValueType();
1242 unsigned Opc =
1243 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1244
1245 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1246 return DAG.getNode(Opc, VT, Op);
1247}
1248
1249static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1250 // Implement fcopysign with a fabs and a conditional fneg.
1251 SDOperand Tmp0 = Op.getOperand(0);
1252 SDOperand Tmp1 = Op.getOperand(1);
1253 MVT::ValueType VT = Op.getValueType();
1254 MVT::ValueType SrcVT = Tmp1.getValueType();
1255 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1256 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1257 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001258 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1259 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001260}
1261
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001262SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain,
1263 SDOperand Dest,
1264 SDOperand Source,
1265 unsigned Size,
1266 unsigned Align,
1267 SelectionDAG &DAG) {
Evan Cheng4102eb52007-10-22 22:11:27 +00001268 // Do repeated 4-byte loads and stores. To be improved.
1269 assert((Align & 3) == 0 && "Expected 4-byte aligned addresses!");
1270 unsigned BytesLeft = Size & 3;
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001271 unsigned NumMemOps = Size >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001272 unsigned EmittedNumMemOps = 0;
1273 unsigned SrcOff = 0, DstOff = 0;
1274 MVT::ValueType VT = MVT::i32;
1275 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001276 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001277 const unsigned MAX_LOADS_IN_LDM = 6;
Evan Cheng4102eb52007-10-22 22:11:27 +00001278 SDOperand TFOps[MAX_LOADS_IN_LDM];
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001279 SDOperand Loads[MAX_LOADS_IN_LDM];
1280
Evan Cheng4102eb52007-10-22 22:11:27 +00001281 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1282 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001283 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001284 while (EmittedNumMemOps < NumMemOps) {
1285 for (i = 0;
1286 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001287 Loads[i] = DAG.getLoad(VT, Chain,
Evan Cheng4102eb52007-10-22 22:11:27 +00001288 DAG.getNode(ISD::ADD, MVT::i32, Source,
1289 DAG.getConstant(SrcOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001290 NULL, 0);
Evan Cheng4102eb52007-10-22 22:11:27 +00001291 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001292 SrcOff += VTSize;
1293 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001294 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001295
Evan Cheng4102eb52007-10-22 22:11:27 +00001296 for (i = 0;
1297 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1298 TFOps[i] = DAG.getStore(Chain, Loads[i],
1299 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1300 DAG.getConstant(DstOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001301 NULL, 0);
1302 DstOff += VTSize;
1303 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001304 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1305
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001306 EmittedNumMemOps += i;
1307 }
1308
Evan Cheng4102eb52007-10-22 22:11:27 +00001309 if (BytesLeft == 0)
1310 return Chain;
1311
1312 // Issue loads / stores for the trailing (1 - 3) bytes.
1313 unsigned BytesLeftSave = BytesLeft;
1314 i = 0;
1315 while (BytesLeft) {
1316 if (BytesLeft >= 2) {
1317 VT = MVT::i16;
1318 VTSize = 2;
1319 } else {
1320 VT = MVT::i8;
1321 VTSize = 1;
1322 }
1323
1324 Loads[i] = DAG.getLoad(VT, Chain,
1325 DAG.getNode(ISD::ADD, MVT::i32, Source,
1326 DAG.getConstant(SrcOff, MVT::i32)),
1327 NULL, 0);
1328 TFOps[i] = Loads[i].getValue(1);
1329 ++i;
1330 SrcOff += VTSize;
1331 BytesLeft -= VTSize;
1332 }
1333 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1334
1335 i = 0;
1336 BytesLeft = BytesLeftSave;
1337 while (BytesLeft) {
1338 if (BytesLeft >= 2) {
1339 VT = MVT::i16;
1340 VTSize = 2;
1341 } else {
1342 VT = MVT::i8;
1343 VTSize = 1;
1344 }
1345
1346 TFOps[i] = DAG.getStore(Chain, Loads[i],
1347 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1348 DAG.getConstant(DstOff, MVT::i32)),
1349 NULL, 0);
1350 ++i;
1351 DstOff += VTSize;
1352 BytesLeft -= VTSize;
1353 }
1354 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001355}
1356
Chris Lattner27a6c732007-11-24 07:07:01 +00001357static SDNode *ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1358 // Turn f64->i64 into FMRRD.
1359 assert(N->getValueType(0) == MVT::i64 &&
1360 N->getOperand(0).getValueType() == MVT::f64);
1361
1362 SDOperand Op = N->getOperand(0);
1363 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1364 &Op, 1);
1365
1366 // Merge the pieces into a single i64 value.
1367 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)).Val;
1368}
1369
1370static SDNode *ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1371 assert(N->getValueType(0) == MVT::i64 &&
1372 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1373 "Unknown shift to lower!");
1374
1375 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1376 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1377 cast<ConstantSDNode>(N->getOperand(1))->getValue() != 1)
1378 return 0;
1379
1380 // If we are in thumb mode, we don't have RRX.
1381 if (ST->isThumb()) return 0;
1382
1383 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1384 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1385 DAG.getConstant(0, MVT::i32));
1386 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1387 DAG.getConstant(1, MVT::i32));
1388
1389 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1390 // captures the result into a carry flag.
1391 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1392 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1393
1394 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1395 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1396
1397 // Merge the pieces into a single i64 value.
1398 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi).Val;
1399}
1400
1401
Evan Chenga8e29892007-01-19 07:51:42 +00001402SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1403 switch (Op.getOpcode()) {
1404 default: assert(0 && "Don't know how to custom lower this!"); abort();
1405 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001406 case ISD::GlobalAddress:
1407 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1408 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001409 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001410 case ISD::CALL: return LowerCALL(Op, DAG);
1411 case ISD::RET: return LowerRET(Op, DAG);
1412 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1413 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1414 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1415 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1416 case ISD::SINT_TO_FP:
1417 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1418 case ISD::FP_TO_SINT:
1419 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1420 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001421 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001422 case ISD::RETURNADDR: break;
1423 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001424 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Rafael Espindolae0703c82007-10-31 14:39:58 +00001425 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001426 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001427
1428
1429 // FIXME: Remove these when LegalizeDAGTypes lands.
1430 case ISD::BIT_CONVERT: return SDOperand(ExpandBIT_CONVERT(Op.Val, DAG), 0);
1431 case ISD::SRL:
1432 case ISD::SRA: return SDOperand(ExpandSRx(Op.Val, DAG,Subtarget),0);
Evan Chenga8e29892007-01-19 07:51:42 +00001433 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001434 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001435}
1436
Chris Lattner27a6c732007-11-24 07:07:01 +00001437
1438/// ExpandOperationResult - Provide custom lowering hooks for expanding
1439/// operations.
1440SDNode *ARMTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
1441 switch (N->getOpcode()) {
1442 default: assert(0 && "Don't know how to custom expand this!"); abort();
1443 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(N, DAG);
1444 case ISD::SRL:
1445 case ISD::SRA: return ExpandSRx(N, DAG, Subtarget);
1446 }
1447}
1448
1449
Evan Chenga8e29892007-01-19 07:51:42 +00001450//===----------------------------------------------------------------------===//
1451// ARM Scheduler Hooks
1452//===----------------------------------------------------------------------===//
1453
1454MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001455ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chenga8e29892007-01-19 07:51:42 +00001456 MachineBasicBlock *BB) {
1457 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1458 switch (MI->getOpcode()) {
1459 default: assert(false && "Unexpected instr type to insert");
1460 case ARM::tMOVCCr: {
1461 // To "insert" a SELECT_CC instruction, we actually have to insert the
1462 // diamond control-flow pattern. The incoming instruction knows the
1463 // destination vreg to set, the condition code register to branch on, the
1464 // true/false values to select between, and a branch opcode to use.
1465 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1466 ilist<MachineBasicBlock>::iterator It = BB;
1467 ++It;
1468
1469 // thisMBB:
1470 // ...
1471 // TrueVal = ...
1472 // cmpTY ccX, r1, r2
1473 // bCC copy1MBB
1474 // fallthrough --> copy0MBB
1475 MachineBasicBlock *thisMBB = BB;
1476 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1477 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1478 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001479 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001480 MachineFunction *F = BB->getParent();
1481 F->getBasicBlockList().insert(It, copy0MBB);
1482 F->getBasicBlockList().insert(It, sinkMBB);
1483 // Update machine-CFG edges by first adding all successors of the current
1484 // block to the new block which will contain the Phi node for the select.
1485 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1486 e = BB->succ_end(); i != e; ++i)
1487 sinkMBB->addSuccessor(*i);
1488 // Next, remove all successors of the current block, and add the true
1489 // and fallthrough blocks as its successors.
1490 while(!BB->succ_empty())
1491 BB->removeSuccessor(BB->succ_begin());
1492 BB->addSuccessor(copy0MBB);
1493 BB->addSuccessor(sinkMBB);
1494
1495 // copy0MBB:
1496 // %FalseValue = ...
1497 // # fallthrough to sinkMBB
1498 BB = copy0MBB;
1499
1500 // Update machine-CFG edges
1501 BB->addSuccessor(sinkMBB);
1502
1503 // sinkMBB:
1504 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1505 // ...
1506 BB = sinkMBB;
1507 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1508 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1509 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1510
1511 delete MI; // The pseudo instruction is gone now.
1512 return BB;
1513 }
1514 }
1515}
1516
1517//===----------------------------------------------------------------------===//
1518// ARM Optimization Hooks
1519//===----------------------------------------------------------------------===//
1520
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001521/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
1522static SDOperand PerformFMRRDCombine(SDNode *N,
1523 TargetLowering::DAGCombinerInfo &DCI) {
1524 // fmrrd(fmdrr x, y) -> x,y
1525 SDOperand InDouble = N->getOperand(0);
1526 if (InDouble.getOpcode() == ARMISD::FMDRR)
1527 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
1528 return SDOperand();
1529}
1530
1531SDOperand ARMTargetLowering::PerformDAGCombine(SDNode *N,
1532 DAGCombinerInfo &DCI) const {
1533 switch (N->getOpcode()) {
1534 default: break;
1535 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1536 }
1537
1538 return SDOperand();
1539}
1540
1541
Evan Chengb01fad62007-03-12 23:30:29 +00001542/// isLegalAddressImmediate - Return true if the integer value can be used
1543/// as the offset of the target addressing mode for load / store of the
1544/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001545static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1546 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001547 if (V == 0)
1548 return true;
1549
Evan Chengb01fad62007-03-12 23:30:29 +00001550 if (Subtarget->isThumb()) {
1551 if (V < 0)
1552 return false;
1553
1554 unsigned Scale = 1;
1555 switch (VT) {
1556 default: return false;
1557 case MVT::i1:
1558 case MVT::i8:
1559 // Scale == 1;
1560 break;
1561 case MVT::i16:
1562 // Scale == 2;
1563 Scale = 2;
1564 break;
1565 case MVT::i32:
1566 // Scale == 4;
1567 Scale = 4;
1568 break;
1569 }
1570
1571 if ((V & (Scale - 1)) != 0)
1572 return false;
1573 V /= Scale;
1574 return V == V & ((1LL << 5) - 1);
1575 }
1576
1577 if (V < 0)
1578 V = - V;
1579 switch (VT) {
1580 default: return false;
1581 case MVT::i1:
1582 case MVT::i8:
1583 case MVT::i32:
1584 // +- imm12
1585 return V == V & ((1LL << 12) - 1);
1586 case MVT::i16:
1587 // +- imm8
1588 return V == V & ((1LL << 8) - 1);
1589 case MVT::f32:
1590 case MVT::f64:
1591 if (!Subtarget->hasVFP2())
1592 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001593 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001594 return false;
1595 V >>= 2;
1596 return V == V & ((1LL << 8) - 1);
1597 }
Evan Chenga8e29892007-01-19 07:51:42 +00001598}
1599
Chris Lattner37caf8c2007-04-09 23:33:39 +00001600/// isLegalAddressingMode - Return true if the addressing mode represented
1601/// by AM is legal for this target, for a load/store of the specified type.
1602bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1603 const Type *Ty) const {
1604 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001605 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001606
1607 // Can never fold addr of global into load/store.
1608 if (AM.BaseGV)
1609 return false;
1610
1611 switch (AM.Scale) {
1612 case 0: // no scale reg, must be "r+i" or "r", or "i".
1613 break;
1614 case 1:
1615 if (Subtarget->isThumb())
1616 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001617 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001618 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001619 // ARM doesn't support any R+R*scale+imm addr modes.
1620 if (AM.BaseOffs)
1621 return false;
1622
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001623 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001624 switch (getValueType(Ty)) {
1625 default: return false;
1626 case MVT::i1:
1627 case MVT::i8:
1628 case MVT::i32:
1629 case MVT::i64:
1630 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1631 // ldrd / strd are used, then its address mode is same as i16.
1632 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001633 if (Scale < 0) Scale = -Scale;
1634 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001635 return true;
1636 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001637 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001638 case MVT::i16:
1639 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001640 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001641 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001642 return false;
1643
Chris Lattner37caf8c2007-04-09 23:33:39 +00001644 case MVT::isVoid:
1645 // Note, we allow "void" uses (basically, uses that aren't loads or
1646 // stores), because arm allows folding a scale into many arithmetic
1647 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001648
Chris Lattner37caf8c2007-04-09 23:33:39 +00001649 // Allow r << imm, but the imm has to be a multiple of two.
1650 if (AM.Scale & 1) return false;
1651 return isPowerOf2_32(AM.Scale);
1652 }
1653 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001654 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001655 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001656}
1657
Chris Lattner37caf8c2007-04-09 23:33:39 +00001658
Evan Chenga8e29892007-01-19 07:51:42 +00001659static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1660 bool isSEXTLoad, SDOperand &Base,
1661 SDOperand &Offset, bool &isInc,
1662 SelectionDAG &DAG) {
1663 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1664 return false;
1665
1666 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1667 // AddressingMode 3
1668 Base = Ptr->getOperand(0);
1669 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1670 int RHSC = (int)RHS->getValue();
1671 if (RHSC < 0 && RHSC > -256) {
1672 isInc = false;
1673 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1674 return true;
1675 }
1676 }
1677 isInc = (Ptr->getOpcode() == ISD::ADD);
1678 Offset = Ptr->getOperand(1);
1679 return true;
1680 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1681 // AddressingMode 2
1682 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1683 int RHSC = (int)RHS->getValue();
1684 if (RHSC < 0 && RHSC > -0x1000) {
1685 isInc = false;
1686 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1687 Base = Ptr->getOperand(0);
1688 return true;
1689 }
1690 }
1691
1692 if (Ptr->getOpcode() == ISD::ADD) {
1693 isInc = true;
1694 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1695 if (ShOpcVal != ARM_AM::no_shift) {
1696 Base = Ptr->getOperand(1);
1697 Offset = Ptr->getOperand(0);
1698 } else {
1699 Base = Ptr->getOperand(0);
1700 Offset = Ptr->getOperand(1);
1701 }
1702 return true;
1703 }
1704
1705 isInc = (Ptr->getOpcode() == ISD::ADD);
1706 Base = Ptr->getOperand(0);
1707 Offset = Ptr->getOperand(1);
1708 return true;
1709 }
1710
1711 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1712 return false;
1713}
1714
1715/// getPreIndexedAddressParts - returns true by value, base pointer and
1716/// offset pointer and addressing mode by reference if the node's address
1717/// can be legally represented as pre-indexed load / store address.
1718bool
1719ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1720 SDOperand &Offset,
1721 ISD::MemIndexedMode &AM,
1722 SelectionDAG &DAG) {
1723 if (Subtarget->isThumb())
1724 return false;
1725
1726 MVT::ValueType VT;
1727 SDOperand Ptr;
1728 bool isSEXTLoad = false;
1729 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1730 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001731 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001732 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1733 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1734 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001735 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001736 } else
1737 return false;
1738
1739 bool isInc;
1740 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1741 isInc, DAG);
1742 if (isLegal) {
1743 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1744 return true;
1745 }
1746 return false;
1747}
1748
1749/// getPostIndexedAddressParts - returns true by value, base pointer and
1750/// offset pointer and addressing mode by reference if this node can be
1751/// combined with a load / store to form a post-indexed load / store.
1752bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1753 SDOperand &Base,
1754 SDOperand &Offset,
1755 ISD::MemIndexedMode &AM,
1756 SelectionDAG &DAG) {
1757 if (Subtarget->isThumb())
1758 return false;
1759
1760 MVT::ValueType VT;
1761 SDOperand Ptr;
1762 bool isSEXTLoad = false;
1763 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001764 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001765 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1766 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001767 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001768 } else
1769 return false;
1770
1771 bool isInc;
1772 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1773 isInc, DAG);
1774 if (isLegal) {
1775 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1776 return true;
1777 }
1778 return false;
1779}
1780
1781void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1782 uint64_t Mask,
1783 uint64_t &KnownZero,
1784 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001785 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001786 unsigned Depth) const {
1787 KnownZero = 0;
1788 KnownOne = 0;
1789 switch (Op.getOpcode()) {
1790 default: break;
1791 case ARMISD::CMOV: {
1792 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001793 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001794 if (KnownZero == 0 && KnownOne == 0) return;
1795
1796 uint64_t KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1798 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001799 KnownZero &= KnownZeroRHS;
1800 KnownOne &= KnownOneRHS;
1801 return;
1802 }
1803 }
1804}
1805
1806//===----------------------------------------------------------------------===//
1807// ARM Inline Assembly Support
1808//===----------------------------------------------------------------------===//
1809
1810/// getConstraintType - Given a constraint letter, return the type of
1811/// constraint it is for this target.
1812ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001813ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1814 if (Constraint.size() == 1) {
1815 switch (Constraint[0]) {
1816 default: break;
1817 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001818 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001819 }
Evan Chenga8e29892007-01-19 07:51:42 +00001820 }
Chris Lattner4234f572007-03-25 02:14:49 +00001821 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001822}
1823
1824std::pair<unsigned, const TargetRegisterClass*>
1825ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1826 MVT::ValueType VT) const {
1827 if (Constraint.size() == 1) {
1828 // GCC RS6000 Constraint Letters
1829 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001830 case 'l':
1831 // FIXME: in thumb mode, 'l' is only low-regs.
1832 // FALL THROUGH.
1833 case 'r':
1834 return std::make_pair(0U, ARM::GPRRegisterClass);
1835 case 'w':
1836 if (VT == MVT::f32)
1837 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001838 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001839 return std::make_pair(0U, ARM::DPRRegisterClass);
1840 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001841 }
1842 }
1843 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1844}
1845
1846std::vector<unsigned> ARMTargetLowering::
1847getRegClassForInlineAsmConstraint(const std::string &Constraint,
1848 MVT::ValueType VT) const {
1849 if (Constraint.size() != 1)
1850 return std::vector<unsigned>();
1851
1852 switch (Constraint[0]) { // GCC ARM Constraint Letters
1853 default: break;
1854 case 'l':
1855 case 'r':
1856 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1857 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1858 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1859 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001860 case 'w':
1861 if (VT == MVT::f32)
1862 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1863 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1864 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1865 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1866 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1867 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1868 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1869 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1870 if (VT == MVT::f64)
1871 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1872 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1873 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1874 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1875 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001876 }
1877
1878 return std::vector<unsigned>();
1879}