Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1 | //===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Evan Cheng and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that ARM uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ARM.h" |
| 16 | #include "ARMAddressingModes.h" |
| 17 | #include "ARMConstantPoolValue.h" |
| 18 | #include "ARMISelLowering.h" |
| 19 | #include "ARMMachineFunctionInfo.h" |
| 20 | #include "ARMRegisterInfo.h" |
| 21 | #include "ARMSubtarget.h" |
| 22 | #include "ARMTargetMachine.h" |
| 23 | #include "llvm/CallingConv.h" |
| 24 | #include "llvm/Constants.h" |
Evan Cheng | 2770747 | 2007-03-16 08:43:56 +0000 | [diff] [blame] | 25 | #include "llvm/Instruction.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/SelectionDAG.h" |
| 31 | #include "llvm/CodeGen/SSARegMap.h" |
Evan Cheng | b6ab254 | 2007-01-31 08:40:13 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/VectorExtras.h" |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| 37 | ARMTargetLowering::ARMTargetLowering(TargetMachine &TM) |
| 38 | : TargetLowering(TM), ARMPCLabelIndex(0) { |
| 39 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
| 40 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 41 | if (Subtarget->isTargetDarwin()) { |
| 42 | // Don't have these. |
| 43 | setLibcallName(RTLIB::UINTTOFP_I64_F32, NULL); |
| 44 | setLibcallName(RTLIB::UINTTOFP_I64_F64, NULL); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 45 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 46 | // Uses VFP for Thumb libfuncs if available. |
| 47 | if (Subtarget->isThumb() && Subtarget->hasVFP2()) { |
| 48 | // Single-precision floating-point arithmetic. |
| 49 | setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); |
| 50 | setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); |
| 51 | setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); |
| 52 | setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 53 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 54 | // Double-precision floating-point arithmetic. |
| 55 | setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); |
| 56 | setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); |
| 57 | setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); |
| 58 | setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); |
Evan Cheng | 193f850 | 2007-01-31 09:30:58 +0000 | [diff] [blame] | 59 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 60 | // Single-precision comparisons. |
| 61 | setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); |
| 62 | setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); |
| 63 | setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); |
| 64 | setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); |
| 65 | setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); |
| 66 | setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); |
| 67 | setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); |
| 68 | setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 69 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 70 | setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); |
| 71 | setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); |
| 72 | setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); |
| 73 | setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); |
| 74 | setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); |
| 75 | setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); |
| 76 | setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); |
| 77 | setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); |
Evan Cheng | 193f850 | 2007-01-31 09:30:58 +0000 | [diff] [blame] | 78 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 79 | // Double-precision comparisons. |
| 80 | setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); |
| 81 | setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); |
| 82 | setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); |
| 83 | setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); |
| 84 | setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); |
| 85 | setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); |
| 86 | setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); |
| 87 | setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 88 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 89 | setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); |
| 90 | setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); |
| 91 | setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); |
| 92 | setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); |
| 93 | setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); |
| 94 | setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); |
| 95 | setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); |
| 96 | setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 97 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 98 | // Floating-point to integer conversions. |
| 99 | // i64 conversions are done via library routines even when generating VFP |
| 100 | // instructions, so use the same ones. |
| 101 | setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); |
| 102 | setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); |
| 103 | setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); |
| 104 | setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 105 | |
Evan Cheng | b1df8f2 | 2007-04-27 08:15:43 +0000 | [diff] [blame] | 106 | // Conversions between floating types. |
| 107 | setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); |
| 108 | setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); |
| 109 | |
| 110 | // Integer to floating-point conversions. |
| 111 | // i64 conversions are done via library routines even when generating VFP |
| 112 | // instructions, so use the same ones. |
| 113 | // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g. |
| 114 | // __floatunsidf vs. __floatunssidfvfp. |
| 115 | setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); |
| 116 | setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); |
| 117 | setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); |
| 118 | setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); |
| 119 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | addRegisterClass(MVT::i32, ARM::GPRRegisterClass); |
Evan Cheng | b6ab254 | 2007-01-31 08:40:13 +0000 | [diff] [blame] | 123 | if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 124 | addRegisterClass(MVT::f32, ARM::SPRRegisterClass); |
| 125 | addRegisterClass(MVT::f64, ARM::DPRRegisterClass); |
| 126 | } |
Evan Cheng | 9f8cbd1 | 2007-05-18 00:19:34 +0000 | [diff] [blame] | 127 | computeRegisterProperties(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 128 | |
| 129 | // ARM does not have f32 extending load. |
| 130 | setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 131 | |
| 132 | // ARM supports all 4 flavors of integer indexed load / store. |
| 133 | for (unsigned im = (unsigned)ISD::PRE_INC; |
| 134 | im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { |
| 135 | setIndexedLoadAction(im, MVT::i1, Legal); |
| 136 | setIndexedLoadAction(im, MVT::i8, Legal); |
| 137 | setIndexedLoadAction(im, MVT::i16, Legal); |
| 138 | setIndexedLoadAction(im, MVT::i32, Legal); |
| 139 | setIndexedStoreAction(im, MVT::i1, Legal); |
| 140 | setIndexedStoreAction(im, MVT::i8, Legal); |
| 141 | setIndexedStoreAction(im, MVT::i16, Legal); |
| 142 | setIndexedStoreAction(im, MVT::i32, Legal); |
| 143 | } |
| 144 | |
| 145 | // i64 operation support. |
| 146 | if (Subtarget->isThumb()) { |
| 147 | setOperationAction(ISD::MUL, MVT::i64, Expand); |
| 148 | setOperationAction(ISD::MULHU, MVT::i32, Expand); |
| 149 | setOperationAction(ISD::MULHS, MVT::i32, Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 150 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 151 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 152 | } else { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 153 | setOperationAction(ISD::MUL, MVT::i64, Expand); |
| 154 | setOperationAction(ISD::MULHU, MVT::i32, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 155 | if (!Subtarget->hasV6Ops()) |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 156 | setOperationAction(ISD::MULHS, MVT::i32, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 157 | } |
| 158 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); |
| 159 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); |
| 160 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); |
| 161 | setOperationAction(ISD::SRL, MVT::i64, Custom); |
| 162 | setOperationAction(ISD::SRA, MVT::i64, Custom); |
| 163 | |
| 164 | // ARM does not have ROTL. |
| 165 | setOperationAction(ISD::ROTL, MVT::i32, Expand); |
| 166 | setOperationAction(ISD::CTTZ , MVT::i32, Expand); |
| 167 | setOperationAction(ISD::CTPOP, MVT::i32, Expand); |
Evan Cheng | b063615 | 2007-02-01 23:34:03 +0000 | [diff] [blame] | 168 | if (!Subtarget->hasV5TOps() || Subtarget->isThumb()) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 169 | setOperationAction(ISD::CTLZ, MVT::i32, Expand); |
| 170 | |
Lauro Ramos Venancio | 368f20f | 2007-03-16 22:54:16 +0000 | [diff] [blame] | 171 | // Only ARMv6 has BSWAP. |
| 172 | if (!Subtarget->hasV6Ops()) |
Chris Lattner | 1719e13 | 2007-03-20 02:25:53 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::BSWAP, MVT::i32, Expand); |
Lauro Ramos Venancio | 368f20f | 2007-03-16 22:54:16 +0000 | [diff] [blame] | 174 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 175 | // These are expanded into libcalls. |
| 176 | setOperationAction(ISD::SDIV, MVT::i32, Expand); |
| 177 | setOperationAction(ISD::UDIV, MVT::i32, Expand); |
| 178 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 179 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 180 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 181 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 182 | |
| 183 | // Support label based line numbers. |
| 184 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
| 185 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 186 | |
| 187 | setOperationAction(ISD::RET, MVT::Other, Custom); |
| 188 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 189 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 190 | setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); |
Lauro Ramos Venancio | 64f4fa5 | 2007-04-27 13:54:47 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 192 | |
| 193 | // Expand mem operations genericly. |
| 194 | setOperationAction(ISD::MEMSET , MVT::Other, Expand); |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 195 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 196 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 197 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 198 | // Use the default implementation. |
| 199 | setOperationAction(ISD::VASTART , MVT::Other, Expand); |
| 200 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 201 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 202 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
| 203 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 204 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 205 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); |
| 206 | |
| 207 | if (!Subtarget->hasV6Ops()) { |
| 208 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); |
| 209 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); |
| 210 | } |
| 211 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 212 | |
Evan Cheng | b6ab254 | 2007-01-31 08:40:13 +0000 | [diff] [blame] | 213 | if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 214 | // Turn f64->i64 into FMRRD iff target supports vfp2. |
| 215 | setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom); |
| 216 | |
| 217 | setOperationAction(ISD::SETCC , MVT::i32, Expand); |
| 218 | setOperationAction(ISD::SETCC , MVT::f32, Expand); |
| 219 | setOperationAction(ISD::SETCC , MVT::f64, Expand); |
| 220 | setOperationAction(ISD::SELECT , MVT::i32, Expand); |
| 221 | setOperationAction(ISD::SELECT , MVT::f32, Expand); |
| 222 | setOperationAction(ISD::SELECT , MVT::f64, Expand); |
| 223 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
| 224 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 225 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
| 226 | |
| 227 | setOperationAction(ISD::BRCOND , MVT::Other, Expand); |
| 228 | setOperationAction(ISD::BR_CC , MVT::i32, Custom); |
| 229 | setOperationAction(ISD::BR_CC , MVT::f32, Custom); |
| 230 | setOperationAction(ISD::BR_CC , MVT::f64, Custom); |
| 231 | setOperationAction(ISD::BR_JT , MVT::Other, Custom); |
| 232 | |
| 233 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 234 | setOperationAction(ISD::VACOPY, MVT::Other, Expand); |
| 235 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
| 236 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 237 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 238 | |
| 239 | // FP Constants can't be immediates. |
| 240 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 241 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
| 242 | |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 243 | // We don't support sin/cos/fmod/copysign/pow |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 244 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 245 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 246 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 247 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 248 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 249 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 250 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 251 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 252 | setOperationAction(ISD::FPOW , MVT::f64, Expand); |
| 253 | setOperationAction(ISD::FPOW , MVT::f32, Expand); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 254 | |
| 255 | // int <-> fp are custom expanded into bit_convert + ARMISD ops. |
| 256 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
| 257 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); |
| 258 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
| 259 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
| 260 | |
| 261 | setStackPointerRegisterToSaveRestore(ARM::SP); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 262 | setSchedulingPreference(SchedulingForRegPressure); |
Evan Cheng | 9f8cbd1 | 2007-05-18 00:19:34 +0000 | [diff] [blame] | 263 | setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10); |
Evan Cheng | 97e604e | 2007-06-19 23:55:02 +0000 | [diff] [blame] | 264 | setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2); |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 265 | |
| 266 | maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 271 | switch (Opcode) { |
| 272 | default: return 0; |
| 273 | case ARMISD::Wrapper: return "ARMISD::Wrapper"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 274 | case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; |
| 275 | case ARMISD::CALL: return "ARMISD::CALL"; |
Evan Cheng | 277f074 | 2007-06-19 21:05:09 +0000 | [diff] [blame] | 276 | case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 277 | case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; |
| 278 | case ARMISD::tCALL: return "ARMISD::tCALL"; |
| 279 | case ARMISD::BRCOND: return "ARMISD::BRCOND"; |
| 280 | case ARMISD::BR_JT: return "ARMISD::BR_JT"; |
| 281 | case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; |
| 282 | case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; |
| 283 | case ARMISD::CMP: return "ARMISD::CMP"; |
Lauro Ramos Venancio | 9996663 | 2007-04-02 01:30:03 +0000 | [diff] [blame] | 284 | case ARMISD::CMPNZ: return "ARMISD::CMPNZ"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 285 | case ARMISD::CMPFP: return "ARMISD::CMPFP"; |
| 286 | case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; |
| 287 | case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; |
| 288 | case ARMISD::CMOV: return "ARMISD::CMOV"; |
| 289 | case ARMISD::CNEG: return "ARMISD::CNEG"; |
| 290 | |
| 291 | case ARMISD::FTOSI: return "ARMISD::FTOSI"; |
| 292 | case ARMISD::FTOUI: return "ARMISD::FTOUI"; |
| 293 | case ARMISD::SITOF: return "ARMISD::SITOF"; |
| 294 | case ARMISD::UITOF: return "ARMISD::UITOF"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 295 | |
| 296 | case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; |
| 297 | case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; |
| 298 | case ARMISD::RRX: return "ARMISD::RRX"; |
| 299 | |
| 300 | case ARMISD::FMRRD: return "ARMISD::FMRRD"; |
| 301 | case ARMISD::FMDRR: return "ARMISD::FMDRR"; |
Lauro Ramos Venancio | 64f4fa5 | 2007-04-27 13:54:47 +0000 | [diff] [blame] | 302 | |
| 303 | case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | // Lowering Code |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
| 311 | |
| 312 | /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC |
| 313 | static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { |
| 314 | switch (CC) { |
| 315 | default: assert(0 && "Unknown condition code!"); |
| 316 | case ISD::SETNE: return ARMCC::NE; |
| 317 | case ISD::SETEQ: return ARMCC::EQ; |
| 318 | case ISD::SETGT: return ARMCC::GT; |
| 319 | case ISD::SETGE: return ARMCC::GE; |
| 320 | case ISD::SETLT: return ARMCC::LT; |
| 321 | case ISD::SETLE: return ARMCC::LE; |
| 322 | case ISD::SETUGT: return ARMCC::HI; |
| 323 | case ISD::SETUGE: return ARMCC::HS; |
| 324 | case ISD::SETULT: return ARMCC::LO; |
| 325 | case ISD::SETULE: return ARMCC::LS; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It |
| 330 | /// returns true if the operands should be inverted to form the proper |
| 331 | /// comparison. |
| 332 | static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, |
| 333 | ARMCC::CondCodes &CondCode2) { |
| 334 | bool Invert = false; |
| 335 | CondCode2 = ARMCC::AL; |
| 336 | switch (CC) { |
| 337 | default: assert(0 && "Unknown FP condition!"); |
| 338 | case ISD::SETEQ: |
| 339 | case ISD::SETOEQ: CondCode = ARMCC::EQ; break; |
| 340 | case ISD::SETGT: |
| 341 | case ISD::SETOGT: CondCode = ARMCC::GT; break; |
| 342 | case ISD::SETGE: |
| 343 | case ISD::SETOGE: CondCode = ARMCC::GE; break; |
| 344 | case ISD::SETOLT: CondCode = ARMCC::MI; break; |
| 345 | case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break; |
| 346 | case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; |
| 347 | case ISD::SETO: CondCode = ARMCC::VC; break; |
| 348 | case ISD::SETUO: CondCode = ARMCC::VS; break; |
| 349 | case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; |
| 350 | case ISD::SETUGT: CondCode = ARMCC::HI; break; |
| 351 | case ISD::SETUGE: CondCode = ARMCC::PL; break; |
| 352 | case ISD::SETLT: |
| 353 | case ISD::SETULT: CondCode = ARMCC::LT; break; |
| 354 | case ISD::SETLE: |
| 355 | case ISD::SETULE: CondCode = ARMCC::LE; break; |
| 356 | case ISD::SETNE: |
| 357 | case ISD::SETUNE: CondCode = ARMCC::NE; break; |
| 358 | } |
| 359 | return Invert; |
| 360 | } |
| 361 | |
| 362 | static void |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 363 | HowToPassArgument(MVT::ValueType ObjectVT, unsigned NumGPRs, |
| 364 | unsigned StackOffset, unsigned &NeededGPRs, |
| 365 | unsigned &NeededStackSize, unsigned &GPRPad, |
| 366 | unsigned &StackPad, unsigned Flags) { |
| 367 | NeededStackSize = 0; |
| 368 | NeededGPRs = 0; |
| 369 | StackPad = 0; |
| 370 | GPRPad = 0; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 371 | unsigned align = (Flags >> ISD::ParamFlags::OrigAlignmentOffs); |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 372 | GPRPad = NumGPRs % ((align + 3)/4); |
| 373 | StackPad = StackOffset % align; |
| 374 | unsigned firstGPR = NumGPRs + GPRPad; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 375 | switch (ObjectVT) { |
| 376 | default: assert(0 && "Unhandled argument type!"); |
| 377 | case MVT::i32: |
| 378 | case MVT::f32: |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 379 | if (firstGPR < 4) |
| 380 | NeededGPRs = 1; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 381 | else |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 382 | NeededStackSize = 4; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 383 | break; |
| 384 | case MVT::i64: |
| 385 | case MVT::f64: |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 386 | if (firstGPR < 3) |
| 387 | NeededGPRs = 2; |
| 388 | else if (firstGPR == 3) { |
| 389 | NeededGPRs = 1; |
| 390 | NeededStackSize = 4; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 391 | } else |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 392 | NeededStackSize = 8; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Evan Cheng | fc40342 | 2007-02-03 08:53:01 +0000 | [diff] [blame] | 396 | /// LowerCALL - Lowering a ISD::CALL node into a callseq_start <- |
| 397 | /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter |
| 398 | /// nodes. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 399 | SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
| 400 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 401 | SDOperand Chain = Op.getOperand(0); |
| 402 | unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 403 | assert((CallConv == CallingConv::C || |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 404 | CallConv == CallingConv::Fast) && "unknown calling convention"); |
| 405 | SDOperand Callee = Op.getOperand(4); |
| 406 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 407 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 408 | unsigned NumGPRs = 0; // GPRs used for parameter passing. |
| 409 | |
| 410 | // Count how many bytes are to be pushed on the stack. |
| 411 | unsigned NumBytes = 0; |
| 412 | |
| 413 | // Add up all the space actually used. |
| 414 | for (unsigned i = 0; i < NumOps; ++i) { |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 415 | unsigned ObjSize; |
| 416 | unsigned ObjGPRs; |
| 417 | unsigned StackPad; |
| 418 | unsigned GPRPad; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 419 | MVT::ValueType ObjectVT = Op.getOperand(5+2*i).getValueType(); |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 420 | unsigned Flags = Op.getConstantOperandVal(5+2*i+1); |
| 421 | HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize, |
| 422 | GPRPad, StackPad, Flags); |
| 423 | NumBytes += ObjSize + StackPad; |
| 424 | NumGPRs += ObjGPRs + GPRPad; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Adjust the stack pointer for the new arguments... |
| 428 | // These operations are automatically eliminated by the prolog/epilog pass |
| 429 | Chain = DAG.getCALLSEQ_START(Chain, |
| 430 | DAG.getConstant(NumBytes, MVT::i32)); |
| 431 | |
| 432 | SDOperand StackPtr = DAG.getRegister(ARM::SP, MVT::i32); |
| 433 | |
| 434 | static const unsigned GPRArgRegs[] = { |
| 435 | ARM::R0, ARM::R1, ARM::R2, ARM::R3 |
| 436 | }; |
| 437 | |
| 438 | NumGPRs = 0; |
| 439 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 440 | std::vector<SDOperand> MemOpChains; |
| 441 | for (unsigned i = 0; i != NumOps; ++i) { |
| 442 | SDOperand Arg = Op.getOperand(5+2*i); |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 443 | unsigned Flags = Op.getConstantOperandVal(5+2*i+1); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 444 | MVT::ValueType ArgVT = Arg.getValueType(); |
| 445 | |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 446 | unsigned ObjSize; |
| 447 | unsigned ObjGPRs; |
| 448 | unsigned GPRPad; |
| 449 | unsigned StackPad; |
| 450 | HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs, |
| 451 | ObjSize, GPRPad, StackPad, Flags); |
| 452 | NumGPRs += GPRPad; |
| 453 | ArgOffset += StackPad; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 454 | if (ObjGPRs > 0) { |
| 455 | switch (ArgVT) { |
| 456 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 457 | case MVT::i32: |
| 458 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg)); |
| 459 | break; |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 460 | case MVT::f32: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 461 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], |
| 462 | DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Arg))); |
| 463 | break; |
| 464 | case MVT::i64: { |
| 465 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg, |
| 466 | DAG.getConstant(0, getPointerTy())); |
| 467 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Arg, |
| 468 | DAG.getConstant(1, getPointerTy())); |
| 469 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo)); |
| 470 | if (ObjGPRs == 2) |
| 471 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi)); |
| 472 | else { |
| 473 | SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
| 474 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 475 | MemOpChains.push_back(DAG.getStore(Chain, Hi, PtrOff, NULL, 0)); |
| 476 | } |
| 477 | break; |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 478 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 479 | case MVT::f64: { |
| 480 | SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, |
| 481 | DAG.getVTList(MVT::i32, MVT::i32), |
| 482 | &Arg, 1); |
| 483 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt)); |
| 484 | if (ObjGPRs == 2) |
| 485 | RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], |
| 486 | Cvt.getValue(1))); |
| 487 | else { |
| 488 | SDOperand PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
| 489 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 490 | MemOpChains.push_back(DAG.getStore(Chain, Cvt.getValue(1), PtrOff, |
| 491 | NULL, 0)); |
| 492 | } |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | } else { |
| 497 | assert(ObjSize != 0); |
| 498 | SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
| 499 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 500 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
| 501 | } |
| 502 | |
| 503 | NumGPRs += ObjGPRs; |
| 504 | ArgOffset += ObjSize; |
| 505 | } |
| 506 | |
| 507 | if (!MemOpChains.empty()) |
| 508 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 509 | &MemOpChains[0], MemOpChains.size()); |
| 510 | |
| 511 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 512 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 513 | SDOperand InFlag; |
| 514 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 515 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 516 | InFlag); |
| 517 | InFlag = Chain.getValue(1); |
| 518 | } |
| 519 | |
| 520 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 521 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 522 | // node so that legalize doesn't hack it. |
| 523 | bool isDirect = false; |
| 524 | bool isARMFunc = false; |
Evan Cheng | 277f074 | 2007-06-19 21:05:09 +0000 | [diff] [blame] | 525 | bool isLocalARMFunc = false; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 526 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 527 | GlobalValue *GV = G->getGlobal(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 528 | isDirect = true; |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 529 | bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() || |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 530 | GV->hasLinkOnceLinkage()); |
Evan Cheng | 970a419 | 2007-01-19 19:28:01 +0000 | [diff] [blame] | 531 | bool isStub = (isExt && Subtarget->isTargetDarwin()) && |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 532 | getTargetMachine().getRelocationModel() != Reloc::Static; |
| 533 | isARMFunc = !Subtarget->isThumb() || isStub; |
Evan Cheng | 277f074 | 2007-06-19 21:05:09 +0000 | [diff] [blame] | 534 | // ARM call to a local ARM function is predicable. |
| 535 | isLocalARMFunc = !Subtarget->isThumb() && !isExt; |
Evan Cheng | c60e76d | 2007-01-30 20:37:08 +0000 | [diff] [blame] | 536 | // tBX takes a register source operand. |
| 537 | if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) { |
| 538 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex, |
| 539 | ARMCP::CPStub, 4); |
| 540 | SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2); |
| 541 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 542 | Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0); |
| 543 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 544 | Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel); |
| 545 | } else |
| 546 | Callee = DAG.getTargetGlobalAddress(GV, getPointerTy()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 547 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 548 | isDirect = true; |
Evan Cheng | 970a419 | 2007-01-19 19:28:01 +0000 | [diff] [blame] | 549 | bool isStub = Subtarget->isTargetDarwin() && |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 550 | getTargetMachine().getRelocationModel() != Reloc::Static; |
| 551 | isARMFunc = !Subtarget->isThumb() || isStub; |
Evan Cheng | c60e76d | 2007-01-30 20:37:08 +0000 | [diff] [blame] | 552 | // tBX takes a register source operand. |
| 553 | const char *Sym = S->getSymbol(); |
| 554 | if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) { |
| 555 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex, |
| 556 | ARMCP::CPStub, 4); |
| 557 | SDOperand CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2); |
| 558 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 559 | Callee = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), CPAddr, NULL, 0); |
| 560 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 561 | Callee = DAG.getNode(ARMISD::PIC_ADD, getPointerTy(), Callee, PICLabel); |
| 562 | } else |
| 563 | Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Lauro Ramos Venancio | 64c88d7 | 2007-03-20 17:57:23 +0000 | [diff] [blame] | 566 | // FIXME: handle tail calls differently. |
| 567 | unsigned CallOpc; |
| 568 | if (Subtarget->isThumb()) { |
| 569 | if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc)) |
| 570 | CallOpc = ARMISD::CALL_NOLINK; |
| 571 | else |
| 572 | CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; |
| 573 | } else { |
| 574 | CallOpc = (isDirect || Subtarget->hasV5TOps()) |
Evan Cheng | 277f074 | 2007-06-19 21:05:09 +0000 | [diff] [blame] | 575 | ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL) |
| 576 | : ARMISD::CALL_NOLINK; |
Lauro Ramos Venancio | 64c88d7 | 2007-03-20 17:57:23 +0000 | [diff] [blame] | 577 | } |
Lauro Ramos Venancio | b8a93a4 | 2007-03-27 16:19:21 +0000 | [diff] [blame] | 578 | if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) { |
| 579 | // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK |
Lauro Ramos Venancio | 64c88d7 | 2007-03-20 17:57:23 +0000 | [diff] [blame] | 580 | Chain = DAG.getCopyToReg(Chain, ARM::LR, |
Lauro Ramos Venancio | b8a93a4 | 2007-03-27 16:19:21 +0000 | [diff] [blame] | 581 | DAG.getNode(ISD::UNDEF, MVT::i32), InFlag); |
Lauro Ramos Venancio | 64c88d7 | 2007-03-20 17:57:23 +0000 | [diff] [blame] | 582 | InFlag = Chain.getValue(1); |
| 583 | } |
| 584 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 585 | std::vector<MVT::ValueType> NodeTys; |
| 586 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 587 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 588 | |
| 589 | std::vector<SDOperand> Ops; |
| 590 | Ops.push_back(Chain); |
| 591 | Ops.push_back(Callee); |
| 592 | |
| 593 | // Add argument registers to the end of the list so that they are known live |
| 594 | // into the call. |
| 595 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 596 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 597 | RegsToPass[i].second.getValueType())); |
| 598 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 599 | if (InFlag.Val) |
| 600 | Ops.push_back(InFlag); |
| 601 | Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size()); |
| 602 | InFlag = Chain.getValue(1); |
| 603 | |
| 604 | SDOperand CSOps[] = { Chain, DAG.getConstant(NumBytes, MVT::i32), InFlag }; |
| 605 | Chain = DAG.getNode(ISD::CALLSEQ_END, |
| 606 | DAG.getNodeValueTypes(MVT::Other, MVT::Flag), |
| 607 | ((RetVT != MVT::Other) ? 2 : 1), CSOps, 3); |
| 608 | if (RetVT != MVT::Other) |
| 609 | InFlag = Chain.getValue(1); |
| 610 | |
| 611 | std::vector<SDOperand> ResultVals; |
| 612 | NodeTys.clear(); |
| 613 | |
| 614 | // If the call has results, copy the values out of the ret val registers. |
| 615 | switch (RetVT) { |
| 616 | default: assert(0 && "Unexpected ret value!"); |
| 617 | case MVT::Other: |
| 618 | break; |
| 619 | case MVT::i32: |
| 620 | Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1); |
| 621 | ResultVals.push_back(Chain.getValue(0)); |
| 622 | if (Op.Val->getValueType(1) == MVT::i32) { |
| 623 | // Returns a i64 value. |
| 624 | Chain = DAG.getCopyFromReg(Chain, ARM::R1, MVT::i32, |
| 625 | Chain.getValue(2)).getValue(1); |
| 626 | ResultVals.push_back(Chain.getValue(0)); |
| 627 | NodeTys.push_back(MVT::i32); |
| 628 | } |
| 629 | NodeTys.push_back(MVT::i32); |
| 630 | break; |
| 631 | case MVT::f32: |
| 632 | Chain = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag).getValue(1); |
| 633 | ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, MVT::f32, |
| 634 | Chain.getValue(0))); |
| 635 | NodeTys.push_back(MVT::f32); |
| 636 | break; |
| 637 | case MVT::f64: { |
| 638 | SDOperand Lo = DAG.getCopyFromReg(Chain, ARM::R0, MVT::i32, InFlag); |
| 639 | SDOperand Hi = DAG.getCopyFromReg(Lo, ARM::R1, MVT::i32, Lo.getValue(2)); |
| 640 | ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi)); |
| 641 | NodeTys.push_back(MVT::f64); |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | NodeTys.push_back(MVT::Other); |
| 647 | |
| 648 | if (ResultVals.empty()) |
| 649 | return Chain; |
| 650 | |
| 651 | ResultVals.push_back(Chain); |
| 652 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, &ResultVals[0], |
| 653 | ResultVals.size()); |
| 654 | return Res.getValue(Op.ResNo); |
| 655 | } |
| 656 | |
| 657 | static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 658 | SDOperand Copy; |
| 659 | SDOperand Chain = Op.getOperand(0); |
| 660 | switch(Op.getNumOperands()) { |
| 661 | default: |
| 662 | assert(0 && "Do not know how to return this many arguments!"); |
| 663 | abort(); |
| 664 | case 1: { |
| 665 | SDOperand LR = DAG.getRegister(ARM::LR, MVT::i32); |
| 666 | return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain); |
| 667 | } |
| 668 | case 3: |
| 669 | Op = Op.getOperand(1); |
| 670 | if (Op.getValueType() == MVT::f32) { |
| 671 | Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op); |
| 672 | } else if (Op.getValueType() == MVT::f64) { |
Chris Lattner | 65a3323 | 2007-10-18 06:17:07 +0000 | [diff] [blame] | 673 | // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is |
| 674 | // available. |
| 675 | Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1); |
| 676 | SDOperand Sign = DAG.getConstant(0, MVT::i32); |
| 677 | return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign, |
| 678 | Op.getValue(1), Sign); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 679 | } |
| 680 | Copy = DAG.getCopyToReg(Chain, ARM::R0, Op, SDOperand()); |
| 681 | if (DAG.getMachineFunction().liveout_empty()) |
| 682 | DAG.getMachineFunction().addLiveOut(ARM::R0); |
| 683 | break; |
| 684 | case 5: |
| 685 | Copy = DAG.getCopyToReg(Chain, ARM::R1, Op.getOperand(3), SDOperand()); |
| 686 | Copy = DAG.getCopyToReg(Copy, ARM::R0, Op.getOperand(1), Copy.getValue(1)); |
| 687 | // If we haven't noted the R0+R1 are live out, do so now. |
| 688 | if (DAG.getMachineFunction().liveout_empty()) { |
| 689 | DAG.getMachineFunction().addLiveOut(ARM::R0); |
| 690 | DAG.getMachineFunction().addLiveOut(ARM::R1); |
| 691 | } |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag |
| 696 | return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1)); |
| 697 | } |
| 698 | |
| 699 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 700 | // their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is |
| 701 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 702 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 703 | // be used to form addressing mode. These wrapped nodes will be selected |
Evan Cheng | 9f6636f | 2007-03-19 07:48:02 +0000 | [diff] [blame] | 704 | // into MOVi. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 705 | static SDOperand LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { |
| 706 | MVT::ValueType PtrVT = Op.getValueType(); |
| 707 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| 708 | SDOperand Res; |
| 709 | if (CP->isMachineConstantPoolEntry()) |
| 710 | Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, |
| 711 | CP->getAlignment()); |
| 712 | else |
| 713 | Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, |
| 714 | CP->getAlignment()); |
| 715 | return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res); |
| 716 | } |
| 717 | |
Lauro Ramos Venancio | 64f4fa5 | 2007-04-27 13:54:47 +0000 | [diff] [blame] | 718 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model |
| 719 | SDOperand |
| 720 | ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, |
| 721 | SelectionDAG &DAG) { |
| 722 | MVT::ValueType PtrVT = getPointerTy(); |
| 723 | unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; |
| 724 | ARMConstantPoolValue *CPV = |
| 725 | new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue, |
| 726 | PCAdj, "tlsgd", true); |
| 727 | SDOperand Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 728 | Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument); |
| 729 | Argument = DAG.getLoad(PtrVT, DAG.getEntryNode(), Argument, NULL, 0); |
| 730 | SDOperand Chain = Argument.getValue(1); |
| 731 | |
| 732 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 733 | Argument = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Argument, PICLabel); |
| 734 | |
| 735 | // call __tls_get_addr. |
| 736 | ArgListTy Args; |
| 737 | ArgListEntry Entry; |
| 738 | Entry.Node = Argument; |
| 739 | Entry.Ty = (const Type *) Type::Int32Ty; |
| 740 | Args.push_back(Entry); |
| 741 | std::pair<SDOperand, SDOperand> CallResult = |
| 742 | LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, |
| 743 | CallingConv::C, false, |
| 744 | DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG); |
| 745 | return CallResult.first; |
| 746 | } |
| 747 | |
| 748 | // Lower ISD::GlobalTLSAddress using the "initial exec" or |
| 749 | // "local exec" model. |
| 750 | SDOperand |
| 751 | ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, |
| 752 | SelectionDAG &DAG) { |
| 753 | GlobalValue *GV = GA->getGlobal(); |
| 754 | SDOperand Offset; |
| 755 | SDOperand Chain = DAG.getEntryNode(); |
| 756 | MVT::ValueType PtrVT = getPointerTy(); |
| 757 | // Get the Thread Pointer |
| 758 | SDOperand ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, PtrVT); |
| 759 | |
| 760 | if (GV->isDeclaration()){ |
| 761 | // initial exec model |
| 762 | unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; |
| 763 | ARMConstantPoolValue *CPV = |
| 764 | new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue, |
| 765 | PCAdj, "gottpoff", true); |
| 766 | Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 767 | Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset); |
| 768 | Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0); |
| 769 | Chain = Offset.getValue(1); |
| 770 | |
| 771 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 772 | Offset = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Offset, PICLabel); |
| 773 | |
| 774 | Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0); |
| 775 | } else { |
| 776 | // local exec model |
| 777 | ARMConstantPoolValue *CPV = |
| 778 | new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff"); |
| 779 | Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 780 | Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset); |
| 781 | Offset = DAG.getLoad(PtrVT, Chain, Offset, NULL, 0); |
| 782 | } |
| 783 | |
| 784 | // The address of the thread local variable is the add of the thread |
| 785 | // pointer with the offset of the variable. |
| 786 | return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset); |
| 787 | } |
| 788 | |
| 789 | SDOperand |
| 790 | ARMTargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) { |
| 791 | // TODO: implement the "local dynamic" model |
| 792 | assert(Subtarget->isTargetELF() && |
| 793 | "TLS not implemented for non-ELF targets"); |
| 794 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
| 795 | // If the relocation model is PIC, use the "General Dynamic" TLS Model, |
| 796 | // otherwise use the "Local Exec" TLS Model |
| 797 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_) |
| 798 | return LowerToTLSGeneralDynamicModel(GA, DAG); |
| 799 | else |
| 800 | return LowerToTLSExecModels(GA, DAG); |
| 801 | } |
| 802 | |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 803 | SDOperand ARMTargetLowering::LowerGlobalAddressELF(SDOperand Op, |
| 804 | SelectionDAG &DAG) { |
| 805 | MVT::ValueType PtrVT = getPointerTy(); |
| 806 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 807 | Reloc::Model RelocM = getTargetMachine().getRelocationModel(); |
| 808 | if (RelocM == Reloc::PIC_) { |
Lauro Ramos Venancio | 5d3d44a | 2007-05-14 23:20:21 +0000 | [diff] [blame] | 809 | bool UseGOTOFF = GV->hasInternalLinkage() || GV->hasHiddenVisibility(); |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 810 | ARMConstantPoolValue *CPV = |
| 811 | new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT"); |
| 812 | SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 813 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 814 | SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0); |
| 815 | SDOperand Chain = Result.getValue(1); |
| 816 | SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT); |
| 817 | Result = DAG.getNode(ISD::ADD, PtrVT, Result, GOT); |
| 818 | if (!UseGOTOFF) |
| 819 | Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0); |
| 820 | return Result; |
| 821 | } else { |
| 822 | SDOperand CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2); |
| 823 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 824 | return DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0); |
| 825 | } |
| 826 | } |
| 827 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 828 | /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol |
Evan Cheng | 97c9bb5 | 2007-05-04 00:26:58 +0000 | [diff] [blame] | 829 | /// even in non-static mode. |
| 830 | static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) { |
| 831 | return RelocM != Reloc::Static && |
| 832 | (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 833 | (GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode())); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 836 | SDOperand ARMTargetLowering::LowerGlobalAddressDarwin(SDOperand Op, |
| 837 | SelectionDAG &DAG) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 838 | MVT::ValueType PtrVT = getPointerTy(); |
| 839 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 840 | Reloc::Model RelocM = getTargetMachine().getRelocationModel(); |
Evan Cheng | 97c9bb5 | 2007-05-04 00:26:58 +0000 | [diff] [blame] | 841 | bool IsIndirect = GVIsIndirectSymbol(GV, RelocM); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 842 | SDOperand CPAddr; |
| 843 | if (RelocM == Reloc::Static) |
| 844 | CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2); |
| 845 | else { |
| 846 | unsigned PCAdj = (RelocM != Reloc::PIC_) |
| 847 | ? 0 : (Subtarget->isThumb() ? 4 : 8); |
Evan Cheng | c60e76d | 2007-01-30 20:37:08 +0000 | [diff] [blame] | 848 | ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr |
| 849 | : ARMCP::CPValue; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 850 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex, |
Evan Cheng | c60e76d | 2007-01-30 20:37:08 +0000 | [diff] [blame] | 851 | Kind, PCAdj); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 852 | CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 853 | } |
| 854 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 855 | |
| 856 | SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0); |
| 857 | SDOperand Chain = Result.getValue(1); |
| 858 | |
| 859 | if (RelocM == Reloc::PIC_) { |
| 860 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 861 | Result = DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel); |
| 862 | } |
| 863 | if (IsIndirect) |
| 864 | Result = DAG.getLoad(PtrVT, Chain, Result, NULL, 0); |
| 865 | |
| 866 | return Result; |
| 867 | } |
| 868 | |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 869 | SDOperand ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDOperand Op, |
| 870 | SelectionDAG &DAG){ |
| 871 | assert(Subtarget->isTargetELF() && |
| 872 | "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); |
| 873 | MVT::ValueType PtrVT = getPointerTy(); |
| 874 | unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; |
| 875 | ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_", |
| 876 | ARMPCLabelIndex, |
| 877 | ARMCP::CPValue, PCAdj); |
| 878 | SDOperand CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2); |
| 879 | CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr); |
| 880 | SDOperand Result = DAG.getLoad(PtrVT, DAG.getEntryNode(), CPAddr, NULL, 0); |
| 881 | SDOperand PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32); |
| 882 | return DAG.getNode(ARMISD::PIC_ADD, PtrVT, Result, PICLabel); |
| 883 | } |
| 884 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 885 | static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG, |
| 886 | unsigned VarArgsFrameIndex) { |
| 887 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 888 | // memory location argument. |
| 889 | MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 890 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT); |
| 891 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2)); |
| 892 | return DAG.getStore(Op.getOperand(0), FR, Op.getOperand(1), SV->getValue(), |
| 893 | SV->getOffset()); |
| 894 | } |
| 895 | |
| 896 | static SDOperand LowerFORMAL_ARGUMENT(SDOperand Op, SelectionDAG &DAG, |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 897 | unsigned *vRegs, unsigned ArgNo, |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 898 | unsigned &NumGPRs, unsigned &ArgOffset) { |
| 899 | MachineFunction &MF = DAG.getMachineFunction(); |
| 900 | MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType(); |
| 901 | SDOperand Root = Op.getOperand(0); |
| 902 | std::vector<SDOperand> ArgValues; |
| 903 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 904 | |
| 905 | static const unsigned GPRArgRegs[] = { |
| 906 | ARM::R0, ARM::R1, ARM::R2, ARM::R3 |
| 907 | }; |
| 908 | |
Lauro Ramos Venancio | 876eaf1 | 2007-02-13 14:07:13 +0000 | [diff] [blame] | 909 | unsigned ObjSize; |
| 910 | unsigned ObjGPRs; |
| 911 | unsigned GPRPad; |
| 912 | unsigned StackPad; |
| 913 | unsigned Flags = Op.getConstantOperandVal(ArgNo + 3); |
| 914 | HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs, |
| 915 | ObjSize, GPRPad, StackPad, Flags); |
| 916 | NumGPRs += GPRPad; |
| 917 | ArgOffset += StackPad; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 918 | |
| 919 | SDOperand ArgValue; |
| 920 | if (ObjGPRs == 1) { |
| 921 | unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass); |
| 922 | MF.addLiveIn(GPRArgRegs[NumGPRs], VReg); |
| 923 | vRegs[NumGPRs] = VReg; |
| 924 | ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32); |
| 925 | if (ObjectVT == MVT::f32) |
| 926 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, ArgValue); |
| 927 | } else if (ObjGPRs == 2) { |
| 928 | unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass); |
| 929 | MF.addLiveIn(GPRArgRegs[NumGPRs], VReg); |
| 930 | vRegs[NumGPRs] = VReg; |
| 931 | ArgValue = DAG.getCopyFromReg(Root, VReg, MVT::i32); |
| 932 | |
| 933 | VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass); |
| 934 | MF.addLiveIn(GPRArgRegs[NumGPRs+1], VReg); |
| 935 | vRegs[NumGPRs+1] = VReg; |
| 936 | SDOperand ArgValue2 = DAG.getCopyFromReg(Root, VReg, MVT::i32); |
| 937 | |
| 938 | if (ObjectVT == MVT::i64) |
| 939 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2); |
| 940 | else |
| 941 | ArgValue = DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2); |
| 942 | } |
| 943 | NumGPRs += ObjGPRs; |
| 944 | |
| 945 | if (ObjSize) { |
| 946 | // If the argument is actually used, emit a load from the right stack |
| 947 | // slot. |
| 948 | if (!Op.Val->hasNUsesOfValue(0, ArgNo)) { |
| 949 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 950 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 951 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 952 | if (ObjGPRs == 0) |
| 953 | ArgValue = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0); |
| 954 | else { |
| 955 | SDOperand ArgValue2 = |
| 956 | DAG.getLoad(MVT::i32, Root, FIN, NULL, 0); |
| 957 | if (ObjectVT == MVT::i64) |
| 958 | ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2); |
| 959 | else |
| 960 | ArgValue= DAG.getNode(ARMISD::FMDRR, MVT::f64, ArgValue, ArgValue2); |
| 961 | } |
| 962 | } else { |
| 963 | // Don't emit a dead load. |
| 964 | ArgValue = DAG.getNode(ISD::UNDEF, ObjectVT); |
| 965 | } |
| 966 | |
| 967 | ArgOffset += ObjSize; // Move on to the next argument. |
| 968 | } |
| 969 | |
| 970 | return ArgValue; |
| 971 | } |
| 972 | |
| 973 | SDOperand |
| 974 | ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
| 975 | std::vector<SDOperand> ArgValues; |
| 976 | SDOperand Root = Op.getOperand(0); |
| 977 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 978 | unsigned NumGPRs = 0; // GPRs used for parameter passing. |
| 979 | unsigned VRegs[4]; |
| 980 | |
| 981 | unsigned NumArgs = Op.Val->getNumValues()-1; |
| 982 | for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) |
| 983 | ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, VRegs, ArgNo, |
| 984 | NumGPRs, ArgOffset)); |
| 985 | |
| 986 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 987 | if (isVarArg) { |
| 988 | static const unsigned GPRArgRegs[] = { |
| 989 | ARM::R0, ARM::R1, ARM::R2, ARM::R3 |
| 990 | }; |
| 991 | |
| 992 | MachineFunction &MF = DAG.getMachineFunction(); |
| 993 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 994 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 995 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Lauro Ramos Venancio | 600c383 | 2007-02-23 20:32:57 +0000 | [diff] [blame] | 996 | unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 997 | unsigned VARegSize = (4 - NumGPRs) * 4; |
| 998 | unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 999 | if (VARegSaveSize) { |
| 1000 | // If this function is vararg, store any remaining integer argument regs |
| 1001 | // to their spots on the stack so that they may be loaded by deferencing |
| 1002 | // the result of va_next. |
| 1003 | AFI->setVarArgsRegSaveSize(VARegSaveSize); |
Lauro Ramos Venancio | 600c383 | 2007-02-23 20:32:57 +0000 | [diff] [blame] | 1004 | VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset + |
| 1005 | VARegSaveSize - VARegSize); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1006 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
| 1007 | |
| 1008 | SmallVector<SDOperand, 4> MemOps; |
| 1009 | for (; NumGPRs < 4; ++NumGPRs) { |
| 1010 | unsigned VReg = RegMap->createVirtualRegister(&ARM::GPRRegClass); |
| 1011 | MF.addLiveIn(GPRArgRegs[NumGPRs], VReg); |
| 1012 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i32); |
| 1013 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
| 1014 | MemOps.push_back(Store); |
| 1015 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 1016 | DAG.getConstant(4, getPointerTy())); |
| 1017 | } |
| 1018 | if (!MemOps.empty()) |
| 1019 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1020 | &MemOps[0], MemOps.size()); |
| 1021 | } else |
| 1022 | // This will point to the next argument passed via stack. |
| 1023 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 1024 | } |
| 1025 | |
| 1026 | ArgValues.push_back(Root); |
| 1027 | |
| 1028 | // Return the new list of results. |
| 1029 | std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(), |
| 1030 | Op.Val->value_end()); |
| 1031 | return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size()); |
| 1032 | } |
| 1033 | |
| 1034 | /// isFloatingPointZero - Return true if this is +0.0. |
| 1035 | static bool isFloatingPointZero(SDOperand Op) { |
| 1036 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1037 | return CFP->getValueAPF().isPosZero(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1038 | else if (ISD::isEXTLoad(Op.Val) || ISD::isNON_EXTLoad(Op.Val)) { |
| 1039 | // Maybe this has already been legalized into the constant pool? |
| 1040 | if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { |
| 1041 | SDOperand WrapperOp = Op.getOperand(1).getOperand(0); |
| 1042 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) |
| 1043 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1044 | return CFP->getValueAPF().isPosZero(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
Evan Cheng | 9a2ef95 | 2007-02-02 01:53:26 +0000 | [diff] [blame] | 1050 | static bool isLegalCmpImmediate(unsigned C, bool isThumb) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1051 | return ( isThumb && (C & ~255U) == 0) || |
| 1052 | (!isThumb && ARM_AM::getSOImmVal(C) != -1); |
| 1053 | } |
| 1054 | |
| 1055 | /// Returns appropriate ARM CMP (cmp) and corresponding condition code for |
| 1056 | /// the given operands. |
| 1057 | static SDOperand getARMCmp(SDOperand LHS, SDOperand RHS, ISD::CondCode CC, |
| 1058 | SDOperand &ARMCC, SelectionDAG &DAG, bool isThumb) { |
| 1059 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.Val)) { |
Evan Cheng | 9a2ef95 | 2007-02-02 01:53:26 +0000 | [diff] [blame] | 1060 | unsigned C = RHSC->getValue(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1061 | if (!isLegalCmpImmediate(C, isThumb)) { |
| 1062 | // Constant does not fit, try adjusting it by one? |
| 1063 | switch (CC) { |
| 1064 | default: break; |
| 1065 | case ISD::SETLT: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1066 | case ISD::SETGE: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1067 | if (isLegalCmpImmediate(C-1, isThumb)) { |
Evan Cheng | 9a2ef95 | 2007-02-02 01:53:26 +0000 | [diff] [blame] | 1068 | CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; |
| 1069 | RHS = DAG.getConstant(C-1, MVT::i32); |
| 1070 | } |
| 1071 | break; |
| 1072 | case ISD::SETULT: |
| 1073 | case ISD::SETUGE: |
| 1074 | if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) { |
| 1075 | CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1076 | RHS = DAG.getConstant(C-1, MVT::i32); |
| 1077 | } |
| 1078 | break; |
| 1079 | case ISD::SETLE: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1080 | case ISD::SETGT: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1081 | if (isLegalCmpImmediate(C+1, isThumb)) { |
Evan Cheng | 9a2ef95 | 2007-02-02 01:53:26 +0000 | [diff] [blame] | 1082 | CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; |
| 1083 | RHS = DAG.getConstant(C+1, MVT::i32); |
| 1084 | } |
| 1085 | break; |
| 1086 | case ISD::SETULE: |
| 1087 | case ISD::SETUGT: |
| 1088 | if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) { |
| 1089 | CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1090 | RHS = DAG.getConstant(C+1, MVT::i32); |
| 1091 | } |
| 1092 | break; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | ARMCC::CondCodes CondCode = IntCCToARMCC(CC); |
Lauro Ramos Venancio | 9996663 | 2007-04-02 01:30:03 +0000 | [diff] [blame] | 1098 | ARMISD::NodeType CompareType; |
| 1099 | switch (CondCode) { |
| 1100 | default: |
| 1101 | CompareType = ARMISD::CMP; |
| 1102 | break; |
| 1103 | case ARMCC::EQ: |
| 1104 | case ARMCC::NE: |
| 1105 | case ARMCC::MI: |
| 1106 | case ARMCC::PL: |
| 1107 | // Uses only N and Z Flags |
| 1108 | CompareType = ARMISD::CMPNZ; |
| 1109 | break; |
| 1110 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1111 | ARMCC = DAG.getConstant(CondCode, MVT::i32); |
Lauro Ramos Venancio | 9996663 | 2007-04-02 01:30:03 +0000 | [diff] [blame] | 1112 | return DAG.getNode(CompareType, MVT::Flag, LHS, RHS); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. |
| 1116 | static SDOperand getVFPCmp(SDOperand LHS, SDOperand RHS, SelectionDAG &DAG) { |
| 1117 | SDOperand Cmp; |
| 1118 | if (!isFloatingPointZero(RHS)) |
| 1119 | Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS); |
| 1120 | else |
| 1121 | Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS); |
| 1122 | return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp); |
| 1123 | } |
| 1124 | |
| 1125 | static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG, |
| 1126 | const ARMSubtarget *ST) { |
| 1127 | MVT::ValueType VT = Op.getValueType(); |
| 1128 | SDOperand LHS = Op.getOperand(0); |
| 1129 | SDOperand RHS = Op.getOperand(1); |
| 1130 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
| 1131 | SDOperand TrueVal = Op.getOperand(2); |
| 1132 | SDOperand FalseVal = Op.getOperand(3); |
| 1133 | |
| 1134 | if (LHS.getValueType() == MVT::i32) { |
| 1135 | SDOperand ARMCC; |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1136 | SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1137 | SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb()); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1138 | return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | ARMCC::CondCodes CondCode, CondCode2; |
| 1142 | if (FPCCToARMCC(CC, CondCode, CondCode2)) |
| 1143 | std::swap(TrueVal, FalseVal); |
| 1144 | |
| 1145 | SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1146 | SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1147 | SDOperand Cmp = getVFPCmp(LHS, RHS, DAG); |
| 1148 | SDOperand Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1149 | ARMCC, CCR, Cmp); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1150 | if (CondCode2 != ARMCC::AL) { |
| 1151 | SDOperand ARMCC2 = DAG.getConstant(CondCode2, MVT::i32); |
| 1152 | // FIXME: Needs another CMP because flag can have but one use. |
| 1153 | SDOperand Cmp2 = getVFPCmp(LHS, RHS, DAG); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1154 | Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1155 | } |
| 1156 | return Result; |
| 1157 | } |
| 1158 | |
| 1159 | static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG, |
| 1160 | const ARMSubtarget *ST) { |
| 1161 | SDOperand Chain = Op.getOperand(0); |
| 1162 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); |
| 1163 | SDOperand LHS = Op.getOperand(2); |
| 1164 | SDOperand RHS = Op.getOperand(3); |
| 1165 | SDOperand Dest = Op.getOperand(4); |
| 1166 | |
| 1167 | if (LHS.getValueType() == MVT::i32) { |
| 1168 | SDOperand ARMCC; |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1169 | SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1170 | SDOperand Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb()); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1171 | return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); |
| 1175 | ARMCC::CondCodes CondCode, CondCode2; |
| 1176 | if (FPCCToARMCC(CC, CondCode, CondCode2)) |
| 1177 | // Swap the LHS/RHS of the comparison if needed. |
| 1178 | std::swap(LHS, RHS); |
| 1179 | |
| 1180 | SDOperand Cmp = getVFPCmp(LHS, RHS, DAG); |
| 1181 | SDOperand ARMCC = DAG.getConstant(CondCode, MVT::i32); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1182 | SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1183 | SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1184 | SDOperand Ops[] = { Chain, Dest, ARMCC, CCR, Cmp }; |
| 1185 | SDOperand Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1186 | if (CondCode2 != ARMCC::AL) { |
| 1187 | ARMCC = DAG.getConstant(CondCode2, MVT::i32); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1188 | SDOperand Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) }; |
| 1189 | Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1190 | } |
| 1191 | return Res; |
| 1192 | } |
| 1193 | |
| 1194 | SDOperand ARMTargetLowering::LowerBR_JT(SDOperand Op, SelectionDAG &DAG) { |
| 1195 | SDOperand Chain = Op.getOperand(0); |
| 1196 | SDOperand Table = Op.getOperand(1); |
| 1197 | SDOperand Index = Op.getOperand(2); |
| 1198 | |
| 1199 | MVT::ValueType PTy = getPointerTy(); |
| 1200 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); |
| 1201 | ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); |
| 1202 | SDOperand UId = DAG.getConstant(AFI->createJumpTableUId(), PTy); |
| 1203 | SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); |
| 1204 | Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId); |
| 1205 | Index = DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(4, PTy)); |
| 1206 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
| 1207 | bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_; |
Evan Cheng | e2446c6 | 2007-06-26 18:31:22 +0000 | [diff] [blame] | 1208 | Addr = DAG.getLoad(isPIC ? (MVT::ValueType)MVT::i32 : PTy, |
| 1209 | Chain, Addr, NULL, 0); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1210 | Chain = Addr.getValue(1); |
| 1211 | if (isPIC) |
| 1212 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, Table); |
| 1213 | return DAG.getNode(ARMISD::BR_JT, MVT::Other, Chain, Addr, JTI, UId); |
| 1214 | } |
| 1215 | |
| 1216 | static SDOperand LowerFP_TO_INT(SDOperand Op, SelectionDAG &DAG) { |
| 1217 | unsigned Opc = |
| 1218 | Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI; |
| 1219 | Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0)); |
| 1220 | return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op); |
| 1221 | } |
| 1222 | |
| 1223 | static SDOperand LowerINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { |
| 1224 | MVT::ValueType VT = Op.getValueType(); |
| 1225 | unsigned Opc = |
| 1226 | Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF; |
| 1227 | |
| 1228 | Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0)); |
| 1229 | return DAG.getNode(Opc, VT, Op); |
| 1230 | } |
| 1231 | |
| 1232 | static SDOperand LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) { |
| 1233 | // Implement fcopysign with a fabs and a conditional fneg. |
| 1234 | SDOperand Tmp0 = Op.getOperand(0); |
| 1235 | SDOperand Tmp1 = Op.getOperand(1); |
| 1236 | MVT::ValueType VT = Op.getValueType(); |
| 1237 | MVT::ValueType SrcVT = Tmp1.getValueType(); |
| 1238 | SDOperand AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0); |
| 1239 | SDOperand Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG); |
| 1240 | SDOperand ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1241 | SDOperand CCR = DAG.getRegister(ARM::CPSR, MVT::i32); |
| 1242 | return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | static SDOperand LowerBIT_CONVERT(SDOperand Op, SelectionDAG &DAG) { |
| 1246 | // Turn f64->i64 into FMRRD. |
| 1247 | assert(Op.getValueType() == MVT::i64 && |
| 1248 | Op.getOperand(0).getValueType() == MVT::f64); |
| 1249 | |
| 1250 | Op = Op.getOperand(0); |
| 1251 | SDOperand Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), |
| 1252 | &Op, 1); |
| 1253 | |
| 1254 | // Merge the pieces into a single i64 value. |
| 1255 | return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1)); |
| 1256 | } |
| 1257 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1258 | static SDOperand LowerSRx(SDOperand Op, SelectionDAG &DAG, |
| 1259 | const ARMSubtarget *ST) { |
| 1260 | assert(Op.getValueType() == MVT::i64 && |
| 1261 | (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) && |
| 1262 | "Unknown shift to lower!"); |
| 1263 | |
| 1264 | // We only lower SRA, SRL of 1 here, all others use generic lowering. |
| 1265 | if (!isa<ConstantSDNode>(Op.getOperand(1)) || |
| 1266 | cast<ConstantSDNode>(Op.getOperand(1))->getValue() != 1) |
| 1267 | return SDOperand(); |
| 1268 | |
| 1269 | // If we are in thumb mode, we don't have RRX. |
| 1270 | if (ST->isThumb()) return SDOperand(); |
| 1271 | |
| 1272 | // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. |
| 1273 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 1274 | DAG.getConstant(0, MVT::i32)); |
| 1275 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 1276 | DAG.getConstant(1, MVT::i32)); |
| 1277 | |
| 1278 | // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and |
| 1279 | // captures the result into a carry flag. |
| 1280 | unsigned Opc = Op.getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; |
| 1281 | Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1); |
| 1282 | |
| 1283 | // The low part is an ARMISD::RRX operand, which shifts the carry in. |
| 1284 | Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1)); |
| 1285 | |
| 1286 | // Merge the pieces into a single i64 value. |
| 1287 | return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi); |
| 1288 | } |
| 1289 | |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1290 | SDOperand ARMTargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) { |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1291 | SDOperand ChainOp = Op.getOperand(0); |
| 1292 | SDOperand DestOp = Op.getOperand(1); |
| 1293 | SDOperand SourceOp = Op.getOperand(2); |
| 1294 | SDOperand CountOp = Op.getOperand(3); |
| 1295 | SDOperand AlignOp = Op.getOperand(4); |
| 1296 | SDOperand AlwaysInlineOp = Op.getOperand(5); |
| 1297 | |
| 1298 | bool AlwaysInline = (bool)cast<ConstantSDNode>(AlwaysInlineOp)->getValue(); |
| 1299 | unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue(); |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1300 | if (Align == 0) Align = 1; |
| 1301 | |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1302 | // If size is unknown, call memcpy. |
| 1303 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp); |
| 1304 | if (!I) { |
| 1305 | assert(!AlwaysInline && "Cannot inline copy of unknown size"); |
| 1306 | return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG); |
| 1307 | } |
| 1308 | unsigned Size = I->getValue(); |
| 1309 | |
| 1310 | if (AlwaysInline) |
| 1311 | return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG); |
| 1312 | |
| 1313 | // The libc version is likely to be faster for the following cases. It can |
| 1314 | // use the address value and run time information about the CPU. |
| 1315 | // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster |
| 1316 | |
| 1317 | // If not DWORD aligned, call memcpy. |
| 1318 | if ((Align & 3) != 0) |
| 1319 | return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG); |
| 1320 | |
| 1321 | // If size is more than the threshold, call memcpy. |
| 1322 | // if (Size > Subtarget->getMinRepStrSizeThreshold()) |
| 1323 | if (Size >= 64) |
| 1324 | return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG); |
| 1325 | |
| 1326 | return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG); |
| 1327 | } |
| 1328 | |
| 1329 | SDOperand ARMTargetLowering::LowerMEMCPYCall(SDOperand Chain, |
| 1330 | SDOperand Dest, |
| 1331 | SDOperand Source, |
| 1332 | SDOperand Count, |
| 1333 | SelectionDAG &DAG) { |
| 1334 | MVT::ValueType IntPtr = getPointerTy(); |
| 1335 | TargetLowering::ArgListTy Args; |
| 1336 | TargetLowering::ArgListEntry Entry; |
| 1337 | Entry.Ty = getTargetData()->getIntPtrType(); |
| 1338 | Entry.Node = Dest; Args.push_back(Entry); |
| 1339 | Entry.Node = Source; Args.push_back(Entry); |
| 1340 | Entry.Node = Count; Args.push_back(Entry); |
| 1341 | std::pair<SDOperand,SDOperand> CallResult = |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1342 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
| 1343 | DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1344 | return CallResult.second; |
| 1345 | } |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1346 | |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1347 | SDOperand ARMTargetLowering::LowerMEMCPYInline(SDOperand Chain, |
| 1348 | SDOperand Dest, |
| 1349 | SDOperand Source, |
| 1350 | unsigned Size, |
| 1351 | unsigned Align, |
| 1352 | SelectionDAG &DAG) { |
| 1353 | |
| 1354 | // Do repeated 4-byte loads and stores. To be improved. |
| 1355 | assert((Size& 3) == 0); |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1356 | assert((Align & 3) == 0); |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1357 | unsigned NumMemOps = Size >> 2; |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1358 | unsigned EmittedNumMemOps = 0; |
| 1359 | unsigned SrcOff = 0, DstOff = 0; |
| 1360 | MVT::ValueType VT = MVT::i32; |
| 1361 | unsigned VTSize = 4; |
Evan Cheng | e5e7ce4 | 2007-05-18 01:19:57 +0000 | [diff] [blame] | 1362 | const unsigned MAX_LOADS_IN_LDM = 6; |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1363 | SDOperand LoadChains[MAX_LOADS_IN_LDM]; |
| 1364 | SDOperand Loads[MAX_LOADS_IN_LDM]; |
| 1365 | |
| 1366 | // Emit up to 4 loads, then a TokenFactor barrier, then the same |
| 1367 | // number of stores. The loads and stores will get combined into |
| 1368 | // ldm/stm later on. |
| 1369 | while(EmittedNumMemOps < NumMemOps) { |
Evan Cheng | e5e7ce4 | 2007-05-18 01:19:57 +0000 | [diff] [blame] | 1370 | unsigned i; |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1371 | for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) { |
| 1372 | Loads[i] = DAG.getLoad(VT, Chain, |
Rafael Espindola | 7b73a5d | 2007-10-19 14:35:17 +0000 | [diff] [blame^] | 1373 | DAG.getNode(ISD::ADD, VT, Source, |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1374 | DAG.getConstant(SrcOff, VT)), |
| 1375 | NULL, 0); |
| 1376 | LoadChains[i] = Loads[i].getValue(1); |
| 1377 | SrcOff += VTSize; |
| 1378 | } |
| 1379 | |
| 1380 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &LoadChains[0], i); |
| 1381 | |
| 1382 | for (i=0; i<MAX_LOADS_IN_LDM && EmittedNumMemOps+i < NumMemOps; i++) { |
| 1383 | Chain = DAG.getStore(Chain, Loads[i], |
| 1384 | DAG.getNode(ISD::ADD, VT, Dest, |
| 1385 | DAG.getConstant(DstOff, VT)), |
| 1386 | NULL, 0); |
| 1387 | DstOff += VTSize; |
| 1388 | } |
| 1389 | EmittedNumMemOps += i; |
| 1390 | } |
| 1391 | |
| 1392 | return Chain; |
| 1393 | } |
| 1394 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1395 | SDOperand ARMTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 1396 | switch (Op.getOpcode()) { |
| 1397 | default: assert(0 && "Don't know how to custom lower this!"); abort(); |
| 1398 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 1399 | case ISD::GlobalAddress: |
| 1400 | return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) : |
| 1401 | LowerGlobalAddressELF(Op, DAG); |
Lauro Ramos Venancio | 64f4fa5 | 2007-04-27 13:54:47 +0000 | [diff] [blame] | 1402 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1403 | case ISD::CALL: return LowerCALL(Op, DAG); |
| 1404 | case ISD::RET: return LowerRET(Op, DAG); |
| 1405 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget); |
| 1406 | case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget); |
| 1407 | case ISD::BR_JT: return LowerBR_JT(Op, DAG); |
| 1408 | case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex); |
| 1409 | case ISD::SINT_TO_FP: |
| 1410 | case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); |
| 1411 | case ISD::FP_TO_SINT: |
| 1412 | case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); |
| 1413 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
| 1414 | case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1415 | case ISD::SRL: |
| 1416 | case ISD::SRA: return LowerSRx(Op, DAG, Subtarget); |
| 1417 | case ISD::FORMAL_ARGUMENTS: |
| 1418 | return LowerFORMAL_ARGUMENTS(Op, DAG); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 1419 | case ISD::RETURNADDR: break; |
| 1420 | case ISD::FRAMEADDR: break; |
Lauro Ramos Venancio | 0ae4a33 | 2007-04-22 00:04:12 +0000 | [diff] [blame] | 1421 | case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); |
Dale Johannesen | 8dd86c1 | 2007-05-17 21:31:21 +0000 | [diff] [blame] | 1422 | case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1423 | } |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 1424 | return SDOperand(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | //===----------------------------------------------------------------------===// |
| 1428 | // ARM Scheduler Hooks |
| 1429 | //===----------------------------------------------------------------------===// |
| 1430 | |
| 1431 | MachineBasicBlock * |
| 1432 | ARMTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1433 | MachineBasicBlock *BB) { |
| 1434 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 1435 | switch (MI->getOpcode()) { |
| 1436 | default: assert(false && "Unexpected instr type to insert"); |
| 1437 | case ARM::tMOVCCr: { |
| 1438 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 1439 | // diamond control-flow pattern. The incoming instruction knows the |
| 1440 | // destination vreg to set, the condition code register to branch on, the |
| 1441 | // true/false values to select between, and a branch opcode to use. |
| 1442 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1443 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1444 | ++It; |
| 1445 | |
| 1446 | // thisMBB: |
| 1447 | // ... |
| 1448 | // TrueVal = ... |
| 1449 | // cmpTY ccX, r1, r2 |
| 1450 | // bCC copy1MBB |
| 1451 | // fallthrough --> copy0MBB |
| 1452 | MachineBasicBlock *thisMBB = BB; |
| 1453 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1454 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1455 | BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB) |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1456 | .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1457 | MachineFunction *F = BB->getParent(); |
| 1458 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1459 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1460 | // Update machine-CFG edges by first adding all successors of the current |
| 1461 | // block to the new block which will contain the Phi node for the select. |
| 1462 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
| 1463 | e = BB->succ_end(); i != e; ++i) |
| 1464 | sinkMBB->addSuccessor(*i); |
| 1465 | // Next, remove all successors of the current block, and add the true |
| 1466 | // and fallthrough blocks as its successors. |
| 1467 | while(!BB->succ_empty()) |
| 1468 | BB->removeSuccessor(BB->succ_begin()); |
| 1469 | BB->addSuccessor(copy0MBB); |
| 1470 | BB->addSuccessor(sinkMBB); |
| 1471 | |
| 1472 | // copy0MBB: |
| 1473 | // %FalseValue = ... |
| 1474 | // # fallthrough to sinkMBB |
| 1475 | BB = copy0MBB; |
| 1476 | |
| 1477 | // Update machine-CFG edges |
| 1478 | BB->addSuccessor(sinkMBB); |
| 1479 | |
| 1480 | // sinkMBB: |
| 1481 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1482 | // ... |
| 1483 | BB = sinkMBB; |
| 1484 | BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg()) |
| 1485 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 1486 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 1487 | |
| 1488 | delete MI; // The pseudo instruction is gone now. |
| 1489 | return BB; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | //===----------------------------------------------------------------------===// |
| 1495 | // ARM Optimization Hooks |
| 1496 | //===----------------------------------------------------------------------===// |
| 1497 | |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1498 | /// isLegalAddressImmediate - Return true if the integer value can be used |
| 1499 | /// as the offset of the target addressing mode for load / store of the |
| 1500 | /// given type. |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1501 | static bool isLegalAddressImmediate(int64_t V, MVT::ValueType VT, |
| 1502 | const ARMSubtarget *Subtarget) { |
Evan Cheng | 961f879 | 2007-03-13 20:37:59 +0000 | [diff] [blame] | 1503 | if (V == 0) |
| 1504 | return true; |
| 1505 | |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1506 | if (Subtarget->isThumb()) { |
| 1507 | if (V < 0) |
| 1508 | return false; |
| 1509 | |
| 1510 | unsigned Scale = 1; |
| 1511 | switch (VT) { |
| 1512 | default: return false; |
| 1513 | case MVT::i1: |
| 1514 | case MVT::i8: |
| 1515 | // Scale == 1; |
| 1516 | break; |
| 1517 | case MVT::i16: |
| 1518 | // Scale == 2; |
| 1519 | Scale = 2; |
| 1520 | break; |
| 1521 | case MVT::i32: |
| 1522 | // Scale == 4; |
| 1523 | Scale = 4; |
| 1524 | break; |
| 1525 | } |
| 1526 | |
| 1527 | if ((V & (Scale - 1)) != 0) |
| 1528 | return false; |
| 1529 | V /= Scale; |
| 1530 | return V == V & ((1LL << 5) - 1); |
| 1531 | } |
| 1532 | |
| 1533 | if (V < 0) |
| 1534 | V = - V; |
| 1535 | switch (VT) { |
| 1536 | default: return false; |
| 1537 | case MVT::i1: |
| 1538 | case MVT::i8: |
| 1539 | case MVT::i32: |
| 1540 | // +- imm12 |
| 1541 | return V == V & ((1LL << 12) - 1); |
| 1542 | case MVT::i16: |
| 1543 | // +- imm8 |
| 1544 | return V == V & ((1LL << 8) - 1); |
| 1545 | case MVT::f32: |
| 1546 | case MVT::f64: |
| 1547 | if (!Subtarget->hasVFP2()) |
| 1548 | return false; |
Evan Cheng | 0b0a9a9 | 2007-05-03 02:00:18 +0000 | [diff] [blame] | 1549 | if ((V & 3) != 0) |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1550 | return false; |
| 1551 | V >>= 2; |
| 1552 | return V == V & ((1LL << 8) - 1); |
| 1553 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1556 | /// isLegalAddressingMode - Return true if the addressing mode represented |
| 1557 | /// by AM is legal for this target, for a load/store of the specified type. |
| 1558 | bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 1559 | const Type *Ty) const { |
| 1560 | if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty), Subtarget)) |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1561 | return false; |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1562 | |
| 1563 | // Can never fold addr of global into load/store. |
| 1564 | if (AM.BaseGV) |
| 1565 | return false; |
| 1566 | |
| 1567 | switch (AM.Scale) { |
| 1568 | case 0: // no scale reg, must be "r+i" or "r", or "i". |
| 1569 | break; |
| 1570 | case 1: |
| 1571 | if (Subtarget->isThumb()) |
| 1572 | return false; |
Chris Lattner | 5a3d40d | 2007-04-13 06:50:55 +0000 | [diff] [blame] | 1573 | // FALL THROUGH. |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1574 | default: |
Chris Lattner | 5a3d40d | 2007-04-13 06:50:55 +0000 | [diff] [blame] | 1575 | // ARM doesn't support any R+R*scale+imm addr modes. |
| 1576 | if (AM.BaseOffs) |
| 1577 | return false; |
| 1578 | |
Chris Lattner | eb13d1b | 2007-04-10 03:48:29 +0000 | [diff] [blame] | 1579 | int Scale = AM.Scale; |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1580 | switch (getValueType(Ty)) { |
| 1581 | default: return false; |
| 1582 | case MVT::i1: |
| 1583 | case MVT::i8: |
| 1584 | case MVT::i32: |
| 1585 | case MVT::i64: |
| 1586 | // This assumes i64 is legalized to a pair of i32. If not (i.e. |
| 1587 | // ldrd / strd are used, then its address mode is same as i16. |
| 1588 | // r + r |
Chris Lattner | eb13d1b | 2007-04-10 03:48:29 +0000 | [diff] [blame] | 1589 | if (Scale < 0) Scale = -Scale; |
| 1590 | if (Scale == 1) |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1591 | return true; |
| 1592 | // r + r << imm |
Chris Lattner | e115294 | 2007-04-11 16:17:12 +0000 | [diff] [blame] | 1593 | return isPowerOf2_32(Scale & ~1); |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1594 | case MVT::i16: |
| 1595 | // r + r |
Chris Lattner | eb13d1b | 2007-04-10 03:48:29 +0000 | [diff] [blame] | 1596 | if (((unsigned)AM.HasBaseReg + Scale) <= 2) |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1597 | return true; |
Chris Lattner | e115294 | 2007-04-11 16:17:12 +0000 | [diff] [blame] | 1598 | return false; |
| 1599 | |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1600 | case MVT::isVoid: |
| 1601 | // Note, we allow "void" uses (basically, uses that aren't loads or |
| 1602 | // stores), because arm allows folding a scale into many arithmetic |
| 1603 | // operations. This should be made more precise and revisited later. |
Chris Lattner | b2c594f | 2007-04-03 00:13:57 +0000 | [diff] [blame] | 1604 | |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1605 | // Allow r << imm, but the imm has to be a multiple of two. |
| 1606 | if (AM.Scale & 1) return false; |
| 1607 | return isPowerOf2_32(AM.Scale); |
| 1608 | } |
| 1609 | break; |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1610 | } |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1611 | return true; |
Evan Cheng | b01fad6 | 2007-03-12 23:30:29 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Chris Lattner | 37caf8c | 2007-04-09 23:33:39 +0000 | [diff] [blame] | 1614 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1615 | static bool getIndexedAddressParts(SDNode *Ptr, MVT::ValueType VT, |
| 1616 | bool isSEXTLoad, SDOperand &Base, |
| 1617 | SDOperand &Offset, bool &isInc, |
| 1618 | SelectionDAG &DAG) { |
| 1619 | if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) |
| 1620 | return false; |
| 1621 | |
| 1622 | if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { |
| 1623 | // AddressingMode 3 |
| 1624 | Base = Ptr->getOperand(0); |
| 1625 | if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { |
| 1626 | int RHSC = (int)RHS->getValue(); |
| 1627 | if (RHSC < 0 && RHSC > -256) { |
| 1628 | isInc = false; |
| 1629 | Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); |
| 1630 | return true; |
| 1631 | } |
| 1632 | } |
| 1633 | isInc = (Ptr->getOpcode() == ISD::ADD); |
| 1634 | Offset = Ptr->getOperand(1); |
| 1635 | return true; |
| 1636 | } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { |
| 1637 | // AddressingMode 2 |
| 1638 | if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { |
| 1639 | int RHSC = (int)RHS->getValue(); |
| 1640 | if (RHSC < 0 && RHSC > -0x1000) { |
| 1641 | isInc = false; |
| 1642 | Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); |
| 1643 | Base = Ptr->getOperand(0); |
| 1644 | return true; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (Ptr->getOpcode() == ISD::ADD) { |
| 1649 | isInc = true; |
| 1650 | ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0)); |
| 1651 | if (ShOpcVal != ARM_AM::no_shift) { |
| 1652 | Base = Ptr->getOperand(1); |
| 1653 | Offset = Ptr->getOperand(0); |
| 1654 | } else { |
| 1655 | Base = Ptr->getOperand(0); |
| 1656 | Offset = Ptr->getOperand(1); |
| 1657 | } |
| 1658 | return true; |
| 1659 | } |
| 1660 | |
| 1661 | isInc = (Ptr->getOpcode() == ISD::ADD); |
| 1662 | Base = Ptr->getOperand(0); |
| 1663 | Offset = Ptr->getOperand(1); |
| 1664 | return true; |
| 1665 | } |
| 1666 | |
| 1667 | // FIXME: Use FLDM / FSTM to emulate indexed FP load / store. |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
| 1671 | /// getPreIndexedAddressParts - returns true by value, base pointer and |
| 1672 | /// offset pointer and addressing mode by reference if the node's address |
| 1673 | /// can be legally represented as pre-indexed load / store address. |
| 1674 | bool |
| 1675 | ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDOperand &Base, |
| 1676 | SDOperand &Offset, |
| 1677 | ISD::MemIndexedMode &AM, |
| 1678 | SelectionDAG &DAG) { |
| 1679 | if (Subtarget->isThumb()) |
| 1680 | return false; |
| 1681 | |
| 1682 | MVT::ValueType VT; |
| 1683 | SDOperand Ptr; |
| 1684 | bool isSEXTLoad = false; |
| 1685 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 1686 | Ptr = LD->getBasePtr(); |
| 1687 | VT = LD->getLoadedVT(); |
| 1688 | isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; |
| 1689 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
| 1690 | Ptr = ST->getBasePtr(); |
| 1691 | VT = ST->getStoredVT(); |
| 1692 | } else |
| 1693 | return false; |
| 1694 | |
| 1695 | bool isInc; |
| 1696 | bool isLegal = getIndexedAddressParts(Ptr.Val, VT, isSEXTLoad, Base, Offset, |
| 1697 | isInc, DAG); |
| 1698 | if (isLegal) { |
| 1699 | AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; |
| 1700 | return true; |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | /// getPostIndexedAddressParts - returns true by value, base pointer and |
| 1706 | /// offset pointer and addressing mode by reference if this node can be |
| 1707 | /// combined with a load / store to form a post-indexed load / store. |
| 1708 | bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, |
| 1709 | SDOperand &Base, |
| 1710 | SDOperand &Offset, |
| 1711 | ISD::MemIndexedMode &AM, |
| 1712 | SelectionDAG &DAG) { |
| 1713 | if (Subtarget->isThumb()) |
| 1714 | return false; |
| 1715 | |
| 1716 | MVT::ValueType VT; |
| 1717 | SDOperand Ptr; |
| 1718 | bool isSEXTLoad = false; |
| 1719 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 1720 | VT = LD->getLoadedVT(); |
| 1721 | isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; |
| 1722 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
| 1723 | VT = ST->getStoredVT(); |
| 1724 | } else |
| 1725 | return false; |
| 1726 | |
| 1727 | bool isInc; |
| 1728 | bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, |
| 1729 | isInc, DAG); |
| 1730 | if (isLegal) { |
| 1731 | AM = isInc ? ISD::POST_INC : ISD::POST_DEC; |
| 1732 | return true; |
| 1733 | } |
| 1734 | return false; |
| 1735 | } |
| 1736 | |
| 1737 | void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 1738 | uint64_t Mask, |
| 1739 | uint64_t &KnownZero, |
| 1740 | uint64_t &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1741 | const SelectionDAG &DAG, |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1742 | unsigned Depth) const { |
| 1743 | KnownZero = 0; |
| 1744 | KnownOne = 0; |
| 1745 | switch (Op.getOpcode()) { |
| 1746 | default: break; |
| 1747 | case ARMISD::CMOV: { |
| 1748 | // Bits are known zero/one if known on the LHS and RHS. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1749 | DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1750 | if (KnownZero == 0 && KnownOne == 0) return; |
| 1751 | |
| 1752 | uint64_t KnownZeroRHS, KnownOneRHS; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1753 | DAG.ComputeMaskedBits(Op.getOperand(1), Mask, |
| 1754 | KnownZeroRHS, KnownOneRHS, Depth+1); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1755 | KnownZero &= KnownZeroRHS; |
| 1756 | KnownOne &= KnownOneRHS; |
| 1757 | return; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | //===----------------------------------------------------------------------===// |
| 1763 | // ARM Inline Assembly Support |
| 1764 | //===----------------------------------------------------------------------===// |
| 1765 | |
| 1766 | /// getConstraintType - Given a constraint letter, return the type of |
| 1767 | /// constraint it is for this target. |
| 1768 | ARMTargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1769 | ARMTargetLowering::getConstraintType(const std::string &Constraint) const { |
| 1770 | if (Constraint.size() == 1) { |
| 1771 | switch (Constraint[0]) { |
| 1772 | default: break; |
| 1773 | case 'l': return C_RegisterClass; |
Chris Lattner | c4e3f8e | 2007-04-02 17:24:08 +0000 | [diff] [blame] | 1774 | case 'w': return C_RegisterClass; |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1775 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1776 | } |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 1777 | return TargetLowering::getConstraintType(Constraint); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | std::pair<unsigned, const TargetRegisterClass*> |
| 1781 | ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
| 1782 | MVT::ValueType VT) const { |
| 1783 | if (Constraint.size() == 1) { |
| 1784 | // GCC RS6000 Constraint Letters |
| 1785 | switch (Constraint[0]) { |
Chris Lattner | c4e3f8e | 2007-04-02 17:24:08 +0000 | [diff] [blame] | 1786 | case 'l': |
| 1787 | // FIXME: in thumb mode, 'l' is only low-regs. |
| 1788 | // FALL THROUGH. |
| 1789 | case 'r': |
| 1790 | return std::make_pair(0U, ARM::GPRRegisterClass); |
| 1791 | case 'w': |
| 1792 | if (VT == MVT::f32) |
| 1793 | return std::make_pair(0U, ARM::SPRRegisterClass); |
Evan Cheng | 0a7baa2 | 2007-04-04 00:06:07 +0000 | [diff] [blame] | 1794 | if (VT == MVT::f64) |
Chris Lattner | c4e3f8e | 2007-04-02 17:24:08 +0000 | [diff] [blame] | 1795 | return std::make_pair(0U, ARM::DPRRegisterClass); |
| 1796 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 1800 | } |
| 1801 | |
| 1802 | std::vector<unsigned> ARMTargetLowering:: |
| 1803 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 1804 | MVT::ValueType VT) const { |
| 1805 | if (Constraint.size() != 1) |
| 1806 | return std::vector<unsigned>(); |
| 1807 | |
| 1808 | switch (Constraint[0]) { // GCC ARM Constraint Letters |
| 1809 | default: break; |
| 1810 | case 'l': |
| 1811 | case 'r': |
| 1812 | return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3, |
| 1813 | ARM::R4, ARM::R5, ARM::R6, ARM::R7, |
| 1814 | ARM::R8, ARM::R9, ARM::R10, ARM::R11, |
| 1815 | ARM::R12, ARM::LR, 0); |
Chris Lattner | c4e3f8e | 2007-04-02 17:24:08 +0000 | [diff] [blame] | 1816 | case 'w': |
| 1817 | if (VT == MVT::f32) |
| 1818 | return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3, |
| 1819 | ARM::S4, ARM::S5, ARM::S6, ARM::S7, |
| 1820 | ARM::S8, ARM::S9, ARM::S10, ARM::S11, |
| 1821 | ARM::S12,ARM::S13,ARM::S14,ARM::S15, |
| 1822 | ARM::S16,ARM::S17,ARM::S18,ARM::S19, |
| 1823 | ARM::S20,ARM::S21,ARM::S22,ARM::S23, |
| 1824 | ARM::S24,ARM::S25,ARM::S26,ARM::S27, |
| 1825 | ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0); |
| 1826 | if (VT == MVT::f64) |
| 1827 | return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3, |
| 1828 | ARM::D4, ARM::D5, ARM::D6, ARM::D7, |
| 1829 | ARM::D8, ARM::D9, ARM::D10,ARM::D11, |
| 1830 | ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0); |
| 1831 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | return std::vector<unsigned>(); |
| 1835 | } |