Arnold Schwaighofer | 92226dd | 2007-10-12 21:53:12 +0000 | [diff] [blame] | 1 | //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===// |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +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" |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 16 | #include "X86InstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 17 | #include "X86ISelLowering.h" |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 18 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 19 | #include "X86TargetMachine.h" |
| 20 | #include "llvm/CallingConv.h" |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/BitVector.h" |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/VectorExtras.h" |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/CallingConvLower.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/SelectionDAG.h" |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/StringExtras.h" |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
Mon P Wang | 9f22a4a | 2008-11-24 02:10:43 +0000 | [diff] [blame] | 45 | DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX")); |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 46 | |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 47 | // Forward declarations. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 48 | static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 49 | SDValue V2); |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 50 | |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 51 | X86TargetLowering::X86TargetLowering(X86TargetMachine &TM) |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 52 | : TargetLowering(TM) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 53 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 54 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 55 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 56 | X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 57 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 58 | bool Fast = false; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 59 | |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 60 | RegInfo = TM.getRegisterInfo(); |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 61 | TD = getTargetData(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 62 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 63 | // Set up the TargetLowering object. |
| 64 | |
| 65 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 66 | setShiftAmountType(MVT::i8); |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 67 | setBooleanContents(ZeroOrOneBooleanContent); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 68 | setSchedulingPreference(SchedulingForRegPressure); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 69 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 70 | setStackPointerRegisterToSaveRestore(X86StackPtr); |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 71 | |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 72 | if (Subtarget->isTargetDarwin()) { |
Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 73 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 74 | setUseUnderscoreSetJmp(false); |
| 75 | setUseUnderscoreLongJmp(false); |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 76 | } else if (Subtarget->isTargetMingw()) { |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 77 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 78 | setUseUnderscoreSetJmp(true); |
| 79 | setUseUnderscoreLongJmp(false); |
| 80 | } else { |
| 81 | setUseUnderscoreSetJmp(true); |
| 82 | setUseUnderscoreLongJmp(true); |
| 83 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 84 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 85 | // Set up the register classes. |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 86 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 87 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 88 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 89 | if (Subtarget->is64Bit()) |
| 90 | addRegisterClass(MVT::i64, X86::GR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 91 | |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 92 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 93 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 94 | // We don't accept any truncstore of integer registers. |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 95 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
| 96 | setTruncStoreAction(MVT::i64, MVT::i16, Expand); |
| 97 | setTruncStoreAction(MVT::i64, MVT::i8 , Expand); |
| 98 | setTruncStoreAction(MVT::i32, MVT::i16, Expand); |
| 99 | setTruncStoreAction(MVT::i32, MVT::i8 , Expand); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 100 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); |
| 101 | |
| 102 | // SETOEQ and SETUNE require checking two conditions. |
| 103 | setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand); |
| 104 | setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand); |
| 105 | setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand); |
| 106 | setCondCodeAction(ISD::SETUNE, MVT::f32, Expand); |
| 107 | setCondCodeAction(ISD::SETUNE, MVT::f64, Expand); |
| 108 | setCondCodeAction(ISD::SETUNE, MVT::f80, Expand); |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 109 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 110 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 111 | // operation. |
| 112 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 113 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 114 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 115 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 116 | if (Subtarget->is64Bit()) { |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 117 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 119 | } else { |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 120 | if (!UseSoftFloat && !NoImplicitFloat && X86ScalarSSEf64) { |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 121 | // We have an impenetrably clever algorithm for ui64->double only. |
| 122 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 123 | |
| 124 | // We have faster algorithm for ui32->single only. |
| 125 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 126 | } else { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 127 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 128 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 130 | |
| 131 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 132 | // this operation. |
| 133 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 134 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 135 | |
| 136 | if (!UseSoftFloat && !NoImplicitFloat) { |
| 137 | // SSE has no i16 to fp conversion, only i32 |
| 138 | if (X86ScalarSSEf32) { |
| 139 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| 140 | // f32 and f64 cases are Legal, f80 case is not |
| 141 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 142 | } else { |
| 143 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 144 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 145 | } |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 146 | } else { |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 147 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| 148 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 150 | |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 151 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 |
| 152 | // are Legal, f80 is custom lowered. |
| 153 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 154 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 155 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 156 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 157 | // this operation. |
| 158 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 159 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 160 | |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 161 | if (X86ScalarSSEf32) { |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 162 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 163 | // f32 and f64 cases are Legal, f80 case is not |
| 164 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 165 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 166 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 167 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 171 | // conversion. |
| 172 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 173 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 174 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 175 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 176 | if (Subtarget->is64Bit()) { |
| 177 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 178 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 179 | } else { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 180 | if (X86ScalarSSEf32 && !Subtarget->hasSSE3()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 181 | // Expand FP_TO_UINT into a select. |
| 182 | // FIXME: We would like to use a Custom expander here eventually to do |
| 183 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 184 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 185 | else |
| 186 | // With SSE3 we can use fisttpll to convert to a signed i64. |
| 187 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 188 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 399610a | 2006-12-05 18:22:22 +0000 | [diff] [blame] | 190 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 191 | if (!X86ScalarSSEf64) { |
Chris Lattner | f3597a1 | 2006-12-05 18:45:06 +0000 | [diff] [blame] | 192 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 193 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
| 194 | } |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 195 | |
Dan Gohman | b00ee21 | 2008-02-18 19:34:53 +0000 | [diff] [blame] | 196 | // Scalar integer divide and remainder are lowered to use operations that |
| 197 | // produce two results, to match the available instructions. This exposes |
| 198 | // the two-result form to trivial CSE, which is able to combine x/y and x%y |
| 199 | // into a single instruction. |
| 200 | // |
| 201 | // Scalar integer multiply-high is also lowered to use two-result |
| 202 | // operations, to match the available instructions. However, plain multiply |
| 203 | // (low) operations are left as Legal, as there are single-result |
| 204 | // instructions for this in x86. Using the two-result multiply instructions |
| 205 | // when both high and low results are needed must be arranged by dagcombine. |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 206 | setOperationAction(ISD::MULHS , MVT::i8 , Expand); |
| 207 | setOperationAction(ISD::MULHU , MVT::i8 , Expand); |
| 208 | setOperationAction(ISD::SDIV , MVT::i8 , Expand); |
| 209 | setOperationAction(ISD::UDIV , MVT::i8 , Expand); |
| 210 | setOperationAction(ISD::SREM , MVT::i8 , Expand); |
| 211 | setOperationAction(ISD::UREM , MVT::i8 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 212 | setOperationAction(ISD::MULHS , MVT::i16 , Expand); |
| 213 | setOperationAction(ISD::MULHU , MVT::i16 , Expand); |
| 214 | setOperationAction(ISD::SDIV , MVT::i16 , Expand); |
| 215 | setOperationAction(ISD::UDIV , MVT::i16 , Expand); |
| 216 | setOperationAction(ISD::SREM , MVT::i16 , Expand); |
| 217 | setOperationAction(ISD::UREM , MVT::i16 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 218 | setOperationAction(ISD::MULHS , MVT::i32 , Expand); |
| 219 | setOperationAction(ISD::MULHU , MVT::i32 , Expand); |
| 220 | setOperationAction(ISD::SDIV , MVT::i32 , Expand); |
| 221 | setOperationAction(ISD::UDIV , MVT::i32 , Expand); |
| 222 | setOperationAction(ISD::SREM , MVT::i32 , Expand); |
| 223 | setOperationAction(ISD::UREM , MVT::i32 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 224 | setOperationAction(ISD::MULHS , MVT::i64 , Expand); |
| 225 | setOperationAction(ISD::MULHU , MVT::i64 , Expand); |
| 226 | setOperationAction(ISD::SDIV , MVT::i64 , Expand); |
| 227 | setOperationAction(ISD::UDIV , MVT::i64 , Expand); |
| 228 | setOperationAction(ISD::SREM , MVT::i64 , Expand); |
| 229 | setOperationAction(ISD::UREM , MVT::i64 , Expand); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 230 | |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 231 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 232 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 233 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 234 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 235 | if (Subtarget->is64Bit()) |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 236 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
| 237 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); |
| 238 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 239 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 240 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
Chris Lattner | d110822 | 2008-03-07 06:36:32 +0000 | [diff] [blame] | 241 | setOperationAction(ISD::FREM , MVT::f32 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 242 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
Chris Lattner | d110822 | 2008-03-07 06:36:32 +0000 | [diff] [blame] | 243 | setOperationAction(ISD::FREM , MVT::f80 , Expand); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 244 | setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 245 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 246 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 247 | setOperationAction(ISD::CTTZ , MVT::i8 , Custom); |
| 248 | setOperationAction(ISD::CTLZ , MVT::i8 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 249 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 250 | setOperationAction(ISD::CTTZ , MVT::i16 , Custom); |
| 251 | setOperationAction(ISD::CTLZ , MVT::i16 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 252 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 253 | setOperationAction(ISD::CTTZ , MVT::i32 , Custom); |
| 254 | setOperationAction(ISD::CTLZ , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 255 | if (Subtarget->is64Bit()) { |
| 256 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 257 | setOperationAction(ISD::CTTZ , MVT::i64 , Custom); |
| 258 | setOperationAction(ISD::CTLZ , MVT::i64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 261 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 262 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 263 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 264 | // These should be promoted to a larger select which is supported. |
| 265 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 266 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 267 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 268 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 269 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 270 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 271 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 272 | setOperationAction(ISD::SELECT , MVT::f80 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 273 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 274 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 275 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 276 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 277 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 278 | setOperationAction(ISD::SETCC , MVT::f80 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 279 | if (Subtarget->is64Bit()) { |
| 280 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| 281 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| 282 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 283 | // X86 ret instruction may pop stack. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 284 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 285 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 286 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 287 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 288 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 289 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 290 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 291 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 292 | if (Subtarget->is64Bit()) |
| 293 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 294 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 295 | if (Subtarget->is64Bit()) { |
| 296 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 297 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 298 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 299 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 300 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 301 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 302 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 303 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 304 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 305 | if (Subtarget->is64Bit()) { |
| 306 | setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom); |
| 307 | setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom); |
| 308 | setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom); |
| 309 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 310 | |
Evan Cheng | d2cde68 | 2008-03-10 19:38:10 +0000 | [diff] [blame] | 311 | if (Subtarget->hasSSE1()) |
| 312 | setOperationAction(ISD::PREFETCH , MVT::Other, Legal); |
Evan Cheng | 27b7db5 | 2008-03-08 00:58:38 +0000 | [diff] [blame] | 313 | |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 314 | if (!Subtarget->hasSSE2()) |
| 315 | setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand); |
| 316 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 317 | // Expand certain atomics |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 318 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom); |
| 319 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom); |
| 320 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); |
| 321 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); |
Bill Wendling | 5bf1b4e | 2008-08-20 00:28:16 +0000 | [diff] [blame] | 322 | |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 323 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom); |
| 324 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom); |
| 325 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); |
| 326 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 327 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 328 | if (!Subtarget->is64Bit()) { |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 329 | setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); |
| 330 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); |
| 331 | setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); |
| 332 | setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); |
| 333 | setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); |
| 334 | setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom); |
| 335 | setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 338 | // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion. |
| 339 | setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 340 | // FIXME - use subtarget debug flags |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 341 | if (!Subtarget->isTargetDarwin() && |
| 342 | !Subtarget->isTargetELF() && |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 343 | !Subtarget->isTargetCygMing()) { |
| 344 | setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand); |
| 345 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| 346 | } |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 347 | |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 348 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); |
| 349 | setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); |
| 350 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 351 | setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); |
| 352 | if (Subtarget->is64Bit()) { |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 353 | setExceptionPointerRegister(X86::RAX); |
| 354 | setExceptionSelectorRegister(X86::RDX); |
| 355 | } else { |
| 356 | setExceptionPointerRegister(X86::EAX); |
| 357 | setExceptionSelectorRegister(X86::EDX); |
| 358 | } |
Anton Korobeynikov | 3825262 | 2007-09-03 00:36:06 +0000 | [diff] [blame] | 359 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 360 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom); |
| 361 | |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 362 | setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 363 | |
Chris Lattner | da68d30 | 2008-01-15 21:58:22 +0000 | [diff] [blame] | 364 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 365 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 366 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 367 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 368 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 369 | if (Subtarget->is64Bit()) { |
| 370 | setOperationAction(ISD::VAARG , MVT::Other, Custom); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 371 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 372 | } else { |
| 373 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 374 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 375 | } |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 376 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 377 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 378 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 379 | if (Subtarget->is64Bit()) |
| 380 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 381 | if (Subtarget->isTargetCygMing()) |
| 382 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); |
| 383 | else |
| 384 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 385 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 386 | if (!UseSoftFloat && X86ScalarSSEf64) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 387 | // f32 and f64 use SSE. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 388 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 389 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 390 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 391 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 392 | // Use ANDPD to simulate FABS. |
| 393 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 394 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 395 | |
| 396 | // Use XORP to simulate FNEG. |
| 397 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 398 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 399 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 400 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| 401 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 402 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 403 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 404 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 405 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 406 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 407 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 408 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 409 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 410 | // Expand FP immediates into loads from the stack, except for the special |
| 411 | // cases we handle. |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 412 | addLegalFPImmediate(APFloat(+0.0)); // xorpd |
| 413 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 414 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 415 | // Floating truncations from f80 and extensions to f80 go through memory. |
| 416 | // If optimizing, we lie about this though and handle it in |
| 417 | // InstructionSelectPreprocess so that dagcombine2 can hack on these. |
| 418 | if (Fast) { |
| 419 | setConvertAction(MVT::f32, MVT::f80, Expand); |
| 420 | setConvertAction(MVT::f64, MVT::f80, Expand); |
| 421 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 422 | setConvertAction(MVT::f80, MVT::f64, Expand); |
| 423 | } |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 424 | } else if (!UseSoftFloat && X86ScalarSSEf32) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 425 | // Use SSE for f32, x87 for f64. |
| 426 | // Set up the FP register classes. |
| 427 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 428 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 429 | |
| 430 | // Use ANDPS to simulate FABS. |
| 431 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 432 | |
| 433 | // Use XORP to simulate FNEG. |
| 434 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 435 | |
| 436 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 437 | |
| 438 | // Use ANDPS and ORPS to simulate FCOPYSIGN. |
| 439 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 440 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 441 | |
| 442 | // We don't support sin/cos/fmod |
| 443 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 444 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 445 | |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 446 | // Special cases we handle for FP constants. |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 447 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
| 448 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 449 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 450 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 451 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
| 452 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 453 | // SSE <-> X87 conversions go through memory. If optimizing, we lie about |
| 454 | // this though and handle it in InstructionSelectPreprocess so that |
| 455 | // dagcombine2 can hack on these. |
| 456 | if (Fast) { |
| 457 | setConvertAction(MVT::f32, MVT::f64, Expand); |
| 458 | setConvertAction(MVT::f32, MVT::f80, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 459 | setConvertAction(MVT::f80, MVT::f32, Expand); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 460 | setConvertAction(MVT::f64, MVT::f32, Expand); |
| 461 | // And x87->x87 truncations also. |
| 462 | setConvertAction(MVT::f80, MVT::f64, Expand); |
| 463 | } |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 464 | |
| 465 | if (!UnsafeFPMath) { |
| 466 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 467 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 468 | } |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 469 | } else if (!UseSoftFloat) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 470 | // f32 and f64 in x87. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 471 | // Set up the FP register classes. |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 472 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 473 | addRegisterClass(MVT::f32, X86::RFP32RegisterClass); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 474 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 475 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 476 | setOperationAction(ISD::UNDEF, MVT::f32, Expand); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 477 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 478 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 479 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 480 | // Floating truncations go through memory. If optimizing, we lie about |
| 481 | // this though and handle it in InstructionSelectPreprocess so that |
| 482 | // dagcombine2 can hack on these. |
| 483 | if (Fast) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 484 | setConvertAction(MVT::f80, MVT::f32, Expand); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 485 | setConvertAction(MVT::f64, MVT::f32, Expand); |
| 486 | setConvertAction(MVT::f80, MVT::f64, Expand); |
| 487 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 488 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 489 | if (!UnsafeFPMath) { |
| 490 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 491 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 492 | } |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 493 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 494 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 495 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 496 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 497 | addLegalFPImmediate(APFloat(+0.0f)); // FLD0 |
| 498 | addLegalFPImmediate(APFloat(+1.0f)); // FLD1 |
| 499 | addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS |
| 500 | addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 501 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 502 | |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 503 | // Long double always uses X87. |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 504 | if (!UseSoftFloat) { |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 505 | addRegisterClass(MVT::f80, X86::RFP80RegisterClass); |
| 506 | setOperationAction(ISD::UNDEF, MVT::f80, Expand); |
| 507 | setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); |
| 508 | { |
| 509 | bool ignored; |
| 510 | APFloat TmpFlt(+0.0); |
| 511 | TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, |
| 512 | &ignored); |
| 513 | addLegalFPImmediate(TmpFlt); // FLD0 |
| 514 | TmpFlt.changeSign(); |
| 515 | addLegalFPImmediate(TmpFlt); // FLD0/FCHS |
| 516 | APFloat TmpFlt2(+1.0); |
| 517 | TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, |
| 518 | &ignored); |
| 519 | addLegalFPImmediate(TmpFlt2); // FLD1 |
| 520 | TmpFlt2.changeSign(); |
| 521 | addLegalFPImmediate(TmpFlt2); // FLD1/FCHS |
| 522 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 523 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 524 | if (!UnsafeFPMath) { |
| 525 | setOperationAction(ISD::FSIN , MVT::f80 , Expand); |
| 526 | setOperationAction(ISD::FCOS , MVT::f80 , Expand); |
| 527 | } |
Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 528 | } |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 529 | |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 530 | // Always use a library call for pow. |
| 531 | setOperationAction(ISD::FPOW , MVT::f32 , Expand); |
| 532 | setOperationAction(ISD::FPOW , MVT::f64 , Expand); |
| 533 | setOperationAction(ISD::FPOW , MVT::f80 , Expand); |
| 534 | |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 535 | setOperationAction(ISD::FLOG, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 536 | setOperationAction(ISD::FLOG2, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 537 | setOperationAction(ISD::FLOG10, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 538 | setOperationAction(ISD::FEXP, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 539 | setOperationAction(ISD::FEXP2, MVT::f80, Expand); |
| 540 | |
Mon P Wang | f007a8b | 2008-11-06 05:31:54 +0000 | [diff] [blame] | 541 | // First set operation action for all vector types to either promote |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 542 | // (for widening) or expand (for scalarization). Then we will selectively |
| 543 | // turn on ones that can be effectively codegen'd. |
Dan Gohman | fa0f77d | 2007-05-18 18:44:07 +0000 | [diff] [blame] | 544 | for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 545 | VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 546 | setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand); |
| 547 | setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand); |
| 548 | setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand); |
| 549 | setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand); |
| 550 | setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand); |
| 551 | setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand); |
| 552 | setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand); |
| 553 | setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand); |
| 554 | setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand); |
| 555 | setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand); |
| 556 | setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand); |
| 557 | setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand); |
| 558 | setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand); |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 559 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand); |
| 560 | setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand); |
| 561 | setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 562 | setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand); |
| 563 | setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand); |
| 564 | setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand); |
| 565 | setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand); |
| 566 | setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand); |
| 567 | setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand); |
| 568 | setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand); |
| 569 | setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand); |
| 570 | setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand); |
| 571 | setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand); |
| 572 | setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand); |
| 573 | setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand); |
| 574 | setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand); |
| 575 | setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand); |
| 576 | setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand); |
| 577 | setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand); |
| 578 | setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand); |
| 579 | setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand); |
| 580 | setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand); |
| 581 | setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand); |
| 582 | setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand); |
| 583 | setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand); |
Dale Johannesen | fb0e132 | 2008-09-10 17:31:40 +0000 | [diff] [blame] | 584 | setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand); |
| 585 | setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand); |
| 586 | setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand); |
| 587 | setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand); |
| 588 | setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 591 | // FIXME: In order to prevent SSE instructions being expanded to MMX ones |
| 592 | // with -msoft-float, disable use of MMX as well. |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 593 | if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 594 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 595 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 596 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 597 | addRegisterClass(MVT::v2f32, X86::VR64RegisterClass); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 598 | addRegisterClass(MVT::v1i64, X86::VR64RegisterClass); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 599 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 600 | setOperationAction(ISD::ADD, MVT::v8i8, Legal); |
| 601 | setOperationAction(ISD::ADD, MVT::v4i16, Legal); |
| 602 | setOperationAction(ISD::ADD, MVT::v2i32, Legal); |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 603 | setOperationAction(ISD::ADD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 604 | |
Bill Wendling | c1fb047 | 2007-03-10 09:57:05 +0000 | [diff] [blame] | 605 | setOperationAction(ISD::SUB, MVT::v8i8, Legal); |
| 606 | setOperationAction(ISD::SUB, MVT::v4i16, Legal); |
| 607 | setOperationAction(ISD::SUB, MVT::v2i32, Legal); |
Dale Johannesen | 8d26e59 | 2007-10-30 01:18:38 +0000 | [diff] [blame] | 608 | setOperationAction(ISD::SUB, MVT::v1i64, Legal); |
Bill Wendling | c1fb047 | 2007-03-10 09:57:05 +0000 | [diff] [blame] | 609 | |
Bill Wendling | 74027e9 | 2007-03-15 21:24:36 +0000 | [diff] [blame] | 610 | setOperationAction(ISD::MULHS, MVT::v4i16, Legal); |
| 611 | setOperationAction(ISD::MUL, MVT::v4i16, Legal); |
| 612 | |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 613 | setOperationAction(ISD::AND, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 614 | AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 615 | setOperationAction(ISD::AND, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 616 | AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64); |
| 617 | setOperationAction(ISD::AND, MVT::v2i32, Promote); |
| 618 | AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64); |
| 619 | setOperationAction(ISD::AND, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 620 | |
| 621 | setOperationAction(ISD::OR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 622 | AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 623 | setOperationAction(ISD::OR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 624 | AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64); |
| 625 | setOperationAction(ISD::OR, MVT::v2i32, Promote); |
| 626 | AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64); |
| 627 | setOperationAction(ISD::OR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 628 | |
| 629 | setOperationAction(ISD::XOR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 630 | AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 631 | setOperationAction(ISD::XOR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 632 | AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64); |
| 633 | setOperationAction(ISD::XOR, MVT::v2i32, Promote); |
| 634 | AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64); |
| 635 | setOperationAction(ISD::XOR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 636 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 637 | setOperationAction(ISD::LOAD, MVT::v8i8, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 638 | AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 639 | setOperationAction(ISD::LOAD, MVT::v4i16, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 640 | AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64); |
| 641 | setOperationAction(ISD::LOAD, MVT::v2i32, Promote); |
| 642 | AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 643 | setOperationAction(ISD::LOAD, MVT::v2f32, Promote); |
| 644 | AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 645 | setOperationAction(ISD::LOAD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 646 | |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 647 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); |
| 648 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); |
| 649 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 650 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom); |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 651 | setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 652 | |
| 653 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); |
| 654 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); |
| 655 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 656 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 657 | |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 658 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom); |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 659 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom); |
| 660 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 661 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom); |
Bill Wendling | 3180e20 | 2008-07-20 02:32:23 +0000 | [diff] [blame] | 662 | |
| 663 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 664 | |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 665 | setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 666 | setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand); |
| 667 | setOperationAction(ISD::SELECT, MVT::v8i8, Promote); |
| 668 | setOperationAction(ISD::SELECT, MVT::v4i16, Promote); |
| 669 | setOperationAction(ISD::SELECT, MVT::v2i32, Promote); |
| 670 | setOperationAction(ISD::SELECT, MVT::v1i64, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 673 | if (!UseSoftFloat && Subtarget->hasSSE1()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 674 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 675 | |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 676 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 677 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 678 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 679 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 680 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 681 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 682 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 683 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 684 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 685 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 686 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 687 | setOperationAction(ISD::VSETCC, MVT::v4f32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 690 | if (!UseSoftFloat && Subtarget->hasSSE2()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 691 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 692 | |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 693 | // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM |
| 694 | // registers cannot be used even for integer operations. |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 695 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 696 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 697 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 698 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 699 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 700 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 701 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 702 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 703 | setOperationAction(ISD::ADD, MVT::v2i64, Legal); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 704 | setOperationAction(ISD::MUL, MVT::v2i64, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 705 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 706 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 707 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 708 | setOperationAction(ISD::SUB, MVT::v2i64, Legal); |
Evan Cheng | f998984 | 2006-04-13 05:10:25 +0000 | [diff] [blame] | 709 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 710 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 711 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 712 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 713 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 714 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 715 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 716 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 717 | setOperationAction(ISD::VSETCC, MVT::v2f64, Custom); |
| 718 | setOperationAction(ISD::VSETCC, MVT::v16i8, Custom); |
| 719 | setOperationAction(ISD::VSETCC, MVT::v8i16, Custom); |
| 720 | setOperationAction(ISD::VSETCC, MVT::v4i32, Custom); |
Nate Begeman | c2616e4 | 2008-05-12 20:34:32 +0000 | [diff] [blame] | 721 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 722 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 723 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 724 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 725 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 726 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 727 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 728 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 729 | for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) { |
| 730 | MVT VT = (MVT::SimpleValueType)i; |
Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 731 | // Do not attempt to custom lower non-power-of-2 vectors |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 732 | if (!isPowerOf2_32(VT.getVectorNumElements())) |
Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 733 | continue; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 734 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 735 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 736 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 737 | } |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 738 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 739 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 740 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 741 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 742 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 743 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 744 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 745 | |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 746 | if (Subtarget->is64Bit()) { |
| 747 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom); |
Dale Johannesen | 25f1d08 | 2007-10-31 00:32:36 +0000 | [diff] [blame] | 748 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 749 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 750 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 751 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 752 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 753 | setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote); |
| 754 | AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64); |
| 755 | setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote); |
| 756 | AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64); |
| 757 | setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote); |
| 758 | AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64); |
| 759 | setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote); |
| 760 | AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64); |
| 761 | setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote); |
| 762 | AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 763 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 764 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 765 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 766 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 767 | // Custom lower v2i64 and v2f64 selects. |
| 768 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 769 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 770 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 771 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 772 | |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 773 | } |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 774 | |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 775 | if (Subtarget->hasSSE41()) { |
| 776 | // FIXME: Do we need to handle scalar-to-vector here? |
| 777 | setOperationAction(ISD::MUL, MVT::v4i32, Legal); |
| 778 | |
| 779 | // i8 and i16 vectors are custom , because the source register and source |
| 780 | // source memory operand types are not the same width. f32 vectors are |
| 781 | // custom since the immediate controlling the insert encodes additional |
| 782 | // information. |
| 783 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom); |
| 784 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 785 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 786 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| 787 | |
| 788 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom); |
| 789 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 790 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom); |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 791 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 792 | |
| 793 | if (Subtarget->is64Bit()) { |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 794 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal); |
| 795 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 798 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 799 | if (Subtarget->hasSSE42()) { |
| 800 | setOperationAction(ISD::VSETCC, MVT::v2i64, Custom); |
| 801 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 802 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 803 | // We want to custom lower some of our intrinsics. |
| 804 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 805 | |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 806 | // Add/Sub/Mul with overflow operations are custom lowered. |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 807 | setOperationAction(ISD::SADDO, MVT::i32, Custom); |
| 808 | setOperationAction(ISD::SADDO, MVT::i64, Custom); |
| 809 | setOperationAction(ISD::UADDO, MVT::i32, Custom); |
| 810 | setOperationAction(ISD::UADDO, MVT::i64, Custom); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 811 | setOperationAction(ISD::SSUBO, MVT::i32, Custom); |
| 812 | setOperationAction(ISD::SSUBO, MVT::i64, Custom); |
| 813 | setOperationAction(ISD::USUBO, MVT::i32, Custom); |
| 814 | setOperationAction(ISD::USUBO, MVT::i64, Custom); |
| 815 | setOperationAction(ISD::SMULO, MVT::i32, Custom); |
| 816 | setOperationAction(ISD::SMULO, MVT::i64, Custom); |
| 817 | setOperationAction(ISD::UMULO, MVT::i32, Custom); |
| 818 | setOperationAction(ISD::UMULO, MVT::i64, Custom); |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 819 | |
Evan Cheng | d54f2d5 | 2009-03-31 19:38:51 +0000 | [diff] [blame] | 820 | if (!Subtarget->is64Bit()) { |
| 821 | // These libcalls are not available in 32-bit. |
| 822 | setLibcallName(RTLIB::SHL_I128, 0); |
| 823 | setLibcallName(RTLIB::SRL_I128, 0); |
| 824 | setLibcallName(RTLIB::SRA_I128, 0); |
| 825 | } |
| 826 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 827 | // We have target-specific dag combine patterns for the following nodes: |
| 828 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 829 | setTargetDAGCombine(ISD::BUILD_VECTOR); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 830 | setTargetDAGCombine(ISD::SELECT); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 831 | setTargetDAGCombine(ISD::SHL); |
| 832 | setTargetDAGCombine(ISD::SRA); |
| 833 | setTargetDAGCombine(ISD::SRL); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 834 | setTargetDAGCombine(ISD::STORE); |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 835 | if (Subtarget->is64Bit()) |
| 836 | setTargetDAGCombine(ISD::MUL); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 837 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 838 | computeRegisterProperties(); |
| 839 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 840 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 841 | // be smaller when we are in optimizing for size mode. |
Dan Gohman | 87060f5 | 2008-06-30 21:00:56 +0000 | [diff] [blame] | 842 | maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores |
| 843 | maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores |
| 844 | maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 845 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 846 | setPrefLoopAlignment(16); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 849 | |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 850 | MVT X86TargetLowering::getSetCCResultType(MVT VT) const { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 851 | return MVT::i8; |
| 852 | } |
| 853 | |
| 854 | |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 855 | /// getMaxByValAlign - Helper for getByValTypeAlignment to determine |
| 856 | /// the desired ByVal argument alignment. |
| 857 | static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) { |
| 858 | if (MaxAlign == 16) |
| 859 | return; |
| 860 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 861 | if (VTy->getBitWidth() == 128) |
| 862 | MaxAlign = 16; |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 863 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 864 | unsigned EltAlign = 0; |
| 865 | getMaxByValAlign(ATy->getElementType(), EltAlign); |
| 866 | if (EltAlign > MaxAlign) |
| 867 | MaxAlign = EltAlign; |
| 868 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 869 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 870 | unsigned EltAlign = 0; |
| 871 | getMaxByValAlign(STy->getElementType(i), EltAlign); |
| 872 | if (EltAlign > MaxAlign) |
| 873 | MaxAlign = EltAlign; |
| 874 | if (MaxAlign == 16) |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 882 | /// function arguments in the caller parameter area. For X86, aggregates |
Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 883 | /// that contain SSE vectors are placed at 16-byte boundaries while the rest |
| 884 | /// are at 4-byte boundaries. |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 885 | unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const { |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 886 | if (Subtarget->is64Bit()) { |
| 887 | // Max of 8 and alignment of type. |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 888 | unsigned TyAlign = TD->getABITypeAlignment(Ty); |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 889 | if (TyAlign > 8) |
| 890 | return TyAlign; |
| 891 | return 8; |
| 892 | } |
| 893 | |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 894 | unsigned Align = 4; |
Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 895 | if (Subtarget->hasSSE1()) |
| 896 | getMaxByValAlign(Ty, Align); |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 897 | return Align; |
| 898 | } |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 899 | |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 900 | /// getOptimalMemOpType - Returns the target specific optimal type for load |
Evan Cheng | 0ef8de3 | 2008-05-15 22:13:02 +0000 | [diff] [blame] | 901 | /// and store operations as a result of memset, memcpy, and memmove |
| 902 | /// lowering. It returns MVT::iAny if SelectionDAG should be responsible for |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 903 | /// determining it. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 904 | MVT |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 905 | X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align, |
| 906 | bool isSrcConst, bool isSrcStr) const { |
Chris Lattner | 4002a1b | 2008-10-28 05:49:35 +0000 | [diff] [blame] | 907 | // FIXME: This turns off use of xmm stores for memset/memcpy on targets like |
| 908 | // linux. This is because the stack realignment code can't handle certain |
| 909 | // cases like PR2962. This should be removed when PR2962 is fixed. |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 910 | if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) { |
Chris Lattner | 4002a1b | 2008-10-28 05:49:35 +0000 | [diff] [blame] | 911 | if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16) |
| 912 | return MVT::v4i32; |
| 913 | if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16) |
| 914 | return MVT::v4f32; |
| 915 | } |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 916 | if (Subtarget->is64Bit() && Size >= 8) |
| 917 | return MVT::i64; |
| 918 | return MVT::i32; |
| 919 | } |
| 920 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 921 | /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC |
| 922 | /// jumptable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 923 | SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table, |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 924 | SelectionDAG &DAG) const { |
| 925 | if (usesGlobalOffsetTable()) |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 926 | return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy()); |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 927 | if (!Subtarget->isPICStyleRIPRel()) |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 928 | // This doesn't have DebugLoc associated with it, but is not really the |
| 929 | // same as a Register. |
| 930 | return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(), |
| 931 | getPointerTy()); |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 932 | return Table; |
| 933 | } |
| 934 | |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 935 | //===----------------------------------------------------------------------===// |
| 936 | // Return Value Calling Convention Implementation |
| 937 | //===----------------------------------------------------------------------===// |
| 938 | |
Chris Lattner | 59ed56b | 2007-02-28 04:55:35 +0000 | [diff] [blame] | 939 | #include "X86GenCallingConv.inc" |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 941 | /// LowerRET - Lower an ISD::RET node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 942 | SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 943 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 944 | assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 946 | SmallVector<CCValAssign, 16> RVLocs; |
| 947 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 948 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
| 949 | CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 950 | CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 952 | // If this is the first return lowered for this function, add the regs to the |
| 953 | // liveout set for the function. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 954 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 955 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 956 | if (RVLocs[i].isRegLoc()) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 957 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 958 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 959 | SDValue Chain = Op.getOperand(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 960 | |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 961 | // Handle tail call return. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 962 | Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 963 | if (Chain.getOpcode() == X86ISD::TAILCALL) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 964 | SDValue TailCall = Chain; |
| 965 | SDValue TargetAddress = TailCall.getOperand(1); |
| 966 | SDValue StackAdjustment = TailCall.getOperand(2); |
Chris Lattner | b4a6eaa | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 967 | assert(((TargetAddress.getOpcode() == ISD::Register && |
Arnold Schwaighofer | 290ae03 | 2008-09-22 14:50:07 +0000 | [diff] [blame] | 968 | (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX || |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 969 | cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) || |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 970 | TargetAddress.getOpcode() == ISD::TargetExternalSymbol || |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 971 | TargetAddress.getOpcode() == ISD::TargetGlobalAddress) && |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 972 | "Expecting an global address, external symbol, or register"); |
Chris Lattner | b4a6eaa | 2008-01-16 05:52:18 +0000 | [diff] [blame] | 973 | assert(StackAdjustment.getOpcode() == ISD::Constant && |
| 974 | "Expecting a const value"); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 975 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 976 | SmallVector<SDValue,8> Operands; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 977 | Operands.push_back(Chain.getOperand(0)); |
| 978 | Operands.push_back(TargetAddress); |
| 979 | Operands.push_back(StackAdjustment); |
| 980 | // Copy registers used by the call. Last operand is a flag so it is not |
| 981 | // copied. |
Arnold Schwaighofer | 448175f | 2007-10-16 09:05:00 +0000 | [diff] [blame] | 982 | for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) { |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 983 | Operands.push_back(Chain.getOperand(i)); |
| 984 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 985 | return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0], |
Arnold Schwaighofer | 448175f | 2007-10-16 09:05:00 +0000 | [diff] [blame] | 986 | Operands.size()); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 987 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 988 | |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 989 | // Regular return. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 990 | SDValue Flag; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 991 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 992 | SmallVector<SDValue, 6> RetOps; |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 993 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) |
| 994 | // Operand #1 = Bytes To Pop |
| 995 | RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 996 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 997 | // Copy the result values into the output registers. |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 998 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 999 | CCValAssign &VA = RVLocs[i]; |
| 1000 | assert(VA.isRegLoc() && "Can only return in registers!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1001 | SDValue ValToCopy = Op.getOperand(i*2+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1003 | // Returns in ST0/ST1 are handled specially: these are pushed as operands to |
| 1004 | // the RET instruction and handled by the FP Stackifier. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1005 | if (VA.getLocReg() == X86::ST0 || |
| 1006 | VA.getLocReg() == X86::ST1) { |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1007 | // If this is a copy from an xmm register to ST(0), use an FPExtend to |
| 1008 | // change the value to the FP stack register class. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1009 | if (isScalarFPTypeInSSEReg(VA.getValVT())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1010 | ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1011 | RetOps.push_back(ValToCopy); |
| 1012 | // Don't emit a copytoreg. |
| 1013 | continue; |
| 1014 | } |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 1015 | |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1016 | // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64 |
| 1017 | // which is returned in RAX / RDX. |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1018 | if (Subtarget->is64Bit()) { |
| 1019 | MVT ValVT = ValToCopy.getValueType(); |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1020 | if (ValVT.isVector() && ValVT.getSizeInBits() == 64) { |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1021 | ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy); |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1022 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) |
| 1023 | ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy); |
| 1024 | } |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1027 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1028 | Flag = Chain.getValue(1); |
| 1029 | } |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1030 | |
| 1031 | // The x86-64 ABI for returning structs by value requires that we copy |
| 1032 | // the sret argument into %rax for the return. We saved the argument into |
| 1033 | // a virtual register in the entry block, so now we copy the value out |
| 1034 | // and into %rax. |
| 1035 | if (Subtarget->is64Bit() && |
| 1036 | DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 1037 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1038 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1039 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| 1040 | if (!Reg) { |
| 1041 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); |
| 1042 | FuncInfo->setSRetReturnReg(Reg); |
| 1043 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1044 | SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy()); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1045 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1046 | Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1047 | Flag = Chain.getValue(1); |
| 1048 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1050 | RetOps[0] = Chain; // Update chain. |
| 1051 | |
| 1052 | // Add the flag if we have it. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1053 | if (Flag.getNode()) |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1054 | RetOps.push_back(Flag); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1055 | |
| 1056 | return DAG.getNode(X86ISD::RET_FLAG, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1057 | MVT::Other, &RetOps[0], RetOps.size()); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1061 | /// LowerCallResult - Lower the result values of an ISD::CALL into the |
| 1062 | /// appropriate copies out of appropriate physical registers. This assumes that |
| 1063 | /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call |
| 1064 | /// being lowered. The returns a SDNode with the same number of values as the |
| 1065 | /// ISD::CALL. |
| 1066 | SDNode *X86TargetLowering:: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1067 | LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall, |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1068 | unsigned CallingConv, SelectionDAG &DAG) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1069 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1070 | DebugLoc dl = TheCall->getDebugLoc(); |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 1071 | // Assign locations to each value returned by this call. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1072 | SmallVector<CCValAssign, 16> RVLocs; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1073 | bool isVarArg = TheCall->isVarArg(); |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1074 | bool Is64Bit = Subtarget->is64Bit(); |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1075 | CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs); |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 1076 | CCInfo.AnalyzeCallResult(TheCall, RetCC_X86); |
| 1077 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1078 | SmallVector<SDValue, 8> ResultVals; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1080 | // Copy all of the result registers out of their specified physreg. |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1081 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1082 | CCValAssign &VA = RVLocs[i]; |
| 1083 | MVT CopyVT = VA.getValVT(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1084 | |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1085 | // If this is x86-64, and we disabled SSE, we can't return FP values |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1086 | if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) && |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1087 | ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) { |
| 1088 | cerr << "SSE register return with SSE disabled\n"; |
| 1089 | exit(1); |
| 1090 | } |
| 1091 | |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1092 | // If this is a call to a function that returns an fp value on the floating |
| 1093 | // point stack, but where we prefer to use the value in xmm registers, copy |
| 1094 | // it out as F80 and use a truncate to move it from fp stack reg to xmm reg. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1095 | if ((VA.getLocReg() == X86::ST0 || |
| 1096 | VA.getLocReg() == X86::ST1) && |
| 1097 | isScalarFPTypeInSSEReg(VA.getValVT())) { |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1098 | CopyVT = MVT::f80; |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1099 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1100 | |
Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1101 | SDValue Val; |
| 1102 | if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) { |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1103 | // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64. |
| 1104 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) { |
| 1105 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1106 | MVT::v2i64, InFlag).getValue(1); |
| 1107 | Val = Chain.getValue(0); |
| 1108 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, |
| 1109 | Val, DAG.getConstant(0, MVT::i64)); |
| 1110 | } else { |
| 1111 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1112 | MVT::i64, InFlag).getValue(1); |
| 1113 | Val = Chain.getValue(0); |
| 1114 | } |
Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1115 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val); |
| 1116 | } else { |
| 1117 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1118 | CopyVT, InFlag).getValue(1); |
| 1119 | Val = Chain.getValue(0); |
| 1120 | } |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1121 | InFlag = Chain.getValue(2); |
Chris Lattner | 112dedc | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 1122 | |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1123 | if (CopyVT != VA.getValVT()) { |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1124 | // Round the F80 the right size, which also moves to the appropriate xmm |
| 1125 | // register. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1126 | Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val, |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1127 | // This truncation won't change the value. |
| 1128 | DAG.getIntPtrConstant(1)); |
| 1129 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1131 | ResultVals.push_back(Val); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1132 | } |
Duncan Sands | 4bdcb61 | 2008-07-02 17:40:58 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1134 | // Merge everything together with a MERGE_VALUES node. |
| 1135 | ResultVals.push_back(Chain); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1136 | return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(), |
| 1137 | &ResultVals[0], ResultVals.size()).getNode(); |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1141 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1142 | // C & StdCall & Fast Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1143 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1144 | // StdCall calling convention seems to be standard for many Windows' API |
| 1145 | // routines and around. It differs from C calling convention just a little: |
| 1146 | // callee should clean up the stack, not caller. Symbols should be also |
| 1147 | // decorated in some fancy way :) It doesn't support any vector arguments. |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1148 | // For info on fast calling convention see Fast Calling Convention (tail call) |
| 1149 | // implementation LowerX86_32FastCCCallTo. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1150 | |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1151 | /// CallIsStructReturn - Determines whether a CALL node uses struct return |
| 1152 | /// semantics. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1153 | static bool CallIsStructReturn(CallSDNode *TheCall) { |
| 1154 | unsigned NumOps = TheCall->getNumArgs(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1155 | if (!NumOps) |
| 1156 | return false; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1157 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1158 | return TheCall->getArgFlags(0).isSRet(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1161 | /// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct |
| 1162 | /// return semantics. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1163 | static bool ArgsAreStructReturn(SDValue Op) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1164 | unsigned NumArgs = Op.getNode()->getNumValues() - 1; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1165 | if (!NumArgs) |
| 1166 | return false; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1167 | |
| 1168 | return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1171 | /// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires |
| 1172 | /// the callee to pop its own arguments. Callee pop is necessary to support tail |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1173 | /// calls. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1174 | bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1175 | if (IsVarArg) |
| 1176 | return false; |
| 1177 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1178 | switch (CallingConv) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1179 | default: |
| 1180 | return false; |
| 1181 | case CallingConv::X86_StdCall: |
| 1182 | return !Subtarget->is64Bit(); |
| 1183 | case CallingConv::X86_FastCall: |
| 1184 | return !Subtarget->is64Bit(); |
| 1185 | case CallingConv::Fast: |
| 1186 | return PerformTailCallOpt; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1190 | /// CCAssignFnForNode - Selects the correct CCAssignFn for a the |
| 1191 | /// given CallingConvention value. |
| 1192 | CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const { |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 1193 | if (Subtarget->is64Bit()) { |
Anton Korobeynikov | 1a979d9 | 2008-03-22 20:57:27 +0000 | [diff] [blame] | 1194 | if (Subtarget->isTargetWin64()) |
Anton Korobeynikov | 8f88cb0 | 2008-03-22 20:37:30 +0000 | [diff] [blame] | 1195 | return CC_X86_Win64_C; |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1196 | else if (CC == CallingConv::Fast && PerformTailCallOpt) |
| 1197 | return CC_X86_64_TailCall; |
| 1198 | else |
| 1199 | return CC_X86_64_C; |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1202 | if (CC == CallingConv::X86_FastCall) |
| 1203 | return CC_X86_32_FastCall; |
Evan Cheng | b188dd9 | 2008-09-10 18:25:29 +0000 | [diff] [blame] | 1204 | else if (CC == CallingConv::Fast) |
| 1205 | return CC_X86_32_FastCC; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1206 | else |
| 1207 | return CC_X86_32_C; |
| 1208 | } |
| 1209 | |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1210 | /// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to |
| 1211 | /// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1212 | NameDecorationStyle |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1213 | X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1214 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1215 | if (CC == CallingConv::X86_FastCall) |
| 1216 | return FastCall; |
| 1217 | else if (CC == CallingConv::X86_StdCall) |
| 1218 | return StdCall; |
| 1219 | return None; |
| 1220 | } |
| 1221 | |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1222 | |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1223 | /// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer |
| 1224 | /// in a register before calling. |
| 1225 | bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) { |
| 1226 | return !IsTailCall && !Is64Bit && |
| 1227 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1228 | Subtarget->isPICStyleGOT(); |
| 1229 | } |
| 1230 | |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1231 | /// CallRequiresFnAddressInReg - Check whether the call requires the function |
| 1232 | /// address to be loaded in a register. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1233 | bool |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1234 | X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1235 | return !Is64Bit && IsTailCall && |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1236 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1237 | Subtarget->isPICStyleGOT(); |
| 1238 | } |
| 1239 | |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1240 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
| 1241 | /// by "Src" to address "Dst" with size and alignment information specified by |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1242 | /// the specific parameter attribute. The copy will be passed as a byval |
| 1243 | /// function parameter. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1244 | static SDValue |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1245 | CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1246 | ISD::ArgFlagsTy Flags, SelectionDAG &DAG, |
| 1247 | DebugLoc dl) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1248 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1249 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1250 | /*AlwaysInline=*/true, NULL, 0, NULL, 0); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1253 | SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG, |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1254 | const CCValAssign &VA, |
| 1255 | MachineFrameInfo *MFI, |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1256 | unsigned CC, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1257 | SDValue Root, unsigned i) { |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1258 | // Create the nodes corresponding to a load from this parameter slot. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1259 | ISD::ArgFlagsTy Flags = |
| 1260 | cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags(); |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1261 | bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1262 | bool isImmutable = !AlwaysUseMutable && !Flags.isByVal(); |
Evan Cheng | e70bb59 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1263 | |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1264 | // FIXME: For now, all byval parameter objects are marked mutable. This can be |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1265 | // changed with more analysis. |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1266 | // In case of tail call optimization mark all arguments mutable. Since they |
| 1267 | // could be overwritten by lowering of arguments in case of a tail call. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1268 | int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1269 | VA.getLocMemOffset(), isImmutable); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1270 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1271 | if (Flags.isByVal()) |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1272 | return FIN; |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1273 | return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1274 | PseudoSourceValue::getFixedStack(FI), 0); |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1277 | SDValue |
| 1278 | X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1279 | MachineFunction &MF = DAG.getMachineFunction(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1280 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1281 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1282 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1283 | const Function* Fn = MF.getFunction(); |
| 1284 | if (Fn->hasExternalLinkage() && |
| 1285 | Subtarget->isTargetCygMing() && |
| 1286 | Fn->getName() == "main") |
| 1287 | FuncInfo->setForceFramePointer(true); |
| 1288 | |
| 1289 | // Decorate the function name. |
| 1290 | FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1291 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1292 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1293 | SDValue Root = Op.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1294 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1295 | unsigned CC = MF.getFunction()->getCallingConv(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1296 | bool Is64Bit = Subtarget->is64Bit(); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1297 | bool IsWin64 = Subtarget->isTargetWin64(); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1298 | |
| 1299 | assert(!(isVarArg && CC == CallingConv::Fast) && |
| 1300 | "Var args not supported with calling convention fastcc"); |
| 1301 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1302 | // Assign locations to all of the incoming arguments. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1303 | SmallVector<CCValAssign, 16> ArgLocs; |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1304 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1305 | CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1306 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1307 | SmallVector<SDValue, 8> ArgValues; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1308 | unsigned LastVal = ~0U; |
| 1309 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1310 | CCValAssign &VA = ArgLocs[i]; |
| 1311 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 1312 | // places. |
| 1313 | assert(VA.getValNo() != LastVal && |
| 1314 | "Don't support value assigned to multiple locs yet"); |
| 1315 | LastVal = VA.getValNo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1317 | if (VA.isRegLoc()) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1318 | MVT RegVT = VA.getLocVT(); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 1319 | TargetRegisterClass *RC = NULL; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1320 | if (RegVT == MVT::i32) |
| 1321 | RC = X86::GR32RegisterClass; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1322 | else if (Is64Bit && RegVT == MVT::i64) |
| 1323 | RC = X86::GR64RegisterClass; |
Dale Johannesen | e672af1 | 2008-02-05 20:46:33 +0000 | [diff] [blame] | 1324 | else if (RegVT == MVT::f32) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1325 | RC = X86::FR32RegisterClass; |
Dale Johannesen | e672af1 | 2008-02-05 20:46:33 +0000 | [diff] [blame] | 1326 | else if (RegVT == MVT::f64) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1327 | RC = X86::FR64RegisterClass; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1328 | else if (RegVT.isVector() && RegVT.getSizeInBits() == 128) |
Evan Cheng | ee472b1 | 2008-04-25 07:56:45 +0000 | [diff] [blame] | 1329 | RC = X86::VR128RegisterClass; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1330 | else if (RegVT.isVector()) { |
| 1331 | assert(RegVT.getSizeInBits() == 64); |
Evan Cheng | ee472b1 | 2008-04-25 07:56:45 +0000 | [diff] [blame] | 1332 | if (!Is64Bit) |
| 1333 | RC = X86::VR64RegisterClass; // MMX values are passed in MMXs. |
| 1334 | else { |
| 1335 | // Darwin calling convention passes MMX values in either GPRs or |
| 1336 | // XMMs in x86-64. Other targets pass them in memory. |
| 1337 | if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) { |
| 1338 | RC = X86::VR128RegisterClass; // MMX values are passed in XMMs. |
| 1339 | RegVT = MVT::v2i64; |
| 1340 | } else { |
| 1341 | RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs. |
| 1342 | RegVT = MVT::i64; |
| 1343 | } |
| 1344 | } |
| 1345 | } else { |
| 1346 | assert(0 && "Unknown argument type!"); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1347 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1348 | |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 1349 | unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1350 | SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1352 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 1353 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 1354 | // right size. |
| 1355 | if (VA.getLocInfo() == CCValAssign::SExt) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1356 | ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1357 | DAG.getValueType(VA.getValVT())); |
| 1358 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1359 | ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1360 | DAG.getValueType(VA.getValVT())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1362 | if (VA.getLocInfo() != CCValAssign::Full) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1363 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1364 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1365 | // Handle MMX values passed in GPRs. |
Evan Cheng | 44c0fd1 | 2008-04-25 20:13:28 +0000 | [diff] [blame] | 1366 | if (Is64Bit && RegVT != VA.getLocVT()) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1367 | if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1368 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue); |
Evan Cheng | 44c0fd1 | 2008-04-25 20:13:28 +0000 | [diff] [blame] | 1369 | else if (RC == X86::VR128RegisterClass) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1370 | ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, |
| 1371 | ArgValue, DAG.getConstant(0, MVT::i64)); |
| 1372 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue); |
Evan Cheng | 44c0fd1 | 2008-04-25 20:13:28 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1376 | ArgValues.push_back(ArgValue); |
| 1377 | } else { |
| 1378 | assert(VA.isMemLoc()); |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1379 | ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i)); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1380 | } |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1381 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1382 | |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1383 | // The x86-64 ABI for returning structs by value requires that we copy |
| 1384 | // the sret argument into %rax for the return. Save the argument into |
| 1385 | // a virtual register so that we can access it from the return points. |
| 1386 | if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 1387 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1388 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1389 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| 1390 | if (!Reg) { |
| 1391 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); |
| 1392 | FuncInfo->setSRetReturnReg(Reg); |
| 1393 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1394 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1395 | Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1398 | unsigned StackSize = CCInfo.getNextStackOffset(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1399 | // align stack specially for tail calls |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1400 | if (PerformTailCallOpt && CC == CallingConv::Fast) |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1401 | StackSize = GetAlignedArgumentStackSize(StackSize, DAG); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1402 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1403 | // If the function takes variable number of arguments, make a frame index for |
| 1404 | // the start of the first vararg value... for expansion of llvm.va_start. |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1405 | if (isVarArg) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1406 | if (Is64Bit || CC != CallingConv::X86_FastCall) { |
| 1407 | VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); |
| 1408 | } |
| 1409 | if (Is64Bit) { |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1410 | unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0; |
| 1411 | |
| 1412 | // FIXME: We should really autogenerate these arrays |
| 1413 | static const unsigned GPR64ArgRegsWin64[] = { |
| 1414 | X86::RCX, X86::RDX, X86::R8, X86::R9 |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1415 | }; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1416 | static const unsigned XMMArgRegsWin64[] = { |
| 1417 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
| 1418 | }; |
| 1419 | static const unsigned GPR64ArgRegs64Bit[] = { |
| 1420 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1421 | }; |
| 1422 | static const unsigned XMMArgRegs64Bit[] = { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1423 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1424 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1425 | }; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1426 | const unsigned *GPR64ArgRegs, *XMMArgRegs; |
| 1427 | |
| 1428 | if (IsWin64) { |
| 1429 | TotalNumIntRegs = 4; TotalNumXMMRegs = 4; |
| 1430 | GPR64ArgRegs = GPR64ArgRegsWin64; |
| 1431 | XMMArgRegs = XMMArgRegsWin64; |
| 1432 | } else { |
| 1433 | TotalNumIntRegs = 6; TotalNumXMMRegs = 8; |
| 1434 | GPR64ArgRegs = GPR64ArgRegs64Bit; |
| 1435 | XMMArgRegs = XMMArgRegs64Bit; |
| 1436 | } |
| 1437 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, |
| 1438 | TotalNumIntRegs); |
| 1439 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, |
| 1440 | TotalNumXMMRegs); |
| 1441 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 1442 | assert(!(NumXMMRegs && !Subtarget->hasSSE1()) && |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1443 | "SSE register cannot be used when SSE is disabled!"); |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 1444 | assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) && |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 1445 | "SSE register cannot be used when SSE is disabled!"); |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 1446 | if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1()) |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1447 | // Kernel mode asks for SSE to be disabled, so don't push them |
| 1448 | // on the stack. |
| 1449 | TotalNumXMMRegs = 0; |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 1450 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1451 | // For X86-64, if there are vararg parameters that are passed via |
| 1452 | // registers, then we must store them to their spots on the stack so they |
| 1453 | // may be loaded by deferencing the result of va_next. |
| 1454 | VarArgsGPOffset = NumIntRegs * 8; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1455 | VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16; |
| 1456 | RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 + |
| 1457 | TotalNumXMMRegs * 16, 16); |
| 1458 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1459 | // Store the integer parameter registers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1460 | SmallVector<SDValue, 8> MemOps; |
| 1461 | SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1462 | SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1463 | DAG.getIntPtrConstant(VarArgsGPOffset)); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1464 | for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) { |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 1465 | unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs], |
| 1466 | X86::GR64RegisterClass); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1467 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1468 | SDValue Store = |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1469 | DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1470 | PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1471 | MemOps.push_back(Store); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1472 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1473 | DAG.getIntPtrConstant(8)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1474 | } |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1475 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1476 | // Now store the XMM (fp + vector) parameter registers. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1477 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1478 | DAG.getIntPtrConstant(VarArgsFPOffset)); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1479 | for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) { |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 1480 | unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs], |
| 1481 | X86::VR128RegisterClass); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1482 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1483 | SDValue Store = |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1484 | DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1485 | PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1486 | MemOps.push_back(Store); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1487 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1488 | DAG.getIntPtrConstant(16)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1489 | } |
| 1490 | if (!MemOps.empty()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1491 | Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1492 | &MemOps[0], MemOps.size()); |
| 1493 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1494 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1495 | |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1496 | ArgValues.push_back(Root); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1497 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1498 | // Some CCs need callee pop. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1499 | if (IsCalleePop(isVarArg, CC)) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1500 | BytesToPopOnReturn = StackSize; // Callee pops everything. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1501 | BytesCallerReserves = 0; |
| 1502 | } else { |
Anton Korobeynikov | 1d9bacc | 2007-03-06 08:12:33 +0000 | [diff] [blame] | 1503 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1504 | // If this is an sret function, the return should pop the hidden pointer. |
Evan Cheng | b188dd9 | 2008-09-10 18:25:29 +0000 | [diff] [blame] | 1505 | if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1506 | BytesToPopOnReturn = 4; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1507 | BytesCallerReserves = StackSize; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1508 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1509 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1510 | if (!Is64Bit) { |
| 1511 | RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only. |
| 1512 | if (CC == CallingConv::X86_FastCall) |
| 1513 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 1514 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1515 | |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1516 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1517 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1518 | // Return the new list of results. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1519 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 1520 | &ArgValues[0], ArgValues.size()).getValue(Op.getResNo()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1523 | SDValue |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1524 | X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1525 | const SDValue &StackPtr, |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1526 | const CCValAssign &VA, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1527 | SDValue Chain, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1528 | SDValue Arg, ISD::ArgFlagsTy Flags) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1529 | DebugLoc dl = TheCall->getDebugLoc(); |
Dan Gohman | 4fdad17 | 2008-02-07 16:28:05 +0000 | [diff] [blame] | 1530 | unsigned LocMemOffset = VA.getLocMemOffset(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1531 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1532 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1533 | if (Flags.isByVal()) { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1534 | return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl); |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1535 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1536 | return DAG.getStore(Chain, dl, Arg, PtrOff, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1537 | PseudoSourceValue::getStack(), LocMemOffset); |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 1540 | /// EmitTailCallLoadRetAddr - Emit a load of return address if tail call |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1541 | /// optimization is performed and it is required. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1542 | SDValue |
| 1543 | X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1544 | SDValue &OutRetAddr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1545 | SDValue Chain, |
| 1546 | bool IsTailCall, |
| 1547 | bool Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1548 | int FPDiff, |
| 1549 | DebugLoc dl) { |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1550 | if (!IsTailCall || FPDiff==0) return Chain; |
| 1551 | |
| 1552 | // Adjust the Return address stack slot. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1553 | MVT VT = getPointerTy(); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1554 | OutRetAddr = getReturnAddressFrameIndex(DAG); |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 1555 | |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1556 | // Load the "old" Return address. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1557 | OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1558 | return SDValue(OutRetAddr.getNode(), 1); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call |
| 1562 | /// optimization is performed and it is required (FPDiff!=0). |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1563 | static SDValue |
| 1564 | EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1565 | SDValue Chain, SDValue RetAddrFrIdx, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1566 | bool Is64Bit, int FPDiff, DebugLoc dl) { |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1567 | // Store the return address to the appropriate stack slot. |
| 1568 | if (!FPDiff) return Chain; |
| 1569 | // Calculate the new stack slot for the return address. |
| 1570 | int SlotSize = Is64Bit ? 8 : 4; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1571 | int NewReturnAddrFI = |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1572 | MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1573 | MVT VT = Is64Bit ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1574 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1575 | Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1576 | PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1577 | return Chain; |
| 1578 | } |
| 1579 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1580 | SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1581 | MachineFunction &MF = DAG.getMachineFunction(); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1582 | CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); |
| 1583 | SDValue Chain = TheCall->getChain(); |
| 1584 | unsigned CC = TheCall->getCallingConv(); |
| 1585 | bool isVarArg = TheCall->isVarArg(); |
| 1586 | bool IsTailCall = TheCall->isTailCall() && |
| 1587 | CC == CallingConv::Fast && PerformTailCallOpt; |
| 1588 | SDValue Callee = TheCall->getCallee(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1589 | bool Is64Bit = Subtarget->is64Bit(); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1590 | bool IsStructRet = CallIsStructReturn(TheCall); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1591 | DebugLoc dl = TheCall->getDebugLoc(); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1592 | |
| 1593 | assert(!(isVarArg && CC == CallingConv::Fast) && |
| 1594 | "Var args not supported with calling convention fastcc"); |
| 1595 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1596 | // Analyze operands of the call, assigning locations to each operand. |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1597 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1598 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1599 | CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1601 | // Get a count of how many bytes are to be pushed on the stack. |
| 1602 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Arnold Schwaighofer | 1fdc40f | 2008-09-11 20:28:43 +0000 | [diff] [blame] | 1603 | if (PerformTailCallOpt && CC == CallingConv::Fast) |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1604 | NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1605 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1606 | int FPDiff = 0; |
| 1607 | if (IsTailCall) { |
| 1608 | // Lower arguments at fp - stackoffset + fpdiff. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1609 | unsigned NumBytesCallerPushed = |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1610 | MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn(); |
| 1611 | FPDiff = NumBytesCallerPushed - NumBytes; |
| 1612 | |
| 1613 | // Set the delta of movement of the returnaddr stackslot. |
| 1614 | // But only set if delta is greater than previous delta. |
| 1615 | if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta())) |
| 1616 | MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff); |
| 1617 | } |
| 1618 | |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 1619 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1620 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1621 | SDValue RetAddrFrIdx; |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1622 | // Load return adress for tail calls. |
| 1623 | Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1624 | FPDiff, dl); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1625 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1626 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 1627 | SmallVector<SDValue, 8> MemOpChains; |
| 1628 | SDValue StackPtr; |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1629 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1630 | // Walk the register/memloc assignments, inserting copies/loads. In the case |
| 1631 | // of tail call optimization arguments are handle later. |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1632 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1633 | CCValAssign &VA = ArgLocs[i]; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1634 | SDValue Arg = TheCall->getArg(i); |
| 1635 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
| 1636 | bool isByVal = Flags.isByVal(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1638 | // Promote the value if needed. |
| 1639 | switch (VA.getLocInfo()) { |
| 1640 | default: assert(0 && "Unknown loc info!"); |
| 1641 | case CCValAssign::Full: break; |
| 1642 | case CCValAssign::SExt: |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1643 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1644 | break; |
| 1645 | case CCValAssign::ZExt: |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1646 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1647 | break; |
| 1648 | case CCValAssign::AExt: |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1649 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1650 | break; |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1651 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1653 | if (VA.isRegLoc()) { |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 1654 | if (Is64Bit) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1655 | MVT RegVT = VA.getLocVT(); |
| 1656 | if (RegVT.isVector() && RegVT.getSizeInBits() == 64) |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 1657 | switch (VA.getLocReg()) { |
| 1658 | default: |
| 1659 | break; |
| 1660 | case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX: |
| 1661 | case X86::R8: { |
| 1662 | // Special case: passing MMX values in GPR registers. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1663 | Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg); |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3: |
| 1667 | case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: { |
| 1668 | // Special case: passing MMX values in XMM registers. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1669 | Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg); |
| 1670 | Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 1671 | Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg); |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | } |
| 1674 | } |
| 1675 | } |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1676 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 1677 | } else { |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1678 | if (!IsTailCall || (IsTailCall && isByVal)) { |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1679 | assert(VA.isMemLoc()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1680 | if (StackPtr.getNode() == 0) |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1681 | StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1682 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1683 | MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA, |
| 1684 | Chain, Arg, Flags)); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1685 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1686 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1687 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1688 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1689 | if (!MemOpChains.empty()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1690 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1691 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1692 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1693 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1694 | // and flag operands which copy the outgoing args into registers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1695 | SDValue InFlag; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1696 | // Tail call byval lowering might overwrite argument registers so in case of |
| 1697 | // tail call optimization the copies to registers are lowered later. |
| 1698 | if (!IsTailCall) |
| 1699 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1700 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1701 | RegsToPass[i].second, InFlag); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1702 | InFlag = Chain.getValue(1); |
| 1703 | } |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1704 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1705 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1706 | // GOT pointer. |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1707 | if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1708 | Chain = DAG.getCopyToReg(Chain, dl, X86::EBX, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1709 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 1710 | DebugLoc::getUnknownLoc(), |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1711 | getPointerTy()), |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1712 | InFlag); |
| 1713 | InFlag = Chain.getValue(1); |
| 1714 | } |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1715 | // If we are tail calling and generating PIC/GOT style code load the address |
| 1716 | // of the callee into ecx. The value in ecx is used as target of the tail |
| 1717 | // jump. This is done to circumvent the ebx/callee-saved problem for tail |
| 1718 | // calls on PIC/GOT architectures. Normally we would just put the address of |
| 1719 | // GOT into ebx and then call target@PLT. But for tail callss ebx would be |
| 1720 | // restored (since ebx is callee saved) before jumping to the target@PLT. |
Arnold Schwaighofer | 258bb1b | 2008-02-26 22:21:54 +0000 | [diff] [blame] | 1721 | if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) { |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1722 | // Note: The actual moving to ecx is done further down. |
| 1723 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 1724 | if (G && !G->getGlobal()->hasHiddenVisibility() && |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1725 | !G->getGlobal()->hasProtectedVisibility()) |
| 1726 | Callee = LowerGlobalAddress(Callee, DAG); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1727 | else if (isa<ExternalSymbolSDNode>(Callee)) |
| 1728 | Callee = LowerExternalSymbol(Callee,DAG); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1729 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1730 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1731 | if (Is64Bit && isVarArg) { |
| 1732 | // From AMD64 ABI document: |
| 1733 | // For calls that may call functions that use varargs or stdargs |
| 1734 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 1735 | // the declaration) %al is used as hidden argument to specify the number |
| 1736 | // of SSE registers used. The contents of %al do not need to match exactly |
| 1737 | // the number of registers, but must be an ubound on the number of SSE |
| 1738 | // registers used and is in the range 0 - 8 inclusive. |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1739 | |
| 1740 | // FIXME: Verify this on Win64 |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1741 | // Count the number of XMM registers allocated. |
| 1742 | static const unsigned XMMArgRegs[] = { |
| 1743 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1744 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1745 | }; |
| 1746 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1747 | assert((Subtarget->hasSSE1() || !NumXMMRegs) |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1748 | && "SSE registers cannot be used when SSE is disabled"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1749 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1750 | Chain = DAG.getCopyToReg(Chain, dl, X86::AL, |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1751 | DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); |
| 1752 | InFlag = Chain.getValue(1); |
| 1753 | } |
| 1754 | |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1755 | |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1756 | // For tail calls lower the arguments to the 'real' stack slot. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1757 | if (IsTailCall) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1758 | SmallVector<SDValue, 8> MemOpChains2; |
| 1759 | SDValue FIN; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1760 | int FI = 0; |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1761 | // Do not flag preceeding copytoreg stuff together with the following stuff. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1762 | InFlag = SDValue(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1763 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1764 | CCValAssign &VA = ArgLocs[i]; |
| 1765 | if (!VA.isRegLoc()) { |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1766 | assert(VA.isMemLoc()); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1767 | SDValue Arg = TheCall->getArg(i); |
| 1768 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1769 | // Create frame index. |
| 1770 | int32_t Offset = VA.getLocMemOffset()+FPDiff; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1771 | uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1772 | FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1773 | FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1774 | |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1775 | if (Flags.isByVal()) { |
Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1776 | // Copy relative to framepointer. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1777 | SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1778 | if (StackPtr.getNode() == 0) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1779 | StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1780 | getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1781 | Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1782 | |
| 1783 | MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1784 | Flags, DAG, dl)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1785 | } else { |
Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1786 | // Store relative to framepointer. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1787 | MemOpChains2.push_back( |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1788 | DAG.getStore(Chain, dl, Arg, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1789 | PseudoSourceValue::getFixedStack(FI), 0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1790 | } |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | if (!MemOpChains2.empty()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1795 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Arnold Schwaighofer | 719eb02 | 2008-01-11 14:34:56 +0000 | [diff] [blame] | 1796 | &MemOpChains2[0], MemOpChains2.size()); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1797 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1798 | // Copy arguments to their registers. |
| 1799 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1800 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1801 | RegsToPass[i].second, InFlag); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1802 | InFlag = Chain.getValue(1); |
| 1803 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1804 | InFlag =SDValue(); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1805 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1806 | // Store the return address to the appropriate stack slot. |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1807 | Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1808 | FPDiff, dl); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1811 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1812 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1813 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 1814 | // We should use extra load for direct calls to dllimported functions in |
| 1815 | // non-JIT mode. |
Evan Cheng | 817a6a9 | 2008-07-16 01:34:02 +0000 | [diff] [blame] | 1816 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1817 | getTargetMachine(), true)) |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1818 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(), |
| 1819 | G->getOffset()); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1820 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
| 1821 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1822 | } else if (IsTailCall) { |
Arnold Schwaighofer | 290ae03 | 2008-09-22 14:50:07 +0000 | [diff] [blame] | 1823 | unsigned Opc = Is64Bit ? X86::R9 : X86::EAX; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1824 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1825 | Chain = DAG.getCopyToReg(Chain, dl, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1826 | DAG.getRegister(Opc, getPointerTy()), |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1827 | Callee,InFlag); |
| 1828 | Callee = DAG.getRegister(Opc, getPointerTy()); |
| 1829 | // Add register as live out. |
| 1830 | DAG.getMachineFunction().getRegInfo().addLiveOut(Opc); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1831 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1832 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 1833 | // Returns a chain & a flag for retval copy to use. |
| 1834 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1835 | SmallVector<SDValue, 8> Ops; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1836 | |
| 1837 | if (IsTailCall) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1838 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 1839 | DAG.getIntPtrConstant(0, true), InFlag); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1840 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1841 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1842 | // Returns a chain & a flag for retval copy to use. |
| 1843 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 1844 | Ops.clear(); |
| 1845 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1846 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1847 | Ops.push_back(Chain); |
| 1848 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1849 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1850 | if (IsTailCall) |
| 1851 | Ops.push_back(DAG.getConstant(FPDiff, MVT::i32)); |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1852 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1853 | // Add argument registers to the end of the list so that they are known live |
| 1854 | // into the call. |
Evan Cheng | 9b44944 | 2008-01-07 23:08:23 +0000 | [diff] [blame] | 1855 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 1856 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 1857 | RegsToPass[i].second.getValueType())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1858 | |
Evan Cheng | 586ccac | 2008-03-18 23:36:35 +0000 | [diff] [blame] | 1859 | // Add an implicit use GOT pointer in EBX. |
| 1860 | if (!IsTailCall && !Is64Bit && |
| 1861 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1862 | Subtarget->isPICStyleGOT()) |
| 1863 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
| 1864 | |
| 1865 | // Add an implicit use of AL for x86 vararg functions. |
| 1866 | if (Is64Bit && isVarArg) |
| 1867 | Ops.push_back(DAG.getRegister(X86::AL, MVT::i8)); |
| 1868 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1869 | if (InFlag.getNode()) |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1870 | Ops.push_back(InFlag); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1871 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1872 | if (IsTailCall) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1873 | assert(InFlag.getNode() && |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1874 | "Flag must be set. Depend on flag being set in LowerRET"); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1875 | Chain = DAG.getNode(X86ISD::TAILCALL, dl, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1876 | TheCall->getVTList(), &Ops[0], Ops.size()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1877 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1878 | return SDValue(Chain.getNode(), Op.getResNo()); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1881 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1882 | InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 1884 | // Create the CALLSEQ_END node. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1885 | unsigned NumBytesForCalleeToPush; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1886 | if (IsCalleePop(isVarArg, CC)) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1887 | NumBytesForCalleeToPush = NumBytes; // Callee pops everything |
Evan Cheng | b188dd9 | 2008-09-10 18:25:29 +0000 | [diff] [blame] | 1888 | else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet) |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1889 | // If this is is a call to a struct-return function, the callee |
| 1890 | // pops the hidden struct pointer, so we have to push it back. |
| 1891 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1892 | NumBytesForCalleeToPush = 4; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1893 | else |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1894 | NumBytesForCalleeToPush = 0; // Callee pops nothing. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1895 | |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1896 | // Returns a flag for retval copy to use. |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1897 | Chain = DAG.getCALLSEQ_END(Chain, |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 1898 | DAG.getIntPtrConstant(NumBytes, true), |
| 1899 | DAG.getIntPtrConstant(NumBytesForCalleeToPush, |
| 1900 | true), |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1901 | InFlag); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1902 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1904 | // Handle result values, copying them out of physregs into vregs that we |
| 1905 | // return. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1906 | return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 1907 | Op.getResNo()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1910 | |
| 1911 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1912 | // Fast Calling Convention (tail call) implementation |
| 1913 | //===----------------------------------------------------------------------===// |
| 1914 | |
| 1915 | // Like std call, callee cleans arguments, convention except that ECX is |
| 1916 | // reserved for storing the tail called function address. Only 2 registers are |
| 1917 | // free for argument passing (inreg). Tail call optimization is performed |
| 1918 | // provided: |
| 1919 | // * tailcallopt is enabled |
| 1920 | // * caller/callee are fastcc |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1921 | // On X86_64 architecture with GOT-style position independent code only local |
| 1922 | // (within module) calls are supported at the moment. |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 1923 | // To keep the stack aligned according to platform abi the function |
| 1924 | // GetAlignedArgumentStackSize ensures that argument delta is always multiples |
| 1925 | // of stack alignment. (Dynamic linkers need this - darwin's dyld for example) |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1926 | // If a tail called function callee has more arguments than the caller the |
| 1927 | // caller needs to make sure that there is room to move the RETADDR to. This is |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 1928 | // achieved by reserving an area the size of the argument delta right after the |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1929 | // original REtADDR, but before the saved framepointer or the spilled registers |
| 1930 | // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) |
| 1931 | // stack layout: |
| 1932 | // arg1 |
| 1933 | // arg2 |
| 1934 | // RETADDR |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1935 | // [ new RETADDR |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1936 | // move area ] |
| 1937 | // (possible EBP) |
| 1938 | // ESI |
| 1939 | // EDI |
| 1940 | // local1 .. |
| 1941 | |
| 1942 | /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned |
| 1943 | /// for a 16 byte align requirement. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1944 | unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1945 | SelectionDAG& DAG) { |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1946 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1947 | const TargetMachine &TM = MF.getTarget(); |
| 1948 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 1949 | unsigned StackAlignment = TFI.getStackAlignment(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1950 | uint64_t AlignMask = StackAlignment - 1; |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1951 | int64_t Offset = StackSize; |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 1952 | uint64_t SlotSize = TD->getPointerSize(); |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1953 | if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) { |
| 1954 | // Number smaller than 12 so just add the difference. |
| 1955 | Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask)); |
| 1956 | } else { |
| 1957 | // Mask out lower bits, add stackalignment once plus the 12 bytes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1958 | Offset = ((~AlignMask) & Offset) + StackAlignment + |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1959 | (StackAlignment-SlotSize); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1960 | } |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1961 | return Offset; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | /// IsEligibleForTailCallElimination - Check to see whether the next instruction |
Evan Cheng | 9df7dc5 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1965 | /// following the call is a return. A function is eligible if caller/callee |
| 1966 | /// calling conventions match, currently only fastcc supports tail calls, and |
| 1967 | /// the function CALL is immediatly followed by a RET. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1968 | bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1969 | SDValue Ret, |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1970 | SelectionDAG& DAG) const { |
Evan Cheng | 9df7dc5 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1971 | if (!PerformTailCallOpt) |
| 1972 | return false; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1973 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1974 | if (CheckTailCallReturnConstraints(TheCall, Ret)) { |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1975 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1976 | unsigned CallerCC = MF.getFunction()->getCallingConv(); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1977 | unsigned CalleeCC= TheCall->getCallingConv(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1978 | if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1979 | SDValue Callee = TheCall->getCallee(); |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1980 | // On x86/32Bit PIC/GOT tail calls are supported. |
Evan Cheng | 9df7dc5 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1981 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_ || |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1982 | !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit()) |
Evan Cheng | 9df7dc5 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1983 | return true; |
| 1984 | |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 1985 | // Can only do local tail calls (in same module, hidden or protected) on |
| 1986 | // x86_64 PIC/GOT at the moment. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1987 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 1988 | return G->getGlobal()->hasHiddenVisibility() |
| 1989 | || G->getGlobal()->hasProtectedVisibility(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
Evan Cheng | 9df7dc5 | 2007-11-02 01:26:22 +0000 | [diff] [blame] | 1992 | |
| 1993 | return false; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1996 | FastISel * |
| 1997 | X86TargetLowering::createFastISel(MachineFunction &mf, |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 1998 | MachineModuleInfo *mmo, |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 1999 | DwarfWriter *dw, |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 2000 | DenseMap<const Value *, unsigned> &vm, |
| 2001 | DenseMap<const BasicBlock *, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 2002 | MachineBasicBlock *> &bm, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 2003 | DenseMap<const AllocaInst *, int> &am |
| 2004 | #ifndef NDEBUG |
| 2005 | , SmallSet<Instruction*, 8> &cil |
| 2006 | #endif |
| 2007 | ) { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 2008 | return X86::createFastISel(mf, mmo, dw, vm, bm, am |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 2009 | #ifndef NDEBUG |
| 2010 | , cil |
| 2011 | #endif |
| 2012 | ); |
Dan Gohman | d9f3c48 | 2008-08-19 21:32:53 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 2016 | //===----------------------------------------------------------------------===// |
| 2017 | // Other Lowering Hooks |
| 2018 | //===----------------------------------------------------------------------===// |
| 2019 | |
| 2020 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2021 | SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 2022 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2023 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 2024 | int ReturnAddrIndex = FuncInfo->getRAIndex(); |
| 2025 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2026 | if (ReturnAddrIndex == 0) { |
| 2027 | // Set up a frame object for the return address. |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 2028 | uint64_t SlotSize = TD->getPointerSize(); |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 2029 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize); |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 2030 | FuncInfo->setRAIndex(ReturnAddrIndex); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2033 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2037 | /// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 2038 | /// specific condition code, returning the condition code and the LHS/RHS of the |
| 2039 | /// comparison to make. |
| 2040 | static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
| 2041 | SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) { |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2042 | if (!isFP) { |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2043 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 2044 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 2045 | // X > -1 -> X == 0, jump !sign. |
| 2046 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2047 | return X86::COND_NS; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2048 | } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| 2049 | // X < 0 -> X == 0, jump on sign. |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2050 | return X86::COND_S; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2051 | } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) { |
Dan Gohman | 5f6913c | 2007-09-17 14:49:27 +0000 | [diff] [blame] | 2052 | // X < 1 -> X <= 0 |
| 2053 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2054 | return X86::COND_LE; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2055 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 2056 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2057 | |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2058 | switch (SetCCOpcode) { |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2059 | default: assert(0 && "Invalid integer condition!"); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2060 | case ISD::SETEQ: return X86::COND_E; |
| 2061 | case ISD::SETGT: return X86::COND_G; |
| 2062 | case ISD::SETGE: return X86::COND_GE; |
| 2063 | case ISD::SETLT: return X86::COND_L; |
| 2064 | case ISD::SETLE: return X86::COND_LE; |
| 2065 | case ISD::SETNE: return X86::COND_NE; |
| 2066 | case ISD::SETULT: return X86::COND_B; |
| 2067 | case ISD::SETUGT: return X86::COND_A; |
| 2068 | case ISD::SETULE: return X86::COND_BE; |
| 2069 | case ISD::SETUGE: return X86::COND_AE; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2070 | } |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2071 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2073 | // First determine if it is required or is profitable to flip the operands. |
Duncan Sands | 4047f4a | 2008-10-24 13:03:10 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2075 | // If LHS is a foldable load, but RHS is not, flip the condition. |
| 2076 | if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) && |
| 2077 | !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) { |
| 2078 | SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode); |
| 2079 | std::swap(LHS, RHS); |
Evan Cheng | 4d46d0a | 2008-08-28 23:48:31 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2082 | switch (SetCCOpcode) { |
| 2083 | default: break; |
| 2084 | case ISD::SETOLT: |
| 2085 | case ISD::SETOLE: |
| 2086 | case ISD::SETUGT: |
| 2087 | case ISD::SETUGE: |
| 2088 | std::swap(LHS, RHS); |
| 2089 | break; |
| 2090 | } |
| 2091 | |
| 2092 | // On a floating point condition, the flags are set as follows: |
| 2093 | // ZF PF CF op |
| 2094 | // 0 | 0 | 0 | X > Y |
| 2095 | // 0 | 0 | 1 | X < Y |
| 2096 | // 1 | 0 | 0 | X == Y |
| 2097 | // 1 | 1 | 1 | unordered |
| 2098 | switch (SetCCOpcode) { |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2099 | default: assert(0 && "Condcode should be pre-legalized away"); |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2100 | case ISD::SETUEQ: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2101 | case ISD::SETEQ: return X86::COND_E; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2102 | case ISD::SETOLT: // flipped |
| 2103 | case ISD::SETOGT: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2104 | case ISD::SETGT: return X86::COND_A; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2105 | case ISD::SETOLE: // flipped |
| 2106 | case ISD::SETOGE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2107 | case ISD::SETGE: return X86::COND_AE; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2108 | case ISD::SETUGT: // flipped |
| 2109 | case ISD::SETULT: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2110 | case ISD::SETLT: return X86::COND_B; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2111 | case ISD::SETUGE: // flipped |
| 2112 | case ISD::SETULE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2113 | case ISD::SETLE: return X86::COND_BE; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2114 | case ISD::SETONE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2115 | case ISD::SETNE: return X86::COND_NE; |
| 2116 | case ISD::SETUO: return X86::COND_P; |
| 2117 | case ISD::SETO: return X86::COND_NP; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2118 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2121 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 2122 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2123 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2124 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2125 | switch (X86CC) { |
| 2126 | default: |
| 2127 | return false; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2128 | case X86::COND_B: |
| 2129 | case X86::COND_BE: |
| 2130 | case X86::COND_E: |
| 2131 | case X86::COND_P: |
| 2132 | case X86::COND_A: |
| 2133 | case X86::COND_AE: |
| 2134 | case X86::COND_NE: |
| 2135 | case X86::COND_NP: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2136 | return true; |
| 2137 | } |
| 2138 | } |
| 2139 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2140 | /// isUndefOrInRange - Return true if Val is undef or if its value falls within |
| 2141 | /// the specified range (L, H]. |
| 2142 | static bool isUndefOrInRange(int Val, int Low, int Hi) { |
| 2143 | return (Val < 0) || (Val >= Low && Val < Hi); |
| 2144 | } |
| 2145 | |
| 2146 | /// isUndefOrEqual - Val is either less than zero (undef) or equal to the |
| 2147 | /// specified value. |
| 2148 | static bool isUndefOrEqual(int Val, int CmpVal) { |
| 2149 | if (Val < 0 || Val == CmpVal) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2150 | return true; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2151 | return false; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2154 | /// isPSHUFDMask - Return true if the node specifies a shuffle of elements that |
| 2155 | /// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference |
| 2156 | /// the second operand. |
| 2157 | static bool isPSHUFDMask(const int *Mask, MVT VT) { |
| 2158 | if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16) |
| 2159 | return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4); |
| 2160 | if (VT == MVT::v2f64 || VT == MVT::v2i64) |
| 2161 | return (Mask[0] < 2 && Mask[1] < 2); |
| 2162 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2165 | bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) { |
| 2166 | return ::isPSHUFDMask(N->getMask(), N->getValueType(0)); |
| 2167 | } |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2168 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2169 | /// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that |
| 2170 | /// is suitable for input to PSHUFHW. |
| 2171 | static bool isPSHUFHWMask(const int *Mask, MVT VT) { |
| 2172 | if (VT != MVT::v8i16) |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2173 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2174 | |
| 2175 | // Lower quadword copied in order or undef. |
| 2176 | for (int i = 0; i != 4; ++i) |
| 2177 | if (Mask[i] >= 0 && Mask[i] != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2178 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2179 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2180 | // Upper quadword shuffled. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2181 | for (int i = 4; i != 8; ++i) |
| 2182 | if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2183 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2184 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2185 | return true; |
| 2186 | } |
| 2187 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2188 | bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) { |
| 2189 | return ::isPSHUFHWMask(N->getMask(), N->getValueType(0)); |
| 2190 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2191 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2192 | /// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that |
| 2193 | /// is suitable for input to PSHUFLW. |
| 2194 | static bool isPSHUFLWMask(const int *Mask, MVT VT) { |
| 2195 | if (VT != MVT::v8i16) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2196 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2197 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2198 | // Upper quadword copied in order. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2199 | for (int i = 4; i != 8; ++i) |
| 2200 | if (Mask[i] >= 0 && Mask[i] != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2201 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2202 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2203 | // Lower quadword shuffled. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2204 | for (int i = 0; i != 4; ++i) |
| 2205 | if (Mask[i] >= 4) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2206 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2207 | |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2208 | return true; |
| 2209 | } |
| 2210 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2211 | bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) { |
| 2212 | return ::isPSHUFLWMask(N->getMask(), N->getValueType(0)); |
| 2213 | } |
| 2214 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2215 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2216 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2217 | static bool isSHUFPMask(const int *Mask, MVT VT) { |
| 2218 | int NumElems = VT.getVectorNumElements(); |
| 2219 | if (NumElems != 2 && NumElems != 4) |
| 2220 | return false; |
| 2221 | |
| 2222 | int Half = NumElems / 2; |
| 2223 | for (int i = 0; i < Half; ++i) |
| 2224 | if (!isUndefOrInRange(Mask[i], 0, NumElems)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2225 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2226 | for (int i = Half; i < NumElems; ++i) |
| 2227 | if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2228 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2229 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2230 | return true; |
| 2231 | } |
| 2232 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2233 | bool X86::isSHUFPMask(ShuffleVectorSDNode *N) { |
| 2234 | return ::isSHUFPMask(N->getMask(), N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2237 | /// isCommutedSHUFP - Returns true if the shuffle mask is exactly |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2238 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 2239 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 2240 | /// the upper half to come from vector 2. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2241 | static bool isCommutedSHUFPMask(const int *Mask, MVT VT) { |
| 2242 | int NumElems = VT.getVectorNumElements(); |
| 2243 | |
| 2244 | if (NumElems != 2 && NumElems != 4) |
| 2245 | return false; |
| 2246 | |
| 2247 | int Half = NumElems / 2; |
| 2248 | for (int i = 0; i < Half; ++i) |
| 2249 | if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2250 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2251 | for (int i = Half; i < NumElems; ++i) |
| 2252 | if (!isUndefOrInRange(Mask[i], 0, NumElems)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2253 | return false; |
| 2254 | return true; |
| 2255 | } |
| 2256 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2257 | static bool isCommutedSHUFP(ShuffleVectorSDNode *N) { |
| 2258 | return isCommutedSHUFPMask(N->getMask(), N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2261 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2262 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2263 | bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) { |
| 2264 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2265 | return false; |
| 2266 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 2267 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2268 | const int *Mask = N->getMask(); |
| 2269 | return isUndefOrEqual(Mask[0], 6) && |
| 2270 | isUndefOrEqual(Mask[1], 7) && |
| 2271 | isUndefOrEqual(Mask[2], 2) && |
| 2272 | isUndefOrEqual(Mask[3], 3); |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2275 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2276 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2277 | bool X86::isMOVLPMask(ShuffleVectorSDNode *N) { |
| 2278 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2279 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2280 | if (NumElems != 2 && NumElems != 4) |
| 2281 | return false; |
| 2282 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2283 | const int *Mask = N->getMask(); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2284 | for (unsigned i = 0; i < NumElems/2; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2285 | if (!isUndefOrEqual(Mask[i], i + NumElems)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2286 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2287 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2288 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2289 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2290 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2291 | |
| 2292 | return true; |
| 2293 | } |
| 2294 | |
| 2295 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2296 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 2297 | /// and MOVLHPS. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2298 | bool X86::isMOVHPMask(ShuffleVectorSDNode *N) { |
| 2299 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2300 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2301 | if (NumElems != 2 && NumElems != 4) |
| 2302 | return false; |
| 2303 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2304 | const int *Mask = N->getMask(); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2305 | for (unsigned i = 0; i < NumElems/2; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2306 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2307 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2308 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2309 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2310 | if (!isUndefOrEqual(Mask[i + NumElems/2], i + NumElems)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2311 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2312 | |
| 2313 | return true; |
| 2314 | } |
| 2315 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2316 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 2317 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 2318 | /// <2, 3, 2, 3> |
| 2319 | bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2320 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
| 2321 | |
| 2322 | if (NumElems != 4) |
| 2323 | return false; |
| 2324 | |
| 2325 | // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3 |
| 2326 | const int *Mask = N->getMask(); |
| 2327 | return isUndefOrEqual(Mask[0], 2) && isUndefOrEqual(Mask[1], 3) && |
| 2328 | isUndefOrEqual(Mask[2], 2) && isUndefOrEqual(Mask[3], 3); |
| 2329 | } |
| 2330 | |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2331 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2332 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2333 | static bool isUNPCKLMask(const int *Mask, MVT VT, bool V2IsSplat = false) { |
| 2334 | int NumElts = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2335 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2336 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2337 | |
| 2338 | for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2339 | int BitI = Mask[i]; |
| 2340 | int BitI1 = Mask[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2341 | if (!isUndefOrEqual(BitI, j)) |
| 2342 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2343 | if (V2IsSplat) { |
Mon P Wang | 7bcaefa | 2009-02-04 01:16:59 +0000 | [diff] [blame] | 2344 | if (!isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2345 | return false; |
| 2346 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2347 | if (!isUndefOrEqual(BitI1, j + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2348 | return false; |
| 2349 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2350 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2351 | return true; |
| 2352 | } |
| 2353 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2354 | bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) { |
| 2355 | return ::isUNPCKLMask(N->getMask(), N->getValueType(0), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2358 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2359 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2360 | static bool isUNPCKHMask(const int *Mask, MVT VT, bool V2IsSplat = false) { |
| 2361 | int NumElts = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2362 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2363 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2364 | |
| 2365 | for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2366 | int BitI = Mask[i]; |
| 2367 | int BitI1 = Mask[i+1]; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2368 | if (!isUndefOrEqual(BitI, j + NumElts/2)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2369 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2370 | if (V2IsSplat) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2371 | if (isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2372 | return false; |
| 2373 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2374 | if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2375 | return false; |
| 2376 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2377 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2378 | return true; |
| 2379 | } |
| 2380 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2381 | bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) { |
| 2382 | return ::isUNPCKHMask(N->getMask(), N->getValueType(0), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2385 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 2386 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 2387 | /// <0, 0, 1, 1> |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2388 | static bool isUNPCKL_v_undef_Mask(const int *Mask, MVT VT) { |
| 2389 | int NumElems = VT.getVectorNumElements(); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2390 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2391 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2392 | |
| 2393 | for (int i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 2394 | int BitI = Mask[i]; |
| 2395 | int BitI1 = Mask[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2396 | if (!isUndefOrEqual(BitI, j)) |
| 2397 | return false; |
| 2398 | if (!isUndefOrEqual(BitI1, j)) |
| 2399 | return false; |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2400 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2401 | return true; |
| 2402 | } |
| 2403 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2404 | bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2405 | return ::isUNPCKL_v_undef_Mask(N->getMask(), N->getValueType(0)); |
| 2406 | } |
| 2407 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2408 | /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form |
| 2409 | /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, |
| 2410 | /// <2, 2, 3, 3> |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2411 | static bool isUNPCKH_v_undef_Mask(const int *Mask, MVT VT) { |
| 2412 | int NumElems = VT.getVectorNumElements(); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2413 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2414 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2415 | |
| 2416 | for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) { |
| 2417 | int BitI = Mask[i]; |
| 2418 | int BitI1 = Mask[i+1]; |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2419 | if (!isUndefOrEqual(BitI, j)) |
| 2420 | return false; |
| 2421 | if (!isUndefOrEqual(BitI1, j)) |
| 2422 | return false; |
| 2423 | } |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2424 | return true; |
| 2425 | } |
| 2426 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2427 | bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2428 | return ::isUNPCKH_v_undef_Mask(N->getMask(), N->getValueType(0)); |
| 2429 | } |
| 2430 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2431 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2432 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 2433 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2434 | static bool isMOVLMask(const int *Mask, MVT VT) { |
| 2435 | int NumElts = VT.getVectorNumElements(); |
Evan Cheng | 1076210 | 2007-12-06 22:14:22 +0000 | [diff] [blame] | 2436 | if (NumElts != 2 && NumElts != 4) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2437 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2438 | |
| 2439 | if (!isUndefOrEqual(Mask[0], NumElts)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2440 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2441 | |
| 2442 | for (int i = 1; i < NumElts; ++i) |
| 2443 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2444 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2445 | |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2446 | return true; |
| 2447 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2448 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2449 | bool X86::isMOVLMask(ShuffleVectorSDNode *N) { |
| 2450 | return ::isMOVLMask(N->getMask(), N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2453 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 2454 | /// of what x86 movss want. X86 movs requires the lowest element to be lowest |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2455 | /// element of vector 2 and the other elements to come from vector 1 in order. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2456 | static bool isCommutedMOVLMask(const int *Mask, MVT VT, bool V2IsSplat = false, |
| 2457 | bool V2IsUndef = false) { |
| 2458 | int NumOps = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2459 | if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2460 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2461 | |
| 2462 | if (!isUndefOrEqual(Mask[0], 0)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2463 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2464 | |
| 2465 | for (int i = 1; i < NumOps; ++i) |
| 2466 | if (!(isUndefOrEqual(Mask[i], i+NumOps) || |
| 2467 | (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) || |
| 2468 | (V2IsSplat && isUndefOrEqual(Mask[i], NumOps)))) |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2469 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2470 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2471 | return true; |
| 2472 | } |
| 2473 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2474 | static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false, |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2475 | bool V2IsUndef = false) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2476 | return isCommutedMOVLMask(N->getMask(), N->getValueType(0), V2IsSplat, |
| 2477 | V2IsUndef); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2480 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2481 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2482 | bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) { |
| 2483 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2484 | return false; |
| 2485 | |
| 2486 | // Expect 1, 1, 3, 3 |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2487 | const int *Mask = N->getMask(); |
| 2488 | for (unsigned i = 0; i < 2; ++i) |
| 2489 | if (Mask[i] >=0 && Mask[i] != 1) |
| 2490 | return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2491 | |
| 2492 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2493 | for (unsigned i = 2; i < 4; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2494 | if (Mask[i] >= 0 && Mask[i] != 3) |
| 2495 | return false; |
| 2496 | if (Mask[i] == 3) |
| 2497 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2498 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2499 | // Don't use movshdup if it can be done with a shufps. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2500 | // FIXME: verify that matching u, u, 3, 3 is what we want. |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2501 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2505 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2506 | bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) { |
| 2507 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2508 | return false; |
| 2509 | |
| 2510 | // Expect 0, 0, 2, 2 |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2511 | const int *Mask = N->getMask(); |
| 2512 | for (unsigned i = 0; i < 2; ++i) |
| 2513 | if (Mask[i] > 0) |
| 2514 | return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2515 | |
| 2516 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2517 | for (unsigned i = 2; i < 4; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2518 | if (Mask[i] >= 0 && Mask[i] != 2) |
| 2519 | return false; |
| 2520 | if (Mask[i] == 2) |
| 2521 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2522 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2523 | // Don't use movsldup if it can be done with a shufps. |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2524 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2527 | /// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2528 | /// specifies a shuffle of elements that is suitable for input to MOVDDUP. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2529 | bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) { |
| 2530 | int e = N->getValueType(0).getVectorNumElements() / 2; |
| 2531 | const int *Mask = N->getMask(); |
| 2532 | |
| 2533 | for (int i = 0; i < e; ++i) |
| 2534 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2535 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2536 | for (int i = 0; i < e; ++i) |
| 2537 | if (!isUndefOrEqual(Mask[e+i], i)) |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2538 | return false; |
| 2539 | return true; |
| 2540 | } |
| 2541 | |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2542 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 2543 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 2544 | /// instructions. |
| 2545 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2546 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 2547 | int NumOperands = SVOp->getValueType(0).getVectorNumElements(); |
| 2548 | const int *MaskP = SVOp->getMask(); |
| 2549 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2550 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 2551 | unsigned Mask = 0; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2552 | for (int i = 0; i < NumOperands; ++i) { |
| 2553 | int Val = MaskP[NumOperands-i-1]; |
| 2554 | if (Val < 0) Val = 0; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2555 | if (Val >= NumOperands) Val -= NumOperands; |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2556 | Mask |= Val; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2557 | if (i != NumOperands - 1) |
| 2558 | Mask <<= Shift; |
| 2559 | } |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2560 | return Mask; |
| 2561 | } |
| 2562 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2563 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 2564 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 2565 | /// instructions. |
| 2566 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2567 | const int *MaskP = cast<ShuffleVectorSDNode>(N)->getMask(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2568 | unsigned Mask = 0; |
| 2569 | // 8 nodes, but we only care about the last 4. |
| 2570 | for (unsigned i = 7; i >= 4; --i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2571 | int Val = MaskP[i]; |
| 2572 | if (Val >= 0) |
Mon P Wang | 7bcaefa | 2009-02-04 01:16:59 +0000 | [diff] [blame] | 2573 | Mask |= (Val - 4); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2574 | if (i != 4) |
| 2575 | Mask <<= 2; |
| 2576 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2577 | return Mask; |
| 2578 | } |
| 2579 | |
| 2580 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2581 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2582 | /// instructions. |
| 2583 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2584 | const int *MaskP = cast<ShuffleVectorSDNode>(N)->getMask(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2585 | unsigned Mask = 0; |
| 2586 | // 8 nodes, but we only care about the first 4. |
| 2587 | for (int i = 3; i >= 0; --i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2588 | int Val = MaskP[i]; |
| 2589 | if (Val >= 0) |
| 2590 | Mask |= Val; |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2591 | if (i != 0) |
| 2592 | Mask <<= 2; |
| 2593 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2594 | return Mask; |
| 2595 | } |
| 2596 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2597 | /// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in |
| 2598 | /// their permute mask. |
| 2599 | static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp, |
| 2600 | SelectionDAG &DAG) { |
| 2601 | MVT VT = SVOp->getValueType(0); |
| 2602 | int NumElems = VT.getVectorNumElements(); |
| 2603 | const int *Mask = SVOp->getMask(); |
| 2604 | SmallVector<int, 8> MaskVec; |
| 2605 | |
| 2606 | for (int i = 0; i != NumElems; ++i) { |
| 2607 | int idx = Mask[i]; |
| 2608 | if (idx < 0) |
| 2609 | MaskVec.push_back(idx); |
| 2610 | else if (idx < NumElems) |
| 2611 | MaskVec.push_back(idx + NumElems); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2612 | else |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2613 | MaskVec.push_back(idx - NumElems); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2614 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2615 | return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1), |
| 2616 | SVOp->getOperand(0), &MaskVec[0]); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Evan Cheng | 779ccea | 2007-12-07 21:30:01 +0000 | [diff] [blame] | 2619 | /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming |
| 2620 | /// the two vector operands have swapped position. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2621 | static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) { |
| 2622 | int NumElems = VT.getVectorNumElements(); |
| 2623 | for (int i = 0; i != NumElems; ++i) { |
| 2624 | int idx = Mask[i]; |
| 2625 | if (idx < 0) |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2626 | continue; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2627 | else if (idx < NumElems) |
| 2628 | Mask[i] = idx + NumElems; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2629 | else |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2630 | Mask[i] = idx - NumElems; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2631 | } |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2634 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2635 | /// match movhlps. The lower half elements should come from upper half of |
| 2636 | /// V1 (and in order), and the upper half elements should come from the upper |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2637 | /// half of V2 (and in order). |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2638 | static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) { |
| 2639 | int NumElems = Op->getValueType(0).getVectorNumElements(); |
| 2640 | const int *Mask = Op->getMask(); |
| 2641 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2642 | if (NumElems != 4) |
| 2643 | return false; |
| 2644 | for (unsigned i = 0, e = 2; i != e; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2645 | if (!isUndefOrEqual(Mask[i], i+2)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2646 | return false; |
| 2647 | for (unsigned i = 2; i != 4; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2648 | if (!isUndefOrEqual(Mask[i], i+4)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2649 | return false; |
| 2650 | return true; |
| 2651 | } |
| 2652 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2653 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 2654 | /// is promoted to a vector. It also returns the LoadSDNode by reference if |
| 2655 | /// required. |
| 2656 | static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) { |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2657 | if (N->getOpcode() != ISD::SCALAR_TO_VECTOR) |
| 2658 | return false; |
| 2659 | N = N->getOperand(0).getNode(); |
| 2660 | if (!ISD::isNON_EXTLoad(N)) |
| 2661 | return false; |
| 2662 | if (LD) |
| 2663 | *LD = cast<LoadSDNode>(N); |
| 2664 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2667 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2668 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2669 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2670 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2671 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2672 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, |
| 2673 | ShuffleVectorSDNode *Op) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2674 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2675 | return false; |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2676 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 2677 | // load folding shufps op. |
| 2678 | if (ISD::isNON_EXTLoad(V2)) |
| 2679 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2680 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2681 | int NumElems = Op->getValueType(0).getVectorNumElements(); |
| 2682 | const int *Mask = Op->getMask(); |
| 2683 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2684 | if (NumElems != 2 && NumElems != 4) |
| 2685 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2686 | for (int i = 0, e = NumElems/2; i != e; ++i) |
| 2687 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2688 | return false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2689 | for (int i = NumElems/2; i != NumElems; ++i) |
| 2690 | if (!isUndefOrEqual(Mask[i], i+NumElems)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2691 | return false; |
| 2692 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2695 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2696 | /// all the same. |
| 2697 | static bool isSplatVector(SDNode *N) { |
| 2698 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2699 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2700 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2701 | SDValue SplatValue = N->getOperand(0); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2702 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2703 | if (N->getOperand(i) != SplatValue) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2704 | return false; |
| 2705 | return true; |
| 2706 | } |
| 2707 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2708 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2709 | /// constant +0.0. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2710 | static inline bool isZeroNode(SDValue Elt) { |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2711 | return ((isa<ConstantSDNode>(Elt) && |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2712 | cast<ConstantSDNode>(Elt)->getZExtValue() == 0) || |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2713 | (isa<ConstantFPSDNode>(Elt) && |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2714 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2718 | /// to an zero vector. |
| 2719 | /// FIXME: move to dag combiner? |
| 2720 | static bool isZeroShuffle(ShuffleVectorSDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2721 | SDValue V1 = N->getOperand(0); |
| 2722 | SDValue V2 = N->getOperand(1); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2723 | const int *Mask = N->getMask(); |
| 2724 | int NumElems = N->getValueType(0).getVectorNumElements(); |
| 2725 | for (int i = 0; i != NumElems; ++i) { |
| 2726 | int Idx = Mask[i]; |
| 2727 | if (Idx >= NumElems) { |
| 2728 | unsigned Opc = V2.getOpcode(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2729 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode())) |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2730 | continue; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2731 | if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems))) |
| 2732 | return false; |
| 2733 | } else if (Idx >= 0) { |
| 2734 | unsigned Opc = V1.getOpcode(); |
| 2735 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode())) |
| 2736 | continue; |
| 2737 | if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx))) |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2738 | return false; |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | return true; |
| 2742 | } |
| 2743 | |
| 2744 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2745 | /// |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2746 | static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG, |
| 2747 | DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2748 | assert(VT.isVector() && "Expected a vector type"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2750 | // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2751 | // type. This ensures they get CSE'd. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2752 | SDValue Vec; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2753 | if (VT.getSizeInBits() == 64) { // MMX |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2754 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2755 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2756 | } else if (HasSSE2) { // SSE2 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2757 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2758 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2759 | } else { // SSE1 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2760 | SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2761 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2762 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2763 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2766 | /// getOnesVector - Returns a vector of specified type with all bits set. |
| 2767 | /// |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2768 | static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2769 | assert(VT.isVector() && "Expected a vector type"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2770 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2771 | // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2772 | // type. This ensures they get CSE'd. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2773 | SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32); |
| 2774 | SDValue Vec; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2775 | if (VT.getSizeInBits() == 64) // MMX |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2776 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2777 | else // SSE |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2778 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2779 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2783 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2784 | /// that point to V2 points to its first element. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2785 | static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| 2786 | MVT VT = SVOp->getValueType(0); |
| 2787 | int NumElems = VT.getVectorNumElements(); |
| 2788 | const int *Mask = SVOp->getMask(); |
| 2789 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2790 | bool Changed = false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2791 | SmallVector<int, 8> MaskVec; |
| 2792 | |
| 2793 | for (int i = 0; i != NumElems; ++i) { |
| 2794 | int idx = Mask[i]; |
| 2795 | if (idx > NumElems) { |
| 2796 | idx = NumElems; |
| 2797 | Changed = true; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2798 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2799 | MaskVec.push_back(idx); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2800 | } |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2801 | if (Changed) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2802 | return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0), |
| 2803 | SVOp->getOperand(1), &MaskVec[0]); |
| 2804 | return SDValue(SVOp, 0); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2807 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2808 | /// operation of specified width. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2809 | static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2810 | SDValue V2) { |
| 2811 | unsigned NumElems = VT.getVectorNumElements(); |
| 2812 | SmallVector<int, 8> Mask; |
| 2813 | Mask.push_back(NumElems); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2814 | for (unsigned i = 1; i != NumElems; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2815 | Mask.push_back(i); |
| 2816 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2819 | /// getUnpackl - Returns a vector_shuffle node for an unpackl operation. |
| 2820 | static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2821 | SDValue V2) { |
| 2822 | unsigned NumElems = VT.getVectorNumElements(); |
| 2823 | SmallVector<int, 8> Mask; |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2824 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2825 | Mask.push_back(i); |
| 2826 | Mask.push_back(i + NumElems); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2827 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2828 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2831 | /// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation. |
| 2832 | static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2833 | SDValue V2) { |
| 2834 | unsigned NumElems = VT.getVectorNumElements(); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2835 | unsigned Half = NumElems/2; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2836 | SmallVector<int, 8> Mask; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2837 | for (unsigned i = 0; i != Half; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2838 | Mask.push_back(i + Half); |
| 2839 | Mask.push_back(i + NumElems + Half); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2840 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2841 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Evan Cheng | 0c0f83f | 2008-04-05 00:30:36 +0000 | [diff] [blame] | 2844 | /// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2845 | static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG, |
| 2846 | bool HasSSE2) { |
| 2847 | if (SV->getValueType(0).getVectorNumElements() <= 4) |
| 2848 | return SDValue(SV, 0); |
| 2849 | |
| 2850 | MVT PVT = MVT::v4f32; |
| 2851 | MVT VT = SV->getValueType(0); |
| 2852 | DebugLoc dl = SV->getDebugLoc(); |
| 2853 | SDValue V1 = SV->getOperand(0); |
| 2854 | int NumElems = VT.getVectorNumElements(); |
| 2855 | int EltNo = SV->getSplatIndex(); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2856 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2857 | // unpack elements to the correct location |
| 2858 | while (NumElems > 4) { |
| 2859 | if (EltNo < NumElems/2) { |
| 2860 | V1 = getUnpackl(DAG, dl, VT, V1, V1); |
| 2861 | } else { |
| 2862 | V1 = getUnpackh(DAG, dl, VT, V1, V1); |
| 2863 | EltNo -= NumElems/2; |
| 2864 | } |
| 2865 | NumElems >>= 1; |
| 2866 | } |
| 2867 | |
| 2868 | // Perform the splat. |
| 2869 | int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2870 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2871 | V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]); |
| 2872 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2875 | /// isVectorLoad - Returns true if the node is a vector load, a scalar |
| 2876 | /// load that's promoted to vector, or a load bitcasted. |
| 2877 | static bool isVectorLoad(SDValue Op) { |
| 2878 | assert(Op.getValueType().isVector() && "Expected a vector type"); |
| 2879 | if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR || |
| 2880 | Op.getOpcode() == ISD::BIT_CONVERT) { |
| 2881 | return isa<LoadSDNode>(Op.getOperand(0)); |
| 2882 | } |
| 2883 | return isa<LoadSDNode>(Op); |
| 2884 | } |
| 2885 | |
| 2886 | |
| 2887 | /// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64. |
| 2888 | /// |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2889 | static SDValue CanonicalizeMovddup(ShuffleVectorSDNode *SV, SelectionDAG &DAG, |
| 2890 | bool HasSSE3) { |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2891 | // If we have sse3 and shuffle has more than one use or input is a load, then |
| 2892 | // use movddup. Otherwise, use movlhps. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2893 | SDValue V1 = SV->getOperand(0); |
| 2894 | |
| 2895 | bool UseMovddup = HasSSE3 && (!SV->hasOneUse() || isVectorLoad(V1)); |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2896 | MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2897 | MVT VT = SV->getValueType(0); |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2898 | if (VT == PVT) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2899 | return SDValue(SV, 0); |
| 2900 | |
| 2901 | DebugLoc dl = SV->getDebugLoc(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2902 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2903 | if (PVT.getVectorNumElements() == 2) { |
| 2904 | int Mask[2] = { 0, 0 }; |
| 2905 | V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), Mask); |
| 2906 | } else { |
| 2907 | int Mask[4] = { 0, 1, 0, 1 }; |
| 2908 | V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), Mask); |
| 2909 | } |
| 2910 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1); |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2913 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2914 | /// vector of zero or undef vector. This produces a shuffle where the low |
| 2915 | /// element of V2 is swizzled into the zero/undef vector, landing at element |
| 2916 | /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3). |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2917 | static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx, |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2918 | bool isZero, bool HasSSE2, |
| 2919 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2920 | MVT VT = V2.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2921 | SDValue V1 = isZero |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2922 | ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT); |
| 2923 | unsigned NumElems = VT.getVectorNumElements(); |
| 2924 | SmallVector<int, 16> MaskVec; |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2925 | for (unsigned i = 0; i != NumElems; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2926 | // If this is the insertion idx, put the low elt of V2 here. |
| 2927 | MaskVec.push_back(i == Idx ? NumElems : i); |
| 2928 | return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2931 | /// getNumOfConsecutiveZeros - Return the number of elements in a result of |
| 2932 | /// a shuffle that is zero. |
| 2933 | static |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2934 | unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, const int *Mask, |
| 2935 | int NumElems, bool Low, SelectionDAG &DAG) { |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2936 | unsigned NumZeros = 0; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2937 | for (int i = 0; i < NumElems; ++i) { |
Evan Cheng | ab26227 | 2008-06-25 20:52:59 +0000 | [diff] [blame] | 2938 | unsigned Index = Low ? i : NumElems-i-1; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2939 | int Idx = Mask[Index]; |
| 2940 | if (Idx < 0) { |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2941 | ++NumZeros; |
| 2942 | continue; |
| 2943 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2944 | SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2945 | if (Elt.getNode() && isZeroNode(Elt)) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2946 | ++NumZeros; |
| 2947 | else |
| 2948 | break; |
| 2949 | } |
| 2950 | return NumZeros; |
| 2951 | } |
| 2952 | |
| 2953 | /// isVectorShift - Returns true if the shuffle can be implemented as a |
| 2954 | /// logical left or right shift of a vector. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2955 | /// FIXME: split into pslldqi, psrldqi, palignr variants. |
| 2956 | static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2957 | bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2958 | const int *Mask = SVOp->getMask(); |
| 2959 | int NumElems = SVOp->getValueType(0).getVectorNumElements(); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2960 | |
| 2961 | isLeft = true; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2962 | unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, Mask, NumElems, true, DAG); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2963 | if (!NumZeros) { |
| 2964 | isLeft = false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2965 | NumZeros = getNumOfConsecutiveZeros(SVOp, Mask, NumElems, false, DAG); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2966 | if (!NumZeros) |
| 2967 | return false; |
| 2968 | } |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2969 | bool SeenV1 = false; |
| 2970 | bool SeenV2 = false; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2971 | for (int i = NumZeros; i < NumElems; ++i) { |
| 2972 | int Val = isLeft ? (i - NumZeros) : i; |
| 2973 | int Idx = Mask[isLeft ? i : (i - NumZeros)]; |
| 2974 | if (Idx < 0) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2975 | continue; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2976 | if (Idx < NumElems) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2977 | SeenV1 = true; |
| 2978 | else { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2979 | Idx -= NumElems; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2980 | SeenV2 = true; |
| 2981 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2982 | if (Idx != Val) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2983 | return false; |
| 2984 | } |
| 2985 | if (SeenV1 && SeenV2) |
| 2986 | return false; |
| 2987 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 2988 | ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 2989 | ShAmt = NumZeros; |
| 2990 | return true; |
| 2991 | } |
| 2992 | |
| 2993 | |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2994 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 2995 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2996 | static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2997 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2998 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2999 | if (NumNonZero > 8) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3000 | return SDValue(); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3001 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3002 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3003 | SDValue V(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3004 | bool First = true; |
| 3005 | for (unsigned i = 0; i < 16; ++i) { |
| 3006 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 3007 | if (ThisIsNonZero && First) { |
| 3008 | if (NumZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3009 | V = getZeroVector(MVT::v8i16, true, DAG, dl); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3010 | else |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3011 | V = DAG.getUNDEF(MVT::v8i16); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3012 | First = false; |
| 3013 | } |
| 3014 | |
| 3015 | if ((i & 1) != 0) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3016 | SDValue ThisElt(0, 0), LastElt(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3017 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 3018 | if (LastIsNonZero) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3019 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3020 | MVT::i16, Op.getOperand(i-1)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3021 | } |
| 3022 | if (ThisIsNonZero) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3023 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i)); |
| 3024 | ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3025 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 3026 | if (LastIsNonZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3027 | ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3028 | } else |
| 3029 | ThisElt = LastElt; |
| 3030 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3031 | if (ThisElt.getNode()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3032 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3033 | DAG.getIntPtrConstant(i/2)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3037 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 3040 | /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3041 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3042 | static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3043 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3044 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3045 | if (NumNonZero > 4) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3046 | return SDValue(); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3047 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3048 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3049 | SDValue V(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3050 | bool First = true; |
| 3051 | for (unsigned i = 0; i < 8; ++i) { |
| 3052 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 3053 | if (isNonZero) { |
| 3054 | if (First) { |
| 3055 | if (NumZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3056 | V = getZeroVector(MVT::v8i16, true, DAG, dl); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3057 | else |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3058 | V = DAG.getUNDEF(MVT::v8i16); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3059 | First = false; |
| 3060 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3061 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3062 | MVT::v8i16, V, Op.getOperand(i), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3063 | DAG.getIntPtrConstant(i)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | return V; |
| 3068 | } |
| 3069 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3070 | /// getVShift - Return a vector logical shift node. |
| 3071 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3072 | static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp, |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3073 | unsigned NumBits, SelectionDAG &DAG, |
| 3074 | const TargetLowering &TLI, DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3075 | bool isMMX = VT.getSizeInBits() == 64; |
| 3076 | MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3077 | unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3078 | SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp); |
| 3079 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3080 | DAG.getNode(Opc, dl, ShVT, SrcOp, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 3081 | DAG.getConstant(NumBits, TLI.getShiftAmountTy()))); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3084 | SDValue |
| 3085 | X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3086 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3087 | // All zero's are handled with pxor, all one's are handled with pcmpeqd. |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 3088 | if (ISD::isBuildVectorAllZeros(Op.getNode()) |
| 3089 | || ISD::isBuildVectorAllOnes(Op.getNode())) { |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3090 | // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to |
| 3091 | // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are |
| 3092 | // eliminated on x86-32 hosts. |
| 3093 | if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32) |
| 3094 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3095 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3096 | if (ISD::isBuildVectorAllOnes(Op.getNode())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3097 | return getOnesVector(Op.getValueType(), DAG, dl); |
| 3098 | return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3099 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3100 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3101 | MVT VT = Op.getValueType(); |
| 3102 | MVT EVT = VT.getVectorElementType(); |
| 3103 | unsigned EVTBits = EVT.getSizeInBits(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3104 | |
| 3105 | unsigned NumElems = Op.getNumOperands(); |
| 3106 | unsigned NumZero = 0; |
| 3107 | unsigned NumNonZero = 0; |
| 3108 | unsigned NonZeros = 0; |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3109 | bool IsAllConstants = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3110 | SmallSet<SDValue, 8> Values; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3111 | for (unsigned i = 0; i < NumElems; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3112 | SDValue Elt = Op.getOperand(i); |
Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 3113 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3114 | continue; |
| 3115 | Values.insert(Elt); |
| 3116 | if (Elt.getOpcode() != ISD::Constant && |
| 3117 | Elt.getOpcode() != ISD::ConstantFP) |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3118 | IsAllConstants = false; |
Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 3119 | if (isZeroNode(Elt)) |
| 3120 | NumZero++; |
| 3121 | else { |
| 3122 | NonZeros |= (1 << i); |
| 3123 | NumNonZero++; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3124 | } |
| 3125 | } |
| 3126 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3127 | if (NumNonZero == 0) { |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3128 | // All undef vector. Return an UNDEF. All zero vectors were handled above. |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3129 | return DAG.getUNDEF(VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3130 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 3132 | // Special case for single non-zero, non-undef, element. |
Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 3133 | if (NumNonZero == 1 && NumElems <= 4) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3134 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3135 | SDValue Item = Op.getOperand(Idx); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3136 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3137 | // If this is an insertion of an i64 value on x86-32, and if the top bits of |
| 3138 | // the value are obviously zero, truncate the value to i32 and do the |
| 3139 | // insertion that way. Only do this if the value is non-constant or if the |
| 3140 | // value is a constant being inserted into element 0. It is cheaper to do |
| 3141 | // a constant pool load than it is to do a movd + shuffle. |
| 3142 | if (EVT == MVT::i64 && !Subtarget->is64Bit() && |
| 3143 | (!IsAllConstants || Idx == 0)) { |
| 3144 | if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) { |
| 3145 | // Handle MMX and SSE both. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3146 | MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32; |
| 3147 | unsigned VecElts = VT == MVT::v2i64 ? 4 : 2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3148 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3149 | // Truncate the value (which may itself be a constant) to i32, and |
| 3150 | // convert it to a vector with movd (S2V+shuffle to zero extend). |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3151 | Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item); |
| 3152 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3153 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, |
| 3154 | Subtarget->hasSSE2(), DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3155 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3156 | // Now we have our 32-bit value zero extended in the low element of |
| 3157 | // a vector. If Idx != 0, swizzle it into place. |
| 3158 | if (Idx != 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3159 | SmallVector<int, 4> Mask; |
| 3160 | Mask.push_back(Idx); |
| 3161 | for (unsigned i = 1; i != VecElts; ++i) |
| 3162 | Mask.push_back(i); |
| 3163 | Item = DAG.getVectorShuffle(VecVT, dl, Item, |
| 3164 | DAG.getUNDEF(Item.getValueType()), |
| 3165 | &Mask[0]); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3166 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3167 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3168 | } |
| 3169 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3170 | |
Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 3171 | // If we have a constant or non-constant insertion into the low element of |
| 3172 | // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into |
| 3173 | // the rest of the elements. This will be matched as movd/movq/movss/movsd |
| 3174 | // depending on what the source datatype is. Because we can only get here |
| 3175 | // when NumElems <= 4, this only needs to handle i32/f32/i64/f64. |
| 3176 | if (Idx == 0 && |
| 3177 | // Don't do this for i64 values on x86-32. |
| 3178 | (EVT != MVT::i64 || Subtarget->is64Bit())) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3179 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3180 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3181 | return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, |
| 3182 | Subtarget->hasSSE2(), DAG); |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3183 | } |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3184 | |
| 3185 | // Is it a vector logical left shift? |
| 3186 | if (NumElems == 2 && Idx == 1 && |
| 3187 | isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3188 | unsigned NumBits = VT.getSizeInBits(); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3189 | return getVShift(true, VT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3190 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 3191 | VT, Op.getOperand(1)), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3192 | NumBits/2, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3193 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3194 | |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3195 | if (IsAllConstants) // Otherwise, it's better to do a constpool load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3196 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 3198 | // Otherwise, if this is a vector with i32 or f32 elements, and the element |
| 3199 | // is a non-constant being inserted into an element other than the low one, |
| 3200 | // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka |
| 3201 | // movd/movss) to move this into the low element, then shuffle it into |
| 3202 | // place. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3203 | if (EVTBits == 32) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3204 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3205 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3206 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3207 | Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, |
| 3208 | Subtarget->hasSSE2(), DAG); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3209 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3210 | for (unsigned i = 0; i < NumElems; i++) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3211 | MaskVec.push_back(i == Idx ? 0 : 1); |
| 3212 | return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | |
Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 3216 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 3217 | if (Values.size() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3218 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3219 | |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 3220 | // A vector full of immediates; various special cases are already |
| 3221 | // handled, so this is best done with a single constant-pool load. |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3222 | if (IsAllConstants) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3223 | return SDValue(); |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 3224 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 3225 | // Let legalizer expand 2-wide build_vectors. |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3226 | if (EVTBits == 64) { |
| 3227 | if (NumNonZero == 1) { |
| 3228 | // One half is zero or undef. |
| 3229 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3230 | SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3231 | Op.getOperand(Idx)); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3232 | return getShuffleVectorZeroOrUndef(V2, Idx, true, |
| 3233 | Subtarget->hasSSE2(), DAG); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3234 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3235 | return SDValue(); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3236 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3237 | |
| 3238 | // If element VT is < 32 bits, convert it to inserts into a zero vector. |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 3239 | if (EVTBits == 8 && NumElems == 16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3240 | SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3241 | *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3242 | if (V.getNode()) return V; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 3245 | if (EVTBits == 16 && NumElems == 8) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3246 | SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3247 | *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3248 | if (V.getNode()) return V; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | // If element VT is == 32 bits, turn it into a number of shuffles. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3252 | SmallVector<SDValue, 8> V; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3253 | V.resize(NumElems); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3254 | if (NumElems == 4 && NumZero > 0) { |
| 3255 | for (unsigned i = 0; i < 4; ++i) { |
| 3256 | bool isZero = !(NonZeros & (1 << i)); |
| 3257 | if (isZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3258 | V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3259 | else |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3260 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | for (unsigned i = 0; i < 2; ++i) { |
| 3264 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 3265 | default: break; |
| 3266 | case 0: |
| 3267 | V[i] = V[i*2]; // Must be a zero vector. |
| 3268 | break; |
| 3269 | case 1: |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3270 | V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3271 | break; |
| 3272 | case 2: |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3273 | V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3274 | break; |
| 3275 | case 3: |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3276 | V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3277 | break; |
| 3278 | } |
| 3279 | } |
| 3280 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3281 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3282 | bool Reverse = (NonZeros & 0x3) == 2; |
| 3283 | for (unsigned i = 0; i < 2; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3284 | MaskVec.push_back(Reverse ? 1-i : i); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3285 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 3286 | for (unsigned i = 0; i < 2; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3287 | MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems); |
| 3288 | return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | if (Values.size() > 2) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3292 | // If we have SSE 4.1, Expand into a number of inserts. |
| 3293 | if (getSubtarget()->hasSSE41()) { |
| 3294 | V[0] = DAG.getUNDEF(VT); |
| 3295 | for (unsigned i = 0; i < NumElems; ++i) |
| 3296 | if (Op.getOperand(i).getOpcode() != ISD::UNDEF) |
| 3297 | V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0], |
| 3298 | Op.getOperand(i), DAG.getIntPtrConstant(i)); |
| 3299 | return V[0]; |
| 3300 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3301 | // Expand into a number of unpckl*. |
| 3302 | // e.g. for v4f32 |
| 3303 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 3304 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 3305 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3306 | for (unsigned i = 0; i < NumElems; ++i) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3307 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3308 | NumElems >>= 1; |
| 3309 | while (NumElems != 0) { |
| 3310 | for (unsigned i = 0; i < NumElems; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3311 | V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3312 | NumElems >>= 1; |
| 3313 | } |
| 3314 | return V[0]; |
| 3315 | } |
| 3316 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3317 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3320 | // v8i16 shuffles - Prefer shuffles in the following order: |
| 3321 | // 1. [all] pshuflw, pshufhw, optional move |
| 3322 | // 2. [ssse3] 1 x pshufb |
| 3323 | // 3. [ssse3] 2 x pshufb + 1 x por |
| 3324 | // 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw) |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3325 | static |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3326 | SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp, |
| 3327 | SelectionDAG &DAG, X86TargetLowering &TLI) { |
| 3328 | SDValue V1 = SVOp->getOperand(0); |
| 3329 | SDValue V2 = SVOp->getOperand(1); |
| 3330 | DebugLoc dl = SVOp->getDebugLoc(); |
| 3331 | const int *Mask = SVOp->getMask(); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3332 | SmallVector<int, 8> MaskVals; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3333 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3334 | // Determine if more than 1 of the words in each of the low and high quadwords |
| 3335 | // of the result come from the same quadword of one of the two inputs. Undef |
| 3336 | // mask values count as coming from any quadword, for better codegen. |
| 3337 | SmallVector<unsigned, 4> LoQuad(4); |
| 3338 | SmallVector<unsigned, 4> HiQuad(4); |
| 3339 | BitVector InputQuads(4); |
| 3340 | for (unsigned i = 0; i < 8; ++i) { |
| 3341 | SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3342 | int EltIdx = Mask[i]; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3343 | MaskVals.push_back(EltIdx); |
| 3344 | if (EltIdx < 0) { |
| 3345 | ++Quad[0]; |
| 3346 | ++Quad[1]; |
| 3347 | ++Quad[2]; |
| 3348 | ++Quad[3]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3349 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3350 | } |
| 3351 | ++Quad[EltIdx / 4]; |
| 3352 | InputQuads.set(EltIdx / 4); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3353 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3354 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3355 | int BestLoQuad = -1; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3356 | unsigned MaxQuad = 1; |
| 3357 | for (unsigned i = 0; i < 4; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3358 | if (LoQuad[i] > MaxQuad) { |
| 3359 | BestLoQuad = i; |
| 3360 | MaxQuad = LoQuad[i]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3361 | } |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3364 | int BestHiQuad = -1; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3365 | MaxQuad = 1; |
| 3366 | for (unsigned i = 0; i < 4; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3367 | if (HiQuad[i] > MaxQuad) { |
| 3368 | BestHiQuad = i; |
| 3369 | MaxQuad = HiQuad[i]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3373 | // For SSSE3, If all 8 words of the result come from only 1 quadword of each |
| 3374 | // of the two input vectors, shuffle them into one input vector so only a |
| 3375 | // single pshufb instruction is necessary. If There are more than 2 input |
| 3376 | // quads, disable the next transformation since it does not help SSSE3. |
| 3377 | bool V1Used = InputQuads[0] || InputQuads[1]; |
| 3378 | bool V2Used = InputQuads[2] || InputQuads[3]; |
| 3379 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3380 | if (InputQuads.count() == 2 && V1Used && V2Used) { |
| 3381 | BestLoQuad = InputQuads.find_first(); |
| 3382 | BestHiQuad = InputQuads.find_next(BestLoQuad); |
| 3383 | } |
| 3384 | if (InputQuads.count() > 2) { |
| 3385 | BestLoQuad = -1; |
| 3386 | BestHiQuad = -1; |
| 3387 | } |
| 3388 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3389 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3390 | // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update |
| 3391 | // the shuffle mask. If a quad is scored as -1, that means that it contains |
| 3392 | // words from all 4 input quadwords. |
| 3393 | SDValue NewV; |
| 3394 | if (BestLoQuad >= 0 || BestHiQuad >= 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3395 | SmallVector<int, 8> MaskV; |
| 3396 | MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad); |
| 3397 | MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad); |
| 3398 | NewV = DAG.getVectorShuffle(MVT::v2i64, dl, |
| 3399 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1), |
| 3400 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3401 | NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3402 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3403 | // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the |
| 3404 | // source words for the shuffle, to aid later transformations. |
| 3405 | bool AllWordsInNewV = true; |
Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 3406 | bool InOrder[2] = { true, true }; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3407 | for (unsigned i = 0; i != 8; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3408 | int idx = MaskVals[i]; |
Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 3409 | if (idx != (int)i) |
| 3410 | InOrder[i/4] = false; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3411 | if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3412 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3413 | AllWordsInNewV = false; |
| 3414 | break; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3415 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3416 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3417 | bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV; |
| 3418 | if (AllWordsInNewV) { |
| 3419 | for (int i = 0; i != 8; ++i) { |
| 3420 | int idx = MaskVals[i]; |
| 3421 | if (idx < 0) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3422 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3423 | idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4; |
| 3424 | if ((idx != i) && idx < 4) |
| 3425 | pshufhw = false; |
| 3426 | if ((idx != i) && idx > 3) |
| 3427 | pshuflw = false; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3428 | } |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3429 | V1 = NewV; |
| 3430 | V2Used = false; |
| 3431 | BestLoQuad = 0; |
| 3432 | BestHiQuad = 1; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3433 | } |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3434 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3435 | // If we've eliminated the use of V2, and the new mask is a pshuflw or |
| 3436 | // pshufhw, that's as cheap as it gets. Return the new shuffle. |
Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 3437 | if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3438 | return DAG.getVectorShuffle(MVT::v8i16, dl, NewV, |
| 3439 | DAG.getUNDEF(MVT::v8i16), &MaskVals[0]); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3440 | } |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3441 | } |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3442 | |
| 3443 | // If we have SSSE3, and all words of the result are from 1 input vector, |
| 3444 | // case 2 is generated, otherwise case 3 is generated. If no SSSE3 |
| 3445 | // is present, fall back to case 4. |
| 3446 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3447 | SmallVector<SDValue,16> pshufbMask; |
| 3448 | |
| 3449 | // If we have elements from both input vectors, set the high bit of the |
| 3450 | // shuffle mask element to zero out elements that come from V2 in the V1 |
| 3451 | // mask, and elements that come from V1 in the V2 mask, so that the two |
| 3452 | // results can be OR'd together. |
| 3453 | bool TwoInputs = V1Used && V2Used; |
| 3454 | for (unsigned i = 0; i != 8; ++i) { |
| 3455 | int EltIdx = MaskVals[i] * 2; |
| 3456 | if (TwoInputs && (EltIdx >= 16)) { |
| 3457 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3458 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3459 | continue; |
| 3460 | } |
| 3461 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| 3462 | pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8)); |
| 3463 | } |
| 3464 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1); |
| 3465 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3466 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3467 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3468 | if (!TwoInputs) |
| 3469 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3470 | |
| 3471 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 3472 | // OR it with the first shuffled input. |
| 3473 | pshufbMask.clear(); |
| 3474 | for (unsigned i = 0; i != 8; ++i) { |
| 3475 | int EltIdx = MaskVals[i] * 2; |
| 3476 | if (EltIdx < 16) { |
| 3477 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3478 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3479 | continue; |
| 3480 | } |
| 3481 | pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); |
| 3482 | pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8)); |
| 3483 | } |
| 3484 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2); |
| 3485 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3486 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3487 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3488 | V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| 3489 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3490 | } |
| 3491 | |
| 3492 | // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order, |
| 3493 | // and update MaskVals with new element order. |
| 3494 | BitVector InOrder(8); |
| 3495 | if (BestLoQuad >= 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3496 | SmallVector<int, 8> MaskV; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3497 | for (int i = 0; i != 4; ++i) { |
| 3498 | int idx = MaskVals[i]; |
| 3499 | if (idx < 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3500 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3501 | InOrder.set(i); |
| 3502 | } else if ((idx / 4) == BestLoQuad) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3503 | MaskV.push_back(idx & 3); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3504 | InOrder.set(i); |
| 3505 | } else { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3506 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | for (unsigned i = 4; i != 8; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3510 | MaskV.push_back(i); |
| 3511 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| 3512 | &MaskV[0]); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | // If BestHi >= 0, generate a pshufhw to put the high elements in order, |
| 3516 | // and update MaskVals with the new element order. |
| 3517 | if (BestHiQuad >= 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3518 | SmallVector<int, 8> MaskV; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3519 | for (unsigned i = 0; i != 4; ++i) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3520 | MaskV.push_back(i); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3521 | for (unsigned i = 4; i != 8; ++i) { |
| 3522 | int idx = MaskVals[i]; |
| 3523 | if (idx < 0) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3524 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3525 | InOrder.set(i); |
| 3526 | } else if ((idx / 4) == BestHiQuad) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3527 | MaskV.push_back((idx & 3) + 4); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3528 | InOrder.set(i); |
| 3529 | } else { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3530 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3531 | } |
| 3532 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3533 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| 3534 | &MaskV[0]); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | // In case BestHi & BestLo were both -1, which means each quadword has a word |
| 3538 | // from each of the four input quadwords, calculate the InOrder bitvector now |
| 3539 | // before falling through to the insert/extract cleanup. |
| 3540 | if (BestLoQuad == -1 && BestHiQuad == -1) { |
| 3541 | NewV = V1; |
| 3542 | for (int i = 0; i != 8; ++i) |
| 3543 | if (MaskVals[i] < 0 || MaskVals[i] == i) |
| 3544 | InOrder.set(i); |
| 3545 | } |
| 3546 | |
| 3547 | // The other elements are put in the right place using pextrw and pinsrw. |
| 3548 | for (unsigned i = 0; i != 8; ++i) { |
| 3549 | if (InOrder[i]) |
| 3550 | continue; |
| 3551 | int EltIdx = MaskVals[i]; |
| 3552 | if (EltIdx < 0) |
| 3553 | continue; |
| 3554 | SDValue ExtOp = (EltIdx < 8) |
| 3555 | ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1, |
| 3556 | DAG.getIntPtrConstant(EltIdx)) |
| 3557 | : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2, |
| 3558 | DAG.getIntPtrConstant(EltIdx - 8)); |
| 3559 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp, |
| 3560 | DAG.getIntPtrConstant(i)); |
| 3561 | } |
| 3562 | return NewV; |
| 3563 | } |
| 3564 | |
| 3565 | // v16i8 shuffles - Prefer shuffles in the following order: |
| 3566 | // 1. [ssse3] 1 x pshufb |
| 3567 | // 2. [ssse3] 2 x pshufb + 1 x por |
| 3568 | // 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw |
| 3569 | static |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3570 | SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp, |
| 3571 | SelectionDAG &DAG, X86TargetLowering &TLI) { |
| 3572 | SDValue V1 = SVOp->getOperand(0); |
| 3573 | SDValue V2 = SVOp->getOperand(1); |
| 3574 | DebugLoc dl = SVOp->getDebugLoc(); |
| 3575 | const int *Mask = SVOp->getMask(); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3576 | SmallVector<int, 16> MaskVals; |
| 3577 | |
| 3578 | // If we have SSSE3, case 1 is generated when all result bytes come from |
| 3579 | // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is |
| 3580 | // present, fall back to case 3. |
| 3581 | // FIXME: kill V2Only once shuffles are canonizalized by getNode. |
| 3582 | bool V1Only = true; |
| 3583 | bool V2Only = true; |
| 3584 | for (unsigned i = 0; i < 16; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3585 | int EltIdx = Mask[i]; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3586 | MaskVals.push_back(EltIdx); |
| 3587 | if (EltIdx < 0) |
| 3588 | continue; |
| 3589 | if (EltIdx < 16) |
| 3590 | V2Only = false; |
| 3591 | else |
| 3592 | V1Only = false; |
| 3593 | } |
| 3594 | |
| 3595 | // If SSSE3, use 1 pshufb instruction per vector with elements in the result. |
| 3596 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3597 | SmallVector<SDValue,16> pshufbMask; |
| 3598 | |
| 3599 | // If all result elements are from one input vector, then only translate |
| 3600 | // undef mask values to 0x80 (zero out result) in the pshufb mask. |
| 3601 | // |
| 3602 | // Otherwise, we have elements from both input vectors, and must zero out |
| 3603 | // elements that come from V2 in the first mask, and V1 in the second mask |
| 3604 | // so that we can OR them together. |
| 3605 | bool TwoInputs = !(V1Only || V2Only); |
| 3606 | for (unsigned i = 0; i != 16; ++i) { |
| 3607 | int EltIdx = MaskVals[i]; |
| 3608 | if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) { |
| 3609 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3610 | continue; |
| 3611 | } |
| 3612 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| 3613 | } |
| 3614 | // If all the elements are from V2, assign it to V1 and return after |
| 3615 | // building the first pshufb. |
| 3616 | if (V2Only) |
| 3617 | V1 = V2; |
| 3618 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3619 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3620 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3621 | if (!TwoInputs) |
| 3622 | return V1; |
| 3623 | |
| 3624 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 3625 | // OR it with the first shuffled input. |
| 3626 | pshufbMask.clear(); |
| 3627 | for (unsigned i = 0; i != 16; ++i) { |
| 3628 | int EltIdx = MaskVals[i]; |
| 3629 | if (EltIdx < 16) { |
| 3630 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3631 | continue; |
| 3632 | } |
| 3633 | pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); |
| 3634 | } |
| 3635 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3636 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3637 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3638 | return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| 3639 | } |
| 3640 | |
| 3641 | // No SSSE3 - Calculate in place words and then fix all out of place words |
| 3642 | // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from |
| 3643 | // the 16 different words that comprise the two doublequadword input vectors. |
| 3644 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3645 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2); |
| 3646 | SDValue NewV = V2Only ? V2 : V1; |
| 3647 | for (int i = 0; i != 8; ++i) { |
| 3648 | int Elt0 = MaskVals[i*2]; |
| 3649 | int Elt1 = MaskVals[i*2+1]; |
| 3650 | |
| 3651 | // This word of the result is all undef, skip it. |
| 3652 | if (Elt0 < 0 && Elt1 < 0) |
| 3653 | continue; |
| 3654 | |
| 3655 | // This word of the result is already in the correct place, skip it. |
| 3656 | if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1)) |
| 3657 | continue; |
| 3658 | if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17)) |
| 3659 | continue; |
| 3660 | |
| 3661 | SDValue Elt0Src = Elt0 < 16 ? V1 : V2; |
| 3662 | SDValue Elt1Src = Elt1 < 16 ? V1 : V2; |
| 3663 | SDValue InsElt; |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3664 | |
| 3665 | // If Elt0 and Elt1 are defined, are consecutive, and can be load |
| 3666 | // using a single extract together, load it and store it. |
| 3667 | if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) { |
| 3668 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| 3669 | DAG.getIntPtrConstant(Elt1 / 2)); |
| 3670 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| 3671 | DAG.getIntPtrConstant(i)); |
| 3672 | continue; |
| 3673 | } |
| 3674 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3675 | // If Elt1 is defined, extract it from the appropriate source. If the |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3676 | // source byte is not also odd, shift the extracted word left 8 bits |
| 3677 | // otherwise clear the bottom 8 bits if we need to do an or. |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3678 | if (Elt1 >= 0) { |
| 3679 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| 3680 | DAG.getIntPtrConstant(Elt1 / 2)); |
| 3681 | if ((Elt1 & 1) == 0) |
| 3682 | InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt, |
| 3683 | DAG.getConstant(8, TLI.getShiftAmountTy())); |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3684 | else if (Elt0 >= 0) |
| 3685 | InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt, |
| 3686 | DAG.getConstant(0xFF00, MVT::i16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3687 | } |
| 3688 | // If Elt0 is defined, extract it from the appropriate source. If the |
| 3689 | // source byte is not also even, shift the extracted word right 8 bits. If |
| 3690 | // Elt1 was also defined, OR the extracted values together before |
| 3691 | // inserting them in the result. |
| 3692 | if (Elt0 >= 0) { |
| 3693 | SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, |
| 3694 | Elt0Src, DAG.getIntPtrConstant(Elt0 / 2)); |
| 3695 | if ((Elt0 & 1) != 0) |
| 3696 | InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0, |
| 3697 | DAG.getConstant(8, TLI.getShiftAmountTy())); |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3698 | else if (Elt1 >= 0) |
| 3699 | InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0, |
| 3700 | DAG.getConstant(0x00FF, MVT::i16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3701 | InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0) |
| 3702 | : InsElt0; |
| 3703 | } |
| 3704 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| 3705 | DAG.getIntPtrConstant(i)); |
| 3706 | } |
| 3707 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3710 | /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide |
| 3711 | /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be |
| 3712 | /// done when every pair / quad of shuffle mask elements point to elements in |
| 3713 | /// the right sequence. e.g. |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3714 | /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15> |
| 3715 | static |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3716 | SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp, |
| 3717 | SelectionDAG &DAG, |
| 3718 | TargetLowering &TLI, DebugLoc dl) { |
| 3719 | MVT VT = SVOp->getValueType(0); |
| 3720 | SDValue V1 = SVOp->getOperand(0); |
| 3721 | SDValue V2 = SVOp->getOperand(1); |
| 3722 | const int *PermMask = SVOp->getMask(); |
| 3723 | unsigned NumElems = VT.getVectorNumElements(); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3724 | unsigned NewWidth = (NumElems == 4) ? 2 : 4; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3725 | MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3726 | MVT MaskEltVT = MaskVT.getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3727 | MVT NewVT = MaskVT; |
| 3728 | switch (VT.getSimpleVT()) { |
| 3729 | default: assert(false && "Unexpected!"); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3730 | case MVT::v4f32: NewVT = MVT::v2f64; break; |
| 3731 | case MVT::v4i32: NewVT = MVT::v2i64; break; |
| 3732 | case MVT::v8i16: NewVT = MVT::v4i32; break; |
| 3733 | case MVT::v16i8: NewVT = MVT::v4i32; break; |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 3736 | if (NewWidth == 2) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3737 | if (VT.isInteger()) |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3738 | NewVT = MVT::v2i64; |
| 3739 | else |
| 3740 | NewVT = MVT::v2f64; |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 3741 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3742 | int Scale = NumElems / NewWidth; |
| 3743 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3744 | for (unsigned i = 0; i < NumElems; i += Scale) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3745 | int StartIdx = -1; |
| 3746 | for (int j = 0; j < Scale; ++j) { |
| 3747 | int EltIdx = PermMask[i+j]; |
| 3748 | if (EltIdx < 0) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3749 | continue; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3750 | if (StartIdx == -1) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3751 | StartIdx = EltIdx - (EltIdx % Scale); |
| 3752 | if (EltIdx != StartIdx + j) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3753 | return SDValue(); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3754 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3755 | if (StartIdx == -1) |
| 3756 | MaskVec.push_back(-1); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3757 | else |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3758 | MaskVec.push_back(StartIdx / Scale); |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3761 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1); |
| 3762 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3763 | return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]); |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 3766 | /// getVZextMovL - Return a zero-extending vector move low node. |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3767 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3768 | static SDValue getVZextMovL(MVT VT, MVT OpVT, |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3769 | SDValue SrcOp, SelectionDAG &DAG, |
| 3770 | const X86Subtarget *Subtarget, DebugLoc dl) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3771 | if (VT == MVT::v2f64 || VT == MVT::v4f32) { |
| 3772 | LoadSDNode *LD = NULL; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3773 | if (!isScalarLoadToVector(SrcOp.getNode(), &LD)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3774 | LD = dyn_cast<LoadSDNode>(SrcOp); |
| 3775 | if (!LD) { |
| 3776 | // movssrr and movsdrr do not clear top bits. Try to use movd, movq |
| 3777 | // instead. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3778 | MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3779 | if ((EVT != MVT::i64 || Subtarget->is64Bit()) && |
| 3780 | SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 3781 | SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT && |
| 3782 | SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) { |
| 3783 | // PR2108 |
| 3784 | OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3785 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3786 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
| 3787 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| 3788 | OpVT, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 3789 | SrcOp.getOperand(0) |
| 3790 | .getOperand(0)))); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3791 | } |
| 3792 | } |
| 3793 | } |
| 3794 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3795 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3796 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3797 | DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3798 | OpVT, SrcOp))); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3801 | /// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of |
| 3802 | /// shuffles. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3803 | static SDValue |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3804 | LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| 3805 | SDValue V1 = SVOp->getOperand(0); |
| 3806 | SDValue V2 = SVOp->getOperand(1); |
| 3807 | DebugLoc dl = SVOp->getDebugLoc(); |
| 3808 | MVT VT = SVOp->getValueType(0); |
| 3809 | const int *PermMaskPtr = SVOp->getMask(); |
| 3810 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3811 | SmallVector<std::pair<int, int>, 8> Locs; |
Rafael Espindola | 833a990 | 2008-08-28 18:32:53 +0000 | [diff] [blame] | 3812 | Locs.resize(4); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3813 | SmallVector<int, 8> Mask1(4U, -1); |
| 3814 | SmallVector<int, 8> PermMask; |
| 3815 | |
| 3816 | for (unsigned i = 0; i != 8; ++i) |
| 3817 | PermMask.push_back(PermMaskPtr[i]); |
| 3818 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3819 | unsigned NumHi = 0; |
| 3820 | unsigned NumLo = 0; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3821 | for (unsigned i = 0; i != 4; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3822 | int Idx = PermMask[i]; |
| 3823 | if (Idx < 0) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3824 | Locs[i] = std::make_pair(-1, -1); |
| 3825 | } else { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3826 | assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!"); |
| 3827 | if (Idx < 4) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3828 | Locs[i] = std::make_pair(0, NumLo); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3829 | Mask1[NumLo] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3830 | NumLo++; |
| 3831 | } else { |
| 3832 | Locs[i] = std::make_pair(1, NumHi); |
| 3833 | if (2+NumHi < 4) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3834 | Mask1[2+NumHi] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3835 | NumHi++; |
| 3836 | } |
| 3837 | } |
| 3838 | } |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3839 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3840 | if (NumLo <= 2 && NumHi <= 2) { |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3841 | // If no more than two elements come from either vector. This can be |
| 3842 | // implemented with two shuffles. First shuffle gather the elements. |
| 3843 | // The second shuffle, which takes the first shuffle as both of its |
| 3844 | // vector operands, put the elements into the right order. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3845 | V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3846 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3847 | SmallVector<int, 8> Mask2(4U, -1); |
| 3848 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3849 | for (unsigned i = 0; i != 4; ++i) { |
| 3850 | if (Locs[i].first == -1) |
| 3851 | continue; |
| 3852 | else { |
| 3853 | unsigned Idx = (i < 2) ? 0 : 4; |
| 3854 | Idx += Locs[i].first * 2 + Locs[i].second; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3855 | Mask2[i] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3859 | return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3860 | } else if (NumLo == 3 || NumHi == 3) { |
| 3861 | // Otherwise, we must have three elements from one vector, call it X, and |
| 3862 | // one element from the other, call it Y. First, use a shufps to build an |
| 3863 | // intermediate vector with the one element from Y and the element from X |
| 3864 | // that will be in the same half in the final destination (the indexes don't |
| 3865 | // matter). Then, use a shufps to build the final vector, taking the half |
| 3866 | // containing the element from Y from the intermediate, and the other half |
| 3867 | // from X. |
| 3868 | if (NumHi == 3) { |
| 3869 | // Normalize it so the 3 elements come from V1. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3870 | CommuteVectorShuffleMask(PermMask, VT); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3871 | std::swap(V1, V2); |
| 3872 | } |
| 3873 | |
| 3874 | // Find the element from V2. |
| 3875 | unsigned HiIndex; |
| 3876 | for (HiIndex = 0; HiIndex < 3; ++HiIndex) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3877 | int Val = PermMask[HiIndex]; |
| 3878 | if (Val < 0) |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3879 | continue; |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3880 | if (Val >= 4) |
| 3881 | break; |
| 3882 | } |
| 3883 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3884 | Mask1[0] = PermMask[HiIndex]; |
| 3885 | Mask1[1] = -1; |
| 3886 | Mask1[2] = PermMask[HiIndex^1]; |
| 3887 | Mask1[3] = -1; |
| 3888 | V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (HiIndex >= 2) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3891 | Mask1[0] = PermMask[0]; |
| 3892 | Mask1[1] = PermMask[1]; |
| 3893 | Mask1[2] = HiIndex & 1 ? 6 : 4; |
| 3894 | Mask1[3] = HiIndex & 1 ? 4 : 6; |
| 3895 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3896 | } else { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3897 | Mask1[0] = HiIndex & 1 ? 2 : 0; |
| 3898 | Mask1[1] = HiIndex & 1 ? 0 : 2; |
| 3899 | Mask1[2] = PermMask[2]; |
| 3900 | Mask1[3] = PermMask[3]; |
| 3901 | if (Mask1[2] >= 0) |
| 3902 | Mask1[2] += 4; |
| 3903 | if (Mask1[3] >= 0) |
| 3904 | Mask1[3] += 4; |
| 3905 | return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3906 | } |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 3910 | Locs.clear(); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3911 | SmallVector<int,8> LoMask(4U, -1); |
| 3912 | SmallVector<int,8> HiMask(4U, -1); |
| 3913 | |
| 3914 | SmallVector<int,8> *MaskPtr = &LoMask; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3915 | unsigned MaskIdx = 0; |
| 3916 | unsigned LoIdx = 0; |
| 3917 | unsigned HiIdx = 2; |
| 3918 | for (unsigned i = 0; i != 4; ++i) { |
| 3919 | if (i == 2) { |
| 3920 | MaskPtr = &HiMask; |
| 3921 | MaskIdx = 1; |
| 3922 | LoIdx = 0; |
| 3923 | HiIdx = 2; |
| 3924 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3925 | int Idx = PermMask[i]; |
| 3926 | if (Idx < 0) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3927 | Locs[i] = std::make_pair(-1, -1); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3928 | } else if (Idx < 4) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3929 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3930 | (*MaskPtr)[LoIdx] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3931 | LoIdx++; |
| 3932 | } else { |
| 3933 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3934 | (*MaskPtr)[HiIdx] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3935 | HiIdx++; |
| 3936 | } |
| 3937 | } |
| 3938 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3939 | SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]); |
| 3940 | SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]); |
| 3941 | SmallVector<int, 8> MaskOps; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3942 | for (unsigned i = 0; i != 4; ++i) { |
| 3943 | if (Locs[i].first == -1) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3944 | MaskOps.push_back(-1); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | unsigned Idx = Locs[i].first * 4 + Locs[i].second; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3947 | MaskOps.push_back(Idx); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3948 | } |
| 3949 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3950 | return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3953 | SDValue |
| 3954 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3955 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3956 | SDValue V1 = Op.getOperand(0); |
| 3957 | SDValue V2 = Op.getOperand(1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3958 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3959 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3960 | const int *PermMask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask(); |
| 3961 | unsigned NumElems = VT.getVectorNumElements(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3962 | bool isMMX = VT.getSizeInBits() == 64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3963 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 3964 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 3965 | bool V1IsSplat = false; |
| 3966 | bool V2IsSplat = false; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3967 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3968 | if (isZeroShuffle(SVOp)) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3969 | return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 3970 | |
Evan Cheng | 4dcc8a3 | 2008-09-25 23:35:16 +0000 | [diff] [blame] | 3971 | // Canonicalize movddup shuffles. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3972 | if (V2IsUndef && Subtarget->hasSSE2() && VT.getSizeInBits() == 128 && |
| 3973 | X86::isMOVDDUPMask(SVOp)) |
| 3974 | return CanonicalizeMovddup(SVOp, DAG, Subtarget->hasSSE3()); |
Evan Cheng | 4dcc8a3 | 2008-09-25 23:35:16 +0000 | [diff] [blame] | 3975 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3976 | // Promote splats to v4f32. |
| 3977 | if (SVOp->isSplat()) { |
| 3978 | if (isMMX || NumElems < 4) |
| 3979 | return Op; |
| 3980 | return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3983 | // If the shuffle can be profitably rewritten as a narrower shuffle, then |
| 3984 | // do it! |
| 3985 | if (VT == MVT::v8i16 || VT == MVT::v16i8) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3986 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3987 | if (NewOp.getNode()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3988 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3989 | LowerVECTOR_SHUFFLE(NewOp, DAG)); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3990 | } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) { |
| 3991 | // FIXME: Figure out a cleaner way to do this. |
| 3992 | // Try to make use of movq to zero out the top part. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3993 | if (ISD::isBuildVectorAllZeros(V2.getNode())) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3994 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3995 | if (NewOp.getNode()) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 3996 | if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false)) |
| 3997 | return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0), |
| 3998 | DAG, Subtarget, dl); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3999 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4000 | } else if (ISD::isBuildVectorAllZeros(V1.getNode())) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4001 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
| 4002 | if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp))) |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 4003 | return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1), |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4004 | DAG, Subtarget, dl); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 4005 | } |
| 4006 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4007 | |
| 4008 | if (X86::isPSHUFDMask(SVOp)) |
| 4009 | return Op; |
| 4010 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4011 | // Check if this can be converted into a logical shift. |
| 4012 | bool isLeft = false; |
| 4013 | unsigned ShAmt = 0; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4014 | SDValue ShVal; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4015 | bool isShift = getSubtarget()->hasSSE2() && |
| 4016 | isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4017 | if (isShift && ShVal.hasOneUse()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4018 | // If the shifted value has multiple uses, it may be cheaper to use |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4019 | // v_set0 + movlhps or movhlps, etc. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4020 | MVT EVT = VT.getVectorElementType(); |
| 4021 | ShAmt *= EVT.getSizeInBits(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4022 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4023 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4024 | |
| 4025 | if (X86::isMOVLMask(SVOp)) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 4026 | if (V1IsUndef) |
| 4027 | return V2; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4028 | if (ISD::isBuildVectorAllZeros(V1.getNode())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4029 | return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 4030 | if (!isMMX) |
| 4031 | return Op; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 4032 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4033 | |
| 4034 | // FIXME: fold these into legal mask. |
| 4035 | if (!isMMX && (X86::isMOVSHDUPMask(SVOp) || |
| 4036 | X86::isMOVSLDUPMask(SVOp) || |
| 4037 | X86::isMOVHLPSMask(SVOp) || |
| 4038 | X86::isMOVHPMask(SVOp) || |
| 4039 | X86::isMOVLPMask(SVOp))) |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4040 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4041 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4042 | if (ShouldXformToMOVHLPS(SVOp) || |
| 4043 | ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp)) |
| 4044 | return CommuteVectorShuffle(SVOp, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4045 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4046 | if (isShift) { |
| 4047 | // No better options. Use a vshl / vsrl. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4048 | MVT EVT = VT.getVectorElementType(); |
| 4049 | ShAmt *= EVT.getSizeInBits(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4050 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4051 | } |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4052 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4053 | bool Commuted = false; |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4054 | // FIXME: This should also accept a bitcast of a splat? Be careful, not |
| 4055 | // 1,1,1,1 -> v8i16 though. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4056 | V1IsSplat = isSplatVector(V1.getNode()); |
| 4057 | V2IsSplat = isSplatVector(V2.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4058 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4059 | // Canonicalize the splat or undef, if present, to be on the RHS. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4060 | if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4061 | Op = CommuteVectorShuffle(SVOp, DAG); |
| 4062 | SVOp = cast<ShuffleVectorSDNode>(Op); |
| 4063 | V1 = SVOp->getOperand(0); |
| 4064 | V2 = SVOp->getOperand(1); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4065 | std::swap(V1IsSplat, V2IsSplat); |
| 4066 | std::swap(V1IsUndef, V2IsUndef); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4067 | Commuted = true; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4070 | if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) { |
| 4071 | // Shuffling low element of v1 into undef, just return v1. |
| 4072 | if (V2IsUndef) |
| 4073 | return V1; |
| 4074 | // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which |
| 4075 | // the instruction selector will not match, so get a canonical MOVL with |
| 4076 | // swapped operands to undo the commute. |
| 4077 | return getMOVL(DAG, dl, VT, V2, V1); |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 4078 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4079 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4080 | if (X86::isUNPCKL_v_undef_Mask(SVOp) || |
| 4081 | X86::isUNPCKH_v_undef_Mask(SVOp) || |
| 4082 | X86::isUNPCKLMask(SVOp) || |
| 4083 | X86::isUNPCKHMask(SVOp)) |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 4084 | return Op; |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 4085 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4086 | if (V2IsSplat) { |
| 4087 | // Normalize mask so all entries that point to V2 points to its first |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4088 | // element then try to match unpck{h|l} again. If match, return a |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4089 | // new vector_shuffle with the corrected mask. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4090 | SDValue NewMask = NormalizeMask(SVOp, DAG); |
| 4091 | ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask); |
| 4092 | if (NSVOp != SVOp) { |
| 4093 | if (X86::isUNPCKLMask(NSVOp, true)) { |
| 4094 | return NewMask; |
| 4095 | } else if (X86::isUNPCKHMask(NSVOp, true)) { |
| 4096 | return NewMask; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4097 | } |
| 4098 | } |
| 4099 | } |
| 4100 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4101 | if (Commuted) { |
| 4102 | // Commute is back and try unpck* again. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4103 | // FIXME: this seems wrong. |
| 4104 | SDValue NewOp = CommuteVectorShuffle(SVOp, DAG); |
| 4105 | ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp); |
| 4106 | if (X86::isUNPCKL_v_undef_Mask(NewSVOp) || |
| 4107 | X86::isUNPCKH_v_undef_Mask(NewSVOp) || |
| 4108 | X86::isUNPCKLMask(NewSVOp) || |
| 4109 | X86::isUNPCKHMask(NewSVOp)) |
| 4110 | return NewOp; |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4111 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4112 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4113 | // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4114 | |
| 4115 | // Normalize the node to match x86 shuffle ops if needed |
| 4116 | if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp)) |
| 4117 | return CommuteVectorShuffle(SVOp, DAG); |
| 4118 | |
| 4119 | // Check for legal shuffle and return? |
| 4120 | if (isShuffleMaskLegal(PermMask, VT)) |
Evan Cheng | 0c0f83f | 2008-04-05 00:30:36 +0000 | [diff] [blame] | 4121 | return Op; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4122 | |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4123 | // Handle v8i16 specifically since SSE can do byte extraction and insertion. |
| 4124 | if (VT == MVT::v8i16) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4125 | SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4126 | if (NewOp.getNode()) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4127 | return NewOp; |
| 4128 | } |
| 4129 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4130 | if (VT == MVT::v16i8) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4131 | SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4132 | if (NewOp.getNode()) |
| 4133 | return NewOp; |
| 4134 | } |
| 4135 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4136 | // Handle all 4 wide cases with a number of shuffles except for MMX. |
| 4137 | if (NumElems == 4 && !isMMX) |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4138 | return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4139 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4140 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4143 | SDValue |
| 4144 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4145 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4146 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4147 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4148 | if (VT.getSizeInBits() == 8) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4149 | SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4150 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4151 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4152 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4153 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4154 | } else if (VT.getSizeInBits() == 16) { |
Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 4155 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 4156 | // If Idx is 0, it's cheaper to do a move instead of a pextrw. |
| 4157 | if (Idx == 0) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4158 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 4159 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
| 4160 | DAG.getNode(ISD::BIT_CONVERT, dl, |
| 4161 | MVT::v4i32, |
Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 4162 | Op.getOperand(0)), |
| 4163 | Op.getOperand(1))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4164 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4165 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4166 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4167 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4168 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4169 | } else if (VT == MVT::f32) { |
| 4170 | // EXTRACTPS outputs to a GPR32 register which will require a movd to copy |
| 4171 | // the result back to FR32 register. It's only worth matching if the |
Dan Gohman | d17cfbe | 2008-10-31 00:57:24 +0000 | [diff] [blame] | 4172 | // result has a single use which is a store or a bitcast to i32. And in |
| 4173 | // the case of a store, it's not worth it if the index is a constant 0, |
| 4174 | // because a MOVSSmr can be used instead, which is smaller and faster. |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4175 | if (!Op.hasOneUse()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4176 | return SDValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4177 | SDNode *User = *Op.getNode()->use_begin(); |
Dan Gohman | d17cfbe | 2008-10-31 00:57:24 +0000 | [diff] [blame] | 4178 | if ((User->getOpcode() != ISD::STORE || |
| 4179 | (isa<ConstantSDNode>(Op.getOperand(1)) && |
| 4180 | cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) && |
Dan Gohman | 171c11e | 2008-04-16 02:32:24 +0000 | [diff] [blame] | 4181 | (User->getOpcode() != ISD::BIT_CONVERT || |
| 4182 | User->getValueType(0) != MVT::i32)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4183 | return SDValue(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4184 | SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4185 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4186 | Op.getOperand(0)), |
| 4187 | Op.getOperand(1)); |
| 4188 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 4189 | } else if (VT == MVT::i32) { |
| 4190 | // ExtractPS works with constant index. |
| 4191 | if (isa<ConstantSDNode>(Op.getOperand(1))) |
| 4192 | return Op; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4193 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4194 | return SDValue(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4198 | SDValue |
| 4199 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4200 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4201 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4202 | |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4203 | if (Subtarget->hasSSE41()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4204 | SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4205 | if (Res.getNode()) |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4206 | return Res; |
| 4207 | } |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4208 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4209 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4210 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4211 | // TODO: handle v16i8. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4212 | if (VT.getSizeInBits() == 16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4213 | SDValue Vec = Op.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4214 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4215 | if (Idx == 0) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4216 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 4217 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4218 | DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4219 | MVT::v4i32, Vec), |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4220 | Op.getOperand(1))); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4221 | // Transform it so it match pextrw which produces a 32-bit result. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4222 | MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4223 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4224 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4225 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4226 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4227 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4228 | } else if (VT.getSizeInBits() == 32) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4229 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4230 | if (Idx == 0) |
| 4231 | return Op; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4232 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4233 | // SHUFPS the element to the lowest double word, then movss. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4234 | int Mask[4] = { Idx, -1, -1, -1 }; |
| 4235 | MVT VVT = Op.getOperand(0).getValueType(); |
| 4236 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| 4237 | DAG.getUNDEF(VVT), Mask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4238 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4239 | DAG.getIntPtrConstant(0)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4240 | } else if (VT.getSizeInBits() == 64) { |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4241 | // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b |
| 4242 | // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught |
| 4243 | // to match extract_elt for f64. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4244 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4245 | if (Idx == 0) |
| 4246 | return Op; |
| 4247 | |
| 4248 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 4249 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 4250 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4251 | int Mask[2] = { 1, -1 }; |
| 4252 | MVT VVT = Op.getOperand(0).getValueType(); |
| 4253 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| 4254 | DAG.getUNDEF(VVT), Mask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4255 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4256 | DAG.getIntPtrConstant(0)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4259 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4262 | SDValue |
| 4263 | X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){ |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4264 | MVT VT = Op.getValueType(); |
| 4265 | MVT EVT = VT.getVectorElementType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4266 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4267 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4268 | SDValue N0 = Op.getOperand(0); |
| 4269 | SDValue N1 = Op.getOperand(1); |
| 4270 | SDValue N2 = Op.getOperand(2); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4271 | |
Dan Gohman | ef521f1 | 2008-08-14 22:53:18 +0000 | [diff] [blame] | 4272 | if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) && |
| 4273 | isa<ConstantSDNode>(N2)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4274 | unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4275 | : X86ISD::PINSRW; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4276 | // Transform it so it match pinsr{b,w} which expects a GR32 as its second |
| 4277 | // argument. |
| 4278 | if (N1.getValueType() != MVT::i32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4279 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4280 | if (N2.getValueType() != MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4281 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4282 | return DAG.getNode(Opc, dl, VT, N0, N1, N2); |
Dan Gohman | c0573b1 | 2008-08-14 22:43:26 +0000 | [diff] [blame] | 4283 | } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) { |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4284 | // Bits [7:6] of the constant are the source select. This will always be |
| 4285 | // zero here. The DAG Combiner may combine an extract_elt index into these |
| 4286 | // bits. For example (insert (extract, 3), 2) could be matched by putting |
| 4287 | // the '3' into bits [7:6] of X86ISD::INSERTPS. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4288 | // Bits [5:4] of the constant are the destination select. This is the |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4289 | // value of the incoming immediate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4290 | // Bits [3:0] of the constant are the zero mask. The DAG Combiner may |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4291 | // combine either bitwise AND or insert of float 0.0 to set these bits. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4292 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4293 | return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 4294 | } else if (EVT == MVT::i32) { |
| 4295 | // InsertPS works with constant index. |
| 4296 | if (isa<ConstantSDNode>(N2)) |
| 4297 | return Op; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4298 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4299 | return SDValue(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4302 | SDValue |
| 4303 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4304 | MVT VT = Op.getValueType(); |
| 4305 | MVT EVT = VT.getVectorElementType(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4306 | |
| 4307 | if (Subtarget->hasSSE41()) |
| 4308 | return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG); |
| 4309 | |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4310 | if (EVT == MVT::i8) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4311 | return SDValue(); |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4312 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4313 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4314 | SDValue N0 = Op.getOperand(0); |
| 4315 | SDValue N1 = Op.getOperand(1); |
| 4316 | SDValue N2 = Op.getOperand(2); |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4317 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4318 | if (EVT.getSizeInBits() == 16) { |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4319 | // Transform it so it match pinsrw which expects a 16-bit value in a GR32 |
| 4320 | // as its second argument. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4321 | if (N1.getValueType() != MVT::i32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4322 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4323 | if (N2.getValueType() != MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4324 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4325 | return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4326 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4327 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4330 | SDValue |
| 4331 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4332 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 4333 | if (Op.getValueType() == MVT::v2f32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4334 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32, |
| 4335 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32, |
| 4336 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 4337 | Op.getOperand(0)))); |
| 4338 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4339 | SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4340 | MVT VT = MVT::v2i32; |
| 4341 | switch (Op.getValueType().getSimpleVT()) { |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 4342 | default: break; |
| 4343 | case MVT::v16i8: |
| 4344 | case MVT::v8i16: |
| 4345 | VT = MVT::v4i32; |
| 4346 | break; |
| 4347 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4348 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), |
| 4349 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4352 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 4353 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 4354 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 4355 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 4356 | // be used to form addressing mode. These wrapped nodes will be selected |
| 4357 | // into MOV32ri. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4358 | SDValue |
| 4359 | X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4360 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4361 | // FIXME there isn't really any debug info here, should come from the parent |
| 4362 | DebugLoc dl = CP->getDebugLoc(); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4363 | SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(), |
| 4364 | CP->getAlignment()); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4365 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4366 | // With PIC, the address is actually $g + Offset. |
| 4367 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 4368 | !Subtarget->isPICStyleRIPRel()) { |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4369 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4370 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 4371 | DebugLoc::getUnknownLoc(), |
| 4372 | getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4373 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | return Result; |
| 4377 | } |
| 4378 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4379 | SDValue |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4380 | X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4381 | int64_t Offset, |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4382 | SelectionDAG &DAG) const { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4383 | bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_; |
| 4384 | bool ExtraLoadRequired = |
| 4385 | Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false); |
| 4386 | |
| 4387 | // Create the TargetGlobalAddress node, folding in the constant |
| 4388 | // offset if it is legal. |
| 4389 | SDValue Result; |
Dan Gohman | 4401361 | 2008-10-21 03:38:42 +0000 | [diff] [blame] | 4390 | if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4391 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset); |
| 4392 | Offset = 0; |
| 4393 | } else |
| 4394 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4395 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4396 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4397 | // With PIC, the address is actually $g + Offset. |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4398 | if (IsPic && !Subtarget->isPICStyleRIPRel()) { |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4399 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 4400 | DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4401 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4402 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4403 | |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 4404 | // For Darwin & Mingw32, external and weak symbols are indirect, so we want to |
| 4405 | // load the value at address GV, not the value of GV itself. This means that |
| 4406 | // the GlobalAddress must be in the base or index register of the address, not |
| 4407 | // the GV offset field. Platform check is inside GVRequiresExtraLoad() call |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4408 | // The same applies for external symbols during PIC codegen |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4409 | if (ExtraLoadRequired) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4410 | Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4411 | PseudoSourceValue::getGOT(), 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4412 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4413 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 4414 | // addition for it. |
| 4415 | if (Offset != 0) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4416 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4417 | DAG.getConstant(Offset, getPointerTy())); |
| 4418 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4419 | return Result; |
| 4420 | } |
| 4421 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4422 | SDValue |
| 4423 | X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) { |
| 4424 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4425 | int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4426 | return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG); |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4429 | static SDValue |
| 4430 | GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA, |
| 4431 | SDValue *InFlag) { |
| 4432 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4433 | DebugLoc dl = GA->getDebugLoc(); |
| 4434 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 4435 | GA->getValueType(0), |
| 4436 | GA->getOffset()); |
| 4437 | if (InFlag) { |
| 4438 | SDValue Ops[] = { Chain, TGA, *InFlag }; |
| 4439 | return DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3); |
| 4440 | } else { |
| 4441 | SDValue Ops[] = { Chain, TGA }; |
| 4442 | return DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2); |
| 4443 | } |
| 4444 | } |
| 4445 | |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4446 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4447 | static SDValue |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4448 | LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4449 | const MVT PtrVT) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4450 | SDValue InFlag; |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 4451 | DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better |
| 4452 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4453 | DAG.getNode(X86ISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4454 | DebugLoc::getUnknownLoc(), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4455 | PtrVT), InFlag); |
| 4456 | InFlag = Chain.getValue(1); |
| 4457 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4458 | Chain = GetTLSADDR(DAG, Chain, GA, &InFlag); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4459 | InFlag = Chain.getValue(1); |
| 4460 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4461 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4462 | SDValue Ops1[] = { Chain, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4463 | DAG.getTargetExternalSymbol("___tls_get_addr", |
| 4464 | PtrVT), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4465 | DAG.getRegister(X86::EAX, PtrVT), |
| 4466 | DAG.getRegister(X86::EBX, PtrVT), |
| 4467 | InFlag }; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4468 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops1, 5); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4469 | InFlag = Chain.getValue(1); |
| 4470 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 4471 | return DAG.getCopyFromReg(Chain, dl, X86::EAX, PtrVT, InFlag); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4474 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4475 | static SDValue |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4476 | LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4477 | const MVT PtrVT) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4478 | SDValue InFlag, Chain; |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 4479 | DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4480 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4481 | Chain = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4482 | InFlag = Chain.getValue(1); |
| 4483 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4484 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4485 | SDValue Ops1[] = { Chain, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4486 | DAG.getTargetExternalSymbol("__tls_get_addr", |
| 4487 | PtrVT), |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4488 | DAG.getRegister(X86::RDI, PtrVT), |
| 4489 | InFlag }; |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4490 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops1, 4); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4491 | InFlag = Chain.getValue(1); |
| 4492 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 4493 | return DAG.getCopyFromReg(Chain, dl, X86::RAX, PtrVT, InFlag); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4496 | // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or |
| 4497 | // "local exec" model. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4498 | static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4499 | const MVT PtrVT, TLSModel::Model model, |
| 4500 | bool is64Bit) { |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4501 | DebugLoc dl = GA->getDebugLoc(); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4502 | // Get the Thread Pointer |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 4503 | SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress, |
| 4504 | DebugLoc::getUnknownLoc(), PtrVT, |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4505 | DAG.getRegister(is64Bit? X86::FS : X86::GS, |
| 4506 | MVT::i32)); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 4507 | |
| 4508 | SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base, |
| 4509 | NULL, 0); |
| 4510 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4511 | // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial |
| 4512 | // exec) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4513 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4514 | GA->getValueType(0), |
| 4515 | GA->getOffset()); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4516 | SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA); |
Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 4517 | |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4518 | if (model == TLSModel::InitialExec) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4519 | Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4520 | PseudoSourceValue::getGOT(), 0); |
Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 4521 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4522 | // The address of the thread local variable is the add of the thread |
| 4523 | // pointer with the offset of the variable. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4524 | return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4527 | SDValue |
| 4528 | X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) { |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4529 | // TODO: implement the "local dynamic" model |
Lauro Ramos Venancio | 2c5c111 | 2007-04-21 20:56:26 +0000 | [diff] [blame] | 4530 | // TODO: implement the "initial exec"model for pic executables |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4531 | assert(Subtarget->isTargetELF() && |
| 4532 | "TLS not implemented for non-ELF targets"); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4533 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4534 | GlobalValue *GV = GA->getGlobal(); |
| 4535 | TLSModel::Model model = |
| 4536 | getTLSModel (GV, getTargetMachine().getRelocationModel()); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4537 | if (Subtarget->is64Bit()) { |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4538 | switch (model) { |
| 4539 | case TLSModel::GeneralDynamic: |
| 4540 | case TLSModel::LocalDynamic: // not implemented |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4541 | return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy()); |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4542 | |
| 4543 | case TLSModel::InitialExec: |
| 4544 | case TLSModel::LocalExec: |
| 4545 | return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true); |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4546 | } |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4547 | } else { |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4548 | switch (model) { |
| 4549 | case TLSModel::GeneralDynamic: |
| 4550 | case TLSModel::LocalDynamic: // not implemented |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4551 | return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy()); |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4552 | |
| 4553 | case TLSModel::InitialExec: |
| 4554 | case TLSModel::LocalExec: |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4555 | return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false); |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4556 | } |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4557 | } |
Chris Lattner | 5867de1 | 2009-04-01 22:14:45 +0000 | [diff] [blame] | 4558 | assert(0 && "Unreachable"); |
| 4559 | return SDValue(); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4562 | SDValue |
| 4563 | X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4564 | // FIXME there isn't really any debug info here |
| 4565 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4566 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 4567 | SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4568 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4569 | // With PIC, the address is actually $g + Offset. |
| 4570 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 4571 | !Subtarget->isPICStyleRIPRel()) { |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4572 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4573 | DAG.getNode(X86ISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4574 | DebugLoc::getUnknownLoc(), |
| 4575 | getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4576 | Result); |
| 4577 | } |
| 4578 | |
| 4579 | return Result; |
| 4580 | } |
| 4581 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4582 | SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) { |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4583 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4584 | // FIXME there isn't really any debug into here |
| 4585 | DebugLoc dl = JT->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4586 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4587 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4588 | // With PIC, the address is actually $g + Offset. |
| 4589 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 4590 | !Subtarget->isPICStyleRIPRel()) { |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4591 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4592 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 4593 | DebugLoc::getUnknownLoc(), |
| 4594 | getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4595 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | return Result; |
| 4599 | } |
| 4600 | |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4601 | /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4602 | /// take a 2 x i32 value to shift plus a shift amount. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4603 | SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 4604 | assert(Op.getNumOperands() == 3 && "Not a double-shift!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4605 | MVT VT = Op.getValueType(); |
| 4606 | unsigned VTBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4607 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4608 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4609 | SDValue ShOpLo = Op.getOperand(0); |
| 4610 | SDValue ShOpHi = Op.getOperand(1); |
| 4611 | SDValue ShAmt = Op.getOperand(2); |
| 4612 | SDValue Tmp1 = isSRA ? |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4613 | DAG.getNode(ISD::SRA, dl, VT, ShOpHi, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4614 | DAG.getConstant(VTBits - 1, MVT::i8)) : |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 4615 | DAG.getConstant(0, VT); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4616 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4617 | SDValue Tmp2, Tmp3; |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4618 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4619 | Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt); |
| 4620 | Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4621 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4622 | Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt); |
| 4623 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4624 | } |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4625 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4626 | SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt, |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 4627 | DAG.getConstant(VTBits, MVT::i8)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4628 | SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT, |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4629 | AndNode, DAG.getConstant(0, MVT::i8)); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4630 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4631 | SDValue Hi, Lo; |
| 4632 | SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| 4633 | SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond }; |
| 4634 | SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond }; |
Duncan Sands | f951620 | 2008-06-30 10:19:09 +0000 | [diff] [blame] | 4635 | |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4636 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4637 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 4638 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4639 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4640 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 4641 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4644 | SDValue Ops[2] = { Lo, Hi }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4645 | return DAG.getMergeValues(Ops, 2, dl); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4646 | } |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4647 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4648 | SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4649 | MVT SrcVT = Op.getOperand(0).getValueType(); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4650 | assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 && |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4651 | "Unknown SINT_TO_FP to lower!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4652 | |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4653 | // These are really Legal; caller falls through into that case. |
| 4654 | if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4655 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4656 | if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 && |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4657 | Subtarget->is64Bit()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4658 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4659 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4660 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4661 | unsigned Size = SrcVT.getSizeInBits()/8; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4662 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4663 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4664 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4665 | SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 4666 | StackSlot, |
| 4667 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4668 | |
| 4669 | // Build the FILD |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4670 | SDVTList Tys; |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 4671 | bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType()); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4672 | if (useSSE) |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4673 | Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); |
| 4674 | else |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 4675 | Tys = DAG.getVTList(Op.getValueType(), MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4676 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4677 | Ops.push_back(Chain); |
| 4678 | Ops.push_back(StackSlot); |
| 4679 | Ops.push_back(DAG.getValueType(SrcVT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4680 | SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl, |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4681 | Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4682 | |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4683 | if (useSSE) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4684 | Chain = Result.getValue(1); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4685 | SDValue InFlag = Result.getValue(2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4686 | |
| 4687 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 4688 | // shouldn't be necessary except that RFP cannot be live across |
| 4689 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4690 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4691 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4692 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4693 | Tys = DAG.getVTList(MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4694 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4695 | Ops.push_back(Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4696 | Ops.push_back(Result); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4697 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4698 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 4699 | Ops.push_back(InFlag); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4700 | Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size()); |
| 4701 | Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 4702 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4703 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4704 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4705 | return Result; |
| 4706 | } |
| 4707 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4708 | // LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion. |
| 4709 | SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) { |
| 4710 | // This algorithm is not obvious. Here it is in C code, more or less: |
| 4711 | /* |
| 4712 | double uint64_to_double( uint32_t hi, uint32_t lo ) { |
| 4713 | static const __m128i exp = { 0x4330000045300000ULL, 0 }; |
| 4714 | static const __m128d bias = { 0x1.0p84, 0x1.0p52 }; |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4715 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4716 | // Copy ints to xmm registers. |
| 4717 | __m128i xh = _mm_cvtsi32_si128( hi ); |
| 4718 | __m128i xl = _mm_cvtsi32_si128( lo ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4719 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4720 | // Combine into low half of a single xmm register. |
| 4721 | __m128i x = _mm_unpacklo_epi32( xh, xl ); |
| 4722 | __m128d d; |
| 4723 | double sd; |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4724 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4725 | // Merge in appropriate exponents to give the integer bits the right |
| 4726 | // magnitude. |
| 4727 | x = _mm_unpacklo_epi32( x, exp ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4728 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4729 | // Subtract away the biases to deal with the IEEE-754 double precision |
| 4730 | // implicit 1. |
| 4731 | d = _mm_sub_pd( (__m128d) x, bias ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4732 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4733 | // All conversions up to here are exact. The correctly rounded result is |
| 4734 | // calculated using the current rounding mode using the following |
| 4735 | // horizontal add. |
| 4736 | d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) ); |
| 4737 | _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this |
| 4738 | // store doesn't really need to be here (except |
| 4739 | // maybe to zero the other double) |
| 4740 | return sd; |
| 4741 | } |
| 4742 | */ |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4743 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4744 | DebugLoc dl = Op.getDebugLoc(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4745 | |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4746 | // Build some magic constants. |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4747 | std::vector<Constant*> CV0; |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4748 | CV0.push_back(ConstantInt::get(APInt(32, 0x45300000))); |
| 4749 | CV0.push_back(ConstantInt::get(APInt(32, 0x43300000))); |
| 4750 | CV0.push_back(ConstantInt::get(APInt(32, 0))); |
| 4751 | CV0.push_back(ConstantInt::get(APInt(32, 0))); |
| 4752 | Constant *C0 = ConstantVector::get(CV0); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4753 | SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4754 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4755 | std::vector<Constant*> CV1; |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4756 | CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL)))); |
| 4757 | CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL)))); |
| 4758 | Constant *C1 = ConstantVector::get(CV1); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4759 | SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4760 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4761 | SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4762 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 6b6aeb3 | 2008-10-22 11:24:12 +0000 | [diff] [blame] | 4763 | Op.getOperand(0), |
| 4764 | DAG.getIntPtrConstant(1))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4765 | SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4766 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 6b6aeb3 | 2008-10-22 11:24:12 +0000 | [diff] [blame] | 4767 | Op.getOperand(0), |
| 4768 | DAG.getIntPtrConstant(0))); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4769 | SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4770 | SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4771 | PseudoSourceValue::getConstantPool(), 0, |
| 4772 | false, 16); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4773 | SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4774 | SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2); |
| 4775 | SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4776 | PseudoSourceValue::getConstantPool(), 0, |
| 4777 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4778 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4779 | |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4780 | // Add the halves; easiest way is to swap them into another reg first. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 4781 | int ShufMask[2] = { 1, -1 }; |
| 4782 | SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub, |
| 4783 | DAG.getUNDEF(MVT::v2f64), ShufMask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4784 | SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub); |
| 4785 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add, |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4786 | DAG.getIntPtrConstant(0)); |
| 4787 | } |
| 4788 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4789 | // LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion. |
| 4790 | SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4791 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4792 | // FP constant to bias correct the final result. |
| 4793 | SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), |
| 4794 | MVT::f64); |
| 4795 | |
| 4796 | // Load the 32-bit value into an XMM register. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4797 | SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4798 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4799 | Op.getOperand(0), |
| 4800 | DAG.getIntPtrConstant(0))); |
| 4801 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4802 | Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| 4803 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load), |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4804 | DAG.getIntPtrConstant(0)); |
| 4805 | |
| 4806 | // Or the load with the bias. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4807 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, |
| 4808 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
| 4809 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Evan Cheng | 50c3dfe | 2009-01-19 08:19:57 +0000 | [diff] [blame] | 4810 | MVT::v2f64, Load)), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4811 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
| 4812 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Evan Cheng | 50c3dfe | 2009-01-19 08:19:57 +0000 | [diff] [blame] | 4813 | MVT::v2f64, Bias))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4814 | Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| 4815 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or), |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4816 | DAG.getIntPtrConstant(0)); |
| 4817 | |
| 4818 | // Subtract the bias. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4819 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4820 | |
| 4821 | // Handle final rounding. |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4822 | MVT DestVT = Op.getValueType(); |
| 4823 | |
| 4824 | if (DestVT.bitsLT(MVT::f64)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4825 | return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4826 | DAG.getIntPtrConstant(0)); |
| 4827 | } else if (DestVT.bitsGT(MVT::f64)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4828 | return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | // Handle final rounding. |
| 4832 | return Sub; |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4836 | SDValue N0 = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4837 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4838 | |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4839 | // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't |
| 4840 | // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform |
| 4841 | // the optimization here. |
| 4842 | if (DAG.SignBitIsZero(N0)) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4843 | return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0); |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4844 | |
| 4845 | MVT SrcVT = N0.getValueType(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4846 | if (SrcVT == MVT::i64) { |
| 4847 | // We only handle SSE2 f64 target here; caller can handle the rest. |
| 4848 | if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64) |
| 4849 | return SDValue(); |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4850 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4851 | return LowerUINT_TO_FP_i64(Op, DAG); |
| 4852 | } else if (SrcVT == MVT::i32) { |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4853 | return LowerUINT_TO_FP_i32(Op, DAG); |
| 4854 | } |
| 4855 | |
| 4856 | assert(0 && "Unknown UINT_TO_FP to lower!"); |
| 4857 | return SDValue(); |
| 4858 | } |
| 4859 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4860 | std::pair<SDValue,SDValue> X86TargetLowering:: |
| 4861 | FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4862 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4863 | assert(Op.getValueType().getSimpleVT() <= MVT::i64 && |
| 4864 | Op.getValueType().getSimpleVT() >= MVT::i16 && |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4865 | "Unknown FP_TO_SINT to lower!"); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4866 | |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4867 | // These are really Legal. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4868 | if (Op.getValueType() == MVT::i32 && |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 4869 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4870 | return std::make_pair(SDValue(), SDValue()); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 4871 | if (Subtarget->is64Bit() && |
| 4872 | Op.getValueType() == MVT::i64 && |
| 4873 | Op.getOperand(0).getValueType() != MVT::f80) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4874 | return std::make_pair(SDValue(), SDValue()); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4875 | |
Evan Cheng | 87c8935 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 4876 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 4877 | // stack slot. |
| 4878 | MachineFunction &MF = DAG.getMachineFunction(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4879 | unsigned MemSize = Op.getValueType().getSizeInBits()/8; |
Evan Cheng | 87c8935 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 4880 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4881 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4882 | unsigned Opc; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4883 | switch (Op.getValueType().getSimpleVT()) { |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4884 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 4885 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 4886 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 4887 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4888 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4889 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4890 | SDValue Chain = DAG.getEntryNode(); |
| 4891 | SDValue Value = Op.getOperand(0); |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 4892 | if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4893 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4894 | Chain = DAG.getStore(Chain, dl, Value, StackSlot, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 4895 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 4896 | SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4897 | SDValue Ops[] = { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4898 | Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) |
| 4899 | }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4900 | Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4901 | Chain = Value.getValue(1); |
| 4902 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 4903 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 4904 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4905 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4906 | // Build the FP_TO_INT*_IN_MEM |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4907 | SDValue Ops[] = { Chain, Value, StackSlot }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4908 | SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 4909 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4910 | return std::make_pair(FIST, StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4913 | SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) { |
| 4914 | std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG); |
| 4915 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4916 | if (FIST.getNode() == 0) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4917 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4918 | // Load the result. |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4919 | return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4920 | FIST, StackSlot, NULL, 0); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4923 | SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4924 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4925 | MVT VT = Op.getValueType(); |
| 4926 | MVT EltVT = VT; |
| 4927 | if (VT.isVector()) |
| 4928 | EltVT = VT.getVectorElementType(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4929 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4930 | if (EltVT == MVT::f64) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 4931 | Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4932 | CV.push_back(C); |
| 4933 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4934 | } else { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 4935 | Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4936 | CV.push_back(C); |
| 4937 | CV.push_back(C); |
| 4938 | CV.push_back(C); |
| 4939 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4940 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4941 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4942 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4943 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4944 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4945 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4946 | return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4949 | SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4950 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4951 | MVT VT = Op.getValueType(); |
| 4952 | MVT EltVT = VT; |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4953 | unsigned EltNum = 1; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4954 | if (VT.isVector()) { |
| 4955 | EltVT = VT.getVectorElementType(); |
| 4956 | EltNum = VT.getVectorNumElements(); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4957 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4958 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4959 | if (EltVT == MVT::f64) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 4960 | Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4961 | CV.push_back(C); |
| 4962 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4963 | } else { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 4964 | Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4965 | CV.push_back(C); |
| 4966 | CV.push_back(C); |
| 4967 | CV.push_back(C); |
| 4968 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4969 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4970 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4971 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4972 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4973 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4974 | false, 16); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4975 | if (VT.isVector()) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4976 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 4977 | DAG.getNode(ISD::XOR, dl, MVT::v2i64, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4978 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4979 | Op.getOperand(0)), |
| 4980 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask))); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4981 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4982 | return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 4983 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4986 | SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) { |
| 4987 | SDValue Op0 = Op.getOperand(0); |
| 4988 | SDValue Op1 = Op.getOperand(1); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4989 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4990 | MVT VT = Op.getValueType(); |
| 4991 | MVT SrcVT = Op1.getValueType(); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 4992 | |
| 4993 | // If second operand is smaller, extend it first. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4994 | if (SrcVT.bitsLT(VT)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4995 | Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 4996 | SrcVT = VT; |
| 4997 | } |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 4998 | // And if it is bigger, shrink it first. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4999 | if (SrcVT.bitsGT(VT)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5000 | Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1)); |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 5001 | SrcVT = VT; |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | // At this point the operands and the result should have the same |
| 5005 | // type, and that won't be f80 since that is not custom lowered. |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5006 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5007 | // First get the sign bit of second operand. |
| 5008 | std::vector<Constant*> CV; |
| 5009 | if (SrcVT == MVT::f64) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 5010 | CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63)))); |
| 5011 | CV.push_back(ConstantFP::get(APFloat(APInt(64, 0)))); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5012 | } else { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 5013 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31)))); |
| 5014 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
| 5015 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
| 5016 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5017 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5018 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5019 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5020 | SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5021 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5022 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5023 | SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5024 | |
| 5025 | // Shift sign bit right or left if the two operands have different types. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5026 | if (SrcVT.bitsGT(VT)) { |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5027 | // Op0 is MVT::f32, Op1 is MVT::f64. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5028 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit); |
| 5029 | SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit, |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5030 | DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5031 | SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit); |
| 5032 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5033 | DAG.getIntPtrConstant(0)); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5036 | // Clear first operand sign bit. |
| 5037 | CV.clear(); |
| 5038 | if (VT == MVT::f64) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 5039 | CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))))); |
| 5040 | CV.push_back(ConstantFP::get(APFloat(APInt(64, 0)))); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5041 | } else { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 5042 | CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31))))); |
| 5043 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
| 5044 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
| 5045 | CV.push_back(ConstantFP::get(APFloat(APInt(32, 0)))); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5046 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5047 | C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5048 | CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5049 | SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5050 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5051 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5052 | SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5053 | |
| 5054 | // Or the value with the sign bit. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5055 | return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5058 | /// Emit nodes that will be selected as "test Op0,Op0", or something |
| 5059 | /// equivalent. |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5060 | SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC, |
| 5061 | SelectionDAG &DAG) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5062 | DebugLoc dl = Op.getDebugLoc(); |
| 5063 | |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5064 | // CF and OF aren't always set the way we want. Determine which |
| 5065 | // of these we need. |
| 5066 | bool NeedCF = false; |
| 5067 | bool NeedOF = false; |
| 5068 | switch (X86CC) { |
| 5069 | case X86::COND_A: case X86::COND_AE: |
| 5070 | case X86::COND_B: case X86::COND_BE: |
| 5071 | NeedCF = true; |
| 5072 | break; |
| 5073 | case X86::COND_G: case X86::COND_GE: |
| 5074 | case X86::COND_L: case X86::COND_LE: |
| 5075 | case X86::COND_O: case X86::COND_NO: |
| 5076 | NeedOF = true; |
| 5077 | break; |
| 5078 | default: break; |
| 5079 | } |
| 5080 | |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5081 | // See if we can use the EFLAGS value from the operand instead of |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5082 | // doing a separate TEST. TEST always sets OF and CF to 0, so unless |
| 5083 | // we prove that the arithmetic won't overflow, we can't use OF or CF. |
| 5084 | if (Op.getResNo() == 0 && !NeedOF && !NeedCF) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5085 | unsigned Opcode = 0; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5086 | unsigned NumOperands = 0; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5087 | switch (Op.getNode()->getOpcode()) { |
| 5088 | case ISD::ADD: |
| 5089 | // Due to an isel shortcoming, be conservative if this add is likely to |
| 5090 | // be selected as part of a load-modify-store instruction. When the root |
| 5091 | // node in a match is a store, isel doesn't know how to remap non-chain |
| 5092 | // non-flag uses of other nodes in the match, such as the ADD in this |
| 5093 | // case. This leads to the ADD being left around and reselected, with |
| 5094 | // the result being two adds in the output. |
| 5095 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 5096 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 5097 | if (UI->getOpcode() == ISD::STORE) |
| 5098 | goto default_case; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5099 | if (ConstantSDNode *C = |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5100 | dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) { |
| 5101 | // An add of one will be selected as an INC. |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5102 | if (C->getAPIntValue() == 1) { |
| 5103 | Opcode = X86ISD::INC; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5104 | NumOperands = 1; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5105 | break; |
| 5106 | } |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5107 | // An add of negative one (subtract of one) will be selected as a DEC. |
| 5108 | if (C->getAPIntValue().isAllOnesValue()) { |
| 5109 | Opcode = X86ISD::DEC; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5110 | NumOperands = 1; |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5111 | break; |
| 5112 | } |
| 5113 | } |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5114 | // Otherwise use a regular EFLAGS-setting add. |
| 5115 | Opcode = X86ISD::ADD; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5116 | NumOperands = 2; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5117 | break; |
| 5118 | case ISD::SUB: |
| 5119 | // Due to the ISEL shortcoming noted above, be conservative if this sub is |
| 5120 | // likely to be selected as part of a load-modify-store instruction. |
| 5121 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 5122 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 5123 | if (UI->getOpcode() == ISD::STORE) |
| 5124 | goto default_case; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5125 | // Otherwise use a regular EFLAGS-setting sub. |
| 5126 | Opcode = X86ISD::SUB; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5127 | NumOperands = 2; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5128 | break; |
| 5129 | case X86ISD::ADD: |
| 5130 | case X86ISD::SUB: |
| 5131 | case X86ISD::INC: |
| 5132 | case X86ISD::DEC: |
| 5133 | return SDValue(Op.getNode(), 1); |
| 5134 | default: |
| 5135 | default_case: |
| 5136 | break; |
| 5137 | } |
| 5138 | if (Opcode != 0) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5139 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5140 | SmallVector<SDValue, 4> Ops; |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5141 | for (unsigned i = 0; i != NumOperands; ++i) |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5142 | Ops.push_back(Op.getOperand(i)); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5143 | SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5144 | DAG.ReplaceAllUsesWith(Op, New); |
| 5145 | return SDValue(New.getNode(), 1); |
| 5146 | } |
| 5147 | } |
| 5148 | |
| 5149 | // Otherwise just emit a CMP with 0, which is the TEST pattern. |
| 5150 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, |
| 5151 | DAG.getConstant(0, Op.getValueType())); |
| 5152 | } |
| 5153 | |
| 5154 | /// Emit nodes that will be selected as "cmp Op0,Op1", or something |
| 5155 | /// equivalent. |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5156 | SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC, |
| 5157 | SelectionDAG &DAG) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5158 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1)) |
| 5159 | if (C->getAPIntValue() == 0) |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5160 | return EmitTest(Op0, X86CC, DAG); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5161 | |
| 5162 | DebugLoc dl = Op0.getDebugLoc(); |
| 5163 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1); |
| 5164 | } |
| 5165 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5166 | SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5167 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5168 | SDValue Op0 = Op.getOperand(0); |
| 5169 | SDValue Op1 = Op.getOperand(1); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5170 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5171 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5172 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5173 | // Lower (X & (1 << N)) == 0 to BT(X, N). |
| 5174 | // Lower ((X >>u N) & 1) != 0 to BT(X, N). |
| 5175 | // Lower ((X >>s N) & 1) != 0 to BT(X, N). |
Dan Gohman | 286575c | 2009-01-13 23:25:30 +0000 | [diff] [blame] | 5176 | if (Op0.getOpcode() == ISD::AND && |
| 5177 | Op0.hasOneUse() && |
| 5178 | Op1.getOpcode() == ISD::Constant && |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5179 | cast<ConstantSDNode>(Op1)->getZExtValue() == 0 && |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5180 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5181 | SDValue LHS, RHS; |
| 5182 | if (Op0.getOperand(1).getOpcode() == ISD::SHL) { |
| 5183 | if (ConstantSDNode *Op010C = |
| 5184 | dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0))) |
| 5185 | if (Op010C->getZExtValue() == 1) { |
| 5186 | LHS = Op0.getOperand(0); |
| 5187 | RHS = Op0.getOperand(1).getOperand(1); |
| 5188 | } |
| 5189 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) { |
| 5190 | if (ConstantSDNode *Op000C = |
| 5191 | dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0))) |
| 5192 | if (Op000C->getZExtValue() == 1) { |
| 5193 | LHS = Op0.getOperand(1); |
| 5194 | RHS = Op0.getOperand(0).getOperand(1); |
| 5195 | } |
| 5196 | } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) { |
| 5197 | ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1)); |
| 5198 | SDValue AndLHS = Op0.getOperand(0); |
| 5199 | if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) { |
| 5200 | LHS = AndLHS.getOperand(0); |
| 5201 | RHS = AndLHS.getOperand(1); |
| 5202 | } |
| 5203 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5204 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5205 | if (LHS.getNode()) { |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5206 | // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT |
| 5207 | // instruction. Since the shift amount is in-range-or-undefined, we know |
| 5208 | // that doing a bittest on the i16 value is ok. We extend to i32 because |
| 5209 | // the encoding for the i16 version is larger than the i32 version. |
| 5210 | if (LHS.getValueType() == MVT::i8) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5211 | LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5212 | |
| 5213 | // If the operand types disagree, extend the shift amount to match. Since |
| 5214 | // BT ignores high bits (like shifts) we can use anyextend. |
| 5215 | if (LHS.getValueType() != RHS.getValueType()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5216 | RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5217 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5218 | SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS); |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 5219 | unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5220 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5221 | DAG.getConstant(Cond, MVT::i8), BT); |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); |
| 5226 | unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5227 | |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5228 | SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5229 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Chris Lattner | 4328708 | 2008-12-24 00:11:37 +0000 | [diff] [blame] | 5230 | DAG.getConstant(X86CC, MVT::i8), Cond); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5231 | } |
| 5232 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5233 | SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) { |
| 5234 | SDValue Cond; |
| 5235 | SDValue Op0 = Op.getOperand(0); |
| 5236 | SDValue Op1 = Op.getOperand(1); |
| 5237 | SDValue CC = Op.getOperand(2); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5238 | MVT VT = Op.getValueType(); |
| 5239 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| 5240 | bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5241 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5242 | |
| 5243 | if (isFP) { |
| 5244 | unsigned SSECC = 8; |
Evan Cheng | e9d5035 | 2008-08-05 22:19:15 +0000 | [diff] [blame] | 5245 | MVT VT0 = Op0.getValueType(); |
| 5246 | assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64); |
| 5247 | unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD; |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5248 | bool Swap = false; |
| 5249 | |
| 5250 | switch (SetCCOpcode) { |
| 5251 | default: break; |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5252 | case ISD::SETOEQ: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5253 | case ISD::SETEQ: SSECC = 0; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5254 | case ISD::SETOGT: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5255 | case ISD::SETGT: Swap = true; // Fallthrough |
| 5256 | case ISD::SETLT: |
| 5257 | case ISD::SETOLT: SSECC = 1; break; |
| 5258 | case ISD::SETOGE: |
| 5259 | case ISD::SETGE: Swap = true; // Fallthrough |
| 5260 | case ISD::SETLE: |
| 5261 | case ISD::SETOLE: SSECC = 2; break; |
| 5262 | case ISD::SETUO: SSECC = 3; break; |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5263 | case ISD::SETUNE: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5264 | case ISD::SETNE: SSECC = 4; break; |
| 5265 | case ISD::SETULE: Swap = true; |
| 5266 | case ISD::SETUGE: SSECC = 5; break; |
| 5267 | case ISD::SETULT: Swap = true; |
| 5268 | case ISD::SETUGT: SSECC = 6; break; |
| 5269 | case ISD::SETO: SSECC = 7; break; |
| 5270 | } |
| 5271 | if (Swap) |
| 5272 | std::swap(Op0, Op1); |
| 5273 | |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5274 | // In the two special cases we can't handle, emit two comparisons. |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5275 | if (SSECC == 8) { |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5276 | if (SetCCOpcode == ISD::SETUEQ) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5277 | SDValue UNORD, EQ; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5278 | UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8)); |
| 5279 | EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8)); |
| 5280 | return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5281 | } |
| 5282 | else if (SetCCOpcode == ISD::SETONE) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5283 | SDValue ORD, NEQ; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5284 | ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8)); |
| 5285 | NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8)); |
| 5286 | return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5287 | } |
| 5288 | assert(0 && "Illegal FP comparison"); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5289 | } |
| 5290 | // Handle all other FP comparisons here. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5291 | return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8)); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5292 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5293 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5294 | // We are handling one of the integer comparisons here. Since SSE only has |
| 5295 | // GT and EQ comparisons for integer, swapping operands and multiple |
| 5296 | // operations may be required for some comparisons. |
| 5297 | unsigned Opc = 0, EQOpc = 0, GTOpc = 0; |
| 5298 | bool Swap = false, Invert = false, FlipSigns = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5299 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5300 | switch (VT.getSimpleVT()) { |
| 5301 | default: break; |
| 5302 | case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break; |
| 5303 | case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break; |
| 5304 | case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break; |
| 5305 | case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break; |
| 5306 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5307 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5308 | switch (SetCCOpcode) { |
| 5309 | default: break; |
| 5310 | case ISD::SETNE: Invert = true; |
| 5311 | case ISD::SETEQ: Opc = EQOpc; break; |
| 5312 | case ISD::SETLT: Swap = true; |
| 5313 | case ISD::SETGT: Opc = GTOpc; break; |
| 5314 | case ISD::SETGE: Swap = true; |
| 5315 | case ISD::SETLE: Opc = GTOpc; Invert = true; break; |
| 5316 | case ISD::SETULT: Swap = true; |
| 5317 | case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break; |
| 5318 | case ISD::SETUGE: Swap = true; |
| 5319 | case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break; |
| 5320 | } |
| 5321 | if (Swap) |
| 5322 | std::swap(Op0, Op1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5323 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5324 | // Since SSE has no unsigned integer comparisons, we need to flip the sign |
| 5325 | // bits of the inputs before performing those operations. |
| 5326 | if (FlipSigns) { |
| 5327 | MVT EltVT = VT.getVectorElementType(); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 5328 | SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), |
| 5329 | EltVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5330 | std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5331 | SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0], |
| 5332 | SignBits.size()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5333 | Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec); |
| 5334 | Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5335 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5336 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5337 | SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5338 | |
| 5339 | // If the logical-not of the result is required, perform that now. |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 5340 | if (Invert) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5341 | Result = DAG.getNOT(dl, Result, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 5342 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5343 | return Result; |
| 5344 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5345 | |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5346 | // isX86LogicalCmp - Return true if opcode is a X86 logical comparison. |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5347 | static bool isX86LogicalCmp(SDValue Op) { |
| 5348 | unsigned Opc = Op.getNode()->getOpcode(); |
| 5349 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) |
| 5350 | return true; |
| 5351 | if (Op.getResNo() == 1 && |
| 5352 | (Opc == X86ISD::ADD || |
| 5353 | Opc == X86ISD::SUB || |
| 5354 | Opc == X86ISD::SMUL || |
| 5355 | Opc == X86ISD::UMUL || |
| 5356 | Opc == X86ISD::INC || |
| 5357 | Opc == X86ISD::DEC)) |
| 5358 | return true; |
| 5359 | |
| 5360 | return false; |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5363 | SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5364 | bool addTest = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5365 | SDValue Cond = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5366 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5367 | SDValue CC; |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 5368 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5369 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5370 | Cond = LowerSETCC(Cond, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5371 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5372 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 5373 | // setting operand in place of the X86ISD::SETCC. |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5374 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 5375 | CC = Cond.getOperand(0); |
| 5376 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5377 | SDValue Cmp = Cond.getOperand(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5378 | unsigned Opc = Cmp.getOpcode(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5379 | MVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5380 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5381 | bool IllegalFPCMov = false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5382 | if (VT.isFloatingPoint() && !VT.isVector() && |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 5383 | !isScalarFPTypeInSSEReg(VT)) // FPStack? |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 5384 | IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5385 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 5386 | if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) || |
| 5387 | Opc == X86ISD::BT) { // FIXME |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5388 | Cond = Cmp; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5389 | addTest = false; |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | if (addTest) { |
| 5394 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5395 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5398 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5399 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5400 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 5401 | // condition is true. |
| 5402 | Ops.push_back(Op.getOperand(2)); |
| 5403 | Ops.push_back(Op.getOperand(1)); |
| 5404 | Ops.push_back(CC); |
| 5405 | Ops.push_back(Cond); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5406 | return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size()); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5409 | // isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or |
| 5410 | // ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart |
| 5411 | // from the AND / OR. |
| 5412 | static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) { |
| 5413 | Opc = Op.getOpcode(); |
| 5414 | if (Opc != ISD::OR && Opc != ISD::AND) |
| 5415 | return false; |
| 5416 | return (Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 5417 | Op.getOperand(0).hasOneUse() && |
| 5418 | Op.getOperand(1).getOpcode() == X86ISD::SETCC && |
| 5419 | Op.getOperand(1).hasOneUse()); |
| 5420 | } |
| 5421 | |
Evan Cheng | 961d6d4 | 2009-02-02 08:19:07 +0000 | [diff] [blame] | 5422 | // isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and |
| 5423 | // 1 and that the SETCC node has a single use. |
Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 5424 | static bool isXor1OfSetCC(SDValue Op) { |
| 5425 | if (Op.getOpcode() != ISD::XOR) |
| 5426 | return false; |
| 5427 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5428 | if (N1C && N1C->getAPIntValue() == 1) { |
| 5429 | return Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 5430 | Op.getOperand(0).hasOneUse(); |
| 5431 | } |
| 5432 | return false; |
| 5433 | } |
| 5434 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5435 | SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5436 | bool addTest = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5437 | SDValue Chain = Op.getOperand(0); |
| 5438 | SDValue Cond = Op.getOperand(1); |
| 5439 | SDValue Dest = Op.getOperand(2); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5440 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5441 | SDValue CC; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5442 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5443 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5444 | Cond = LowerSETCC(Cond, DAG); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5445 | #if 0 |
| 5446 | // FIXME: LowerXALUO doesn't handle these!! |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 5447 | else if (Cond.getOpcode() == X86ISD::ADD || |
| 5448 | Cond.getOpcode() == X86ISD::SUB || |
| 5449 | Cond.getOpcode() == X86ISD::SMUL || |
| 5450 | Cond.getOpcode() == X86ISD::UMUL) |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5451 | Cond = LowerXALUO(Cond, DAG); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5452 | #endif |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5453 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5454 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 5455 | // setting operand in place of the X86ISD::SETCC. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5456 | if (Cond.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5457 | CC = Cond.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5458 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5459 | SDValue Cmp = Cond.getOperand(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5460 | unsigned Opc = Cmp.getOpcode(); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5461 | // FIXME: WHY THE SPECIAL CASING OF LogicalCmp?? |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5462 | if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) { |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5463 | Cond = Cmp; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5464 | addTest = false; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 5465 | } else { |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5466 | switch (cast<ConstantSDNode>(CC)->getZExtValue()) { |
Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 5467 | default: break; |
| 5468 | case X86::COND_O: |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 5469 | case X86::COND_B: |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5470 | // These can only come from an arithmetic instruction with overflow, |
| 5471 | // e.g. SADDO, UADDO. |
Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 5472 | Cond = Cond.getNode()->getOperand(1); |
| 5473 | addTest = false; |
| 5474 | break; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 5475 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5476 | } |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5477 | } else { |
| 5478 | unsigned CondOpc; |
| 5479 | if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) { |
| 5480 | SDValue Cmp = Cond.getOperand(0).getOperand(1); |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5481 | if (CondOpc == ISD::OR) { |
| 5482 | // Also, recognize the pattern generated by an FCMP_UNE. We can emit |
| 5483 | // two branches instead of an explicit OR instruction with a |
| 5484 | // separate test. |
| 5485 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5486 | isX86LogicalCmp(Cmp)) { |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5487 | CC = Cond.getOperand(0).getOperand(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5488 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5489 | Chain, Dest, CC, Cmp); |
| 5490 | CC = Cond.getOperand(1).getOperand(0); |
| 5491 | Cond = Cmp; |
| 5492 | addTest = false; |
| 5493 | } |
| 5494 | } else { // ISD::AND |
| 5495 | // Also, recognize the pattern generated by an FCMP_OEQ. We can emit |
| 5496 | // two branches instead of an explicit AND instruction with a |
| 5497 | // separate test. However, we only do this if this block doesn't |
| 5498 | // have a fall-through edge, because this requires an explicit |
| 5499 | // jmp when the condition is false. |
| 5500 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5501 | isX86LogicalCmp(Cmp) && |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5502 | Op.getNode()->hasOneUse()) { |
| 5503 | X86::CondCode CCode = |
| 5504 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 5505 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5506 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5507 | SDValue User = SDValue(*Op.getNode()->use_begin(), 0); |
| 5508 | // Look for an unconditional branch following this conditional branch. |
| 5509 | // We need this because we need to reverse the successors in order |
| 5510 | // to implement FCMP_OEQ. |
| 5511 | if (User.getOpcode() == ISD::BR) { |
| 5512 | SDValue FalseBB = User.getOperand(1); |
| 5513 | SDValue NewBR = |
| 5514 | DAG.UpdateNodeOperands(User, User.getOperand(0), Dest); |
| 5515 | assert(NewBR == User); |
| 5516 | Dest = FalseBB; |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5517 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5518 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5519 | Chain, Dest, CC, Cmp); |
| 5520 | X86::CondCode CCode = |
| 5521 | (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0); |
| 5522 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5523 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5524 | Cond = Cmp; |
| 5525 | addTest = false; |
| 5526 | } |
| 5527 | } |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5528 | } |
Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 5529 | } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) { |
| 5530 | // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition. |
| 5531 | // It should be transformed during dag combiner except when the condition |
| 5532 | // is set by a arithmetics with overflow node. |
| 5533 | X86::CondCode CCode = |
| 5534 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 5535 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5536 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5537 | Cond = Cond.getOperand(0).getOperand(1); |
| 5538 | addTest = false; |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5539 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | if (addTest) { |
| 5543 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5544 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5545 | } |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5546 | return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5547 | Chain, Dest, CC, Cond); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 5550 | |
| 5551 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. |
| 5552 | // Calls to _alloca is needed to probe the stack when allocating more than 4k |
| 5553 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure |
| 5554 | // that the guard pages used by the OS virtual memory manager are allocated in |
| 5555 | // correct sequence. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5556 | SDValue |
| 5557 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5558 | SelectionDAG &DAG) { |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 5559 | assert(Subtarget->isTargetCygMing() && |
| 5560 | "This should be used only on Cygwin/Mingw targets"); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5561 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5562 | |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5563 | // Get the inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5564 | SDValue Chain = Op.getOperand(0); |
| 5565 | SDValue Size = Op.getOperand(1); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5566 | // FIXME: Ensure alignment here |
| 5567 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5568 | SDValue Flag; |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5569 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5570 | MVT IntPtr = getPointerTy(); |
| 5571 | MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5572 | |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 5573 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5574 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 5575 | Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag); |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5576 | Flag = Chain.getValue(1); |
| 5577 | |
| 5578 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5579 | SDValue Ops[] = { Chain, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5580 | DAG.getTargetExternalSymbol("_alloca", IntPtr), |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5581 | DAG.getRegister(X86::EAX, IntPtr), |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5582 | DAG.getRegister(X86StackPtr, SPTy), |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5583 | Flag }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5584 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5); |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5585 | Flag = Chain.getValue(1); |
| 5586 | |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5587 | Chain = DAG.getCALLSEQ_END(Chain, |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 5588 | DAG.getIntPtrConstant(0, true), |
| 5589 | DAG.getIntPtrConstant(0, true), |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5590 | Flag); |
| 5591 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 5592 | Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5593 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5594 | SDValue Ops1[2] = { Chain.getValue(0), Chain }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5595 | return DAG.getMergeValues(Ops1, 2, dl); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5596 | } |
| 5597 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5598 | SDValue |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5599 | X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5600 | SDValue Chain, |
| 5601 | SDValue Dst, SDValue Src, |
| 5602 | SDValue Size, unsigned Align, |
| 5603 | const Value *DstSV, |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5604 | uint64_t DstSVOff) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5605 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5606 | |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5607 | // If not DWORD aligned or size is more than the threshold, call the library. |
| 5608 | // The libc version is likely to be faster for these cases. It can use the |
| 5609 | // address value and run time information about the CPU. |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5610 | if ((Align & 3) != 0 || |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5611 | !ConstantSize || |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5612 | ConstantSize->getZExtValue() > |
| 5613 | getSubtarget()->getMaxInlineSizeThreshold()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5614 | SDValue InFlag(0, 0); |
Dan Gohman | 68d599d | 2008-04-01 20:38:36 +0000 | [diff] [blame] | 5615 | |
| 5616 | // Check to see if there is a specialized entry-point for memory zeroing. |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5617 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src); |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5618 | |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5619 | if (const char *bzeroEntry = V && |
| 5620 | V->isNullValue() ? Subtarget->getBZeroEntry() : 0) { |
| 5621 | MVT IntPtr = getPointerTy(); |
| 5622 | const Type *IntPtrTy = TD->getIntPtrType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5623 | TargetLowering::ArgListTy Args; |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5624 | TargetLowering::ArgListEntry Entry; |
| 5625 | Entry.Node = Dst; |
| 5626 | Entry.Ty = IntPtrTy; |
| 5627 | Args.push_back(Entry); |
| 5628 | Entry.Node = Size; |
| 5629 | Args.push_back(Entry); |
| 5630 | std::pair<SDValue,SDValue> CallResult = |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5631 | LowerCallTo(Chain, Type::VoidTy, false, false, false, false, |
| 5632 | CallingConv::C, false, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5633 | DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl); |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5634 | return CallResult.second; |
Dan Gohman | 68d599d | 2008-04-01 20:38:36 +0000 | [diff] [blame] | 5635 | } |
| 5636 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5637 | // Otherwise have the target-independent code call memset. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5638 | return SDValue(); |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 5639 | } |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 5640 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5641 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5642 | SDValue InFlag(0, 0); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5643 | MVT AVT; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5644 | SDValue Count; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5645 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5646 | unsigned BytesLeft = 0; |
| 5647 | bool TwoRepStos = false; |
| 5648 | if (ValC) { |
| 5649 | unsigned ValReg; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5650 | uint64_t Val = ValC->getZExtValue() & 255; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 5651 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5652 | // If the value is a constant, then we can potentially use larger sets. |
| 5653 | switch (Align & 3) { |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5654 | case 2: // WORD aligned |
| 5655 | AVT = MVT::i16; |
| 5656 | ValReg = X86::AX; |
| 5657 | Val = (Val << 8) | Val; |
| 5658 | break; |
| 5659 | case 0: // DWORD aligned |
| 5660 | AVT = MVT::i32; |
| 5661 | ValReg = X86::EAX; |
| 5662 | Val = (Val << 8) | Val; |
| 5663 | Val = (Val << 16) | Val; |
| 5664 | if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned |
| 5665 | AVT = MVT::i64; |
| 5666 | ValReg = X86::RAX; |
| 5667 | Val = (Val << 32) | Val; |
| 5668 | } |
| 5669 | break; |
| 5670 | default: // Byte aligned |
| 5671 | AVT = MVT::i8; |
| 5672 | ValReg = X86::AL; |
| 5673 | Count = DAG.getIntPtrConstant(SizeVal); |
| 5674 | break; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5677 | if (AVT.bitsGT(MVT::i8)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5678 | unsigned UBytes = AVT.getSizeInBits() / 8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5679 | Count = DAG.getIntPtrConstant(SizeVal / UBytes); |
| 5680 | BytesLeft = SizeVal % UBytes; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5683 | Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT), |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5684 | InFlag); |
| 5685 | InFlag = Chain.getValue(1); |
| 5686 | } else { |
| 5687 | AVT = MVT::i8; |
Dan Gohman | bcda285 | 2008-04-16 01:32:32 +0000 | [diff] [blame] | 5688 | Count = DAG.getIntPtrConstant(SizeVal); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5689 | Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5690 | InFlag = Chain.getValue(1); |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 5691 | } |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 5692 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5693 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5694 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5695 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5696 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5697 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5698 | X86::EDI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5699 | Dst, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5700 | InFlag = Chain.getValue(1); |
Evan Cheng | a0b3afb | 2006-03-27 07:00:16 +0000 | [diff] [blame] | 5701 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5702 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5703 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5704 | Ops.push_back(Chain); |
| 5705 | Ops.push_back(DAG.getValueType(AVT)); |
| 5706 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5707 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 5708 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5709 | if (TwoRepStos) { |
| 5710 | InFlag = Chain.getValue(1); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5711 | Count = Size; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5712 | MVT CVT = Count.getValueType(); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5713 | SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5714 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5715 | Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5716 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5717 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5718 | InFlag = Chain.getValue(1); |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5719 | Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5720 | Ops.clear(); |
| 5721 | Ops.push_back(Chain); |
| 5722 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 5723 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5724 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5725 | } else if (BytesLeft) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5726 | // Handle the last 1 - 7 bytes. |
| 5727 | unsigned Offset = SizeVal - BytesLeft; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5728 | MVT AddrVT = Dst.getValueType(); |
| 5729 | MVT SizeVT = Size.getValueType(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5730 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5731 | Chain = DAG.getMemset(Chain, dl, |
| 5732 | DAG.getNode(ISD::ADD, dl, AddrVT, Dst, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5733 | DAG.getConstant(Offset, AddrVT)), |
| 5734 | Src, |
| 5735 | DAG.getConstant(BytesLeft, SizeVT), |
Dan Gohman | 1f13c68 | 2008-04-28 17:15:20 +0000 | [diff] [blame] | 5736 | Align, DstSV, DstSVOff + Offset); |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 5737 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 5738 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5739 | // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5740 | return Chain; |
| 5741 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 5742 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5743 | SDValue |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5744 | X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl, |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5745 | SDValue Chain, SDValue Dst, SDValue Src, |
| 5746 | SDValue Size, unsigned Align, |
| 5747 | bool AlwaysInline, |
| 5748 | const Value *DstSV, uint64_t DstSVOff, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5749 | const Value *SrcSV, uint64_t SrcSVOff) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5750 | // This requires the copy size to be a constant, preferrably |
| 5751 | // within a subtarget-specific limit. |
| 5752 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 5753 | if (!ConstantSize) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5754 | return SDValue(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5755 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5756 | if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5757 | return SDValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5758 | |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5759 | /// If not DWORD aligned, call the library. |
| 5760 | if ((Align & 3) != 0) |
| 5761 | return SDValue(); |
| 5762 | |
| 5763 | // DWORD aligned |
| 5764 | MVT AVT = MVT::i32; |
| 5765 | if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5766 | AVT = MVT::i64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5767 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5768 | unsigned UBytes = AVT.getSizeInBits() / 8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5769 | unsigned CountVal = SizeVal / UBytes; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5770 | SDValue Count = DAG.getIntPtrConstant(CountVal); |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5771 | unsigned BytesLeft = SizeVal % UBytes; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5772 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5773 | SDValue InFlag(0, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5774 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5775 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5776 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5777 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5778 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5779 | X86::EDI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5780 | Dst, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5781 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5782 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5783 | X86::ESI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5784 | Src, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5785 | InFlag = Chain.getValue(1); |
| 5786 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5787 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5788 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5789 | Ops.push_back(Chain); |
| 5790 | Ops.push_back(DAG.getValueType(AVT)); |
| 5791 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5792 | SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5793 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5794 | SmallVector<SDValue, 4> Results; |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5795 | Results.push_back(RepMovs); |
Rafael Espindola | 068317b | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 5796 | if (BytesLeft) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5797 | // Handle the last 1 - 7 bytes. |
| 5798 | unsigned Offset = SizeVal - BytesLeft; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5799 | MVT DstVT = Dst.getValueType(); |
| 5800 | MVT SrcVT = Src.getValueType(); |
| 5801 | MVT SizeVT = Size.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5802 | Results.push_back(DAG.getMemcpy(Chain, dl, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5803 | DAG.getNode(ISD::ADD, dl, DstVT, Dst, |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5804 | DAG.getConstant(Offset, DstVT)), |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5805 | DAG.getNode(ISD::ADD, dl, SrcVT, Src, |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5806 | DAG.getConstant(Offset, SrcVT)), |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5807 | DAG.getConstant(BytesLeft, SizeVT), |
| 5808 | Align, AlwaysInline, |
Dan Gohman | 1f13c68 | 2008-04-28 17:15:20 +0000 | [diff] [blame] | 5809 | DstSV, DstSVOff + Offset, |
| 5810 | SrcSV, SrcSVOff + Offset)); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 5811 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5812 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5813 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5814 | &Results[0], Results.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5817 | SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5818 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5819 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5820 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5821 | if (!Subtarget->is64Bit()) { |
| 5822 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 5823 | // memory location argument. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5824 | SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5825 | return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | // __va_list_tag: |
| 5829 | // gp_offset (0 - 6 * 8) |
| 5830 | // fp_offset (48 - 48 + 8 * 16) |
| 5831 | // overflow_arg_area (point to parameters coming in memory). |
| 5832 | // reg_save_area |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5833 | SmallVector<SDValue, 8> MemOps; |
| 5834 | SDValue FIN = Op.getOperand(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5835 | // Store gp_offset |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5836 | SDValue Store = DAG.getStore(Op.getOperand(0), dl, |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5837 | DAG.getConstant(VarArgsGPOffset, MVT::i32), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5838 | FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5839 | MemOps.push_back(Store); |
| 5840 | |
| 5841 | // Store fp_offset |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5842 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5843 | FIN, DAG.getIntPtrConstant(4)); |
| 5844 | Store = DAG.getStore(Op.getOperand(0), dl, |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5845 | DAG.getConstant(VarArgsFPOffset, MVT::i32), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5846 | FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5847 | MemOps.push_back(Store); |
| 5848 | |
| 5849 | // Store ptr to overflow_arg_area |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5850 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5851 | FIN, DAG.getIntPtrConstant(4)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5852 | SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5853 | Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5854 | MemOps.push_back(Store); |
| 5855 | |
| 5856 | // Store ptr to reg_save_area. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5857 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5858 | FIN, DAG.getIntPtrConstant(8)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5859 | SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5860 | Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5861 | MemOps.push_back(Store); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5862 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5863 | &MemOps[0], MemOps.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5866 | SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 5867 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
| 5868 | assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5869 | SDValue Chain = Op.getOperand(0); |
| 5870 | SDValue SrcPtr = Op.getOperand(1); |
| 5871 | SDValue SrcSV = Op.getOperand(2); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 5872 | |
| 5873 | assert(0 && "VAArgInst is not yet implemented for x86-64!"); |
| 5874 | abort(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5875 | return SDValue(); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5878 | SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 5879 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
Dan Gohman | 2826913 | 2008-04-18 20:55:41 +0000 | [diff] [blame] | 5880 | assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5881 | SDValue Chain = Op.getOperand(0); |
| 5882 | SDValue DstPtr = Op.getOperand(1); |
| 5883 | SDValue SrcPtr = Op.getOperand(2); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5884 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); |
| 5885 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5886 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 5887 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 5888 | return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr, |
Dan Gohman | 2826913 | 2008-04-18 20:55:41 +0000 | [diff] [blame] | 5889 | DAG.getIntPtrConstant(24), 8, false, |
| 5890 | DstSV, 0, SrcSV, 0); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5893 | SDValue |
| 5894 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5895 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5896 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5897 | switch (IntNo) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5898 | default: return SDValue(); // Don't custom lower most intrinsics. |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 5899 | // Comparison intrinsics. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5900 | case Intrinsic::x86_sse_comieq_ss: |
| 5901 | case Intrinsic::x86_sse_comilt_ss: |
| 5902 | case Intrinsic::x86_sse_comile_ss: |
| 5903 | case Intrinsic::x86_sse_comigt_ss: |
| 5904 | case Intrinsic::x86_sse_comige_ss: |
| 5905 | case Intrinsic::x86_sse_comineq_ss: |
| 5906 | case Intrinsic::x86_sse_ucomieq_ss: |
| 5907 | case Intrinsic::x86_sse_ucomilt_ss: |
| 5908 | case Intrinsic::x86_sse_ucomile_ss: |
| 5909 | case Intrinsic::x86_sse_ucomigt_ss: |
| 5910 | case Intrinsic::x86_sse_ucomige_ss: |
| 5911 | case Intrinsic::x86_sse_ucomineq_ss: |
| 5912 | case Intrinsic::x86_sse2_comieq_sd: |
| 5913 | case Intrinsic::x86_sse2_comilt_sd: |
| 5914 | case Intrinsic::x86_sse2_comile_sd: |
| 5915 | case Intrinsic::x86_sse2_comigt_sd: |
| 5916 | case Intrinsic::x86_sse2_comige_sd: |
| 5917 | case Intrinsic::x86_sse2_comineq_sd: |
| 5918 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 5919 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 5920 | case Intrinsic::x86_sse2_ucomile_sd: |
| 5921 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 5922 | case Intrinsic::x86_sse2_ucomige_sd: |
| 5923 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 5924 | unsigned Opc = 0; |
| 5925 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 5926 | switch (IntNo) { |
| 5927 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5928 | case Intrinsic::x86_sse_comieq_ss: |
| 5929 | case Intrinsic::x86_sse2_comieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5930 | Opc = X86ISD::COMI; |
| 5931 | CC = ISD::SETEQ; |
| 5932 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5933 | case Intrinsic::x86_sse_comilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5934 | case Intrinsic::x86_sse2_comilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5935 | Opc = X86ISD::COMI; |
| 5936 | CC = ISD::SETLT; |
| 5937 | break; |
| 5938 | case Intrinsic::x86_sse_comile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5939 | case Intrinsic::x86_sse2_comile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5940 | Opc = X86ISD::COMI; |
| 5941 | CC = ISD::SETLE; |
| 5942 | break; |
| 5943 | case Intrinsic::x86_sse_comigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5944 | case Intrinsic::x86_sse2_comigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5945 | Opc = X86ISD::COMI; |
| 5946 | CC = ISD::SETGT; |
| 5947 | break; |
| 5948 | case Intrinsic::x86_sse_comige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5949 | case Intrinsic::x86_sse2_comige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5950 | Opc = X86ISD::COMI; |
| 5951 | CC = ISD::SETGE; |
| 5952 | break; |
| 5953 | case Intrinsic::x86_sse_comineq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5954 | case Intrinsic::x86_sse2_comineq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5955 | Opc = X86ISD::COMI; |
| 5956 | CC = ISD::SETNE; |
| 5957 | break; |
| 5958 | case Intrinsic::x86_sse_ucomieq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5959 | case Intrinsic::x86_sse2_ucomieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5960 | Opc = X86ISD::UCOMI; |
| 5961 | CC = ISD::SETEQ; |
| 5962 | break; |
| 5963 | case Intrinsic::x86_sse_ucomilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5964 | case Intrinsic::x86_sse2_ucomilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5965 | Opc = X86ISD::UCOMI; |
| 5966 | CC = ISD::SETLT; |
| 5967 | break; |
| 5968 | case Intrinsic::x86_sse_ucomile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5969 | case Intrinsic::x86_sse2_ucomile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5970 | Opc = X86ISD::UCOMI; |
| 5971 | CC = ISD::SETLE; |
| 5972 | break; |
| 5973 | case Intrinsic::x86_sse_ucomigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5974 | case Intrinsic::x86_sse2_ucomigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5975 | Opc = X86ISD::UCOMI; |
| 5976 | CC = ISD::SETGT; |
| 5977 | break; |
| 5978 | case Intrinsic::x86_sse_ucomige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5979 | case Intrinsic::x86_sse2_ucomige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5980 | Opc = X86ISD::UCOMI; |
| 5981 | CC = ISD::SETGE; |
| 5982 | break; |
| 5983 | case Intrinsic::x86_sse_ucomineq_ss: |
| 5984 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 5985 | Opc = X86ISD::UCOMI; |
| 5986 | CC = ISD::SETNE; |
| 5987 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5988 | } |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5989 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5990 | SDValue LHS = Op.getOperand(1); |
| 5991 | SDValue RHS = Op.getOperand(2); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 5992 | unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5993 | SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS); |
| 5994 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Evan Cheng | 0ac3fc2 | 2008-08-17 19:22:34 +0000 | [diff] [blame] | 5995 | DAG.getConstant(X86CC, MVT::i8), Cond); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5996 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 5997 | } |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 5998 | |
| 5999 | // Fix vector shift instructions where the last operand is a non-immediate |
| 6000 | // i32 value. |
| 6001 | case Intrinsic::x86_sse2_pslli_w: |
| 6002 | case Intrinsic::x86_sse2_pslli_d: |
| 6003 | case Intrinsic::x86_sse2_pslli_q: |
| 6004 | case Intrinsic::x86_sse2_psrli_w: |
| 6005 | case Intrinsic::x86_sse2_psrli_d: |
| 6006 | case Intrinsic::x86_sse2_psrli_q: |
| 6007 | case Intrinsic::x86_sse2_psrai_w: |
| 6008 | case Intrinsic::x86_sse2_psrai_d: |
| 6009 | case Intrinsic::x86_mmx_pslli_w: |
| 6010 | case Intrinsic::x86_mmx_pslli_d: |
| 6011 | case Intrinsic::x86_mmx_pslli_q: |
| 6012 | case Intrinsic::x86_mmx_psrli_w: |
| 6013 | case Intrinsic::x86_mmx_psrli_d: |
| 6014 | case Intrinsic::x86_mmx_psrli_q: |
| 6015 | case Intrinsic::x86_mmx_psrai_w: |
| 6016 | case Intrinsic::x86_mmx_psrai_d: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6017 | SDValue ShAmt = Op.getOperand(2); |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6018 | if (isa<ConstantSDNode>(ShAmt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6019 | return SDValue(); |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6020 | |
| 6021 | unsigned NewIntNo = 0; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6022 | MVT ShAmtVT = MVT::v4i32; |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6023 | switch (IntNo) { |
| 6024 | case Intrinsic::x86_sse2_pslli_w: |
| 6025 | NewIntNo = Intrinsic::x86_sse2_psll_w; |
| 6026 | break; |
| 6027 | case Intrinsic::x86_sse2_pslli_d: |
| 6028 | NewIntNo = Intrinsic::x86_sse2_psll_d; |
| 6029 | break; |
| 6030 | case Intrinsic::x86_sse2_pslli_q: |
| 6031 | NewIntNo = Intrinsic::x86_sse2_psll_q; |
| 6032 | break; |
| 6033 | case Intrinsic::x86_sse2_psrli_w: |
| 6034 | NewIntNo = Intrinsic::x86_sse2_psrl_w; |
| 6035 | break; |
| 6036 | case Intrinsic::x86_sse2_psrli_d: |
| 6037 | NewIntNo = Intrinsic::x86_sse2_psrl_d; |
| 6038 | break; |
| 6039 | case Intrinsic::x86_sse2_psrli_q: |
| 6040 | NewIntNo = Intrinsic::x86_sse2_psrl_q; |
| 6041 | break; |
| 6042 | case Intrinsic::x86_sse2_psrai_w: |
| 6043 | NewIntNo = Intrinsic::x86_sse2_psra_w; |
| 6044 | break; |
| 6045 | case Intrinsic::x86_sse2_psrai_d: |
| 6046 | NewIntNo = Intrinsic::x86_sse2_psra_d; |
| 6047 | break; |
| 6048 | default: { |
| 6049 | ShAmtVT = MVT::v2i32; |
| 6050 | switch (IntNo) { |
| 6051 | case Intrinsic::x86_mmx_pslli_w: |
| 6052 | NewIntNo = Intrinsic::x86_mmx_psll_w; |
| 6053 | break; |
| 6054 | case Intrinsic::x86_mmx_pslli_d: |
| 6055 | NewIntNo = Intrinsic::x86_mmx_psll_d; |
| 6056 | break; |
| 6057 | case Intrinsic::x86_mmx_pslli_q: |
| 6058 | NewIntNo = Intrinsic::x86_mmx_psll_q; |
| 6059 | break; |
| 6060 | case Intrinsic::x86_mmx_psrli_w: |
| 6061 | NewIntNo = Intrinsic::x86_mmx_psrl_w; |
| 6062 | break; |
| 6063 | case Intrinsic::x86_mmx_psrli_d: |
| 6064 | NewIntNo = Intrinsic::x86_mmx_psrl_d; |
| 6065 | break; |
| 6066 | case Intrinsic::x86_mmx_psrli_q: |
| 6067 | NewIntNo = Intrinsic::x86_mmx_psrl_q; |
| 6068 | break; |
| 6069 | case Intrinsic::x86_mmx_psrai_w: |
| 6070 | NewIntNo = Intrinsic::x86_mmx_psra_w; |
| 6071 | break; |
| 6072 | case Intrinsic::x86_mmx_psrai_d: |
| 6073 | NewIntNo = Intrinsic::x86_mmx_psra_d; |
| 6074 | break; |
| 6075 | default: abort(); // Can't reach here. |
| 6076 | } |
| 6077 | break; |
| 6078 | } |
| 6079 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6080 | MVT VT = Op.getValueType(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6081 | ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 6082 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt)); |
| 6083 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6084 | DAG.getConstant(NewIntNo, MVT::i32), |
| 6085 | Op.getOperand(1), ShAmt); |
| 6086 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 6087 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 6088 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6089 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6090 | SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) { |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6091 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6092 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6093 | |
| 6094 | if (Depth > 0) { |
| 6095 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); |
| 6096 | SDValue Offset = |
| 6097 | DAG.getConstant(TD->getPointerSize(), |
| 6098 | Subtarget->is64Bit() ? MVT::i64 : MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6099 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6100 | DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6101 | FrameAddr, Offset), |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6102 | NULL, 0); |
| 6103 | } |
| 6104 | |
| 6105 | // Just load the return address. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6106 | SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6107 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6108 | RetAddrFI, NULL, 0); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6111 | SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6112 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 6113 | MFI->setFrameAddressIsTaken(true); |
| 6114 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6115 | DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6116 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 6117 | unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP; |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6118 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6119 | while (Depth--) |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6120 | FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0); |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6121 | return FrameAddr; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6124 | SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op, |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 6125 | SelectionDAG &DAG) { |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6126 | return DAG.getIntPtrConstant(2*TD->getPointerSize()); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6129 | SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6130 | { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6131 | MachineFunction &MF = DAG.getMachineFunction(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6132 | SDValue Chain = Op.getOperand(0); |
| 6133 | SDValue Offset = Op.getOperand(1); |
| 6134 | SDValue Handler = Op.getOperand(2); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6135 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6136 | |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6137 | SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP, |
| 6138 | getPointerTy()); |
| 6139 | unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6140 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6141 | SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame, |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6142 | DAG.getIntPtrConstant(-TD->getPointerSize())); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6143 | StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset); |
| 6144 | Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6145 | Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr); |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6146 | MF.getRegInfo().addLiveOut(StoreAddrReg); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6147 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6148 | return DAG.getNode(X86ISD::EH_RETURN, dl, |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6149 | MVT::Other, |
| 6150 | Chain, DAG.getRegister(StoreAddrReg, getPointerTy())); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6153 | SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op, |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6154 | SelectionDAG &DAG) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6155 | SDValue Root = Op.getOperand(0); |
| 6156 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 6157 | SDValue FPtr = Op.getOperand(2); // nested function |
| 6158 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6159 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6160 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6161 | const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6162 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6163 | const X86InstrInfo *TII = |
| 6164 | ((X86TargetMachine&)getTargetMachine()).getInstrInfo(); |
| 6165 | |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6166 | if (Subtarget->is64Bit()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6167 | SDValue OutChains[6]; |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6168 | |
| 6169 | // Large code-model. |
| 6170 | |
| 6171 | const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r); |
| 6172 | const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri); |
| 6173 | |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 6174 | const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10); |
| 6175 | const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6176 | |
| 6177 | const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix |
| 6178 | |
| 6179 | // Load the pointer to the nested function into R11. |
| 6180 | unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6181 | SDValue Addr = Trmp; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6182 | OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6183 | Addr, TrmpAddr, 0); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6184 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6185 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6186 | DAG.getConstant(2, MVT::i64)); |
| 6187 | OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6188 | |
| 6189 | // Load the 'nest' parameter value into R10. |
| 6190 | // R10 is specified in X86CallingConv.td |
| 6191 | OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6192 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6193 | DAG.getConstant(10, MVT::i64)); |
| 6194 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6195 | Addr, TrmpAddr, 10); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6196 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6197 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6198 | DAG.getConstant(12, MVT::i64)); |
| 6199 | OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6200 | |
| 6201 | // Jump to the nested function. |
| 6202 | OpCode = (JMP64r << 8) | REX_WB; // jmpq *... |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6203 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6204 | DAG.getConstant(20, MVT::i64)); |
| 6205 | OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6206 | Addr, TrmpAddr, 20); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6207 | |
| 6208 | unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6209 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6210 | DAG.getConstant(22, MVT::i64)); |
| 6211 | OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6212 | TrmpAddr, 22); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6213 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6214 | SDValue Ops[] = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6215 | { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) }; |
| 6216 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6217 | } else { |
Dan Gohman | bbfb9c5 | 2008-01-31 01:01:48 +0000 | [diff] [blame] | 6218 | const Function *Func = |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6219 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); |
| 6220 | unsigned CC = Func->getCallingConv(); |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6221 | unsigned NestReg; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6222 | |
| 6223 | switch (CC) { |
| 6224 | default: |
| 6225 | assert(0 && "Unsupported calling convention"); |
| 6226 | case CallingConv::C: |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6227 | case CallingConv::X86_StdCall: { |
| 6228 | // Pass 'nest' parameter in ECX. |
| 6229 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6230 | NestReg = X86::ECX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Check that ECX wasn't needed by an 'inreg' parameter. |
| 6233 | const FunctionType *FTy = Func->getFunctionType(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6234 | const AttrListPtr &Attrs = Func->getAttributes(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6235 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6236 | if (!Attrs.isEmpty() && !Func->isVarArg()) { |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6237 | unsigned InRegCount = 0; |
| 6238 | unsigned Idx = 1; |
| 6239 | |
| 6240 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 6241 | E = FTy->param_end(); I != E; ++I, ++Idx) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6242 | if (Attrs.paramHasAttr(Idx, Attribute::InReg)) |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6243 | // FIXME: should only count parameters that are lowered to integers. |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6244 | InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6245 | |
| 6246 | if (InRegCount > 2) { |
| 6247 | cerr << "Nest register in use - reduce number of inreg parameters!\n"; |
| 6248 | abort(); |
| 6249 | } |
| 6250 | } |
| 6251 | break; |
| 6252 | } |
| 6253 | case CallingConv::X86_FastCall: |
Duncan Sands | bf53c29 | 2008-09-10 13:22:10 +0000 | [diff] [blame] | 6254 | case CallingConv::Fast: |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6255 | // Pass 'nest' parameter in EAX. |
| 6256 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6257 | NestReg = X86::EAX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6258 | break; |
| 6259 | } |
| 6260 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6261 | SDValue OutChains[4]; |
| 6262 | SDValue Addr, Disp; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6263 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6264 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6265 | DAG.getConstant(10, MVT::i32)); |
| 6266 | Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6267 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6268 | const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri); |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 6269 | const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6270 | OutChains[0] = DAG.getStore(Root, dl, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6271 | DAG.getConstant(MOV32ri|N86Reg, MVT::i8), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6272 | Trmp, TrmpAddr, 0); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6273 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6274 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6275 | DAG.getConstant(1, MVT::i32)); |
| 6276 | OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6277 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6278 | const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6279 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6280 | DAG.getConstant(5, MVT::i32)); |
| 6281 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6282 | TrmpAddr, 5, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6283 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6284 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6285 | DAG.getConstant(6, MVT::i32)); |
| 6286 | OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6287 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6288 | SDValue Ops[] = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6289 | { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) }; |
| 6290 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6291 | } |
| 6292 | } |
| 6293 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6294 | SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) { |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6295 | /* |
| 6296 | The rounding mode is in bits 11:10 of FPSR, and has the following |
| 6297 | settings: |
| 6298 | 00 Round to nearest |
| 6299 | 01 Round to -inf |
| 6300 | 10 Round to +inf |
| 6301 | 11 Round to 0 |
| 6302 | |
| 6303 | FLT_ROUNDS, on the other hand, expects the following: |
| 6304 | -1 Undefined |
| 6305 | 0 Round to 0 |
| 6306 | 1 Round to nearest |
| 6307 | 2 Round to +inf |
| 6308 | 3 Round to -inf |
| 6309 | |
| 6310 | To perform the conversion, we do: |
| 6311 | (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3) |
| 6312 | */ |
| 6313 | |
| 6314 | MachineFunction &MF = DAG.getMachineFunction(); |
| 6315 | const TargetMachine &TM = MF.getTarget(); |
| 6316 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 6317 | unsigned StackAlignment = TFI.getStackAlignment(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6318 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6319 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6320 | |
| 6321 | // Save FP Control Word to stack slot |
| 6322 | int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6323 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6324 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6325 | SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other, |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 6326 | DAG.getEntryNode(), StackSlot); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6327 | |
| 6328 | // Load FP Control Word from stack slot |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6329 | SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6330 | |
| 6331 | // Transform as necessary |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6332 | SDValue CWD1 = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6333 | DAG.getNode(ISD::SRL, dl, MVT::i16, |
| 6334 | DAG.getNode(ISD::AND, dl, MVT::i16, |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6335 | CWD, DAG.getConstant(0x800, MVT::i16)), |
| 6336 | DAG.getConstant(11, MVT::i8)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6337 | SDValue CWD2 = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6338 | DAG.getNode(ISD::SRL, dl, MVT::i16, |
| 6339 | DAG.getNode(ISD::AND, dl, MVT::i16, |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6340 | CWD, DAG.getConstant(0x400, MVT::i16)), |
| 6341 | DAG.getConstant(9, MVT::i8)); |
| 6342 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6343 | SDValue RetVal = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6344 | DAG.getNode(ISD::AND, dl, MVT::i16, |
| 6345 | DAG.getNode(ISD::ADD, dl, MVT::i16, |
| 6346 | DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2), |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6347 | DAG.getConstant(1, MVT::i16)), |
| 6348 | DAG.getConstant(3, MVT::i16)); |
| 6349 | |
| 6350 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6351 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 6352 | ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6355 | SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6356 | MVT VT = Op.getValueType(); |
| 6357 | MVT OpVT = VT; |
| 6358 | unsigned NumBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6359 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6360 | |
| 6361 | Op = Op.getOperand(0); |
| 6362 | if (VT == MVT::i8) { |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6363 | // Zero extend to i32 since there is not an i8 bsr. |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6364 | OpVT = MVT::i32; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6365 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6366 | } |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6367 | |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6368 | // Issue a bsr (scan bits in reverse) which also sets EFLAGS. |
| 6369 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6370 | Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6371 | |
| 6372 | // If src is zero (i.e. bsr sets ZF), returns NumBits. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6373 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6374 | Ops.push_back(Op); |
| 6375 | Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT)); |
| 6376 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 6377 | Ops.push_back(Op.getValue(1)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6378 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6379 | |
| 6380 | // Finally xor with NumBits-1. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6381 | Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT)); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6382 | |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6383 | if (VT == MVT::i8) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6384 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6385 | return Op; |
| 6386 | } |
| 6387 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6388 | SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6389 | MVT VT = Op.getValueType(); |
| 6390 | MVT OpVT = VT; |
| 6391 | unsigned NumBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6392 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6393 | |
| 6394 | Op = Op.getOperand(0); |
| 6395 | if (VT == MVT::i8) { |
| 6396 | OpVT = MVT::i32; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6397 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6398 | } |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6399 | |
| 6400 | // Issue a bsf (scan bits forward) which also sets EFLAGS. |
| 6401 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6402 | Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6403 | |
| 6404 | // If src is zero (i.e. bsf sets ZF), returns NumBits. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6405 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6406 | Ops.push_back(Op); |
| 6407 | Ops.push_back(DAG.getConstant(NumBits, OpVT)); |
| 6408 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 6409 | Ops.push_back(Op.getValue(1)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6410 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6411 | |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6412 | if (VT == MVT::i8) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6413 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6414 | return Op; |
| 6415 | } |
| 6416 | |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6417 | SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) { |
| 6418 | MVT VT = Op.getValueType(); |
| 6419 | assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply"); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6420 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6421 | |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6422 | // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32); |
| 6423 | // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32); |
| 6424 | // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b ); |
| 6425 | // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi ); |
| 6426 | // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b ); |
| 6427 | // |
| 6428 | // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 ); |
| 6429 | // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 ); |
| 6430 | // return AloBlo + AloBhi + AhiBlo; |
| 6431 | |
| 6432 | SDValue A = Op.getOperand(0); |
| 6433 | SDValue B = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6434 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6435 | SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6436 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 6437 | A, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6438 | SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6439 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 6440 | B, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6441 | SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6442 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6443 | A, B); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6444 | SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6445 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6446 | A, Bhi); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6447 | SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6448 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6449 | Ahi, B); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6450 | AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6451 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 6452 | AloBhi, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6453 | AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6454 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 6455 | AhiBlo, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6456 | SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi); |
| 6457 | Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6458 | return Res; |
| 6459 | } |
| 6460 | |
| 6461 | |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6462 | SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) { |
| 6463 | // Lower the "add/sub/mul with overflow" instruction into a regular ins plus |
| 6464 | // a "setcc" instruction that checks the overflow flag. The "brcond" lowering |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6465 | // looks for this combo and may remove the "setcc" instruction if the "setcc" |
| 6466 | // has only one use. |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6467 | SDNode *N = Op.getNode(); |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6468 | SDValue LHS = N->getOperand(0); |
| 6469 | SDValue RHS = N->getOperand(1); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6470 | unsigned BaseOp = 0; |
| 6471 | unsigned Cond = 0; |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6472 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6473 | |
| 6474 | switch (Op.getOpcode()) { |
| 6475 | default: assert(0 && "Unknown ovf instruction!"); |
| 6476 | case ISD::SADDO: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 6477 | // A subtract of one will be selected as a INC. Note that INC doesn't |
| 6478 | // set CF, so we can't do this for UADDO. |
| 6479 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) |
| 6480 | if (C->getAPIntValue() == 1) { |
| 6481 | BaseOp = X86ISD::INC; |
| 6482 | Cond = X86::COND_O; |
| 6483 | break; |
| 6484 | } |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6485 | BaseOp = X86ISD::ADD; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6486 | Cond = X86::COND_O; |
| 6487 | break; |
| 6488 | case ISD::UADDO: |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6489 | BaseOp = X86ISD::ADD; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6490 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6491 | break; |
| 6492 | case ISD::SSUBO: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 6493 | // A subtract of one will be selected as a DEC. Note that DEC doesn't |
| 6494 | // set CF, so we can't do this for USUBO. |
| 6495 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) |
| 6496 | if (C->getAPIntValue() == 1) { |
| 6497 | BaseOp = X86ISD::DEC; |
| 6498 | Cond = X86::COND_O; |
| 6499 | break; |
| 6500 | } |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6501 | BaseOp = X86ISD::SUB; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6502 | Cond = X86::COND_O; |
| 6503 | break; |
| 6504 | case ISD::USUBO: |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6505 | BaseOp = X86ISD::SUB; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6506 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6507 | break; |
| 6508 | case ISD::SMULO: |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 6509 | BaseOp = X86ISD::SMUL; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6510 | Cond = X86::COND_O; |
| 6511 | break; |
| 6512 | case ISD::UMULO: |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 6513 | BaseOp = X86ISD::UMUL; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6514 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6515 | break; |
| 6516 | } |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6517 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6518 | // Also sets EFLAGS. |
| 6519 | SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6520 | SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS); |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6521 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6522 | SDValue SetCC = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6523 | DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1), |
Bill Wendling | bc5e15e | 2008-12-10 02:01:32 +0000 | [diff] [blame] | 6524 | DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1)); |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6525 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6526 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC); |
| 6527 | return Sum; |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6530 | SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | fd4418f | 2008-06-25 16:07:49 +0000 | [diff] [blame] | 6531 | MVT T = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6532 | DebugLoc dl = Op.getDebugLoc(); |
Andrew Lenharth | a76e2f0 | 2008-03-04 21:13:33 +0000 | [diff] [blame] | 6533 | unsigned Reg = 0; |
| 6534 | unsigned size = 0; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6535 | switch(T.getSimpleVT()) { |
| 6536 | default: |
| 6537 | assert(false && "Invalid value type!"); |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6538 | case MVT::i8: Reg = X86::AL; size = 1; break; |
| 6539 | case MVT::i16: Reg = X86::AX; size = 2; break; |
| 6540 | case MVT::i32: Reg = X86::EAX; size = 4; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6541 | case MVT::i64: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6542 | assert(Subtarget->is64Bit() && "Node not type legal!"); |
| 6543 | Reg = X86::RAX; size = 8; |
Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 6544 | break; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6545 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6546 | SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg, |
Dale Johannesen | d18a462 | 2008-09-11 03:12:59 +0000 | [diff] [blame] | 6547 | Op.getOperand(2), SDValue()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6548 | SDValue Ops[] = { cpIn.getValue(0), |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 6549 | Op.getOperand(1), |
| 6550 | Op.getOperand(3), |
| 6551 | DAG.getTargetConstant(size, MVT::i8), |
| 6552 | cpIn.getValue(1) }; |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6553 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6554 | SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6555 | SDValue cpOut = |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6556 | DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1)); |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6557 | return cpOut; |
| 6558 | } |
| 6559 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6560 | SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 6561 | SelectionDAG &DAG) { |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6562 | assert(Subtarget->is64Bit() && "Result not type legalized?"); |
Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 6563 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6564 | SDValue TheChain = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6565 | DebugLoc dl = Op.getDebugLoc(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6566 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6567 | SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1)); |
| 6568 | SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6569 | rax.getValue(2)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6570 | SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6571 | DAG.getConstant(32, MVT::i8)); |
| 6572 | SDValue Ops[] = { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6573 | DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6574 | rdx.getValue(1) |
| 6575 | }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6576 | return DAG.getMergeValues(Ops, 2, dl); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6579 | SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) { |
| 6580 | SDNode *Node = Op.getNode(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6581 | DebugLoc dl = Node->getDebugLoc(); |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6582 | MVT T = Node->getValueType(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6583 | SDValue negOp = DAG.getNode(ISD::SUB, dl, T, |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 6584 | DAG.getConstant(0, T), Node->getOperand(2)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6585 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl, |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6586 | cast<AtomicSDNode>(Node)->getMemoryVT(), |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6587 | Node->getOperand(0), |
| 6588 | Node->getOperand(1), negOp, |
| 6589 | cast<AtomicSDNode>(Node)->getSrcValue(), |
| 6590 | cast<AtomicSDNode>(Node)->getAlignment()); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6593 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 6594 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6595 | SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6596 | switch (Op.getOpcode()) { |
| 6597 | default: assert(0 && "Should not custom lower this!"); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6598 | case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG); |
| 6599 | case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6600 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 6601 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 6602 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 6603 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 6604 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 6605 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 6606 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 6607 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 6608 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6609 | case ISD::SHL_PARTS: |
| 6610 | case ISD::SRA_PARTS: |
| 6611 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 6612 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 6613 | case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6614 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| 6615 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 6616 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6617 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 6618 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 6619 | case ISD::VSETCC: return LowerVSETCC(Op, DAG); |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 6620 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 6621 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6622 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 6623 | case ISD::CALL: return LowerCALL(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6624 | case ISD::RET: return LowerRET(Op, DAG); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 6625 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6626 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 6627 | case ISD::VAARG: return LowerVAARG(Op, DAG); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 6628 | case ISD::VACOPY: return LowerVACOPY(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6629 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6630 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 6631 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6632 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 6633 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 6634 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6635 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6636 | case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 6637 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6638 | case ISD::CTLZ: return LowerCTLZ(Op, DAG); |
| 6639 | case ISD::CTTZ: return LowerCTTZ(Op, DAG); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6640 | case ISD::MUL: return LowerMUL_V2I64(Op, DAG); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6641 | case ISD::SADDO: |
| 6642 | case ISD::UADDO: |
| 6643 | case ISD::SSUBO: |
| 6644 | case ISD::USUBO: |
| 6645 | case ISD::SMULO: |
| 6646 | case ISD::UMULO: return LowerXALUO(Op, DAG); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6647 | case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6648 | } |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6651 | void X86TargetLowering:: |
| 6652 | ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results, |
| 6653 | SelectionDAG &DAG, unsigned NewOp) { |
| 6654 | MVT T = Node->getValueType(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6655 | DebugLoc dl = Node->getDebugLoc(); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6656 | assert (T == MVT::i64 && "Only know how to expand i64 atomics"); |
| 6657 | |
| 6658 | SDValue Chain = Node->getOperand(0); |
| 6659 | SDValue In1 = Node->getOperand(1); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6660 | SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6661 | Node->getOperand(2), DAG.getIntPtrConstant(0)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6662 | SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6663 | Node->getOperand(2), DAG.getIntPtrConstant(1)); |
| 6664 | // This is a generalized SDNode, not an AtomicSDNode, so it doesn't |
| 6665 | // have a MemOperand. Pass the info through as a normal operand. |
| 6666 | SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand()); |
| 6667 | SDValue Ops[] = { Chain, In1, In2L, In2H, LSI }; |
| 6668 | SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6669 | SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6670 | SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)}; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6671 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6672 | Results.push_back(Result.getValue(2)); |
| 6673 | } |
| 6674 | |
Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 6675 | /// ReplaceNodeResults - Replace a node with an illegal result type |
| 6676 | /// with a new node built out of custom code. |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6677 | void X86TargetLowering::ReplaceNodeResults(SDNode *N, |
| 6678 | SmallVectorImpl<SDValue>&Results, |
| 6679 | SelectionDAG &DAG) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6680 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6681 | switch (N->getOpcode()) { |
Duncan Sands | ed294c4 | 2008-10-20 15:56:33 +0000 | [diff] [blame] | 6682 | default: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6683 | assert(false && "Do not know how to custom type legalize this operation!"); |
| 6684 | return; |
| 6685 | case ISD::FP_TO_SINT: { |
| 6686 | std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG); |
| 6687 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| 6688 | if (FIST.getNode() != 0) { |
| 6689 | MVT VT = N->getValueType(0); |
| 6690 | // Return a load from the stack slot. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6691 | Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6692 | } |
| 6693 | return; |
| 6694 | } |
| 6695 | case ISD::READCYCLECOUNTER: { |
| 6696 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 6697 | SDValue TheChain = N->getOperand(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6698 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6699 | SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6700 | rd.getValue(1)); |
| 6701 | SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6702 | eax.getValue(2)); |
| 6703 | // Use a buildpair to merge the two 32-bit values into a 64-bit one. |
| 6704 | SDValue Ops[] = { eax, edx }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6705 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6706 | Results.push_back(edx.getValue(1)); |
| 6707 | return; |
| 6708 | } |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6709 | case ISD::ATOMIC_CMP_SWAP: { |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6710 | MVT T = N->getValueType(0); |
| 6711 | assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap"); |
| 6712 | SDValue cpInL, cpInH; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6713 | cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6714 | DAG.getConstant(0, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6715 | cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6716 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6717 | cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue()); |
| 6718 | cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6719 | cpInL.getValue(1)); |
| 6720 | SDValue swapInL, swapInH; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6721 | swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6722 | DAG.getConstant(0, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6723 | swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6724 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6725 | swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6726 | cpInH.getValue(1)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6727 | swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6728 | swapInL.getValue(1)); |
| 6729 | SDValue Ops[] = { swapInH.getValue(0), |
| 6730 | N->getOperand(1), |
| 6731 | swapInH.getValue(1) }; |
| 6732 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6733 | SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6734 | SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX, |
| 6735 | MVT::i32, Result.getValue(1)); |
| 6736 | SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX, |
| 6737 | MVT::i32, cpOutL.getValue(2)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6738 | SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)}; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6739 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6740 | Results.push_back(cpOutH.getValue(1)); |
| 6741 | return; |
| 6742 | } |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6743 | case ISD::ATOMIC_LOAD_ADD: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6744 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG); |
| 6745 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6746 | case ISD::ATOMIC_LOAD_AND: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6747 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG); |
| 6748 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6749 | case ISD::ATOMIC_LOAD_NAND: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6750 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG); |
| 6751 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6752 | case ISD::ATOMIC_LOAD_OR: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6753 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG); |
| 6754 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6755 | case ISD::ATOMIC_LOAD_SUB: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6756 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG); |
| 6757 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6758 | case ISD::ATOMIC_LOAD_XOR: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6759 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG); |
| 6760 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6761 | case ISD::ATOMIC_SWAP: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6762 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG); |
| 6763 | return; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6764 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6767 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 6768 | switch (Opcode) { |
| 6769 | default: return NULL; |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6770 | case X86ISD::BSF: return "X86ISD::BSF"; |
| 6771 | case X86ISD::BSR: return "X86ISD::BSR"; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 6772 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 6773 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 6774 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6775 | case X86ISD::FOR: return "X86ISD::FOR"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 6776 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6777 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 6778 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 6779 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6780 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 6781 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 6782 | case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 6783 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 6784 | case X86ISD::FST: return "X86ISD::FST"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6785 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 6786 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 6787 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
Dan Gohman | c7a37d4 | 2008-12-23 22:45:23 +0000 | [diff] [blame] | 6788 | case X86ISD::BT: return "X86ISD::BT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6789 | case X86ISD::CMP: return "X86ISD::CMP"; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6790 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 6791 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 6792 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6793 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 6794 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 6795 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 6796 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 6797 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 6798 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 6799 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 6800 | case X86ISD::PEXTRB: return "X86ISD::PEXTRB"; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 6801 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 6802 | case X86ISD::INSERTPS: return "X86ISD::INSERTPS"; |
| 6803 | case X86ISD::PINSRB: return "X86ISD::PINSRB"; |
Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 6804 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6805 | case X86ISD::PSHUFB: return "X86ISD::PSHUFB"; |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 6806 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 6807 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 6808 | case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; |
| 6809 | case X86ISD::FRCP: return "X86ISD::FRCP"; |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 6810 | case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 6811 | case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress"; |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6812 | case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 6813 | case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN"; |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6814 | case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m"; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6815 | case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG"; |
| 6816 | case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG"; |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 6817 | case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG"; |
| 6818 | case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG"; |
| 6819 | case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG"; |
| 6820 | case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG"; |
| 6821 | case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG"; |
| 6822 | case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG"; |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 6823 | case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL"; |
| 6824 | case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD"; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6825 | case X86ISD::VSHL: return "X86ISD::VSHL"; |
| 6826 | case X86ISD::VSRL: return "X86ISD::VSRL"; |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 6827 | case X86ISD::CMPPD: return "X86ISD::CMPPD"; |
| 6828 | case X86ISD::CMPPS: return "X86ISD::CMPPS"; |
| 6829 | case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB"; |
| 6830 | case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW"; |
| 6831 | case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD"; |
| 6832 | case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ"; |
| 6833 | case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB"; |
| 6834 | case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW"; |
| 6835 | case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD"; |
| 6836 | case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ"; |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6837 | case X86ISD::ADD: return "X86ISD::ADD"; |
| 6838 | case X86ISD::SUB: return "X86ISD::SUB"; |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 6839 | case X86ISD::SMUL: return "X86ISD::SMUL"; |
| 6840 | case X86ISD::UMUL: return "X86ISD::UMUL"; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 6841 | case X86ISD::INC: return "X86ISD::INC"; |
| 6842 | case X86ISD::DEC: return "X86ISD::DEC"; |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 6843 | case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6844 | } |
| 6845 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 6846 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6847 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 6848 | // by AM is legal for this target, for a load/store of the specified type. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6849 | bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6850 | const Type *Ty) const { |
| 6851 | // X86 supports extremely general addressing modes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6852 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6853 | // X86 allows a sign-extended 32-bit immediate field as a displacement. |
| 6854 | if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1) |
| 6855 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6856 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6857 | if (AM.BaseGV) { |
Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 6858 | // We can only fold this if we don't need an extra load. |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6859 | if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false)) |
| 6860 | return false; |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 6861 | // If BaseGV requires a register, we cannot also have a BaseReg. |
| 6862 | if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) && |
| 6863 | AM.HasBaseReg) |
| 6864 | return false; |
Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 6865 | |
| 6866 | // X86-64 only supports addr of globals in small code model. |
| 6867 | if (Subtarget->is64Bit()) { |
| 6868 | if (getTargetMachine().getCodeModel() != CodeModel::Small) |
| 6869 | return false; |
| 6870 | // If lower 4G is not available, then we must use rip-relative addressing. |
| 6871 | if (AM.BaseOffs || AM.Scale > 1) |
| 6872 | return false; |
| 6873 | } |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6874 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6875 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6876 | switch (AM.Scale) { |
| 6877 | case 0: |
| 6878 | case 1: |
| 6879 | case 2: |
| 6880 | case 4: |
| 6881 | case 8: |
| 6882 | // These scales always work. |
| 6883 | break; |
| 6884 | case 3: |
| 6885 | case 5: |
| 6886 | case 9: |
| 6887 | // These scales are formed with basereg+scalereg. Only accept if there is |
| 6888 | // no basereg yet. |
| 6889 | if (AM.HasBaseReg) |
| 6890 | return false; |
| 6891 | break; |
| 6892 | default: // Other stuff never works. |
| 6893 | return false; |
| 6894 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6895 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 6896 | return true; |
| 6897 | } |
| 6898 | |
| 6899 | |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 6900 | bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const { |
| 6901 | if (!Ty1->isInteger() || !Ty2->isInteger()) |
| 6902 | return false; |
Evan Cheng | e127a73 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 6903 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); |
| 6904 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 6905 | if (NumBits1 <= NumBits2) |
Evan Cheng | e127a73 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 6906 | return false; |
| 6907 | return Subtarget->is64Bit() || NumBits1 < 64; |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6910 | bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const { |
| 6911 | if (!VT1.isInteger() || !VT2.isInteger()) |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 6912 | return false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6913 | unsigned NumBits1 = VT1.getSizeInBits(); |
| 6914 | unsigned NumBits2 = VT2.getSizeInBits(); |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 6915 | if (NumBits1 <= NumBits2) |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 6916 | return false; |
| 6917 | return Subtarget->is64Bit() || NumBits1 < 64; |
| 6918 | } |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 6919 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 6920 | bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const { |
Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 6921 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 6922 | return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit(); |
| 6923 | } |
| 6924 | |
| 6925 | bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const { |
Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 6926 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 6927 | return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit(); |
| 6928 | } |
| 6929 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 6930 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 6931 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 6932 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 6933 | /// are assumed to be legal. |
| 6934 | bool |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 6935 | X86TargetLowering::isShuffleMaskLegal(const int *Mask, MVT VT) const { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 6936 | // Only do shuffles on 128-bit vector types for now. |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 6937 | if (VT.getSizeInBits() == 64) |
| 6938 | return false; |
| 6939 | |
| 6940 | // FIXME: pshufb, blends, palignr, shifts. |
| 6941 | return (VT.getVectorNumElements() == 2 || |
| 6942 | ShuffleVectorSDNode::isSplatMask(Mask, VT) || |
| 6943 | isMOVLMask(Mask, VT) || |
| 6944 | isSHUFPMask(Mask, VT) || |
| 6945 | isPSHUFDMask(Mask, VT) || |
| 6946 | isPSHUFHWMask(Mask, VT) || |
| 6947 | isPSHUFLWMask(Mask, VT) || |
| 6948 | isUNPCKLMask(Mask, VT) || |
| 6949 | isUNPCKHMask(Mask, VT) || |
| 6950 | isUNPCKL_v_undef_Mask(Mask, VT) || |
| 6951 | isUNPCKH_v_undef_Mask(Mask, VT)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
Dan Gohman | 7d8143f | 2008-04-09 20:09:42 +0000 | [diff] [blame] | 6954 | bool |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 6955 | X86TargetLowering::isVectorClearMaskLegal(const int *Mask, MVT VT) const { |
| 6956 | unsigned NumElts = VT.getVectorNumElements(); |
| 6957 | // FIXME: This collection of masks seems suspect. |
| 6958 | if (NumElts == 2) |
| 6959 | return true; |
| 6960 | if (NumElts == 4 && VT.getSizeInBits() == 128) { |
| 6961 | return (isMOVLMask(Mask, VT) || |
| 6962 | isCommutedMOVLMask(Mask, VT, true) || |
| 6963 | isSHUFPMask(Mask, VT) || |
| 6964 | isCommutedSHUFPMask(Mask, VT)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 6965 | } |
| 6966 | return false; |
| 6967 | } |
| 6968 | |
| 6969 | //===----------------------------------------------------------------------===// |
| 6970 | // X86 Scheduler Hooks |
| 6971 | //===----------------------------------------------------------------------===// |
| 6972 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6973 | // private utility function |
| 6974 | MachineBasicBlock * |
| 6975 | X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr, |
| 6976 | MachineBasicBlock *MBB, |
| 6977 | unsigned regOpc, |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 6978 | unsigned immOpc, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 6979 | unsigned LoadOpc, |
| 6980 | unsigned CXchgOpc, |
| 6981 | unsigned copyOpc, |
| 6982 | unsigned notOpc, |
| 6983 | unsigned EAXreg, |
| 6984 | TargetRegisterClass *RC, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 6985 | bool invSrc) const { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6986 | // For the atomic bitwise operator, we generate |
| 6987 | // thisMBB: |
| 6988 | // newMBB: |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 6989 | // ld t1 = [bitinstr.addr] |
| 6990 | // op t2 = t1, [bitinstr.val] |
| 6991 | // mov EAX = t1 |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6992 | // lcs dest = [bitinstr.addr], t2 [EAX is implicit] |
| 6993 | // bz newMBB |
| 6994 | // fallthrough -->nextMBB |
| 6995 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 6996 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 6997 | MachineFunction::iterator MBBIter = MBB; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6998 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6999 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7000 | /// First build the CFG |
| 7001 | MachineFunction *F = MBB->getParent(); |
| 7002 | MachineBasicBlock *thisMBB = MBB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7003 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7004 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7005 | F->insert(MBBIter, newMBB); |
| 7006 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7007 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7008 | // Move all successors to thisMBB to nextMBB |
| 7009 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7010 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7011 | // Update thisMBB to fall through to newMBB |
| 7012 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7013 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7014 | // newMBB jumps to itself and fall through to nextMBB |
| 7015 | newMBB->addSuccessor(nextMBB); |
| 7016 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7017 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7018 | // Insert instructions into newMBB based on incoming instruction |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7019 | assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 && |
| 7020 | "unexpected number of operands"); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7021 | DebugLoc dl = bInstr->getDebugLoc(); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7022 | MachineOperand& destOper = bInstr->getOperand(0); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7023 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7024 | int numArgs = bInstr->getNumOperands() - 1; |
| 7025 | for (int i=0; i < numArgs; ++i) |
| 7026 | argOpers[i] = &bInstr->getOperand(i+1); |
| 7027 | |
| 7028 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7029 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
| 7030 | int valArgIndx = lastAddrIndx + 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7031 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7032 | unsigned t1 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7033 | MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7034 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7035 | (*MIB).addOperand(*argOpers[i]); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7036 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7037 | unsigned tt = F->getRegInfo().createVirtualRegister(RC); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7038 | if (invSrc) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7039 | MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7040 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7041 | else |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7042 | tt = t1; |
| 7043 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7044 | unsigned t2 = F->getRegInfo().createVirtualRegister(RC); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7045 | assert((argOpers[valArgIndx]->isReg() || |
| 7046 | argOpers[valArgIndx]->isImm()) && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 7047 | "invalid operand"); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7048 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7049 | MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7050 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7051 | MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7052 | MIB.addReg(tt); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7053 | (*MIB).addOperand(*argOpers[valArgIndx]); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7054 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7055 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7056 | MIB.addReg(t1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7057 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7058 | MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7059 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7060 | (*MIB).addOperand(*argOpers[i]); |
| 7061 | MIB.addReg(t2); |
Mon P Wang | f595266 | 2008-07-17 04:54:06 +0000 | [diff] [blame] | 7062 | assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7063 | (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); |
| 7064 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7065 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg()); |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7066 | MIB.addReg(EAXreg); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7067 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7068 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7069 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7070 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7071 | F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7072 | return nextMBB; |
| 7073 | } |
| 7074 | |
Dale Johannesen | 1b54c7f | 2008-10-03 19:41:08 +0000 | [diff] [blame] | 7075 | // private utility function: 64 bit atomics on 32 bit host. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7076 | MachineBasicBlock * |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7077 | X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr, |
| 7078 | MachineBasicBlock *MBB, |
| 7079 | unsigned regOpcL, |
| 7080 | unsigned regOpcH, |
| 7081 | unsigned immOpcL, |
| 7082 | unsigned immOpcH, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7083 | bool invSrc) const { |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7084 | // For the atomic bitwise operator, we generate |
| 7085 | // thisMBB (instructions are in pairs, except cmpxchg8b) |
| 7086 | // ld t1,t2 = [bitinstr.addr] |
| 7087 | // newMBB: |
| 7088 | // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4) |
| 7089 | // op t5, t6 <- out1, out2, [bitinstr.val] |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7090 | // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val]) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7091 | // mov ECX, EBX <- t5, t6 |
| 7092 | // mov EAX, EDX <- t1, t2 |
| 7093 | // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit] |
| 7094 | // mov t3, t4 <- EAX, EDX |
| 7095 | // bz newMBB |
| 7096 | // result in out1, out2 |
| 7097 | // fallthrough -->nextMBB |
| 7098 | |
| 7099 | const TargetRegisterClass *RC = X86::GR32RegisterClass; |
| 7100 | const unsigned LoadOpc = X86::MOV32rm; |
| 7101 | const unsigned copyOpc = X86::MOV32rr; |
| 7102 | const unsigned NotOpc = X86::NOT32r; |
| 7103 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 7104 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 7105 | MachineFunction::iterator MBBIter = MBB; |
| 7106 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7107 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7108 | /// First build the CFG |
| 7109 | MachineFunction *F = MBB->getParent(); |
| 7110 | MachineBasicBlock *thisMBB = MBB; |
| 7111 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7112 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7113 | F->insert(MBBIter, newMBB); |
| 7114 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7115 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7116 | // Move all successors to thisMBB to nextMBB |
| 7117 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7118 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7119 | // Update thisMBB to fall through to newMBB |
| 7120 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7121 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7122 | // newMBB jumps to itself and fall through to nextMBB |
| 7123 | newMBB->addSuccessor(nextMBB); |
| 7124 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7125 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7126 | DebugLoc dl = bInstr->getDebugLoc(); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7127 | // Insert instructions into newMBB based on incoming instruction |
| 7128 | // There are 8 "real" operands plus 9 implicit def/uses, ignored here. |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7129 | assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 && |
| 7130 | "unexpected number of operands"); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7131 | MachineOperand& dest1Oper = bInstr->getOperand(0); |
| 7132 | MachineOperand& dest2Oper = bInstr->getOperand(1); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7133 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
| 7134 | for (int i=0; i < 2 + X86AddrNumOperands; ++i) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7135 | argOpers[i] = &bInstr->getOperand(i+2); |
| 7136 | |
| 7137 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7138 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7139 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7140 | unsigned t1 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7141 | MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7142 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7143 | (*MIB).addOperand(*argOpers[i]); |
| 7144 | unsigned t2 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7145 | MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7146 | // add 4 to displacement. |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7147 | for (int i=0; i <= lastAddrIndx-2; ++i) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7148 | (*MIB).addOperand(*argOpers[i]); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7149 | MachineOperand newOp3 = *(argOpers[3]); |
| 7150 | if (newOp3.isImm()) |
| 7151 | newOp3.setImm(newOp3.getImm()+4); |
| 7152 | else |
| 7153 | newOp3.setOffset(newOp3.getOffset()+4); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7154 | (*MIB).addOperand(newOp3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7155 | (*MIB).addOperand(*argOpers[lastAddrIndx]); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7156 | |
| 7157 | // t3/4 are defined later, at the bottom of the loop |
| 7158 | unsigned t3 = F->getRegInfo().createVirtualRegister(RC); |
| 7159 | unsigned t4 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7160 | BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg()) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7161 | .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7162 | BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg()) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7163 | .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB); |
| 7164 | |
| 7165 | unsigned tt1 = F->getRegInfo().createVirtualRegister(RC); |
| 7166 | unsigned tt2 = F->getRegInfo().createVirtualRegister(RC); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7167 | if (invSrc) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7168 | MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1); |
| 7169 | MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7170 | } else { |
| 7171 | tt1 = t1; |
| 7172 | tt2 = t2; |
| 7173 | } |
| 7174 | |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7175 | int valArgIndx = lastAddrIndx + 1; |
| 7176 | assert((argOpers[valArgIndx]->isReg() || |
| 7177 | argOpers[valArgIndx]->isImm()) && |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7178 | "invalid operand"); |
| 7179 | unsigned t5 = F->getRegInfo().createVirtualRegister(RC); |
| 7180 | unsigned t6 = F->getRegInfo().createVirtualRegister(RC); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7181 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7182 | MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7183 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7184 | MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7185 | if (regOpcL != X86::MOV32rr) |
| 7186 | MIB.addReg(tt1); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7187 | (*MIB).addOperand(*argOpers[valArgIndx]); |
| 7188 | assert(argOpers[valArgIndx + 1]->isReg() == |
| 7189 | argOpers[valArgIndx]->isReg()); |
| 7190 | assert(argOpers[valArgIndx + 1]->isImm() == |
| 7191 | argOpers[valArgIndx]->isImm()); |
| 7192 | if (argOpers[valArgIndx + 1]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7193 | MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7194 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7195 | MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7196 | if (regOpcH != X86::MOV32rr) |
| 7197 | MIB.addReg(tt2); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7198 | (*MIB).addOperand(*argOpers[valArgIndx + 1]); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7199 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7200 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7201 | MIB.addReg(t1); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7202 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7203 | MIB.addReg(t2); |
| 7204 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7205 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7206 | MIB.addReg(t5); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7207 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7208 | MIB.addReg(t6); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7209 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7210 | MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B)); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7211 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7212 | (*MIB).addOperand(*argOpers[i]); |
| 7213 | |
| 7214 | assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7215 | (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); |
| 7216 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7217 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7218 | MIB.addReg(X86::EAX); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7219 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7220 | MIB.addReg(X86::EDX); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7221 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7222 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7223 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7224 | |
| 7225 | F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. |
| 7226 | return nextMBB; |
| 7227 | } |
| 7228 | |
| 7229 | // private utility function |
| 7230 | MachineBasicBlock * |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7231 | X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr, |
| 7232 | MachineBasicBlock *MBB, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7233 | unsigned cmovOpc) const { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7234 | // For the atomic min/max operator, we generate |
| 7235 | // thisMBB: |
| 7236 | // newMBB: |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7237 | // ld t1 = [min/max.addr] |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7238 | // mov t2 = [min/max.val] |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7239 | // cmp t1, t2 |
| 7240 | // cmov[cond] t2 = t1 |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7241 | // mov EAX = t1 |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7242 | // lcs dest = [bitinstr.addr], t2 [EAX is implicit] |
| 7243 | // bz newMBB |
| 7244 | // fallthrough -->nextMBB |
| 7245 | // |
| 7246 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 7247 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7248 | MachineFunction::iterator MBBIter = MBB; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7249 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7250 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7251 | /// First build the CFG |
| 7252 | MachineFunction *F = MBB->getParent(); |
| 7253 | MachineBasicBlock *thisMBB = MBB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7254 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7255 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7256 | F->insert(MBBIter, newMBB); |
| 7257 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7258 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7259 | // Move all successors to thisMBB to nextMBB |
| 7260 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7261 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7262 | // Update thisMBB to fall through to newMBB |
| 7263 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7264 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7265 | // newMBB jumps to newMBB and fall through to nextMBB |
| 7266 | newMBB->addSuccessor(nextMBB); |
| 7267 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7268 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7269 | DebugLoc dl = mInstr->getDebugLoc(); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7270 | // Insert instructions into newMBB based on incoming instruction |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7271 | assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 && |
| 7272 | "unexpected number of operands"); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7273 | MachineOperand& destOper = mInstr->getOperand(0); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7274 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7275 | int numArgs = mInstr->getNumOperands() - 1; |
| 7276 | for (int i=0; i < numArgs; ++i) |
| 7277 | argOpers[i] = &mInstr->getOperand(i+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7278 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7279 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7280 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
| 7281 | int valArgIndx = lastAddrIndx + 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7282 | |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7283 | unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7284 | MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7285 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7286 | (*MIB).addOperand(*argOpers[i]); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7287 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7288 | // We only support register and immediate values |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7289 | assert((argOpers[valArgIndx]->isReg() || |
| 7290 | argOpers[valArgIndx]->isImm()) && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 7291 | "invalid operand"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7292 | |
| 7293 | unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7294 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7295 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7296 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7297 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7298 | (*MIB).addOperand(*argOpers[valArgIndx]); |
| 7299 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7300 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7301 | MIB.addReg(t1); |
| 7302 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7303 | MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7304 | MIB.addReg(t1); |
| 7305 | MIB.addReg(t2); |
| 7306 | |
| 7307 | // Generate movc |
| 7308 | unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7309 | MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7310 | MIB.addReg(t2); |
| 7311 | MIB.addReg(t1); |
| 7312 | |
| 7313 | // Cmp and exchange if none has modified the memory location |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7314 | MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7315 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7316 | (*MIB).addOperand(*argOpers[i]); |
| 7317 | MIB.addReg(t3); |
Mon P Wang | f595266 | 2008-07-17 04:54:06 +0000 | [diff] [blame] | 7318 | assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7319 | (*MIB).addMemOperand(*F, *mInstr->memoperands_begin()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7320 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7321 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg()); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7322 | MIB.addReg(X86::EAX); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7323 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7324 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7325 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7326 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7327 | F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7328 | return nextMBB; |
| 7329 | } |
| 7330 | |
| 7331 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7332 | MachineBasicBlock * |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 7333 | X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7334 | MachineBasicBlock *BB) const { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7335 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7336 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7337 | switch (MI->getOpcode()) { |
| 7338 | default: assert(false && "Unexpected instr type to insert"); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 7339 | case X86::CMOV_V1I64: |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7340 | case X86::CMOV_FR32: |
| 7341 | case X86::CMOV_FR64: |
| 7342 | case X86::CMOV_V4F32: |
| 7343 | case X86::CMOV_V2F64: |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 7344 | case X86::CMOV_V2I64: { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7345 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 7346 | // diamond control-flow pattern. The incoming instruction knows the |
| 7347 | // destination vreg to set, the condition code register to branch on, the |
| 7348 | // true/false values to select between, and a branch opcode to use. |
| 7349 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7350 | MachineFunction::iterator It = BB; |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7351 | ++It; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7352 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7353 | // thisMBB: |
| 7354 | // ... |
| 7355 | // TrueVal = ... |
| 7356 | // cmpTY ccX, r1, r2 |
| 7357 | // bCC copy1MBB |
| 7358 | // fallthrough --> copy0MBB |
| 7359 | MachineBasicBlock *thisMBB = BB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7360 | MachineFunction *F = BB->getParent(); |
| 7361 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7362 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7363 | unsigned Opc = |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7364 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7365 | BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7366 | F->insert(It, copy0MBB); |
| 7367 | F->insert(It, sinkMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7368 | // Update machine-CFG edges by transferring all successors of the current |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7369 | // block to the new block which will contain the Phi node for the select. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7370 | sinkMBB->transferSuccessors(BB); |
| 7371 | |
| 7372 | // Add the true and fallthrough blocks as its successors. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7373 | BB->addSuccessor(copy0MBB); |
| 7374 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7375 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7376 | // copy0MBB: |
| 7377 | // %FalseValue = ... |
| 7378 | // # fallthrough to sinkMBB |
| 7379 | BB = copy0MBB; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7380 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7381 | // Update machine-CFG edges |
| 7382 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7383 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7384 | // sinkMBB: |
| 7385 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 7386 | // ... |
| 7387 | BB = sinkMBB; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7388 | BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7389 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 7390 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 7391 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7392 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7393 | return BB; |
| 7394 | } |
| 7395 | |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 7396 | case X86::FP32_TO_INT16_IN_MEM: |
| 7397 | case X86::FP32_TO_INT32_IN_MEM: |
| 7398 | case X86::FP32_TO_INT64_IN_MEM: |
| 7399 | case X86::FP64_TO_INT16_IN_MEM: |
| 7400 | case X86::FP64_TO_INT32_IN_MEM: |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 7401 | case X86::FP64_TO_INT64_IN_MEM: |
| 7402 | case X86::FP80_TO_INT16_IN_MEM: |
| 7403 | case X86::FP80_TO_INT32_IN_MEM: |
| 7404 | case X86::FP80_TO_INT64_IN_MEM: { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7405 | // Change the floating point control register to use "round towards zero" |
| 7406 | // mode when truncating to an integer value. |
| 7407 | MachineFunction *F = BB->getParent(); |
| 7408 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7409 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7410 | |
| 7411 | // Load the old value of the high byte of the control word... |
| 7412 | unsigned OldCW = |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 7413 | F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7414 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7415 | CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7416 | |
| 7417 | // Set the high part to be round to zero... |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7418 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7419 | .addImm(0xC7F); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7420 | |
| 7421 | // Reload the modified control word now... |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7422 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7423 | |
| 7424 | // Restore the memory image of control word to original value |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7425 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7426 | .addReg(OldCW); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7427 | |
| 7428 | // Get the X86 opcode to use. |
| 7429 | unsigned Opc; |
| 7430 | switch (MI->getOpcode()) { |
| 7431 | default: assert(0 && "illegal opcode!"); |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 7432 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; |
| 7433 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; |
| 7434 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; |
| 7435 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; |
| 7436 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; |
| 7437 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 7438 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; |
| 7439 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; |
| 7440 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | X86AddressMode AM; |
| 7444 | MachineOperand &Op = MI->getOperand(0); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7445 | if (Op.isReg()) { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7446 | AM.BaseType = X86AddressMode::RegBase; |
| 7447 | AM.Base.Reg = Op.getReg(); |
| 7448 | } else { |
| 7449 | AM.BaseType = X86AddressMode::FrameIndexBase; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 7450 | AM.Base.FrameIndex = Op.getIndex(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7451 | } |
| 7452 | Op = MI->getOperand(1); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7453 | if (Op.isImm()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7454 | AM.Scale = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7455 | Op = MI->getOperand(2); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7456 | if (Op.isImm()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7457 | AM.IndexReg = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7458 | Op = MI->getOperand(3); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7459 | if (Op.isGlobal()) { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7460 | AM.GV = Op.getGlobal(); |
| 7461 | } else { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7462 | AM.Disp = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7463 | } |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7464 | addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM) |
Rafael Espindola | 8ef2b89 | 2009-04-08 08:09:33 +0000 | [diff] [blame] | 7465 | .addReg(MI->getOperand(X86AddrNumOperands).getReg()); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7466 | |
| 7467 | // Reload the original control word now. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7468 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7469 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7470 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7471 | return BB; |
| 7472 | } |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7473 | case X86::ATOMAND32: |
| 7474 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7475 | X86::AND32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7476 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7477 | X86::NOT32r, X86::EAX, |
| 7478 | X86::GR32RegisterClass); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7479 | case X86::ATOMOR32: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7480 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr, |
| 7481 | X86::OR32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7482 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7483 | X86::NOT32r, X86::EAX, |
| 7484 | X86::GR32RegisterClass); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7485 | case X86::ATOMXOR32: |
| 7486 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7487 | X86::XOR32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7488 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7489 | X86::NOT32r, X86::EAX, |
| 7490 | X86::GR32RegisterClass); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7491 | case X86::ATOMNAND32: |
| 7492 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7493 | X86::AND32ri, X86::MOV32rm, |
| 7494 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7495 | X86::NOT32r, X86::EAX, |
| 7496 | X86::GR32RegisterClass, true); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7497 | case X86::ATOMMIN32: |
| 7498 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr); |
| 7499 | case X86::ATOMMAX32: |
| 7500 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr); |
| 7501 | case X86::ATOMUMIN32: |
| 7502 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr); |
| 7503 | case X86::ATOMUMAX32: |
| 7504 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr); |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7505 | |
| 7506 | case X86::ATOMAND16: |
| 7507 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, |
| 7508 | X86::AND16ri, X86::MOV16rm, |
| 7509 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7510 | X86::NOT16r, X86::AX, |
| 7511 | X86::GR16RegisterClass); |
| 7512 | case X86::ATOMOR16: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7513 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7514 | X86::OR16ri, X86::MOV16rm, |
| 7515 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7516 | X86::NOT16r, X86::AX, |
| 7517 | X86::GR16RegisterClass); |
| 7518 | case X86::ATOMXOR16: |
| 7519 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr, |
| 7520 | X86::XOR16ri, X86::MOV16rm, |
| 7521 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7522 | X86::NOT16r, X86::AX, |
| 7523 | X86::GR16RegisterClass); |
| 7524 | case X86::ATOMNAND16: |
| 7525 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, |
| 7526 | X86::AND16ri, X86::MOV16rm, |
| 7527 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7528 | X86::NOT16r, X86::AX, |
| 7529 | X86::GR16RegisterClass, true); |
| 7530 | case X86::ATOMMIN16: |
| 7531 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr); |
| 7532 | case X86::ATOMMAX16: |
| 7533 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr); |
| 7534 | case X86::ATOMUMIN16: |
| 7535 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr); |
| 7536 | case X86::ATOMUMAX16: |
| 7537 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr); |
| 7538 | |
| 7539 | case X86::ATOMAND8: |
| 7540 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, |
| 7541 | X86::AND8ri, X86::MOV8rm, |
| 7542 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7543 | X86::NOT8r, X86::AL, |
| 7544 | X86::GR8RegisterClass); |
| 7545 | case X86::ATOMOR8: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7546 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7547 | X86::OR8ri, X86::MOV8rm, |
| 7548 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7549 | X86::NOT8r, X86::AL, |
| 7550 | X86::GR8RegisterClass); |
| 7551 | case X86::ATOMXOR8: |
| 7552 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr, |
| 7553 | X86::XOR8ri, X86::MOV8rm, |
| 7554 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7555 | X86::NOT8r, X86::AL, |
| 7556 | X86::GR8RegisterClass); |
| 7557 | case X86::ATOMNAND8: |
| 7558 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, |
| 7559 | X86::AND8ri, X86::MOV8rm, |
| 7560 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7561 | X86::NOT8r, X86::AL, |
| 7562 | X86::GR8RegisterClass, true); |
| 7563 | // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way. |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7564 | // This group is for 64-bit host. |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7565 | case X86::ATOMAND64: |
| 7566 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7567 | X86::AND64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7568 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7569 | X86::NOT64r, X86::RAX, |
| 7570 | X86::GR64RegisterClass); |
| 7571 | case X86::ATOMOR64: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7572 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr, |
| 7573 | X86::OR64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7574 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7575 | X86::NOT64r, X86::RAX, |
| 7576 | X86::GR64RegisterClass); |
| 7577 | case X86::ATOMXOR64: |
| 7578 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7579 | X86::XOR64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7580 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7581 | X86::NOT64r, X86::RAX, |
| 7582 | X86::GR64RegisterClass); |
| 7583 | case X86::ATOMNAND64: |
| 7584 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, |
| 7585 | X86::AND64ri32, X86::MOV64rm, |
| 7586 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7587 | X86::NOT64r, X86::RAX, |
| 7588 | X86::GR64RegisterClass, true); |
| 7589 | case X86::ATOMMIN64: |
| 7590 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr); |
| 7591 | case X86::ATOMMAX64: |
| 7592 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr); |
| 7593 | case X86::ATOMUMIN64: |
| 7594 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr); |
| 7595 | case X86::ATOMUMAX64: |
| 7596 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7597 | |
| 7598 | // This group does 64-bit operations on a 32-bit host. |
| 7599 | case X86::ATOMAND6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7600 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7601 | X86::AND32rr, X86::AND32rr, |
| 7602 | X86::AND32ri, X86::AND32ri, |
| 7603 | false); |
| 7604 | case X86::ATOMOR6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7605 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7606 | X86::OR32rr, X86::OR32rr, |
| 7607 | X86::OR32ri, X86::OR32ri, |
| 7608 | false); |
| 7609 | case X86::ATOMXOR6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7610 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7611 | X86::XOR32rr, X86::XOR32rr, |
| 7612 | X86::XOR32ri, X86::XOR32ri, |
| 7613 | false); |
| 7614 | case X86::ATOMNAND6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7615 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7616 | X86::AND32rr, X86::AND32rr, |
| 7617 | X86::AND32ri, X86::AND32ri, |
| 7618 | true); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7619 | case X86::ATOMADD6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7620 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7621 | X86::ADD32rr, X86::ADC32rr, |
| 7622 | X86::ADD32ri, X86::ADC32ri, |
| 7623 | false); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7624 | case X86::ATOMSUB6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7625 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7626 | X86::SUB32rr, X86::SBB32rr, |
| 7627 | X86::SUB32ri, X86::SBB32ri, |
| 7628 | false); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7629 | case X86::ATOMSWAP6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7630 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7631 | X86::MOV32rr, X86::MOV32rr, |
| 7632 | X86::MOV32ri, X86::MOV32ri, |
| 7633 | false); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7634 | } |
| 7635 | } |
| 7636 | |
| 7637 | //===----------------------------------------------------------------------===// |
| 7638 | // X86 Optimization Hooks |
| 7639 | //===----------------------------------------------------------------------===// |
| 7640 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7641 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 7642 | const APInt &Mask, |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 7643 | APInt &KnownZero, |
| 7644 | APInt &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 7645 | const SelectionDAG &DAG, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 7646 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7647 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 7648 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 7649 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 7650 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 7651 | Opc == ISD::INTRINSIC_VOID) && |
| 7652 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 7653 | " is a target node!"); |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7654 | |
Dan Gohman | f4f92f5 | 2008-02-13 23:07:24 +0000 | [diff] [blame] | 7655 | KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7656 | switch (Opc) { |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 7657 | default: break; |
Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 7658 | case X86ISD::ADD: |
| 7659 | case X86ISD::SUB: |
| 7660 | case X86ISD::SMUL: |
| 7661 | case X86ISD::UMUL: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 7662 | case X86ISD::INC: |
| 7663 | case X86ISD::DEC: |
Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 7664 | // These nodes' second result is a boolean. |
| 7665 | if (Op.getResNo() == 0) |
| 7666 | break; |
| 7667 | // Fallthrough |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7668 | case X86ISD::SETCC: |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 7669 | KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(), |
| 7670 | Mask.getBitWidth() - 1); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 7671 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7672 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7673 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 7674 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7675 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7676 | /// node is a GlobalAddress + offset. |
| 7677 | bool X86TargetLowering::isGAPlusOffset(SDNode *N, |
| 7678 | GlobalValue* &GA, int64_t &Offset) const{ |
| 7679 | if (N->getOpcode() == X86ISD::Wrapper) { |
| 7680 | if (isa<GlobalAddressSDNode>(N->getOperand(0))) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7681 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7682 | Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7683 | return true; |
| 7684 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7685 | } |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7686 | return TargetLowering::isGAPlusOffset(N, GA, Offset); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7689 | static bool isBaseAlignmentOfN(unsigned N, SDNode *Base, |
| 7690 | const TargetLowering &TLI) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7691 | GlobalValue *GV; |
Nick Lewycky | 916a9f0 | 2008-02-02 08:29:58 +0000 | [diff] [blame] | 7692 | int64_t Offset = 0; |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7693 | if (TLI.isGAPlusOffset(Base, GV, Offset)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7694 | return (GV->getAlignment() >= N && (Offset % N) == 0); |
Chris Lattner | ba96fbc | 2008-01-26 20:07:42 +0000 | [diff] [blame] | 7695 | // DAG combine handles the stack object case. |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7696 | return false; |
| 7697 | } |
| 7698 | |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 7699 | static bool EltsFromConsecutiveLoads(SDNode *N, const int *PermMask, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7700 | unsigned NumElems, MVT EVT, |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7701 | SDNode *&Base, |
| 7702 | SelectionDAG &DAG, MachineFrameInfo *MFI, |
| 7703 | const TargetLowering &TLI) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7704 | Base = NULL; |
| 7705 | for (unsigned i = 0; i < NumElems; ++i) { |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 7706 | if (PermMask[i] < 0) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7707 | if (!Base) |
| 7708 | return false; |
| 7709 | continue; |
| 7710 | } |
| 7711 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7712 | SDValue Elt = DAG.getShuffleScalarElt(N, i); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7713 | if (!Elt.getNode() || |
| 7714 | (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode()))) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7715 | return false; |
| 7716 | if (!Base) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7717 | Base = Elt.getNode(); |
Evan Cheng | 50d9e72 | 2008-05-10 06:46:49 +0000 | [diff] [blame] | 7718 | if (Base->getOpcode() == ISD::UNDEF) |
| 7719 | return false; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7720 | continue; |
| 7721 | } |
| 7722 | if (Elt.getOpcode() == ISD::UNDEF) |
| 7723 | continue; |
| 7724 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7725 | if (!TLI.isConsecutiveLoad(Elt.getNode(), Base, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7726 | EVT.getSizeInBits()/8, i, MFI)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7727 | return false; |
| 7728 | } |
| 7729 | return true; |
| 7730 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7731 | |
| 7732 | /// PerformShuffleCombine - Combine a vector_shuffle that is equal to |
| 7733 | /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load |
| 7734 | /// if the load addresses are consecutive, non-overlapping, and in the right |
Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 7735 | /// order. In the case of v2i64, it will see if it can rewrite the |
| 7736 | /// shuffle to be an appropriate build vector so it can take advantage of |
| 7737 | // performBuildVectorCombine. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7738 | static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 7739 | const TargetLowering &TLI) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7740 | DebugLoc dl = N->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7741 | MVT VT = N->getValueType(0); |
| 7742 | MVT EVT = VT.getVectorElementType(); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 7743 | const int *PermMask = cast<ShuffleVectorSDNode>(N)->getMask(); |
| 7744 | unsigned NumElems = VT.getVectorNumElements(); |
Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 7745 | |
| 7746 | // For x86-32 machines, if we see an insert and then a shuffle in a v2i64 |
| 7747 | // where the upper half is 0, it is advantageous to rewrite it as a build |
| 7748 | // vector of (0, val) so it can use movq. |
| 7749 | if (VT == MVT::v2i64) { |
| 7750 | SDValue In[2]; |
| 7751 | In[0] = N->getOperand(0); |
| 7752 | In[1] = N->getOperand(1); |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 7753 | unsigned Idx0 = PermMask[0]; |
| 7754 | unsigned Idx1 = PermMask[1]; |
| 7755 | // FIXME: can we take advantage of undef index? |
| 7756 | if (PermMask[0] >= 0 && PermMask[1] >= 0 && |
Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 7757 | In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT && |
| 7758 | In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) { |
| 7759 | ConstantSDNode* InsertVecIdx = |
| 7760 | dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2)); |
| 7761 | if (InsertVecIdx && |
| 7762 | InsertVecIdx->getZExtValue() == (Idx0 % 2) && |
| 7763 | isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) { |
| 7764 | return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, |
| 7765 | In[Idx0/2].getOperand(1), |
| 7766 | In[Idx1/2].getOperand(Idx1 % 2)); |
| 7767 | } |
| 7768 | } |
| 7769 | } |
| 7770 | |
| 7771 | // Try to combine a vector_shuffle into a 128-bit load. |
| 7772 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7773 | SDNode *Base = NULL; |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7774 | if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base, |
| 7775 | DAG, MFI, TLI)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7776 | return SDValue(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7777 | |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 7778 | LoadSDNode *LD = cast<LoadSDNode>(Base); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7779 | if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI)) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7780 | return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7781 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7782 | LD->isVolatile()); |
| 7783 | return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), |
| 7784 | LD->getSrcValue(), LD->getSrcValueOffset(), |
| 7785 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7786 | } |
| 7787 | |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 7788 | /// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7789 | static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG, |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 7790 | TargetLowering::DAGCombinerInfo &DCI, |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 7791 | const X86Subtarget *Subtarget, |
| 7792 | const TargetLowering &TLI) { |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 7793 | unsigned NumOps = N->getNumOperands(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7794 | DebugLoc dl = N->getDebugLoc(); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 7795 | |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7796 | // Ignore single operand BUILD_VECTOR. |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 7797 | if (NumOps == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7798 | return SDValue(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7799 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7800 | MVT VT = N->getValueType(0); |
| 7801 | MVT EVT = VT.getVectorElementType(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7802 | if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit()) |
| 7803 | // We are looking for load i64 and zero extend. We want to transform |
| 7804 | // it before legalizer has a chance to expand it. Also look for i64 |
| 7805 | // BUILD_PAIR bit casted to f64. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7806 | return SDValue(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7807 | // This must be an insertion into a zero vector. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7808 | SDValue HighElt = N->getOperand(1); |
Evan Cheng | 25210da | 2008-05-10 00:58:41 +0000 | [diff] [blame] | 7809 | if (!isZeroNode(HighElt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7810 | return SDValue(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7811 | |
| 7812 | // Value must be a load. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7813 | SDNode *Base = N->getOperand(0).getNode(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7814 | if (!isa<LoadSDNode>(Base)) { |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 7815 | if (Base->getOpcode() != ISD::BIT_CONVERT) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7816 | return SDValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7817 | Base = Base->getOperand(0).getNode(); |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 7818 | if (!isa<LoadSDNode>(Base)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7819 | return SDValue(); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7820 | } |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7821 | |
| 7822 | // Transform it into VZEXT_LOAD addr. |
Evan Cheng | 9bfa03c | 2008-05-12 23:04:07 +0000 | [diff] [blame] | 7823 | LoadSDNode *LD = cast<LoadSDNode>(Base); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7824 | |
Nate Begeman | f7333bf | 2008-05-28 00:24:25 +0000 | [diff] [blame] | 7825 | // Load must not be an extload. |
| 7826 | if (LD->getExtensionType() != ISD::NON_EXTLOAD) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7827 | return SDValue(); |
Mon P Wang | 7ad9b51 | 2009-01-30 07:07:40 +0000 | [diff] [blame] | 7828 | |
| 7829 | // Load type should legal type so we don't have to legalize it. |
| 7830 | if (!TLI.isTypeLegal(VT)) |
| 7831 | return SDValue(); |
| 7832 | |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 7833 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); |
| 7834 | SDValue Ops[] = { LD->getChain(), LD->getBasePtr() }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7835 | SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 7836 | TargetLowering::TargetLoweringOpt TLO(DAG); |
| 7837 | TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1)); |
| 7838 | DCI.CommitTargetLoweringOpt(TLO); |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 7839 | return ResNode; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7840 | } |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7841 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7842 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7843 | static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7844 | const X86Subtarget *Subtarget) { |
| 7845 | DebugLoc DL = N->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7846 | SDValue Cond = N->getOperand(0); |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7847 | // Get the LHS/RHS of the select. |
| 7848 | SDValue LHS = N->getOperand(1); |
| 7849 | SDValue RHS = N->getOperand(2); |
| 7850 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7851 | // If we have SSE[12] support, try to form min/max nodes. |
| 7852 | if (Subtarget->hasSSE2() && |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7853 | (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) && |
| 7854 | Cond.getOpcode() == ISD::SETCC) { |
| 7855 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7856 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7857 | unsigned Opcode = 0; |
| 7858 | if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { |
| 7859 | switch (CC) { |
| 7860 | default: break; |
| 7861 | case ISD::SETOLE: // (X <= Y) ? X : Y -> min |
| 7862 | case ISD::SETULE: |
| 7863 | case ISD::SETLE: |
| 7864 | if (!UnsafeFPMath) break; |
| 7865 | // FALL THROUGH. |
| 7866 | case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min |
| 7867 | case ISD::SETLT: |
| 7868 | Opcode = X86ISD::FMIN; |
| 7869 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7870 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7871 | case ISD::SETOGT: // (X > Y) ? X : Y -> max |
| 7872 | case ISD::SETUGT: |
| 7873 | case ISD::SETGT: |
| 7874 | if (!UnsafeFPMath) break; |
| 7875 | // FALL THROUGH. |
| 7876 | case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max |
| 7877 | case ISD::SETGE: |
| 7878 | Opcode = X86ISD::FMAX; |
| 7879 | break; |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7880 | } |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7881 | } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { |
| 7882 | switch (CC) { |
| 7883 | default: break; |
| 7884 | case ISD::SETOGT: // (X > Y) ? Y : X -> min |
| 7885 | case ISD::SETUGT: |
| 7886 | case ISD::SETGT: |
| 7887 | if (!UnsafeFPMath) break; |
| 7888 | // FALL THROUGH. |
| 7889 | case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min |
| 7890 | case ISD::SETGE: |
| 7891 | Opcode = X86ISD::FMIN; |
| 7892 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7893 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7894 | case ISD::SETOLE: // (X <= Y) ? Y : X -> max |
| 7895 | case ISD::SETULE: |
| 7896 | case ISD::SETLE: |
| 7897 | if (!UnsafeFPMath) break; |
| 7898 | // FALL THROUGH. |
| 7899 | case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max |
| 7900 | case ISD::SETLT: |
| 7901 | Opcode = X86ISD::FMAX; |
| 7902 | break; |
| 7903 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7904 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7905 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7906 | if (Opcode) |
| 7907 | return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7908 | } |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7909 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7910 | // If this is a select between two integer constants, try to do some |
| 7911 | // optimizations. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7912 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) { |
| 7913 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS)) |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7914 | // Don't do this for crazy integer types. |
| 7915 | if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) { |
| 7916 | // If this is efficiently invertible, canonicalize the LHSC/RHSC values |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7917 | // so that TrueC (the true value) is larger than FalseC. |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7918 | bool NeedsCondInvert = false; |
| 7919 | |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7920 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) && |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7921 | // Efficiently invertible. |
| 7922 | (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible. |
| 7923 | (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible. |
| 7924 | isa<ConstantSDNode>(Cond.getOperand(1))))) { |
| 7925 | NeedsCondInvert = true; |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7926 | std::swap(TrueC, FalseC); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7930 | if (FalseC->getAPIntValue() == 0 && |
| 7931 | TrueC->getAPIntValue().isPowerOf2()) { |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7932 | if (NeedsCondInvert) // Invert the condition if needed. |
| 7933 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 7934 | DAG.getConstant(1, Cond.getValueType())); |
| 7935 | |
| 7936 | // Zero extend the condition if needed. |
| 7937 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond); |
| 7938 | |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7939 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 7940 | return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond, |
| 7941 | DAG.getConstant(ShAmt, MVT::i8)); |
| 7942 | } |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 7943 | |
| 7944 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7945 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 7946 | if (NeedsCondInvert) // Invert the condition if needed. |
| 7947 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 7948 | DAG.getConstant(1, Cond.getValueType())); |
| 7949 | |
| 7950 | // Zero extend the condition if needed. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7951 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 7952 | FalseC->getValueType(0), Cond); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 7953 | return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7954 | SDValue(FalseC, 0)); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 7955 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 7956 | |
| 7957 | // Optimize cases that will turn into an LEA instruction. This requires |
| 7958 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| 7959 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| 7960 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| 7961 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| 7962 | |
| 7963 | bool isFastMultiplier = false; |
| 7964 | if (Diff < 10) { |
| 7965 | switch ((unsigned char)Diff) { |
| 7966 | default: break; |
| 7967 | case 1: // result = add base, cond |
| 7968 | case 2: // result = lea base( , cond*2) |
| 7969 | case 3: // result = lea base(cond, cond*2) |
| 7970 | case 4: // result = lea base( , cond*4) |
| 7971 | case 5: // result = lea base(cond, cond*4) |
| 7972 | case 8: // result = lea base( , cond*8) |
| 7973 | case 9: // result = lea base(cond, cond*8) |
| 7974 | isFastMultiplier = true; |
| 7975 | break; |
| 7976 | } |
| 7977 | } |
| 7978 | |
| 7979 | if (isFastMultiplier) { |
| 7980 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| 7981 | if (NeedsCondInvert) // Invert the condition if needed. |
| 7982 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 7983 | DAG.getConstant(1, Cond.getValueType())); |
| 7984 | |
| 7985 | // Zero extend the condition if needed. |
| 7986 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 7987 | Cond); |
| 7988 | // Scale the condition by the difference. |
| 7989 | if (Diff != 1) |
| 7990 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 7991 | DAG.getConstant(Diff, Cond.getValueType())); |
| 7992 | |
| 7993 | // Add the base if non-zero. |
| 7994 | if (FalseC->getAPIntValue() != 0) |
| 7995 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 7996 | SDValue(FalseC, 0)); |
| 7997 | return Cond; |
| 7998 | } |
| 7999 | } |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8000 | } |
| 8001 | } |
| 8002 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8003 | return SDValue(); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8004 | } |
| 8005 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8006 | /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL] |
| 8007 | static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG, |
| 8008 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8009 | DebugLoc DL = N->getDebugLoc(); |
| 8010 | |
| 8011 | // If the flag operand isn't dead, don't touch this CMOV. |
| 8012 | if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty()) |
| 8013 | return SDValue(); |
| 8014 | |
| 8015 | // If this is a select between two integer constants, try to do some |
| 8016 | // optimizations. Note that the operands are ordered the opposite of SELECT |
| 8017 | // operands. |
| 8018 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) { |
| 8019 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
| 8020 | // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is |
| 8021 | // larger than FalseC (the false value). |
| 8022 | X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2); |
| 8023 | |
| 8024 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) { |
| 8025 | CC = X86::GetOppositeBranchCondition(CC); |
| 8026 | std::swap(TrueC, FalseC); |
| 8027 | } |
| 8028 | |
| 8029 | // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8030 | // This is efficient for any integer data type (including i8/i16) and |
| 8031 | // shift amount. |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8032 | if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) { |
| 8033 | SDValue Cond = N->getOperand(3); |
| 8034 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8035 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8036 | |
| 8037 | // Zero extend the condition if needed. |
| 8038 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond); |
| 8039 | |
| 8040 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
| 8041 | Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond, |
| 8042 | DAG.getConstant(ShAmt, MVT::i8)); |
| 8043 | if (N->getNumValues() == 2) // Dead flag value? |
| 8044 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8045 | return Cond; |
| 8046 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8047 | |
| 8048 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient |
| 8049 | // for any integer data type, including i8/i16. |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8050 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
| 8051 | SDValue Cond = N->getOperand(3); |
| 8052 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8053 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8054 | |
| 8055 | // Zero extend the condition if needed. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8056 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 8057 | FalseC->getValueType(0), Cond); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8058 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 8059 | SDValue(FalseC, 0)); |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8060 | |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8061 | if (N->getNumValues() == 2) // Dead flag value? |
| 8062 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8063 | return Cond; |
| 8064 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8065 | |
| 8066 | // Optimize cases that will turn into an LEA instruction. This requires |
| 8067 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| 8068 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| 8069 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| 8070 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| 8071 | |
| 8072 | bool isFastMultiplier = false; |
| 8073 | if (Diff < 10) { |
| 8074 | switch ((unsigned char)Diff) { |
| 8075 | default: break; |
| 8076 | case 1: // result = add base, cond |
| 8077 | case 2: // result = lea base( , cond*2) |
| 8078 | case 3: // result = lea base(cond, cond*2) |
| 8079 | case 4: // result = lea base( , cond*4) |
| 8080 | case 5: // result = lea base(cond, cond*4) |
| 8081 | case 8: // result = lea base( , cond*8) |
| 8082 | case 9: // result = lea base(cond, cond*8) |
| 8083 | isFastMultiplier = true; |
| 8084 | break; |
| 8085 | } |
| 8086 | } |
| 8087 | |
| 8088 | if (isFastMultiplier) { |
| 8089 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| 8090 | SDValue Cond = N->getOperand(3); |
| 8091 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8092 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8093 | // Zero extend the condition if needed. |
| 8094 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 8095 | Cond); |
| 8096 | // Scale the condition by the difference. |
| 8097 | if (Diff != 1) |
| 8098 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 8099 | DAG.getConstant(Diff, Cond.getValueType())); |
| 8100 | |
| 8101 | // Add the base if non-zero. |
| 8102 | if (FalseC->getAPIntValue() != 0) |
| 8103 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 8104 | SDValue(FalseC, 0)); |
| 8105 | if (N->getNumValues() == 2) // Dead flag value? |
| 8106 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8107 | return Cond; |
| 8108 | } |
| 8109 | } |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8110 | } |
| 8111 | } |
| 8112 | return SDValue(); |
| 8113 | } |
| 8114 | |
| 8115 | |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8116 | /// PerformMulCombine - Optimize a single multiply with constant into two |
| 8117 | /// in order to implement it with two cheaper instructions, e.g. |
| 8118 | /// LEA + SHL, LEA + LEA. |
| 8119 | static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG, |
| 8120 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8121 | if (DAG.getMachineFunction(). |
| 8122 | getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
| 8123 | return SDValue(); |
| 8124 | |
| 8125 | if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) |
| 8126 | return SDValue(); |
| 8127 | |
| 8128 | MVT VT = N->getValueType(0); |
| 8129 | if (VT != MVT::i64) |
| 8130 | return SDValue(); |
| 8131 | |
| 8132 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 8133 | if (!C) |
| 8134 | return SDValue(); |
| 8135 | uint64_t MulAmt = C->getZExtValue(); |
| 8136 | if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9) |
| 8137 | return SDValue(); |
| 8138 | |
| 8139 | uint64_t MulAmt1 = 0; |
| 8140 | uint64_t MulAmt2 = 0; |
| 8141 | if ((MulAmt % 9) == 0) { |
| 8142 | MulAmt1 = 9; |
| 8143 | MulAmt2 = MulAmt / 9; |
| 8144 | } else if ((MulAmt % 5) == 0) { |
| 8145 | MulAmt1 = 5; |
| 8146 | MulAmt2 = MulAmt / 5; |
| 8147 | } else if ((MulAmt % 3) == 0) { |
| 8148 | MulAmt1 = 3; |
| 8149 | MulAmt2 = MulAmt / 3; |
| 8150 | } |
| 8151 | if (MulAmt2 && |
| 8152 | (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){ |
| 8153 | DebugLoc DL = N->getDebugLoc(); |
| 8154 | |
| 8155 | if (isPowerOf2_64(MulAmt2) && |
| 8156 | !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD)) |
| 8157 | // If second multiplifer is pow2, issue it first. We want the multiply by |
| 8158 | // 3, 5, or 9 to be folded into the addressing mode unless the lone use |
| 8159 | // is an add. |
| 8160 | std::swap(MulAmt1, MulAmt2); |
| 8161 | |
| 8162 | SDValue NewMul; |
| 8163 | if (isPowerOf2_64(MulAmt1)) |
| 8164 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), |
| 8165 | DAG.getConstant(Log2_64(MulAmt1), MVT::i8)); |
| 8166 | else |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 8167 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8168 | DAG.getConstant(MulAmt1, VT)); |
| 8169 | |
| 8170 | if (isPowerOf2_64(MulAmt2)) |
| 8171 | NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul, |
| 8172 | DAG.getConstant(Log2_64(MulAmt2), MVT::i8)); |
| 8173 | else |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 8174 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8175 | DAG.getConstant(MulAmt2, VT)); |
| 8176 | |
| 8177 | // Do not add new nodes to DAG combiner worklist. |
| 8178 | DCI.CombineTo(N, NewMul, false); |
| 8179 | } |
| 8180 | return SDValue(); |
| 8181 | } |
| 8182 | |
| 8183 | |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8184 | /// PerformShiftCombine - Transforms vector shift nodes to use vector shifts |
| 8185 | /// when possible. |
| 8186 | static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG, |
| 8187 | const X86Subtarget *Subtarget) { |
| 8188 | // On X86 with SSE2 support, we can transform this to a vector shift if |
| 8189 | // all elements are shifted by the same amount. We can't do this in legalize |
| 8190 | // because the a constant vector is typically transformed to a constant pool |
| 8191 | // so we have no knowledge of the shift amount. |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8192 | if (!Subtarget->hasSSE2()) |
| 8193 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8194 | |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8195 | MVT VT = N->getValueType(0); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8196 | if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16) |
| 8197 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8198 | |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8199 | SDValue ShAmtOp = N->getOperand(1); |
| 8200 | MVT EltVT = VT.getVectorElementType(); |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8201 | DebugLoc DL = N->getDebugLoc(); |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8202 | SDValue BaseShAmt; |
| 8203 | if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) { |
| 8204 | unsigned NumElts = VT.getVectorNumElements(); |
| 8205 | unsigned i = 0; |
| 8206 | for (; i != NumElts; ++i) { |
| 8207 | SDValue Arg = ShAmtOp.getOperand(i); |
| 8208 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 8209 | BaseShAmt = Arg; |
| 8210 | break; |
| 8211 | } |
| 8212 | for (; i != NumElts; ++i) { |
| 8213 | SDValue Arg = ShAmtOp.getOperand(i); |
| 8214 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 8215 | if (Arg != BaseShAmt) { |
| 8216 | return SDValue(); |
| 8217 | } |
| 8218 | } |
| 8219 | } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE && |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame^] | 8220 | cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) { |
| 8221 | BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp, |
| 8222 | DAG.getIntPtrConstant(0)); |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8223 | } else |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8224 | return SDValue(); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8225 | |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8226 | if (EltVT.bitsGT(MVT::i32)) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8227 | BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8228 | else if (EltVT.bitsLT(MVT::i32)) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8229 | BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8230 | |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8231 | // The shift amount is identical so we can do a vector shift. |
| 8232 | SDValue ValOp = N->getOperand(0); |
| 8233 | switch (N->getOpcode()) { |
| 8234 | default: |
| 8235 | assert(0 && "Unknown shift opcode!"); |
| 8236 | break; |
| 8237 | case ISD::SHL: |
| 8238 | if (VT == MVT::v2i64) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8239 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8240 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 8241 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8242 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8243 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8244 | DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32), |
| 8245 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8246 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8247 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8248 | DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), |
| 8249 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8250 | break; |
| 8251 | case ISD::SRA: |
| 8252 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8253 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8254 | DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32), |
| 8255 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8256 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8257 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8258 | DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32), |
| 8259 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8260 | break; |
| 8261 | case ISD::SRL: |
| 8262 | if (VT == MVT::v2i64) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8263 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8264 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 8265 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8266 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8267 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8268 | DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32), |
| 8269 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8270 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8271 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8272 | DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32), |
| 8273 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8274 | break; |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8275 | } |
| 8276 | return SDValue(); |
| 8277 | } |
| 8278 | |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8279 | /// PerformSTORECombine - Do target-specific dag combines on STORE nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8280 | static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG, |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8281 | const X86Subtarget *Subtarget) { |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8282 | // Turn load->store of MMX types into GPR load/stores. This avoids clobbering |
| 8283 | // the FP state in cases where an emms may be missing. |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8284 | // A preferable solution to the general problem is to figure out the right |
| 8285 | // places to insert EMMS. This qualifies as a quick hack. |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8286 | |
| 8287 | // Similarly, turn load->store of i64 into double load/stores in 32-bit mode. |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 8288 | StoreSDNode *St = cast<StoreSDNode>(N); |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8289 | MVT VT = St->getValue().getValueType(); |
| 8290 | if (VT.getSizeInBits() != 64) |
| 8291 | return SDValue(); |
| 8292 | |
| 8293 | bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2(); |
| 8294 | if ((VT.isVector() || |
| 8295 | (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) && |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8296 | isa<LoadSDNode>(St->getValue()) && |
| 8297 | !cast<LoadSDNode>(St->getValue())->isVolatile() && |
| 8298 | St->getChain().hasOneUse() && !St->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8299 | SDNode* LdVal = St->getValue().getNode(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8300 | LoadSDNode *Ld = 0; |
| 8301 | int TokenFactorIndex = -1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8302 | SmallVector<SDValue, 8> Ops; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8303 | SDNode* ChainVal = St->getChain().getNode(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8304 | // Must be a store of a load. We currently handle two cases: the load |
| 8305 | // is a direct child, and it's under an intervening TokenFactor. It is |
| 8306 | // possible to dig deeper under nested TokenFactors. |
Dale Johannesen | 14e2ea9 | 2008-02-25 22:29:22 +0000 | [diff] [blame] | 8307 | if (ChainVal == LdVal) |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8308 | Ld = cast<LoadSDNode>(St->getChain()); |
| 8309 | else if (St->getValue().hasOneUse() && |
| 8310 | ChainVal->getOpcode() == ISD::TokenFactor) { |
| 8311 | for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8312 | if (ChainVal->getOperand(i).getNode() == LdVal) { |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8313 | TokenFactorIndex = i; |
| 8314 | Ld = cast<LoadSDNode>(St->getValue()); |
| 8315 | } else |
| 8316 | Ops.push_back(ChainVal->getOperand(i)); |
| 8317 | } |
| 8318 | } |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8319 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8320 | if (!Ld || !ISD::isNormalLoad(Ld)) |
| 8321 | return SDValue(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8322 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8323 | // If this is not the MMX case, i.e. we are just turning i64 load/store |
| 8324 | // into f64 load/store, avoid the transformation if there are multiple |
| 8325 | // uses of the loaded value. |
| 8326 | if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0)) |
| 8327 | return SDValue(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8328 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8329 | DebugLoc LdDL = Ld->getDebugLoc(); |
| 8330 | DebugLoc StDL = N->getDebugLoc(); |
| 8331 | // If we are a 64-bit capable x86, lower to a single movq load/store pair. |
| 8332 | // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store |
| 8333 | // pair instead. |
| 8334 | if (Subtarget->is64Bit() || F64IsLegal) { |
| 8335 | MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64; |
| 8336 | SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), |
| 8337 | Ld->getBasePtr(), Ld->getSrcValue(), |
| 8338 | Ld->getSrcValueOffset(), Ld->isVolatile(), |
| 8339 | Ld->getAlignment()); |
| 8340 | SDValue NewChain = NewLd.getValue(1); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8341 | if (TokenFactorIndex != -1) { |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8342 | Ops.push_back(NewChain); |
| 8343 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8344 | Ops.size()); |
| 8345 | } |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8346 | return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(), |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8347 | St->getSrcValue(), St->getSrcValueOffset(), |
| 8348 | St->isVolatile(), St->getAlignment()); |
| 8349 | } |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8350 | |
| 8351 | // Otherwise, lower to two pairs of 32-bit loads / stores. |
| 8352 | SDValue LoAddr = Ld->getBasePtr(); |
| 8353 | SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr, |
| 8354 | DAG.getConstant(4, MVT::i32)); |
| 8355 | |
| 8356 | SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr, |
| 8357 | Ld->getSrcValue(), Ld->getSrcValueOffset(), |
| 8358 | Ld->isVolatile(), Ld->getAlignment()); |
| 8359 | SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr, |
| 8360 | Ld->getSrcValue(), Ld->getSrcValueOffset()+4, |
| 8361 | Ld->isVolatile(), |
| 8362 | MinAlign(Ld->getAlignment(), 4)); |
| 8363 | |
| 8364 | SDValue NewChain = LoLd.getValue(1); |
| 8365 | if (TokenFactorIndex != -1) { |
| 8366 | Ops.push_back(LoLd); |
| 8367 | Ops.push_back(HiLd); |
| 8368 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
| 8369 | Ops.size()); |
| 8370 | } |
| 8371 | |
| 8372 | LoAddr = St->getBasePtr(); |
| 8373 | HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr, |
| 8374 | DAG.getConstant(4, MVT::i32)); |
| 8375 | |
| 8376 | SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr, |
| 8377 | St->getSrcValue(), St->getSrcValueOffset(), |
| 8378 | St->isVolatile(), St->getAlignment()); |
| 8379 | SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr, |
| 8380 | St->getSrcValue(), |
| 8381 | St->getSrcValueOffset() + 4, |
| 8382 | St->isVolatile(), |
| 8383 | MinAlign(St->getAlignment(), 4)); |
| 8384 | return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8385 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8386 | return SDValue(); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8387 | } |
| 8388 | |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8389 | /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and |
| 8390 | /// X86ISD::FXOR nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8391 | static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) { |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8392 | assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR); |
| 8393 | // F[X]OR(0.0, x) -> x |
| 8394 | // F[X]OR(x, 0.0) -> x |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8395 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 8396 | if (C->getValueAPF().isPosZero()) |
| 8397 | return N->getOperand(1); |
| 8398 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 8399 | if (C->getValueAPF().isPosZero()) |
| 8400 | return N->getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8401 | return SDValue(); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8402 | } |
| 8403 | |
| 8404 | /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8405 | static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) { |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8406 | // FAND(0.0, x) -> 0.0 |
| 8407 | // FAND(x, 0.0) -> 0.0 |
| 8408 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 8409 | if (C->getValueAPF().isPosZero()) |
| 8410 | return N->getOperand(0); |
| 8411 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 8412 | if (C->getValueAPF().isPosZero()) |
| 8413 | return N->getOperand(1); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8414 | return SDValue(); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8415 | } |
| 8416 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 8417 | static SDValue PerformBTCombine(SDNode *N, |
| 8418 | SelectionDAG &DAG, |
| 8419 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8420 | // BT ignores high bits in the bit index operand. |
| 8421 | SDValue Op1 = N->getOperand(1); |
| 8422 | if (Op1.hasOneUse()) { |
| 8423 | unsigned BitWidth = Op1.getValueSizeInBits(); |
| 8424 | APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth)); |
| 8425 | APInt KnownZero, KnownOne; |
| 8426 | TargetLowering::TargetLoweringOpt TLO(DAG); |
| 8427 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 8428 | if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) || |
| 8429 | TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO)) |
| 8430 | DCI.CommitTargetLoweringOpt(TLO); |
| 8431 | } |
| 8432 | return SDValue(); |
| 8433 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8434 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8435 | SDValue X86TargetLowering::PerformDAGCombine(SDNode *N, |
Evan Cheng | 9dd93b3 | 2008-11-05 06:03:38 +0000 | [diff] [blame] | 8436 | DAGCombinerInfo &DCI) const { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8437 | SelectionDAG &DAG = DCI.DAG; |
| 8438 | switch (N->getOpcode()) { |
| 8439 | default: break; |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 8440 | case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this); |
| 8441 | case ISD::BUILD_VECTOR: |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 8442 | return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8443 | case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8444 | case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI); |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8445 | case ISD::MUL: return PerformMulCombine(N, DAG, DCI); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8446 | case ISD::SHL: |
| 8447 | case ISD::SRA: |
| 8448 | case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 8449 | case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget); |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8450 | case X86ISD::FXOR: |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8451 | case X86ISD::FOR: return PerformFORCombine(N, DAG); |
| 8452 | case X86ISD::FAND: return PerformFANDCombine(N, DAG); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 8453 | case X86ISD::BT: return PerformBTCombine(N, DAG, DCI); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8454 | } |
| 8455 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8456 | return SDValue(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8457 | } |
| 8458 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 8459 | //===----------------------------------------------------------------------===// |
| 8460 | // X86 Inline Assembly Support |
| 8461 | //===----------------------------------------------------------------------===// |
| 8462 | |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8463 | /// getConstraintType - Given a constraint letter, return the type of |
| 8464 | /// constraint it is for this target. |
| 8465 | X86TargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8466 | X86TargetLowering::getConstraintType(const std::string &Constraint) const { |
| 8467 | if (Constraint.size() == 1) { |
| 8468 | switch (Constraint[0]) { |
| 8469 | case 'A': |
Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 8470 | return C_Register; |
Chris Lattner | fce84ac | 2008-03-11 19:06:29 +0000 | [diff] [blame] | 8471 | case 'f': |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8472 | case 'r': |
| 8473 | case 'R': |
| 8474 | case 'l': |
| 8475 | case 'q': |
| 8476 | case 'Q': |
| 8477 | case 'x': |
Dale Johannesen | 2ffbcac | 2008-04-01 00:57:48 +0000 | [diff] [blame] | 8478 | case 'y': |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8479 | case 'Y': |
| 8480 | return C_RegisterClass; |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8481 | case 'e': |
| 8482 | case 'Z': |
| 8483 | return C_Other; |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8484 | default: |
| 8485 | break; |
| 8486 | } |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8487 | } |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8488 | return TargetLowering::getConstraintType(Constraint); |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8489 | } |
| 8490 | |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8491 | /// LowerXConstraint - try to replace an X constraint, which matches anything, |
| 8492 | /// with another that has more specific requirements based on the type of the |
| 8493 | /// corresponding operand. |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8494 | const char *X86TargetLowering:: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8495 | LowerXConstraint(MVT ConstraintVT) const { |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8496 | // FP X constraints get lowered to SSE1/2 registers if available, otherwise |
| 8497 | // 'f' like normal targets. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8498 | if (ConstraintVT.isFloatingPoint()) { |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8499 | if (Subtarget->hasSSE2()) |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8500 | return "Y"; |
| 8501 | if (Subtarget->hasSSE1()) |
| 8502 | return "x"; |
| 8503 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8504 | |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8505 | return TargetLowering::LowerXConstraint(ConstraintVT); |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8508 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 8509 | /// vector. If it is invalid, don't add anything to Ops. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8510 | void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8511 | char Constraint, |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 8512 | bool hasMemory, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8513 | std::vector<SDValue>&Ops, |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8514 | SelectionDAG &DAG) const { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8515 | SDValue Result(0, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8516 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8517 | switch (Constraint) { |
| 8518 | default: break; |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 8519 | case 'I': |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8520 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8521 | if (C->getZExtValue() <= 31) { |
| 8522 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8523 | break; |
| 8524 | } |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 8525 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8526 | return; |
Evan Cheng | 364091e | 2008-09-22 23:57:37 +0000 | [diff] [blame] | 8527 | case 'J': |
| 8528 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 8529 | if (C->getZExtValue() <= 63) { |
| 8530 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 8531 | break; |
| 8532 | } |
| 8533 | } |
| 8534 | return; |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8535 | case 'N': |
| 8536 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8537 | if (C->getZExtValue() <= 255) { |
| 8538 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8539 | break; |
| 8540 | } |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8541 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8542 | return; |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8543 | case 'e': { |
| 8544 | // 32-bit signed value |
| 8545 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 8546 | const ConstantInt *CI = C->getConstantIntValue(); |
| 8547 | if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) { |
| 8548 | // Widen to 64 bits here to get it sign extended. |
| 8549 | Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64); |
| 8550 | break; |
| 8551 | } |
| 8552 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 8553 | // memory models; it's complicated. |
| 8554 | } |
| 8555 | return; |
| 8556 | } |
| 8557 | case 'Z': { |
| 8558 | // 32-bit unsigned value |
| 8559 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 8560 | const ConstantInt *CI = C->getConstantIntValue(); |
| 8561 | if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) { |
| 8562 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 8563 | break; |
| 8564 | } |
| 8565 | } |
| 8566 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 8567 | // memory models; it's complicated. |
| 8568 | return; |
| 8569 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8570 | case 'i': { |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8571 | // Literal immediates are always ok. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8572 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8573 | // Widen to 64 bits here to get it sign extended. |
| 8574 | Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8575 | break; |
| 8576 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8577 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8578 | // If we are in non-pic codegen mode, we allow the address of a global (with |
| 8579 | // an optional displacement) to be used with 'i'. |
| 8580 | GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); |
| 8581 | int64_t Offset = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8582 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8583 | // Match either (GA) or (GA+C) |
| 8584 | if (GA) { |
| 8585 | Offset = GA->getOffset(); |
| 8586 | } else if (Op.getOpcode() == ISD::ADD) { |
| 8587 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 8588 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 8589 | if (C && GA) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8590 | Offset = GA->getOffset()+C->getZExtValue(); |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8591 | } else { |
| 8592 | C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 8593 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 8594 | if (C && GA) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8595 | Offset = GA->getOffset()+C->getZExtValue(); |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8596 | else |
| 8597 | C = 0, GA = 0; |
| 8598 | } |
| 8599 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8600 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8601 | if (GA) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8602 | if (hasMemory) |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 8603 | Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 8604 | Offset, DAG); |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 8605 | else |
| 8606 | Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), |
| 8607 | Offset); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8608 | Result = Op; |
| 8609 | break; |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8610 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8611 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8612 | // Otherwise, not valid for this mode. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8613 | return; |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8614 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8615 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8616 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8617 | if (Result.getNode()) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8618 | Ops.push_back(Result); |
| 8619 | return; |
| 8620 | } |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 8621 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory, |
| 8622 | Ops, DAG); |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8623 | } |
| 8624 | |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8625 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 8626 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8627 | MVT VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8628 | if (Constraint.size() == 1) { |
| 8629 | // FIXME: not handling fp-stack yet! |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8630 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8631 | default: break; // Unknown constraint letter |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8632 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 8633 | case 'Q': // Q_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 8634 | if (VT == MVT::i32) |
| 8635 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 8636 | else if (VT == MVT::i16) |
| 8637 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 8638 | else if (VT == MVT::i8) |
Evan Cheng | 1291438 | 2007-08-13 23:27:11 +0000 | [diff] [blame] | 8639 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); |
Chris Lattner | 03e6c70 | 2007-11-04 06:51:12 +0000 | [diff] [blame] | 8640 | else if (VT == MVT::i64) |
| 8641 | return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0); |
| 8642 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8643 | } |
| 8644 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8645 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 8646 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8647 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8648 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8649 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8650 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8651 | MVT VT) const { |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 8652 | // First, see if this is a constraint that directly corresponds to an LLVM |
| 8653 | // register class. |
| 8654 | if (Constraint.size() == 1) { |
| 8655 | // GCC Constraint Letters |
| 8656 | switch (Constraint[0]) { |
| 8657 | default: break; |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8658 | case 'r': // GENERAL_REGS |
| 8659 | case 'R': // LEGACY_REGS |
| 8660 | case 'l': // INDEX_REGS |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 8661 | if (VT == MVT::i8) |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8662 | return std::make_pair(0U, X86::GR8RegisterClass); |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 8663 | if (VT == MVT::i16) |
| 8664 | return std::make_pair(0U, X86::GR16RegisterClass); |
| 8665 | if (VT == MVT::i32 || !Subtarget->is64Bit()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8666 | return std::make_pair(0U, X86::GR32RegisterClass); |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 8667 | return std::make_pair(0U, X86::GR64RegisterClass); |
Chris Lattner | fce84ac | 2008-03-11 19:06:29 +0000 | [diff] [blame] | 8668 | case 'f': // FP Stack registers. |
| 8669 | // If SSE is enabled for this VT, use f80 to ensure the isel moves the |
| 8670 | // value to the correct fpstack register class. |
| 8671 | if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT)) |
| 8672 | return std::make_pair(0U, X86::RFP32RegisterClass); |
| 8673 | if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT)) |
| 8674 | return std::make_pair(0U, X86::RFP64RegisterClass); |
| 8675 | return std::make_pair(0U, X86::RFP80RegisterClass); |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 8676 | case 'y': // MMX_REGS if MMX allowed. |
| 8677 | if (!Subtarget->hasMMX()) break; |
| 8678 | return std::make_pair(0U, X86::VR64RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8679 | case 'Y': // SSE_REGS if SSE2 allowed |
| 8680 | if (!Subtarget->hasSSE2()) break; |
| 8681 | // FALL THROUGH. |
| 8682 | case 'x': // SSE_REGS if SSE1 allowed |
| 8683 | if (!Subtarget->hasSSE1()) break; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8684 | |
| 8685 | switch (VT.getSimpleVT()) { |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8686 | default: break; |
| 8687 | // Scalar SSE types. |
| 8688 | case MVT::f32: |
| 8689 | case MVT::i32: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 8690 | return std::make_pair(0U, X86::FR32RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8691 | case MVT::f64: |
| 8692 | case MVT::i64: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 8693 | return std::make_pair(0U, X86::FR64RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8694 | // Vector types. |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 8695 | case MVT::v16i8: |
| 8696 | case MVT::v8i16: |
| 8697 | case MVT::v4i32: |
| 8698 | case MVT::v2i64: |
| 8699 | case MVT::v4f32: |
| 8700 | case MVT::v2f64: |
| 8701 | return std::make_pair(0U, X86::VR128RegisterClass); |
| 8702 | } |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 8703 | break; |
| 8704 | } |
| 8705 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8706 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8707 | // Use the default implementation in TargetLowering to convert the register |
| 8708 | // constraint into a member of a register class. |
| 8709 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 8710 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 8711 | |
| 8712 | // Not found as a standard register? |
| 8713 | if (Res.second == 0) { |
| 8714 | // GCC calls "st(0)" just plain "st". |
| 8715 | if (StringsEqualNoCase("{st}", Constraint)) { |
| 8716 | Res.first = X86::ST0; |
Chris Lattner | 9b4baf1 | 2007-09-24 05:27:37 +0000 | [diff] [blame] | 8717 | Res.second = X86::RFP80RegisterClass; |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 8718 | } |
Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 8719 | // 'A' means EAX + EDX. |
| 8720 | if (Constraint == "A") { |
| 8721 | Res.first = X86::EAX; |
| 8722 | Res.second = X86::GRADRegisterClass; |
| 8723 | } |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 8724 | return Res; |
| 8725 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8726 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8727 | // Otherwise, check to see if this is a register class of the wrong value |
| 8728 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 8729 | // turn into {ax},{dx}. |
| 8730 | if (Res.second->hasType(VT)) |
| 8731 | return Res; // Correct type already, nothing to do. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8732 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8733 | // All of the single-register GCC register classes map their values onto |
| 8734 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 8735 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 8736 | // class and return the appropriate register. |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 8737 | if (Res.second == X86::GR16RegisterClass) { |
| 8738 | if (VT == MVT::i8) { |
| 8739 | unsigned DestReg = 0; |
| 8740 | switch (Res.first) { |
| 8741 | default: break; |
| 8742 | case X86::AX: DestReg = X86::AL; break; |
| 8743 | case X86::DX: DestReg = X86::DL; break; |
| 8744 | case X86::CX: DestReg = X86::CL; break; |
| 8745 | case X86::BX: DestReg = X86::BL; break; |
| 8746 | } |
| 8747 | if (DestReg) { |
| 8748 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 8749 | Res.second = X86::GR8RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 8750 | } |
| 8751 | } else if (VT == MVT::i32) { |
| 8752 | unsigned DestReg = 0; |
| 8753 | switch (Res.first) { |
| 8754 | default: break; |
| 8755 | case X86::AX: DestReg = X86::EAX; break; |
| 8756 | case X86::DX: DestReg = X86::EDX; break; |
| 8757 | case X86::CX: DestReg = X86::ECX; break; |
| 8758 | case X86::BX: DestReg = X86::EBX; break; |
| 8759 | case X86::SI: DestReg = X86::ESI; break; |
| 8760 | case X86::DI: DestReg = X86::EDI; break; |
| 8761 | case X86::BP: DestReg = X86::EBP; break; |
| 8762 | case X86::SP: DestReg = X86::ESP; break; |
| 8763 | } |
| 8764 | if (DestReg) { |
| 8765 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 8766 | Res.second = X86::GR32RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 8767 | } |
| 8768 | } else if (VT == MVT::i64) { |
| 8769 | unsigned DestReg = 0; |
| 8770 | switch (Res.first) { |
| 8771 | default: break; |
| 8772 | case X86::AX: DestReg = X86::RAX; break; |
| 8773 | case X86::DX: DestReg = X86::RDX; break; |
| 8774 | case X86::CX: DestReg = X86::RCX; break; |
| 8775 | case X86::BX: DestReg = X86::RBX; break; |
| 8776 | case X86::SI: DestReg = X86::RSI; break; |
| 8777 | case X86::DI: DestReg = X86::RDI; break; |
| 8778 | case X86::BP: DestReg = X86::RBP; break; |
| 8779 | case X86::SP: DestReg = X86::RSP; break; |
| 8780 | } |
| 8781 | if (DestReg) { |
| 8782 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 8783 | Res.second = X86::GR64RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 8784 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8785 | } |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 8786 | } else if (Res.second == X86::FR32RegisterClass || |
| 8787 | Res.second == X86::FR64RegisterClass || |
| 8788 | Res.second == X86::VR128RegisterClass) { |
| 8789 | // Handle references to XMM physical registers that got mapped into the |
| 8790 | // wrong class. This can happen with constraints like {xmm0} where the |
| 8791 | // target independent register mapper will just pick the first match it can |
| 8792 | // find, ignoring the required type. |
| 8793 | if (VT == MVT::f32) |
| 8794 | Res.second = X86::FR32RegisterClass; |
| 8795 | else if (VT == MVT::f64) |
| 8796 | Res.second = X86::FR64RegisterClass; |
| 8797 | else if (X86::VR128RegisterClass->hasType(VT)) |
| 8798 | Res.second = X86::VR128RegisterClass; |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8799 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8800 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8801 | return Res; |
| 8802 | } |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8803 | |
| 8804 | //===----------------------------------------------------------------------===// |
| 8805 | // X86 Widen vector type |
| 8806 | //===----------------------------------------------------------------------===// |
| 8807 | |
| 8808 | /// getWidenVectorType: given a vector type, returns the type to widen |
| 8809 | /// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself. |
| 8810 | /// If there is no vector type that we want to widen to, returns MVT::Other |
Mon P Wang | f007a8b | 2008-11-06 05:31:54 +0000 | [diff] [blame] | 8811 | /// When and where to widen is target dependent based on the cost of |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8812 | /// scalarizing vs using the wider vector type. |
| 8813 | |
Dan Gohman | c13cf13 | 2009-01-15 17:34:08 +0000 | [diff] [blame] | 8814 | MVT X86TargetLowering::getWidenVectorType(MVT VT) const { |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8815 | assert(VT.isVector()); |
| 8816 | if (isTypeLegal(VT)) |
| 8817 | return VT; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8818 | |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8819 | // TODO: In computeRegisterProperty, we can compute the list of legal vector |
| 8820 | // type based on element type. This would speed up our search (though |
| 8821 | // it may not be worth it since the size of the list is relatively |
| 8822 | // small). |
| 8823 | MVT EltVT = VT.getVectorElementType(); |
| 8824 | unsigned NElts = VT.getVectorNumElements(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8825 | |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8826 | // On X86, it make sense to widen any vector wider than 1 |
| 8827 | if (NElts <= 1) |
| 8828 | return MVT::Other; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8829 | |
| 8830 | for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE; |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8831 | nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) { |
| 8832 | MVT SVT = (MVT::SimpleValueType)nVT; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8833 | |
| 8834 | if (isTypeLegal(SVT) && |
| 8835 | SVT.getVectorElementType() == EltVT && |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 8836 | SVT.getVectorNumElements() > NElts) |
| 8837 | return SVT; |
| 8838 | } |
| 8839 | return MVT::Other; |
| 8840 | } |