blob: 7218560cc6cc8c143573a2a3b31d2105d1089d1c [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.
Nate Begeman48a65512008-02-04 21:44:06 +0000206 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000207 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);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000213 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000214
215 if (!Subtarget->hasV6Ops()) {
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
217 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
218 }
219 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
220
Evan Chengb6ab2542007-01-31 08:40:13 +0000221 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000222 // Turn f64->i64 into FMRRD iff target supports vfp2.
223 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000224
225 // We want to custom lower some of our intrinsics.
226 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
227
Evan Chenga8e29892007-01-19 07:51:42 +0000228 setOperationAction(ISD::SETCC , MVT::i32, Expand);
229 setOperationAction(ISD::SETCC , MVT::f32, Expand);
230 setOperationAction(ISD::SETCC , MVT::f64, Expand);
231 setOperationAction(ISD::SELECT , MVT::i32, Expand);
232 setOperationAction(ISD::SELECT , MVT::f32, Expand);
233 setOperationAction(ISD::SELECT , MVT::f64, Expand);
234 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
235 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
236 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
237
238 setOperationAction(ISD::BRCOND , MVT::Other, Expand);
239 setOperationAction(ISD::BR_CC , MVT::i32, Custom);
240 setOperationAction(ISD::BR_CC , MVT::f32, Custom);
241 setOperationAction(ISD::BR_CC , MVT::f64, Custom);
242 setOperationAction(ISD::BR_JT , MVT::Other, Custom);
243
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000244 // We don't support sin/cos/fmod/copysign/pow
Evan Chenga8e29892007-01-19 07:51:42 +0000245 setOperationAction(ISD::FSIN , MVT::f64, Expand);
246 setOperationAction(ISD::FSIN , MVT::f32, Expand);
247 setOperationAction(ISD::FCOS , MVT::f32, Expand);
248 setOperationAction(ISD::FCOS , MVT::f64, Expand);
249 setOperationAction(ISD::FREM , MVT::f64, Expand);
250 setOperationAction(ISD::FREM , MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000251 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
252 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
253 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
254 }
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000255 setOperationAction(ISD::FPOW , MVT::f64, Expand);
256 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000257
258 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000259 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
260 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
261 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
262 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
263 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
264 }
Evan Chenga8e29892007-01-19 07:51:42 +0000265
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000266 // We have target-specific dag combine patterns for the following nodes:
267 // ARMISD::FMRRD - No need to call setTargetDAGCombine
268
Evan Chenga8e29892007-01-19 07:51:42 +0000269 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000270 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000271 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000272 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000273
274 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000275}
276
277
278const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
279 switch (Opcode) {
280 default: return 0;
281 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000282 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
283 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000284 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000285 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
286 case ARMISD::tCALL: return "ARMISD::tCALL";
287 case ARMISD::BRCOND: return "ARMISD::BRCOND";
288 case ARMISD::BR_JT: return "ARMISD::BR_JT";
289 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
290 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
291 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000292 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000293 case ARMISD::CMPFP: return "ARMISD::CMPFP";
294 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
295 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
296 case ARMISD::CMOV: return "ARMISD::CMOV";
297 case ARMISD::CNEG: return "ARMISD::CNEG";
298
299 case ARMISD::FTOSI: return "ARMISD::FTOSI";
300 case ARMISD::FTOUI: return "ARMISD::FTOUI";
301 case ARMISD::SITOF: return "ARMISD::SITOF";
302 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000303
304 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
305 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
306 case ARMISD::RRX: return "ARMISD::RRX";
307
308 case ARMISD::FMRRD: return "ARMISD::FMRRD";
309 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000310
311 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000312 }
313}
314
315//===----------------------------------------------------------------------===//
316// Lowering Code
317//===----------------------------------------------------------------------===//
318
319
320/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
321static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
322 switch (CC) {
323 default: assert(0 && "Unknown condition code!");
324 case ISD::SETNE: return ARMCC::NE;
325 case ISD::SETEQ: return ARMCC::EQ;
326 case ISD::SETGT: return ARMCC::GT;
327 case ISD::SETGE: return ARMCC::GE;
328 case ISD::SETLT: return ARMCC::LT;
329 case ISD::SETLE: return ARMCC::LE;
330 case ISD::SETUGT: return ARMCC::HI;
331 case ISD::SETUGE: return ARMCC::HS;
332 case ISD::SETULT: return ARMCC::LO;
333 case ISD::SETULE: return ARMCC::LS;
334 }
335}
336
337/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
338/// returns true if the operands should be inverted to form the proper
339/// comparison.
340static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
341 ARMCC::CondCodes &CondCode2) {
342 bool Invert = false;
343 CondCode2 = ARMCC::AL;
344 switch (CC) {
345 default: assert(0 && "Unknown FP condition!");
346 case ISD::SETEQ:
347 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
348 case ISD::SETGT:
349 case ISD::SETOGT: CondCode = ARMCC::GT; break;
350 case ISD::SETGE:
351 case ISD::SETOGE: CondCode = ARMCC::GE; break;
352 case ISD::SETOLT: CondCode = ARMCC::MI; break;
353 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
354 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
355 case ISD::SETO: CondCode = ARMCC::VC; break;
356 case ISD::SETUO: CondCode = ARMCC::VS; break;
357 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
358 case ISD::SETUGT: CondCode = ARMCC::HI; break;
359 case ISD::SETUGE: CondCode = ARMCC::PL; break;
360 case ISD::SETLT:
361 case ISD::SETULT: CondCode = ARMCC::LT; break;
362 case ISD::SETLE:
363 case ISD::SETULE: CondCode = ARMCC::LE; break;
364 case ISD::SETNE:
365 case ISD::SETUNE: CondCode = ARMCC::NE; break;
366 }
367 return Invert;
368}
369
370static void
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000371HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs,
372 unsigned StackOffset, unsigned &NeededGPRs,
373 unsigned &NeededStackSize, unsigned &GPRPad,
Duncan Sands276dcbd2008-03-21 09:14:45 +0000374 unsigned &StackPad, ISD::ArgFlagsTy Flags) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000375 NeededStackSize = 0;
376 NeededGPRs = 0;
377 StackPad = 0;
378 GPRPad = 0;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000379 unsigned align = Flags.getOrigAlign();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000380 GPRPad = NumGPRs % ((align + 3)/4);
381 StackPad = StackOffset % align;
382 unsigned firstGPR = NumGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000383 switch (ObjectVT) {
384 default: assert(0 && "Unhandled argument type!");
385 case MVT::i32:
386 case MVT::f32:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000387 if (firstGPR < 4)
388 NeededGPRs = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000389 else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000390 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000391 break;
392 case MVT::i64:
393 case MVT::f64:
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000394 if (firstGPR < 3)
395 NeededGPRs = 2;
396 else if (firstGPR == 3) {
397 NeededGPRs = 1;
398 NeededStackSize = 4;
Evan Chenga8e29892007-01-19 07:51:42 +0000399 } else
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000400 NeededStackSize = 8;
Evan Chenga8e29892007-01-19 07:51:42 +0000401 }
402}
403
Evan Chengfc403422007-02-03 08:53:01 +0000404/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
405/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
406/// nodes.
Evan Chenga8e29892007-01-19 07:51:42 +0000407SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
408 MVT::ValueType RetVT= Op.Val->getValueType(0);
409 SDOperand Chain = Op.getOperand(0);
410 unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
411 assert((CallConv == CallingConv::C ||
Evan Chenga8e29892007-01-19 07:51:42 +0000412 CallConv == CallingConv::Fast) && "unknown calling convention");
413 SDOperand Callee = Op.getOperand(4);
414 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
415 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
416 unsigned NumGPRs = 0; // GPRs used for parameter passing.
417
418 // Count how many bytes are to be pushed on the stack.
419 unsigned NumBytes = 0;
420
421 // Add up all the space actually used.
422 for (unsigned i = 0; i < NumOps; ++i) {
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000423 unsigned ObjSize;
424 unsigned ObjGPRs;
425 unsigned StackPad;
426 unsigned GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000427 MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType();
Duncan Sands276dcbd2008-03-21 09:14:45 +0000428 ISD::ArgFlagsTy Flags =
429 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000430 HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
431 GPRPad, StackPad, Flags);
432 NumBytes += ObjSize + StackPad;
433 NumGPRs += ObjGPRs + GPRPad;
Evan Chenga8e29892007-01-19 07:51:42 +0000434 }
435
436 // Adjust the stack pointer for the new arguments...
437 // These operations are automatically eliminated by the prolog/epilog pass
438 Chain = DAG.getCALLSEQ_START(Chain,
439 DAG.getConstant(NumBytes, MVT::i32));
440
441 SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
442
443 static const unsigned GPRArgRegs[] = {
444 ARM::R0, ARM::R1, ARM::R2, ARM::R3
445 };
446
447 NumGPRs = 0;
448 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
449 std::vector<SDOperand> MemOpChains;
450 for (unsigned i = 0; i != NumOps; ++i) {
451 SDOperand Arg = Op.getOperand(5+2*i);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000452 ISD::ArgFlagsTy Flags =
453 cast<ARG_FLAGSSDNode>(Op.getOperand(5+2*i+1))->getArgFlags();
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 =
Duncan Sands00fee652008-02-14 17:28:50 +0000752 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000753 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);
Dan Gohman69de1932008-02-06 22:27:42 +0000911 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
912 return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +0000913}
914
915static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG,
Nate Begemanbf1caa92008-02-12 22:54:40 +0000916 unsigned ArgNo, unsigned &NumGPRs,
917 unsigned &ArgOffset) {
Evan Chenga8e29892007-01-19 07:51:42 +0000918 MachineFunction &MF = DAG.getMachineFunction();
919 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
920 SDOperand Root = Op.getOperand(0);
921 std::vector<SDOperand> ArgValues;
Chris Lattner84bc5422007-12-31 04:13:23 +0000922 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000923
924 static const unsigned GPRArgRegs[] = {
925 ARM::R0, ARM::R1, ARM::R2, ARM::R3
926 };
927
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000928 unsigned ObjSize;
929 unsigned ObjGPRs;
930 unsigned GPRPad;
931 unsigned StackPad;
Duncan Sands276dcbd2008-03-21 09:14:45 +0000932 ISD::ArgFlagsTy Flags =
933 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo + 3))->getArgFlags();
Lauro Ramos Venancio876eaf12007-02-13 14:07:13 +0000934 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 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
944 if (ObjectVT == MVT::f32)
945 ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue);
946 } else if (ObjGPRs == 2) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000947 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
948 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000949 ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32);
950
Chris Lattner84bc5422007-12-31 04:13:23 +0000951 VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
952 RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +0000953 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32);
954
Chris Lattner27a6c732007-11-24 07:07:01 +0000955 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
956 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000957 }
958 NumGPRs += ObjGPRs;
959
960 if (ObjSize) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +0000961 MachineFrameInfo *MFI = MF.getFrameInfo();
962 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
963 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
964 if (ObjGPRs == 0)
965 ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
966 else {
967 SDOperand ArgValue2 = DAG.getLoad(MVT::i32, Root, FIN, NULL, 0);
968 assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
969 ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2);
Evan Chenga8e29892007-01-19 07:51:42 +0000970 }
971
972 ArgOffset += ObjSize; // Move on to the next argument.
973 }
974
975 return ArgValue;
976}
977
978SDOperand
979ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
980 std::vector<SDOperand> ArgValues;
981 SDOperand Root = Op.getOperand(0);
982 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
983 unsigned NumGPRs = 0; // GPRs used for parameter passing.
Evan Chenga8e29892007-01-19 07:51:42 +0000984
985 unsigned NumArgs = Op.Val->getNumValues()-1;
986 for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
Nate Begemanbf1caa92008-02-12 22:54:40 +0000987 ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, ArgNo,
Evan Chenga8e29892007-01-19 07:51:42 +0000988 NumGPRs, ArgOffset));
989
990 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
991 if (isVarArg) {
992 static const unsigned GPRArgRegs[] = {
993 ARM::R0, ARM::R1, ARM::R2, ARM::R3
994 };
995
996 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +0000997 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000998 MachineFrameInfo *MFI = MF.getFrameInfo();
999 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001000 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1001 unsigned VARegSize = (4 - NumGPRs) * 4;
1002 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Evan Chenga8e29892007-01-19 07:51:42 +00001003 if (VARegSaveSize) {
1004 // If this function is vararg, store any remaining integer argument regs
1005 // to their spots on the stack so that they may be loaded by deferencing
1006 // the result of va_next.
1007 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001008 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1009 VARegSaveSize - VARegSize);
Evan Chenga8e29892007-01-19 07:51:42 +00001010 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1011
1012 SmallVector<SDOperand, 4> MemOps;
1013 for (; NumGPRs < 4; ++NumGPRs) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001014 unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1015 RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
Evan Chenga8e29892007-01-19 07:51:42 +00001016 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32);
1017 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1018 MemOps.push_back(Store);
1019 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1020 DAG.getConstant(4, getPointerTy()));
1021 }
1022 if (!MemOps.empty())
1023 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1024 &MemOps[0], MemOps.size());
1025 } else
1026 // This will point to the next argument passed via stack.
1027 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1028 }
1029
1030 ArgValues.push_back(Root);
1031
1032 // Return the new list of results.
1033 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1034 Op.Val->value_end());
1035 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1036}
1037
1038/// isFloatingPointZero - Return true if this is +0.0.
1039static bool isFloatingPointZero(SDOperand Op) {
1040 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001041 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001042 else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) {
1043 // Maybe this has already been legalized into the constant pool?
1044 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1045 SDOperand WrapperOp = Op.getOperand(1).getOperand(0);
1046 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1047 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001048 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001049 }
1050 }
1051 return false;
1052}
1053
Evan Cheng9a2ef952007-02-02 01:53:26 +00001054static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001055 return ( isThumb && (C & ~255U) == 0) ||
1056 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1057}
1058
1059/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1060/// the given operands.
1061static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC,
1062 SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) {
1063 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001064 unsigned C = RHSC->getValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001065 if (!isLegalCmpImmediate(C, isThumb)) {
1066 // Constant does not fit, try adjusting it by one?
1067 switch (CC) {
1068 default: break;
1069 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001070 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001071 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001072 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1073 RHS = DAG.getConstant(C-1, MVT::i32);
1074 }
1075 break;
1076 case ISD::SETULT:
1077 case ISD::SETUGE:
1078 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1079 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001080 RHS = DAG.getConstant(C-1, MVT::i32);
1081 }
1082 break;
1083 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001084 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001085 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001086 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1087 RHS = DAG.getConstant(C+1, MVT::i32);
1088 }
1089 break;
1090 case ISD::SETULE:
1091 case ISD::SETUGT:
1092 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1093 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001094 RHS = DAG.getConstant(C+1, MVT::i32);
1095 }
1096 break;
1097 }
1098 }
1099 }
1100
1101 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001102 ARMISD::NodeType CompareType;
1103 switch (CondCode) {
1104 default:
1105 CompareType = ARMISD::CMP;
1106 break;
1107 case ARMCC::EQ:
1108 case ARMCC::NE:
1109 case ARMCC::MI:
1110 case ARMCC::PL:
1111 // Uses only N and Z Flags
1112 CompareType = ARMISD::CMPNZ;
1113 break;
1114 }
Evan Chenga8e29892007-01-19 07:51:42 +00001115 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001116 return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001117}
1118
1119/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1120static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) {
1121 SDOperand Cmp;
1122 if (!isFloatingPointZero(RHS))
1123 Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1124 else
1125 Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1126 return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1127}
1128
1129static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG,
1130 const ARMSubtarget *ST) {
1131 MVT::ValueType VT = Op.getValueType();
1132 SDOperand LHS = Op.getOperand(0);
1133 SDOperand RHS = Op.getOperand(1);
1134 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1135 SDOperand TrueVal = Op.getOperand(2);
1136 SDOperand FalseVal = Op.getOperand(3);
1137
1138 if (LHS.getValueType() == MVT::i32) {
1139 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001140 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001141 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001142 return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001143 }
1144
1145 ARMCC::CondCodes CondCode, CondCode2;
1146 if (FPCCToARMCC(CC, CondCode, CondCode2))
1147 std::swap(TrueVal, FalseVal);
1148
1149 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001150 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001151 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1152 SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001153 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001154 if (CondCode2 != ARMCC::AL) {
1155 SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1156 // FIXME: Needs another CMP because flag can have but one use.
1157 SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001158 Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001159 }
1160 return Result;
1161}
1162
1163static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG,
1164 const ARMSubtarget *ST) {
1165 SDOperand Chain = Op.getOperand(0);
1166 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1167 SDOperand LHS = Op.getOperand(2);
1168 SDOperand RHS = Op.getOperand(3);
1169 SDOperand Dest = Op.getOperand(4);
1170
1171 if (LHS.getValueType() == MVT::i32) {
1172 SDOperand ARMCC;
Evan Cheng0e1d3792007-07-05 07:18:20 +00001173 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001174 SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
Evan Cheng0e1d3792007-07-05 07:18:20 +00001175 return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001176 }
1177
1178 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1179 ARMCC::CondCodes CondCode, CondCode2;
1180 if (FPCCToARMCC(CC, CondCode, CondCode2))
1181 // Swap the LHS/RHS of the comparison if needed.
1182 std::swap(LHS, RHS);
1183
1184 SDOperand Cmp = getVFPCmp(LHS, RHS, DAG);
1185 SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001186 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001187 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001188 SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1189 SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001190 if (CondCode2 != ARMCC::AL) {
1191 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001192 SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1193 Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001194 }
1195 return Res;
1196}
1197
1198SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) {
1199 SDOperand Chain = Op.getOperand(0);
1200 SDOperand Table = Op.getOperand(1);
1201 SDOperand Index = Op.getOperand(2);
1202
1203 MVT::ValueType PTy = getPointerTy();
1204 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1205 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1206 SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1207 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1208 Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1209 Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy));
1210 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1211 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Evan Chenge2446c62007-06-26 18:31:22 +00001212 Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy,
1213 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001214 Chain = Addr.getValue(1);
1215 if (isPIC)
1216 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table);
1217 return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId);
1218}
1219
1220static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) {
1221 unsigned Opc =
1222 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1223 Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1224 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1225}
1226
1227static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
1228 MVT::ValueType VT = Op.getValueType();
1229 unsigned Opc =
1230 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1231
1232 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1233 return DAG.getNode(Opc, VT, Op);
1234}
1235
1236static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
1237 // Implement fcopysign with a fabs and a conditional fneg.
1238 SDOperand Tmp0 = Op.getOperand(0);
1239 SDOperand Tmp1 = Op.getOperand(1);
1240 MVT::ValueType VT = Op.getValueType();
1241 MVT::ValueType SrcVT = Tmp1.getValueType();
1242 SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1243 SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1244 SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001245 SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1246 return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001247}
1248
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001249SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain,
1250 SDOperand Dest,
1251 SDOperand Source,
1252 unsigned Size,
1253 unsigned Align,
1254 SelectionDAG &DAG) {
Evan Cheng4102eb52007-10-22 22:11:27 +00001255 // Do repeated 4-byte loads and stores. To be improved.
1256 assert((Align & 3) == 0 && "Expected 4-byte aligned addresses!");
1257 unsigned BytesLeft = Size & 3;
Rafael Espindola7b73a5d2007-10-19 14:35:17 +00001258 unsigned NumMemOps = Size >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001259 unsigned EmittedNumMemOps = 0;
1260 unsigned SrcOff = 0, DstOff = 0;
1261 MVT::ValueType VT = MVT::i32;
1262 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001263 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001264 const unsigned MAX_LOADS_IN_LDM = 6;
Evan Cheng4102eb52007-10-22 22:11:27 +00001265 SDOperand TFOps[MAX_LOADS_IN_LDM];
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001266 SDOperand Loads[MAX_LOADS_IN_LDM];
1267
Evan Cheng4102eb52007-10-22 22:11:27 +00001268 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1269 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001270 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001271 while (EmittedNumMemOps < NumMemOps) {
1272 for (i = 0;
1273 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001274 Loads[i] = DAG.getLoad(VT, Chain,
Evan Cheng4102eb52007-10-22 22:11:27 +00001275 DAG.getNode(ISD::ADD, MVT::i32, Source,
1276 DAG.getConstant(SrcOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001277 NULL, 0);
Evan Cheng4102eb52007-10-22 22:11:27 +00001278 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001279 SrcOff += VTSize;
1280 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001281 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001282
Evan Cheng4102eb52007-10-22 22:11:27 +00001283 for (i = 0;
1284 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1285 TFOps[i] = DAG.getStore(Chain, Loads[i],
1286 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1287 DAG.getConstant(DstOff, MVT::i32)),
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001288 NULL, 0);
1289 DstOff += VTSize;
1290 }
Evan Cheng4102eb52007-10-22 22:11:27 +00001291 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1292
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001293 EmittedNumMemOps += i;
1294 }
1295
Evan Cheng4102eb52007-10-22 22:11:27 +00001296 if (BytesLeft == 0)
1297 return Chain;
1298
1299 // Issue loads / stores for the trailing (1 - 3) bytes.
1300 unsigned BytesLeftSave = BytesLeft;
1301 i = 0;
1302 while (BytesLeft) {
1303 if (BytesLeft >= 2) {
1304 VT = MVT::i16;
1305 VTSize = 2;
1306 } else {
1307 VT = MVT::i8;
1308 VTSize = 1;
1309 }
1310
1311 Loads[i] = DAG.getLoad(VT, Chain,
1312 DAG.getNode(ISD::ADD, MVT::i32, Source,
1313 DAG.getConstant(SrcOff, MVT::i32)),
1314 NULL, 0);
1315 TFOps[i] = Loads[i].getValue(1);
1316 ++i;
1317 SrcOff += VTSize;
1318 BytesLeft -= VTSize;
1319 }
1320 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
1321
1322 i = 0;
1323 BytesLeft = BytesLeftSave;
1324 while (BytesLeft) {
1325 if (BytesLeft >= 2) {
1326 VT = MVT::i16;
1327 VTSize = 2;
1328 } else {
1329 VT = MVT::i8;
1330 VTSize = 1;
1331 }
1332
1333 TFOps[i] = DAG.getStore(Chain, Loads[i],
1334 DAG.getNode(ISD::ADD, MVT::i32, Dest,
1335 DAG.getConstant(DstOff, MVT::i32)),
1336 NULL, 0);
1337 ++i;
1338 DstOff += VTSize;
1339 BytesLeft -= VTSize;
1340 }
1341 return DAG.getNode(ISD::TokenFactor, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001342}
1343
Chris Lattner27a6c732007-11-24 07:07:01 +00001344static SDNode *ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1345 // Turn f64->i64 into FMRRD.
1346 assert(N->getValueType(0) == MVT::i64 &&
1347 N->getOperand(0).getValueType() == MVT::f64);
1348
1349 SDOperand Op = N->getOperand(0);
1350 SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1351 &Op, 1);
1352
1353 // Merge the pieces into a single i64 value.
1354 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)).Val;
1355}
1356
1357static SDNode *ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1358 assert(N->getValueType(0) == MVT::i64 &&
1359 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1360 "Unknown shift to lower!");
1361
1362 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1363 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1364 cast<ConstantSDNode>(N->getOperand(1))->getValue() != 1)
1365 return 0;
1366
1367 // If we are in thumb mode, we don't have RRX.
1368 if (ST->isThumb()) return 0;
1369
1370 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
1371 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1372 DAG.getConstant(0, MVT::i32));
1373 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1374 DAG.getConstant(1, MVT::i32));
1375
1376 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1377 // captures the result into a carry flag.
1378 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1379 Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1380
1381 // The low part is an ARMISD::RRX operand, which shifts the carry in.
1382 Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1383
1384 // Merge the pieces into a single i64 value.
1385 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi).Val;
1386}
1387
1388
Evan Chenga8e29892007-01-19 07:51:42 +00001389SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1390 switch (Op.getOpcode()) {
1391 default: assert(0 && "Don't know how to custom lower this!"); abort();
1392 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001393 case ISD::GlobalAddress:
1394 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1395 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001396 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001397 case ISD::CALL: return LowerCALL(Op, DAG);
1398 case ISD::RET: return LowerRET(Op, DAG);
1399 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1400 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1401 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1402 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1403 case ISD::SINT_TO_FP:
1404 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1405 case ISD::FP_TO_SINT:
1406 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1407 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001408 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001409 case ISD::RETURNADDR: break;
1410 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001411 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Rafael Espindolae0703c82007-10-31 14:39:58 +00001412 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001413 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001414
1415
1416 // FIXME: Remove these when LegalizeDAGTypes lands.
1417 case ISD::BIT_CONVERT: return SDOperand(ExpandBIT_CONVERT(Op.Val, DAG), 0);
1418 case ISD::SRL:
1419 case ISD::SRA: return SDOperand(ExpandSRx(Op.Val, DAG,Subtarget),0);
Evan Chenga8e29892007-01-19 07:51:42 +00001420 }
Nate Begemanbcc5f362007-01-29 22:58:52 +00001421 return SDOperand();
Evan Chenga8e29892007-01-19 07:51:42 +00001422}
1423
Chris Lattner27a6c732007-11-24 07:07:01 +00001424
1425/// ExpandOperationResult - Provide custom lowering hooks for expanding
1426/// operations.
1427SDNode *ARMTargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
1428 switch (N->getOpcode()) {
1429 default: assert(0 && "Don't know how to custom expand this!"); abort();
1430 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(N, DAG);
1431 case ISD::SRL:
1432 case ISD::SRA: return ExpandSRx(N, DAG, Subtarget);
1433 }
1434}
1435
1436
Evan Chenga8e29892007-01-19 07:51:42 +00001437//===----------------------------------------------------------------------===//
1438// ARM Scheduler Hooks
1439//===----------------------------------------------------------------------===//
1440
1441MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001442ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chenga8e29892007-01-19 07:51:42 +00001443 MachineBasicBlock *BB) {
1444 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1445 switch (MI->getOpcode()) {
1446 default: assert(false && "Unexpected instr type to insert");
1447 case ARM::tMOVCCr: {
1448 // To "insert" a SELECT_CC instruction, we actually have to insert the
1449 // diamond control-flow pattern. The incoming instruction knows the
1450 // destination vreg to set, the condition code register to branch on, the
1451 // true/false values to select between, and a branch opcode to use.
1452 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1453 ilist<MachineBasicBlock>::iterator It = BB;
1454 ++It;
1455
1456 // thisMBB:
1457 // ...
1458 // TrueVal = ...
1459 // cmpTY ccX, r1, r2
1460 // bCC copy1MBB
1461 // fallthrough --> copy0MBB
1462 MachineBasicBlock *thisMBB = BB;
1463 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1464 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1465 BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001466 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001467 MachineFunction *F = BB->getParent();
1468 F->getBasicBlockList().insert(It, copy0MBB);
1469 F->getBasicBlockList().insert(It, sinkMBB);
1470 // Update machine-CFG edges by first adding all successors of the current
1471 // block to the new block which will contain the Phi node for the select.
1472 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1473 e = BB->succ_end(); i != e; ++i)
1474 sinkMBB->addSuccessor(*i);
1475 // Next, remove all successors of the current block, and add the true
1476 // and fallthrough blocks as its successors.
1477 while(!BB->succ_empty())
1478 BB->removeSuccessor(BB->succ_begin());
1479 BB->addSuccessor(copy0MBB);
1480 BB->addSuccessor(sinkMBB);
1481
1482 // copy0MBB:
1483 // %FalseValue = ...
1484 // # fallthrough to sinkMBB
1485 BB = copy0MBB;
1486
1487 // Update machine-CFG edges
1488 BB->addSuccessor(sinkMBB);
1489
1490 // sinkMBB:
1491 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1492 // ...
1493 BB = sinkMBB;
1494 BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1495 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1496 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1497
1498 delete MI; // The pseudo instruction is gone now.
1499 return BB;
1500 }
1501 }
1502}
1503
1504//===----------------------------------------------------------------------===//
1505// ARM Optimization Hooks
1506//===----------------------------------------------------------------------===//
1507
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001508/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
1509static SDOperand PerformFMRRDCombine(SDNode *N,
1510 TargetLowering::DAGCombinerInfo &DCI) {
1511 // fmrrd(fmdrr x, y) -> x,y
1512 SDOperand InDouble = N->getOperand(0);
1513 if (InDouble.getOpcode() == ARMISD::FMDRR)
1514 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
1515 return SDOperand();
1516}
1517
1518SDOperand ARMTargetLowering::PerformDAGCombine(SDNode *N,
1519 DAGCombinerInfo &DCI) const {
1520 switch (N->getOpcode()) {
1521 default: break;
1522 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1523 }
1524
1525 return SDOperand();
1526}
1527
1528
Evan Chengb01fad62007-03-12 23:30:29 +00001529/// isLegalAddressImmediate - Return true if the integer value can be used
1530/// as the offset of the target addressing mode for load / store of the
1531/// given type.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001532static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT,
1533 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001534 if (V == 0)
1535 return true;
1536
Evan Chengb01fad62007-03-12 23:30:29 +00001537 if (Subtarget->isThumb()) {
1538 if (V < 0)
1539 return false;
1540
1541 unsigned Scale = 1;
1542 switch (VT) {
1543 default: return false;
1544 case MVT::i1:
1545 case MVT::i8:
1546 // Scale == 1;
1547 break;
1548 case MVT::i16:
1549 // Scale == 2;
1550 Scale = 2;
1551 break;
1552 case MVT::i32:
1553 // Scale == 4;
1554 Scale = 4;
1555 break;
1556 }
1557
1558 if ((V & (Scale - 1)) != 0)
1559 return false;
1560 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001561 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001562 }
1563
1564 if (V < 0)
1565 V = - V;
1566 switch (VT) {
1567 default: return false;
1568 case MVT::i1:
1569 case MVT::i8:
1570 case MVT::i32:
1571 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001572 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001573 case MVT::i16:
1574 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001575 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001576 case MVT::f32:
1577 case MVT::f64:
1578 if (!Subtarget->hasVFP2())
1579 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001580 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001581 return false;
1582 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001583 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001584 }
Evan Chenga8e29892007-01-19 07:51:42 +00001585}
1586
Chris Lattner37caf8c2007-04-09 23:33:39 +00001587/// isLegalAddressingMode - Return true if the addressing mode represented
1588/// by AM is legal for this target, for a load/store of the specified type.
1589bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1590 const Type *Ty) const {
1591 if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001592 return false;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001593
1594 // Can never fold addr of global into load/store.
1595 if (AM.BaseGV)
1596 return false;
1597
1598 switch (AM.Scale) {
1599 case 0: // no scale reg, must be "r+i" or "r", or "i".
1600 break;
1601 case 1:
1602 if (Subtarget->isThumb())
1603 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001604 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001605 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001606 // ARM doesn't support any R+R*scale+imm addr modes.
1607 if (AM.BaseOffs)
1608 return false;
1609
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001610 int Scale = AM.Scale;
Chris Lattner37caf8c2007-04-09 23:33:39 +00001611 switch (getValueType(Ty)) {
1612 default: return false;
1613 case MVT::i1:
1614 case MVT::i8:
1615 case MVT::i32:
1616 case MVT::i64:
1617 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1618 // ldrd / strd are used, then its address mode is same as i16.
1619 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001620 if (Scale < 0) Scale = -Scale;
1621 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001622 return true;
1623 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001624 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001625 case MVT::i16:
1626 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001627 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001628 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001629 return false;
1630
Chris Lattner37caf8c2007-04-09 23:33:39 +00001631 case MVT::isVoid:
1632 // Note, we allow "void" uses (basically, uses that aren't loads or
1633 // stores), because arm allows folding a scale into many arithmetic
1634 // operations. This should be made more precise and revisited later.
Chris Lattnerb2c594f2007-04-03 00:13:57 +00001635
Chris Lattner37caf8c2007-04-09 23:33:39 +00001636 // Allow r << imm, but the imm has to be a multiple of two.
1637 if (AM.Scale & 1) return false;
1638 return isPowerOf2_32(AM.Scale);
1639 }
1640 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001641 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001642 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001643}
1644
Chris Lattner37caf8c2007-04-09 23:33:39 +00001645
Evan Chenga8e29892007-01-19 07:51:42 +00001646static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT,
1647 bool isSEXTLoad, SDOperand &Base,
1648 SDOperand &Offset, bool &isInc,
1649 SelectionDAG &DAG) {
1650 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1651 return false;
1652
1653 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1654 // AddressingMode 3
1655 Base = Ptr->getOperand(0);
1656 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1657 int RHSC = (int)RHS->getValue();
1658 if (RHSC < 0 && RHSC > -256) {
1659 isInc = false;
1660 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1661 return true;
1662 }
1663 }
1664 isInc = (Ptr->getOpcode() == ISD::ADD);
1665 Offset = Ptr->getOperand(1);
1666 return true;
1667 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1668 // AddressingMode 2
1669 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1670 int RHSC = (int)RHS->getValue();
1671 if (RHSC < 0 && RHSC > -0x1000) {
1672 isInc = false;
1673 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1674 Base = Ptr->getOperand(0);
1675 return true;
1676 }
1677 }
1678
1679 if (Ptr->getOpcode() == ISD::ADD) {
1680 isInc = true;
1681 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1682 if (ShOpcVal != ARM_AM::no_shift) {
1683 Base = Ptr->getOperand(1);
1684 Offset = Ptr->getOperand(0);
1685 } else {
1686 Base = Ptr->getOperand(0);
1687 Offset = Ptr->getOperand(1);
1688 }
1689 return true;
1690 }
1691
1692 isInc = (Ptr->getOpcode() == ISD::ADD);
1693 Base = Ptr->getOperand(0);
1694 Offset = Ptr->getOperand(1);
1695 return true;
1696 }
1697
1698 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1699 return false;
1700}
1701
1702/// getPreIndexedAddressParts - returns true by value, base pointer and
1703/// offset pointer and addressing mode by reference if the node's address
1704/// can be legally represented as pre-indexed load / store address.
1705bool
1706ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
1707 SDOperand &Offset,
1708 ISD::MemIndexedMode &AM,
1709 SelectionDAG &DAG) {
1710 if (Subtarget->isThumb())
1711 return false;
1712
1713 MVT::ValueType VT;
1714 SDOperand Ptr;
1715 bool isSEXTLoad = false;
1716 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1717 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001718 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001719 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1720 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1721 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001722 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001723 } else
1724 return false;
1725
1726 bool isInc;
1727 bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset,
1728 isInc, DAG);
1729 if (isLegal) {
1730 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1731 return true;
1732 }
1733 return false;
1734}
1735
1736/// getPostIndexedAddressParts - returns true by value, base pointer and
1737/// offset pointer and addressing mode by reference if this node can be
1738/// combined with a load / store to form a post-indexed load / store.
1739bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1740 SDOperand &Base,
1741 SDOperand &Offset,
1742 ISD::MemIndexedMode &AM,
1743 SelectionDAG &DAG) {
1744 if (Subtarget->isThumb())
1745 return false;
1746
1747 MVT::ValueType VT;
1748 SDOperand Ptr;
1749 bool isSEXTLoad = false;
1750 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001751 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001752 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1753 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001754 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00001755 } else
1756 return false;
1757
1758 bool isInc;
1759 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1760 isInc, DAG);
1761 if (isLegal) {
1762 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1763 return true;
1764 }
1765 return false;
1766}
1767
1768void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00001769 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001770 APInt &KnownZero,
1771 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00001773 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001774 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001775 switch (Op.getOpcode()) {
1776 default: break;
1777 case ARMISD::CMOV: {
1778 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00001779 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001780 if (KnownZero == 0 && KnownOne == 0) return;
1781
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001782 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00001783 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1784 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00001785 KnownZero &= KnownZeroRHS;
1786 KnownOne &= KnownOneRHS;
1787 return;
1788 }
1789 }
1790}
1791
1792//===----------------------------------------------------------------------===//
1793// ARM Inline Assembly Support
1794//===----------------------------------------------------------------------===//
1795
1796/// getConstraintType - Given a constraint letter, return the type of
1797/// constraint it is for this target.
1798ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00001799ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1800 if (Constraint.size() == 1) {
1801 switch (Constraint[0]) {
1802 default: break;
1803 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001804 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00001805 }
Evan Chenga8e29892007-01-19 07:51:42 +00001806 }
Chris Lattner4234f572007-03-25 02:14:49 +00001807 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00001808}
1809
1810std::pair<unsigned, const TargetRegisterClass*>
1811ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1812 MVT::ValueType VT) const {
1813 if (Constraint.size() == 1) {
1814 // GCC RS6000 Constraint Letters
1815 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001816 case 'l':
1817 // FIXME: in thumb mode, 'l' is only low-regs.
1818 // FALL THROUGH.
1819 case 'r':
1820 return std::make_pair(0U, ARM::GPRRegisterClass);
1821 case 'w':
1822 if (VT == MVT::f32)
1823 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00001824 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001825 return std::make_pair(0U, ARM::DPRRegisterClass);
1826 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001827 }
1828 }
1829 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1830}
1831
1832std::vector<unsigned> ARMTargetLowering::
1833getRegClassForInlineAsmConstraint(const std::string &Constraint,
1834 MVT::ValueType VT) const {
1835 if (Constraint.size() != 1)
1836 return std::vector<unsigned>();
1837
1838 switch (Constraint[0]) { // GCC ARM Constraint Letters
1839 default: break;
1840 case 'l':
1841 case 'r':
1842 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1843 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1844 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1845 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00001846 case 'w':
1847 if (VT == MVT::f32)
1848 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1849 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1850 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1851 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1852 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1853 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1854 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1855 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1856 if (VT == MVT::f64)
1857 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1858 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1859 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1860 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1861 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001862 }
1863
1864 return std::vector<unsigned>();
1865}