Arnold Schwaighofer | a70fe79 | 2007-10-12 21:53:12 +0000 | [diff] [blame] | 1 | //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that X86 uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86.h" |
| 16 | #include "X86InstrBuilder.h" |
| 17 | #include "X86ISelLowering.h" |
| 18 | #include "X86MachineFunctionInfo.h" |
| 19 | #include "X86TargetMachine.h" |
| 20 | #include "llvm/CallingConv.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/GlobalVariable.h" |
| 24 | #include "llvm/Function.h" |
| 25 | #include "llvm/Intrinsics.h" |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/VectorExtras.h" |
| 28 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 29 | #include "llvm/CodeGen/CallingConvLower.h" |
| 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/StringExtras.h" |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 40 | #include "llvm/ParameterAttributes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | X86TargetLowering::X86TargetLowering(TargetMachine &TM) |
| 44 | : TargetLowering(TM) { |
| 45 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 46 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 47 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 49 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | |
| 51 | RegInfo = TM.getRegisterInfo(); |
| 52 | |
| 53 | // Set up the TargetLowering object. |
| 54 | |
| 55 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 56 | setShiftAmountType(MVT::i8); |
| 57 | setSetCCResultType(MVT::i8); |
| 58 | setSetCCResultContents(ZeroOrOneSetCCResult); |
| 59 | setSchedulingPreference(SchedulingForRegPressure); |
| 60 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
| 61 | setStackPointerRegisterToSaveRestore(X86StackPtr); |
| 62 | |
| 63 | if (Subtarget->isTargetDarwin()) { |
| 64 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
| 65 | setUseUnderscoreSetJmp(false); |
| 66 | setUseUnderscoreLongJmp(false); |
| 67 | } else if (Subtarget->isTargetMingw()) { |
| 68 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 69 | setUseUnderscoreSetJmp(true); |
| 70 | setUseUnderscoreLongJmp(false); |
| 71 | } else { |
| 72 | setUseUnderscoreSetJmp(true); |
| 73 | setUseUnderscoreLongJmp(true); |
| 74 | } |
| 75 | |
| 76 | // Set up the register classes. |
| 77 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 78 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 79 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
| 80 | if (Subtarget->is64Bit()) |
| 81 | addRegisterClass(MVT::i64, X86::GR64RegisterClass); |
| 82 | |
Duncan Sands | 082524c | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 83 | setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 3bc0850 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 85 | // We don't accept any truncstore of integer registers. |
| 86 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
| 87 | setTruncStoreAction(MVT::i64, MVT::i16, Expand); |
| 88 | setTruncStoreAction(MVT::i64, MVT::i8 , Expand); |
| 89 | setTruncStoreAction(MVT::i32, MVT::i16, Expand); |
| 90 | setTruncStoreAction(MVT::i32, MVT::i8 , Expand); |
| 91 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); |
| 92 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 94 | // operation. |
| 95 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 96 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 97 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
| 98 | |
| 99 | if (Subtarget->is64Bit()) { |
| 100 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); |
| 101 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| 102 | } else { |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 103 | if (X86ScalarSSEf64) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP. |
| 105 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); |
| 106 | else |
| 107 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| 108 | } |
| 109 | |
| 110 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 111 | // this operation. |
| 112 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 113 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
| 114 | // SSE has no i16 to fp conversion, only i32 |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 115 | if (X86ScalarSSEf32) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 117 | // f32 and f64 cases are Legal, f80 case is not |
| 118 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 119 | } else { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 121 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 122 | } |
| 123 | |
Dale Johannesen | 958b08b | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 124 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 |
| 125 | // are Legal, f80 is custom lowered. |
| 126 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 127 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | |
| 129 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 130 | // this operation. |
| 131 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 132 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 133 | |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 134 | if (X86ScalarSSEf32) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 136 | // f32 and f64 cases are Legal, f80 case is not |
| 137 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | } else { |
| 139 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
| 140 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
| 141 | } |
| 142 | |
| 143 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 144 | // conversion. |
| 145 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 146 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 147 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 148 | |
| 149 | if (Subtarget->is64Bit()) { |
| 150 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
| 151 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 152 | } else { |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 153 | if (X86ScalarSSEf32 && !Subtarget->hasSSE3()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 154 | // Expand FP_TO_UINT into a select. |
| 155 | // FIXME: We would like to use a Custom expander here eventually to do |
| 156 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 157 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 158 | else |
| 159 | // With SSE3 we can use fisttpll to convert to a signed i64. |
| 160 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 161 | } |
| 162 | |
| 163 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 164 | if (!X86ScalarSSEf64) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 165 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 166 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
| 167 | } |
| 168 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 169 | // Scalar integer multiply, multiply-high, divide, and remainder are |
| 170 | // lowered to use operations that produce two results, to match the |
| 171 | // available instructions. This exposes the two-result form to trivial |
| 172 | // CSE, which is able to combine x/y and x%y into a single instruction, |
| 173 | // for example. The single-result multiply instructions are introduced |
| 174 | // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part |
| 175 | // is not needed. |
| 176 | setOperationAction(ISD::MUL , MVT::i8 , Expand); |
| 177 | setOperationAction(ISD::MULHS , MVT::i8 , Expand); |
| 178 | setOperationAction(ISD::MULHU , MVT::i8 , Expand); |
| 179 | setOperationAction(ISD::SDIV , MVT::i8 , Expand); |
| 180 | setOperationAction(ISD::UDIV , MVT::i8 , Expand); |
| 181 | setOperationAction(ISD::SREM , MVT::i8 , Expand); |
| 182 | setOperationAction(ISD::UREM , MVT::i8 , Expand); |
| 183 | setOperationAction(ISD::MUL , MVT::i16 , Expand); |
| 184 | setOperationAction(ISD::MULHS , MVT::i16 , Expand); |
| 185 | setOperationAction(ISD::MULHU , MVT::i16 , Expand); |
| 186 | setOperationAction(ISD::SDIV , MVT::i16 , Expand); |
| 187 | setOperationAction(ISD::UDIV , MVT::i16 , Expand); |
| 188 | setOperationAction(ISD::SREM , MVT::i16 , Expand); |
| 189 | setOperationAction(ISD::UREM , MVT::i16 , Expand); |
| 190 | setOperationAction(ISD::MUL , MVT::i32 , Expand); |
| 191 | setOperationAction(ISD::MULHS , MVT::i32 , Expand); |
| 192 | setOperationAction(ISD::MULHU , MVT::i32 , Expand); |
| 193 | setOperationAction(ISD::SDIV , MVT::i32 , Expand); |
| 194 | setOperationAction(ISD::UDIV , MVT::i32 , Expand); |
| 195 | setOperationAction(ISD::SREM , MVT::i32 , Expand); |
| 196 | setOperationAction(ISD::UREM , MVT::i32 , Expand); |
| 197 | setOperationAction(ISD::MUL , MVT::i64 , Expand); |
| 198 | setOperationAction(ISD::MULHS , MVT::i64 , Expand); |
| 199 | setOperationAction(ISD::MULHU , MVT::i64 , Expand); |
| 200 | setOperationAction(ISD::SDIV , MVT::i64 , Expand); |
| 201 | setOperationAction(ISD::UDIV , MVT::i64 , Expand); |
| 202 | setOperationAction(ISD::SREM , MVT::i64 , Expand); |
| 203 | setOperationAction(ISD::UREM , MVT::i64 , Expand); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 204 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 205 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
| 206 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
| 207 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 208 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
| 209 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 210 | if (Subtarget->is64Bit()) |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 211 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
| 212 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); |
| 213 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 214 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 215 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 216 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
Anton Korobeynikov | fbe230e | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 217 | setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom); |
| 218 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 219 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 220 | setOperationAction(ISD::CTTZ , MVT::i8 , Custom); |
| 221 | setOperationAction(ISD::CTLZ , MVT::i8 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 222 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 223 | setOperationAction(ISD::CTTZ , MVT::i16 , Custom); |
| 224 | setOperationAction(ISD::CTLZ , MVT::i16 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 226 | setOperationAction(ISD::CTTZ , MVT::i32 , Custom); |
| 227 | setOperationAction(ISD::CTLZ , MVT::i32 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 228 | if (Subtarget->is64Bit()) { |
| 229 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 230 | setOperationAction(ISD::CTTZ , MVT::i64 , Custom); |
| 231 | setOperationAction(ISD::CTLZ , MVT::i64 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
| 235 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
| 236 | |
| 237 | // These should be promoted to a larger select which is supported. |
| 238 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 239 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
| 240 | // X86 wants to expand cmov itself. |
| 241 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 242 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 243 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 244 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 245 | setOperationAction(ISD::SELECT , MVT::f80 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 247 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 248 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 249 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 250 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 251 | setOperationAction(ISD::SETCC , MVT::f80 , Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | if (Subtarget->is64Bit()) { |
| 253 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| 254 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| 255 | } |
| 256 | // X86 ret instruction may pop stack. |
| 257 | setOperationAction(ISD::RET , MVT::Other, Custom); |
| 258 | if (!Subtarget->is64Bit()) |
| 259 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); |
| 260 | |
| 261 | // Darwin ABI issue. |
| 262 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
| 263 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
| 264 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
| 265 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); |
| 266 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
| 267 | if (Subtarget->is64Bit()) { |
| 268 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 269 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 270 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
| 271 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
| 272 | } |
| 273 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
| 274 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 275 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 276 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
| 277 | // X86 wants to expand memset / memcpy itself. |
| 278 | setOperationAction(ISD::MEMSET , MVT::Other, Custom); |
| 279 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
| 280 | |
Dan Gohman | 2144285 | 2007-09-25 15:10:49 +0000 | [diff] [blame] | 281 | // Use the default ISD::LOCATION expansion. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 283 | // FIXME - use subtarget debug flags |
| 284 | if (!Subtarget->isTargetDarwin() && |
| 285 | !Subtarget->isTargetELF() && |
| 286 | !Subtarget->isTargetCygMing()) |
| 287 | setOperationAction(ISD::LABEL, MVT::Other, Expand); |
| 288 | |
| 289 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); |
| 290 | setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); |
| 291 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 292 | setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); |
| 293 | if (Subtarget->is64Bit()) { |
| 294 | // FIXME: Verify |
| 295 | setExceptionPointerRegister(X86::RAX); |
| 296 | setExceptionSelectorRegister(X86::RDX); |
| 297 | } else { |
| 298 | setExceptionPointerRegister(X86::EAX); |
| 299 | setExceptionSelectorRegister(X86::EDX); |
| 300 | } |
Anton Korobeynikov | 23ca9c5 | 2007-09-03 00:36:06 +0000 | [diff] [blame] | 301 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 302 | |
Duncan Sands | 7407a9f | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 303 | setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 56b941f | 2008-01-15 21:58:22 +0000 | [diff] [blame] | 305 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Anton Korobeynikov | 39d40ba | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 306 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 307 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 308 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| 309 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 310 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
| 311 | if (Subtarget->is64Bit()) |
| 312 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
| 313 | else |
| 314 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 315 | |
| 316 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 317 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 318 | if (Subtarget->is64Bit()) |
| 319 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
| 320 | if (Subtarget->isTargetCygMing()) |
| 321 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); |
| 322 | else |
| 323 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
| 324 | |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 325 | if (X86ScalarSSEf64) { |
| 326 | // f32 and f64 use SSE. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 327 | // Set up the FP register classes. |
| 328 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 329 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
| 330 | |
| 331 | // Use ANDPD to simulate FABS. |
| 332 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 333 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 334 | |
| 335 | // Use XORP to simulate FNEG. |
| 336 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 337 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 338 | |
| 339 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| 340 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 341 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 342 | |
| 343 | // We don't support sin/cos/fmod |
| 344 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 345 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 346 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 347 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 348 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 349 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 350 | |
| 351 | // Expand FP immediates into loads from the stack, except for the special |
| 352 | // cases we handle. |
| 353 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 354 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 355 | addLegalFPImmediate(APFloat(+0.0)); // xorpd |
| 356 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
Dale Johannesen | 8f83a6b | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 357 | |
| 358 | // Conversions to long double (in X87) go through memory. |
| 359 | setConvertAction(MVT::f32, MVT::f80, Expand); |
| 360 | setConvertAction(MVT::f64, MVT::f80, Expand); |
| 361 | |
| 362 | // Conversions from long double (in X87) go through memory. |
| 363 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 364 | setConvertAction(MVT::f80, MVT::f64, Expand); |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 365 | } else if (X86ScalarSSEf32) { |
| 366 | // Use SSE for f32, x87 for f64. |
| 367 | // Set up the FP register classes. |
| 368 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 369 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 370 | |
| 371 | // Use ANDPS to simulate FABS. |
| 372 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 373 | |
| 374 | // Use XORP to simulate FNEG. |
| 375 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 376 | |
| 377 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 378 | |
| 379 | // Use ANDPS and ORPS to simulate FCOPYSIGN. |
| 380 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 381 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 382 | |
| 383 | // We don't support sin/cos/fmod |
| 384 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 385 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 386 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 387 | |
| 388 | // Expand FP immediates into loads from the stack, except for the special |
| 389 | // cases we handle. |
| 390 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 391 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
| 392 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
| 393 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 394 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 395 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 396 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
| 397 | |
| 398 | // SSE->x87 conversions go through memory. |
| 399 | setConvertAction(MVT::f32, MVT::f64, Expand); |
| 400 | setConvertAction(MVT::f32, MVT::f80, Expand); |
| 401 | |
| 402 | // x87->SSE truncations need to go through memory. |
| 403 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 404 | setConvertAction(MVT::f64, MVT::f32, Expand); |
| 405 | // And x87->x87 truncations also. |
| 406 | setConvertAction(MVT::f80, MVT::f64, Expand); |
| 407 | |
| 408 | if (!UnsafeFPMath) { |
| 409 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 410 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 411 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 412 | } else { |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 413 | // f32 and f64 in x87. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 414 | // Set up the FP register classes. |
| 415 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 416 | addRegisterClass(MVT::f32, X86::RFP32RegisterClass); |
| 417 | |
| 418 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 419 | setOperationAction(ISD::UNDEF, MVT::f32, Expand); |
| 420 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 421 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Dale Johannesen | 8f83a6b | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 422 | |
| 423 | // Floating truncations need to go through memory. |
| 424 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 425 | setConvertAction(MVT::f64, MVT::f32, Expand); |
| 426 | setConvertAction(MVT::f80, MVT::f64, Expand); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 427 | |
| 428 | if (!UnsafeFPMath) { |
| 429 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 430 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 431 | } |
| 432 | |
| 433 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 434 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Dale Johannesen | bbe2b70 | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 435 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 436 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 437 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 438 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 439 | addLegalFPImmediate(APFloat(+0.0f)); // FLD0 |
| 440 | addLegalFPImmediate(APFloat(+1.0f)); // FLD1 |
| 441 | addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS |
| 442 | addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Dale Johannesen | 4ab00bd | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 445 | // Long double always uses X87. |
| 446 | addRegisterClass(MVT::f80, X86::RFP80RegisterClass); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 447 | setOperationAction(ISD::UNDEF, MVT::f80, Expand); |
| 448 | setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); |
| 449 | setOperationAction(ISD::ConstantFP, MVT::f80, Expand); |
Dale Johannesen | 7f1076b | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 450 | if (!UnsafeFPMath) { |
| 451 | setOperationAction(ISD::FSIN , MVT::f80 , Expand); |
| 452 | setOperationAction(ISD::FCOS , MVT::f80 , Expand); |
| 453 | } |
Dale Johannesen | 4ab00bd | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 454 | |
Dan Gohman | 2f7b198 | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 455 | // Always use a library call for pow. |
| 456 | setOperationAction(ISD::FPOW , MVT::f32 , Expand); |
| 457 | setOperationAction(ISD::FPOW , MVT::f64 , Expand); |
| 458 | setOperationAction(ISD::FPOW , MVT::f80 , Expand); |
| 459 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 460 | // First set operation action for all vector types to expand. Then we |
| 461 | // will selectively turn on ones that can be effectively codegen'd. |
| 462 | for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 463 | VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { |
| 464 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 465 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
| 466 | setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand); |
| 467 | setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand); |
| 468 | setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand); |
| 469 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
| 470 | setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand); |
| 471 | setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand); |
| 472 | setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand); |
| 473 | setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand); |
| 474 | setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand); |
| 475 | setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand); |
| 476 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); |
| 477 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); |
| 478 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
| 479 | setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
| 480 | setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand); |
| 481 | setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand); |
| 482 | setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand); |
| 483 | setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand); |
| 484 | setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand); |
| 485 | setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand); |
| 486 | setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 487 | setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand); |
| 488 | setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand); |
| 489 | setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand); |
| 490 | setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand); |
Dan Gohman | 2f7b198 | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 491 | setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand); |
Dan Gohman | 1d2dc2c | 2007-10-12 14:09:42 +0000 | [diff] [blame] | 492 | setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand); |
| 493 | setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand); |
| 494 | setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand); |
Dan Gohman | 5b9d641 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 495 | setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand); |
| 496 | setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand); |
| 497 | setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand); |
| 498 | setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand); |
| 499 | setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand); |
| 500 | setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (Subtarget->hasMMX()) { |
| 504 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 505 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 506 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
| 507 | addRegisterClass(MVT::v1i64, X86::VR64RegisterClass); |
| 508 | |
| 509 | // FIXME: add MMX packed arithmetics |
| 510 | |
| 511 | setOperationAction(ISD::ADD, MVT::v8i8, Legal); |
| 512 | setOperationAction(ISD::ADD, MVT::v4i16, Legal); |
| 513 | setOperationAction(ISD::ADD, MVT::v2i32, Legal); |
| 514 | setOperationAction(ISD::ADD, MVT::v1i64, Legal); |
| 515 | |
| 516 | setOperationAction(ISD::SUB, MVT::v8i8, Legal); |
| 517 | setOperationAction(ISD::SUB, MVT::v4i16, Legal); |
| 518 | setOperationAction(ISD::SUB, MVT::v2i32, Legal); |
Dale Johannesen | 6b65c33 | 2007-10-30 01:18:38 +0000 | [diff] [blame] | 519 | setOperationAction(ISD::SUB, MVT::v1i64, Legal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 520 | |
| 521 | setOperationAction(ISD::MULHS, MVT::v4i16, Legal); |
| 522 | setOperationAction(ISD::MUL, MVT::v4i16, Legal); |
| 523 | |
| 524 | setOperationAction(ISD::AND, MVT::v8i8, Promote); |
| 525 | AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64); |
| 526 | setOperationAction(ISD::AND, MVT::v4i16, Promote); |
| 527 | AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64); |
| 528 | setOperationAction(ISD::AND, MVT::v2i32, Promote); |
| 529 | AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64); |
| 530 | setOperationAction(ISD::AND, MVT::v1i64, Legal); |
| 531 | |
| 532 | setOperationAction(ISD::OR, MVT::v8i8, Promote); |
| 533 | AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64); |
| 534 | setOperationAction(ISD::OR, MVT::v4i16, Promote); |
| 535 | AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64); |
| 536 | setOperationAction(ISD::OR, MVT::v2i32, Promote); |
| 537 | AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64); |
| 538 | setOperationAction(ISD::OR, MVT::v1i64, Legal); |
| 539 | |
| 540 | setOperationAction(ISD::XOR, MVT::v8i8, Promote); |
| 541 | AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64); |
| 542 | setOperationAction(ISD::XOR, MVT::v4i16, Promote); |
| 543 | AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64); |
| 544 | setOperationAction(ISD::XOR, MVT::v2i32, Promote); |
| 545 | AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64); |
| 546 | setOperationAction(ISD::XOR, MVT::v1i64, Legal); |
| 547 | |
| 548 | setOperationAction(ISD::LOAD, MVT::v8i8, Promote); |
| 549 | AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64); |
| 550 | setOperationAction(ISD::LOAD, MVT::v4i16, Promote); |
| 551 | AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64); |
| 552 | setOperationAction(ISD::LOAD, MVT::v2i32, Promote); |
| 553 | AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64); |
| 554 | setOperationAction(ISD::LOAD, MVT::v1i64, Legal); |
| 555 | |
| 556 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); |
| 557 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); |
| 558 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); |
| 559 | setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); |
| 560 | |
| 561 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); |
| 562 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); |
| 563 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); |
| 564 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); |
| 565 | |
| 566 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom); |
| 567 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom); |
| 568 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom); |
| 569 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom); |
| 570 | } |
| 571 | |
| 572 | if (Subtarget->hasSSE1()) { |
| 573 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 574 | |
| 575 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 576 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 577 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 578 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
| 579 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 580 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 581 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 582 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 583 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
| 584 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
| 585 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
| 586 | } |
| 587 | |
| 588 | if (Subtarget->hasSSE2()) { |
| 589 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
| 590 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 591 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 592 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 593 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 594 | |
| 595 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 596 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 597 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
| 598 | setOperationAction(ISD::ADD, MVT::v2i64, Legal); |
| 599 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 600 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 601 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
| 602 | setOperationAction(ISD::SUB, MVT::v2i64, Legal); |
| 603 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
| 604 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 605 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 606 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 607 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
| 608 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 609 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 610 | |
| 611 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 612 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
| 613 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
| 614 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 615 | // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones. |
| 616 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| 617 | |
| 618 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| 619 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
Nate Begeman | c16406d | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 620 | // Do not attempt to custom lower non-power-of-2 vectors |
| 621 | if (!isPowerOf2_32(MVT::getVectorNumElements(VT))) |
| 622 | continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 623 | setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom); |
| 624 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom); |
| 625 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom); |
| 626 | } |
| 627 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 628 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 629 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 630 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 631 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
Dale Johannesen | 2ff963d | 2007-10-31 00:32:36 +0000 | [diff] [blame] | 632 | if (Subtarget->is64Bit()) |
| 633 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 634 | |
| 635 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
| 636 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 637 | setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote); |
| 638 | AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64); |
| 639 | setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote); |
| 640 | AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64); |
| 641 | setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote); |
| 642 | AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64); |
| 643 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote); |
| 644 | AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64); |
| 645 | setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote); |
| 646 | AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64); |
| 647 | } |
| 648 | |
Chris Lattner | 3bc0850 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 649 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 650 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 651 | // Custom lower v2i64 and v2f64 selects. |
| 652 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
| 653 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
| 654 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
| 655 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
| 656 | } |
| 657 | |
| 658 | // We want to custom lower some of our intrinsics. |
| 659 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 660 | |
| 661 | // We have target-specific dag combine patterns for the following nodes: |
| 662 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
| 663 | setTargetDAGCombine(ISD::SELECT); |
| 664 | |
| 665 | computeRegisterProperties(); |
| 666 | |
| 667 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 668 | // be smaller when we are in optimizing for size mode. |
| 669 | maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores |
| 670 | maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores |
| 671 | maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores |
| 672 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 673 | } |
| 674 | |
Evan Cheng | 5a67b81 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 675 | /// getMaxByValAlign - Helper for getByValTypeAlignment to determine |
| 676 | /// the desired ByVal argument alignment. |
| 677 | static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) { |
| 678 | if (MaxAlign == 16) |
| 679 | return; |
| 680 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 681 | if (VTy->getBitWidth() == 128) |
| 682 | MaxAlign = 16; |
| 683 | else if (VTy->getBitWidth() == 64) |
| 684 | if (MaxAlign < 8) |
| 685 | MaxAlign = 8; |
| 686 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 687 | unsigned EltAlign = 0; |
| 688 | getMaxByValAlign(ATy->getElementType(), EltAlign); |
| 689 | if (EltAlign > MaxAlign) |
| 690 | MaxAlign = EltAlign; |
| 691 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 692 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 693 | unsigned EltAlign = 0; |
| 694 | getMaxByValAlign(STy->getElementType(i), EltAlign); |
| 695 | if (EltAlign > MaxAlign) |
| 696 | MaxAlign = EltAlign; |
| 697 | if (MaxAlign == 16) |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 705 | /// function arguments in the caller parameter area. For X86, aggregates |
| 706 | /// that contains are placed at 16-byte boundaries while the rest are at |
| 707 | /// 4-byte boundaries. |
| 708 | unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const { |
| 709 | if (Subtarget->is64Bit()) |
| 710 | return getTargetData()->getABITypeAlignment(Ty); |
| 711 | unsigned Align = 4; |
| 712 | getMaxByValAlign(Ty, Align); |
| 713 | return Align; |
| 714 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 715 | |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 716 | /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC |
| 717 | /// jumptable. |
| 718 | SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table, |
| 719 | SelectionDAG &DAG) const { |
| 720 | if (usesGlobalOffsetTable()) |
| 721 | return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy()); |
| 722 | if (!Subtarget->isPICStyleRIPRel()) |
| 723 | return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()); |
| 724 | return Table; |
| 725 | } |
| 726 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 727 | //===----------------------------------------------------------------------===// |
| 728 | // Return Value Calling Convention Implementation |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
| 731 | #include "X86GenCallingConv.inc" |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 732 | |
| 733 | /// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it |
| 734 | /// exists skip possible ISD:TokenFactor. |
| 735 | static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) { |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 736 | if (Chain.getOpcode() == X86ISD::TAILCALL) { |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 737 | return Chain; |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 738 | } else if (Chain.getOpcode() == ISD::TokenFactor) { |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 739 | if (Chain.getNumOperands() && |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 740 | Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL) |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 741 | return Chain.getOperand(0); |
| 742 | } |
| 743 | return Chain; |
| 744 | } |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 745 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 746 | /// LowerRET - Lower an ISD::RET node. |
| 747 | SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 748 | assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args"); |
| 749 | |
| 750 | SmallVector<CCValAssign, 16> RVLocs; |
| 751 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
| 752 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
| 753 | CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs); |
| 754 | CCInfo.AnalyzeReturn(Op.Val, RetCC_X86); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 755 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 756 | // If this is the first return lowered for this function, add the regs to the |
| 757 | // liveout set for the function. |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 758 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 759 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 760 | if (RVLocs[i].isRegLoc()) |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 761 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 762 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 763 | SDOperand Chain = Op.getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 764 | |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 765 | // Handle tail call return. |
| 766 | Chain = GetPossiblePreceedingTailCall(Chain); |
| 767 | if (Chain.getOpcode() == X86ISD::TAILCALL) { |
| 768 | SDOperand TailCall = Chain; |
| 769 | SDOperand TargetAddress = TailCall.getOperand(1); |
| 770 | SDOperand StackAdjustment = TailCall.getOperand(2); |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 771 | assert(((TargetAddress.getOpcode() == ISD::Register && |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 772 | (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX || |
| 773 | cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) || |
| 774 | TargetAddress.getOpcode() == ISD::TargetExternalSymbol || |
| 775 | TargetAddress.getOpcode() == ISD::TargetGlobalAddress) && |
| 776 | "Expecting an global address, external symbol, or register"); |
Chris Lattner | f8decf5 | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 777 | assert(StackAdjustment.getOpcode() == ISD::Constant && |
| 778 | "Expecting a const value"); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 779 | |
| 780 | SmallVector<SDOperand,8> Operands; |
| 781 | Operands.push_back(Chain.getOperand(0)); |
| 782 | Operands.push_back(TargetAddress); |
| 783 | Operands.push_back(StackAdjustment); |
| 784 | // Copy registers used by the call. Last operand is a flag so it is not |
| 785 | // copied. |
Arnold Schwaighofer | 10202b3 | 2007-10-16 09:05:00 +0000 | [diff] [blame] | 786 | for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) { |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 787 | Operands.push_back(Chain.getOperand(i)); |
| 788 | } |
Arnold Schwaighofer | 10202b3 | 2007-10-16 09:05:00 +0000 | [diff] [blame] | 789 | return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0], |
| 790 | Operands.size()); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Regular return. |
| 794 | SDOperand Flag; |
| 795 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 796 | // Copy the result values into the output registers. |
| 797 | if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() || |
| 798 | RVLocs[0].getLocReg() != X86::ST0) { |
| 799 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 800 | CCValAssign &VA = RVLocs[i]; |
| 801 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 802 | Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), |
| 803 | Flag); |
| 804 | Flag = Chain.getValue(1); |
| 805 | } |
| 806 | } else { |
| 807 | // We need to handle a destination of ST0 specially, because it isn't really |
| 808 | // a register. |
| 809 | SDOperand Value = Op.getOperand(1); |
| 810 | |
| 811 | // If this is an FP return with ScalarSSE, we need to move the value from |
| 812 | // an XMM register onto the fp-stack. |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 813 | if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 814 | SDOperand MemLoc; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 815 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 816 | // If this is a load into a scalarsse value, don't store the loaded value |
| 817 | // back to the stack, only to reload it: just replace the scalar-sse load. |
| 818 | if (ISD::isNON_EXTLoad(Value.Val) && |
Chris Lattner | f51fdb5 | 2008-01-16 05:56:59 +0000 | [diff] [blame] | 819 | Chain.reachesChainWithoutSideEffects(Value.getOperand(0))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 820 | Chain = Value.getOperand(0); |
| 821 | MemLoc = Value.getOperand(1); |
| 822 | } else { |
| 823 | // Spill the value to memory and reload it into top of stack. |
| 824 | unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8; |
| 825 | MachineFunction &MF = DAG.getMachineFunction(); |
| 826 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 827 | MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 828 | Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0); |
| 829 | } |
| 830 | SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other); |
| 831 | SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())}; |
| 832 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); |
| 833 | Chain = Value.getValue(1); |
| 834 | } |
| 835 | |
| 836 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 837 | SDOperand Ops[] = { Chain, Value }; |
| 838 | Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2); |
| 839 | Flag = Chain.getValue(1); |
| 840 | } |
| 841 | |
| 842 | SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16); |
| 843 | if (Flag.Val) |
| 844 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag); |
| 845 | else |
| 846 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop); |
| 847 | } |
| 848 | |
| 849 | |
| 850 | /// LowerCallResult - Lower the result values of an ISD::CALL into the |
| 851 | /// appropriate copies out of appropriate physical registers. This assumes that |
| 852 | /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call |
| 853 | /// being lowered. The returns a SDNode with the same number of values as the |
| 854 | /// ISD::CALL. |
| 855 | SDNode *X86TargetLowering:: |
| 856 | LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall, |
| 857 | unsigned CallingConv, SelectionDAG &DAG) { |
| 858 | |
| 859 | // Assign locations to each value returned by this call. |
| 860 | SmallVector<CCValAssign, 16> RVLocs; |
| 861 | bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0; |
| 862 | CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs); |
| 863 | CCInfo.AnalyzeCallResult(TheCall, RetCC_X86); |
| 864 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 865 | SmallVector<SDOperand, 8> ResultVals; |
| 866 | |
| 867 | // Copy all of the result registers out of their specified physreg. |
| 868 | if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) { |
| 869 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 870 | Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(), |
| 871 | RVLocs[i].getValVT(), InFlag).getValue(1); |
| 872 | InFlag = Chain.getValue(2); |
| 873 | ResultVals.push_back(Chain.getValue(0)); |
| 874 | } |
| 875 | } else { |
| 876 | // Copies from the FP stack are special, as ST0 isn't a valid register |
| 877 | // before the fp stackifier runs. |
| 878 | |
| 879 | // Copy ST0 into an RFP register with FP_GET_RESULT. |
| 880 | SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag); |
| 881 | SDOperand GROps[] = { Chain, InFlag }; |
| 882 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2); |
| 883 | Chain = RetVal.getValue(1); |
| 884 | InFlag = RetVal.getValue(2); |
| 885 | |
| 886 | // If we are using ScalarSSE, store ST(0) to the stack and reload it into |
| 887 | // an XMM register. |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 888 | if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) { |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 889 | SDOperand StoreLoc; |
| 890 | const Value *SrcVal = 0; |
| 891 | int SrcValOffset = 0; |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 892 | MVT::ValueType RetStoreVT = RVLocs[0].getValVT(); |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 893 | |
| 894 | // Determine where to store the value. If the call result is directly |
| 895 | // used by a store, see if we can store directly into the location. In |
| 896 | // this case, we'll end up producing a fst + movss[load] + movss[store] to |
| 897 | // the same location, and the two movss's will be nuked as dead. This |
| 898 | // optimizes common things like "*D = atof(..)" to not need an |
| 899 | // intermediate stack slot. |
| 900 | if (SDOperand(TheCall, 0).hasOneUse() && |
| 901 | SDOperand(TheCall, 1).hasOneUse()) { |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 902 | // In addition to direct uses, we also support a FP_ROUND that uses the |
| 903 | // value, if it is directly stored somewhere. |
| 904 | SDNode *User = *TheCall->use_begin(); |
| 905 | if (User->getOpcode() == ISD::FP_ROUND && User->hasOneUse()) |
| 906 | User = *User->use_begin(); |
| 907 | |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 908 | // Ok, we have one use of the value and one use of the chain. See if |
| 909 | // they are the same node: a store. |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 910 | if (StoreSDNode *N = dyn_cast<StoreSDNode>(User)) { |
| 911 | // Verify that the value being stored is either the call or a |
| 912 | // truncation of the call. |
| 913 | SDNode *StoreVal = N->getValue().Val; |
| 914 | if (StoreVal == TheCall) |
| 915 | ; // ok. |
| 916 | else if (StoreVal->getOpcode() == ISD::FP_ROUND && |
| 917 | StoreVal->hasOneUse() && |
| 918 | StoreVal->getOperand(0).Val == TheCall) |
| 919 | ; // ok. |
| 920 | else |
| 921 | N = 0; // not ok. |
| 922 | |
| 923 | if (N && N->getChain().Val == TheCall && |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 924 | !N->isVolatile() && !N->isTruncatingStore() && |
| 925 | N->getAddressingMode() == ISD::UNINDEXED) { |
| 926 | StoreLoc = N->getBasePtr(); |
| 927 | SrcVal = N->getSrcValue(); |
| 928 | SrcValOffset = N->getSrcValueOffset(); |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 929 | RetStoreVT = N->getValue().getValueType(); |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // If we weren't able to optimize the result, just create a temporary |
| 935 | // stack slot. |
| 936 | if (StoreLoc.Val == 0) { |
| 937 | MachineFunction &MF = DAG.getMachineFunction(); |
| 938 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 939 | StoreLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 940 | } |
| 941 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 942 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 943 | // shouldn't be necessary except that RFP cannot be live across |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 944 | // multiple blocks (which could happen if a select gets lowered into |
| 945 | // multiple blocks and scheduled in between them). When stackifier is |
| 946 | // fixed, they can be uncoupled. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 947 | SDOperand Ops[] = { |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 948 | Chain, RetVal, StoreLoc, DAG.getValueType(RetStoreVT), InFlag |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 949 | }; |
| 950 | Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5); |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 951 | RetVal = DAG.getLoad(RetStoreVT, Chain, |
Chris Lattner | 4075873 | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 952 | StoreLoc, SrcVal, SrcValOffset); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 953 | Chain = RetVal.getValue(1); |
Chris Lattner | 8b815c2 | 2007-12-29 06:57:38 +0000 | [diff] [blame] | 954 | |
| 955 | // If we optimized a truncate, then extend the result back to its desired |
| 956 | // type. |
| 957 | if (RVLocs[0].getValVT() != RetStoreVT) |
| 958 | RetVal = DAG.getNode(ISD::FP_EXTEND, RVLocs[0].getValVT(), RetVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 959 | } |
| 960 | ResultVals.push_back(RetVal); |
| 961 | } |
| 962 | |
| 963 | // Merge everything together with a MERGE_VALUES node. |
| 964 | ResultVals.push_back(Chain); |
| 965 | return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), |
| 966 | &ResultVals[0], ResultVals.size()).Val; |
| 967 | } |
| 968 | |
| 969 | |
| 970 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 971 | // C & StdCall & Fast Calling Convention implementation |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 972 | //===----------------------------------------------------------------------===// |
| 973 | // StdCall calling convention seems to be standard for many Windows' API |
| 974 | // routines and around. It differs from C calling convention just a little: |
| 975 | // callee should clean up the stack, not caller. Symbols should be also |
| 976 | // decorated in some fancy way :) It doesn't support any vector arguments. |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 977 | // For info on fast calling convention see Fast Calling Convention (tail call) |
| 978 | // implementation LowerX86_32FastCCCallTo. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 979 | |
| 980 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 981 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 982 | /// register for it. |
| 983 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 984 | const TargetRegisterClass *RC) { |
| 985 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 986 | unsigned VReg = MF.getRegInfo().createVirtualRegister(RC); |
| 987 | MF.getRegInfo().addLiveIn(PReg, VReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 988 | return VReg; |
| 989 | } |
| 990 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 991 | // Determines whether a CALL node uses struct return semantics. |
| 992 | static bool CallIsStructReturn(SDOperand Op) { |
| 993 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 994 | if (!NumOps) |
| 995 | return false; |
| 996 | |
| 997 | ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6)); |
| 998 | return Flags->getValue() & ISD::ParamFlags::StructReturn; |
| 999 | } |
| 1000 | |
| 1001 | // Determines whether a FORMAL_ARGUMENTS node uses struct return semantics. |
| 1002 | static bool ArgsAreStructReturn(SDOperand Op) { |
| 1003 | unsigned NumArgs = Op.Val->getNumValues() - 1; |
| 1004 | if (!NumArgs) |
| 1005 | return false; |
| 1006 | |
| 1007 | ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3)); |
| 1008 | return Flags->getValue() & ISD::ParamFlags::StructReturn; |
| 1009 | } |
| 1010 | |
| 1011 | // Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop |
| 1012 | // its own arguments. Callee pop is necessary to support tail calls. |
| 1013 | bool X86TargetLowering::IsCalleePop(SDOperand Op) { |
| 1014 | bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 1015 | if (IsVarArg) |
| 1016 | return false; |
| 1017 | |
| 1018 | switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) { |
| 1019 | default: |
| 1020 | return false; |
| 1021 | case CallingConv::X86_StdCall: |
| 1022 | return !Subtarget->is64Bit(); |
| 1023 | case CallingConv::X86_FastCall: |
| 1024 | return !Subtarget->is64Bit(); |
| 1025 | case CallingConv::Fast: |
| 1026 | return PerformTailCallOpt; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node. |
| 1031 | CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const { |
| 1032 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 1033 | |
| 1034 | if (Subtarget->is64Bit()) |
| 1035 | if (CC == CallingConv::Fast && PerformTailCallOpt) |
| 1036 | return CC_X86_64_TailCall; |
| 1037 | else |
| 1038 | return CC_X86_64_C; |
| 1039 | |
| 1040 | if (CC == CallingConv::X86_FastCall) |
| 1041 | return CC_X86_32_FastCall; |
| 1042 | else if (CC == CallingConv::Fast && PerformTailCallOpt) |
| 1043 | return CC_X86_32_TailCall; |
| 1044 | else |
| 1045 | return CC_X86_32_C; |
| 1046 | } |
| 1047 | |
| 1048 | // Selects the appropriate decoration to apply to a MachineFunction containing a |
| 1049 | // given FORMAL_ARGUMENTS node. |
| 1050 | NameDecorationStyle |
| 1051 | X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) { |
| 1052 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 1053 | if (CC == CallingConv::X86_FastCall) |
| 1054 | return FastCall; |
| 1055 | else if (CC == CallingConv::X86_StdCall) |
| 1056 | return StdCall; |
| 1057 | return None; |
| 1058 | } |
| 1059 | |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1060 | |
Arnold Schwaighofer | 0e3c27e | 2008-01-11 17:10:15 +0000 | [diff] [blame] | 1061 | // IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly |
| 1062 | // be overwritten when lowering the outgoing arguments in a tail call. Currently |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1063 | // the implementation of this call is very conservative and assumes all |
| 1064 | // arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual |
Arnold Schwaighofer | 0e3c27e | 2008-01-11 17:10:15 +0000 | [diff] [blame] | 1065 | // registers would be overwritten by direct lowering. |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1066 | // Possible improvement: |
| 1067 | // Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes |
| 1068 | // indicating inreg passed arguments which also need not be lowered to a safe |
| 1069 | // stack slot. |
Arnold Schwaighofer | 0e3c27e | 2008-01-11 17:10:15 +0000 | [diff] [blame] | 1070 | static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) { |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1071 | RegisterSDNode * OpReg = NULL; |
| 1072 | if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS || |
| 1073 | (Op.getOpcode()== ISD::CopyFromReg && |
| 1074 | (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) && |
| 1075 | OpReg->getReg() >= MRegisterInfo::FirstVirtualRegister)) |
| 1076 | return true; |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1080 | // CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
| 1081 | // by "Src" to address "Dst" with size and alignment information specified by |
| 1082 | // the specific parameter attribute. The copy will be passed as a byval function |
| 1083 | // parameter. |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1084 | static SDOperand |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1085 | CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain, |
| 1086 | unsigned Flags, SelectionDAG &DAG) { |
| 1087 | unsigned Align = 1 << |
| 1088 | ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs); |
| 1089 | unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >> |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1090 | ISD::ParamFlags::ByValSizeOffs; |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1091 | SDOperand AlignNode = DAG.getConstant(Align, MVT::i32); |
| 1092 | SDOperand SizeNode = DAG.getConstant(Size, MVT::i32); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1093 | SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32); |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1094 | return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1097 | SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG, |
| 1098 | const CCValAssign &VA, |
| 1099 | MachineFrameInfo *MFI, |
| 1100 | SDOperand Root, unsigned i) { |
| 1101 | // Create the nodes corresponding to a load from this parameter slot. |
Evan Cheng | 3e42a52 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1102 | unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue(); |
| 1103 | bool isByVal = Flags & ISD::ParamFlags::ByVal; |
| 1104 | |
| 1105 | // FIXME: For now, all byval parameter objects are marked mutable. This |
| 1106 | // can be changed with more analysis. |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1107 | int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8, |
Evan Cheng | 3e42a52 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1108 | VA.getLocMemOffset(), !isByVal); |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1109 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Evan Cheng | 3e42a52 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1110 | if (isByVal) |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1111 | return FIN; |
Evan Cheng | 3e42a52 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1112 | return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0); |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1115 | SDOperand |
| 1116 | X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1117 | MachineFunction &MF = DAG.getMachineFunction(); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1118 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1119 | |
| 1120 | const Function* Fn = MF.getFunction(); |
| 1121 | if (Fn->hasExternalLinkage() && |
| 1122 | Subtarget->isTargetCygMing() && |
| 1123 | Fn->getName() == "main") |
| 1124 | FuncInfo->setForceFramePointer(true); |
| 1125 | |
| 1126 | // Decorate the function name. |
| 1127 | FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op)); |
| 1128 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1129 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1130 | SDOperand Root = Op.getOperand(0); |
| 1131 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1132 | unsigned CC = MF.getFunction()->getCallingConv(); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1133 | bool Is64Bit = Subtarget->is64Bit(); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1134 | |
| 1135 | assert(!(isVarArg && CC == CallingConv::Fast) && |
| 1136 | "Var args not supported with calling convention fastcc"); |
| 1137 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1138 | // Assign locations to all of the incoming arguments. |
| 1139 | SmallVector<CCValAssign, 16> ArgLocs; |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1140 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1141 | CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op)); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1142 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1143 | SmallVector<SDOperand, 8> ArgValues; |
| 1144 | unsigned LastVal = ~0U; |
| 1145 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1146 | CCValAssign &VA = ArgLocs[i]; |
| 1147 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 1148 | // places. |
| 1149 | assert(VA.getValNo() != LastVal && |
| 1150 | "Don't support value assigned to multiple locs yet"); |
| 1151 | LastVal = VA.getValNo(); |
| 1152 | |
| 1153 | if (VA.isRegLoc()) { |
| 1154 | MVT::ValueType RegVT = VA.getLocVT(); |
| 1155 | TargetRegisterClass *RC; |
| 1156 | if (RegVT == MVT::i32) |
| 1157 | RC = X86::GR32RegisterClass; |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1158 | else if (Is64Bit && RegVT == MVT::i64) |
| 1159 | RC = X86::GR64RegisterClass; |
| 1160 | else if (Is64Bit && RegVT == MVT::f32) |
| 1161 | RC = X86::FR32RegisterClass; |
| 1162 | else if (Is64Bit && RegVT == MVT::f64) |
| 1163 | RC = X86::FR64RegisterClass; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1164 | else { |
| 1165 | assert(MVT::isVector(RegVT)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1166 | if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) { |
| 1167 | RC = X86::GR64RegisterClass; // MMX values are passed in GPRs. |
| 1168 | RegVT = MVT::i64; |
| 1169 | } else |
| 1170 | RC = X86::VR128RegisterClass; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1171 | } |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1172 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1173 | unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); |
| 1174 | SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT); |
| 1175 | |
| 1176 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 1177 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 1178 | // right size. |
| 1179 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 1180 | ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, |
| 1181 | DAG.getValueType(VA.getValVT())); |
| 1182 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 1183 | ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, |
| 1184 | DAG.getValueType(VA.getValVT())); |
| 1185 | |
| 1186 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1187 | ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); |
| 1188 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1189 | // Handle MMX values passed in GPRs. |
| 1190 | if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass && |
| 1191 | MVT::getSizeInBits(RegVT) == 64) |
| 1192 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue); |
| 1193 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1194 | ArgValues.push_back(ArgValue); |
| 1195 | } else { |
| 1196 | assert(VA.isMemLoc()); |
Rafael Espindola | 03cbeb7 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1197 | ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1200 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1201 | unsigned StackSize = CCInfo.getNextStackOffset(); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1202 | // align stack specially for tail calls |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1203 | if (CC == CallingConv::Fast) |
| 1204 | StackSize = GetAlignedArgumentStackSize(StackSize, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1205 | |
| 1206 | // If the function takes variable number of arguments, make a frame index for |
| 1207 | // the start of the first vararg value... for expansion of llvm.va_start. |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1208 | if (isVarArg) { |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1209 | if (Is64Bit || CC != CallingConv::X86_FastCall) { |
| 1210 | VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); |
| 1211 | } |
| 1212 | if (Is64Bit) { |
| 1213 | static const unsigned GPR64ArgRegs[] = { |
| 1214 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1215 | }; |
| 1216 | static const unsigned XMMArgRegs[] = { |
| 1217 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1218 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1219 | }; |
| 1220 | |
| 1221 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6); |
| 1222 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
| 1223 | |
| 1224 | // For X86-64, if there are vararg parameters that are passed via |
| 1225 | // registers, then we must store them to their spots on the stack so they |
| 1226 | // may be loaded by deferencing the result of va_next. |
| 1227 | VarArgsGPOffset = NumIntRegs * 8; |
| 1228 | VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16; |
| 1229 | RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16); |
| 1230 | |
| 1231 | // Store the integer parameter registers. |
| 1232 | SmallVector<SDOperand, 8> MemOps; |
| 1233 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
| 1234 | SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1235 | DAG.getIntPtrConstant(VarArgsGPOffset)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1236 | for (; NumIntRegs != 6; ++NumIntRegs) { |
| 1237 | unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs], |
| 1238 | X86::GR64RegisterClass); |
| 1239 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64); |
| 1240 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
| 1241 | MemOps.push_back(Store); |
| 1242 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1243 | DAG.getIntPtrConstant(8)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | // Now store the XMM (fp + vector) parameter registers. |
| 1247 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1248 | DAG.getIntPtrConstant(VarArgsFPOffset)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1249 | for (; NumXMMRegs != 8; ++NumXMMRegs) { |
| 1250 | unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], |
| 1251 | X86::VR128RegisterClass); |
| 1252 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32); |
| 1253 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
| 1254 | MemOps.push_back(Store); |
| 1255 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1256 | DAG.getIntPtrConstant(16)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1257 | } |
| 1258 | if (!MemOps.empty()) |
| 1259 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1260 | &MemOps[0], MemOps.size()); |
| 1261 | } |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1262 | } |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1265 | // arguments and the arguments after the retaddr has been pushed are |
| 1266 | // aligned. |
| 1267 | if (!Is64Bit && CC == CallingConv::X86_FastCall && |
| 1268 | !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() && |
| 1269 | (StackSize & 7) == 0) |
| 1270 | StackSize += 4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1271 | |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1272 | ArgValues.push_back(Root); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1273 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1274 | // Some CCs need callee pop. |
| 1275 | if (IsCalleePop(Op)) { |
| 1276 | BytesToPopOnReturn = StackSize; // Callee pops everything. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1277 | BytesCallerReserves = 0; |
| 1278 | } else { |
| 1279 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1280 | // If this is an sret function, the return should pop the hidden pointer. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1281 | if (!Is64Bit && ArgsAreStructReturn(Op)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1282 | BytesToPopOnReturn = 4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1283 | BytesCallerReserves = StackSize; |
| 1284 | } |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1285 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1286 | if (!Is64Bit) { |
| 1287 | RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only. |
| 1288 | if (CC == CallingConv::X86_FastCall) |
| 1289 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 1290 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1291 | |
Anton Korobeynikov | e844e47 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1292 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Return the new list of results. |
| 1295 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), |
| 1296 | &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); |
| 1297 | } |
| 1298 | |
Evan Cheng | bc077bf | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1299 | SDOperand |
| 1300 | X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG, |
| 1301 | const SDOperand &StackPtr, |
| 1302 | const CCValAssign &VA, |
| 1303 | SDOperand Chain, |
| 1304 | SDOperand Arg) { |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1305 | SDOperand PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()); |
Evan Cheng | bc077bf | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1306 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1307 | SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo()); |
| 1308 | unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue(); |
| 1309 | if (Flags & ISD::ParamFlags::ByVal) { |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1310 | return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG); |
Evan Cheng | bc077bf | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1311 | } |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1312 | return DAG.getStore(Chain, Arg, PtrOff, NULL, 0); |
Evan Cheng | bc077bf | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1315 | SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
| 1316 | MachineFunction &MF = DAG.getMachineFunction(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1317 | SDOperand Chain = Op.getOperand(0); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1318 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1319 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1320 | bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0 |
| 1321 | && CC == CallingConv::Fast && PerformTailCallOpt; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1322 | SDOperand Callee = Op.getOperand(4); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1323 | bool Is64Bit = Subtarget->is64Bit(); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1324 | |
| 1325 | assert(!(isVarArg && CC == CallingConv::Fast) && |
| 1326 | "Var args not supported with calling convention fastcc"); |
| 1327 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1328 | // Analyze operands of the call, assigning locations to each operand. |
| 1329 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1330 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1331 | CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Get a count of how many bytes are to be pushed on the stack. |
| 1334 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1335 | if (CC == CallingConv::Fast) |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1336 | NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1337 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1338 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1339 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1340 | if (!Is64Bit && CC == CallingConv::X86_FastCall && |
| 1341 | !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() && |
| 1342 | (NumBytes & 7) == 0) |
| 1343 | NumBytes += 4; |
| 1344 | |
| 1345 | int FPDiff = 0; |
| 1346 | if (IsTailCall) { |
| 1347 | // Lower arguments at fp - stackoffset + fpdiff. |
| 1348 | unsigned NumBytesCallerPushed = |
| 1349 | MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn(); |
| 1350 | FPDiff = NumBytesCallerPushed - NumBytes; |
| 1351 | |
| 1352 | // Set the delta of movement of the returnaddr stackslot. |
| 1353 | // But only set if delta is greater than previous delta. |
| 1354 | if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta())) |
| 1355 | MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff); |
| 1356 | } |
| 1357 | |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1358 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1359 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1360 | SDOperand RetAddrFrIdx, NewRetAddrFrIdx; |
| 1361 | if (IsTailCall) { |
| 1362 | // Adjust the Return address stack slot. |
| 1363 | if (FPDiff) { |
| 1364 | MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32; |
| 1365 | RetAddrFrIdx = getReturnAddressFrameIndex(DAG); |
| 1366 | // Load the "old" Return address. |
| 1367 | RetAddrFrIdx = |
| 1368 | DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0); |
| 1369 | // Calculate the new stack slot for the return address. |
| 1370 | int SlotSize = Is64Bit ? 8 : 4; |
| 1371 | int NewReturnAddrFI = |
| 1372 | MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize); |
| 1373 | NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT); |
| 1374 | Chain = SDOperand(RetAddrFrIdx.Val, 1); |
| 1375 | } |
| 1376 | } |
| 1377 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1378 | SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; |
| 1379 | SmallVector<SDOperand, 8> MemOpChains; |
| 1380 | |
| 1381 | SDOperand StackPtr; |
| 1382 | |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1383 | // Walk the register/memloc assignments, inserting copies/loads. For tail |
| 1384 | // calls, lower arguments which could otherwise be possibly overwritten to the |
| 1385 | // stack slot where they would go on normal function calls. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1386 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1387 | CCValAssign &VA = ArgLocs[i]; |
| 1388 | SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); |
| 1389 | |
| 1390 | // Promote the value if needed. |
| 1391 | switch (VA.getLocInfo()) { |
| 1392 | default: assert(0 && "Unknown loc info!"); |
| 1393 | case CCValAssign::Full: break; |
| 1394 | case CCValAssign::SExt: |
| 1395 | Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); |
| 1396 | break; |
| 1397 | case CCValAssign::ZExt: |
| 1398 | Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); |
| 1399 | break; |
| 1400 | case CCValAssign::AExt: |
| 1401 | Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); |
| 1402 | break; |
| 1403 | } |
| 1404 | |
| 1405 | if (VA.isRegLoc()) { |
| 1406 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 1407 | } else { |
Arnold Schwaighofer | 0e3c27e | 2008-01-11 17:10:15 +0000 | [diff] [blame] | 1408 | if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) { |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1409 | assert(VA.isMemLoc()); |
| 1410 | if (StackPtr.Val == 0) |
| 1411 | StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy()); |
| 1412 | |
| 1413 | MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain, |
| 1414 | Arg)); |
| 1415 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (!MemOpChains.empty()) |
| 1420 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1421 | &MemOpChains[0], MemOpChains.size()); |
| 1422 | |
| 1423 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1424 | // and flag operands which copy the outgoing args into registers. |
| 1425 | SDOperand InFlag; |
| 1426 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1427 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1428 | InFlag); |
| 1429 | InFlag = Chain.getValue(1); |
| 1430 | } |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1431 | |
| 1432 | if (IsTailCall) |
| 1433 | InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output? |
| 1434 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1435 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 1436 | // GOT pointer. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1437 | // Does not work with tail call since ebx is not restored correctly by |
| 1438 | // tailcaller. TODO: at least for x86 - verify for x86-64 |
| 1439 | if (!IsTailCall && !Is64Bit && |
| 1440 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1441 | Subtarget->isPICStyleGOT()) { |
| 1442 | Chain = DAG.getCopyToReg(Chain, X86::EBX, |
| 1443 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 1444 | InFlag); |
| 1445 | InFlag = Chain.getValue(1); |
| 1446 | } |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1447 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1448 | if (Is64Bit && isVarArg) { |
| 1449 | // From AMD64 ABI document: |
| 1450 | // For calls that may call functions that use varargs or stdargs |
| 1451 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 1452 | // the declaration) %al is used as hidden argument to specify the number |
| 1453 | // of SSE registers used. The contents of %al do not need to match exactly |
| 1454 | // the number of registers, but must be an ubound on the number of SSE |
| 1455 | // registers used and is in the range 0 - 8 inclusive. |
| 1456 | |
| 1457 | // Count the number of XMM registers allocated. |
| 1458 | static const unsigned XMMArgRegs[] = { |
| 1459 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1460 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1461 | }; |
| 1462 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
| 1463 | |
| 1464 | Chain = DAG.getCopyToReg(Chain, X86::AL, |
| 1465 | DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); |
| 1466 | InFlag = Chain.getValue(1); |
| 1467 | } |
| 1468 | |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1469 | // For tail calls lower the arguments to the 'real' stack slot. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1470 | if (IsTailCall) { |
| 1471 | SmallVector<SDOperand, 8> MemOpChains2; |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1472 | SDOperand FIN; |
| 1473 | int FI = 0; |
| 1474 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1475 | CCValAssign &VA = ArgLocs[i]; |
| 1476 | if (!VA.isRegLoc()) { |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1477 | assert(VA.isMemLoc()); |
| 1478 | SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1479 | SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo()); |
| 1480 | unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue(); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1481 | // Create frame index. |
| 1482 | int32_t Offset = VA.getLocMemOffset()+FPDiff; |
| 1483 | uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8; |
| 1484 | FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset); |
| 1485 | FIN = DAG.getFrameIndex(FI, MVT::i32); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1486 | SDOperand Source = Arg; |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1487 | if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) { |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1488 | // Copy from stack slots to stack slot of a tail called function. This |
| 1489 | // needs to be done because if we would lower the arguments directly |
| 1490 | // to their real stack slot we might end up overwriting each other. |
| 1491 | // Get source stack slot. |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1492 | Source = DAG.getIntPtrConstant(VA.getLocMemOffset()); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1493 | if (StackPtr.Val == 0) |
| 1494 | StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy()); |
| 1495 | Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source); |
| 1496 | if ((Flags & ISD::ParamFlags::ByVal)==0) |
Duncan Sands | 2298163 | 2008-01-13 21:20:29 +0000 | [diff] [blame] | 1497 | Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1500 | if (Flags & ISD::ParamFlags::ByVal) { |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1501 | // Copy relative to framepointer. |
| 1502 | MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain, |
| 1503 | Flags, DAG)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1504 | } else { |
Evan Cheng | 5817a0e | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1505 | // Store relative to framepointer. |
| 1506 | MemOpChains2.push_back(DAG.getStore(Chain, Source, FIN, NULL, 0)); |
Arnold Schwaighofer | 449b01a | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1507 | } |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | if (!MemOpChains2.empty()) |
| 1512 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
Arnold Schwaighofer | dfb2130 | 2008-01-11 14:34:56 +0000 | [diff] [blame] | 1513 | &MemOpChains2[0], MemOpChains2.size()); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1514 | |
| 1515 | // Store the return address to the appropriate stack slot. |
| 1516 | if (FPDiff) |
| 1517 | Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0); |
| 1518 | } |
| 1519 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1520 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1521 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 1522 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 1523 | // We should use extra load for direct calls to dllimported functions in |
| 1524 | // non-JIT mode. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1525 | if ((IsTailCall || !Is64Bit || |
| 1526 | getTargetMachine().getCodeModel() != CodeModel::Large) |
| 1527 | && !Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1528 | getTargetMachine(), true)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1529 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1530 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1531 | if (IsTailCall || !Is64Bit || |
| 1532 | getTargetMachine().getCodeModel() != CodeModel::Large) |
| 1533 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 1534 | } else if (IsTailCall) { |
| 1535 | assert(Callee.getOpcode() == ISD::LOAD && |
| 1536 | "Function destination must be loaded into virtual register"); |
| 1537 | unsigned Opc = Is64Bit ? X86::R9 : X86::ECX; |
| 1538 | |
| 1539 | Chain = DAG.getCopyToReg(Chain, |
| 1540 | DAG.getRegister(Opc, getPointerTy()) , |
| 1541 | Callee,InFlag); |
| 1542 | Callee = DAG.getRegister(Opc, getPointerTy()); |
| 1543 | // Add register as live out. |
| 1544 | DAG.getMachineFunction().getRegInfo().addLiveOut(Opc); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1547 | // Returns a chain & a flag for retval copy to use. |
| 1548 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 1549 | SmallVector<SDOperand, 8> Ops; |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (IsTailCall) { |
| 1552 | Ops.push_back(Chain); |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1553 | Ops.push_back(DAG.getIntPtrConstant(NumBytes)); |
| 1554 | Ops.push_back(DAG.getIntPtrConstant(0)); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1555 | if (InFlag.Val) |
| 1556 | Ops.push_back(InFlag); |
| 1557 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
| 1558 | InFlag = Chain.getValue(1); |
| 1559 | |
| 1560 | // Returns a chain & a flag for retval copy to use. |
| 1561 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 1562 | Ops.clear(); |
| 1563 | } |
| 1564 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1565 | Ops.push_back(Chain); |
| 1566 | Ops.push_back(Callee); |
| 1567 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1568 | if (IsTailCall) |
| 1569 | Ops.push_back(DAG.getConstant(FPDiff, MVT::i32)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Add an implicit use GOT pointer in EBX. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1572 | if (!IsTailCall && !Is64Bit && |
| 1573 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1574 | Subtarget->isPICStyleGOT()) |
| 1575 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1576 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1577 | // Add argument registers to the end of the list so that they are known live |
| 1578 | // into the call. |
Evan Cheng | e14fc24 | 2008-01-07 23:08:23 +0000 | [diff] [blame] | 1579 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 1580 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 1581 | RegsToPass[i].second.getValueType())); |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1582 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1583 | if (InFlag.Val) |
| 1584 | Ops.push_back(InFlag); |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1585 | |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1586 | if (IsTailCall) { |
| 1587 | assert(InFlag.Val && |
| 1588 | "Flag must be set. Depend on flag being set in LowerRET"); |
| 1589 | Chain = DAG.getNode(X86ISD::TAILCALL, |
| 1590 | Op.Val->getVTList(), &Ops[0], Ops.size()); |
| 1591 | |
| 1592 | return SDOperand(Chain.Val, Op.ResNo); |
| 1593 | } |
| 1594 | |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1595 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1596 | InFlag = Chain.getValue(1); |
| 1597 | |
| 1598 | // Create the CALLSEQ_END node. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1599 | unsigned NumBytesForCalleeToPush; |
| 1600 | if (IsCalleePop(Op)) |
| 1601 | NumBytesForCalleeToPush = NumBytes; // Callee pops everything |
| 1602 | else if (!Is64Bit && CallIsStructReturn(Op)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1603 | // If this is is a call to a struct-return function, the callee |
| 1604 | // pops the hidden struct pointer, so we have to push it back. |
| 1605 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1606 | NumBytesForCalleeToPush = 4; |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1607 | else |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1608 | NumBytesForCalleeToPush = 0; // Callee pops nothing. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1609 | |
Gordon Henriksen | 6bbcc67 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1610 | // Returns a flag for retval copy to use. |
Bill Wendling | 22f8deb | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1611 | Chain = DAG.getCALLSEQ_END(Chain, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1612 | DAG.getIntPtrConstant(NumBytes), |
| 1613 | DAG.getIntPtrConstant(NumBytesForCalleeToPush), |
Bill Wendling | 22f8deb | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1614 | InFlag); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1615 | InFlag = Chain.getValue(1); |
| 1616 | |
| 1617 | // Handle result values, copying them out of physregs into vregs that we |
| 1618 | // return. |
| 1619 | return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); |
| 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1624 | // Fast Calling Convention (tail call) implementation |
| 1625 | //===----------------------------------------------------------------------===// |
| 1626 | |
| 1627 | // Like std call, callee cleans arguments, convention except that ECX is |
| 1628 | // reserved for storing the tail called function address. Only 2 registers are |
| 1629 | // free for argument passing (inreg). Tail call optimization is performed |
| 1630 | // provided: |
| 1631 | // * tailcallopt is enabled |
| 1632 | // * caller/callee are fastcc |
| 1633 | // * elf/pic is disabled OR |
| 1634 | // * elf/pic enabled + callee is in module + callee has |
| 1635 | // visibility protected or hidden |
Arnold Schwaighofer | 373e865 | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 1636 | // To keep the stack aligned according to platform abi the function |
| 1637 | // GetAlignedArgumentStackSize ensures that argument delta is always multiples |
| 1638 | // of stack alignment. (Dynamic linkers need this - darwin's dyld for example) |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1639 | // If a tail called function callee has more arguments than the caller the |
| 1640 | // caller needs to make sure that there is room to move the RETADDR to. This is |
Arnold Schwaighofer | 373e865 | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 1641 | // achieved by reserving an area the size of the argument delta right after the |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1642 | // original REtADDR, but before the saved framepointer or the spilled registers |
| 1643 | // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) |
| 1644 | // stack layout: |
| 1645 | // arg1 |
| 1646 | // arg2 |
| 1647 | // RETADDR |
| 1648 | // [ new RETADDR |
| 1649 | // move area ] |
| 1650 | // (possible EBP) |
| 1651 | // ESI |
| 1652 | // EDI |
| 1653 | // local1 .. |
| 1654 | |
| 1655 | /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned |
| 1656 | /// for a 16 byte align requirement. |
| 1657 | unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, |
| 1658 | SelectionDAG& DAG) { |
| 1659 | if (PerformTailCallOpt) { |
| 1660 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1661 | const TargetMachine &TM = MF.getTarget(); |
| 1662 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 1663 | unsigned StackAlignment = TFI.getStackAlignment(); |
| 1664 | uint64_t AlignMask = StackAlignment - 1; |
| 1665 | int64_t Offset = StackSize; |
| 1666 | unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4; |
| 1667 | if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) { |
| 1668 | // Number smaller than 12 so just add the difference. |
| 1669 | Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask)); |
| 1670 | } else { |
| 1671 | // Mask out lower bits, add stackalignment once plus the 12 bytes. |
| 1672 | Offset = ((~AlignMask) & Offset) + StackAlignment + |
| 1673 | (StackAlignment-SlotSize); |
| 1674 | } |
| 1675 | StackSize = Offset; |
| 1676 | } |
| 1677 | return StackSize; |
| 1678 | } |
| 1679 | |
| 1680 | /// IsEligibleForTailCallElimination - Check to see whether the next instruction |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1681 | /// following the call is a return. A function is eligible if caller/callee |
| 1682 | /// calling conventions match, currently only fastcc supports tail calls, and |
| 1683 | /// the function CALL is immediatly followed by a RET. |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1684 | bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call, |
| 1685 | SDOperand Ret, |
| 1686 | SelectionDAG& DAG) const { |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1687 | if (!PerformTailCallOpt) |
| 1688 | return false; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Check whether CALL node immediatly preceeds the RET node and whether the |
| 1691 | // return uses the result of the node or is a void return. |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1692 | unsigned NumOps = Ret.getNumOperands(); |
| 1693 | if ((NumOps == 1 && |
| 1694 | (Ret.getOperand(0) == SDOperand(Call.Val,1) || |
| 1695 | Ret.getOperand(0) == SDOperand(Call.Val,0))) || |
Evan Cheng | 26c0e98 | 2007-11-02 17:45:40 +0000 | [diff] [blame] | 1696 | (NumOps > 1 && |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1697 | Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) && |
| 1698 | Ret.getOperand(1) == SDOperand(Call.Val,0))) { |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1699 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1700 | unsigned CallerCC = MF.getFunction()->getCallingConv(); |
| 1701 | unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue(); |
| 1702 | if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { |
| 1703 | SDOperand Callee = Call.getOperand(4); |
| 1704 | // On elf/pic %ebx needs to be livein. |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1705 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ || |
| 1706 | !Subtarget->isPICStyleGOT()) |
| 1707 | return true; |
| 1708 | |
| 1709 | // Can only do local tail calls with PIC. |
Gordon Henriksen | 18ace10 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1710 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 1711 | return G->getGlobal()->hasHiddenVisibility() |
| 1712 | || G->getGlobal()->hasProtectedVisibility(); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
Evan Cheng | e7a8739 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1715 | |
| 1716 | return false; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1719 | //===----------------------------------------------------------------------===// |
| 1720 | // Other Lowering Hooks |
| 1721 | //===----------------------------------------------------------------------===// |
| 1722 | |
| 1723 | |
| 1724 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
Anton Korobeynikov | e844e47 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1725 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1726 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1727 | int ReturnAddrIndex = FuncInfo->getRAIndex(); |
| 1728 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1729 | if (ReturnAddrIndex == 0) { |
| 1730 | // Set up a frame object for the return address. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1731 | if (Subtarget->is64Bit()) |
| 1732 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8); |
| 1733 | else |
| 1734 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
Anton Korobeynikov | e844e47 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1735 | |
| 1736 | FuncInfo->setRAIndex(ReturnAddrIndex); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 1745 | /// specific condition code. It returns a false if it cannot do a direct |
| 1746 | /// translation. X86CC is the translated CondCode. LHS/RHS are modified as |
| 1747 | /// needed. |
| 1748 | static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
| 1749 | unsigned &X86CC, SDOperand &LHS, SDOperand &RHS, |
| 1750 | SelectionDAG &DAG) { |
| 1751 | X86CC = X86::COND_INVALID; |
| 1752 | if (!isFP) { |
| 1753 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 1754 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 1755 | // X > -1 -> X == 0, jump !sign. |
| 1756 | RHS = DAG.getConstant(0, RHS.getValueType()); |
| 1757 | X86CC = X86::COND_NS; |
| 1758 | return true; |
| 1759 | } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| 1760 | // X < 0 -> X == 0, jump on sign. |
| 1761 | X86CC = X86::COND_S; |
| 1762 | return true; |
Dan Gohman | 37b3426 | 2007-09-17 14:49:27 +0000 | [diff] [blame] | 1763 | } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) { |
| 1764 | // X < 1 -> X <= 0 |
| 1765 | RHS = DAG.getConstant(0, RHS.getValueType()); |
| 1766 | X86CC = X86::COND_LE; |
| 1767 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | switch (SetCCOpcode) { |
| 1772 | default: break; |
| 1773 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
| 1774 | case ISD::SETGT: X86CC = X86::COND_G; break; |
| 1775 | case ISD::SETGE: X86CC = X86::COND_GE; break; |
| 1776 | case ISD::SETLT: X86CC = X86::COND_L; break; |
| 1777 | case ISD::SETLE: X86CC = X86::COND_LE; break; |
| 1778 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 1779 | case ISD::SETULT: X86CC = X86::COND_B; break; |
| 1780 | case ISD::SETUGT: X86CC = X86::COND_A; break; |
| 1781 | case ISD::SETULE: X86CC = X86::COND_BE; break; |
| 1782 | case ISD::SETUGE: X86CC = X86::COND_AE; break; |
| 1783 | } |
| 1784 | } else { |
| 1785 | // On a floating point condition, the flags are set as follows: |
| 1786 | // ZF PF CF op |
| 1787 | // 0 | 0 | 0 | X > Y |
| 1788 | // 0 | 0 | 1 | X < Y |
| 1789 | // 1 | 0 | 0 | X == Y |
| 1790 | // 1 | 1 | 1 | unordered |
| 1791 | bool Flip = false; |
| 1792 | switch (SetCCOpcode) { |
| 1793 | default: break; |
| 1794 | case ISD::SETUEQ: |
| 1795 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
| 1796 | case ISD::SETOLT: Flip = true; // Fallthrough |
| 1797 | case ISD::SETOGT: |
| 1798 | case ISD::SETGT: X86CC = X86::COND_A; break; |
| 1799 | case ISD::SETOLE: Flip = true; // Fallthrough |
| 1800 | case ISD::SETOGE: |
| 1801 | case ISD::SETGE: X86CC = X86::COND_AE; break; |
| 1802 | case ISD::SETUGT: Flip = true; // Fallthrough |
| 1803 | case ISD::SETULT: |
| 1804 | case ISD::SETLT: X86CC = X86::COND_B; break; |
| 1805 | case ISD::SETUGE: Flip = true; // Fallthrough |
| 1806 | case ISD::SETULE: |
| 1807 | case ISD::SETLE: X86CC = X86::COND_BE; break; |
| 1808 | case ISD::SETONE: |
| 1809 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 1810 | case ISD::SETUO: X86CC = X86::COND_P; break; |
| 1811 | case ISD::SETO: X86CC = X86::COND_NP; break; |
| 1812 | } |
| 1813 | if (Flip) |
| 1814 | std::swap(LHS, RHS); |
| 1815 | } |
| 1816 | |
| 1817 | return X86CC != X86::COND_INVALID; |
| 1818 | } |
| 1819 | |
| 1820 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 1821 | /// code. Current x86 isa includes the following FP cmov instructions: |
| 1822 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
| 1823 | static bool hasFPCMov(unsigned X86CC) { |
| 1824 | switch (X86CC) { |
| 1825 | default: |
| 1826 | return false; |
| 1827 | case X86::COND_B: |
| 1828 | case X86::COND_BE: |
| 1829 | case X86::COND_E: |
| 1830 | case X86::COND_P: |
| 1831 | case X86::COND_A: |
| 1832 | case X86::COND_AE: |
| 1833 | case X86::COND_NE: |
| 1834 | case X86::COND_NP: |
| 1835 | return true; |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return |
| 1840 | /// true if Op is undef or if its value falls within the specified range (L, H]. |
| 1841 | static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) { |
| 1842 | if (Op.getOpcode() == ISD::UNDEF) |
| 1843 | return true; |
| 1844 | |
| 1845 | unsigned Val = cast<ConstantSDNode>(Op)->getValue(); |
| 1846 | return (Val >= Low && Val < Hi); |
| 1847 | } |
| 1848 | |
| 1849 | /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return |
| 1850 | /// true if Op is undef or if its value equal to the specified value. |
| 1851 | static bool isUndefOrEqual(SDOperand Op, unsigned Val) { |
| 1852 | if (Op.getOpcode() == ISD::UNDEF) |
| 1853 | return true; |
| 1854 | return cast<ConstantSDNode>(Op)->getValue() == Val; |
| 1855 | } |
| 1856 | |
| 1857 | /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1858 | /// specifies a shuffle of elements that is suitable for input to PSHUFD. |
| 1859 | bool X86::isPSHUFDMask(SDNode *N) { |
| 1860 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1861 | |
Dan Gohman | 7dc1901 | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 1862 | if (N->getNumOperands() != 2 && N->getNumOperands() != 4) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | |
| 1865 | // Check if the value doesn't reference the second vector. |
| 1866 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 1867 | SDOperand Arg = N->getOperand(i); |
| 1868 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1869 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Dan Gohman | 7dc1901 | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 1870 | if (cast<ConstantSDNode>(Arg)->getValue() >= e) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1871 | return false; |
| 1872 | } |
| 1873 | |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1878 | /// specifies a shuffle of elements that is suitable for input to PSHUFHW. |
| 1879 | bool X86::isPSHUFHWMask(SDNode *N) { |
| 1880 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1881 | |
| 1882 | if (N->getNumOperands() != 8) |
| 1883 | return false; |
| 1884 | |
| 1885 | // Lower quadword copied in order. |
| 1886 | for (unsigned i = 0; i != 4; ++i) { |
| 1887 | SDOperand Arg = N->getOperand(i); |
| 1888 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1889 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1890 | if (cast<ConstantSDNode>(Arg)->getValue() != i) |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | // Upper quadword shuffled. |
| 1895 | for (unsigned i = 4; i != 8; ++i) { |
| 1896 | SDOperand Arg = N->getOperand(i); |
| 1897 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1898 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1899 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1900 | if (Val < 4 || Val > 7) |
| 1901 | return false; |
| 1902 | } |
| 1903 | |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
| 1907 | /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1908 | /// specifies a shuffle of elements that is suitable for input to PSHUFLW. |
| 1909 | bool X86::isPSHUFLWMask(SDNode *N) { |
| 1910 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1911 | |
| 1912 | if (N->getNumOperands() != 8) |
| 1913 | return false; |
| 1914 | |
| 1915 | // Upper quadword copied in order. |
| 1916 | for (unsigned i = 4; i != 8; ++i) |
| 1917 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 1918 | return false; |
| 1919 | |
| 1920 | // Lower quadword shuffled. |
| 1921 | for (unsigned i = 0; i != 4; ++i) |
| 1922 | if (!isUndefOrInRange(N->getOperand(i), 0, 4)) |
| 1923 | return false; |
| 1924 | |
| 1925 | return true; |
| 1926 | } |
| 1927 | |
| 1928 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1929 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
| 1930 | static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) { |
| 1931 | if (NumElems != 2 && NumElems != 4) return false; |
| 1932 | |
| 1933 | unsigned Half = NumElems / 2; |
| 1934 | for (unsigned i = 0; i < Half; ++i) |
| 1935 | if (!isUndefOrInRange(Elems[i], 0, NumElems)) |
| 1936 | return false; |
| 1937 | for (unsigned i = Half; i < NumElems; ++i) |
| 1938 | if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2)) |
| 1939 | return false; |
| 1940 | |
| 1941 | return true; |
| 1942 | } |
| 1943 | |
| 1944 | bool X86::isSHUFPMask(SDNode *N) { |
| 1945 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1946 | return ::isSHUFPMask(N->op_begin(), N->getNumOperands()); |
| 1947 | } |
| 1948 | |
| 1949 | /// isCommutedSHUFP - Returns true if the shuffle mask is exactly |
| 1950 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 1951 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 1952 | /// the upper half to come from vector 2. |
| 1953 | static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) { |
| 1954 | if (NumOps != 2 && NumOps != 4) return false; |
| 1955 | |
| 1956 | unsigned Half = NumOps / 2; |
| 1957 | for (unsigned i = 0; i < Half; ++i) |
| 1958 | if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2)) |
| 1959 | return false; |
| 1960 | for (unsigned i = Half; i < NumOps; ++i) |
| 1961 | if (!isUndefOrInRange(Ops[i], 0, NumOps)) |
| 1962 | return false; |
| 1963 | return true; |
| 1964 | } |
| 1965 | |
| 1966 | static bool isCommutedSHUFP(SDNode *N) { |
| 1967 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1968 | return isCommutedSHUFP(N->op_begin(), N->getNumOperands()); |
| 1969 | } |
| 1970 | |
| 1971 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1972 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
| 1973 | bool X86::isMOVHLPSMask(SDNode *N) { |
| 1974 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1975 | |
| 1976 | if (N->getNumOperands() != 4) |
| 1977 | return false; |
| 1978 | |
| 1979 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
| 1980 | return isUndefOrEqual(N->getOperand(0), 6) && |
| 1981 | isUndefOrEqual(N->getOperand(1), 7) && |
| 1982 | isUndefOrEqual(N->getOperand(2), 2) && |
| 1983 | isUndefOrEqual(N->getOperand(3), 3); |
| 1984 | } |
| 1985 | |
| 1986 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 1987 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 1988 | /// <2, 3, 2, 3> |
| 1989 | bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) { |
| 1990 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1991 | |
| 1992 | if (N->getNumOperands() != 4) |
| 1993 | return false; |
| 1994 | |
| 1995 | // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3 |
| 1996 | return isUndefOrEqual(N->getOperand(0), 2) && |
| 1997 | isUndefOrEqual(N->getOperand(1), 3) && |
| 1998 | isUndefOrEqual(N->getOperand(2), 2) && |
| 1999 | isUndefOrEqual(N->getOperand(3), 3); |
| 2000 | } |
| 2001 | |
| 2002 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2003 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
| 2004 | bool X86::isMOVLPMask(SDNode *N) { |
| 2005 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2006 | |
| 2007 | unsigned NumElems = N->getNumOperands(); |
| 2008 | if (NumElems != 2 && NumElems != 4) |
| 2009 | return false; |
| 2010 | |
| 2011 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2012 | if (!isUndefOrEqual(N->getOperand(i), i + NumElems)) |
| 2013 | return false; |
| 2014 | |
| 2015 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
| 2016 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 2017 | return false; |
| 2018 | |
| 2019 | return true; |
| 2020 | } |
| 2021 | |
| 2022 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2023 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 2024 | /// and MOVLHPS. |
| 2025 | bool X86::isMOVHPMask(SDNode *N) { |
| 2026 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2027 | |
| 2028 | unsigned NumElems = N->getNumOperands(); |
| 2029 | if (NumElems != 2 && NumElems != 4) |
| 2030 | return false; |
| 2031 | |
| 2032 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2033 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 2034 | return false; |
| 2035 | |
| 2036 | for (unsigned i = 0; i < NumElems/2; ++i) { |
| 2037 | SDOperand Arg = N->getOperand(i + NumElems/2); |
| 2038 | if (!isUndefOrEqual(Arg, i + NumElems)) |
| 2039 | return false; |
| 2040 | } |
| 2041 | |
| 2042 | return true; |
| 2043 | } |
| 2044 | |
| 2045 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2046 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
| 2047 | bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts, |
| 2048 | bool V2IsSplat = false) { |
| 2049 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
| 2050 | return false; |
| 2051 | |
| 2052 | for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2053 | SDOperand BitI = Elts[i]; |
| 2054 | SDOperand BitI1 = Elts[i+1]; |
| 2055 | if (!isUndefOrEqual(BitI, j)) |
| 2056 | return false; |
| 2057 | if (V2IsSplat) { |
| 2058 | if (isUndefOrEqual(BitI1, NumElts)) |
| 2059 | return false; |
| 2060 | } else { |
| 2061 | if (!isUndefOrEqual(BitI1, j + NumElts)) |
| 2062 | return false; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | return true; |
| 2067 | } |
| 2068 | |
| 2069 | bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) { |
| 2070 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2071 | return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat); |
| 2072 | } |
| 2073 | |
| 2074 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2075 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
| 2076 | bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts, |
| 2077 | bool V2IsSplat = false) { |
| 2078 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
| 2079 | return false; |
| 2080 | |
| 2081 | for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2082 | SDOperand BitI = Elts[i]; |
| 2083 | SDOperand BitI1 = Elts[i+1]; |
| 2084 | if (!isUndefOrEqual(BitI, j + NumElts/2)) |
| 2085 | return false; |
| 2086 | if (V2IsSplat) { |
| 2087 | if (isUndefOrEqual(BitI1, NumElts)) |
| 2088 | return false; |
| 2089 | } else { |
| 2090 | if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) |
| 2091 | return false; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | return true; |
| 2096 | } |
| 2097 | |
| 2098 | bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) { |
| 2099 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2100 | return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat); |
| 2101 | } |
| 2102 | |
| 2103 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 2104 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 2105 | /// <0, 0, 1, 1> |
| 2106 | bool X86::isUNPCKL_v_undef_Mask(SDNode *N) { |
| 2107 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2108 | |
| 2109 | unsigned NumElems = N->getNumOperands(); |
| 2110 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2111 | return false; |
| 2112 | |
| 2113 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 2114 | SDOperand BitI = N->getOperand(i); |
| 2115 | SDOperand BitI1 = N->getOperand(i+1); |
| 2116 | |
| 2117 | if (!isUndefOrEqual(BitI, j)) |
| 2118 | return false; |
| 2119 | if (!isUndefOrEqual(BitI1, j)) |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
| 2123 | return true; |
| 2124 | } |
| 2125 | |
| 2126 | /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form |
| 2127 | /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, |
| 2128 | /// <2, 2, 3, 3> |
| 2129 | bool X86::isUNPCKH_v_undef_Mask(SDNode *N) { |
| 2130 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2131 | |
| 2132 | unsigned NumElems = N->getNumOperands(); |
| 2133 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2134 | return false; |
| 2135 | |
| 2136 | for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) { |
| 2137 | SDOperand BitI = N->getOperand(i); |
| 2138 | SDOperand BitI1 = N->getOperand(i + 1); |
| 2139 | |
| 2140 | if (!isUndefOrEqual(BitI, j)) |
| 2141 | return false; |
| 2142 | if (!isUndefOrEqual(BitI1, j)) |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
| 2146 | return true; |
| 2147 | } |
| 2148 | |
| 2149 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2150 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 2151 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
| 2152 | static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) { |
Evan Cheng | 62cdc64 | 2007-12-06 22:14:22 +0000 | [diff] [blame] | 2153 | if (NumElts != 2 && NumElts != 4) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2154 | return false; |
| 2155 | |
| 2156 | if (!isUndefOrEqual(Elts[0], NumElts)) |
| 2157 | return false; |
| 2158 | |
| 2159 | for (unsigned i = 1; i < NumElts; ++i) { |
| 2160 | if (!isUndefOrEqual(Elts[i], i)) |
| 2161 | return false; |
| 2162 | } |
| 2163 | |
| 2164 | return true; |
| 2165 | } |
| 2166 | |
| 2167 | bool X86::isMOVLMask(SDNode *N) { |
| 2168 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2169 | return ::isMOVLMask(N->op_begin(), N->getNumOperands()); |
| 2170 | } |
| 2171 | |
| 2172 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 2173 | /// of what x86 movss want. X86 movs requires the lowest element to be lowest |
| 2174 | /// element of vector 2 and the other elements to come from vector 1 in order. |
| 2175 | static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps, |
| 2176 | bool V2IsSplat = false, |
| 2177 | bool V2IsUndef = false) { |
| 2178 | if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) |
| 2179 | return false; |
| 2180 | |
| 2181 | if (!isUndefOrEqual(Ops[0], 0)) |
| 2182 | return false; |
| 2183 | |
| 2184 | for (unsigned i = 1; i < NumOps; ++i) { |
| 2185 | SDOperand Arg = Ops[i]; |
| 2186 | if (!(isUndefOrEqual(Arg, i+NumOps) || |
| 2187 | (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) || |
| 2188 | (V2IsSplat && isUndefOrEqual(Arg, NumOps)))) |
| 2189 | return false; |
| 2190 | } |
| 2191 | |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false, |
| 2196 | bool V2IsUndef = false) { |
| 2197 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2198 | return isCommutedMOVL(N->op_begin(), N->getNumOperands(), |
| 2199 | V2IsSplat, V2IsUndef); |
| 2200 | } |
| 2201 | |
| 2202 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2203 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
| 2204 | bool X86::isMOVSHDUPMask(SDNode *N) { |
| 2205 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2206 | |
| 2207 | if (N->getNumOperands() != 4) |
| 2208 | return false; |
| 2209 | |
| 2210 | // Expect 1, 1, 3, 3 |
| 2211 | for (unsigned i = 0; i < 2; ++i) { |
| 2212 | SDOperand Arg = N->getOperand(i); |
| 2213 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2214 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2215 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2216 | if (Val != 1) return false; |
| 2217 | } |
| 2218 | |
| 2219 | bool HasHi = false; |
| 2220 | for (unsigned i = 2; i < 4; ++i) { |
| 2221 | SDOperand Arg = N->getOperand(i); |
| 2222 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2223 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2224 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2225 | if (Val != 3) return false; |
| 2226 | HasHi = true; |
| 2227 | } |
| 2228 | |
| 2229 | // Don't use movshdup if it can be done with a shufps. |
| 2230 | return HasHi; |
| 2231 | } |
| 2232 | |
| 2233 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2234 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
| 2235 | bool X86::isMOVSLDUPMask(SDNode *N) { |
| 2236 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2237 | |
| 2238 | if (N->getNumOperands() != 4) |
| 2239 | return false; |
| 2240 | |
| 2241 | // Expect 0, 0, 2, 2 |
| 2242 | for (unsigned i = 0; i < 2; ++i) { |
| 2243 | SDOperand Arg = N->getOperand(i); |
| 2244 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2245 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2246 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2247 | if (Val != 0) return false; |
| 2248 | } |
| 2249 | |
| 2250 | bool HasHi = false; |
| 2251 | for (unsigned i = 2; i < 4; ++i) { |
| 2252 | SDOperand Arg = N->getOperand(i); |
| 2253 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2254 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2255 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2256 | if (Val != 2) return false; |
| 2257 | HasHi = true; |
| 2258 | } |
| 2259 | |
| 2260 | // Don't use movshdup if it can be done with a shufps. |
| 2261 | return HasHi; |
| 2262 | } |
| 2263 | |
| 2264 | /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2265 | /// specifies a identity operation on the LHS or RHS. |
| 2266 | static bool isIdentityMask(SDNode *N, bool RHS = false) { |
| 2267 | unsigned NumElems = N->getNumOperands(); |
| 2268 | for (unsigned i = 0; i < NumElems; ++i) |
| 2269 | if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0))) |
| 2270 | return false; |
| 2271 | return true; |
| 2272 | } |
| 2273 | |
| 2274 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2275 | /// a splat of a single element. |
| 2276 | static bool isSplatMask(SDNode *N) { |
| 2277 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2278 | |
| 2279 | // This is a splat operation if each element of the permute is the same, and |
| 2280 | // if the value doesn't reference the second vector. |
| 2281 | unsigned NumElems = N->getNumOperands(); |
| 2282 | SDOperand ElementBase; |
| 2283 | unsigned i = 0; |
| 2284 | for (; i != NumElems; ++i) { |
| 2285 | SDOperand Elt = N->getOperand(i); |
| 2286 | if (isa<ConstantSDNode>(Elt)) { |
| 2287 | ElementBase = Elt; |
| 2288 | break; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | if (!ElementBase.Val) |
| 2293 | return false; |
| 2294 | |
| 2295 | for (; i != NumElems; ++i) { |
| 2296 | SDOperand Arg = N->getOperand(i); |
| 2297 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2298 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2299 | if (Arg != ElementBase) return false; |
| 2300 | } |
| 2301 | |
| 2302 | // Make sure it is a splat of the first vector operand. |
| 2303 | return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems; |
| 2304 | } |
| 2305 | |
| 2306 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2307 | /// a splat of a single element and it's a 2 or 4 element mask. |
| 2308 | bool X86::isSplatMask(SDNode *N) { |
| 2309 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2310 | |
| 2311 | // We can only splat 64-bit, and 32-bit quantities with a single instruction. |
| 2312 | if (N->getNumOperands() != 4 && N->getNumOperands() != 2) |
| 2313 | return false; |
| 2314 | return ::isSplatMask(N); |
| 2315 | } |
| 2316 | |
| 2317 | /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2318 | /// specifies a splat of zero element. |
| 2319 | bool X86::isSplatLoMask(SDNode *N) { |
| 2320 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2321 | |
| 2322 | for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) |
| 2323 | if (!isUndefOrEqual(N->getOperand(i), 0)) |
| 2324 | return false; |
| 2325 | return true; |
| 2326 | } |
| 2327 | |
| 2328 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 2329 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 2330 | /// instructions. |
| 2331 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
| 2332 | unsigned NumOperands = N->getNumOperands(); |
| 2333 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 2334 | unsigned Mask = 0; |
| 2335 | for (unsigned i = 0; i < NumOperands; ++i) { |
| 2336 | unsigned Val = 0; |
| 2337 | SDOperand Arg = N->getOperand(NumOperands-i-1); |
| 2338 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2339 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2340 | if (Val >= NumOperands) Val -= NumOperands; |
| 2341 | Mask |= Val; |
| 2342 | if (i != NumOperands - 1) |
| 2343 | Mask <<= Shift; |
| 2344 | } |
| 2345 | |
| 2346 | return Mask; |
| 2347 | } |
| 2348 | |
| 2349 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 2350 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 2351 | /// instructions. |
| 2352 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
| 2353 | unsigned Mask = 0; |
| 2354 | // 8 nodes, but we only care about the last 4. |
| 2355 | for (unsigned i = 7; i >= 4; --i) { |
| 2356 | unsigned Val = 0; |
| 2357 | SDOperand Arg = N->getOperand(i); |
| 2358 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2359 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2360 | Mask |= (Val - 4); |
| 2361 | if (i != 4) |
| 2362 | Mask <<= 2; |
| 2363 | } |
| 2364 | |
| 2365 | return Mask; |
| 2366 | } |
| 2367 | |
| 2368 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2369 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2370 | /// instructions. |
| 2371 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
| 2372 | unsigned Mask = 0; |
| 2373 | // 8 nodes, but we only care about the first 4. |
| 2374 | for (int i = 3; i >= 0; --i) { |
| 2375 | unsigned Val = 0; |
| 2376 | SDOperand Arg = N->getOperand(i); |
| 2377 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2378 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2379 | Mask |= Val; |
| 2380 | if (i != 0) |
| 2381 | Mask <<= 2; |
| 2382 | } |
| 2383 | |
| 2384 | return Mask; |
| 2385 | } |
| 2386 | |
| 2387 | /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand |
| 2388 | /// specifies a 8 element shuffle that can be broken into a pair of |
| 2389 | /// PSHUFHW and PSHUFLW. |
| 2390 | static bool isPSHUFHW_PSHUFLWMask(SDNode *N) { |
| 2391 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2392 | |
| 2393 | if (N->getNumOperands() != 8) |
| 2394 | return false; |
| 2395 | |
| 2396 | // Lower quadword shuffled. |
| 2397 | for (unsigned i = 0; i != 4; ++i) { |
| 2398 | SDOperand Arg = N->getOperand(i); |
| 2399 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2400 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2401 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 2402 | if (Val >= 4) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2403 | return false; |
| 2404 | } |
| 2405 | |
| 2406 | // Upper quadword shuffled. |
| 2407 | for (unsigned i = 4; i != 8; ++i) { |
| 2408 | SDOperand Arg = N->getOperand(i); |
| 2409 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2410 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2411 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2412 | if (Val < 4 || Val > 7) |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
| 2416 | return true; |
| 2417 | } |
| 2418 | |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2419 | /// CommuteVectorShuffle - Swap vector_shuffle operands as well as |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2420 | /// values in ther permute mask. |
| 2421 | static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1, |
| 2422 | SDOperand &V2, SDOperand &Mask, |
| 2423 | SelectionDAG &DAG) { |
| 2424 | MVT::ValueType VT = Op.getValueType(); |
| 2425 | MVT::ValueType MaskVT = Mask.getValueType(); |
| 2426 | MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT); |
| 2427 | unsigned NumElems = Mask.getNumOperands(); |
| 2428 | SmallVector<SDOperand, 8> MaskVec; |
| 2429 | |
| 2430 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2431 | SDOperand Arg = Mask.getOperand(i); |
| 2432 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2433 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 2434 | continue; |
| 2435 | } |
| 2436 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2437 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2438 | if (Val < NumElems) |
| 2439 | MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); |
| 2440 | else |
| 2441 | MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); |
| 2442 | } |
| 2443 | |
| 2444 | std::swap(V1, V2); |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2445 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2446 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2447 | } |
| 2448 | |
Evan Cheng | a6769df | 2007-12-07 21:30:01 +0000 | [diff] [blame] | 2449 | /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming |
| 2450 | /// the two vector operands have swapped position. |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2451 | static |
| 2452 | SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) { |
| 2453 | MVT::ValueType MaskVT = Mask.getValueType(); |
| 2454 | MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT); |
| 2455 | unsigned NumElems = Mask.getNumOperands(); |
| 2456 | SmallVector<SDOperand, 8> MaskVec; |
| 2457 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2458 | SDOperand Arg = Mask.getOperand(i); |
| 2459 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2460 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 2461 | continue; |
| 2462 | } |
| 2463 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2464 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2465 | if (Val < NumElems) |
| 2466 | MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); |
| 2467 | else |
| 2468 | MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); |
| 2469 | } |
| 2470 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems); |
| 2471 | } |
| 2472 | |
| 2473 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2474 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2475 | /// match movhlps. The lower half elements should come from upper half of |
| 2476 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2477 | /// half of V2 (and in order). |
| 2478 | static bool ShouldXformToMOVHLPS(SDNode *Mask) { |
| 2479 | unsigned NumElems = Mask->getNumOperands(); |
| 2480 | if (NumElems != 4) |
| 2481 | return false; |
| 2482 | for (unsigned i = 0, e = 2; i != e; ++i) |
| 2483 | if (!isUndefOrEqual(Mask->getOperand(i), i+2)) |
| 2484 | return false; |
| 2485 | for (unsigned i = 2; i != 4; ++i) |
| 2486 | if (!isUndefOrEqual(Mask->getOperand(i), i+4)) |
| 2487 | return false; |
| 2488 | return true; |
| 2489 | } |
| 2490 | |
| 2491 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
| 2492 | /// is promoted to a vector. |
| 2493 | static inline bool isScalarLoadToVector(SDNode *N) { |
| 2494 | if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 2495 | N = N->getOperand(0).Val; |
| 2496 | return ISD::isNON_EXTLoad(N); |
| 2497 | } |
| 2498 | return false; |
| 2499 | } |
| 2500 | |
| 2501 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2502 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2503 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2504 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2505 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
| 2506 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) { |
| 2507 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
| 2508 | return false; |
| 2509 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 2510 | // load folding shufps op. |
| 2511 | if (ISD::isNON_EXTLoad(V2)) |
| 2512 | return false; |
| 2513 | |
| 2514 | unsigned NumElems = Mask->getNumOperands(); |
| 2515 | if (NumElems != 2 && NumElems != 4) |
| 2516 | return false; |
| 2517 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| 2518 | if (!isUndefOrEqual(Mask->getOperand(i), i)) |
| 2519 | return false; |
| 2520 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
| 2521 | if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems)) |
| 2522 | return false; |
| 2523 | return true; |
| 2524 | } |
| 2525 | |
| 2526 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2527 | /// all the same. |
| 2528 | static bool isSplatVector(SDNode *N) { |
| 2529 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2530 | return false; |
| 2531 | |
| 2532 | SDOperand SplatValue = N->getOperand(0); |
| 2533 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2534 | if (N->getOperand(i) != SplatValue) |
| 2535 | return false; |
| 2536 | return true; |
| 2537 | } |
| 2538 | |
| 2539 | /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| 2540 | /// to an undef. |
| 2541 | static bool isUndefShuffle(SDNode *N) { |
| 2542 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) |
| 2543 | return false; |
| 2544 | |
| 2545 | SDOperand V1 = N->getOperand(0); |
| 2546 | SDOperand V2 = N->getOperand(1); |
| 2547 | SDOperand Mask = N->getOperand(2); |
| 2548 | unsigned NumElems = Mask.getNumOperands(); |
| 2549 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2550 | SDOperand Arg = Mask.getOperand(i); |
| 2551 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2552 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2553 | if (Val < NumElems && V1.getOpcode() != ISD::UNDEF) |
| 2554 | return false; |
| 2555 | else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF) |
| 2556 | return false; |
| 2557 | } |
| 2558 | } |
| 2559 | return true; |
| 2560 | } |
| 2561 | |
| 2562 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2563 | /// constant +0.0. |
| 2564 | static inline bool isZeroNode(SDOperand Elt) { |
| 2565 | return ((isa<ConstantSDNode>(Elt) && |
| 2566 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 2567 | (isa<ConstantFPSDNode>(Elt) && |
Dale Johannesen | df8a831 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2568 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| 2572 | /// to an zero vector. |
| 2573 | static bool isZeroShuffle(SDNode *N) { |
| 2574 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) |
| 2575 | return false; |
| 2576 | |
| 2577 | SDOperand V1 = N->getOperand(0); |
| 2578 | SDOperand V2 = N->getOperand(1); |
| 2579 | SDOperand Mask = N->getOperand(2); |
| 2580 | unsigned NumElems = Mask.getNumOperands(); |
| 2581 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2582 | SDOperand Arg = Mask.getOperand(i); |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2583 | if (Arg.getOpcode() == ISD::UNDEF) |
| 2584 | continue; |
| 2585 | |
| 2586 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 2587 | if (Idx < NumElems) { |
| 2588 | unsigned Opc = V1.Val->getOpcode(); |
| 2589 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val)) |
| 2590 | continue; |
| 2591 | if (Opc != ISD::BUILD_VECTOR || |
| 2592 | !isZeroNode(V1.Val->getOperand(Idx))) |
| 2593 | return false; |
| 2594 | } else if (Idx >= NumElems) { |
| 2595 | unsigned Opc = V2.Val->getOpcode(); |
| 2596 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val)) |
| 2597 | continue; |
| 2598 | if (Opc != ISD::BUILD_VECTOR || |
| 2599 | !isZeroNode(V2.Val->getOperand(Idx - NumElems))) |
| 2600 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | return true; |
| 2604 | } |
| 2605 | |
| 2606 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2607 | /// |
| 2608 | static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) { |
| 2609 | assert(MVT::isVector(VT) && "Expected a vector type"); |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2610 | |
| 2611 | // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2612 | // type. This ensures they get CSE'd. |
| 2613 | SDOperand Cst = DAG.getTargetConstant(0, MVT::i32); |
| 2614 | SDOperand Vec; |
| 2615 | if (MVT::getSizeInBits(VT) == 64) // MMX |
| 2616 | Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst); |
| 2617 | else // SSE |
| 2618 | Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst); |
| 2619 | return DAG.getNode(ISD::BIT_CONVERT, VT, Vec); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2622 | /// getOnesVector - Returns a vector of specified type with all bits set. |
| 2623 | /// |
| 2624 | static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) { |
| 2625 | assert(MVT::isVector(VT) && "Expected a vector type"); |
| 2626 | |
| 2627 | // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2628 | // type. This ensures they get CSE'd. |
| 2629 | SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32); |
| 2630 | SDOperand Vec; |
| 2631 | if (MVT::getSizeInBits(VT) == 64) // MMX |
| 2632 | Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst); |
| 2633 | else // SSE |
| 2634 | Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst); |
| 2635 | return DAG.getNode(ISD::BIT_CONVERT, VT, Vec); |
| 2636 | } |
| 2637 | |
| 2638 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2639 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2640 | /// that point to V2 points to its first element. |
| 2641 | static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) { |
| 2642 | assert(Mask.getOpcode() == ISD::BUILD_VECTOR); |
| 2643 | |
| 2644 | bool Changed = false; |
| 2645 | SmallVector<SDOperand, 8> MaskVec; |
| 2646 | unsigned NumElems = Mask.getNumOperands(); |
| 2647 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2648 | SDOperand Arg = Mask.getOperand(i); |
| 2649 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2650 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2651 | if (Val > NumElems) { |
| 2652 | Arg = DAG.getConstant(NumElems, Arg.getValueType()); |
| 2653 | Changed = true; |
| 2654 | } |
| 2655 | } |
| 2656 | MaskVec.push_back(Arg); |
| 2657 | } |
| 2658 | |
| 2659 | if (Changed) |
| 2660 | Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), |
| 2661 | &MaskVec[0], MaskVec.size()); |
| 2662 | return Mask; |
| 2663 | } |
| 2664 | |
| 2665 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2666 | /// operation of specified width. |
| 2667 | static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2668 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2669 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
| 2670 | |
| 2671 | SmallVector<SDOperand, 8> MaskVec; |
| 2672 | MaskVec.push_back(DAG.getConstant(NumElems, BaseVT)); |
| 2673 | for (unsigned i = 1; i != NumElems; ++i) |
| 2674 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2675 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
| 2676 | } |
| 2677 | |
| 2678 | /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation |
| 2679 | /// of specified width. |
| 2680 | static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2681 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2682 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
| 2683 | SmallVector<SDOperand, 8> MaskVec; |
| 2684 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
| 2685 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2686 | MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT)); |
| 2687 | } |
| 2688 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
| 2689 | } |
| 2690 | |
| 2691 | /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation |
| 2692 | /// of specified width. |
| 2693 | static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2694 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2695 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
| 2696 | unsigned Half = NumElems/2; |
| 2697 | SmallVector<SDOperand, 8> MaskVec; |
| 2698 | for (unsigned i = 0; i != Half; ++i) { |
| 2699 | MaskVec.push_back(DAG.getConstant(i + Half, BaseVT)); |
| 2700 | MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT)); |
| 2701 | } |
| 2702 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
| 2703 | } |
| 2704 | |
| 2705 | /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32. |
| 2706 | /// |
| 2707 | static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) { |
| 2708 | SDOperand V1 = Op.getOperand(0); |
| 2709 | SDOperand Mask = Op.getOperand(2); |
| 2710 | MVT::ValueType VT = Op.getValueType(); |
| 2711 | unsigned NumElems = Mask.getNumOperands(); |
| 2712 | Mask = getUnpacklMask(NumElems, DAG); |
| 2713 | while (NumElems != 4) { |
| 2714 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask); |
| 2715 | NumElems >>= 1; |
| 2716 | } |
| 2717 | V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1); |
| 2718 | |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2719 | Mask = getZeroVector(MVT::v4i32, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2720 | SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, |
| 2721 | DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask); |
| 2722 | return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle); |
| 2723 | } |
| 2724 | |
| 2725 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2726 | /// vector of zero or undef vector. This produces a shuffle where the low |
| 2727 | /// element of V2 is swizzled into the zero/undef vector, landing at element |
| 2728 | /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3). |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2729 | static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT, |
| 2730 | unsigned NumElems, unsigned Idx, |
| 2731 | bool isZero, SelectionDAG &DAG) { |
| 2732 | SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT); |
| 2733 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2734 | MVT::ValueType EVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2735 | SmallVector<SDOperand, 16> MaskVec; |
| 2736 | for (unsigned i = 0; i != NumElems; ++i) |
| 2737 | if (i == Idx) // If this is the insertion idx, put the low elt of V2 here. |
| 2738 | MaskVec.push_back(DAG.getConstant(NumElems, EVT)); |
| 2739 | else |
| 2740 | MaskVec.push_back(DAG.getConstant(i, EVT)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2741 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2742 | &MaskVec[0], MaskVec.size()); |
| 2743 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2744 | } |
| 2745 | |
| 2746 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 2747 | /// |
| 2748 | static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros, |
| 2749 | unsigned NumNonZero, unsigned NumZero, |
| 2750 | SelectionDAG &DAG, TargetLowering &TLI) { |
| 2751 | if (NumNonZero > 8) |
| 2752 | return SDOperand(); |
| 2753 | |
| 2754 | SDOperand V(0, 0); |
| 2755 | bool First = true; |
| 2756 | for (unsigned i = 0; i < 16; ++i) { |
| 2757 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 2758 | if (ThisIsNonZero && First) { |
| 2759 | if (NumZero) |
| 2760 | V = getZeroVector(MVT::v8i16, DAG); |
| 2761 | else |
| 2762 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2763 | First = false; |
| 2764 | } |
| 2765 | |
| 2766 | if ((i & 1) != 0) { |
| 2767 | SDOperand ThisElt(0, 0), LastElt(0, 0); |
| 2768 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 2769 | if (LastIsNonZero) { |
| 2770 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1)); |
| 2771 | } |
| 2772 | if (ThisIsNonZero) { |
| 2773 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i)); |
| 2774 | ThisElt = DAG.getNode(ISD::SHL, MVT::i16, |
| 2775 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 2776 | if (LastIsNonZero) |
| 2777 | ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt); |
| 2778 | } else |
| 2779 | ThisElt = LastElt; |
| 2780 | |
| 2781 | if (ThisElt.Val) |
| 2782 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2783 | DAG.getIntPtrConstant(i/2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V); |
| 2788 | } |
| 2789 | |
| 2790 | /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. |
| 2791 | /// |
| 2792 | static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros, |
| 2793 | unsigned NumNonZero, unsigned NumZero, |
| 2794 | SelectionDAG &DAG, TargetLowering &TLI) { |
| 2795 | if (NumNonZero > 4) |
| 2796 | return SDOperand(); |
| 2797 | |
| 2798 | SDOperand V(0, 0); |
| 2799 | bool First = true; |
| 2800 | for (unsigned i = 0; i < 8; ++i) { |
| 2801 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 2802 | if (isNonZero) { |
| 2803 | if (First) { |
| 2804 | if (NumZero) |
| 2805 | V = getZeroVector(MVT::v8i16, DAG); |
| 2806 | else |
| 2807 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2808 | First = false; |
| 2809 | } |
| 2810 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i), |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2811 | DAG.getIntPtrConstant(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | return V; |
| 2816 | } |
| 2817 | |
| 2818 | SDOperand |
| 2819 | X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2820 | // All zero's are handled with pxor, all one's are handled with pcmpeqd. |
| 2821 | if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) { |
| 2822 | // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to |
| 2823 | // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are |
| 2824 | // eliminated on x86-32 hosts. |
| 2825 | if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32) |
| 2826 | return Op; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2827 | |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2828 | if (ISD::isBuildVectorAllOnes(Op.Val)) |
| 2829 | return getOnesVector(Op.getValueType(), DAG); |
| 2830 | return getZeroVector(Op.getValueType(), DAG); |
| 2831 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2832 | |
| 2833 | MVT::ValueType VT = Op.getValueType(); |
| 2834 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
| 2835 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
| 2836 | |
| 2837 | unsigned NumElems = Op.getNumOperands(); |
| 2838 | unsigned NumZero = 0; |
| 2839 | unsigned NumNonZero = 0; |
| 2840 | unsigned NonZeros = 0; |
Evan Cheng | c107349 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 2841 | bool HasNonImms = false; |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 2842 | SmallSet<SDOperand, 8> Values; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2843 | for (unsigned i = 0; i < NumElems; ++i) { |
| 2844 | SDOperand Elt = Op.getOperand(i); |
Evan Cheng | c107349 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 2845 | if (Elt.getOpcode() == ISD::UNDEF) |
| 2846 | continue; |
| 2847 | Values.insert(Elt); |
| 2848 | if (Elt.getOpcode() != ISD::Constant && |
| 2849 | Elt.getOpcode() != ISD::ConstantFP) |
| 2850 | HasNonImms = true; |
| 2851 | if (isZeroNode(Elt)) |
| 2852 | NumZero++; |
| 2853 | else { |
| 2854 | NonZeros |= (1 << i); |
| 2855 | NumNonZero++; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | if (NumNonZero == 0) { |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2860 | // All undef vector. Return an UNDEF. All zero vectors were handled above. |
| 2861 | return DAG.getNode(ISD::UNDEF, VT); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 2865 | if (Values.size() == 1) |
| 2866 | return SDOperand(); |
| 2867 | |
| 2868 | // Special case for single non-zero element. |
Evan Cheng | c107349 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 2869 | if (NumNonZero == 1 && NumElems <= 4) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2870 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
| 2871 | SDOperand Item = Op.getOperand(Idx); |
| 2872 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item); |
| 2873 | if (Idx == 0) |
| 2874 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| 2875 | return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx, |
| 2876 | NumZero > 0, DAG); |
Evan Cheng | c107349 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 2877 | else if (!HasNonImms) // Otherwise, it's better to do a constpool load. |
| 2878 | return SDOperand(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2879 | |
| 2880 | if (EVTBits == 32) { |
| 2881 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
| 2882 | Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0, |
| 2883 | DAG); |
| 2884 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2885 | MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT); |
| 2886 | SmallVector<SDOperand, 8> MaskVec; |
| 2887 | for (unsigned i = 0; i < NumElems; i++) |
| 2888 | MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT)); |
| 2889 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2890 | &MaskVec[0], MaskVec.size()); |
| 2891 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item, |
| 2892 | DAG.getNode(ISD::UNDEF, VT), Mask); |
| 2893 | } |
| 2894 | } |
| 2895 | |
Dan Gohman | 2146324 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 2896 | // A vector full of immediates; various special cases are already |
| 2897 | // handled, so this is best done with a single constant-pool load. |
Evan Cheng | c107349 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 2898 | if (!HasNonImms) |
Dan Gohman | 2146324 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 2899 | return SDOperand(); |
| 2900 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2901 | // Let legalizer expand 2-wide build_vectors. |
| 2902 | if (EVTBits == 64) |
| 2903 | return SDOperand(); |
| 2904 | |
| 2905 | // If element VT is < 32 bits, convert it to inserts into a zero vector. |
| 2906 | if (EVTBits == 8 && NumElems == 16) { |
| 2907 | SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 2908 | *this); |
| 2909 | if (V.Val) return V; |
| 2910 | } |
| 2911 | |
| 2912 | if (EVTBits == 16 && NumElems == 8) { |
| 2913 | SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 2914 | *this); |
| 2915 | if (V.Val) return V; |
| 2916 | } |
| 2917 | |
| 2918 | // If element VT is == 32 bits, turn it into a number of shuffles. |
| 2919 | SmallVector<SDOperand, 8> V; |
| 2920 | V.resize(NumElems); |
| 2921 | if (NumElems == 4 && NumZero > 0) { |
| 2922 | for (unsigned i = 0; i < 4; ++i) { |
| 2923 | bool isZero = !(NonZeros & (1 << i)); |
| 2924 | if (isZero) |
| 2925 | V[i] = getZeroVector(VT, DAG); |
| 2926 | else |
| 2927 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2928 | } |
| 2929 | |
| 2930 | for (unsigned i = 0; i < 2; ++i) { |
| 2931 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 2932 | default: break; |
| 2933 | case 0: |
| 2934 | V[i] = V[i*2]; // Must be a zero vector. |
| 2935 | break; |
| 2936 | case 1: |
| 2937 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2], |
| 2938 | getMOVLMask(NumElems, DAG)); |
| 2939 | break; |
| 2940 | case 2: |
| 2941 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2942 | getMOVLMask(NumElems, DAG)); |
| 2943 | break; |
| 2944 | case 3: |
| 2945 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2946 | getUnpacklMask(NumElems, DAG)); |
| 2947 | break; |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd) |
| 2952 | // clears the upper bits. |
| 2953 | // FIXME: we can do the same for v4f32 case when we know both parts of |
| 2954 | // the lower half come from scalar_to_vector (loadf32). We should do |
| 2955 | // that in post legalizer dag combiner with target specific hooks. |
| 2956 | if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0) |
| 2957 | return V[0]; |
| 2958 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2959 | MVT::ValueType EVT = MVT::getVectorElementType(MaskVT); |
| 2960 | SmallVector<SDOperand, 8> MaskVec; |
| 2961 | bool Reverse = (NonZeros & 0x3) == 2; |
| 2962 | for (unsigned i = 0; i < 2; ++i) |
| 2963 | if (Reverse) |
| 2964 | MaskVec.push_back(DAG.getConstant(1-i, EVT)); |
| 2965 | else |
| 2966 | MaskVec.push_back(DAG.getConstant(i, EVT)); |
| 2967 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 2968 | for (unsigned i = 0; i < 2; ++i) |
| 2969 | if (Reverse) |
| 2970 | MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT)); |
| 2971 | else |
| 2972 | MaskVec.push_back(DAG.getConstant(i+NumElems, EVT)); |
| 2973 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2974 | &MaskVec[0], MaskVec.size()); |
| 2975 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask); |
| 2976 | } |
| 2977 | |
| 2978 | if (Values.size() > 2) { |
| 2979 | // Expand into a number of unpckl*. |
| 2980 | // e.g. for v4f32 |
| 2981 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 2982 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 2983 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
| 2984 | SDOperand UnpckMask = getUnpacklMask(NumElems, DAG); |
| 2985 | for (unsigned i = 0; i < NumElems; ++i) |
| 2986 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2987 | NumElems >>= 1; |
| 2988 | while (NumElems != 0) { |
| 2989 | for (unsigned i = 0; i < NumElems; ++i) |
| 2990 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems], |
| 2991 | UnpckMask); |
| 2992 | NumElems >>= 1; |
| 2993 | } |
| 2994 | return V[0]; |
| 2995 | } |
| 2996 | |
| 2997 | return SDOperand(); |
| 2998 | } |
| 2999 | |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3000 | static |
| 3001 | SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2, |
| 3002 | SDOperand PermMask, SelectionDAG &DAG, |
| 3003 | TargetLowering &TLI) { |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3004 | SDOperand NewV; |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3005 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8); |
| 3006 | MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3007 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 3008 | SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(), |
| 3009 | PermMask.Val->op_end()); |
| 3010 | |
| 3011 | // First record which half of which vector the low elements come from. |
| 3012 | SmallVector<unsigned, 4> LowQuad(4); |
| 3013 | for (unsigned i = 0; i < 4; ++i) { |
| 3014 | SDOperand Elt = MaskElts[i]; |
| 3015 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3016 | continue; |
| 3017 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3018 | int QuadIdx = EltIdx / 4; |
| 3019 | ++LowQuad[QuadIdx]; |
| 3020 | } |
| 3021 | int BestLowQuad = -1; |
| 3022 | unsigned MaxQuad = 1; |
| 3023 | for (unsigned i = 0; i < 4; ++i) { |
| 3024 | if (LowQuad[i] > MaxQuad) { |
| 3025 | BestLowQuad = i; |
| 3026 | MaxQuad = LowQuad[i]; |
| 3027 | } |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3030 | // Record which half of which vector the high elements come from. |
| 3031 | SmallVector<unsigned, 4> HighQuad(4); |
| 3032 | for (unsigned i = 4; i < 8; ++i) { |
| 3033 | SDOperand Elt = MaskElts[i]; |
| 3034 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3035 | continue; |
| 3036 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3037 | int QuadIdx = EltIdx / 4; |
| 3038 | ++HighQuad[QuadIdx]; |
| 3039 | } |
| 3040 | int BestHighQuad = -1; |
| 3041 | MaxQuad = 1; |
| 3042 | for (unsigned i = 0; i < 4; ++i) { |
| 3043 | if (HighQuad[i] > MaxQuad) { |
| 3044 | BestHighQuad = i; |
| 3045 | MaxQuad = HighQuad[i]; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it. |
| 3050 | if (BestLowQuad != -1 || BestHighQuad != -1) { |
| 3051 | // First sort the 4 chunks in order using shufpd. |
| 3052 | SmallVector<SDOperand, 8> MaskVec; |
| 3053 | if (BestLowQuad != -1) |
| 3054 | MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32)); |
| 3055 | else |
| 3056 | MaskVec.push_back(DAG.getConstant(0, MVT::i32)); |
| 3057 | if (BestHighQuad != -1) |
| 3058 | MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32)); |
| 3059 | else |
| 3060 | MaskVec.push_back(DAG.getConstant(1, MVT::i32)); |
| 3061 | SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2); |
| 3062 | NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64, |
| 3063 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1), |
| 3064 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask); |
| 3065 | NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV); |
| 3066 | |
| 3067 | // Now sort high and low parts separately. |
| 3068 | BitVector InOrder(8); |
| 3069 | if (BestLowQuad != -1) { |
| 3070 | // Sort lower half in order using PSHUFLW. |
| 3071 | MaskVec.clear(); |
| 3072 | bool AnyOutOrder = false; |
| 3073 | for (unsigned i = 0; i != 4; ++i) { |
| 3074 | SDOperand Elt = MaskElts[i]; |
| 3075 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3076 | MaskVec.push_back(Elt); |
| 3077 | InOrder.set(i); |
| 3078 | } else { |
| 3079 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3080 | if (EltIdx != i) |
| 3081 | AnyOutOrder = true; |
| 3082 | MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT)); |
| 3083 | // If this element is in the right place after this shuffle, then |
| 3084 | // remember it. |
| 3085 | if ((int)(EltIdx / 4) == BestLowQuad) |
| 3086 | InOrder.set(i); |
| 3087 | } |
| 3088 | } |
| 3089 | if (AnyOutOrder) { |
| 3090 | for (unsigned i = 4; i != 8; ++i) |
| 3091 | MaskVec.push_back(DAG.getConstant(i, MaskEVT)); |
| 3092 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8); |
| 3093 | NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask); |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | if (BestHighQuad != -1) { |
| 3098 | // Sort high half in order using PSHUFHW if possible. |
| 3099 | MaskVec.clear(); |
| 3100 | for (unsigned i = 0; i != 4; ++i) |
| 3101 | MaskVec.push_back(DAG.getConstant(i, MaskEVT)); |
| 3102 | bool AnyOutOrder = false; |
| 3103 | for (unsigned i = 4; i != 8; ++i) { |
| 3104 | SDOperand Elt = MaskElts[i]; |
| 3105 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3106 | MaskVec.push_back(Elt); |
| 3107 | InOrder.set(i); |
| 3108 | } else { |
| 3109 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3110 | if (EltIdx != i) |
| 3111 | AnyOutOrder = true; |
| 3112 | MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT)); |
| 3113 | // If this element is in the right place after this shuffle, then |
| 3114 | // remember it. |
| 3115 | if ((int)(EltIdx / 4) == BestHighQuad) |
| 3116 | InOrder.set(i); |
| 3117 | } |
| 3118 | } |
| 3119 | if (AnyOutOrder) { |
| 3120 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8); |
| 3121 | NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask); |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | // The other elements are put in the right place using pextrw and pinsrw. |
| 3126 | for (unsigned i = 0; i != 8; ++i) { |
| 3127 | if (InOrder[i]) |
| 3128 | continue; |
| 3129 | SDOperand Elt = MaskElts[i]; |
| 3130 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3131 | if (EltIdx == i) |
| 3132 | continue; |
| 3133 | SDOperand ExtOp = (EltIdx < 8) |
| 3134 | ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1, |
| 3135 | DAG.getConstant(EltIdx, PtrVT)) |
| 3136 | : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2, |
| 3137 | DAG.getConstant(EltIdx - 8, PtrVT)); |
| 3138 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp, |
| 3139 | DAG.getConstant(i, PtrVT)); |
| 3140 | } |
| 3141 | return NewV; |
| 3142 | } |
| 3143 | |
| 3144 | // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use |
| 3145 | ///as few as possible. |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3146 | // First, let's find out how many elements are already in the right order. |
| 3147 | unsigned V1InOrder = 0; |
| 3148 | unsigned V1FromV1 = 0; |
| 3149 | unsigned V2InOrder = 0; |
| 3150 | unsigned V2FromV2 = 0; |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3151 | SmallVector<SDOperand, 8> V1Elts; |
| 3152 | SmallVector<SDOperand, 8> V2Elts; |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3153 | for (unsigned i = 0; i < 8; ++i) { |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3154 | SDOperand Elt = MaskElts[i]; |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3155 | if (Elt.getOpcode() == ISD::UNDEF) { |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3156 | V1Elts.push_back(Elt); |
| 3157 | V2Elts.push_back(Elt); |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3158 | ++V1InOrder; |
| 3159 | ++V2InOrder; |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3160 | continue; |
| 3161 | } |
| 3162 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3163 | if (EltIdx == i) { |
| 3164 | V1Elts.push_back(Elt); |
| 3165 | V2Elts.push_back(DAG.getConstant(i+8, MaskEVT)); |
| 3166 | ++V1InOrder; |
| 3167 | } else if (EltIdx == i+8) { |
| 3168 | V1Elts.push_back(Elt); |
| 3169 | V2Elts.push_back(DAG.getConstant(i, MaskEVT)); |
| 3170 | ++V2InOrder; |
| 3171 | } else if (EltIdx < 8) { |
| 3172 | V1Elts.push_back(Elt); |
| 3173 | ++V1FromV1; |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3174 | } else { |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3175 | V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT)); |
| 3176 | ++V2FromV2; |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | if (V2InOrder > V1InOrder) { |
| 3181 | PermMask = CommuteVectorShuffleMask(PermMask, DAG); |
| 3182 | std::swap(V1, V2); |
| 3183 | std::swap(V1Elts, V2Elts); |
| 3184 | std::swap(V1FromV1, V2FromV2); |
| 3185 | } |
| 3186 | |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3187 | if ((V1FromV1 + V1InOrder) != 8) { |
| 3188 | // Some elements are from V2. |
| 3189 | if (V1FromV1) { |
| 3190 | // If there are elements that are from V1 but out of place, |
| 3191 | // then first sort them in place |
| 3192 | SmallVector<SDOperand, 8> MaskVec; |
| 3193 | for (unsigned i = 0; i < 8; ++i) { |
| 3194 | SDOperand Elt = V1Elts[i]; |
| 3195 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3196 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3197 | continue; |
| 3198 | } |
| 3199 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3200 | if (EltIdx >= 8) |
| 3201 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3202 | else |
| 3203 | MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT)); |
| 3204 | } |
| 3205 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8); |
| 3206 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask); |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3207 | } |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3208 | |
| 3209 | NewV = V1; |
| 3210 | for (unsigned i = 0; i < 8; ++i) { |
| 3211 | SDOperand Elt = V1Elts[i]; |
| 3212 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3213 | continue; |
| 3214 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3215 | if (EltIdx < 8) |
| 3216 | continue; |
| 3217 | SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2, |
| 3218 | DAG.getConstant(EltIdx - 8, PtrVT)); |
| 3219 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp, |
| 3220 | DAG.getConstant(i, PtrVT)); |
| 3221 | } |
| 3222 | return NewV; |
| 3223 | } else { |
| 3224 | // All elements are from V1. |
| 3225 | NewV = V1; |
| 3226 | for (unsigned i = 0; i < 8; ++i) { |
| 3227 | SDOperand Elt = V1Elts[i]; |
| 3228 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3229 | continue; |
| 3230 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3231 | SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1, |
| 3232 | DAG.getConstant(EltIdx, PtrVT)); |
| 3233 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp, |
| 3234 | DAG.getConstant(i, PtrVT)); |
| 3235 | } |
| 3236 | return NewV; |
| 3237 | } |
| 3238 | } |
| 3239 | |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3240 | /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide |
| 3241 | /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be |
| 3242 | /// done when every pair / quad of shuffle mask elements point to elements in |
| 3243 | /// the right sequence. e.g. |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3244 | /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15> |
| 3245 | static |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3246 | SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2, |
| 3247 | MVT::ValueType VT, |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3248 | SDOperand PermMask, SelectionDAG &DAG, |
| 3249 | TargetLowering &TLI) { |
| 3250 | unsigned NumElems = PermMask.getNumOperands(); |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3251 | unsigned NewWidth = (NumElems == 4) ? 2 : 4; |
| 3252 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth); |
| 3253 | MVT::ValueType NewVT = MaskVT; |
| 3254 | switch (VT) { |
| 3255 | case MVT::v4f32: NewVT = MVT::v2f64; break; |
| 3256 | case MVT::v4i32: NewVT = MVT::v2i64; break; |
| 3257 | case MVT::v8i16: NewVT = MVT::v4i32; break; |
| 3258 | case MVT::v16i8: NewVT = MVT::v4i32; break; |
| 3259 | default: assert(false && "Unexpected!"); |
| 3260 | } |
| 3261 | |
| 3262 | if (NewWidth == 2) |
| 3263 | if (MVT::isInteger(VT)) |
| 3264 | NewVT = MVT::v2i64; |
| 3265 | else |
| 3266 | NewVT = MVT::v2f64; |
| 3267 | unsigned Scale = NumElems / NewWidth; |
| 3268 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3269 | for (unsigned i = 0; i < NumElems; i += Scale) { |
| 3270 | unsigned StartIdx = ~0U; |
| 3271 | for (unsigned j = 0; j < Scale; ++j) { |
| 3272 | SDOperand Elt = PermMask.getOperand(i+j); |
| 3273 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3274 | continue; |
| 3275 | unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue(); |
| 3276 | if (StartIdx == ~0U) |
| 3277 | StartIdx = EltIdx - (EltIdx % Scale); |
| 3278 | if (EltIdx != StartIdx + j) |
| 3279 | return SDOperand(); |
| 3280 | } |
| 3281 | if (StartIdx == ~0U) |
| 3282 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 3283 | else |
| 3284 | MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32)); |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3287 | V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1); |
| 3288 | V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2); |
| 3289 | return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2, |
| 3290 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3291 | &MaskVec[0], MaskVec.size())); |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3294 | SDOperand |
| 3295 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { |
| 3296 | SDOperand V1 = Op.getOperand(0); |
| 3297 | SDOperand V2 = Op.getOperand(1); |
| 3298 | SDOperand PermMask = Op.getOperand(2); |
| 3299 | MVT::ValueType VT = Op.getValueType(); |
| 3300 | unsigned NumElems = PermMask.getNumOperands(); |
| 3301 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 3302 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
| 3303 | bool V1IsSplat = false; |
| 3304 | bool V2IsSplat = false; |
| 3305 | |
| 3306 | if (isUndefShuffle(Op.Val)) |
| 3307 | return DAG.getNode(ISD::UNDEF, VT); |
| 3308 | |
| 3309 | if (isZeroShuffle(Op.Val)) |
| 3310 | return getZeroVector(VT, DAG); |
| 3311 | |
| 3312 | if (isIdentityMask(PermMask.Val)) |
| 3313 | return V1; |
| 3314 | else if (isIdentityMask(PermMask.Val, true)) |
| 3315 | return V2; |
| 3316 | |
| 3317 | if (isSplatMask(PermMask.Val)) { |
| 3318 | if (NumElems <= 4) return Op; |
| 3319 | // Promote it to a v4i32 splat. |
| 3320 | return PromoteSplat(Op, DAG); |
| 3321 | } |
| 3322 | |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3323 | // If the shuffle can be profitably rewritten as a narrower shuffle, then |
| 3324 | // do it! |
| 3325 | if (VT == MVT::v8i16 || VT == MVT::v16i8) { |
| 3326 | SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this); |
| 3327 | if (NewOp.Val) |
| 3328 | return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG)); |
| 3329 | } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) { |
| 3330 | // FIXME: Figure out a cleaner way to do this. |
| 3331 | // Try to make use of movq to zero out the top part. |
| 3332 | if (ISD::isBuildVectorAllZeros(V2.Val)) { |
| 3333 | SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this); |
| 3334 | if (NewOp.Val) { |
| 3335 | SDOperand NewV1 = NewOp.getOperand(0); |
| 3336 | SDOperand NewV2 = NewOp.getOperand(1); |
| 3337 | SDOperand NewMask = NewOp.getOperand(2); |
| 3338 | if (isCommutedMOVL(NewMask.Val, true, false)) { |
| 3339 | NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG); |
| 3340 | NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(), |
| 3341 | NewV1, NewV2, getMOVLMask(2, DAG)); |
| 3342 | return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG)); |
| 3343 | } |
| 3344 | } |
| 3345 | } else if (ISD::isBuildVectorAllZeros(V1.Val)) { |
| 3346 | SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this); |
| 3347 | if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val)) |
| 3348 | return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG)); |
| 3349 | } |
| 3350 | } |
| 3351 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3352 | if (X86::isMOVLMask(PermMask.Val)) |
| 3353 | return (V1IsUndef) ? V2 : Op; |
| 3354 | |
| 3355 | if (X86::isMOVSHDUPMask(PermMask.Val) || |
| 3356 | X86::isMOVSLDUPMask(PermMask.Val) || |
| 3357 | X86::isMOVHLPSMask(PermMask.Val) || |
| 3358 | X86::isMOVHPMask(PermMask.Val) || |
| 3359 | X86::isMOVLPMask(PermMask.Val)) |
| 3360 | return Op; |
| 3361 | |
| 3362 | if (ShouldXformToMOVHLPS(PermMask.Val) || |
| 3363 | ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val)) |
| 3364 | return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3365 | |
| 3366 | bool Commuted = false; |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3367 | // FIXME: This should also accept a bitcast of a splat? Be careful, not |
| 3368 | // 1,1,1,1 -> v8i16 though. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3369 | V1IsSplat = isSplatVector(V1.Val); |
| 3370 | V2IsSplat = isSplatVector(V2.Val); |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Canonicalize the splat or undef, if present, to be on the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3373 | if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { |
| 3374 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3375 | std::swap(V1IsSplat, V2IsSplat); |
| 3376 | std::swap(V1IsUndef, V2IsUndef); |
| 3377 | Commuted = true; |
| 3378 | } |
| 3379 | |
Evan Cheng | 15e8f5a | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3380 | // FIXME: Figure out a cleaner way to do this. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3381 | if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) { |
| 3382 | if (V2IsUndef) return V1; |
| 3383 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3384 | if (V2IsSplat) { |
| 3385 | // V2 is a splat, so the mask may be malformed. That is, it may point |
| 3386 | // to any V2 element. The instruction selectior won't like this. Get |
| 3387 | // a corrected mask and commute to form a proper MOVS{S|D}. |
| 3388 | SDOperand NewMask = getMOVLMask(NumElems, DAG); |
| 3389 | if (NewMask.Val != PermMask.Val) |
| 3390 | Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 3391 | } |
| 3392 | return Op; |
| 3393 | } |
| 3394 | |
| 3395 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
| 3396 | X86::isUNPCKH_v_undef_Mask(PermMask.Val) || |
| 3397 | X86::isUNPCKLMask(PermMask.Val) || |
| 3398 | X86::isUNPCKHMask(PermMask.Val)) |
| 3399 | return Op; |
| 3400 | |
| 3401 | if (V2IsSplat) { |
| 3402 | // Normalize mask so all entries that point to V2 points to its first |
| 3403 | // element then try to match unpck{h|l} again. If match, return a |
| 3404 | // new vector_shuffle with the corrected mask. |
| 3405 | SDOperand NewMask = NormalizeMask(PermMask, DAG); |
| 3406 | if (NewMask.Val != PermMask.Val) { |
| 3407 | if (X86::isUNPCKLMask(PermMask.Val, true)) { |
| 3408 | SDOperand NewMask = getUnpacklMask(NumElems, DAG); |
| 3409 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 3410 | } else if (X86::isUNPCKHMask(PermMask.Val, true)) { |
| 3411 | SDOperand NewMask = getUnpackhMask(NumElems, DAG); |
| 3412 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | // Normalize the node to match x86 shuffle ops if needed |
| 3418 | if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val)) |
| 3419 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3420 | |
| 3421 | if (Commuted) { |
| 3422 | // Commute is back and try unpck* again. |
| 3423 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3424 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
| 3425 | X86::isUNPCKH_v_undef_Mask(PermMask.Val) || |
| 3426 | X86::isUNPCKLMask(PermMask.Val) || |
| 3427 | X86::isUNPCKHMask(PermMask.Val)) |
| 3428 | return Op; |
| 3429 | } |
| 3430 | |
| 3431 | // If VT is integer, try PSHUF* first, then SHUFP*. |
| 3432 | if (MVT::isInteger(VT)) { |
Dan Gohman | 7dc1901 | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 3433 | // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically |
| 3434 | // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented. |
| 3435 | if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) && |
| 3436 | X86::isPSHUFDMask(PermMask.Val)) || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3437 | X86::isPSHUFHWMask(PermMask.Val) || |
| 3438 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 3439 | if (V2.getOpcode() != ISD::UNDEF) |
| 3440 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 3441 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 3442 | return Op; |
| 3443 | } |
| 3444 | |
| 3445 | if (X86::isSHUFPMask(PermMask.Val) && |
| 3446 | MVT::getSizeInBits(VT) != 64) // Don't do this for MMX. |
| 3447 | return Op; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3448 | } else { |
| 3449 | // Floating point cases in the other order. |
| 3450 | if (X86::isSHUFPMask(PermMask.Val)) |
| 3451 | return Op; |
| 3452 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 3453 | X86::isPSHUFHWMask(PermMask.Val) || |
| 3454 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 3455 | if (V2.getOpcode() != ISD::UNDEF) |
| 3456 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 3457 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 3458 | return Op; |
| 3459 | } |
| 3460 | } |
| 3461 | |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3462 | // Handle v8i16 specifically since SSE can do byte extraction and insertion. |
| 3463 | if (VT == MVT::v8i16) { |
| 3464 | SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this); |
| 3465 | if (NewOp.Val) |
| 3466 | return NewOp; |
| 3467 | } |
| 3468 | |
| 3469 | // Handle all 4 wide cases with a number of shuffles. |
| 3470 | if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) { |
Evan Cheng | fca2924 | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3471 | // Don't do this for MMX. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3472 | MVT::ValueType MaskVT = PermMask.getValueType(); |
| 3473 | MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT); |
| 3474 | SmallVector<std::pair<int, int>, 8> Locs; |
| 3475 | Locs.reserve(NumElems); |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3476 | SmallVector<SDOperand, 8> Mask1(NumElems, |
| 3477 | DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3478 | SmallVector<SDOperand, 8> Mask2(NumElems, |
| 3479 | DAG.getNode(ISD::UNDEF, MaskEVT)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3480 | unsigned NumHi = 0; |
| 3481 | unsigned NumLo = 0; |
| 3482 | // If no more than two elements come from either vector. This can be |
| 3483 | // implemented with two shuffles. First shuffle gather the elements. |
| 3484 | // The second shuffle, which takes the first shuffle as both of its |
| 3485 | // vector operands, put the elements into the right order. |
| 3486 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3487 | SDOperand Elt = PermMask.getOperand(i); |
| 3488 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3489 | Locs[i] = std::make_pair(-1, -1); |
| 3490 | } else { |
| 3491 | unsigned Val = cast<ConstantSDNode>(Elt)->getValue(); |
| 3492 | if (Val < NumElems) { |
| 3493 | Locs[i] = std::make_pair(0, NumLo); |
| 3494 | Mask1[NumLo] = Elt; |
| 3495 | NumLo++; |
| 3496 | } else { |
| 3497 | Locs[i] = std::make_pair(1, NumHi); |
| 3498 | if (2+NumHi < NumElems) |
| 3499 | Mask1[2+NumHi] = Elt; |
| 3500 | NumHi++; |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | if (NumLo <= 2 && NumHi <= 2) { |
| 3505 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 3506 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3507 | &Mask1[0], Mask1.size())); |
| 3508 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3509 | if (Locs[i].first == -1) |
| 3510 | continue; |
| 3511 | else { |
| 3512 | unsigned Idx = (i < NumElems/2) ? 0 : NumElems; |
| 3513 | Idx += Locs[i].first * (NumElems/2) + Locs[i].second; |
| 3514 | Mask2[i] = DAG.getConstant(Idx, MaskEVT); |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, |
| 3519 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3520 | &Mask2[0], Mask2.size())); |
| 3521 | } |
| 3522 | |
| 3523 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 3524 | Locs.clear(); |
| 3525 | SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3526 | SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3527 | SmallVector<SDOperand,8> *MaskPtr = &LoMask; |
| 3528 | unsigned MaskIdx = 0; |
| 3529 | unsigned LoIdx = 0; |
| 3530 | unsigned HiIdx = NumElems/2; |
| 3531 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3532 | if (i == NumElems/2) { |
| 3533 | MaskPtr = &HiMask; |
| 3534 | MaskIdx = 1; |
| 3535 | LoIdx = 0; |
| 3536 | HiIdx = NumElems/2; |
| 3537 | } |
| 3538 | SDOperand Elt = PermMask.getOperand(i); |
| 3539 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3540 | Locs[i] = std::make_pair(-1, -1); |
| 3541 | } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) { |
| 3542 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
| 3543 | (*MaskPtr)[LoIdx] = Elt; |
| 3544 | LoIdx++; |
| 3545 | } else { |
| 3546 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
| 3547 | (*MaskPtr)[HiIdx] = Elt; |
| 3548 | HiIdx++; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | SDOperand LoShuffle = |
| 3553 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 3554 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3555 | &LoMask[0], LoMask.size())); |
| 3556 | SDOperand HiShuffle = |
| 3557 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 3558 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3559 | &HiMask[0], HiMask.size())); |
| 3560 | SmallVector<SDOperand, 8> MaskOps; |
| 3561 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3562 | if (Locs[i].first == -1) { |
| 3563 | MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3564 | } else { |
| 3565 | unsigned Idx = Locs[i].first * NumElems + Locs[i].second; |
| 3566 | MaskOps.push_back(DAG.getConstant(Idx, MaskEVT)); |
| 3567 | } |
| 3568 | } |
| 3569 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle, |
| 3570 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3571 | &MaskOps[0], MaskOps.size())); |
| 3572 | } |
| 3573 | |
| 3574 | return SDOperand(); |
| 3575 | } |
| 3576 | |
| 3577 | SDOperand |
| 3578 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
| 3579 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 3580 | return SDOperand(); |
| 3581 | |
| 3582 | MVT::ValueType VT = Op.getValueType(); |
| 3583 | // TODO: handle v16i8. |
| 3584 | if (MVT::getSizeInBits(VT) == 16) { |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3585 | SDOperand Vec = Op.getOperand(0); |
| 3586 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3587 | if (Idx == 0) |
| 3588 | return DAG.getNode(ISD::TRUNCATE, MVT::i16, |
| 3589 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, |
| 3590 | DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec), |
| 3591 | Op.getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3592 | // Transform it so it match pextrw which produces a 32-bit result. |
| 3593 | MVT::ValueType EVT = (MVT::ValueType)(VT+1); |
| 3594 | SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT, |
| 3595 | Op.getOperand(0), Op.getOperand(1)); |
| 3596 | SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract, |
| 3597 | DAG.getValueType(VT)); |
| 3598 | return DAG.getNode(ISD::TRUNCATE, VT, Assert); |
| 3599 | } else if (MVT::getSizeInBits(VT) == 32) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3600 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3601 | if (Idx == 0) |
| 3602 | return Op; |
| 3603 | // SHUFPS the element to the lowest double word, then movss. |
| 3604 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 3605 | SmallVector<SDOperand, 8> IdxVec; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 3606 | IdxVec. |
| 3607 | push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT))); |
| 3608 | IdxVec. |
| 3609 | push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
| 3610 | IdxVec. |
| 3611 | push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
| 3612 | IdxVec. |
| 3613 | push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3614 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3615 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3616 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3617 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 3618 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
| 3619 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3620 | DAG.getIntPtrConstant(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3621 | } else if (MVT::getSizeInBits(VT) == 64) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3622 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3623 | if (Idx == 0) |
| 3624 | return Op; |
| 3625 | |
| 3626 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 3627 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 3628 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
| 3629 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 3630 | SmallVector<SDOperand, 8> IdxVec; |
| 3631 | IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT))); |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 3632 | IdxVec. |
| 3633 | push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3634 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3635 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 75184a9 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3636 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3637 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 3638 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
| 3639 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3640 | DAG.getIntPtrConstant(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | return SDOperand(); |
| 3644 | } |
| 3645 | |
| 3646 | SDOperand |
| 3647 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3648 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | e12a7eb | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 3649 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
| 3650 | if (EVT == MVT::i8) |
| 3651 | return SDOperand(); |
| 3652 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3653 | SDOperand N0 = Op.getOperand(0); |
| 3654 | SDOperand N1 = Op.getOperand(1); |
| 3655 | SDOperand N2 = Op.getOperand(2); |
Evan Cheng | e12a7eb | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 3656 | |
| 3657 | if (MVT::getSizeInBits(EVT) == 16) { |
| 3658 | // Transform it so it match pinsrw which expects a 16-bit value in a GR32 |
| 3659 | // as its second argument. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3660 | if (N1.getValueType() != MVT::i32) |
| 3661 | N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1); |
| 3662 | if (N2.getValueType() != MVT::i32) |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3663 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3664 | return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3665 | } |
Nate Begeman | 9e1a41f | 2008-01-05 20:51:30 +0000 | [diff] [blame] | 3666 | return SDOperand(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | SDOperand |
| 3670 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 3671 | SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0)); |
| 3672 | return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt); |
| 3673 | } |
| 3674 | |
| 3675 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 3676 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 3677 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 3678 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 3679 | // be used to form addressing mode. These wrapped nodes will be selected |
| 3680 | // into MOV32ri. |
| 3681 | SDOperand |
| 3682 | X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { |
| 3683 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| 3684 | SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(), |
| 3685 | getPointerTy(), |
| 3686 | CP->getAlignment()); |
| 3687 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3688 | // With PIC, the address is actually $g + Offset. |
| 3689 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3690 | !Subtarget->isPICStyleRIPRel()) { |
| 3691 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3692 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3693 | Result); |
| 3694 | } |
| 3695 | |
| 3696 | return Result; |
| 3697 | } |
| 3698 | |
| 3699 | SDOperand |
| 3700 | X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) { |
| 3701 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 3702 | SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy()); |
| 3703 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3704 | // With PIC, the address is actually $g + Offset. |
| 3705 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3706 | !Subtarget->isPICStyleRIPRel()) { |
| 3707 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3708 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3709 | Result); |
| 3710 | } |
| 3711 | |
| 3712 | // For Darwin & Mingw32, external and weak symbols are indirect, so we want to |
| 3713 | // load the value at address GV, not the value of GV itself. This means that |
| 3714 | // the GlobalAddress must be in the base or index register of the address, not |
| 3715 | // the GV offset field. Platform check is inside GVRequiresExtraLoad() call |
| 3716 | // The same applies for external symbols during PIC codegen |
| 3717 | if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)) |
| 3718 | Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0); |
| 3719 | |
| 3720 | return Result; |
| 3721 | } |
| 3722 | |
| 3723 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model |
| 3724 | static SDOperand |
| 3725 | LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| 3726 | const MVT::ValueType PtrVT) { |
| 3727 | SDOperand InFlag; |
| 3728 | SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX, |
| 3729 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 3730 | PtrVT), InFlag); |
| 3731 | InFlag = Chain.getValue(1); |
| 3732 | |
| 3733 | // emit leal symbol@TLSGD(,%ebx,1), %eax |
| 3734 | SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag); |
| 3735 | SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 3736 | GA->getValueType(0), |
| 3737 | GA->getOffset()); |
| 3738 | SDOperand Ops[] = { Chain, TGA, InFlag }; |
| 3739 | SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3); |
| 3740 | InFlag = Result.getValue(2); |
| 3741 | Chain = Result.getValue(1); |
| 3742 | |
| 3743 | // call ___tls_get_addr. This function receives its argument in |
| 3744 | // the register EAX. |
| 3745 | Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag); |
| 3746 | InFlag = Chain.getValue(1); |
| 3747 | |
| 3748 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 3749 | SDOperand Ops1[] = { Chain, |
| 3750 | DAG.getTargetExternalSymbol("___tls_get_addr", |
| 3751 | PtrVT), |
| 3752 | DAG.getRegister(X86::EAX, PtrVT), |
| 3753 | DAG.getRegister(X86::EBX, PtrVT), |
| 3754 | InFlag }; |
| 3755 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5); |
| 3756 | InFlag = Chain.getValue(1); |
| 3757 | |
| 3758 | return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag); |
| 3759 | } |
| 3760 | |
| 3761 | // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or |
| 3762 | // "local exec" model. |
| 3763 | static SDOperand |
| 3764 | LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| 3765 | const MVT::ValueType PtrVT) { |
| 3766 | // Get the Thread Pointer |
| 3767 | SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT); |
| 3768 | // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial |
| 3769 | // exec) |
| 3770 | SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 3771 | GA->getValueType(0), |
| 3772 | GA->getOffset()); |
| 3773 | SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA); |
| 3774 | |
| 3775 | if (GA->getGlobal()->isDeclaration()) // initial exec TLS model |
| 3776 | Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0); |
| 3777 | |
| 3778 | // The address of the thread local variable is the add of the thread |
| 3779 | // pointer with the offset of the variable. |
| 3780 | return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset); |
| 3781 | } |
| 3782 | |
| 3783 | SDOperand |
| 3784 | X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) { |
| 3785 | // TODO: implement the "local dynamic" model |
| 3786 | // TODO: implement the "initial exec"model for pic executables |
| 3787 | assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() && |
| 3788 | "TLS not implemented for non-ELF and 64-bit targets"); |
| 3789 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
| 3790 | // If the relocation model is PIC, use the "General Dynamic" TLS Model, |
| 3791 | // otherwise use the "Local Exec"TLS Model |
| 3792 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_) |
| 3793 | return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy()); |
| 3794 | else |
| 3795 | return LowerToTLSExecModel(GA, DAG, getPointerTy()); |
| 3796 | } |
| 3797 | |
| 3798 | SDOperand |
| 3799 | X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) { |
| 3800 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 3801 | SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
| 3802 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3803 | // With PIC, the address is actually $g + Offset. |
| 3804 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3805 | !Subtarget->isPICStyleRIPRel()) { |
| 3806 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3807 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3808 | Result); |
| 3809 | } |
| 3810 | |
| 3811 | return Result; |
| 3812 | } |
| 3813 | |
| 3814 | SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) { |
| 3815 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 3816 | SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); |
| 3817 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3818 | // With PIC, the address is actually $g + Offset. |
| 3819 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3820 | !Subtarget->isPICStyleRIPRel()) { |
| 3821 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3822 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3823 | Result); |
| 3824 | } |
| 3825 | |
| 3826 | return Result; |
| 3827 | } |
| 3828 | |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3829 | /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and |
| 3830 | /// take a 2 x i32 value to shift plus a shift amount. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3831 | SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3832 | assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && |
| 3833 | "Not an i64 shift!"); |
| 3834 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| 3835 | SDOperand ShOpLo = Op.getOperand(0); |
| 3836 | SDOperand ShOpHi = Op.getOperand(1); |
| 3837 | SDOperand ShAmt = Op.getOperand(2); |
| 3838 | SDOperand Tmp1 = isSRA ? |
| 3839 | DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) : |
| 3840 | DAG.getConstant(0, MVT::i32); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3842 | SDOperand Tmp2, Tmp3; |
| 3843 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3844 | Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); |
| 3845 | Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); |
| 3846 | } else { |
| 3847 | Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); |
| 3848 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); |
| 3849 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3850 | |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3851 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3852 | SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt, |
| 3853 | DAG.getConstant(32, MVT::i8)); |
| 3854 | SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32, |
| 3855 | AndNode, DAG.getConstant(0, MVT::i8)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3856 | |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3857 | SDOperand Hi, Lo; |
| 3858 | SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| 3859 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag); |
| 3860 | SmallVector<SDOperand, 4> Ops; |
| 3861 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3862 | Ops.push_back(Tmp2); |
| 3863 | Ops.push_back(Tmp3); |
| 3864 | Ops.push_back(CC); |
| 3865 | Ops.push_back(Cond); |
| 3866 | Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3867 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3868 | Ops.clear(); |
Chris Lattner | 62814a3 | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 3869 | Ops.push_back(Tmp3); |
| 3870 | Ops.push_back(Tmp1); |
| 3871 | Ops.push_back(CC); |
| 3872 | Ops.push_back(Cond); |
| 3873 | Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size()); |
| 3874 | } else { |
| 3875 | Ops.push_back(Tmp2); |
| 3876 | Ops.push_back(Tmp3); |
| 3877 | Ops.push_back(CC); |
| 3878 | Ops.push_back(Cond); |
| 3879 | Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size()); |
| 3880 | |
| 3881 | Ops.clear(); |
| 3882 | Ops.push_back(Tmp3); |
| 3883 | Ops.push_back(Tmp1); |
| 3884 | Ops.push_back(CC); |
| 3885 | Ops.push_back(Cond); |
| 3886 | Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size()); |
| 3887 | } |
| 3888 | |
| 3889 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32); |
| 3890 | Ops.clear(); |
| 3891 | Ops.push_back(Lo); |
| 3892 | Ops.push_back(Hi); |
| 3893 | return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { |
| 3897 | assert(Op.getOperand(0).getValueType() <= MVT::i64 && |
| 3898 | Op.getOperand(0).getValueType() >= MVT::i16 && |
| 3899 | "Unknown SINT_TO_FP to lower!"); |
| 3900 | |
| 3901 | SDOperand Result; |
| 3902 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3903 | unsigned Size = MVT::getSizeInBits(SrcVT)/8; |
| 3904 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3905 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 3906 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3907 | SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0), |
| 3908 | StackSlot, NULL, 0); |
| 3909 | |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3910 | // These are really Legal; caller falls through into that case. |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 3911 | if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType())) |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 3912 | return Result; |
Chris Lattner | fca7f22 | 2008-01-16 06:19:45 +0000 | [diff] [blame] | 3913 | if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 && |
Dale Johannesen | 958b08b | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3914 | Subtarget->is64Bit()) |
| 3915 | return Result; |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3916 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3917 | // Build the FILD |
| 3918 | SDVTList Tys; |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 3919 | bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType()); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3920 | if (useSSE) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3921 | Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); |
| 3922 | else |
| 3923 | Tys = DAG.getVTList(Op.getValueType(), MVT::Other); |
| 3924 | SmallVector<SDOperand, 8> Ops; |
| 3925 | Ops.push_back(Chain); |
| 3926 | Ops.push_back(StackSlot); |
| 3927 | Ops.push_back(DAG.getValueType(SrcVT)); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3928 | Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3929 | Tys, &Ops[0], Ops.size()); |
| 3930 | |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3931 | if (useSSE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3932 | Chain = Result.getValue(1); |
| 3933 | SDOperand InFlag = Result.getValue(2); |
| 3934 | |
| 3935 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 3936 | // shouldn't be necessary except that RFP cannot be live across |
| 3937 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 3938 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3939 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 3940 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3941 | Tys = DAG.getVTList(MVT::Other); |
| 3942 | SmallVector<SDOperand, 8> Ops; |
| 3943 | Ops.push_back(Chain); |
| 3944 | Ops.push_back(Result); |
| 3945 | Ops.push_back(StackSlot); |
| 3946 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 3947 | Ops.push_back(InFlag); |
| 3948 | Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); |
| 3949 | Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0); |
| 3950 | } |
| 3951 | |
| 3952 | return Result; |
| 3953 | } |
| 3954 | |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3955 | std::pair<SDOperand,SDOperand> X86TargetLowering:: |
| 3956 | FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3957 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
| 3958 | "Unknown FP_TO_SINT to lower!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3959 | |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3960 | // These are really Legal. |
Dale Johannesen | e0e0fd0 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 3961 | if (Op.getValueType() == MVT::i32 && |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 3962 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3963 | return std::make_pair(SDOperand(), SDOperand()); |
Dale Johannesen | 958b08b | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3964 | if (Subtarget->is64Bit() && |
| 3965 | Op.getValueType() == MVT::i64 && |
| 3966 | Op.getOperand(0).getValueType() != MVT::f80) |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3967 | return std::make_pair(SDOperand(), SDOperand()); |
Dale Johannesen | 2fc2078 | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 3968 | |
Evan Cheng | 05441e6 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 3969 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 3970 | // stack slot. |
| 3971 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3972 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3973 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3974 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3975 | unsigned Opc; |
| 3976 | switch (Op.getValueType()) { |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3977 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 3978 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 3979 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 3980 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | SDOperand Chain = DAG.getEntryNode(); |
| 3984 | SDOperand Value = Op.getOperand(0); |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 3985 | if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3986 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
| 3987 | Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0); |
| 3988 | SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); |
| 3989 | SDOperand Ops[] = { |
| 3990 | Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) |
| 3991 | }; |
| 3992 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); |
| 3993 | Chain = Value.getValue(1); |
| 3994 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3995 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3996 | } |
| 3997 | |
| 3998 | // Build the FP_TO_INT*_IN_MEM |
| 3999 | SDOperand Ops[] = { Chain, Value, StackSlot }; |
| 4000 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3); |
| 4001 | |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4002 | return std::make_pair(FIST, StackSlot); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4005 | SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4006 | std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG); |
| 4007 | SDOperand FIST = Vals.first, StackSlot = Vals.second; |
| 4008 | if (FIST.Val == 0) return SDOperand(); |
| 4009 | |
| 4010 | // Load the result. |
| 4011 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0); |
| 4012 | } |
| 4013 | |
| 4014 | SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) { |
| 4015 | std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG); |
| 4016 | SDOperand FIST = Vals.first, StackSlot = Vals.second; |
| 4017 | if (FIST.Val == 0) return 0; |
| 4018 | |
| 4019 | // Return an i64 load from the stack slot. |
| 4020 | SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0); |
| 4021 | |
| 4022 | // Use a MERGE_VALUES node to drop the chain result value. |
| 4023 | return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val; |
| 4024 | } |
| 4025 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4026 | SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) { |
| 4027 | MVT::ValueType VT = Op.getValueType(); |
| 4028 | MVT::ValueType EltVT = VT; |
| 4029 | if (MVT::isVector(VT)) |
| 4030 | EltVT = MVT::getVectorElementType(VT); |
| 4031 | const Type *OpNTy = MVT::getTypeForValueType(EltVT); |
| 4032 | std::vector<Constant*> CV; |
| 4033 | if (EltVT == MVT::f64) { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4034 | Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4035 | CV.push_back(C); |
| 4036 | CV.push_back(C); |
| 4037 | } else { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4038 | Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4039 | CV.push_back(C); |
| 4040 | CV.push_back(C); |
| 4041 | CV.push_back(C); |
| 4042 | CV.push_back(C); |
| 4043 | } |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4044 | Constant *C = ConstantVector::get(CV); |
| 4045 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 4046 | SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 4047 | false, 16); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4048 | return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); |
| 4049 | } |
| 4050 | |
| 4051 | SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) { |
| 4052 | MVT::ValueType VT = Op.getValueType(); |
| 4053 | MVT::ValueType EltVT = VT; |
Evan Cheng | 92b8f78 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4054 | unsigned EltNum = 1; |
| 4055 | if (MVT::isVector(VT)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4056 | EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | 92b8f78 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4057 | EltNum = MVT::getVectorNumElements(VT); |
| 4058 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4059 | const Type *OpNTy = MVT::getTypeForValueType(EltVT); |
| 4060 | std::vector<Constant*> CV; |
| 4061 | if (EltVT == MVT::f64) { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4062 | Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4063 | CV.push_back(C); |
| 4064 | CV.push_back(C); |
| 4065 | } else { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4066 | Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4067 | CV.push_back(C); |
| 4068 | CV.push_back(C); |
| 4069 | CV.push_back(C); |
| 4070 | CV.push_back(C); |
| 4071 | } |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4072 | Constant *C = ConstantVector::get(CV); |
| 4073 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 4074 | SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 4075 | false, 16); |
Evan Cheng | 92b8f78 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4076 | if (MVT::isVector(VT)) { |
Evan Cheng | 92b8f78 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4077 | return DAG.getNode(ISD::BIT_CONVERT, VT, |
| 4078 | DAG.getNode(ISD::XOR, MVT::v2i64, |
| 4079 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)), |
| 4080 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask))); |
| 4081 | } else { |
Evan Cheng | 92b8f78 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4082 | return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); |
| 4083 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) { |
| 4087 | SDOperand Op0 = Op.getOperand(0); |
| 4088 | SDOperand Op1 = Op.getOperand(1); |
| 4089 | MVT::ValueType VT = Op.getValueType(); |
| 4090 | MVT::ValueType SrcVT = Op1.getValueType(); |
| 4091 | const Type *SrcTy = MVT::getTypeForValueType(SrcVT); |
| 4092 | |
| 4093 | // If second operand is smaller, extend it first. |
| 4094 | if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) { |
| 4095 | Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1); |
| 4096 | SrcVT = VT; |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 4097 | SrcTy = MVT::getTypeForValueType(SrcVT); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4098 | } |
Dale Johannesen | fb0fa91 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 4099 | // And if it is bigger, shrink it first. |
| 4100 | if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) { |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4101 | Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1)); |
Dale Johannesen | fb0fa91 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 4102 | SrcVT = VT; |
| 4103 | SrcTy = MVT::getTypeForValueType(SrcVT); |
| 4104 | } |
| 4105 | |
| 4106 | // At this point the operands and the result should have the same |
| 4107 | // type, and that won't be f80 since that is not custom lowered. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4108 | |
| 4109 | // First get the sign bit of second operand. |
| 4110 | std::vector<Constant*> CV; |
| 4111 | if (SrcVT == MVT::f64) { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4112 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63)))); |
| 4113 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4114 | } else { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4115 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31)))); |
| 4116 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
| 4117 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
| 4118 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4119 | } |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4120 | Constant *C = ConstantVector::get(CV); |
| 4121 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 4122 | SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 4123 | false, 16); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4124 | SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1); |
| 4125 | |
| 4126 | // Shift sign bit right or left if the two operands have different types. |
| 4127 | if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) { |
| 4128 | // Op0 is MVT::f32, Op1 is MVT::f64. |
| 4129 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit); |
| 4130 | SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit, |
| 4131 | DAG.getConstant(32, MVT::i32)); |
| 4132 | SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit); |
| 4133 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4134 | DAG.getIntPtrConstant(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | // Clear first operand sign bit. |
| 4138 | CV.clear(); |
| 4139 | if (VT == MVT::f64) { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4140 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63))))); |
| 4141 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4142 | } else { |
Dale Johannesen | 1616e90 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 4143 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31))))); |
| 4144 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
| 4145 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
| 4146 | CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0)))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4147 | } |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4148 | C = ConstantVector::get(CV); |
| 4149 | CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 4150 | SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 4151 | false, 16); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4152 | SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2); |
| 4153 | |
| 4154 | // Or the value with the sign bit. |
| 4155 | return DAG.getNode(X86ISD::FOR, VT, Val, SignBit); |
| 4156 | } |
| 4157 | |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4158 | SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4159 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
Evan Cheng | 6afec3d | 2007-09-26 00:45:55 +0000 | [diff] [blame] | 4160 | SDOperand Cond; |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4161 | SDOperand Op0 = Op.getOperand(0); |
| 4162 | SDOperand Op1 = Op.getOperand(1); |
| 4163 | SDOperand CC = Op.getOperand(2); |
| 4164 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| 4165 | bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); |
| 4166 | unsigned X86CC; |
| 4167 | |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4168 | if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, |
Evan Cheng | 6afec3d | 2007-09-26 00:45:55 +0000 | [diff] [blame] | 4169 | Op0, Op1, DAG)) { |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4170 | Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1); |
| 4171 | return DAG.getNode(X86ISD::SETCC, MVT::i8, |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4172 | DAG.getConstant(X86CC, MVT::i8), Cond); |
Evan Cheng | 6afec3d | 2007-09-26 00:45:55 +0000 | [diff] [blame] | 4173 | } |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4174 | |
| 4175 | assert(isFP && "Illegal integer SetCC!"); |
| 4176 | |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4177 | Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1); |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4178 | switch (SetCCOpcode) { |
| 4179 | default: assert(false && "Illegal floating point SetCC!"); |
| 4180 | case ISD::SETOEQ: { // !PF & ZF |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4181 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4182 | DAG.getConstant(X86::COND_NP, MVT::i8), Cond); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4183 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4184 | DAG.getConstant(X86::COND_E, MVT::i8), Cond); |
| 4185 | return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); |
| 4186 | } |
| 4187 | case ISD::SETUNE: { // PF | !ZF |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4188 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4189 | DAG.getConstant(X86::COND_P, MVT::i8), Cond); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4190 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4191 | DAG.getConstant(X86::COND_NE, MVT::i8), Cond); |
| 4192 | return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); |
| 4193 | } |
| 4194 | } |
| 4195 | } |
| 4196 | |
| 4197 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4198 | SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) { |
| 4199 | bool addTest = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4200 | SDOperand Cond = Op.getOperand(0); |
| 4201 | SDOperand CC; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4202 | |
| 4203 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4204 | Cond = LowerSETCC(Cond, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4205 | |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4206 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 4207 | // setting operand in place of the X86ISD::SETCC. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4208 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 4209 | CC = Cond.getOperand(0); |
| 4210 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4211 | SDOperand Cmp = Cond.getOperand(1); |
| 4212 | unsigned Opc = Cmp.getOpcode(); |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4213 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | fca7f22 | 2008-01-16 06:19:45 +0000 | [diff] [blame] | 4214 | |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4215 | bool IllegalFPCMov = false; |
Chris Lattner | fca7f22 | 2008-01-16 06:19:45 +0000 | [diff] [blame] | 4216 | if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) && |
Chris Lattner | cf515b5 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 4217 | !isScalarFPTypeInSSEReg(VT)) // FPStack? |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4218 | IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); |
Chris Lattner | fca7f22 | 2008-01-16 06:19:45 +0000 | [diff] [blame] | 4219 | |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4220 | if ((Opc == X86ISD::CMP || |
| 4221 | Opc == X86ISD::COMI || |
| 4222 | Opc == X86ISD::UCOMI) && !IllegalFPCMov) { |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4223 | Cond = Cmp; |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4224 | addTest = false; |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | if (addTest) { |
| 4229 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4230 | Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8)); |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(), |
| 4234 | MVT::Flag); |
| 4235 | SmallVector<SDOperand, 4> Ops; |
| 4236 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 4237 | // condition is true. |
| 4238 | Ops.push_back(Op.getOperand(2)); |
| 4239 | Ops.push_back(Op.getOperand(1)); |
| 4240 | Ops.push_back(CC); |
| 4241 | Ops.push_back(Cond); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4242 | return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4245 | SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) { |
| 4246 | bool addTest = true; |
| 4247 | SDOperand Chain = Op.getOperand(0); |
| 4248 | SDOperand Cond = Op.getOperand(1); |
| 4249 | SDOperand Dest = Op.getOperand(2); |
| 4250 | SDOperand CC; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4251 | |
| 4252 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4253 | Cond = LowerSETCC(Cond, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4254 | |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4255 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 4256 | // setting operand in place of the X86ISD::SETCC. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4257 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 4258 | CC = Cond.getOperand(0); |
| 4259 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4260 | SDOperand Cmp = Cond.getOperand(1); |
| 4261 | unsigned Opc = Cmp.getOpcode(); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4262 | if (Opc == X86ISD::CMP || |
| 4263 | Opc == X86ISD::COMI || |
| 4264 | Opc == X86ISD::UCOMI) { |
Evan Cheng | 50d37ab | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 4265 | Cond = Cmp; |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4266 | addTest = false; |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | if (addTest) { |
| 4271 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4272 | Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8)); |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4273 | } |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4274 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
Evan Cheng | 950aac0 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 4275 | Chain, Op.getOperand(2), CC, Cond); |
| 4276 | } |
| 4277 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4278 | |
| 4279 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. |
| 4280 | // Calls to _alloca is needed to probe the stack when allocating more than 4k |
| 4281 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure |
| 4282 | // that the guard pages used by the OS virtual memory manager are allocated in |
| 4283 | // correct sequence. |
| 4284 | SDOperand |
| 4285 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op, |
| 4286 | SelectionDAG &DAG) { |
| 4287 | assert(Subtarget->isTargetCygMing() && |
| 4288 | "This should be used only on Cygwin/Mingw targets"); |
| 4289 | |
| 4290 | // Get the inputs. |
| 4291 | SDOperand Chain = Op.getOperand(0); |
| 4292 | SDOperand Size = Op.getOperand(1); |
| 4293 | // FIXME: Ensure alignment here |
| 4294 | |
| 4295 | SDOperand Flag; |
| 4296 | |
| 4297 | MVT::ValueType IntPtr = getPointerTy(); |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4298 | MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4299 | |
| 4300 | Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag); |
| 4301 | Flag = Chain.getValue(1); |
| 4302 | |
| 4303 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4304 | SDOperand Ops[] = { Chain, |
| 4305 | DAG.getTargetExternalSymbol("_alloca", IntPtr), |
| 4306 | DAG.getRegister(X86::EAX, IntPtr), |
| 4307 | Flag }; |
| 4308 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4); |
| 4309 | Flag = Chain.getValue(1); |
| 4310 | |
| 4311 | Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1); |
| 4312 | |
| 4313 | std::vector<MVT::ValueType> Tys; |
| 4314 | Tys.push_back(SPTy); |
| 4315 | Tys.push_back(MVT::Other); |
| 4316 | SDOperand Ops1[2] = { Chain.getValue(0), Chain }; |
| 4317 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2); |
| 4318 | } |
| 4319 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4320 | SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) { |
| 4321 | SDOperand InFlag(0, 0); |
| 4322 | SDOperand Chain = Op.getOperand(0); |
| 4323 | unsigned Align = |
| 4324 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 4325 | if (Align == 0) Align = 1; |
| 4326 | |
| 4327 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
Rafael Espindola | 5d3e762 | 2007-08-27 10:18:20 +0000 | [diff] [blame] | 4328 | // If not DWORD aligned or size is more than the threshold, call memset. |
Rafael Espindola | b2e7a6b | 2007-08-27 17:48:26 +0000 | [diff] [blame] | 4329 | // The libc version is likely to be faster for these cases. It can use the |
| 4330 | // address value and run time information about the CPU. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4331 | if ((Align & 3) != 0 || |
Rafael Espindola | 7afa9b1 | 2007-10-31 11:52:06 +0000 | [diff] [blame] | 4332 | (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4333 | MVT::ValueType IntPtr = getPointerTy(); |
| 4334 | const Type *IntPtrTy = getTargetData()->getIntPtrType(); |
| 4335 | TargetLowering::ArgListTy Args; |
| 4336 | TargetLowering::ArgListEntry Entry; |
| 4337 | Entry.Node = Op.getOperand(1); |
| 4338 | Entry.Ty = IntPtrTy; |
| 4339 | Args.push_back(Entry); |
| 4340 | // Extend the unsigned i8 argument to be an int value for the call. |
| 4341 | Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); |
| 4342 | Entry.Ty = IntPtrTy; |
| 4343 | Args.push_back(Entry); |
| 4344 | Entry.Node = Op.getOperand(3); |
| 4345 | Args.push_back(Entry); |
| 4346 | std::pair<SDOperand,SDOperand> CallResult = |
| 4347 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
| 4348 | DAG.getExternalSymbol("memset", IntPtr), Args, DAG); |
| 4349 | return CallResult.second; |
| 4350 | } |
| 4351 | |
| 4352 | MVT::ValueType AVT; |
| 4353 | SDOperand Count; |
| 4354 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 4355 | unsigned BytesLeft = 0; |
| 4356 | bool TwoRepStos = false; |
| 4357 | if (ValC) { |
| 4358 | unsigned ValReg; |
| 4359 | uint64_t Val = ValC->getValue() & 255; |
| 4360 | |
| 4361 | // If the value is a constant, then we can potentially use larger sets. |
| 4362 | switch (Align & 3) { |
| 4363 | case 2: // WORD aligned |
| 4364 | AVT = MVT::i16; |
| 4365 | ValReg = X86::AX; |
| 4366 | Val = (Val << 8) | Val; |
| 4367 | break; |
| 4368 | case 0: // DWORD aligned |
| 4369 | AVT = MVT::i32; |
| 4370 | ValReg = X86::EAX; |
| 4371 | Val = (Val << 8) | Val; |
| 4372 | Val = (Val << 16) | Val; |
| 4373 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned |
| 4374 | AVT = MVT::i64; |
| 4375 | ValReg = X86::RAX; |
| 4376 | Val = (Val << 32) | Val; |
| 4377 | } |
| 4378 | break; |
| 4379 | default: // Byte aligned |
| 4380 | AVT = MVT::i8; |
| 4381 | ValReg = X86::AL; |
| 4382 | Count = Op.getOperand(3); |
| 4383 | break; |
| 4384 | } |
| 4385 | |
| 4386 | if (AVT > MVT::i8) { |
| 4387 | if (I) { |
| 4388 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4389 | Count = DAG.getIntPtrConstant(I->getValue() / UBytes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4390 | BytesLeft = I->getValue() % UBytes; |
| 4391 | } else { |
| 4392 | assert(AVT >= MVT::i32 && |
| 4393 | "Do not use rep;stos if not at least DWORD aligned"); |
| 4394 | Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), |
| 4395 | Op.getOperand(3), DAG.getConstant(2, MVT::i8)); |
| 4396 | TwoRepStos = true; |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), |
| 4401 | InFlag); |
| 4402 | InFlag = Chain.getValue(1); |
| 4403 | } else { |
| 4404 | AVT = MVT::i8; |
| 4405 | Count = Op.getOperand(3); |
| 4406 | Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); |
| 4407 | InFlag = Chain.getValue(1); |
| 4408 | } |
| 4409 | |
| 4410 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 4411 | Count, InFlag); |
| 4412 | InFlag = Chain.getValue(1); |
| 4413 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
| 4414 | Op.getOperand(1), InFlag); |
| 4415 | InFlag = Chain.getValue(1); |
| 4416 | |
| 4417 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4418 | SmallVector<SDOperand, 8> Ops; |
| 4419 | Ops.push_back(Chain); |
| 4420 | Ops.push_back(DAG.getValueType(AVT)); |
| 4421 | Ops.push_back(InFlag); |
| 4422 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
| 4423 | |
| 4424 | if (TwoRepStos) { |
| 4425 | InFlag = Chain.getValue(1); |
| 4426 | Count = Op.getOperand(3); |
| 4427 | MVT::ValueType CVT = Count.getValueType(); |
| 4428 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
| 4429 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 4430 | Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, |
| 4431 | Left, InFlag); |
| 4432 | InFlag = Chain.getValue(1); |
| 4433 | Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4434 | Ops.clear(); |
| 4435 | Ops.push_back(Chain); |
| 4436 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 4437 | Ops.push_back(InFlag); |
| 4438 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
| 4439 | } else if (BytesLeft) { |
| 4440 | // Issue stores for the last 1 - 7 bytes. |
| 4441 | SDOperand Value; |
| 4442 | unsigned Val = ValC->getValue() & 255; |
| 4443 | unsigned Offset = I->getValue() - BytesLeft; |
| 4444 | SDOperand DstAddr = Op.getOperand(1); |
| 4445 | MVT::ValueType AddrVT = DstAddr.getValueType(); |
| 4446 | if (BytesLeft >= 4) { |
| 4447 | Val = (Val << 8) | Val; |
| 4448 | Val = (Val << 16) | Val; |
| 4449 | Value = DAG.getConstant(Val, MVT::i32); |
| 4450 | Chain = DAG.getStore(Chain, Value, |
| 4451 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4452 | DAG.getConstant(Offset, AddrVT)), |
| 4453 | NULL, 0); |
| 4454 | BytesLeft -= 4; |
| 4455 | Offset += 4; |
| 4456 | } |
| 4457 | if (BytesLeft >= 2) { |
| 4458 | Value = DAG.getConstant((Val << 8) | Val, MVT::i16); |
| 4459 | Chain = DAG.getStore(Chain, Value, |
| 4460 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4461 | DAG.getConstant(Offset, AddrVT)), |
| 4462 | NULL, 0); |
| 4463 | BytesLeft -= 2; |
| 4464 | Offset += 2; |
| 4465 | } |
| 4466 | if (BytesLeft == 1) { |
| 4467 | Value = DAG.getConstant(Val, MVT::i8); |
| 4468 | Chain = DAG.getStore(Chain, Value, |
| 4469 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4470 | DAG.getConstant(Offset, AddrVT)), |
| 4471 | NULL, 0); |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | return Chain; |
| 4476 | } |
| 4477 | |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4478 | SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain, |
| 4479 | SDOperand Dest, |
| 4480 | SDOperand Source, |
| 4481 | unsigned Size, |
| 4482 | unsigned Align, |
| 4483 | SelectionDAG &DAG) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4484 | MVT::ValueType AVT; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4485 | unsigned BytesLeft = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4486 | switch (Align & 3) { |
| 4487 | case 2: // WORD aligned |
| 4488 | AVT = MVT::i16; |
| 4489 | break; |
| 4490 | case 0: // DWORD aligned |
| 4491 | AVT = MVT::i32; |
| 4492 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned |
| 4493 | AVT = MVT::i64; |
| 4494 | break; |
| 4495 | default: // Byte aligned |
| 4496 | AVT = MVT::i8; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4497 | break; |
| 4498 | } |
| 4499 | |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4500 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4501 | SDOperand Count = DAG.getIntPtrConstant(Size / UBytes); |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4502 | BytesLeft = Size % UBytes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4503 | |
| 4504 | SDOperand InFlag(0, 0); |
| 4505 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 4506 | Count, InFlag); |
| 4507 | InFlag = Chain.getValue(1); |
| 4508 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4509 | Dest, InFlag); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4510 | InFlag = Chain.getValue(1); |
| 4511 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI, |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4512 | Source, InFlag); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4513 | InFlag = Chain.getValue(1); |
| 4514 | |
| 4515 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4516 | SmallVector<SDOperand, 8> Ops; |
| 4517 | Ops.push_back(Chain); |
| 4518 | Ops.push_back(DAG.getValueType(AVT)); |
| 4519 | Ops.push_back(InFlag); |
| 4520 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); |
| 4521 | |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4522 | if (BytesLeft) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4523 | // Issue loads and stores for the last 1 - 7 bytes. |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4524 | unsigned Offset = Size - BytesLeft; |
| 4525 | SDOperand DstAddr = Dest; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4526 | MVT::ValueType DstVT = DstAddr.getValueType(); |
Rafael Espindola | f12f3a9 | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 4527 | SDOperand SrcAddr = Source; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4528 | MVT::ValueType SrcVT = SrcAddr.getValueType(); |
| 4529 | SDOperand Value; |
| 4530 | if (BytesLeft >= 4) { |
| 4531 | Value = DAG.getLoad(MVT::i32, Chain, |
| 4532 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4533 | DAG.getConstant(Offset, SrcVT)), |
| 4534 | NULL, 0); |
| 4535 | Chain = Value.getValue(1); |
| 4536 | Chain = DAG.getStore(Chain, Value, |
| 4537 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4538 | DAG.getConstant(Offset, DstVT)), |
| 4539 | NULL, 0); |
| 4540 | BytesLeft -= 4; |
| 4541 | Offset += 4; |
| 4542 | } |
| 4543 | if (BytesLeft >= 2) { |
| 4544 | Value = DAG.getLoad(MVT::i16, Chain, |
| 4545 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4546 | DAG.getConstant(Offset, SrcVT)), |
| 4547 | NULL, 0); |
| 4548 | Chain = Value.getValue(1); |
| 4549 | Chain = DAG.getStore(Chain, Value, |
| 4550 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4551 | DAG.getConstant(Offset, DstVT)), |
| 4552 | NULL, 0); |
| 4553 | BytesLeft -= 2; |
| 4554 | Offset += 2; |
| 4555 | } |
| 4556 | |
| 4557 | if (BytesLeft == 1) { |
| 4558 | Value = DAG.getLoad(MVT::i8, Chain, |
| 4559 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4560 | DAG.getConstant(Offset, SrcVT)), |
| 4561 | NULL, 0); |
| 4562 | Chain = Value.getValue(1); |
| 4563 | Chain = DAG.getStore(Chain, Value, |
| 4564 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4565 | DAG.getConstant(Offset, DstVT)), |
| 4566 | NULL, 0); |
| 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | return Chain; |
| 4571 | } |
| 4572 | |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4573 | /// Expand the result of: i64,outchain = READCYCLECOUNTER inchain |
| 4574 | SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4575 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4576 | SDOperand TheChain = N->getOperand(0); |
| 4577 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4578 | if (Subtarget->is64Bit()) { |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4579 | SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1)); |
| 4580 | SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, |
| 4581 | MVT::i64, rax.getValue(2)); |
| 4582 | SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4583 | DAG.getConstant(32, MVT::i8)); |
| 4584 | SDOperand Ops[] = { |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4585 | DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4586 | }; |
| 4587 | |
| 4588 | Tys = DAG.getVTList(MVT::i64, MVT::Other); |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4589 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4592 | SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)); |
| 4593 | SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, |
| 4594 | MVT::i32, eax.getValue(2)); |
| 4595 | // Use a buildpair to merge the two 32-bit values into a 64-bit one. |
| 4596 | SDOperand Ops[] = { eax, edx }; |
| 4597 | Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2); |
| 4598 | |
| 4599 | // Use a MERGE_VALUES to return the value and chain. |
| 4600 | Ops[1] = edx.getValue(1); |
| 4601 | Tys = DAG.getVTList(MVT::i64, MVT::Other); |
| 4602 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) { |
| 4606 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2)); |
| 4607 | |
| 4608 | if (!Subtarget->is64Bit()) { |
| 4609 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 4610 | // memory location argument. |
| 4611 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
| 4612 | return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(), |
| 4613 | SV->getOffset()); |
| 4614 | } |
| 4615 | |
| 4616 | // __va_list_tag: |
| 4617 | // gp_offset (0 - 6 * 8) |
| 4618 | // fp_offset (48 - 48 + 8 * 16) |
| 4619 | // overflow_arg_area (point to parameters coming in memory). |
| 4620 | // reg_save_area |
| 4621 | SmallVector<SDOperand, 8> MemOps; |
| 4622 | SDOperand FIN = Op.getOperand(1); |
| 4623 | // Store gp_offset |
| 4624 | SDOperand Store = DAG.getStore(Op.getOperand(0), |
| 4625 | DAG.getConstant(VarArgsGPOffset, MVT::i32), |
| 4626 | FIN, SV->getValue(), SV->getOffset()); |
| 4627 | MemOps.push_back(Store); |
| 4628 | |
| 4629 | // Store fp_offset |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4630 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4631 | Store = DAG.getStore(Op.getOperand(0), |
| 4632 | DAG.getConstant(VarArgsFPOffset, MVT::i32), |
| 4633 | FIN, SV->getValue(), SV->getOffset()); |
| 4634 | MemOps.push_back(Store); |
| 4635 | |
| 4636 | // Store ptr to overflow_arg_area |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4637 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4638 | SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
| 4639 | Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(), |
| 4640 | SV->getOffset()); |
| 4641 | MemOps.push_back(Store); |
| 4642 | |
| 4643 | // Store ptr to reg_save_area. |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4644 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4645 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
| 4646 | Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(), |
| 4647 | SV->getOffset()); |
| 4648 | MemOps.push_back(Store); |
| 4649 | return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size()); |
| 4650 | } |
| 4651 | |
| 4652 | SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) { |
| 4653 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
| 4654 | SDOperand Chain = Op.getOperand(0); |
| 4655 | SDOperand DstPtr = Op.getOperand(1); |
| 4656 | SDOperand SrcPtr = Op.getOperand(2); |
| 4657 | SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3)); |
| 4658 | SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4)); |
| 4659 | |
| 4660 | SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, |
| 4661 | SrcSV->getValue(), SrcSV->getOffset()); |
| 4662 | Chain = SrcPtr.getValue(1); |
| 4663 | for (unsigned i = 0; i < 3; ++i) { |
| 4664 | SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, |
| 4665 | SrcSV->getValue(), SrcSV->getOffset()); |
| 4666 | Chain = Val.getValue(1); |
| 4667 | Chain = DAG.getStore(Chain, Val, DstPtr, |
| 4668 | DstSV->getValue(), DstSV->getOffset()); |
| 4669 | if (i == 2) |
| 4670 | break; |
| 4671 | SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4672 | DAG.getIntPtrConstant(8)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4673 | DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4674 | DAG.getIntPtrConstant(8)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4675 | } |
| 4676 | return Chain; |
| 4677 | } |
| 4678 | |
| 4679 | SDOperand |
| 4680 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) { |
| 4681 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue(); |
| 4682 | switch (IntNo) { |
| 4683 | default: return SDOperand(); // Don't custom lower most intrinsics. |
| 4684 | // Comparison intrinsics. |
| 4685 | case Intrinsic::x86_sse_comieq_ss: |
| 4686 | case Intrinsic::x86_sse_comilt_ss: |
| 4687 | case Intrinsic::x86_sse_comile_ss: |
| 4688 | case Intrinsic::x86_sse_comigt_ss: |
| 4689 | case Intrinsic::x86_sse_comige_ss: |
| 4690 | case Intrinsic::x86_sse_comineq_ss: |
| 4691 | case Intrinsic::x86_sse_ucomieq_ss: |
| 4692 | case Intrinsic::x86_sse_ucomilt_ss: |
| 4693 | case Intrinsic::x86_sse_ucomile_ss: |
| 4694 | case Intrinsic::x86_sse_ucomigt_ss: |
| 4695 | case Intrinsic::x86_sse_ucomige_ss: |
| 4696 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4697 | case Intrinsic::x86_sse2_comieq_sd: |
| 4698 | case Intrinsic::x86_sse2_comilt_sd: |
| 4699 | case Intrinsic::x86_sse2_comile_sd: |
| 4700 | case Intrinsic::x86_sse2_comigt_sd: |
| 4701 | case Intrinsic::x86_sse2_comige_sd: |
| 4702 | case Intrinsic::x86_sse2_comineq_sd: |
| 4703 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 4704 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 4705 | case Intrinsic::x86_sse2_ucomile_sd: |
| 4706 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 4707 | case Intrinsic::x86_sse2_ucomige_sd: |
| 4708 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 4709 | unsigned Opc = 0; |
| 4710 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 4711 | switch (IntNo) { |
| 4712 | default: break; |
| 4713 | case Intrinsic::x86_sse_comieq_ss: |
| 4714 | case Intrinsic::x86_sse2_comieq_sd: |
| 4715 | Opc = X86ISD::COMI; |
| 4716 | CC = ISD::SETEQ; |
| 4717 | break; |
| 4718 | case Intrinsic::x86_sse_comilt_ss: |
| 4719 | case Intrinsic::x86_sse2_comilt_sd: |
| 4720 | Opc = X86ISD::COMI; |
| 4721 | CC = ISD::SETLT; |
| 4722 | break; |
| 4723 | case Intrinsic::x86_sse_comile_ss: |
| 4724 | case Intrinsic::x86_sse2_comile_sd: |
| 4725 | Opc = X86ISD::COMI; |
| 4726 | CC = ISD::SETLE; |
| 4727 | break; |
| 4728 | case Intrinsic::x86_sse_comigt_ss: |
| 4729 | case Intrinsic::x86_sse2_comigt_sd: |
| 4730 | Opc = X86ISD::COMI; |
| 4731 | CC = ISD::SETGT; |
| 4732 | break; |
| 4733 | case Intrinsic::x86_sse_comige_ss: |
| 4734 | case Intrinsic::x86_sse2_comige_sd: |
| 4735 | Opc = X86ISD::COMI; |
| 4736 | CC = ISD::SETGE; |
| 4737 | break; |
| 4738 | case Intrinsic::x86_sse_comineq_ss: |
| 4739 | case Intrinsic::x86_sse2_comineq_sd: |
| 4740 | Opc = X86ISD::COMI; |
| 4741 | CC = ISD::SETNE; |
| 4742 | break; |
| 4743 | case Intrinsic::x86_sse_ucomieq_ss: |
| 4744 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 4745 | Opc = X86ISD::UCOMI; |
| 4746 | CC = ISD::SETEQ; |
| 4747 | break; |
| 4748 | case Intrinsic::x86_sse_ucomilt_ss: |
| 4749 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 4750 | Opc = X86ISD::UCOMI; |
| 4751 | CC = ISD::SETLT; |
| 4752 | break; |
| 4753 | case Intrinsic::x86_sse_ucomile_ss: |
| 4754 | case Intrinsic::x86_sse2_ucomile_sd: |
| 4755 | Opc = X86ISD::UCOMI; |
| 4756 | CC = ISD::SETLE; |
| 4757 | break; |
| 4758 | case Intrinsic::x86_sse_ucomigt_ss: |
| 4759 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 4760 | Opc = X86ISD::UCOMI; |
| 4761 | CC = ISD::SETGT; |
| 4762 | break; |
| 4763 | case Intrinsic::x86_sse_ucomige_ss: |
| 4764 | case Intrinsic::x86_sse2_ucomige_sd: |
| 4765 | Opc = X86ISD::UCOMI; |
| 4766 | CC = ISD::SETGE; |
| 4767 | break; |
| 4768 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4769 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 4770 | Opc = X86ISD::UCOMI; |
| 4771 | CC = ISD::SETNE; |
| 4772 | break; |
| 4773 | } |
| 4774 | |
| 4775 | unsigned X86CC; |
| 4776 | SDOperand LHS = Op.getOperand(1); |
| 4777 | SDOperand RHS = Op.getOperand(2); |
| 4778 | translateX86CC(CC, true, X86CC, LHS, RHS, DAG); |
| 4779 | |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 4780 | SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS); |
| 4781 | SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 4782 | DAG.getConstant(X86CC, MVT::i8), Cond); |
| 4783 | return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4784 | } |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4789 | // Depths > 0 not supported yet! |
| 4790 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4791 | return SDOperand(); |
| 4792 | |
| 4793 | // Just load the return address |
| 4794 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4795 | return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0); |
| 4796 | } |
| 4797 | |
| 4798 | SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4799 | // Depths > 0 not supported yet! |
| 4800 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4801 | return SDOperand(); |
| 4802 | |
| 4803 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4804 | return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4805 | DAG.getIntPtrConstant(4)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op, |
| 4809 | SelectionDAG &DAG) { |
| 4810 | // Is not yet supported on x86-64 |
| 4811 | if (Subtarget->is64Bit()) |
| 4812 | return SDOperand(); |
| 4813 | |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4814 | return DAG.getIntPtrConstant(8); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG) |
| 4818 | { |
| 4819 | assert(!Subtarget->is64Bit() && |
| 4820 | "Lowering of eh_return builtin is not supported yet on x86-64"); |
| 4821 | |
| 4822 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4823 | SDOperand Chain = Op.getOperand(0); |
| 4824 | SDOperand Offset = Op.getOperand(1); |
| 4825 | SDOperand Handler = Op.getOperand(2); |
| 4826 | |
| 4827 | SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF), |
| 4828 | getPointerTy()); |
| 4829 | |
| 4830 | SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame, |
Chris Lattner | 5872a36 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4831 | DAG.getIntPtrConstant(-4UL)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4832 | StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset); |
| 4833 | Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0); |
| 4834 | Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 4835 | MF.getRegInfo().addLiveOut(X86::ECX); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4836 | |
| 4837 | return DAG.getNode(X86ISD::EH_RETURN, MVT::Other, |
| 4838 | Chain, DAG.getRegister(X86::ECX, getPointerTy())); |
| 4839 | } |
| 4840 | |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4841 | SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op, |
| 4842 | SelectionDAG &DAG) { |
| 4843 | SDOperand Root = Op.getOperand(0); |
| 4844 | SDOperand Trmp = Op.getOperand(1); // trampoline |
| 4845 | SDOperand FPtr = Op.getOperand(2); // nested function |
| 4846 | SDOperand Nest = Op.getOperand(3); // 'nest' parameter value |
| 4847 | |
| 4848 | SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4)); |
| 4849 | |
Duncan Sands | 3e8ff6f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 4850 | const X86InstrInfo *TII = |
| 4851 | ((X86TargetMachine&)getTargetMachine()).getInstrInfo(); |
| 4852 | |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4853 | if (Subtarget->is64Bit()) { |
Duncan Sands | 3e8ff6f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 4854 | SDOperand OutChains[6]; |
| 4855 | |
| 4856 | // Large code-model. |
| 4857 | |
| 4858 | const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r); |
| 4859 | const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri); |
| 4860 | |
| 4861 | const unsigned char N86R10 = |
| 4862 | ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10); |
| 4863 | const unsigned char N86R11 = |
| 4864 | ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11); |
| 4865 | |
| 4866 | const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix |
| 4867 | |
| 4868 | // Load the pointer to the nested function into R11. |
| 4869 | unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 |
| 4870 | SDOperand Addr = Trmp; |
| 4871 | OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr, |
| 4872 | TrmpSV->getValue(), TrmpSV->getOffset()); |
| 4873 | |
| 4874 | Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64)); |
| 4875 | OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpSV->getValue(), |
| 4876 | TrmpSV->getOffset() + 2, false, 2); |
| 4877 | |
| 4878 | // Load the 'nest' parameter value into R10. |
| 4879 | // R10 is specified in X86CallingConv.td |
| 4880 | OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 |
| 4881 | Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64)); |
| 4882 | OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr, |
| 4883 | TrmpSV->getValue(), TrmpSV->getOffset() + 10); |
| 4884 | |
| 4885 | Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64)); |
| 4886 | OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(), |
| 4887 | TrmpSV->getOffset() + 12, false, 2); |
| 4888 | |
| 4889 | // Jump to the nested function. |
| 4890 | OpCode = (JMP64r << 8) | REX_WB; // jmpq *... |
| 4891 | Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64)); |
| 4892 | OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr, |
| 4893 | TrmpSV->getValue(), TrmpSV->getOffset() + 20); |
| 4894 | |
| 4895 | unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 |
| 4896 | Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64)); |
| 4897 | OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr, |
| 4898 | TrmpSV->getValue(), TrmpSV->getOffset() + 22); |
| 4899 | |
| 4900 | SDOperand Ops[] = |
| 4901 | { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) }; |
| 4902 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4903 | } else { |
| 4904 | Function *Func = (Function *) |
| 4905 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); |
| 4906 | unsigned CC = Func->getCallingConv(); |
Duncan Sands | 466eadd | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4907 | unsigned NestReg; |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4908 | |
| 4909 | switch (CC) { |
| 4910 | default: |
| 4911 | assert(0 && "Unsupported calling convention"); |
| 4912 | case CallingConv::C: |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4913 | case CallingConv::X86_StdCall: { |
| 4914 | // Pass 'nest' parameter in ECX. |
| 4915 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | 466eadd | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4916 | NestReg = X86::ECX; |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4917 | |
| 4918 | // Check that ECX wasn't needed by an 'inreg' parameter. |
| 4919 | const FunctionType *FTy = Func->getFunctionType(); |
Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 4920 | const ParamAttrsList *Attrs = Func->getParamAttrs(); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4921 | |
| 4922 | if (Attrs && !Func->isVarArg()) { |
| 4923 | unsigned InRegCount = 0; |
| 4924 | unsigned Idx = 1; |
| 4925 | |
| 4926 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 4927 | E = FTy->param_end(); I != E; ++I, ++Idx) |
| 4928 | if (Attrs->paramHasAttr(Idx, ParamAttr::InReg)) |
| 4929 | // FIXME: should only count parameters that are lowered to integers. |
| 4930 | InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32; |
| 4931 | |
| 4932 | if (InRegCount > 2) { |
| 4933 | cerr << "Nest register in use - reduce number of inreg parameters!\n"; |
| 4934 | abort(); |
| 4935 | } |
| 4936 | } |
| 4937 | break; |
| 4938 | } |
| 4939 | case CallingConv::X86_FastCall: |
| 4940 | // Pass 'nest' parameter in EAX. |
| 4941 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | 466eadd | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4942 | NestReg = X86::EAX; |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4943 | break; |
| 4944 | } |
| 4945 | |
| 4946 | SDOperand OutChains[4]; |
| 4947 | SDOperand Addr, Disp; |
| 4948 | |
| 4949 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32)); |
| 4950 | Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr); |
| 4951 | |
Duncan Sands | 3e8ff6f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 4952 | const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri); |
| 4953 | const unsigned char N86Reg = |
| 4954 | ((X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg); |
Duncan Sands | 466eadd | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4955 | OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8), |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4956 | Trmp, TrmpSV->getValue(), TrmpSV->getOffset()); |
| 4957 | |
| 4958 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32)); |
| 4959 | OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(), |
| 4960 | TrmpSV->getOffset() + 1, false, 1); |
| 4961 | |
Duncan Sands | 3e8ff6f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 4962 | const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4963 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32)); |
| 4964 | OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr, |
| 4965 | TrmpSV->getValue() + 5, TrmpSV->getOffset()); |
| 4966 | |
| 4967 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32)); |
| 4968 | OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(), |
| 4969 | TrmpSV->getOffset() + 6, false, 1); |
| 4970 | |
Duncan Sands | 7407a9f | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 4971 | SDOperand Ops[] = |
| 4972 | { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) }; |
| 4973 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4974 | } |
| 4975 | } |
| 4976 | |
Anton Korobeynikov | fbe230e | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 4977 | SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) { |
| 4978 | /* |
| 4979 | The rounding mode is in bits 11:10 of FPSR, and has the following |
| 4980 | settings: |
| 4981 | 00 Round to nearest |
| 4982 | 01 Round to -inf |
| 4983 | 10 Round to +inf |
| 4984 | 11 Round to 0 |
| 4985 | |
| 4986 | FLT_ROUNDS, on the other hand, expects the following: |
| 4987 | -1 Undefined |
| 4988 | 0 Round to 0 |
| 4989 | 1 Round to nearest |
| 4990 | 2 Round to +inf |
| 4991 | 3 Round to -inf |
| 4992 | |
| 4993 | To perform the conversion, we do: |
| 4994 | (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3) |
| 4995 | */ |
| 4996 | |
| 4997 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4998 | const TargetMachine &TM = MF.getTarget(); |
| 4999 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 5000 | unsigned StackAlignment = TFI.getStackAlignment(); |
| 5001 | MVT::ValueType VT = Op.getValueType(); |
| 5002 | |
| 5003 | // Save FP Control Word to stack slot |
| 5004 | int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment); |
| 5005 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 5006 | |
| 5007 | SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other, |
| 5008 | DAG.getEntryNode(), StackSlot); |
| 5009 | |
| 5010 | // Load FP Control Word from stack slot |
| 5011 | SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0); |
| 5012 | |
| 5013 | // Transform as necessary |
| 5014 | SDOperand CWD1 = |
| 5015 | DAG.getNode(ISD::SRL, MVT::i16, |
| 5016 | DAG.getNode(ISD::AND, MVT::i16, |
| 5017 | CWD, DAG.getConstant(0x800, MVT::i16)), |
| 5018 | DAG.getConstant(11, MVT::i8)); |
| 5019 | SDOperand CWD2 = |
| 5020 | DAG.getNode(ISD::SRL, MVT::i16, |
| 5021 | DAG.getNode(ISD::AND, MVT::i16, |
| 5022 | CWD, DAG.getConstant(0x400, MVT::i16)), |
| 5023 | DAG.getConstant(9, MVT::i8)); |
| 5024 | |
| 5025 | SDOperand RetVal = |
| 5026 | DAG.getNode(ISD::AND, MVT::i16, |
| 5027 | DAG.getNode(ISD::ADD, MVT::i16, |
| 5028 | DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2), |
| 5029 | DAG.getConstant(1, MVT::i16)), |
| 5030 | DAG.getConstant(3, MVT::i16)); |
| 5031 | |
| 5032 | |
| 5033 | return DAG.getNode((MVT::getSizeInBits(VT) < 16 ? |
| 5034 | ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal); |
| 5035 | } |
| 5036 | |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5037 | SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) { |
| 5038 | MVT::ValueType VT = Op.getValueType(); |
| 5039 | MVT::ValueType OpVT = VT; |
| 5040 | unsigned NumBits = MVT::getSizeInBits(VT); |
| 5041 | |
| 5042 | Op = Op.getOperand(0); |
| 5043 | if (VT == MVT::i8) { |
Evan Cheng | 7cfbfe3 | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 5044 | // Zero extend to i32 since there is not an i8 bsr. |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5045 | OpVT = MVT::i32; |
| 5046 | Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op); |
| 5047 | } |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5048 | |
Evan Cheng | 7cfbfe3 | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 5049 | // Issue a bsr (scan bits in reverse) which also sets EFLAGS. |
| 5050 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
| 5051 | Op = DAG.getNode(X86ISD::BSR, VTs, Op); |
| 5052 | |
| 5053 | // If src is zero (i.e. bsr sets ZF), returns NumBits. |
| 5054 | SmallVector<SDOperand, 4> Ops; |
| 5055 | Ops.push_back(Op); |
| 5056 | Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT)); |
| 5057 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 5058 | Ops.push_back(Op.getValue(1)); |
| 5059 | Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4); |
| 5060 | |
| 5061 | // Finally xor with NumBits-1. |
| 5062 | Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT)); |
| 5063 | |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5064 | if (VT == MVT::i8) |
| 5065 | Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op); |
| 5066 | return Op; |
| 5067 | } |
| 5068 | |
| 5069 | SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) { |
| 5070 | MVT::ValueType VT = Op.getValueType(); |
| 5071 | MVT::ValueType OpVT = VT; |
Evan Cheng | 7cfbfe3 | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 5072 | unsigned NumBits = MVT::getSizeInBits(VT); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5073 | |
| 5074 | Op = Op.getOperand(0); |
| 5075 | if (VT == MVT::i8) { |
| 5076 | OpVT = MVT::i32; |
| 5077 | Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op); |
| 5078 | } |
Evan Cheng | 7cfbfe3 | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 5079 | |
| 5080 | // Issue a bsf (scan bits forward) which also sets EFLAGS. |
| 5081 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
| 5082 | Op = DAG.getNode(X86ISD::BSF, VTs, Op); |
| 5083 | |
| 5084 | // If src is zero (i.e. bsf sets ZF), returns NumBits. |
| 5085 | SmallVector<SDOperand, 4> Ops; |
| 5086 | Ops.push_back(Op); |
| 5087 | Ops.push_back(DAG.getConstant(NumBits, OpVT)); |
| 5088 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 5089 | Ops.push_back(Op.getValue(1)); |
| 5090 | Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4); |
| 5091 | |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5092 | if (VT == MVT::i8) |
| 5093 | Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op); |
| 5094 | return Op; |
| 5095 | } |
| 5096 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5097 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 5098 | /// |
| 5099 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 5100 | switch (Op.getOpcode()) { |
| 5101 | default: assert(0 && "Should not custom lower this!"); |
| 5102 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 5103 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 5104 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 5105 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 5106 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 5107 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 5108 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 5109 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
| 5110 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
| 5111 | case ISD::SHL_PARTS: |
| 5112 | case ISD::SRA_PARTS: |
| 5113 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 5114 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
| 5115 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| 5116 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 5117 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
| 5118 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5119 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
| 5120 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 5121 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5122 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
| 5123 | case ISD::CALL: return LowerCALL(Op, DAG); |
| 5124 | case ISD::RET: return LowerRET(Op, DAG); |
| 5125 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
| 5126 | case ISD::MEMSET: return LowerMEMSET(Op, DAG); |
| 5127 | case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5128 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
| 5129 | case ISD::VACOPY: return LowerVACOPY(Op, DAG); |
| 5130 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 5131 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 5132 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
| 5133 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 5134 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); |
| 5135 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); |
| 5136 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); |
Duncan Sands | d8455ca | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 5137 | case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); |
Anton Korobeynikov | fbe230e | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 5138 | case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG); |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5139 | case ISD::CTLZ: return LowerCTLZ(Op, DAG); |
| 5140 | case ISD::CTTZ: return LowerCTTZ(Op, DAG); |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5141 | |
| 5142 | // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands. |
| 5143 | case ISD::READCYCLECOUNTER: |
| 5144 | return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5145 | } |
Chris Lattner | dfb947d | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | /// ExpandOperation - Provide custom lowering hooks for expanding operations. |
| 5149 | SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) { |
| 5150 | switch (N->getOpcode()) { |
| 5151 | default: assert(0 && "Should not custom lower this!"); |
| 5152 | case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG); |
| 5153 | case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG); |
| 5154 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
| 5157 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 5158 | switch (Opcode) { |
| 5159 | default: return NULL; |
Evan Cheng | 48679f4 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 5160 | case X86ISD::BSF: return "X86ISD::BSF"; |
| 5161 | case X86ISD::BSR: return "X86ISD::BSR"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5162 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 5163 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
| 5164 | case X86ISD::FAND: return "X86ISD::FAND"; |
| 5165 | case X86ISD::FOR: return "X86ISD::FOR"; |
| 5166 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
| 5167 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
| 5168 | case X86ISD::FILD: return "X86ISD::FILD"; |
| 5169 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
| 5170 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 5171 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 5172 | case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; |
| 5173 | case X86ISD::FLD: return "X86ISD::FLD"; |
| 5174 | case X86ISD::FST: return "X86ISD::FST"; |
| 5175 | case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; |
| 5176 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
| 5177 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 5178 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 5179 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 5180 | case X86ISD::CMP: return "X86ISD::CMP"; |
| 5181 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 5182 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
| 5183 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
| 5184 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 5185 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
| 5186 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
| 5187 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 5188 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5189 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
| 5190 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
| 5191 | case X86ISD::S2VEC: return "X86ISD::S2VEC"; |
| 5192 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
| 5193 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
| 5194 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 5195 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
| 5196 | case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; |
| 5197 | case X86ISD::FRCP: return "X86ISD::FRCP"; |
| 5198 | case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; |
| 5199 | case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER"; |
| 5200 | case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 5201 | case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN"; |
Anton Korobeynikov | fbe230e | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 5202 | case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 5207 | // by AM is legal for this target, for a load/store of the specified type. |
| 5208 | bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 5209 | const Type *Ty) const { |
| 5210 | // X86 supports extremely general addressing modes. |
| 5211 | |
| 5212 | // X86 allows a sign-extended 32-bit immediate field as a displacement. |
| 5213 | if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1) |
| 5214 | return false; |
| 5215 | |
| 5216 | if (AM.BaseGV) { |
Evan Cheng | 6a1f3f1 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 5217 | // We can only fold this if we don't need an extra load. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5218 | if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false)) |
| 5219 | return false; |
Evan Cheng | 6a1f3f1 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 5220 | |
| 5221 | // X86-64 only supports addr of globals in small code model. |
| 5222 | if (Subtarget->is64Bit()) { |
| 5223 | if (getTargetMachine().getCodeModel() != CodeModel::Small) |
| 5224 | return false; |
| 5225 | // If lower 4G is not available, then we must use rip-relative addressing. |
| 5226 | if (AM.BaseOffs || AM.Scale > 1) |
| 5227 | return false; |
| 5228 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | switch (AM.Scale) { |
| 5232 | case 0: |
| 5233 | case 1: |
| 5234 | case 2: |
| 5235 | case 4: |
| 5236 | case 8: |
| 5237 | // These scales always work. |
| 5238 | break; |
| 5239 | case 3: |
| 5240 | case 5: |
| 5241 | case 9: |
| 5242 | // These scales are formed with basereg+scalereg. Only accept if there is |
| 5243 | // no basereg yet. |
| 5244 | if (AM.HasBaseReg) |
| 5245 | return false; |
| 5246 | break; |
| 5247 | default: // Other stuff never works. |
| 5248 | return false; |
| 5249 | } |
| 5250 | |
| 5251 | return true; |
| 5252 | } |
| 5253 | |
| 5254 | |
Evan Cheng | 27a820a | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 5255 | bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const { |
| 5256 | if (!Ty1->isInteger() || !Ty2->isInteger()) |
| 5257 | return false; |
Evan Cheng | 7f15260 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 5258 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); |
| 5259 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); |
| 5260 | if (NumBits1 <= NumBits2) |
| 5261 | return false; |
| 5262 | return Subtarget->is64Bit() || NumBits1 < 64; |
Evan Cheng | 27a820a | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Evan Cheng | 9decb33 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 5265 | bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1, |
| 5266 | MVT::ValueType VT2) const { |
| 5267 | if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2)) |
| 5268 | return false; |
| 5269 | unsigned NumBits1 = MVT::getSizeInBits(VT1); |
| 5270 | unsigned NumBits2 = MVT::getSizeInBits(VT2); |
| 5271 | if (NumBits1 <= NumBits2) |
| 5272 | return false; |
| 5273 | return Subtarget->is64Bit() || NumBits1 < 64; |
| 5274 | } |
Evan Cheng | 27a820a | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 5275 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5276 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 5277 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 5278 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 5279 | /// are assumed to be legal. |
| 5280 | bool |
| 5281 | X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const { |
| 5282 | // Only do shuffles on 128-bit vector types for now. |
| 5283 | if (MVT::getSizeInBits(VT) == 64) return false; |
| 5284 | return (Mask.Val->getNumOperands() <= 4 || |
| 5285 | isIdentityMask(Mask.Val) || |
| 5286 | isIdentityMask(Mask.Val, true) || |
| 5287 | isSplatMask(Mask.Val) || |
| 5288 | isPSHUFHW_PSHUFLWMask(Mask.Val) || |
| 5289 | X86::isUNPCKLMask(Mask.Val) || |
| 5290 | X86::isUNPCKHMask(Mask.Val) || |
| 5291 | X86::isUNPCKL_v_undef_Mask(Mask.Val) || |
| 5292 | X86::isUNPCKH_v_undef_Mask(Mask.Val)); |
| 5293 | } |
| 5294 | |
| 5295 | bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps, |
| 5296 | MVT::ValueType EVT, |
| 5297 | SelectionDAG &DAG) const { |
| 5298 | unsigned NumElts = BVOps.size(); |
| 5299 | // Only do shuffles on 128-bit vector types for now. |
| 5300 | if (MVT::getSizeInBits(EVT) * NumElts == 64) return false; |
| 5301 | if (NumElts == 2) return true; |
| 5302 | if (NumElts == 4) { |
| 5303 | return (isMOVLMask(&BVOps[0], 4) || |
| 5304 | isCommutedMOVL(&BVOps[0], 4, true) || |
| 5305 | isSHUFPMask(&BVOps[0], 4) || |
| 5306 | isCommutedSHUFP(&BVOps[0], 4)); |
| 5307 | } |
| 5308 | return false; |
| 5309 | } |
| 5310 | |
| 5311 | //===----------------------------------------------------------------------===// |
| 5312 | // X86 Scheduler Hooks |
| 5313 | //===----------------------------------------------------------------------===// |
| 5314 | |
| 5315 | MachineBasicBlock * |
| 5316 | X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 5317 | MachineBasicBlock *BB) { |
| 5318 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 5319 | switch (MI->getOpcode()) { |
| 5320 | default: assert(false && "Unexpected instr type to insert"); |
| 5321 | case X86::CMOV_FR32: |
| 5322 | case X86::CMOV_FR64: |
| 5323 | case X86::CMOV_V4F32: |
| 5324 | case X86::CMOV_V2F64: |
Evan Cheng | 621216e | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5325 | case X86::CMOV_V2I64: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5326 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 5327 | // diamond control-flow pattern. The incoming instruction knows the |
| 5328 | // destination vreg to set, the condition code register to branch on, the |
| 5329 | // true/false values to select between, and a branch opcode to use. |
| 5330 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 5331 | ilist<MachineBasicBlock>::iterator It = BB; |
| 5332 | ++It; |
| 5333 | |
| 5334 | // thisMBB: |
| 5335 | // ... |
| 5336 | // TrueVal = ... |
| 5337 | // cmpTY ccX, r1, r2 |
| 5338 | // bCC copy1MBB |
| 5339 | // fallthrough --> copy0MBB |
| 5340 | MachineBasicBlock *thisMBB = BB; |
| 5341 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 5342 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 5343 | unsigned Opc = |
| 5344 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
| 5345 | BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB); |
| 5346 | MachineFunction *F = BB->getParent(); |
| 5347 | F->getBasicBlockList().insert(It, copy0MBB); |
| 5348 | F->getBasicBlockList().insert(It, sinkMBB); |
| 5349 | // Update machine-CFG edges by first adding all successors of the current |
| 5350 | // block to the new block which will contain the Phi node for the select. |
| 5351 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
| 5352 | e = BB->succ_end(); i != e; ++i) |
| 5353 | sinkMBB->addSuccessor(*i); |
| 5354 | // Next, remove all successors of the current block, and add the true |
| 5355 | // and fallthrough blocks as its successors. |
| 5356 | while(!BB->succ_empty()) |
| 5357 | BB->removeSuccessor(BB->succ_begin()); |
| 5358 | BB->addSuccessor(copy0MBB); |
| 5359 | BB->addSuccessor(sinkMBB); |
| 5360 | |
| 5361 | // copy0MBB: |
| 5362 | // %FalseValue = ... |
| 5363 | // # fallthrough to sinkMBB |
| 5364 | BB = copy0MBB; |
| 5365 | |
| 5366 | // Update machine-CFG edges |
| 5367 | BB->addSuccessor(sinkMBB); |
| 5368 | |
| 5369 | // sinkMBB: |
| 5370 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 5371 | // ... |
| 5372 | BB = sinkMBB; |
| 5373 | BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg()) |
| 5374 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 5375 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 5376 | |
| 5377 | delete MI; // The pseudo instruction is gone now. |
| 5378 | return BB; |
| 5379 | } |
| 5380 | |
| 5381 | case X86::FP32_TO_INT16_IN_MEM: |
| 5382 | case X86::FP32_TO_INT32_IN_MEM: |
| 5383 | case X86::FP32_TO_INT64_IN_MEM: |
| 5384 | case X86::FP64_TO_INT16_IN_MEM: |
| 5385 | case X86::FP64_TO_INT32_IN_MEM: |
Dale Johannesen | 6d0e36a | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 5386 | case X86::FP64_TO_INT64_IN_MEM: |
| 5387 | case X86::FP80_TO_INT16_IN_MEM: |
| 5388 | case X86::FP80_TO_INT32_IN_MEM: |
| 5389 | case X86::FP80_TO_INT64_IN_MEM: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5390 | // Change the floating point control register to use "round towards zero" |
| 5391 | // mode when truncating to an integer value. |
| 5392 | MachineFunction *F = BB->getParent(); |
| 5393 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 5394 | addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
| 5395 | |
| 5396 | // Load the old value of the high byte of the control word... |
| 5397 | unsigned OldCW = |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 5398 | F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5399 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx); |
| 5400 | |
| 5401 | // Set the high part to be round to zero... |
| 5402 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx) |
| 5403 | .addImm(0xC7F); |
| 5404 | |
| 5405 | // Reload the modified control word now... |
| 5406 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
| 5407 | |
| 5408 | // Restore the memory image of control word to original value |
| 5409 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx) |
| 5410 | .addReg(OldCW); |
| 5411 | |
| 5412 | // Get the X86 opcode to use. |
| 5413 | unsigned Opc; |
| 5414 | switch (MI->getOpcode()) { |
| 5415 | default: assert(0 && "illegal opcode!"); |
| 5416 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; |
| 5417 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; |
| 5418 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; |
| 5419 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; |
| 5420 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; |
| 5421 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; |
Dale Johannesen | 6d0e36a | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 5422 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; |
| 5423 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; |
| 5424 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | X86AddressMode AM; |
| 5428 | MachineOperand &Op = MI->getOperand(0); |
| 5429 | if (Op.isRegister()) { |
| 5430 | AM.BaseType = X86AddressMode::RegBase; |
| 5431 | AM.Base.Reg = Op.getReg(); |
| 5432 | } else { |
| 5433 | AM.BaseType = X86AddressMode::FrameIndexBase; |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 5434 | AM.Base.FrameIndex = Op.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5435 | } |
| 5436 | Op = MI->getOperand(1); |
| 5437 | if (Op.isImmediate()) |
| 5438 | AM.Scale = Op.getImm(); |
| 5439 | Op = MI->getOperand(2); |
| 5440 | if (Op.isImmediate()) |
| 5441 | AM.IndexReg = Op.getImm(); |
| 5442 | Op = MI->getOperand(3); |
| 5443 | if (Op.isGlobalAddress()) { |
| 5444 | AM.GV = Op.getGlobal(); |
| 5445 | } else { |
| 5446 | AM.Disp = Op.getImm(); |
| 5447 | } |
| 5448 | addFullAddress(BuildMI(BB, TII->get(Opc)), AM) |
| 5449 | .addReg(MI->getOperand(4).getReg()); |
| 5450 | |
| 5451 | // Reload the original control word now. |
| 5452 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
| 5453 | |
| 5454 | delete MI; // The pseudo instruction is gone now. |
| 5455 | return BB; |
| 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | //===----------------------------------------------------------------------===// |
| 5461 | // X86 Optimization Hooks |
| 5462 | //===----------------------------------------------------------------------===// |
| 5463 | |
| 5464 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 5465 | uint64_t Mask, |
| 5466 | uint64_t &KnownZero, |
| 5467 | uint64_t &KnownOne, |
| 5468 | const SelectionDAG &DAG, |
| 5469 | unsigned Depth) const { |
| 5470 | unsigned Opc = Op.getOpcode(); |
| 5471 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 5472 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 5473 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 5474 | Opc == ISD::INTRINSIC_VOID) && |
| 5475 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 5476 | " is a target node!"); |
| 5477 | |
| 5478 | KnownZero = KnownOne = 0; // Don't know anything. |
| 5479 | switch (Opc) { |
| 5480 | default: break; |
| 5481 | case X86ISD::SETCC: |
| 5482 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 5483 | break; |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | /// getShuffleScalarElt - Returns the scalar element that will make up the ith |
| 5488 | /// element of the result of the vector shuffle. |
| 5489 | static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) { |
| 5490 | MVT::ValueType VT = N->getValueType(0); |
| 5491 | SDOperand PermMask = N->getOperand(2); |
| 5492 | unsigned NumElems = PermMask.getNumOperands(); |
| 5493 | SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1); |
| 5494 | i %= NumElems; |
| 5495 | if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 5496 | return (i == 0) |
Arnold Schwaighofer | e2d6bbb | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 5497 | ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5498 | } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) { |
| 5499 | SDOperand Idx = PermMask.getOperand(i); |
| 5500 | if (Idx.getOpcode() == ISD::UNDEF) |
| 5501 | return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT)); |
| 5502 | return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG); |
| 5503 | } |
| 5504 | return SDOperand(); |
| 5505 | } |
| 5506 | |
| 5507 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
| 5508 | /// node is a GlobalAddress + an offset. |
| 5509 | static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) { |
| 5510 | unsigned Opc = N->getOpcode(); |
| 5511 | if (Opc == X86ISD::Wrapper) { |
| 5512 | if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) { |
| 5513 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
| 5514 | return true; |
| 5515 | } |
| 5516 | } else if (Opc == ISD::ADD) { |
| 5517 | SDOperand N1 = N->getOperand(0); |
| 5518 | SDOperand N2 = N->getOperand(1); |
| 5519 | if (isGAPlusOffset(N1.Val, GA, Offset)) { |
| 5520 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2); |
| 5521 | if (V) { |
| 5522 | Offset += V->getSignExtended(); |
| 5523 | return true; |
| 5524 | } |
| 5525 | } else if (isGAPlusOffset(N2.Val, GA, Offset)) { |
| 5526 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1); |
| 5527 | if (V) { |
| 5528 | Offset += V->getSignExtended(); |
| 5529 | return true; |
| 5530 | } |
| 5531 | } |
| 5532 | } |
| 5533 | return false; |
| 5534 | } |
| 5535 | |
| 5536 | /// isConsecutiveLoad - Returns true if N is loading from an address of Base |
| 5537 | /// + Dist * Size. |
| 5538 | static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size, |
| 5539 | MachineFrameInfo *MFI) { |
| 5540 | if (N->getOperand(0).Val != Base->getOperand(0).Val) |
| 5541 | return false; |
| 5542 | |
| 5543 | SDOperand Loc = N->getOperand(1); |
| 5544 | SDOperand BaseLoc = Base->getOperand(1); |
| 5545 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 5546 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 5547 | return false; |
Dan Gohman | 53491e9 | 2007-07-23 20:24:29 +0000 | [diff] [blame] | 5548 | int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 5549 | int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5550 | int FS = MFI->getObjectSize(FI); |
| 5551 | int BFS = MFI->getObjectSize(BFI); |
| 5552 | if (FS != BFS || FS != Size) return false; |
| 5553 | return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size); |
| 5554 | } else { |
| 5555 | GlobalValue *GV1 = NULL; |
| 5556 | GlobalValue *GV2 = NULL; |
| 5557 | int64_t Offset1 = 0; |
| 5558 | int64_t Offset2 = 0; |
| 5559 | bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1); |
| 5560 | bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2); |
| 5561 | if (isGA1 && isGA2 && GV1 == GV2) |
| 5562 | return Offset1 == (Offset2 + Dist*Size); |
| 5563 | } |
| 5564 | |
| 5565 | return false; |
| 5566 | } |
| 5567 | |
| 5568 | static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI, |
| 5569 | const X86Subtarget *Subtarget) { |
| 5570 | GlobalValue *GV; |
| 5571 | int64_t Offset; |
| 5572 | if (isGAPlusOffset(Base, GV, Offset)) |
| 5573 | return (GV->getAlignment() >= 16 && (Offset % 16) == 0); |
| 5574 | else { |
| 5575 | assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!"); |
Dan Gohman | 53491e9 | 2007-07-23 20:24:29 +0000 | [diff] [blame] | 5576 | int BFI = cast<FrameIndexSDNode>(Base)->getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5577 | if (BFI < 0) |
| 5578 | // Fixed objects do not specify alignment, however the offsets are known. |
| 5579 | return ((Subtarget->getStackAlignment() % 16) == 0 && |
| 5580 | (MFI->getObjectOffset(BFI) % 16) == 0); |
| 5581 | else |
| 5582 | return MFI->getObjectAlignment(BFI) >= 16; |
| 5583 | } |
| 5584 | return false; |
| 5585 | } |
| 5586 | |
| 5587 | |
| 5588 | /// PerformShuffleCombine - Combine a vector_shuffle that is equal to |
| 5589 | /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load |
| 5590 | /// if the load addresses are consecutive, non-overlapping, and in the right |
| 5591 | /// order. |
| 5592 | static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
| 5593 | const X86Subtarget *Subtarget) { |
| 5594 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5595 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 5596 | MVT::ValueType VT = N->getValueType(0); |
| 5597 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
| 5598 | SDOperand PermMask = N->getOperand(2); |
| 5599 | int NumElems = (int)PermMask.getNumOperands(); |
| 5600 | SDNode *Base = NULL; |
| 5601 | for (int i = 0; i < NumElems; ++i) { |
| 5602 | SDOperand Idx = PermMask.getOperand(i); |
| 5603 | if (Idx.getOpcode() == ISD::UNDEF) { |
| 5604 | if (!Base) return SDOperand(); |
| 5605 | } else { |
| 5606 | SDOperand Arg = |
| 5607 | getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG); |
| 5608 | if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val)) |
| 5609 | return SDOperand(); |
| 5610 | if (!Base) |
| 5611 | Base = Arg.Val; |
| 5612 | else if (!isConsecutiveLoad(Arg.Val, Base, |
| 5613 | i, MVT::getSizeInBits(EVT)/8,MFI)) |
| 5614 | return SDOperand(); |
| 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget); |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5619 | LoadSDNode *LD = cast<LoadSDNode>(Base); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5620 | if (isAlign16) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5621 | return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5622 | LD->getSrcValueOffset(), LD->isVolatile()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5623 | } else { |
Dan Gohman | 1182170 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5624 | return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), |
| 5625 | LD->getSrcValueOffset(), LD->isVolatile(), |
| 5626 | LD->getAlignment()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. |
| 5631 | static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
| 5632 | const X86Subtarget *Subtarget) { |
| 5633 | SDOperand Cond = N->getOperand(0); |
| 5634 | |
| 5635 | // If we have SSE[12] support, try to form min/max nodes. |
| 5636 | if (Subtarget->hasSSE2() && |
| 5637 | (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) { |
| 5638 | if (Cond.getOpcode() == ISD::SETCC) { |
| 5639 | // Get the LHS/RHS of the select. |
| 5640 | SDOperand LHS = N->getOperand(1); |
| 5641 | SDOperand RHS = N->getOperand(2); |
| 5642 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| 5643 | |
| 5644 | unsigned Opcode = 0; |
| 5645 | if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { |
| 5646 | switch (CC) { |
| 5647 | default: break; |
| 5648 | case ISD::SETOLE: // (X <= Y) ? X : Y -> min |
| 5649 | case ISD::SETULE: |
| 5650 | case ISD::SETLE: |
| 5651 | if (!UnsafeFPMath) break; |
| 5652 | // FALL THROUGH. |
| 5653 | case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min |
| 5654 | case ISD::SETLT: |
| 5655 | Opcode = X86ISD::FMIN; |
| 5656 | break; |
| 5657 | |
| 5658 | case ISD::SETOGT: // (X > Y) ? X : Y -> max |
| 5659 | case ISD::SETUGT: |
| 5660 | case ISD::SETGT: |
| 5661 | if (!UnsafeFPMath) break; |
| 5662 | // FALL THROUGH. |
| 5663 | case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max |
| 5664 | case ISD::SETGE: |
| 5665 | Opcode = X86ISD::FMAX; |
| 5666 | break; |
| 5667 | } |
| 5668 | } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { |
| 5669 | switch (CC) { |
| 5670 | default: break; |
| 5671 | case ISD::SETOGT: // (X > Y) ? Y : X -> min |
| 5672 | case ISD::SETUGT: |
| 5673 | case ISD::SETGT: |
| 5674 | if (!UnsafeFPMath) break; |
| 5675 | // FALL THROUGH. |
| 5676 | case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min |
| 5677 | case ISD::SETGE: |
| 5678 | Opcode = X86ISD::FMIN; |
| 5679 | break; |
| 5680 | |
| 5681 | case ISD::SETOLE: // (X <= Y) ? Y : X -> max |
| 5682 | case ISD::SETULE: |
| 5683 | case ISD::SETLE: |
| 5684 | if (!UnsafeFPMath) break; |
| 5685 | // FALL THROUGH. |
| 5686 | case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max |
| 5687 | case ISD::SETLT: |
| 5688 | Opcode = X86ISD::FMAX; |
| 5689 | break; |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | if (Opcode) |
| 5694 | return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS); |
| 5695 | } |
| 5696 | |
| 5697 | } |
| 5698 | |
| 5699 | return SDOperand(); |
| 5700 | } |
| 5701 | |
| 5702 | |
| 5703 | SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N, |
| 5704 | DAGCombinerInfo &DCI) const { |
| 5705 | SelectionDAG &DAG = DCI.DAG; |
| 5706 | switch (N->getOpcode()) { |
| 5707 | default: break; |
| 5708 | case ISD::VECTOR_SHUFFLE: |
| 5709 | return PerformShuffleCombine(N, DAG, Subtarget); |
| 5710 | case ISD::SELECT: |
| 5711 | return PerformSELECTCombine(N, DAG, Subtarget); |
| 5712 | } |
| 5713 | |
| 5714 | return SDOperand(); |
| 5715 | } |
| 5716 | |
| 5717 | //===----------------------------------------------------------------------===// |
| 5718 | // X86 Inline Assembly Support |
| 5719 | //===----------------------------------------------------------------------===// |
| 5720 | |
| 5721 | /// getConstraintType - Given a constraint letter, return the type of |
| 5722 | /// constraint it is for this target. |
| 5723 | X86TargetLowering::ConstraintType |
| 5724 | X86TargetLowering::getConstraintType(const std::string &Constraint) const { |
| 5725 | if (Constraint.size() == 1) { |
| 5726 | switch (Constraint[0]) { |
| 5727 | case 'A': |
| 5728 | case 'r': |
| 5729 | case 'R': |
| 5730 | case 'l': |
| 5731 | case 'q': |
| 5732 | case 'Q': |
| 5733 | case 'x': |
| 5734 | case 'Y': |
| 5735 | return C_RegisterClass; |
| 5736 | default: |
| 5737 | break; |
| 5738 | } |
| 5739 | } |
| 5740 | return TargetLowering::getConstraintType(Constraint); |
| 5741 | } |
| 5742 | |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5743 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 5744 | /// vector. If it is invalid, don't add anything to Ops. |
| 5745 | void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op, |
| 5746 | char Constraint, |
| 5747 | std::vector<SDOperand>&Ops, |
| 5748 | SelectionDAG &DAG) { |
| 5749 | SDOperand Result(0, 0); |
| 5750 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5751 | switch (Constraint) { |
| 5752 | default: break; |
| 5753 | case 'I': |
| 5754 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5755 | if (C->getValue() <= 31) { |
| 5756 | Result = DAG.getTargetConstant(C->getValue(), Op.getValueType()); |
| 5757 | break; |
| 5758 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5759 | } |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5760 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5761 | case 'N': |
| 5762 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5763 | if (C->getValue() <= 255) { |
| 5764 | Result = DAG.getTargetConstant(C->getValue(), Op.getValueType()); |
| 5765 | break; |
| 5766 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5767 | } |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5768 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5769 | case 'i': { |
| 5770 | // Literal immediates are always ok. |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5771 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { |
| 5772 | Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType()); |
| 5773 | break; |
| 5774 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5775 | |
| 5776 | // If we are in non-pic codegen mode, we allow the address of a global (with |
| 5777 | // an optional displacement) to be used with 'i'. |
| 5778 | GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); |
| 5779 | int64_t Offset = 0; |
| 5780 | |
| 5781 | // Match either (GA) or (GA+C) |
| 5782 | if (GA) { |
| 5783 | Offset = GA->getOffset(); |
| 5784 | } else if (Op.getOpcode() == ISD::ADD) { |
| 5785 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5786 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 5787 | if (C && GA) { |
| 5788 | Offset = GA->getOffset()+C->getValue(); |
| 5789 | } else { |
| 5790 | C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5791 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 5792 | if (C && GA) |
| 5793 | Offset = GA->getOffset()+C->getValue(); |
| 5794 | else |
| 5795 | C = 0, GA = 0; |
| 5796 | } |
| 5797 | } |
| 5798 | |
| 5799 | if (GA) { |
| 5800 | // If addressing this global requires a load (e.g. in PIC mode), we can't |
| 5801 | // match. |
| 5802 | if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(), |
| 5803 | false)) |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5804 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5805 | |
| 5806 | Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), |
| 5807 | Offset); |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5808 | Result = Op; |
| 5809 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
| 5812 | // Otherwise, not valid for this mode. |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5813 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5814 | } |
| 5815 | } |
Chris Lattner | a531abc | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5816 | |
| 5817 | if (Result.Val) { |
| 5818 | Ops.push_back(Result); |
| 5819 | return; |
| 5820 | } |
| 5821 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | std::vector<unsigned> X86TargetLowering:: |
| 5825 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 5826 | MVT::ValueType VT) const { |
| 5827 | if (Constraint.size() == 1) { |
| 5828 | // FIXME: not handling fp-stack yet! |
| 5829 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
| 5830 | default: break; // Unknown constraint letter |
| 5831 | case 'A': // EAX/EDX |
| 5832 | if (VT == MVT::i32 || VT == MVT::i64) |
| 5833 | return make_vector<unsigned>(X86::EAX, X86::EDX, 0); |
| 5834 | break; |
| 5835 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 5836 | case 'Q': // Q_REGS |
| 5837 | if (VT == MVT::i32) |
| 5838 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 5839 | else if (VT == MVT::i16) |
| 5840 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 5841 | else if (VT == MVT::i8) |
Evan Cheng | f85c10f | 2007-08-13 23:27:11 +0000 | [diff] [blame] | 5842 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); |
Chris Lattner | 3503259 | 2007-11-04 06:51:12 +0000 | [diff] [blame] | 5843 | else if (VT == MVT::i64) |
| 5844 | return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0); |
| 5845 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | return std::vector<unsigned>(); |
| 5850 | } |
| 5851 | |
| 5852 | std::pair<unsigned, const TargetRegisterClass*> |
| 5853 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
| 5854 | MVT::ValueType VT) const { |
| 5855 | // First, see if this is a constraint that directly corresponds to an LLVM |
| 5856 | // register class. |
| 5857 | if (Constraint.size() == 1) { |
| 5858 | // GCC Constraint Letters |
| 5859 | switch (Constraint[0]) { |
| 5860 | default: break; |
| 5861 | case 'r': // GENERAL_REGS |
| 5862 | case 'R': // LEGACY_REGS |
| 5863 | case 'l': // INDEX_REGS |
| 5864 | if (VT == MVT::i64 && Subtarget->is64Bit()) |
| 5865 | return std::make_pair(0U, X86::GR64RegisterClass); |
| 5866 | if (VT == MVT::i32) |
| 5867 | return std::make_pair(0U, X86::GR32RegisterClass); |
| 5868 | else if (VT == MVT::i16) |
| 5869 | return std::make_pair(0U, X86::GR16RegisterClass); |
| 5870 | else if (VT == MVT::i8) |
| 5871 | return std::make_pair(0U, X86::GR8RegisterClass); |
| 5872 | break; |
| 5873 | case 'y': // MMX_REGS if MMX allowed. |
| 5874 | if (!Subtarget->hasMMX()) break; |
| 5875 | return std::make_pair(0U, X86::VR64RegisterClass); |
| 5876 | break; |
| 5877 | case 'Y': // SSE_REGS if SSE2 allowed |
| 5878 | if (!Subtarget->hasSSE2()) break; |
| 5879 | // FALL THROUGH. |
| 5880 | case 'x': // SSE_REGS if SSE1 allowed |
| 5881 | if (!Subtarget->hasSSE1()) break; |
| 5882 | |
| 5883 | switch (VT) { |
| 5884 | default: break; |
| 5885 | // Scalar SSE types. |
| 5886 | case MVT::f32: |
| 5887 | case MVT::i32: |
| 5888 | return std::make_pair(0U, X86::FR32RegisterClass); |
| 5889 | case MVT::f64: |
| 5890 | case MVT::i64: |
| 5891 | return std::make_pair(0U, X86::FR64RegisterClass); |
| 5892 | // Vector types. |
| 5893 | case MVT::v16i8: |
| 5894 | case MVT::v8i16: |
| 5895 | case MVT::v4i32: |
| 5896 | case MVT::v2i64: |
| 5897 | case MVT::v4f32: |
| 5898 | case MVT::v2f64: |
| 5899 | return std::make_pair(0U, X86::VR128RegisterClass); |
| 5900 | } |
| 5901 | break; |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | // Use the default implementation in TargetLowering to convert the register |
| 5906 | // constraint into a member of a register class. |
| 5907 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 5908 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 5909 | |
| 5910 | // Not found as a standard register? |
| 5911 | if (Res.second == 0) { |
| 5912 | // GCC calls "st(0)" just plain "st". |
| 5913 | if (StringsEqualNoCase("{st}", Constraint)) { |
| 5914 | Res.first = X86::ST0; |
Chris Lattner | 3cfe51b | 2007-09-24 05:27:37 +0000 | [diff] [blame] | 5915 | Res.second = X86::RFP80RegisterClass; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | return Res; |
| 5919 | } |
| 5920 | |
| 5921 | // Otherwise, check to see if this is a register class of the wrong value |
| 5922 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 5923 | // turn into {ax},{dx}. |
| 5924 | if (Res.second->hasType(VT)) |
| 5925 | return Res; // Correct type already, nothing to do. |
| 5926 | |
| 5927 | // All of the single-register GCC register classes map their values onto |
| 5928 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 5929 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 5930 | // class and return the appropriate register. |
| 5931 | if (Res.second != X86::GR16RegisterClass) |
| 5932 | return Res; |
| 5933 | |
| 5934 | if (VT == MVT::i8) { |
| 5935 | unsigned DestReg = 0; |
| 5936 | switch (Res.first) { |
| 5937 | default: break; |
| 5938 | case X86::AX: DestReg = X86::AL; break; |
| 5939 | case X86::DX: DestReg = X86::DL; break; |
| 5940 | case X86::CX: DestReg = X86::CL; break; |
| 5941 | case X86::BX: DestReg = X86::BL; break; |
| 5942 | } |
| 5943 | if (DestReg) { |
| 5944 | Res.first = DestReg; |
| 5945 | Res.second = Res.second = X86::GR8RegisterClass; |
| 5946 | } |
| 5947 | } else if (VT == MVT::i32) { |
| 5948 | unsigned DestReg = 0; |
| 5949 | switch (Res.first) { |
| 5950 | default: break; |
| 5951 | case X86::AX: DestReg = X86::EAX; break; |
| 5952 | case X86::DX: DestReg = X86::EDX; break; |
| 5953 | case X86::CX: DestReg = X86::ECX; break; |
| 5954 | case X86::BX: DestReg = X86::EBX; break; |
| 5955 | case X86::SI: DestReg = X86::ESI; break; |
| 5956 | case X86::DI: DestReg = X86::EDI; break; |
| 5957 | case X86::BP: DestReg = X86::EBP; break; |
| 5958 | case X86::SP: DestReg = X86::ESP; break; |
| 5959 | } |
| 5960 | if (DestReg) { |
| 5961 | Res.first = DestReg; |
| 5962 | Res.second = Res.second = X86::GR32RegisterClass; |
| 5963 | } |
| 5964 | } else if (VT == MVT::i64) { |
| 5965 | unsigned DestReg = 0; |
| 5966 | switch (Res.first) { |
| 5967 | default: break; |
| 5968 | case X86::AX: DestReg = X86::RAX; break; |
| 5969 | case X86::DX: DestReg = X86::RDX; break; |
| 5970 | case X86::CX: DestReg = X86::RCX; break; |
| 5971 | case X86::BX: DestReg = X86::RBX; break; |
| 5972 | case X86::SI: DestReg = X86::RSI; break; |
| 5973 | case X86::DI: DestReg = X86::RDI; break; |
| 5974 | case X86::BP: DestReg = X86::RBP; break; |
| 5975 | case X86::SP: DestReg = X86::RSP; break; |
| 5976 | } |
| 5977 | if (DestReg) { |
| 5978 | Res.first = DestReg; |
| 5979 | Res.second = Res.second = X86::GR64RegisterClass; |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | return Res; |
| 5984 | } |