blob: ab33ef475594348abcb523a15b53f4e1001b8fee [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"
Bob Wilson1f595bb2009-04-17 19:07:39 +000025#include "llvm/Function.h"
Evan Cheng27707472007-03-16 08:43:56 +000026#include "llvm/Instruction.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000027#include "llvm/Intrinsics.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/GlobalValue.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/CodeGen/CallingConvLower.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000037#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000038#include "llvm/ADT/VectorExtras.h"
Evan Chengb01fad62007-03-12 23:30:29 +000039#include "llvm/Support/MathExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000040using namespace llvm;
41
Bob Wilson1f595bb2009-04-17 19:07:39 +000042static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
43 MVT &LocVT,
44 CCValAssign::LocInfo &LocInfo,
45 ISD::ArgFlagsTy &ArgFlags,
46 CCState &State);
47static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
48 MVT &LocVT,
49 CCValAssign::LocInfo &LocInfo,
50 ISD::ArgFlagsTy &ArgFlags,
51 CCState &State);
52static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
53 MVT &LocVT,
54 CCValAssign::LocInfo &LocInfo,
55 ISD::ArgFlagsTy &ArgFlags,
56 CCState &State);
57static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
58 MVT &LocVT,
59 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
61 CCState &State);
62
Evan Chenga8e29892007-01-19 07:51:42 +000063ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
64 : TargetLowering(TM), ARMPCLabelIndex(0) {
65 Subtarget = &TM.getSubtarget<ARMSubtarget>();
66
Evan Chengb1df8f22007-04-27 08:15:43 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +000068 // Uses VFP for Thumb libfuncs if available.
69 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
70 // Single-precision floating-point arithmetic.
71 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
72 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
73 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
74 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000075
Evan Chengb1df8f22007-04-27 08:15:43 +000076 // Double-precision floating-point arithmetic.
77 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
78 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
79 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
80 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +000081
Evan Chengb1df8f22007-04-27 08:15:43 +000082 // Single-precision comparisons.
83 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
84 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
85 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
86 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
87 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
88 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
89 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
90 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +000091
Evan Chengb1df8f22007-04-27 08:15:43 +000092 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
93 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
94 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
95 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
96 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
97 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
98 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
99 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000100
Evan Chengb1df8f22007-04-27 08:15:43 +0000101 // Double-precision comparisons.
102 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
103 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
104 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
105 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
106 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
107 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
108 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
109 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000110
Evan Chengb1df8f22007-04-27 08:15:43 +0000111 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
112 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
113 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
114 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
115 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
116 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
117 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
118 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000119
Evan Chengb1df8f22007-04-27 08:15:43 +0000120 // Floating-point to integer conversions.
121 // i64 conversions are done via library routines even when generating VFP
122 // instructions, so use the same ones.
123 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
124 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
125 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
126 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000127
Evan Chengb1df8f22007-04-27 08:15:43 +0000128 // Conversions between floating types.
129 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
130 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
131
132 // Integer to floating-point conversions.
133 // i64 conversions are done via library routines even when generating VFP
134 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000135 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
136 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000137 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
138 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
139 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
140 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
141 }
Evan Chenga8e29892007-01-19 07:51:42 +0000142 }
143
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000144 if (Subtarget->isThumb())
145 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
146 else
147 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Evan Chengb6ab2542007-01-31 08:40:13 +0000148 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000149 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
150 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000151
Chris Lattnerddf89562008-01-17 19:59:44 +0000152 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000153 }
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000154 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000155
156 // ARM does not have f32 extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000157 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000158
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000159 // ARM does not have i1 sign extending load.
Evan Cheng03294662008-10-14 21:26:46 +0000160 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000161
Evan Chenga8e29892007-01-19 07:51:42 +0000162 // ARM supports all 4 flavors of integer indexed load / store.
163 for (unsigned im = (unsigned)ISD::PRE_INC;
164 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
165 setIndexedLoadAction(im, MVT::i1, Legal);
166 setIndexedLoadAction(im, MVT::i8, Legal);
167 setIndexedLoadAction(im, MVT::i16, Legal);
168 setIndexedLoadAction(im, MVT::i32, Legal);
169 setIndexedStoreAction(im, MVT::i1, Legal);
170 setIndexedStoreAction(im, MVT::i8, Legal);
171 setIndexedStoreAction(im, MVT::i16, Legal);
172 setIndexedStoreAction(im, MVT::i32, Legal);
173 }
174
175 // i64 operation support.
176 if (Subtarget->isThumb()) {
177 setOperationAction(ISD::MUL, MVT::i64, Expand);
178 setOperationAction(ISD::MULHU, MVT::i32, Expand);
179 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000180 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
181 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000182 } else {
Dan Gohman525178c2007-10-08 18:33:35 +0000183 setOperationAction(ISD::MUL, MVT::i64, Expand);
184 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000185 if (!Subtarget->hasV6Ops())
Dan Gohman525178c2007-10-08 18:33:35 +0000186 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000187 }
188 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
189 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
190 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
191 setOperationAction(ISD::SRL, MVT::i64, Custom);
192 setOperationAction(ISD::SRA, MVT::i64, Custom);
193
194 // ARM does not have ROTL.
195 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000196 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000197 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Evan Chengb0636152007-02-01 23:34:03 +0000198 if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
Evan Chenga8e29892007-01-19 07:51:42 +0000199 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
200
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000201 // Only ARMv6 has BSWAP.
202 if (!Subtarget->hasV6Ops())
Chris Lattner1719e132007-03-20 02:25:53 +0000203 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000204
Evan Chenga8e29892007-01-19 07:51:42 +0000205 // These are expanded into libcalls.
206 setOperationAction(ISD::SDIV, MVT::i32, Expand);
207 setOperationAction(ISD::UDIV, MVT::i32, Expand);
208 setOperationAction(ISD::SREM, MVT::i32, Expand);
209 setOperationAction(ISD::UREM, MVT::i32, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000210 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
211 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000212
Evan Chenga8e29892007-01-19 07:51:42 +0000213 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000214 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000215 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000216
217 setOperationAction(ISD::RET, MVT::Other, Custom);
218 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
219 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000220 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000221 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000222
Evan Chenga8e29892007-01-19 07:51:42 +0000223 // Use the default implementation.
Bob Wilson2dc4f542009-03-20 22:42:55 +0000224 setOperationAction(ISD::VASTART, MVT::Other, Custom);
225 setOperationAction(ISD::VAARG, MVT::Other, Expand);
226 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
227 setOperationAction(ISD::VAEND, MVT::Other, Expand);
228 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000229 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000230 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
231 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000232
233 if (!Subtarget->hasV6Ops()) {
234 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
236 }
237 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
238
Evan Chengb6ab2542007-01-31 08:40:13 +0000239 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
Evan Chengc7c77292008-11-04 19:57:48 +0000240 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
Evan Chenga8e29892007-01-19 07:51:42 +0000241 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000242
243 // We want to custom lower some of our intrinsics.
244 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
245
Bob Wilson2dc4f542009-03-20 22:42:55 +0000246 setOperationAction(ISD::SETCC, MVT::i32, Expand);
247 setOperationAction(ISD::SETCC, MVT::f32, Expand);
248 setOperationAction(ISD::SETCC, MVT::f64, Expand);
249 setOperationAction(ISD::SELECT, MVT::i32, Expand);
250 setOperationAction(ISD::SELECT, MVT::f32, Expand);
251 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000252 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
253 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
254 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
255
Bob Wilson2dc4f542009-03-20 22:42:55 +0000256 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
257 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
258 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
259 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
260 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000261
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000262 // We don't support sin/cos/fmod/copysign/pow
Bob Wilson2dc4f542009-03-20 22:42:55 +0000263 setOperationAction(ISD::FSIN, MVT::f64, Expand);
264 setOperationAction(ISD::FSIN, MVT::f32, Expand);
265 setOperationAction(ISD::FCOS, MVT::f32, Expand);
266 setOperationAction(ISD::FCOS, MVT::f64, Expand);
267 setOperationAction(ISD::FREM, MVT::f64, Expand);
268 setOperationAction(ISD::FREM, MVT::f32, Expand);
Evan Cheng110cf482008-04-01 01:50:16 +0000269 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
270 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
271 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
272 }
Bob Wilson2dc4f542009-03-20 22:42:55 +0000273 setOperationAction(ISD::FPOW, MVT::f64, Expand);
274 setOperationAction(ISD::FPOW, MVT::f32, Expand);
275
Evan Chenga8e29892007-01-19 07:51:42 +0000276 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
Evan Cheng110cf482008-04-01 01:50:16 +0000277 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
278 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
279 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
280 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
281 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
282 }
Evan Chenga8e29892007-01-19 07:51:42 +0000283
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000284 // We have target-specific dag combine patterns for the following nodes:
285 // ARMISD::FMRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000286 setTargetDAGCombine(ISD::ADD);
287 setTargetDAGCombine(ISD::SUB);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000288
Evan Chenga8e29892007-01-19 07:51:42 +0000289 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Chenga8e29892007-01-19 07:51:42 +0000290 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000291 setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
Evan Cheng97e604e2007-06-19 23:55:02 +0000292 setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000293
294 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
Evan Chenga8e29892007-01-19 07:51:42 +0000295}
296
Evan Chenga8e29892007-01-19 07:51:42 +0000297const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
298 switch (Opcode) {
299 default: return 0;
300 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000301 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
302 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000303 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000304 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
305 case ARMISD::tCALL: return "ARMISD::tCALL";
306 case ARMISD::BRCOND: return "ARMISD::BRCOND";
307 case ARMISD::BR_JT: return "ARMISD::BR_JT";
308 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
309 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
310 case ARMISD::CMP: return "ARMISD::CMP";
Lauro Ramos Venancio99966632007-04-02 01:30:03 +0000311 case ARMISD::CMPNZ: return "ARMISD::CMPNZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000312 case ARMISD::CMPFP: return "ARMISD::CMPFP";
313 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
314 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
315 case ARMISD::CMOV: return "ARMISD::CMOV";
316 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000317
Evan Chenga8e29892007-01-19 07:51:42 +0000318 case ARMISD::FTOSI: return "ARMISD::FTOSI";
319 case ARMISD::FTOUI: return "ARMISD::FTOUI";
320 case ARMISD::SITOF: return "ARMISD::SITOF";
321 case ARMISD::UITOF: return "ARMISD::UITOF";
Evan Chenga8e29892007-01-19 07:51:42 +0000322
323 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
324 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
325 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000326
Evan Chenga8e29892007-01-19 07:51:42 +0000327 case ARMISD::FMRRD: return "ARMISD::FMRRD";
328 case ARMISD::FMDRR: return "ARMISD::FMDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000329
330 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Evan Chenga8e29892007-01-19 07:51:42 +0000331 }
332}
333
334//===----------------------------------------------------------------------===//
335// Lowering Code
336//===----------------------------------------------------------------------===//
337
Evan Chenga8e29892007-01-19 07:51:42 +0000338/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
339static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
340 switch (CC) {
341 default: assert(0 && "Unknown condition code!");
342 case ISD::SETNE: return ARMCC::NE;
343 case ISD::SETEQ: return ARMCC::EQ;
344 case ISD::SETGT: return ARMCC::GT;
345 case ISD::SETGE: return ARMCC::GE;
346 case ISD::SETLT: return ARMCC::LT;
347 case ISD::SETLE: return ARMCC::LE;
348 case ISD::SETUGT: return ARMCC::HI;
349 case ISD::SETUGE: return ARMCC::HS;
350 case ISD::SETULT: return ARMCC::LO;
351 case ISD::SETULE: return ARMCC::LS;
352 }
353}
354
355/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
356/// returns true if the operands should be inverted to form the proper
357/// comparison.
358static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
359 ARMCC::CondCodes &CondCode2) {
360 bool Invert = false;
361 CondCode2 = ARMCC::AL;
362 switch (CC) {
363 default: assert(0 && "Unknown FP condition!");
364 case ISD::SETEQ:
365 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
366 case ISD::SETGT:
367 case ISD::SETOGT: CondCode = ARMCC::GT; break;
368 case ISD::SETGE:
369 case ISD::SETOGE: CondCode = ARMCC::GE; break;
370 case ISD::SETOLT: CondCode = ARMCC::MI; break;
371 case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
372 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
373 case ISD::SETO: CondCode = ARMCC::VC; break;
374 case ISD::SETUO: CondCode = ARMCC::VS; break;
375 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
376 case ISD::SETUGT: CondCode = ARMCC::HI; break;
377 case ISD::SETUGE: CondCode = ARMCC::PL; break;
378 case ISD::SETLT:
379 case ISD::SETULT: CondCode = ARMCC::LT; break;
380 case ISD::SETLE:
381 case ISD::SETULE: CondCode = ARMCC::LE; break;
382 case ISD::SETNE:
383 case ISD::SETUNE: CondCode = ARMCC::NE; break;
384 }
385 return Invert;
386}
387
Bob Wilson1f595bb2009-04-17 19:07:39 +0000388//===----------------------------------------------------------------------===//
389// Calling Convention Implementation
390//
391// The lower operations present on calling convention works on this order:
392// LowerCALL (virt regs --> phys regs, virt regs --> stack)
393// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
394// LowerRET (virt regs --> phys regs)
395// LowerCALL (phys regs --> virt regs)
396//
397//===----------------------------------------------------------------------===//
398
399#include "ARMGenCallingConv.inc"
400
401// APCS f64 is in register pairs, possibly split to stack
402static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
403 MVT &LocVT,
404 CCValAssign::LocInfo &LocInfo,
405 ISD::ArgFlagsTy &ArgFlags,
406 CCState &State) {
407 static const unsigned HiRegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
408 static const unsigned LoRegList[] = { ARM::R1,
409 ARM::R2,
410 ARM::R3,
411 ARM::NoRegister };
412
413 if (unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 4)) {
414 unsigned i;
415 for (i = 0; i < 4; ++i)
416 if (HiRegList[i] == Reg)
417 break;
418
419 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
420 MVT::i32, LocInfo));
421 if (LoRegList[i] != ARM::NoRegister)
422 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
423 MVT::i32, LocInfo));
Evan Chenga8e29892007-01-19 07:51:42 +0000424 else
Bob Wilson1f595bb2009-04-17 19:07:39 +0000425 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
426 State.AllocateStack(4, 4),
427 MVT::i32, LocInfo));
428 return true; // we handled it
Evan Chenga8e29892007-01-19 07:51:42 +0000429 }
Bob Wilson1f595bb2009-04-17 19:07:39 +0000430
431 return false; // we didn't handle it
432}
433
434// AAPCS f64 is in aligned register pairs
435static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
436 MVT &LocVT,
437 CCValAssign::LocInfo &LocInfo,
438 ISD::ArgFlagsTy &ArgFlags,
439 CCState &State) {
440 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
441 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
442
443 if (unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2)) {
444 unsigned i;
445 for (i = 0; i < 2; ++i)
446 if (HiRegList[i] == Reg)
447 break;
448
449 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
450 MVT::i32, LocInfo));
451 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
452 MVT::i32, LocInfo));
453 return true; // we handled it
454 }
455
456 return false; // we didn't handle it
457}
458
459static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
460 MVT &LocVT,
461 CCValAssign::LocInfo &LocInfo,
462 ISD::ArgFlagsTy &ArgFlags,
463 CCState &State) {
464 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
465 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
466
467 if (unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2)) {
468 unsigned i;
469 for (i = 0; i < 2; ++i)
470 if (HiRegList[i] == Reg)
471 break;
472
473 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
474 MVT::i32, LocInfo));
475 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
476 MVT::i32, LocInfo));
477 return true; // we handled it
478 }
479
480 return false; // we didn't handle it
481}
482
483static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT,
484 MVT &LocVT,
485 CCValAssign::LocInfo &LocInfo,
486 ISD::ArgFlagsTy &ArgFlags,
487 CCState &State) {
488 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
489 State);
490}
491
492/// AddLiveIn - This helper function adds the specified physical register to the
493/// MachineFunction as a live in value. It also creates a corresponding virtual
494/// register for it.
495static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
496 const TargetRegisterClass *RC) {
497 assert(RC->contains(PReg) && "Not the correct regclass!");
498 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
499 MF.getRegInfo().addLiveIn(PReg, VReg);
500 return VReg;
501}
502
503/// LowerCallResult - Lower the result values of an ISD::CALL into the
504/// appropriate copies out of appropriate physical registers. This assumes that
505/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
506/// being lowered. The returns a SDNode with the same number of values as the
507/// ISD::CALL.
508SDNode *ARMTargetLowering::
509LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
510 unsigned CallingConv, SelectionDAG &DAG) {
511
512 DebugLoc dl = TheCall->getDebugLoc();
513 // Assign locations to each value returned by this call.
514 SmallVector<CCValAssign, 16> RVLocs;
515 bool isVarArg = TheCall->isVarArg();
516 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
517 CCInfo.AnalyzeCallResult(TheCall, RetCC_ARM);
518
519 SmallVector<SDValue, 8> ResultVals;
520
521 // Copy all of the result registers out of their specified physreg.
522 for (unsigned i = 0; i != RVLocs.size(); ++i) {
523 CCValAssign VA = RVLocs[i];
524
525 // handle f64 as custom
526 if (VA.needsCustom()) {
527 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
528 InFlag);
529 VA = RVLocs[++i]; // skip ahead to next loc
530 SDValue Hi = DAG.getCopyFromReg(Lo, dl, VA.getLocReg(), VA.getLocVT(),
531 Lo.getValue(2));
532 ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, VA.getValVT(), Lo,
533 Hi));
534 } else {
535 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
536 InFlag).getValue(1);
537 SDValue Val = Chain.getValue(0);
538 InFlag = Chain.getValue(2);
539
540 switch (VA.getLocInfo()) {
541 default: assert(0 && "Unknown loc info!");
542 case CCValAssign::Full: break;
543 case CCValAssign::BCvt:
544 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(),
545 Chain.getValue(0));
546 break;
547 }
548
549 ResultVals.push_back(Val);
550 }
551 }
552
553 // Merge everything together with a MERGE_VALUES node.
554 ResultVals.push_back(Chain);
555 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
556 &ResultVals[0], ResultVals.size()).getNode();
557}
558
559/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
560/// by "Src" to address "Dst" of size "Size". Alignment information is
561/// specified by the specific parameter attribute. The copy will be passed as
562/// a byval function parameter.
563/// Sometimes what we are copying is the end of a larger object, the part that
564/// does not fit in registers.
565static SDValue
566CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
567 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
568 DebugLoc dl) {
569 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
570 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
571 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
572}
573
574/// LowerMemOpCallTo - Store the argument to the stack
575SDValue
576ARMTargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
577 const SDValue &StackPtr,
578 const CCValAssign &VA,
579 SDValue Chain,
580 SDValue Arg, ISD::ArgFlagsTy Flags) {
581 DebugLoc dl = TheCall->getDebugLoc();
582 unsigned LocMemOffset = VA.getLocMemOffset();
583 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
584 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
585 if (Flags.isByVal()) {
586 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
587 }
588 return DAG.getStore(Chain, dl, Arg, PtrOff,
589 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chenga8e29892007-01-19 07:51:42 +0000590}
591
Evan Chengfc403422007-02-03 08:53:01 +0000592/// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
593/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
594/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000595SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000596 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
Bob Wilson1f595bb2009-04-17 19:07:39 +0000597 MVT RetVT = TheCall->getRetValType(0);
598 SDValue Chain = TheCall->getChain();
599 unsigned CC = TheCall->getCallingConv();
600 assert((CC == CallingConv::C ||
601 CC == CallingConv::Fast) && "unknown calling convention");
602 bool isVarArg = TheCall->isVarArg();
603 SDValue Callee = TheCall->getCallee();
604 DebugLoc dl = TheCall->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000605
Bob Wilson1f595bb2009-04-17 19:07:39 +0000606 // Analyze operands of the call, assigning locations to each operand.
607 SmallVector<CCValAssign, 16> ArgLocs;
608 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
609 CCInfo.AnalyzeCallOperands(TheCall, CC_ARM);
Evan Chenga8e29892007-01-19 07:51:42 +0000610
Bob Wilson1f595bb2009-04-17 19:07:39 +0000611 // Get a count of how many bytes are to be pushed on the stack.
612 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +0000613
614 // Adjust the stack pointer for the new arguments...
615 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +0000616 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +0000617
Dan Gohman475871a2008-07-27 21:46:04 +0000618 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +0000619
Bob Wilson1f595bb2009-04-17 19:07:39 +0000620 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
621 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +0000622
Bob Wilson1f595bb2009-04-17 19:07:39 +0000623 // Walk the register/memloc assignments, inserting copies/loads. In the case
624 // of tail call optimization arguments are handle later.
625 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
626 i != e;
627 ++i, ++realArgIdx) {
628 CCValAssign &VA = ArgLocs[i];
629 SDValue Arg = TheCall->getArg(realArgIdx);
630 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(realArgIdx);
Evan Chenga8e29892007-01-19 07:51:42 +0000631
Bob Wilson1f595bb2009-04-17 19:07:39 +0000632 // Promote the value if needed.
633 switch (VA.getLocInfo()) {
634 default: assert(0 && "Unknown loc info!");
635 case CCValAssign::Full: break;
636 case CCValAssign::SExt:
637 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
638 break;
639 case CCValAssign::ZExt:
640 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
641 break;
642 case CCValAssign::AExt:
643 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
644 break;
645 case CCValAssign::BCvt:
646 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
647 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000648 }
649
Bob Wilson1f595bb2009-04-17 19:07:39 +0000650 // f64 is passed in i32 pairs and must be combined
651 if (VA.needsCustom()) {
652 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
653 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
654 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
655 VA = ArgLocs[++i]; // skip ahead to next loc
656 if (VA.isRegLoc())
657 RegsToPass.push_back(std::make_pair(VA.getLocReg(),
658 fmrrd.getValue(1)));
659 else {
660 assert(VA.isMemLoc());
661 if (StackPtr.getNode() == 0)
662 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
663
664 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
665 Chain, fmrrd.getValue(1),
666 Flags));
667 }
668 } else if (VA.isRegLoc()) {
669 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
670 } else {
671 assert(VA.isMemLoc());
672 if (StackPtr.getNode() == 0)
673 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
674
675 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
676 Chain, Arg, Flags));
677 }
Evan Chenga8e29892007-01-19 07:51:42 +0000678 }
679
680 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000681 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +0000682 &MemOpChains[0], MemOpChains.size());
683
684 // Build a sequence of copy-to-reg nodes chained together with token chain
685 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +0000686 SDValue InFlag;
Evan Chenga8e29892007-01-19 07:51:42 +0000687 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Bob Wilson2dc4f542009-03-20 22:42:55 +0000688 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000689 RegsToPass[i].second, InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000690 InFlag = Chain.getValue(1);
691 }
692
Bill Wendling056292f2008-09-16 21:48:12 +0000693 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
694 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
695 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +0000696 bool isDirect = false;
697 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +0000698 bool isLocalARMFunc = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000699 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
700 GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +0000701 isDirect = true;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000702 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000703 GV->hasLinkOnceLinkage());
Evan Cheng970a4192007-01-19 19:28:01 +0000704 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +0000705 getTargetMachine().getRelocationModel() != Reloc::Static;
706 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +0000707 // ARM call to a local ARM function is predicable.
708 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
Evan Chengc60e76d2007-01-30 20:37:08 +0000709 // tBX takes a register source operand.
710 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
711 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
712 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000713 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000714 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000715 Callee = DAG.getLoad(getPointerTy(), dl,
716 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000718 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000719 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000720 } else
721 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000722 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000723 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +0000724 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000725 getTargetMachine().getRelocationModel() != Reloc::Static;
726 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +0000727 // tBX takes a register source operand.
728 const char *Sym = S->getSymbol();
729 if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
730 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
731 ARMCP::CPStub, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000732 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000733 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000734 Callee = DAG.getLoad(getPointerTy(), dl,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000735 DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000736 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000737 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000738 getPointerTy(), Callee, PICLabel);
Evan Chengc60e76d2007-01-30 20:37:08 +0000739 } else
Bill Wendling056292f2008-09-16 21:48:12 +0000740 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +0000741 }
742
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000743 // FIXME: handle tail calls differently.
744 unsigned CallOpc;
745 if (Subtarget->isThumb()) {
746 if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
747 CallOpc = ARMISD::CALL_NOLINK;
748 else
749 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
750 } else {
751 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +0000752 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
753 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000754 }
Lauro Ramos Venanciob8a93a42007-03-27 16:19:21 +0000755 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
756 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
Dale Johannesene8d72302009-02-06 23:05:02 +0000757 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +0000758 InFlag = Chain.getValue(1);
759 }
760
Dan Gohman475871a2008-07-27 21:46:04 +0000761 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +0000762 Ops.push_back(Chain);
763 Ops.push_back(Callee);
764
765 // Add argument registers to the end of the list so that they are known live
766 // into the call.
767 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
768 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
769 RegsToPass[i].second.getValueType()));
770
Gabor Greifba36cb52008-08-28 21:40:38 +0000771 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +0000772 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000773 // Returns a chain and a flag for retval copy to use.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000774 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands4bdcb612008-07-02 17:40:58 +0000775 &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +0000776 InFlag = Chain.getValue(1);
777
Chris Lattnere563bbc2008-10-11 22:08:30 +0000778 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
779 DAG.getIntPtrConstant(0, true), InFlag);
Evan Chenga8e29892007-01-19 07:51:42 +0000780 if (RetVT != MVT::Other)
781 InFlag = Chain.getValue(1);
782
Bob Wilson1f595bb2009-04-17 19:07:39 +0000783 // Handle result values, copying them out of physregs into vregs that we
784 // return.
785 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
786 Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +0000787}
788
Bob Wilson1f595bb2009-04-17 19:07:39 +0000789SDValue ARMTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
790 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +0000791 SDValue Chain = Op.getOperand(0);
Dale Johannesena05dca42009-02-04 23:02:30 +0000792 DebugLoc dl = Op.getDebugLoc();
Bob Wilson2dc4f542009-03-20 22:42:55 +0000793
Bob Wilson1f595bb2009-04-17 19:07:39 +0000794 // CCValAssign - represent the assignment of
795 // the return value to a location
796 SmallVector<CCValAssign, 16> RVLocs;
797 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
798 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
799
800 // CCState - Info about the registers and stack slot.
801 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
802
803 // Analize return values of ISD::RET
804 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_ARM);
805
806 // If this is the first return lowered for this function, add
807 // the regs to the liveout set for the function.
808 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
809 for (unsigned i = 0; i != RVLocs.size(); ++i)
810 if (RVLocs[i].isRegLoc())
811 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000812 }
813
Bob Wilson1f595bb2009-04-17 19:07:39 +0000814 SDValue Flag;
815
816 // Copy the result values into the output registers.
817 for (unsigned i = 0, realRVLocIdx = 0;
818 i != RVLocs.size();
819 ++i, ++realRVLocIdx) {
820 CCValAssign &VA = RVLocs[i];
821 assert(VA.isRegLoc() && "Can only return in registers!");
822
823 // ISD::RET => ret chain, (regnum1,val1), ...
824 // So i*2+1 index only the regnums
825 SDValue Arg = Op.getOperand(realRVLocIdx*2+1);
826
827 switch (VA.getLocInfo()) {
828 default: assert(0 && "Unknown loc info!");
829 case CCValAssign::Full: break;
830 case CCValAssign::BCvt:
831 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
832 break;
833 }
834
835 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
836 // available.
837 if (VA.needsCustom()) {
838 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
839 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
840 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
841 VA = RVLocs[++i]; // skip ahead to next loc
842 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
843 Flag);
844 } else
845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
846
847 // guarantee that all emitted copies are
848 // stuck together, avoiding something bad
849 Flag = Chain.getValue(1);
850 }
851
852 SDValue result;
853 if (Flag.getNode())
854 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
855 else // Return Void
856 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
857
858 return result;
Evan Chenga8e29892007-01-19 07:51:42 +0000859}
860
Bob Wilson2dc4f542009-03-20 22:42:55 +0000861// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Bill Wendling056292f2008-09-16 21:48:12 +0000862// their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
863// one of the above mentioned nodes. It has to be wrapped because otherwise
864// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
865// be used to form addressing mode. These wrapped nodes will be selected
866// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +0000867static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868 MVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000869 // FIXME there is no actual debug info here
870 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +0000871 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +0000873 if (CP->isMachineConstantPoolEntry())
874 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
875 CP->getAlignment());
876 else
877 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
878 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000879 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +0000880}
881
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000882// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +0000883SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000884ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
885 SelectionDAG &DAG) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000886 DebugLoc dl = GA->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000887 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000888 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
889 ARMConstantPoolValue *CPV =
890 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
891 PCAdj, "tlsgd", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000892 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000893 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000894 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000896
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000898 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000899
900 // call __tls_get_addr.
901 ArgListTy Args;
902 ArgListEntry Entry;
903 Entry.Node = Argument;
904 Entry.Ty = (const Type *) Type::Int32Ty;
905 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000906 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +0000907 std::pair<SDValue, SDValue> CallResult =
Dale Johannesen86098bd2008-09-26 19:31:26 +0000908 LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000909 CallingConv::C, false,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000910 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000911 return CallResult.first;
912}
913
914// Lower ISD::GlobalTLSAddress using the "initial exec" or
915// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +0000916SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000917ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000918 SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000919 GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000920 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue Offset;
922 SDValue Chain = DAG.getEntryNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000923 MVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000924 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000925 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000926
927 if (GV->isDeclaration()){
928 // initial exec model
929 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
930 ARMConstantPoolValue *CPV =
931 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
932 PCAdj, "gottpoff", true);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000933 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000934 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000935 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000936 Chain = Offset.getValue(1);
937
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000939 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000940
Dale Johannesen33c960f2009-02-04 20:06:27 +0000941 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000942 } else {
943 // local exec model
944 ARMConstantPoolValue *CPV =
945 new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000946 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000947 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000948 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000949 }
950
951 // The address of the thread local variable is the add of the thread
952 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000953 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000954}
955
Dan Gohman475871a2008-07-27 21:46:04 +0000956SDValue
957ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000958 // TODO: implement the "local dynamic" model
959 assert(Subtarget->isTargetELF() &&
960 "TLS not implemented for non-ELF targets");
961 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
962 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
963 // otherwise use the "Local Exec" TLS Model
964 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
965 return LowerToTLSGeneralDynamicModel(GA, DAG);
966 else
967 return LowerToTLSExecModels(GA, DAG);
968}
969
Dan Gohman475871a2008-07-27 21:46:04 +0000970SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +0000971 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000972 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000973 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000974 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
975 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
976 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000977 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000978 ARMConstantPoolValue *CPV =
979 new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
Evan Cheng1606e8e2009-03-13 07:51:59 +0000980 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000981 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000982 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Dale Johannesen33c960f2009-02-04 20:06:27 +0000983 CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000984 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000985 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000986 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000987 if (!UseGOTOFF)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000988 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000989 return Result;
990 } else {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000991 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000992 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000993 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000994 }
995}
996
Evan Chenga8e29892007-01-19 07:51:42 +0000997/// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
Evan Cheng97c9bb52007-05-04 00:26:58 +0000998/// even in non-static mode.
999static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
Evan Chengae94e592008-12-05 01:06:39 +00001000 // If symbol visibility is hidden, the extra load is not needed if
1001 // the symbol is definitely defined in the current translation unit.
1002 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
1003 if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
1004 return false;
Duncan Sands667d4b82009-03-07 15:45:40 +00001005 return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
Evan Chenga8e29892007-01-19 07:51:42 +00001006}
1007
Dan Gohman475871a2008-07-27 21:46:04 +00001008SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001009 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001010 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001011 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001012 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1013 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng97c9bb52007-05-04 00:26:58 +00001014 bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
Dan Gohman475871a2008-07-27 21:46:04 +00001015 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001016 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001017 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001018 else {
1019 unsigned PCAdj = (RelocM != Reloc::PIC_)
1020 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Evan Chengc60e76d2007-01-30 20:37:08 +00001021 ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
1022 : ARMCP::CPValue;
Evan Chenga8e29892007-01-19 07:51:42 +00001023 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
Evan Chengc60e76d2007-01-30 20:37:08 +00001024 Kind, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001025 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001026 }
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001027 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001028
Dale Johannesen33c960f2009-02-04 20:06:27 +00001029 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001030 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001031
1032 if (RelocM == Reloc::PIC_) {
Dan Gohman475871a2008-07-27 21:46:04 +00001033 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001034 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001035 }
1036 if (IsIndirect)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001037 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001038
1039 return Result;
1040}
1041
Dan Gohman475871a2008-07-27 21:46:04 +00001042SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001043 SelectionDAG &DAG){
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001044 assert(Subtarget->isTargetELF() &&
1045 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001046 MVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001047 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001048 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1049 ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
1050 ARMPCLabelIndex,
1051 ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001052 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001053 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001054 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001056 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001057}
1058
Dan Gohman475871a2008-07-27 21:46:04 +00001059static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001060 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001061 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001062 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001063 default: return SDValue(); // Don't custom lower most intrinsics.
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001064 case Intrinsic::arm_thread_pointer:
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001065 return DAG.getNode(ARMISD::THREAD_POINTER, DebugLoc::getUnknownLoc(),
1066 PtrVT);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001067 }
1068}
1069
Dan Gohman475871a2008-07-27 21:46:04 +00001070static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001071 unsigned VarArgsFrameIndex) {
Evan Chenga8e29892007-01-19 07:51:42 +00001072 // vastart just stores the address of the VarArgsFrameIndex slot into the
1073 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001074 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001075 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001076 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001077 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001078 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001079}
1080
Dan Gohman475871a2008-07-27 21:46:04 +00001081SDValue
1082ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 MachineFunction &MF = DAG.getMachineFunction();
1084 MachineFrameInfo *MFI = MF.getFrameInfo();
1085
Dan Gohman475871a2008-07-27 21:46:04 +00001086 SDValue Root = Op.getOperand(0);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001087 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001088 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089 unsigned CC = MF.getFunction()->getCallingConv();
1090 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1091
1092 // Assign locations to all of the incoming arguments.
1093 SmallVector<CCValAssign, 16> ArgLocs;
1094 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1095 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_ARM);
1096
1097 SmallVector<SDValue, 16> ArgValues;
1098
1099 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1100 CCValAssign &VA = ArgLocs[i];
1101
1102 // Arguments stored on registers
1103 if (VA.isRegLoc()) {
1104 MVT RegVT = VA.getLocVT();
1105 TargetRegisterClass *RC;
1106 if (AFI->isThumbFunction())
1107 RC = ARM::tGPRRegisterClass;
1108 else
1109 RC = ARM::GPRRegisterClass;
1110
1111 if (RegVT == MVT::f64) {
1112 // f64 is passed in pairs of GPRs and must be combined
1113 RegVT = MVT::i32;
1114 } else if (!((RegVT == MVT::i32) || (RegVT == MVT::f32)))
1115 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
1116
1117 // Transform the arguments stored on
1118 // physical registers into virtual ones
1119 unsigned Reg = AddLiveIn(MF, VA.getLocReg(), RC);
1120 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
1121
1122 // f64 is passed in i32 pairs and must be combined
1123 if (VA.needsCustom()) {
1124 SDValue ArgValue2;
1125
1126 VA = ArgLocs[++i]; // skip ahead to next loc
1127 if (VA.isMemLoc()) {
1128 // must be APCS and older than V5T to split like this
1129 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1130 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1131
1132 // Create load node to retrieve arguments from the stack
1133 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1134 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
1135 } else {
1136 Reg = AddLiveIn(MF, VA.getLocReg(), RC);
1137 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1138 }
1139
1140 ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64,
1141 ArgValue, ArgValue2);
1142 }
1143
1144 // If this is an 8 or 16-bit value, it is really passed promoted
1145 // to 32 bits. Insert an assert[sz]ext to capture this, then
1146 // truncate to the right size.
1147 switch (VA.getLocInfo()) {
1148 default: assert(0 && "Unknown loc info!");
1149 case CCValAssign::Full: break;
1150 case CCValAssign::BCvt:
1151 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1152 break;
1153 case CCValAssign::SExt:
1154 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1155 DAG.getValueType(VA.getValVT()));
1156 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1157 break;
1158 case CCValAssign::ZExt:
1159 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1160 DAG.getValueType(VA.getValVT()));
1161 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1162 break;
1163 }
1164
1165 ArgValues.push_back(ArgValue);
1166
1167 } else { // VA.isRegLoc()
1168
1169 // sanity check
1170 assert(VA.isMemLoc());
1171 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1172
1173 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1174 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1175
1176 // Create load nodes to retrieve arguments from the stack
1177 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1178 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1179 }
1180 }
1181
1182 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00001183 if (isVarArg) {
1184 static const unsigned GPRArgRegs[] = {
1185 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1186 };
1187
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188 unsigned NumGPRs = CCInfo.getFirstUnallocated(GPRArgRegs,
1189 sizeof(GPRArgRegs)/sizeof(GPRArgRegs[0]));
1190
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001191 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1192 unsigned VARegSize = (4 - NumGPRs) * 4;
1193 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001194 unsigned ArgOffset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +00001195 if (VARegSaveSize) {
1196 // If this function is vararg, store any remaining integer argument regs
1197 // to their spots on the stack so that they may be loaded by deferencing
1198 // the result of va_next.
1199 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 ArgOffset = CCInfo.getNextStackOffset();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00001201 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1202 VARegSaveSize - VARegSize);
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001204
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00001206 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 TargetRegisterClass *RC;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001208 if (AFI->isThumbFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00001210 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00001211 RC = ARM::GPRRegisterClass;
1212
1213 unsigned VReg = AddLiveIn(MF, GPRArgRegs[NumGPRs], RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001214 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1215 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001216 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001217 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00001218 DAG.getConstant(4, getPointerTy()));
1219 }
1220 if (!MemOps.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +00001221 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001222 &MemOps[0], MemOps.size());
1223 } else
1224 // This will point to the next argument passed via stack.
1225 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1226 }
1227
1228 ArgValues.push_back(Root);
1229
1230 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001231 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Evan Chenga8e29892007-01-19 07:51:42 +00001233}
1234
1235/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001236static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00001237 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001238 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00001239 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00001240 // Maybe this has already been legalized into the constant pool?
1241 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00001242 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001243 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1244 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00001245 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00001246 }
1247 }
1248 return false;
1249}
1250
Evan Cheng9a2ef952007-02-02 01:53:26 +00001251static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
Evan Chenga8e29892007-01-19 07:51:42 +00001252 return ( isThumb && (C & ~255U) == 0) ||
1253 (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1254}
1255
1256/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1257/// the given operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001258static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Dale Johannesende064702009-02-06 21:50:26 +00001259 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb,
1260 DebugLoc dl) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001262 unsigned C = RHSC->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001263 if (!isLegalCmpImmediate(C, isThumb)) {
1264 // Constant does not fit, try adjusting it by one?
1265 switch (CC) {
1266 default: break;
1267 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00001268 case ISD::SETGE:
Evan Chenga8e29892007-01-19 07:51:42 +00001269 if (isLegalCmpImmediate(C-1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001270 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1271 RHS = DAG.getConstant(C-1, MVT::i32);
1272 }
1273 break;
1274 case ISD::SETULT:
1275 case ISD::SETUGE:
1276 if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1277 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Evan Chenga8e29892007-01-19 07:51:42 +00001278 RHS = DAG.getConstant(C-1, MVT::i32);
1279 }
1280 break;
1281 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00001282 case ISD::SETGT:
Evan Chenga8e29892007-01-19 07:51:42 +00001283 if (isLegalCmpImmediate(C+1, isThumb)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00001284 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1285 RHS = DAG.getConstant(C+1, MVT::i32);
1286 }
1287 break;
1288 case ISD::SETULE:
1289 case ISD::SETUGT:
1290 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1291 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Evan Chenga8e29892007-01-19 07:51:42 +00001292 RHS = DAG.getConstant(C+1, MVT::i32);
1293 }
1294 break;
1295 }
1296 }
1297 }
1298
1299 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00001300 ARMISD::NodeType CompareType;
1301 switch (CondCode) {
1302 default:
1303 CompareType = ARMISD::CMP;
1304 break;
1305 case ARMCC::EQ:
1306 case ARMCC::NE:
1307 case ARMCC::MI:
1308 case ARMCC::PL:
1309 // Uses only N and Z Flags
1310 CompareType = ARMISD::CMPNZ;
1311 break;
1312 }
Evan Chenga8e29892007-01-19 07:51:42 +00001313 ARMCC = DAG.getConstant(CondCode, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001314 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001315}
1316
1317/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001318static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Dale Johannesende064702009-02-06 21:50:26 +00001319 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00001321 if (!isFloatingPointZero(RHS))
Dale Johannesende064702009-02-06 21:50:26 +00001322 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00001323 else
Dale Johannesende064702009-02-06 21:50:26 +00001324 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1325 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001326}
1327
Dan Gohman475871a2008-07-27 21:46:04 +00001328static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001329 const ARMSubtarget *ST) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001330 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001331 SDValue LHS = Op.getOperand(0);
1332 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001333 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SDValue TrueVal = Op.getOperand(2);
1335 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00001336 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001337
1338 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SDValue ARMCC;
1340 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001341 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
1342 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001343 }
1344
1345 ARMCC::CondCodes CondCode, CondCode2;
1346 if (FPCCToARMCC(CC, CondCode, CondCode2))
1347 std::swap(TrueVal, FalseVal);
1348
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1350 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001351 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1352 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0e1d3792007-07-05 07:18:20 +00001353 ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001354 if (CondCode2 != ARMCC::AL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001356 // FIXME: Needs another CMP because flag can have but one use.
Dale Johannesende064702009-02-06 21:50:26 +00001357 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001358 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Dale Johannesende064702009-02-06 21:50:26 +00001359 Result, TrueVal, ARMCC2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00001360 }
1361 return Result;
1362}
1363
Dan Gohman475871a2008-07-27 21:46:04 +00001364static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001365 const ARMSubtarget *ST) {
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00001367 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue LHS = Op.getOperand(2);
1369 SDValue RHS = Op.getOperand(3);
1370 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00001371 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001372
1373 if (LHS.getValueType() == MVT::i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue ARMCC;
1375 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001376 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb(), dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001377 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +00001378 Chain, Dest, ARMCC, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001379 }
1380
1381 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1382 ARMCC::CondCodes CondCode, CondCode2;
1383 if (FPCCToARMCC(CC, CondCode, CondCode2))
1384 // Swap the LHS/RHS of the comparison if needed.
1385 std::swap(LHS, RHS);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001386
Dale Johannesende064702009-02-06 21:50:26 +00001387 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1389 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00001390 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001391 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00001392 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001393 if (CondCode2 != ARMCC::AL) {
1394 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00001395 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00001396 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00001397 }
1398 return Res;
1399}
1400
Dan Gohman475871a2008-07-27 21:46:04 +00001401SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1402 SDValue Chain = Op.getOperand(0);
1403 SDValue Table = Op.getOperand(1);
1404 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001405 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001406
Duncan Sands83ec4b62008-06-06 12:08:01 +00001407 MVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00001408 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1409 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1411 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Dale Johannesende064702009-02-06 21:50:26 +00001412 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001413 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1414 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chenga8e29892007-01-19 07:51:42 +00001415 bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001416 Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
Evan Chenge2446c62007-06-26 18:31:22 +00001417 Chain, Addr, NULL, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001418 Chain = Addr.getValue(1);
1419 if (isPIC)
Dale Johannesen33c960f2009-02-04 20:06:27 +00001420 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1421 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chenga8e29892007-01-19 07:51:42 +00001422}
1423
Dan Gohman475871a2008-07-27 21:46:04 +00001424static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00001425 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001426 unsigned Opc =
1427 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
Dale Johannesende064702009-02-06 21:50:26 +00001428 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1429 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001430}
1431
Dan Gohman475871a2008-07-27 21:46:04 +00001432static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001433 MVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001434 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001435 unsigned Opc =
1436 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1437
Dale Johannesende064702009-02-06 21:50:26 +00001438 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1439 return DAG.getNode(Opc, dl, VT, Op);
Evan Chenga8e29892007-01-19 07:51:42 +00001440}
1441
Dan Gohman475871a2008-07-27 21:46:04 +00001442static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001443 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00001444 SDValue Tmp0 = Op.getOperand(0);
1445 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00001446 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 MVT VT = Op.getValueType();
1448 MVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001449 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1450 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1452 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00001453 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00001454}
1455
Dan Gohman475871a2008-07-27 21:46:04 +00001456SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00001457ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001458 SDValue Chain,
1459 SDValue Dst, SDValue Src,
1460 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00001461 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00001462 const Value *DstSV, uint64_t DstSVOff,
1463 const Value *SrcSV, uint64_t SrcSVOff){
Evan Cheng4102eb52007-10-22 22:11:27 +00001464 // Do repeated 4-byte loads and stores. To be improved.
Dan Gohman707e0182008-04-12 04:36:06 +00001465 // This requires 4-byte alignment.
1466 if ((Align & 3) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00001467 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001468 // This requires the copy size to be a constant, preferrably
1469 // within a subtarget-specific limit.
1470 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1471 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00001472 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001473 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001474 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00001475 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00001476
1477 unsigned BytesLeft = SizeVal & 3;
1478 unsigned NumMemOps = SizeVal >> 2;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001479 unsigned EmittedNumMemOps = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001480 MVT VT = MVT::i32;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001481 unsigned VTSize = 4;
Evan Cheng4102eb52007-10-22 22:11:27 +00001482 unsigned i = 0;
Evan Chenge5e7ce42007-05-18 01:19:57 +00001483 const unsigned MAX_LOADS_IN_LDM = 6;
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue TFOps[MAX_LOADS_IN_LDM];
1485 SDValue Loads[MAX_LOADS_IN_LDM];
Dan Gohman1f13c682008-04-28 17:15:20 +00001486 uint64_t SrcOff = 0, DstOff = 0;
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001487
Evan Cheng4102eb52007-10-22 22:11:27 +00001488 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1489 // same number of stores. The loads and stores will get combined into
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001490 // ldm/stm later on.
Evan Cheng4102eb52007-10-22 22:11:27 +00001491 while (EmittedNumMemOps < NumMemOps) {
1492 for (i = 0;
1493 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001494 Loads[i] = DAG.getLoad(VT, dl, Chain,
1495 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001496 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001497 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001498 TFOps[i] = Loads[i].getValue(1);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001499 SrcOff += VTSize;
1500 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001501 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001502
Evan Cheng4102eb52007-10-22 22:11:27 +00001503 for (i = 0;
1504 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00001505 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001506 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001507 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001508 DstSV, DstSVOff + DstOff);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001509 DstOff += VTSize;
1510 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001511 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001512
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001513 EmittedNumMemOps += i;
1514 }
1515
Bob Wilson2dc4f542009-03-20 22:42:55 +00001516 if (BytesLeft == 0)
Evan Cheng4102eb52007-10-22 22:11:27 +00001517 return Chain;
1518
1519 // Issue loads / stores for the trailing (1 - 3) bytes.
1520 unsigned BytesLeftSave = BytesLeft;
1521 i = 0;
1522 while (BytesLeft) {
1523 if (BytesLeft >= 2) {
1524 VT = MVT::i16;
1525 VTSize = 2;
1526 } else {
1527 VT = MVT::i8;
1528 VTSize = 1;
1529 }
1530
Dale Johannesen0f502f62009-02-03 22:26:09 +00001531 Loads[i] = DAG.getLoad(VT, dl, Chain,
1532 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
Evan Cheng4102eb52007-10-22 22:11:27 +00001533 DAG.getConstant(SrcOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001534 SrcSV, SrcSVOff + SrcOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001535 TFOps[i] = Loads[i].getValue(1);
1536 ++i;
1537 SrcOff += VTSize;
1538 BytesLeft -= VTSize;
1539 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001540 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Evan Cheng4102eb52007-10-22 22:11:27 +00001541
1542 i = 0;
1543 BytesLeft = BytesLeftSave;
1544 while (BytesLeft) {
1545 if (BytesLeft >= 2) {
1546 VT = MVT::i16;
1547 VTSize = 2;
1548 } else {
1549 VT = MVT::i8;
1550 VTSize = 1;
1551 }
1552
Dale Johannesen0f502f62009-02-03 22:26:09 +00001553 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
Bob Wilson2dc4f542009-03-20 22:42:55 +00001554 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
Evan Cheng4102eb52007-10-22 22:11:27 +00001555 DAG.getConstant(DstOff, MVT::i32)),
Dan Gohman1f13c682008-04-28 17:15:20 +00001556 DstSV, DstSVOff + DstOff);
Evan Cheng4102eb52007-10-22 22:11:27 +00001557 ++i;
1558 DstOff += VTSize;
1559 BytesLeft -= VTSize;
1560 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00001561 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
Dale Johannesen8dd86c12007-05-17 21:31:21 +00001562}
1563
Duncan Sands1607f052008-12-01 11:39:25 +00001564static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001565 SDValue Op = N->getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +00001566 DebugLoc dl = N->getDebugLoc();
Evan Chengc7c77292008-11-04 19:57:48 +00001567 if (N->getValueType(0) == MVT::f64) {
1568 // Turn i64->f64 into FMDRR.
Dale Johannesende064702009-02-06 21:50:26 +00001569 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001570 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001571 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Evan Chengc7c77292008-11-04 19:57:48 +00001572 DAG.getConstant(1, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001573 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
Evan Chengc7c77292008-11-04 19:57:48 +00001574 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001575
Evan Chengc7c77292008-11-04 19:57:48 +00001576 // Turn f64->i64 into FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001577 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
Dale Johannesende064702009-02-06 21:50:26 +00001578 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001579
Chris Lattner27a6c732007-11-24 07:07:01 +00001580 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001581 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
Chris Lattner27a6c732007-11-24 07:07:01 +00001582}
1583
Duncan Sands1607f052008-12-01 11:39:25 +00001584static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001585 assert(N->getValueType(0) == MVT::i64 &&
1586 (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1587 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00001588
Chris Lattner27a6c732007-11-24 07:07:01 +00001589 // We only lower SRA, SRL of 1 here, all others use generic lowering.
1590 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001591 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00001592 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001593
Chris Lattner27a6c732007-11-24 07:07:01 +00001594 // If we are in thumb mode, we don't have RRX.
Duncan Sands1607f052008-12-01 11:39:25 +00001595 if (ST->isThumb()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00001596
Chris Lattner27a6c732007-11-24 07:07:01 +00001597 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Dale Johannesende064702009-02-06 21:50:26 +00001598 DebugLoc dl = N->getDebugLoc();
1599 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001600 DAG.getConstant(0, MVT::i32));
Dale Johannesende064702009-02-06 21:50:26 +00001601 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Chris Lattner27a6c732007-11-24 07:07:01 +00001602 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001603
Chris Lattner27a6c732007-11-24 07:07:01 +00001604 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1605 // captures the result into a carry flag.
1606 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Dale Johannesende064702009-02-06 21:50:26 +00001607 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001608
Chris Lattner27a6c732007-11-24 07:07:01 +00001609 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Dale Johannesende064702009-02-06 21:50:26 +00001610 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00001611
Chris Lattner27a6c732007-11-24 07:07:01 +00001612 // Merge the pieces into a single i64 value.
Dale Johannesende064702009-02-06 21:50:26 +00001613 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00001614}
1615
Dan Gohman475871a2008-07-27 21:46:04 +00001616SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00001617 switch (Op.getOpcode()) {
1618 default: assert(0 && "Don't know how to custom lower this!"); abort();
1619 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001620 case ISD::GlobalAddress:
1621 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1622 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001623 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00001624 case ISD::CALL: return LowerCALL(Op, DAG);
1625 case ISD::RET: return LowerRET(Op, DAG);
1626 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
1627 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
1628 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1629 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1630 case ISD::SINT_TO_FP:
1631 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
1632 case ISD::FP_TO_SINT:
1633 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
1634 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001635 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00001636 case ISD::RETURNADDR: break;
1637 case ISD::FRAMEADDR: break;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001638 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00001639 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00001640 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00001641 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001642 case ISD::SRA: return ExpandSRx(Op.getNode(), DAG,Subtarget);
Evan Chenga8e29892007-01-19 07:51:42 +00001643 }
Dan Gohman475871a2008-07-27 21:46:04 +00001644 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001645}
1646
Duncan Sands1607f052008-12-01 11:39:25 +00001647/// ReplaceNodeResults - Replace the results of node with an illegal result
1648/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00001649void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1650 SmallVectorImpl<SDValue>&Results,
1651 SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00001652 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00001653 default:
1654 assert(0 && "Don't know how to custom expand this!");
1655 return;
1656 case ISD::BIT_CONVERT:
1657 Results.push_back(ExpandBIT_CONVERT(N, DAG));
1658 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00001659 case ISD::SRL:
Duncan Sands1607f052008-12-01 11:39:25 +00001660 case ISD::SRA: {
1661 SDValue Res = ExpandSRx(N, DAG, Subtarget);
1662 if (Res.getNode())
1663 Results.push_back(Res);
1664 return;
1665 }
Chris Lattner27a6c732007-11-24 07:07:01 +00001666 }
1667}
Chris Lattner27a6c732007-11-24 07:07:01 +00001668
Evan Chenga8e29892007-01-19 07:51:42 +00001669//===----------------------------------------------------------------------===//
1670// ARM Scheduler Hooks
1671//===----------------------------------------------------------------------===//
1672
1673MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00001674ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001675 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00001676 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00001677 DebugLoc dl = MI->getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001678 switch (MI->getOpcode()) {
1679 default: assert(false && "Unexpected instr type to insert");
1680 case ARM::tMOVCCr: {
1681 // To "insert" a SELECT_CC instruction, we actually have to insert the
1682 // diamond control-flow pattern. The incoming instruction knows the
1683 // destination vreg to set, the condition code register to branch on, the
1684 // true/false values to select between, and a branch opcode to use.
1685 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001686 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00001687 ++It;
1688
1689 // thisMBB:
1690 // ...
1691 // TrueVal = ...
1692 // cmpTY ccX, r1, r2
1693 // bCC copy1MBB
1694 // fallthrough --> copy0MBB
1695 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001696 MachineFunction *F = BB->getParent();
1697 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1698 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenb6728402009-02-13 02:25:56 +00001699 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
Evan Cheng0e1d3792007-07-05 07:18:20 +00001700 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001701 F->insert(It, copy0MBB);
1702 F->insert(It, sinkMBB);
Evan Chenga8e29892007-01-19 07:51:42 +00001703 // Update machine-CFG edges by first adding all successors of the current
1704 // block to the new block which will contain the Phi node for the select.
1705 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1706 e = BB->succ_end(); i != e; ++i)
1707 sinkMBB->addSuccessor(*i);
1708 // Next, remove all successors of the current block, and add the true
1709 // and fallthrough blocks as its successors.
1710 while(!BB->succ_empty())
1711 BB->removeSuccessor(BB->succ_begin());
1712 BB->addSuccessor(copy0MBB);
1713 BB->addSuccessor(sinkMBB);
1714
1715 // copy0MBB:
1716 // %FalseValue = ...
1717 // # fallthrough to sinkMBB
1718 BB = copy0MBB;
1719
1720 // Update machine-CFG edges
1721 BB->addSuccessor(sinkMBB);
1722
1723 // sinkMBB:
1724 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1725 // ...
1726 BB = sinkMBB;
Dale Johannesenb6728402009-02-13 02:25:56 +00001727 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00001728 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1729 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1730
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001731 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00001732 return BB;
1733 }
1734 }
1735}
1736
1737//===----------------------------------------------------------------------===//
1738// ARM Optimization Hooks
1739//===----------------------------------------------------------------------===//
1740
Chris Lattnerd1980a52009-03-12 06:52:53 +00001741static
1742SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
1743 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00001744 SelectionDAG &DAG = DCI.DAG;
1745 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1746 MVT VT = N->getValueType(0);
1747 unsigned Opc = N->getOpcode();
1748 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
1749 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
1750 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
1751 ISD::CondCode CC = ISD::SETCC_INVALID;
1752
1753 if (isSlctCC) {
1754 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
1755 } else {
1756 SDValue CCOp = Slct.getOperand(0);
1757 if (CCOp.getOpcode() == ISD::SETCC)
1758 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
1759 }
1760
1761 bool DoXform = false;
1762 bool InvCC = false;
1763 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
1764 "Bad input!");
1765
1766 if (LHS.getOpcode() == ISD::Constant &&
1767 cast<ConstantSDNode>(LHS)->isNullValue()) {
1768 DoXform = true;
1769 } else if (CC != ISD::SETCC_INVALID &&
1770 RHS.getOpcode() == ISD::Constant &&
1771 cast<ConstantSDNode>(RHS)->isNullValue()) {
1772 std::swap(LHS, RHS);
1773 SDValue Op0 = Slct.getOperand(0);
1774 MVT OpVT = isSlctCC ? Op0.getValueType() :
1775 Op0.getOperand(0).getValueType();
1776 bool isInt = OpVT.isInteger();
1777 CC = ISD::getSetCCInverse(CC, isInt);
1778
1779 if (!TLI.isCondCodeLegal(CC, OpVT))
1780 return SDValue(); // Inverse operator isn't legal.
1781
1782 DoXform = true;
1783 InvCC = true;
1784 }
1785
1786 if (DoXform) {
1787 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
1788 if (isSlctCC)
1789 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
1790 Slct.getOperand(0), Slct.getOperand(1), CC);
1791 SDValue CCOp = Slct.getOperand(0);
1792 if (InvCC)
1793 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
1794 CCOp.getOperand(0), CCOp.getOperand(1), CC);
1795 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
1796 CCOp, OtherOp, Result);
1797 }
1798 return SDValue();
1799}
1800
1801/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
1802static SDValue PerformADDCombine(SDNode *N,
1803 TargetLowering::DAGCombinerInfo &DCI) {
1804 // added by evan in r37685 with no testcase.
1805 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001806
Chris Lattnerd1980a52009-03-12 06:52:53 +00001807 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1808 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
1809 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
1810 if (Result.getNode()) return Result;
1811 }
1812 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1813 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1814 if (Result.getNode()) return Result;
1815 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001816
Chris Lattnerd1980a52009-03-12 06:52:53 +00001817 return SDValue();
1818}
1819
1820/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
1821static SDValue PerformSUBCombine(SDNode *N,
1822 TargetLowering::DAGCombinerInfo &DCI) {
1823 // added by evan in r37685 with no testcase.
1824 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001825
Chris Lattnerd1980a52009-03-12 06:52:53 +00001826 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1827 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
1828 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
1829 if (Result.getNode()) return Result;
1830 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001831
Chris Lattnerd1980a52009-03-12 06:52:53 +00001832 return SDValue();
1833}
1834
1835
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001836/// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001837static SDValue PerformFMRRDCombine(SDNode *N,
1838 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001839 // fmrrd(fmdrr x, y) -> x,y
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue InDouble = N->getOperand(0);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001841 if (InDouble.getOpcode() == ARMISD::FMDRR)
1842 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001843 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001844}
1845
Dan Gohman475871a2008-07-27 21:46:04 +00001846SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00001847 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001848 switch (N->getOpcode()) {
1849 default: break;
Chris Lattnerd1980a52009-03-12 06:52:53 +00001850 case ISD::ADD: return PerformADDCombine(N, DCI);
1851 case ISD::SUB: return PerformSUBCombine(N, DCI);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001852 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1853 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00001854
Dan Gohman475871a2008-07-27 21:46:04 +00001855 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00001856}
1857
Evan Chengb01fad62007-03-12 23:30:29 +00001858/// isLegalAddressImmediate - Return true if the integer value can be used
1859/// as the offset of the target addressing mode for load / store of the
1860/// given type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001861static bool isLegalAddressImmediate(int64_t V, MVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001862 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00001863 if (V == 0)
1864 return true;
1865
Evan Cheng65011532009-03-09 19:15:00 +00001866 if (!VT.isSimple())
1867 return false;
1868
Evan Chengb01fad62007-03-12 23:30:29 +00001869 if (Subtarget->isThumb()) {
1870 if (V < 0)
1871 return false;
1872
1873 unsigned Scale = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001874 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001875 default: return false;
1876 case MVT::i1:
1877 case MVT::i8:
1878 // Scale == 1;
1879 break;
1880 case MVT::i16:
1881 // Scale == 2;
1882 Scale = 2;
1883 break;
1884 case MVT::i32:
1885 // Scale == 4;
1886 Scale = 4;
1887 break;
1888 }
1889
1890 if ((V & (Scale - 1)) != 0)
1891 return false;
1892 V /= Scale;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001893 return V == (V & ((1LL << 5) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001894 }
1895
1896 if (V < 0)
1897 V = - V;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001898 switch (VT.getSimpleVT()) {
Evan Chengb01fad62007-03-12 23:30:29 +00001899 default: return false;
1900 case MVT::i1:
1901 case MVT::i8:
1902 case MVT::i32:
1903 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001904 return V == (V & ((1LL << 12) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001905 case MVT::i16:
1906 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001907 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001908 case MVT::f32:
1909 case MVT::f64:
1910 if (!Subtarget->hasVFP2())
1911 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00001912 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00001913 return false;
1914 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001915 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00001916 }
Evan Chenga8e29892007-01-19 07:51:42 +00001917}
1918
Chris Lattner37caf8c2007-04-09 23:33:39 +00001919/// isLegalAddressingMode - Return true if the addressing mode represented
1920/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001921bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00001922 const Type *Ty) const {
Bob Wilson2c7dab12009-04-08 17:55:28 +00001923 MVT VT = getValueType(Ty, true);
1924 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00001925 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001926
Chris Lattner37caf8c2007-04-09 23:33:39 +00001927 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001928 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001929 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001930
Chris Lattner37caf8c2007-04-09 23:33:39 +00001931 switch (AM.Scale) {
1932 case 0: // no scale reg, must be "r+i" or "r", or "i".
1933 break;
1934 case 1:
1935 if (Subtarget->isThumb())
1936 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001937 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00001938 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00001939 // ARM doesn't support any R+R*scale+imm addr modes.
1940 if (AM.BaseOffs)
1941 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001942
Bob Wilson2c7dab12009-04-08 17:55:28 +00001943 if (!VT.isSimple())
1944 return false;
1945
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001946 int Scale = AM.Scale;
Bob Wilson2c7dab12009-04-08 17:55:28 +00001947 switch (VT.getSimpleVT()) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00001948 default: return false;
1949 case MVT::i1:
1950 case MVT::i8:
1951 case MVT::i32:
1952 case MVT::i64:
1953 // This assumes i64 is legalized to a pair of i32. If not (i.e.
1954 // ldrd / strd are used, then its address mode is same as i16.
1955 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001956 if (Scale < 0) Scale = -Scale;
1957 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001958 return true;
1959 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00001960 return isPowerOf2_32(Scale & ~1);
Chris Lattner37caf8c2007-04-09 23:33:39 +00001961 case MVT::i16:
1962 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00001963 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00001964 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00001965 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00001966
Chris Lattner37caf8c2007-04-09 23:33:39 +00001967 case MVT::isVoid:
1968 // Note, we allow "void" uses (basically, uses that aren't loads or
1969 // stores), because arm allows folding a scale into many arithmetic
1970 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00001971
Chris Lattner37caf8c2007-04-09 23:33:39 +00001972 // Allow r << imm, but the imm has to be a multiple of two.
1973 if (AM.Scale & 1) return false;
1974 return isPowerOf2_32(AM.Scale);
1975 }
1976 break;
Evan Chengb01fad62007-03-12 23:30:29 +00001977 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00001978 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00001979}
1980
Duncan Sands83ec4b62008-06-06 12:08:01 +00001981static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001982 bool isSEXTLoad, SDValue &Base,
1983 SDValue &Offset, bool &isInc,
Evan Chenga8e29892007-01-19 07:51:42 +00001984 SelectionDAG &DAG) {
1985 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1986 return false;
1987
1988 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1989 // AddressingMode 3
1990 Base = Ptr->getOperand(0);
1991 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001992 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00001993 if (RHSC < 0 && RHSC > -256) {
1994 isInc = false;
1995 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1996 return true;
1997 }
1998 }
1999 isInc = (Ptr->getOpcode() == ISD::ADD);
2000 Offset = Ptr->getOperand(1);
2001 return true;
2002 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
2003 // AddressingMode 2
2004 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002005 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00002006 if (RHSC < 0 && RHSC > -0x1000) {
2007 isInc = false;
2008 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
2009 Base = Ptr->getOperand(0);
2010 return true;
2011 }
2012 }
2013
2014 if (Ptr->getOpcode() == ISD::ADD) {
2015 isInc = true;
2016 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
2017 if (ShOpcVal != ARM_AM::no_shift) {
2018 Base = Ptr->getOperand(1);
2019 Offset = Ptr->getOperand(0);
2020 } else {
2021 Base = Ptr->getOperand(0);
2022 Offset = Ptr->getOperand(1);
2023 }
2024 return true;
2025 }
2026
2027 isInc = (Ptr->getOpcode() == ISD::ADD);
2028 Base = Ptr->getOperand(0);
2029 Offset = Ptr->getOperand(1);
2030 return true;
2031 }
2032
2033 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
2034 return false;
2035}
2036
2037/// getPreIndexedAddressParts - returns true by value, base pointer and
2038/// offset pointer and addressing mode by reference if the node's address
2039/// can be legally represented as pre-indexed load / store address.
2040bool
Dan Gohman475871a2008-07-27 21:46:04 +00002041ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
2042 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002043 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002044 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002045 if (Subtarget->isThumb())
2046 return false;
2047
Duncan Sands83ec4b62008-06-06 12:08:01 +00002048 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002050 bool isSEXTLoad = false;
2051 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
2052 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002053 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002054 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2055 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
2056 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002057 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002058 } else
2059 return false;
2060
2061 bool isInc;
Gabor Greifba36cb52008-08-28 21:40:38 +00002062 bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002063 isInc, DAG);
2064 if (isLegal) {
2065 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
2066 return true;
2067 }
2068 return false;
2069}
2070
2071/// getPostIndexedAddressParts - returns true by value, base pointer and
2072/// offset pointer and addressing mode by reference if this node can be
2073/// combined with a load / store to form a post-indexed load / store.
2074bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00002075 SDValue &Base,
2076 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00002077 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00002078 SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002079 if (Subtarget->isThumb())
2080 return false;
2081
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 MVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00002084 bool isSEXTLoad = false;
2085 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002086 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002087 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
2088 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002089 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00002090 } else
2091 return false;
2092
2093 bool isInc;
2094 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
2095 isInc, DAG);
2096 if (isLegal) {
2097 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
2098 return true;
2099 }
2100 return false;
2101}
2102
Dan Gohman475871a2008-07-27 21:46:04 +00002103void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00002104 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00002105 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002106 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00002107 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00002108 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002109 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002110 switch (Op.getOpcode()) {
2111 default: break;
2112 case ARMISD::CMOV: {
2113 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00002114 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002115 if (KnownZero == 0 && KnownOne == 0) return;
2116
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002117 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
2119 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00002120 KnownZero &= KnownZeroRHS;
2121 KnownOne &= KnownOneRHS;
2122 return;
2123 }
2124 }
2125}
2126
2127//===----------------------------------------------------------------------===//
2128// ARM Inline Assembly Support
2129//===----------------------------------------------------------------------===//
2130
2131/// getConstraintType - Given a constraint letter, return the type of
2132/// constraint it is for this target.
2133ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00002134ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
2135 if (Constraint.size() == 1) {
2136 switch (Constraint[0]) {
2137 default: break;
2138 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002139 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00002140 }
Evan Chenga8e29892007-01-19 07:51:42 +00002141 }
Chris Lattner4234f572007-03-25 02:14:49 +00002142 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00002143}
2144
Bob Wilson2dc4f542009-03-20 22:42:55 +00002145std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00002146ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002147 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002148 if (Constraint.size() == 1) {
2149 // GCC RS6000 Constraint Letters
2150 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002151 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002152 if (Subtarget->isThumb())
2153 return std::make_pair(0U, ARM::tGPRRegisterClass);
2154 else
2155 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002156 case 'r':
2157 return std::make_pair(0U, ARM::GPRRegisterClass);
2158 case 'w':
2159 if (VT == MVT::f32)
2160 return std::make_pair(0U, ARM::SPRRegisterClass);
Evan Cheng0a7baa22007-04-04 00:06:07 +00002161 if (VT == MVT::f64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002162 return std::make_pair(0U, ARM::DPRRegisterClass);
2163 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002164 }
2165 }
2166 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2167}
2168
2169std::vector<unsigned> ARMTargetLowering::
2170getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002171 MVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002172 if (Constraint.size() != 1)
2173 return std::vector<unsigned>();
2174
2175 switch (Constraint[0]) { // GCC ARM Constraint Letters
2176 default: break;
2177 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002178 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2179 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2180 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002181 case 'r':
2182 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
2183 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
2184 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
2185 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00002186 case 'w':
2187 if (VT == MVT::f32)
2188 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
2189 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
2190 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
2191 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
2192 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
2193 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
2194 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
2195 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
2196 if (VT == MVT::f64)
2197 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
2198 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
2199 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
2200 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
2201 break;
Evan Chenga8e29892007-01-19 07:51:42 +00002202 }
2203
2204 return std::vector<unsigned>();
2205}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00002206
2207/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2208/// vector. If it is invalid, don't add anything to Ops.
2209void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
2210 char Constraint,
2211 bool hasMemory,
2212 std::vector<SDValue>&Ops,
2213 SelectionDAG &DAG) const {
2214 SDValue Result(0, 0);
2215
2216 switch (Constraint) {
2217 default: break;
2218 case 'I': case 'J': case 'K': case 'L':
2219 case 'M': case 'N': case 'O':
2220 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2221 if (!C)
2222 return;
2223
2224 int64_t CVal64 = C->getSExtValue();
2225 int CVal = (int) CVal64;
2226 // None of these constraints allow values larger than 32 bits. Check
2227 // that the value fits in an int.
2228 if (CVal != CVal64)
2229 return;
2230
2231 switch (Constraint) {
2232 case 'I':
2233 if (Subtarget->isThumb()) {
2234 // This must be a constant between 0 and 255, for ADD immediates.
2235 if (CVal >= 0 && CVal <= 255)
2236 break;
2237 } else {
2238 // A constant that can be used as an immediate value in a
2239 // data-processing instruction.
2240 if (ARM_AM::getSOImmVal(CVal) != -1)
2241 break;
2242 }
2243 return;
2244
2245 case 'J':
2246 if (Subtarget->isThumb()) {
2247 // This must be a constant between -255 and -1, for negated ADD
2248 // immediates. This can be used in GCC with an "n" modifier that
2249 // prints the negated value, for use with SUB instructions. It is
2250 // not useful otherwise but is implemented for compatibility.
2251 if (CVal >= -255 && CVal <= -1)
2252 break;
2253 } else {
2254 // This must be a constant between -4095 and 4095. It is not clear
2255 // what this constraint is intended for. Implemented for
2256 // compatibility with GCC.
2257 if (CVal >= -4095 && CVal <= 4095)
2258 break;
2259 }
2260 return;
2261
2262 case 'K':
2263 if (Subtarget->isThumb()) {
2264 // A 32-bit value where only one byte has a nonzero value. Exclude
2265 // zero to match GCC. This constraint is used by GCC internally for
2266 // constants that can be loaded with a move/shift combination.
2267 // It is not useful otherwise but is implemented for compatibility.
2268 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
2269 break;
2270 } else {
2271 // A constant whose bitwise inverse can be used as an immediate
2272 // value in a data-processing instruction. This can be used in GCC
2273 // with a "B" modifier that prints the inverted value, for use with
2274 // BIC and MVN instructions. It is not useful otherwise but is
2275 // implemented for compatibility.
2276 if (ARM_AM::getSOImmVal(~CVal) != -1)
2277 break;
2278 }
2279 return;
2280
2281 case 'L':
2282 if (Subtarget->isThumb()) {
2283 // This must be a constant between -7 and 7,
2284 // for 3-operand ADD/SUB immediate instructions.
2285 if (CVal >= -7 && CVal < 7)
2286 break;
2287 } else {
2288 // A constant whose negation can be used as an immediate value in a
2289 // data-processing instruction. This can be used in GCC with an "n"
2290 // modifier that prints the negated value, for use with SUB
2291 // instructions. It is not useful otherwise but is implemented for
2292 // compatibility.
2293 if (ARM_AM::getSOImmVal(-CVal) != -1)
2294 break;
2295 }
2296 return;
2297
2298 case 'M':
2299 if (Subtarget->isThumb()) {
2300 // This must be a multiple of 4 between 0 and 1020, for
2301 // ADD sp + immediate.
2302 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
2303 break;
2304 } else {
2305 // A power of two or a constant between 0 and 32. This is used in
2306 // GCC for the shift amount on shifted register operands, but it is
2307 // useful in general for any shift amounts.
2308 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
2309 break;
2310 }
2311 return;
2312
2313 case 'N':
2314 if (Subtarget->isThumb()) {
2315 // This must be a constant between 0 and 31, for shift amounts.
2316 if (CVal >= 0 && CVal <= 31)
2317 break;
2318 }
2319 return;
2320
2321 case 'O':
2322 if (Subtarget->isThumb()) {
2323 // This must be a multiple of 4 between -508 and 508, for
2324 // ADD/SUB sp = sp + immediate.
2325 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
2326 break;
2327 }
2328 return;
2329 }
2330 Result = DAG.getTargetConstant(CVal, Op.getValueType());
2331 break;
2332 }
2333
2334 if (Result.getNode()) {
2335 Ops.push_back(Result);
2336 return;
2337 }
2338 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
2339 Ops, DAG);
2340}