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" |
| 18 | #include "X86TargetMachine.h" |
| 19 | #include "llvm/CallingConv.h" |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalAlias.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" |
Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 26 | #include "llvm/Intrinsics.h" |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 27 | #include "llvm/LLVMContext.h" |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/BitVector.h" |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/VectorExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/PseudoSourceValue.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" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringExtras.h" |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 44 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
Mon P Wang | 9f22a4a | 2008-11-24 02:10:43 +0000 | [diff] [blame] | 48 | DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX")); |
Mon P Wang | 3c81d35 | 2008-11-23 04:37:22 +0000 | [diff] [blame] | 49 | |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 50 | // Forward declarations. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 51 | static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 52 | SDValue V2); |
Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 53 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 54 | static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) { |
| 55 | switch (TM.getSubtarget<X86Subtarget>().TargetType) { |
| 56 | default: llvm_unreachable("unknown subtarget type"); |
| 57 | case X86Subtarget::isDarwin: |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 58 | return new TargetLoweringObjectFileMachO(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 59 | case X86Subtarget::isELF: |
| 60 | return new TargetLoweringObjectFileELF(); |
| 61 | case X86Subtarget::isMingw: |
| 62 | case X86Subtarget::isCygwin: |
| 63 | case X86Subtarget::isWindows: |
| 64 | return new TargetLoweringObjectFileCOFF(); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 69 | X86TargetLowering::X86TargetLowering(X86TargetMachine &TM) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 70 | : TargetLowering(TM, createTLOF(TM)) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 71 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 72 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 73 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 74 | X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 75 | |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 76 | RegInfo = TM.getRegisterInfo(); |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 77 | TD = getTargetData(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 78 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 79 | // Set up the TargetLowering object. |
| 80 | |
| 81 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 82 | setShiftAmountType(MVT::i8); |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 83 | setBooleanContents(ZeroOrOneBooleanContent); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 84 | setSchedulingPreference(SchedulingForRegPressure); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 85 | setStackPointerRegisterToSaveRestore(X86StackPtr); |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 86 | |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 87 | if (Subtarget->isTargetDarwin()) { |
Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 88 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 89 | setUseUnderscoreSetJmp(false); |
| 90 | setUseUnderscoreLongJmp(false); |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 91 | } else if (Subtarget->isTargetMingw()) { |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 92 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 93 | setUseUnderscoreSetJmp(true); |
| 94 | setUseUnderscoreLongJmp(false); |
| 95 | } else { |
| 96 | setUseUnderscoreSetJmp(true); |
| 97 | setUseUnderscoreLongJmp(true); |
| 98 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 99 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 100 | // Set up the register classes. |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 101 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 102 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 103 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 104 | if (Subtarget->is64Bit()) |
| 105 | addRegisterClass(MVT::i64, X86::GR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 106 | |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 107 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 108 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 109 | // We don't accept any truncstore of integer registers. |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 110 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
| 111 | setTruncStoreAction(MVT::i64, MVT::i16, Expand); |
| 112 | setTruncStoreAction(MVT::i64, MVT::i8 , Expand); |
| 113 | setTruncStoreAction(MVT::i32, MVT::i16, Expand); |
| 114 | setTruncStoreAction(MVT::i32, MVT::i8 , Expand); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 115 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); |
| 116 | |
| 117 | // SETOEQ and SETUNE require checking two conditions. |
| 118 | setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand); |
| 119 | setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand); |
| 120 | setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand); |
| 121 | setCondCodeAction(ISD::SETUNE, MVT::f32, Expand); |
| 122 | setCondCodeAction(ISD::SETUNE, MVT::f64, Expand); |
| 123 | setCondCodeAction(ISD::SETUNE, MVT::f80, Expand); |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 124 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 125 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 126 | // operation. |
| 127 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 128 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 129 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 130 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 131 | if (Subtarget->is64Bit()) { |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 132 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 133 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 134 | } else if (!UseSoftFloat) { |
| 135 | if (X86ScalarSSEf64) { |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 136 | // We have an impenetrably clever algorithm for ui64->double only. |
| 137 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 138 | } |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 139 | // We have an algorithm for SSE2, and we turn this into a 64-bit |
| 140 | // FILD for other targets. |
| 141 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 143 | |
| 144 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 145 | // this operation. |
| 146 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 147 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 148 | |
Devang Patel | 6a78489 | 2009-06-05 18:48:29 +0000 | [diff] [blame] | 149 | if (!UseSoftFloat) { |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 150 | // SSE has no i16 to fp conversion, only i32 |
| 151 | if (X86ScalarSSEf32) { |
| 152 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| 153 | // f32 and f64 cases are Legal, f80 case is not |
| 154 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 155 | } else { |
| 156 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 157 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 158 | } |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 159 | } else { |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 160 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| 161 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 163 | |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 164 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 |
| 165 | // are Legal, f80 is custom lowered. |
| 166 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 167 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 168 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 169 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 170 | // this operation. |
| 171 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 172 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 173 | |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 174 | if (X86ScalarSSEf32) { |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 175 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 176 | // f32 and f64 cases are Legal, f80 case is not |
| 177 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 178 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 179 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 180 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 184 | // conversion. |
| 185 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 186 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 187 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 188 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 189 | if (Subtarget->is64Bit()) { |
| 190 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 192 | } else if (!UseSoftFloat) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 193 | if (X86ScalarSSEf32 && !Subtarget->hasSSE3()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 194 | // Expand FP_TO_UINT into a select. |
| 195 | // FIXME: We would like to use a Custom expander here eventually to do |
| 196 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 197 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 198 | else |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 199 | // With SSE3 we can use fisttpll to convert to a signed i64; without |
| 200 | // SSE, we're stuck with a fistpll. |
| 201 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 202 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 399610a | 2006-12-05 18:22:22 +0000 | [diff] [blame] | 204 | // 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] | 205 | if (!X86ScalarSSEf64) { |
Chris Lattner | f3597a1 | 2006-12-05 18:45:06 +0000 | [diff] [blame] | 206 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 207 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
| 208 | } |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 209 | |
Dan Gohman | b00ee21 | 2008-02-18 19:34:53 +0000 | [diff] [blame] | 210 | // Scalar integer divide and remainder are lowered to use operations that |
| 211 | // produce two results, to match the available instructions. This exposes |
| 212 | // the two-result form to trivial CSE, which is able to combine x/y and x%y |
| 213 | // into a single instruction. |
| 214 | // |
| 215 | // Scalar integer multiply-high is also lowered to use two-result |
| 216 | // operations, to match the available instructions. However, plain multiply |
| 217 | // (low) operations are left as Legal, as there are single-result |
| 218 | // instructions for this in x86. Using the two-result multiply instructions |
| 219 | // 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] | 220 | setOperationAction(ISD::MULHS , MVT::i8 , Expand); |
| 221 | setOperationAction(ISD::MULHU , MVT::i8 , Expand); |
| 222 | setOperationAction(ISD::SDIV , MVT::i8 , Expand); |
| 223 | setOperationAction(ISD::UDIV , MVT::i8 , Expand); |
| 224 | setOperationAction(ISD::SREM , MVT::i8 , Expand); |
| 225 | setOperationAction(ISD::UREM , MVT::i8 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 226 | setOperationAction(ISD::MULHS , MVT::i16 , Expand); |
| 227 | setOperationAction(ISD::MULHU , MVT::i16 , Expand); |
| 228 | setOperationAction(ISD::SDIV , MVT::i16 , Expand); |
| 229 | setOperationAction(ISD::UDIV , MVT::i16 , Expand); |
| 230 | setOperationAction(ISD::SREM , MVT::i16 , Expand); |
| 231 | setOperationAction(ISD::UREM , MVT::i16 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 232 | setOperationAction(ISD::MULHS , MVT::i32 , Expand); |
| 233 | setOperationAction(ISD::MULHU , MVT::i32 , Expand); |
| 234 | setOperationAction(ISD::SDIV , MVT::i32 , Expand); |
| 235 | setOperationAction(ISD::UDIV , MVT::i32 , Expand); |
| 236 | setOperationAction(ISD::SREM , MVT::i32 , Expand); |
| 237 | setOperationAction(ISD::UREM , MVT::i32 , Expand); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 238 | setOperationAction(ISD::MULHS , MVT::i64 , Expand); |
| 239 | setOperationAction(ISD::MULHU , MVT::i64 , Expand); |
| 240 | setOperationAction(ISD::SDIV , MVT::i64 , Expand); |
| 241 | setOperationAction(ISD::UDIV , MVT::i64 , Expand); |
| 242 | setOperationAction(ISD::SREM , MVT::i64 , Expand); |
| 243 | setOperationAction(ISD::UREM , MVT::i64 , Expand); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 244 | |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 245 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 246 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 247 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 248 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 249 | if (Subtarget->is64Bit()) |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 250 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
| 251 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); |
| 252 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 253 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 254 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
Chris Lattner | d110822 | 2008-03-07 06:36:32 +0000 | [diff] [blame] | 255 | setOperationAction(ISD::FREM , MVT::f32 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 256 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
Chris Lattner | d110822 | 2008-03-07 06:36:32 +0000 | [diff] [blame] | 257 | setOperationAction(ISD::FREM , MVT::f80 , Expand); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 258 | setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 259 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 260 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 261 | setOperationAction(ISD::CTTZ , MVT::i8 , Custom); |
| 262 | setOperationAction(ISD::CTLZ , MVT::i8 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 263 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 264 | setOperationAction(ISD::CTTZ , MVT::i16 , Custom); |
| 265 | setOperationAction(ISD::CTLZ , MVT::i16 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 266 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 267 | setOperationAction(ISD::CTTZ , MVT::i32 , Custom); |
| 268 | setOperationAction(ISD::CTLZ , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 269 | if (Subtarget->is64Bit()) { |
| 270 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 271 | setOperationAction(ISD::CTTZ , MVT::i64 , Custom); |
| 272 | setOperationAction(ISD::CTLZ , MVT::i64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 275 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 276 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 277 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 278 | // These should be promoted to a larger select which is supported. |
| 279 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 280 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 281 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 282 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 283 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 284 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 285 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 286 | setOperationAction(ISD::SELECT , MVT::f80 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 287 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 288 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 289 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 290 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 291 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 292 | setOperationAction(ISD::SETCC , MVT::f80 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 293 | if (Subtarget->is64Bit()) { |
| 294 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| 295 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| 296 | } |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 297 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 298 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 299 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 300 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 301 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 302 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 303 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 304 | if (Subtarget->is64Bit()) |
| 305 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 306 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 307 | if (Subtarget->is64Bit()) { |
| 308 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 309 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 310 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 311 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 312 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 313 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 314 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 315 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 316 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 317 | if (Subtarget->is64Bit()) { |
| 318 | setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom); |
| 319 | setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom); |
| 320 | setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom); |
| 321 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 322 | |
Evan Cheng | d2cde68 | 2008-03-10 19:38:10 +0000 | [diff] [blame] | 323 | if (Subtarget->hasSSE1()) |
| 324 | setOperationAction(ISD::PREFETCH , MVT::Other, Legal); |
Evan Cheng | 27b7db5 | 2008-03-08 00:58:38 +0000 | [diff] [blame] | 325 | |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 326 | if (!Subtarget->hasSSE2()) |
| 327 | setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand); |
| 328 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 329 | // Expand certain atomics |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 330 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom); |
| 331 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom); |
| 332 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); |
| 333 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); |
Bill Wendling | 5bf1b4e | 2008-08-20 00:28:16 +0000 | [diff] [blame] | 334 | |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 335 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom); |
| 336 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom); |
| 337 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); |
| 338 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 339 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 340 | if (!Subtarget->is64Bit()) { |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 341 | setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); |
| 342 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); |
| 343 | setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); |
| 344 | setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); |
| 345 | setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); |
| 346 | setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom); |
| 347 | setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 350 | // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion. |
| 351 | setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 352 | // FIXME - use subtarget debug flags |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 353 | if (!Subtarget->isTargetDarwin() && |
| 354 | !Subtarget->isTargetELF() && |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 355 | !Subtarget->isTargetCygMing()) { |
| 356 | setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand); |
| 357 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| 358 | } |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 359 | |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 360 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); |
| 361 | setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); |
| 362 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 363 | setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); |
| 364 | if (Subtarget->is64Bit()) { |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 365 | setExceptionPointerRegister(X86::RAX); |
| 366 | setExceptionSelectorRegister(X86::RDX); |
| 367 | } else { |
| 368 | setExceptionPointerRegister(X86::EAX); |
| 369 | setExceptionSelectorRegister(X86::EDX); |
| 370 | } |
Anton Korobeynikov | 3825262 | 2007-09-03 00:36:06 +0000 | [diff] [blame] | 371 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 372 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom); |
| 373 | |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 374 | setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 375 | |
Chris Lattner | da68d30 | 2008-01-15 21:58:22 +0000 | [diff] [blame] | 376 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 377 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 378 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 379 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 380 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 381 | if (Subtarget->is64Bit()) { |
| 382 | setOperationAction(ISD::VAARG , MVT::Other, Custom); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 383 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 384 | } else { |
| 385 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 386 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 387 | } |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 388 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 389 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 390 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 391 | if (Subtarget->is64Bit()) |
| 392 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 393 | if (Subtarget->isTargetCygMing()) |
| 394 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); |
| 395 | else |
| 396 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 397 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 398 | if (!UseSoftFloat && X86ScalarSSEf64) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 399 | // f32 and f64 use SSE. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 400 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 401 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 402 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 403 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 404 | // Use ANDPD to simulate FABS. |
| 405 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 406 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 407 | |
| 408 | // Use XORP to simulate FNEG. |
| 409 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 410 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 411 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 412 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| 413 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 414 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 415 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 416 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 417 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 418 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 419 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 420 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 421 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 422 | // Expand FP immediates into loads from the stack, except for the special |
| 423 | // cases we handle. |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 424 | addLegalFPImmediate(APFloat(+0.0)); // xorpd |
| 425 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 426 | } else if (!UseSoftFloat && X86ScalarSSEf32) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 427 | // Use SSE for f32, x87 for f64. |
| 428 | // Set up the FP register classes. |
| 429 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 430 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 431 | |
| 432 | // Use ANDPS to simulate FABS. |
| 433 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 434 | |
| 435 | // Use XORP to simulate FNEG. |
| 436 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 437 | |
| 438 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 439 | |
| 440 | // Use ANDPS and ORPS to simulate FCOPYSIGN. |
| 441 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 442 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 443 | |
| 444 | // We don't support sin/cos/fmod |
| 445 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 446 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 447 | |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 448 | // Special cases we handle for FP constants. |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 449 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
| 450 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 451 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 452 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 453 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
| 454 | |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 455 | if (!UnsafeFPMath) { |
| 456 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 457 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 458 | } |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 459 | } else if (!UseSoftFloat) { |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 460 | // f32 and f64 in x87. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 461 | // Set up the FP register classes. |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 462 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 463 | addRegisterClass(MVT::f32, X86::RFP32RegisterClass); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 464 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 465 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 466 | setOperationAction(ISD::UNDEF, MVT::f32, Expand); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 467 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 468 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 469 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 470 | if (!UnsafeFPMath) { |
| 471 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 472 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 473 | } |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 474 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 475 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 476 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 477 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 478 | addLegalFPImmediate(APFloat(+0.0f)); // FLD0 |
| 479 | addLegalFPImmediate(APFloat(+1.0f)); // FLD1 |
| 480 | addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS |
| 481 | addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 482 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 483 | |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 484 | // Long double always uses X87. |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 485 | if (!UseSoftFloat) { |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 486 | addRegisterClass(MVT::f80, X86::RFP80RegisterClass); |
| 487 | setOperationAction(ISD::UNDEF, MVT::f80, Expand); |
| 488 | setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); |
| 489 | { |
| 490 | bool ignored; |
| 491 | APFloat TmpFlt(+0.0); |
| 492 | TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, |
| 493 | &ignored); |
| 494 | addLegalFPImmediate(TmpFlt); // FLD0 |
| 495 | TmpFlt.changeSign(); |
| 496 | addLegalFPImmediate(TmpFlt); // FLD0/FCHS |
| 497 | APFloat TmpFlt2(+1.0); |
| 498 | TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, |
| 499 | &ignored); |
| 500 | addLegalFPImmediate(TmpFlt2); // FLD1 |
| 501 | TmpFlt2.changeSign(); |
| 502 | addLegalFPImmediate(TmpFlt2); // FLD1/FCHS |
| 503 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 504 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 505 | if (!UnsafeFPMath) { |
| 506 | setOperationAction(ISD::FSIN , MVT::f80 , Expand); |
| 507 | setOperationAction(ISD::FCOS , MVT::f80 , Expand); |
| 508 | } |
Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 509 | } |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 510 | |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 511 | // Always use a library call for pow. |
| 512 | setOperationAction(ISD::FPOW , MVT::f32 , Expand); |
| 513 | setOperationAction(ISD::FPOW , MVT::f64 , Expand); |
| 514 | setOperationAction(ISD::FPOW , MVT::f80 , Expand); |
| 515 | |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 516 | setOperationAction(ISD::FLOG, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 517 | setOperationAction(ISD::FLOG2, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 518 | setOperationAction(ISD::FLOG10, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 519 | setOperationAction(ISD::FEXP, MVT::f80, Expand); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 520 | setOperationAction(ISD::FEXP2, MVT::f80, Expand); |
| 521 | |
Mon P Wang | f007a8b | 2008-11-06 05:31:54 +0000 | [diff] [blame] | 522 | // First set operation action for all vector types to either promote |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 523 | // (for widening) or expand (for scalarization). Then we will selectively |
| 524 | // turn on ones that can be effectively codegen'd. |
Dan Gohman | fa0f77d | 2007-05-18 18:44:07 +0000 | [diff] [blame] | 525 | for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 526 | VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 527 | setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand); |
| 528 | setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand); |
| 529 | setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand); |
| 530 | setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand); |
| 531 | setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand); |
| 532 | setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand); |
| 533 | setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand); |
| 534 | setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand); |
| 535 | setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand); |
| 536 | setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand); |
| 537 | setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand); |
| 538 | setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand); |
| 539 | setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand); |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 540 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand); |
| 541 | setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand); |
Eli Friedman | 108b519 | 2009-05-23 22:44:52 +0000 | [diff] [blame] | 542 | setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand); |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 543 | setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 544 | setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand); |
| 545 | setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand); |
| 546 | setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand); |
| 547 | setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand); |
| 548 | setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand); |
| 549 | setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand); |
| 550 | setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand); |
| 551 | setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand); |
| 552 | setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand); |
| 553 | setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand); |
| 554 | setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand); |
| 555 | setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand); |
| 556 | setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand); |
| 557 | setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand); |
| 558 | setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand); |
| 559 | setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand); |
| 560 | setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand); |
| 561 | setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand); |
| 562 | setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand); |
| 563 | setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand); |
| 564 | setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand); |
| 565 | setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand); |
Dale Johannesen | fb0e132 | 2008-09-10 17:31:40 +0000 | [diff] [blame] | 566 | setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand); |
| 567 | setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand); |
| 568 | setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand); |
| 569 | setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand); |
| 570 | setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand); |
Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 571 | setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand); |
| 572 | setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand); |
| 573 | setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand); |
| 574 | setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 577 | // FIXME: In order to prevent SSE instructions being expanded to MMX ones |
| 578 | // with -msoft-float, disable use of MMX as well. |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 579 | if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 580 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 581 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 582 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 583 | addRegisterClass(MVT::v2f32, X86::VR64RegisterClass); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 584 | addRegisterClass(MVT::v1i64, X86::VR64RegisterClass); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 585 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 586 | setOperationAction(ISD::ADD, MVT::v8i8, Legal); |
| 587 | setOperationAction(ISD::ADD, MVT::v4i16, Legal); |
| 588 | setOperationAction(ISD::ADD, MVT::v2i32, Legal); |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 589 | setOperationAction(ISD::ADD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 590 | |
Bill Wendling | c1fb047 | 2007-03-10 09:57:05 +0000 | [diff] [blame] | 591 | setOperationAction(ISD::SUB, MVT::v8i8, Legal); |
| 592 | setOperationAction(ISD::SUB, MVT::v4i16, Legal); |
| 593 | setOperationAction(ISD::SUB, MVT::v2i32, Legal); |
Dale Johannesen | 8d26e59 | 2007-10-30 01:18:38 +0000 | [diff] [blame] | 594 | setOperationAction(ISD::SUB, MVT::v1i64, Legal); |
Bill Wendling | c1fb047 | 2007-03-10 09:57:05 +0000 | [diff] [blame] | 595 | |
Bill Wendling | 74027e9 | 2007-03-15 21:24:36 +0000 | [diff] [blame] | 596 | setOperationAction(ISD::MULHS, MVT::v4i16, Legal); |
| 597 | setOperationAction(ISD::MUL, MVT::v4i16, Legal); |
| 598 | |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 599 | setOperationAction(ISD::AND, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 600 | AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 601 | setOperationAction(ISD::AND, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 602 | AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64); |
| 603 | setOperationAction(ISD::AND, MVT::v2i32, Promote); |
| 604 | AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64); |
| 605 | setOperationAction(ISD::AND, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 606 | |
| 607 | setOperationAction(ISD::OR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 608 | AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 609 | setOperationAction(ISD::OR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 610 | AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64); |
| 611 | setOperationAction(ISD::OR, MVT::v2i32, Promote); |
| 612 | AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64); |
| 613 | setOperationAction(ISD::OR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 614 | |
| 615 | setOperationAction(ISD::XOR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 616 | AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 617 | setOperationAction(ISD::XOR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 618 | AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64); |
| 619 | setOperationAction(ISD::XOR, MVT::v2i32, Promote); |
| 620 | AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64); |
| 621 | setOperationAction(ISD::XOR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 622 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 623 | setOperationAction(ISD::LOAD, MVT::v8i8, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 624 | AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 625 | setOperationAction(ISD::LOAD, MVT::v4i16, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 626 | AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64); |
| 627 | setOperationAction(ISD::LOAD, MVT::v2i32, Promote); |
| 628 | AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 629 | setOperationAction(ISD::LOAD, MVT::v2f32, Promote); |
| 630 | AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 631 | setOperationAction(ISD::LOAD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 632 | |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 633 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); |
| 634 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); |
| 635 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 636 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom); |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 637 | setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 638 | |
| 639 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); |
| 640 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); |
| 641 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 642 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 643 | |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 644 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom); |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 645 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom); |
| 646 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 647 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom); |
Bill Wendling | 3180e20 | 2008-07-20 02:32:23 +0000 | [diff] [blame] | 648 | |
| 649 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 650 | |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 651 | setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 652 | setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand); |
| 653 | setOperationAction(ISD::SELECT, MVT::v8i8, Promote); |
| 654 | setOperationAction(ISD::SELECT, MVT::v4i16, Promote); |
| 655 | setOperationAction(ISD::SELECT, MVT::v2i32, Promote); |
| 656 | setOperationAction(ISD::SELECT, MVT::v1i64, Custom); |
Eli Friedman | 3dae284 | 2009-07-22 01:06:52 +0000 | [diff] [blame] | 657 | setOperationAction(ISD::VSETCC, MVT::v8i8, Custom); |
| 658 | setOperationAction(ISD::VSETCC, MVT::v4i16, Custom); |
| 659 | setOperationAction(ISD::VSETCC, MVT::v2i32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 662 | if (!UseSoftFloat && Subtarget->hasSSE1()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 663 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 664 | |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 665 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 666 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 667 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 668 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 669 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 670 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 671 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 672 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 673 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 674 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 675 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 676 | setOperationAction(ISD::VSETCC, MVT::v4f32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Evan Cheng | 9272253 | 2009-03-26 23:06:32 +0000 | [diff] [blame] | 679 | if (!UseSoftFloat && Subtarget->hasSSE2()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 680 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 681 | |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 682 | // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM |
| 683 | // registers cannot be used even for integer operations. |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 684 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 685 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 686 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 687 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 688 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 689 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 690 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 691 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 692 | setOperationAction(ISD::ADD, MVT::v2i64, Legal); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 693 | setOperationAction(ISD::MUL, MVT::v2i64, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 694 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 695 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 696 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 697 | setOperationAction(ISD::SUB, MVT::v2i64, Legal); |
Evan Cheng | f998984 | 2006-04-13 05:10:25 +0000 | [diff] [blame] | 698 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 699 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 700 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 701 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 702 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 703 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 704 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 705 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 706 | setOperationAction(ISD::VSETCC, MVT::v2f64, Custom); |
| 707 | setOperationAction(ISD::VSETCC, MVT::v16i8, Custom); |
| 708 | setOperationAction(ISD::VSETCC, MVT::v8i16, Custom); |
| 709 | setOperationAction(ISD::VSETCC, MVT::v4i32, Custom); |
Nate Begeman | c2616e4 | 2008-05-12 20:34:32 +0000 | [diff] [blame] | 710 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 711 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 712 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 713 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 714 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 715 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 716 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 717 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 718 | for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) { |
| 719 | MVT VT = (MVT::SimpleValueType)i; |
Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 720 | // Do not attempt to custom lower non-power-of-2 vectors |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 721 | if (!isPowerOf2_32(VT.getVectorNumElements())) |
Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 722 | continue; |
David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 723 | // Do not attempt to custom lower non-128-bit vectors |
| 724 | if (!VT.is128BitVector()) |
| 725 | continue; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 726 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 727 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 728 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 729 | } |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 730 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 731 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 732 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 733 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 734 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 735 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 736 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 737 | |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 738 | if (Subtarget->is64Bit()) { |
| 739 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom); |
Dale Johannesen | 25f1d08 | 2007-10-31 00:32:36 +0000 | [diff] [blame] | 740 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 741 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 742 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 743 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 744 | for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) { |
| 745 | MVT VT = (MVT::SimpleValueType)i; |
| 746 | |
| 747 | // Do not attempt to promote non-128-bit vectors |
| 748 | if (!VT.is128BitVector()) { |
| 749 | continue; |
| 750 | } |
| 751 | setOperationAction(ISD::AND, VT, Promote); |
| 752 | AddPromotedToType (ISD::AND, VT, MVT::v2i64); |
| 753 | setOperationAction(ISD::OR, VT, Promote); |
| 754 | AddPromotedToType (ISD::OR, VT, MVT::v2i64); |
| 755 | setOperationAction(ISD::XOR, VT, Promote); |
| 756 | AddPromotedToType (ISD::XOR, VT, MVT::v2i64); |
| 757 | setOperationAction(ISD::LOAD, VT, Promote); |
| 758 | AddPromotedToType (ISD::LOAD, VT, MVT::v2i64); |
| 759 | setOperationAction(ISD::SELECT, VT, Promote); |
| 760 | AddPromotedToType (ISD::SELECT, VT, MVT::v2i64); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 761 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 762 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 763 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 764 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 765 | // Custom lower v2i64 and v2f64 selects. |
| 766 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 767 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 768 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 769 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 770 | |
Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 771 | setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); |
| 772 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); |
| 773 | if (!DisableMMX && Subtarget->hasMMX()) { |
| 774 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom); |
| 775 | setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom); |
| 776 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 777 | } |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 778 | |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 779 | if (Subtarget->hasSSE41()) { |
| 780 | // FIXME: Do we need to handle scalar-to-vector here? |
| 781 | setOperationAction(ISD::MUL, MVT::v4i32, Legal); |
| 782 | |
| 783 | // i8 and i16 vectors are custom , because the source register and source |
| 784 | // source memory operand types are not the same width. f32 vectors are |
| 785 | // custom since the immediate controlling the insert encodes additional |
| 786 | // information. |
| 787 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom); |
| 788 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 789 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 790 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| 791 | |
| 792 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom); |
| 793 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 794 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom); |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 795 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 796 | |
| 797 | if (Subtarget->is64Bit()) { |
Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 798 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal); |
| 799 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 802 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 803 | if (Subtarget->hasSSE42()) { |
| 804 | setOperationAction(ISD::VSETCC, MVT::v2i64, Custom); |
| 805 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 806 | |
David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 807 | if (!UseSoftFloat && Subtarget->hasAVX()) { |
David Greene | d94c101 | 2009-06-29 22:50:51 +0000 | [diff] [blame] | 808 | addRegisterClass(MVT::v8f32, X86::VR256RegisterClass); |
| 809 | addRegisterClass(MVT::v4f64, X86::VR256RegisterClass); |
| 810 | addRegisterClass(MVT::v8i32, X86::VR256RegisterClass); |
| 811 | addRegisterClass(MVT::v4i64, X86::VR256RegisterClass); |
| 812 | |
David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 813 | setOperationAction(ISD::LOAD, MVT::v8f32, Legal); |
| 814 | setOperationAction(ISD::LOAD, MVT::v8i32, Legal); |
| 815 | setOperationAction(ISD::LOAD, MVT::v4f64, Legal); |
| 816 | setOperationAction(ISD::LOAD, MVT::v4i64, Legal); |
| 817 | setOperationAction(ISD::FADD, MVT::v8f32, Legal); |
| 818 | setOperationAction(ISD::FSUB, MVT::v8f32, Legal); |
| 819 | setOperationAction(ISD::FMUL, MVT::v8f32, Legal); |
| 820 | setOperationAction(ISD::FDIV, MVT::v8f32, Legal); |
| 821 | setOperationAction(ISD::FSQRT, MVT::v8f32, Legal); |
| 822 | setOperationAction(ISD::FNEG, MVT::v8f32, Custom); |
| 823 | //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom); |
| 824 | //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom); |
| 825 | //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom); |
| 826 | //setOperationAction(ISD::SELECT, MVT::v8f32, Custom); |
| 827 | //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom); |
| 828 | |
| 829 | // Operations to consider commented out -v16i16 v32i8 |
| 830 | //setOperationAction(ISD::ADD, MVT::v16i16, Legal); |
| 831 | setOperationAction(ISD::ADD, MVT::v8i32, Custom); |
| 832 | setOperationAction(ISD::ADD, MVT::v4i64, Custom); |
| 833 | //setOperationAction(ISD::SUB, MVT::v32i8, Legal); |
| 834 | //setOperationAction(ISD::SUB, MVT::v16i16, Legal); |
| 835 | setOperationAction(ISD::SUB, MVT::v8i32, Custom); |
| 836 | setOperationAction(ISD::SUB, MVT::v4i64, Custom); |
| 837 | //setOperationAction(ISD::MUL, MVT::v16i16, Legal); |
| 838 | setOperationAction(ISD::FADD, MVT::v4f64, Legal); |
| 839 | setOperationAction(ISD::FSUB, MVT::v4f64, Legal); |
| 840 | setOperationAction(ISD::FMUL, MVT::v4f64, Legal); |
| 841 | setOperationAction(ISD::FDIV, MVT::v4f64, Legal); |
| 842 | setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); |
| 843 | setOperationAction(ISD::FNEG, MVT::v4f64, Custom); |
| 844 | |
| 845 | setOperationAction(ISD::VSETCC, MVT::v4f64, Custom); |
| 846 | // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom); |
| 847 | // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom); |
| 848 | setOperationAction(ISD::VSETCC, MVT::v8i32, Custom); |
| 849 | |
| 850 | // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom); |
| 851 | // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom); |
| 852 | // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom); |
| 853 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom); |
| 854 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom); |
| 855 | |
| 856 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom); |
| 857 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom); |
| 858 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom); |
| 859 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom); |
| 860 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom); |
| 861 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom); |
| 862 | |
| 863 | #if 0 |
| 864 | // Not sure we want to do this since there are no 256-bit integer |
| 865 | // operations in AVX |
| 866 | |
| 867 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| 868 | // This includes 256-bit vectors |
| 869 | for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) { |
| 870 | MVT VT = (MVT::SimpleValueType)i; |
| 871 | |
| 872 | // Do not attempt to custom lower non-power-of-2 vectors |
| 873 | if (!isPowerOf2_32(VT.getVectorNumElements())) |
| 874 | continue; |
| 875 | |
| 876 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 877 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 878 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); |
| 879 | } |
| 880 | |
| 881 | if (Subtarget->is64Bit()) { |
| 882 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom); |
| 883 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom); |
| 884 | } |
| 885 | #endif |
| 886 | |
| 887 | #if 0 |
| 888 | // Not sure we want to do this since there are no 256-bit integer |
| 889 | // operations in AVX |
| 890 | |
| 891 | // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64. |
| 892 | // Including 256-bit vectors |
| 893 | for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) { |
| 894 | MVT VT = (MVT::SimpleValueType)i; |
| 895 | |
| 896 | if (!VT.is256BitVector()) { |
| 897 | continue; |
| 898 | } |
| 899 | setOperationAction(ISD::AND, VT, Promote); |
| 900 | AddPromotedToType (ISD::AND, VT, MVT::v4i64); |
| 901 | setOperationAction(ISD::OR, VT, Promote); |
| 902 | AddPromotedToType (ISD::OR, VT, MVT::v4i64); |
| 903 | setOperationAction(ISD::XOR, VT, Promote); |
| 904 | AddPromotedToType (ISD::XOR, VT, MVT::v4i64); |
| 905 | setOperationAction(ISD::LOAD, VT, Promote); |
| 906 | AddPromotedToType (ISD::LOAD, VT, MVT::v4i64); |
| 907 | setOperationAction(ISD::SELECT, VT, Promote); |
| 908 | AddPromotedToType (ISD::SELECT, VT, MVT::v4i64); |
| 909 | } |
| 910 | |
| 911 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 912 | #endif |
| 913 | } |
| 914 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 915 | // We want to custom lower some of our intrinsics. |
| 916 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 917 | |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 918 | // Add/Sub/Mul with overflow operations are custom lowered. |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 919 | setOperationAction(ISD::SADDO, MVT::i32, Custom); |
| 920 | setOperationAction(ISD::SADDO, MVT::i64, Custom); |
| 921 | setOperationAction(ISD::UADDO, MVT::i32, Custom); |
| 922 | setOperationAction(ISD::UADDO, MVT::i64, Custom); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 923 | setOperationAction(ISD::SSUBO, MVT::i32, Custom); |
| 924 | setOperationAction(ISD::SSUBO, MVT::i64, Custom); |
| 925 | setOperationAction(ISD::USUBO, MVT::i32, Custom); |
| 926 | setOperationAction(ISD::USUBO, MVT::i64, Custom); |
| 927 | setOperationAction(ISD::SMULO, MVT::i32, Custom); |
| 928 | setOperationAction(ISD::SMULO, MVT::i64, Custom); |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 929 | |
Evan Cheng | d54f2d5 | 2009-03-31 19:38:51 +0000 | [diff] [blame] | 930 | if (!Subtarget->is64Bit()) { |
| 931 | // These libcalls are not available in 32-bit. |
| 932 | setLibcallName(RTLIB::SHL_I128, 0); |
| 933 | setLibcallName(RTLIB::SRL_I128, 0); |
| 934 | setLibcallName(RTLIB::SRA_I128, 0); |
| 935 | } |
| 936 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 937 | // We have target-specific dag combine patterns for the following nodes: |
| 938 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 939 | setTargetDAGCombine(ISD::BUILD_VECTOR); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 940 | setTargetDAGCombine(ISD::SELECT); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 941 | setTargetDAGCombine(ISD::SHL); |
| 942 | setTargetDAGCombine(ISD::SRA); |
| 943 | setTargetDAGCombine(ISD::SRL); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 944 | setTargetDAGCombine(ISD::STORE); |
Owen Anderson | 9917700 | 2009-06-29 18:04:45 +0000 | [diff] [blame] | 945 | setTargetDAGCombine(ISD::MEMBARRIER); |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 946 | if (Subtarget->is64Bit()) |
| 947 | setTargetDAGCombine(ISD::MUL); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 948 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 949 | computeRegisterProperties(); |
| 950 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 951 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 952 | // be smaller when we are in optimizing for size mode. |
Dan Gohman | 87060f5 | 2008-06-30 21:00:56 +0000 | [diff] [blame] | 953 | maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores |
| 954 | maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores |
| 955 | maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 956 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 957 | setPrefLoopAlignment(16); |
Evan Cheng | 6ebf7bc | 2009-05-13 21:42:09 +0000 | [diff] [blame] | 958 | benefitFromCodePlacementOpt = true; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 961 | |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 962 | MVT X86TargetLowering::getSetCCResultType(MVT VT) const { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 963 | return MVT::i8; |
| 964 | } |
| 965 | |
| 966 | |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 967 | /// getMaxByValAlign - Helper for getByValTypeAlignment to determine |
| 968 | /// the desired ByVal argument alignment. |
| 969 | static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) { |
| 970 | if (MaxAlign == 16) |
| 971 | return; |
| 972 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 973 | if (VTy->getBitWidth() == 128) |
| 974 | MaxAlign = 16; |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 975 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 976 | unsigned EltAlign = 0; |
| 977 | getMaxByValAlign(ATy->getElementType(), EltAlign); |
| 978 | if (EltAlign > MaxAlign) |
| 979 | MaxAlign = EltAlign; |
| 980 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 981 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 982 | unsigned EltAlign = 0; |
| 983 | getMaxByValAlign(STy->getElementType(i), EltAlign); |
| 984 | if (EltAlign > MaxAlign) |
| 985 | MaxAlign = EltAlign; |
| 986 | if (MaxAlign == 16) |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 994 | /// function arguments in the caller parameter area. For X86, aggregates |
Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 995 | /// that contain SSE vectors are placed at 16-byte boundaries while the rest |
| 996 | /// are at 4-byte boundaries. |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 997 | unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const { |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 998 | if (Subtarget->is64Bit()) { |
| 999 | // Max of 8 and alignment of type. |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 1000 | unsigned TyAlign = TD->getABITypeAlignment(Ty); |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 1001 | if (TyAlign > 8) |
| 1002 | return TyAlign; |
| 1003 | return 8; |
| 1004 | } |
| 1005 | |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1006 | unsigned Align = 4; |
Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 1007 | if (Subtarget->hasSSE1()) |
| 1008 | getMaxByValAlign(Ty, Align); |
Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1009 | return Align; |
| 1010 | } |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1011 | |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1012 | /// getOptimalMemOpType - Returns the target specific optimal type for load |
Evan Cheng | 0ef8de3 | 2008-05-15 22:13:02 +0000 | [diff] [blame] | 1013 | /// and store operations as a result of memset, memcpy, and memmove |
| 1014 | /// lowering. It returns MVT::iAny if SelectionDAG should be responsible for |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1015 | /// determining it. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1016 | MVT |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1017 | X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align, |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 1018 | bool isSrcConst, bool isSrcStr, |
| 1019 | SelectionDAG &DAG) const { |
Chris Lattner | 4002a1b | 2008-10-28 05:49:35 +0000 | [diff] [blame] | 1020 | // FIXME: This turns off use of xmm stores for memset/memcpy on targets like |
| 1021 | // linux. This is because the stack realignment code can't handle certain |
| 1022 | // cases like PR2962. This should be removed when PR2962 is fixed. |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 1023 | const Function *F = DAG.getMachineFunction().getFunction(); |
| 1024 | bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat); |
| 1025 | if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) { |
Chris Lattner | 4002a1b | 2008-10-28 05:49:35 +0000 | [diff] [blame] | 1026 | if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16) |
| 1027 | return MVT::v4i32; |
| 1028 | if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16) |
| 1029 | return MVT::v4f32; |
| 1030 | } |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1031 | if (Subtarget->is64Bit() && Size >= 8) |
| 1032 | return MVT::i64; |
| 1033 | return MVT::i32; |
| 1034 | } |
| 1035 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1036 | /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC |
| 1037 | /// jumptable. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1038 | SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table, |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1039 | SelectionDAG &DAG) const { |
| 1040 | if (usesGlobalOffsetTable()) |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1041 | return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy()); |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 1042 | if (!Subtarget->is64Bit()) |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1043 | // This doesn't have DebugLoc associated with it, but is not really the |
| 1044 | // same as a Register. |
| 1045 | return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(), |
| 1046 | getPointerTy()); |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1047 | return Table; |
| 1048 | } |
| 1049 | |
Bill Wendling | b4202b8 | 2009-07-01 18:50:55 +0000 | [diff] [blame] | 1050 | /// getFunctionAlignment - Return the Log2 alignment of this function. |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 1051 | unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const { |
| 1052 | return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4; |
| 1053 | } |
| 1054 | |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1055 | //===----------------------------------------------------------------------===// |
| 1056 | // Return Value Calling Convention Implementation |
| 1057 | //===----------------------------------------------------------------------===// |
| 1058 | |
Chris Lattner | 59ed56b | 2007-02-28 04:55:35 +0000 | [diff] [blame] | 1059 | #include "X86GenCallingConv.inc" |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1060 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1061 | SDValue |
| 1062 | X86TargetLowering::LowerReturn(SDValue Chain, |
| 1063 | unsigned CallConv, bool isVarArg, |
| 1064 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1065 | DebugLoc dl, SelectionDAG &DAG) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1067 | SmallVector<CCValAssign, 16> RVLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1068 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 1069 | RVLocs, *DAG.getContext()); |
| 1070 | CCInfo.AnalyzeReturn(Outs, RetCC_X86); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1072 | // If this is the first return lowered for this function, add the regs to the |
| 1073 | // liveout set for the function. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1074 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1075 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 1076 | if (RVLocs[i].isRegLoc()) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1077 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1078 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1079 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1080 | SDValue Flag; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1082 | SmallVector<SDValue, 6> RetOps; |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1083 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) |
| 1084 | // Operand #1 = Bytes To Pop |
| 1085 | RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1087 | // Copy the result values into the output registers. |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1088 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 1089 | CCValAssign &VA = RVLocs[i]; |
| 1090 | assert(VA.isRegLoc() && "Can only return in registers!"); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1091 | SDValue ValToCopy = Outs[i].Val; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1093 | // Returns in ST0/ST1 are handled specially: these are pushed as operands to |
| 1094 | // the RET instruction and handled by the FP Stackifier. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1095 | if (VA.getLocReg() == X86::ST0 || |
| 1096 | VA.getLocReg() == X86::ST1) { |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1097 | // If this is a copy from an xmm register to ST(0), use an FPExtend to |
| 1098 | // change the value to the FP stack register class. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1099 | if (isScalarFPTypeInSSEReg(VA.getValVT())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1100 | ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy); |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1101 | RetOps.push_back(ValToCopy); |
| 1102 | // Don't emit a copytoreg. |
| 1103 | continue; |
| 1104 | } |
Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 1105 | |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1106 | // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64 |
| 1107 | // which is returned in RAX / RDX. |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1108 | if (Subtarget->is64Bit()) { |
| 1109 | MVT ValVT = ValToCopy.getValueType(); |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1110 | if (ValVT.isVector() && ValVT.getSizeInBits() == 64) { |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1111 | ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy); |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1112 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) |
| 1113 | ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy); |
| 1114 | } |
Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1117 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1118 | Flag = Chain.getValue(1); |
| 1119 | } |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1120 | |
| 1121 | // The x86-64 ABI for returning structs by value requires that we copy |
| 1122 | // the sret argument into %rax for the return. We saved the argument into |
| 1123 | // a virtual register in the entry block, so now we copy the value out |
| 1124 | // and into %rax. |
| 1125 | if (Subtarget->is64Bit() && |
| 1126 | DAG.getMachineFunction().getFunction()->hasStructRetAttr()) { |
| 1127 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1128 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1129 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| 1130 | if (!Reg) { |
| 1131 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); |
| 1132 | FuncInfo->setSRetReturnReg(Reg); |
| 1133 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1134 | SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy()); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1135 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1136 | Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1137 | Flag = Chain.getValue(1); |
| 1138 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1140 | RetOps[0] = Chain; // Update chain. |
| 1141 | |
| 1142 | // Add the flag if we have it. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1143 | if (Flag.getNode()) |
Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1144 | RetOps.push_back(Flag); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1145 | |
| 1146 | return DAG.getNode(X86ISD::RET_FLAG, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1147 | MVT::Other, &RetOps[0], RetOps.size()); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1150 | /// LowerCallResult - Lower the result values of a call into the |
| 1151 | /// appropriate copies out of appropriate physical registers. |
| 1152 | /// |
| 1153 | SDValue |
| 1154 | X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
| 1155 | unsigned CallConv, bool isVarArg, |
| 1156 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 1157 | DebugLoc dl, SelectionDAG &DAG, |
| 1158 | SmallVectorImpl<SDValue> &InVals) { |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 1160 | // Assign locations to each value returned by this call. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1161 | SmallVector<CCValAssign, 16> RVLocs; |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1162 | bool Is64Bit = Subtarget->is64Bit(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1163 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1164 | RVLocs, *DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1165 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1167 | // Copy all of the result registers out of their specified physreg. |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1168 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1169 | CCValAssign &VA = RVLocs[i]; |
| 1170 | MVT CopyVT = VA.getValVT(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1171 | |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1172 | // 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] | 1173 | if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) && |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1174 | ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) { |
Torok Edwin | 804e0fe | 2009-07-08 19:04:27 +0000 | [diff] [blame] | 1175 | llvm_report_error("SSE register return with SSE disabled"); |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1178 | // If this is a call to a function that returns an fp value on the floating |
| 1179 | // point stack, but where we prefer to use the value in xmm registers, copy |
| 1180 | // 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] | 1181 | if ((VA.getLocReg() == X86::ST0 || |
| 1182 | VA.getLocReg() == X86::ST1) && |
| 1183 | isScalarFPTypeInSSEReg(VA.getValVT())) { |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1184 | CopyVT = MVT::f80; |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1185 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1186 | |
Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1187 | SDValue Val; |
| 1188 | if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) { |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1189 | // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64. |
| 1190 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) { |
| 1191 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1192 | MVT::v2i64, InFlag).getValue(1); |
| 1193 | Val = Chain.getValue(0); |
| 1194 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, |
Anton Korobeynikov | cf6b739 | 2009-08-03 08:12:53 +0000 | [diff] [blame] | 1195 | Val, DAG.getConstant(0, MVT::i64)); |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1196 | } else { |
| 1197 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1198 | MVT::i64, InFlag).getValue(1); |
| 1199 | Val = Chain.getValue(0); |
| 1200 | } |
Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1201 | Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val); |
| 1202 | } else { |
| 1203 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1204 | CopyVT, InFlag).getValue(1); |
| 1205 | Val = Chain.getValue(0); |
| 1206 | } |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1207 | InFlag = Chain.getValue(2); |
Chris Lattner | 112dedc | 2007-12-29 06:41:28 +0000 | [diff] [blame] | 1208 | |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1209 | if (CopyVT != VA.getValVT()) { |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1210 | // Round the F80 the right size, which also moves to the appropriate xmm |
| 1211 | // register. |
Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1212 | Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val, |
Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1213 | // This truncation won't change the value. |
| 1214 | DAG.getIntPtrConstant(1)); |
| 1215 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1216 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1217 | InVals.push_back(Val); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1218 | } |
Duncan Sands | 4bdcb61 | 2008-07-02 17:40:58 +0000 | [diff] [blame] | 1219 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1220 | return Chain; |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1224 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1225 | // C & StdCall & Fast Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1226 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1227 | // StdCall calling convention seems to be standard for many Windows' API |
| 1228 | // routines and around. It differs from C calling convention just a little: |
| 1229 | // callee should clean up the stack, not caller. Symbols should be also |
| 1230 | // decorated in some fancy way :) It doesn't support any vector arguments. |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1231 | // For info on fast calling convention see Fast Calling Convention (tail call) |
| 1232 | // implementation LowerX86_32FastCCCallTo. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1233 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1234 | /// CallIsStructReturn - Determines whether a call uses struct return |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1235 | /// semantics. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1236 | static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) { |
| 1237 | if (Outs.empty()) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1238 | return false; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1239 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1240 | return Outs[0].Flags.isSRet(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1243 | /// ArgsAreStructReturn - Determines whether a function uses struct |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1244 | /// return semantics. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1245 | static bool |
| 1246 | ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) { |
| 1247 | if (Ins.empty()) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1248 | return false; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1249 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1250 | return Ins[0].Flags.isSRet(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1253 | /// IsCalleePop - Determines whether the callee is required to pop its |
| 1254 | /// own arguments. Callee pop is necessary to support tail calls. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1255 | bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1256 | if (IsVarArg) |
| 1257 | return false; |
| 1258 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1259 | switch (CallingConv) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1260 | default: |
| 1261 | return false; |
| 1262 | case CallingConv::X86_StdCall: |
| 1263 | return !Subtarget->is64Bit(); |
| 1264 | case CallingConv::X86_FastCall: |
| 1265 | return !Subtarget->is64Bit(); |
| 1266 | case CallingConv::Fast: |
| 1267 | return PerformTailCallOpt; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1271 | /// CCAssignFnForNode - Selects the correct CCAssignFn for a the |
| 1272 | /// given CallingConvention value. |
| 1273 | CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const { |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 1274 | if (Subtarget->is64Bit()) { |
Anton Korobeynikov | 1a979d9 | 2008-03-22 20:57:27 +0000 | [diff] [blame] | 1275 | if (Subtarget->isTargetWin64()) |
Anton Korobeynikov | 8f88cb0 | 2008-03-22 20:37:30 +0000 | [diff] [blame] | 1276 | return CC_X86_Win64_C; |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 1277 | else |
| 1278 | return CC_X86_64_C; |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1281 | if (CC == CallingConv::X86_FastCall) |
| 1282 | return CC_X86_32_FastCall; |
Evan Cheng | b188dd9 | 2008-09-10 18:25:29 +0000 | [diff] [blame] | 1283 | else if (CC == CallingConv::Fast) |
| 1284 | return CC_X86_32_FastCC; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1285 | else |
| 1286 | return CC_X86_32_C; |
| 1287 | } |
| 1288 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1289 | /// NameDecorationForCallConv - Selects the appropriate decoration to |
| 1290 | /// apply to a MachineFunction containing a given calling convention. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1291 | NameDecorationStyle |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1292 | X86TargetLowering::NameDecorationForCallConv(unsigned CallConv) { |
| 1293 | if (CallConv == CallingConv::X86_FastCall) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1294 | return FastCall; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1295 | else if (CallConv == CallingConv::X86_StdCall) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1296 | return StdCall; |
| 1297 | return None; |
| 1298 | } |
| 1299 | |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1300 | |
Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1301 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
| 1302 | /// by "Src" to address "Dst" with size and alignment information specified by |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1303 | /// the specific parameter attribute. The copy will be passed as a byval |
| 1304 | /// function parameter. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1305 | static SDValue |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1306 | CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1307 | ISD::ArgFlagsTy Flags, SelectionDAG &DAG, |
| 1308 | DebugLoc dl) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1309 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1310 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1311 | /*AlwaysInline=*/true, NULL, 0, NULL, 0); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1314 | SDValue |
| 1315 | X86TargetLowering::LowerMemArgument(SDValue Chain, |
| 1316 | unsigned CallConv, |
| 1317 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 1318 | DebugLoc dl, SelectionDAG &DAG, |
| 1319 | const CCValAssign &VA, |
| 1320 | MachineFrameInfo *MFI, |
| 1321 | unsigned i) { |
| 1322 | |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1323 | // Create the nodes corresponding to a load from this parameter slot. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1324 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
| 1325 | bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1326 | bool isImmutable = !AlwaysUseMutable && !Flags.isByVal(); |
Evan Cheng | e70bb59 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1327 | |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1328 | // 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] | 1329 | // changed with more analysis. |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1330 | // In case of tail call optimization mark all arguments mutable. Since they |
| 1331 | // 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] | 1332 | int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1333 | VA.getLocMemOffset(), isImmutable); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1334 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1335 | if (Flags.isByVal()) |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1336 | return FIN; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1337 | return DAG.getLoad(VA.getValVT(), dl, Chain, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1338 | PseudoSourceValue::getFixedStack(FI), 0); |
Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1341 | SDValue |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1342 | X86TargetLowering::LowerFormalArguments(SDValue Chain, |
| 1343 | unsigned CallConv, |
| 1344 | bool isVarArg, |
| 1345 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 1346 | DebugLoc dl, |
| 1347 | SelectionDAG &DAG, |
| 1348 | SmallVectorImpl<SDValue> &InVals) { |
| 1349 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1350 | MachineFunction &MF = DAG.getMachineFunction(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1351 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1352 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1353 | const Function* Fn = MF.getFunction(); |
| 1354 | if (Fn->hasExternalLinkage() && |
| 1355 | Subtarget->isTargetCygMing() && |
| 1356 | Fn->getName() == "main") |
| 1357 | FuncInfo->setForceFramePointer(true); |
| 1358 | |
| 1359 | // Decorate the function name. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1360 | FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1361 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1362 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1363 | bool Is64Bit = Subtarget->is64Bit(); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1364 | bool IsWin64 = Subtarget->isTargetWin64(); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1365 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1366 | assert(!(isVarArg && CallConv == CallingConv::Fast) && |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1367 | "Var args not supported with calling convention fastcc"); |
| 1368 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1369 | // Assign locations to all of the incoming arguments. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1370 | SmallVector<CCValAssign, 16> ArgLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1371 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 1372 | ArgLocs, *DAG.getContext()); |
| 1373 | CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1375 | unsigned LastVal = ~0U; |
Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1376 | SDValue ArgValue; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1377 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1378 | CCValAssign &VA = ArgLocs[i]; |
| 1379 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 1380 | // places. |
| 1381 | assert(VA.getValNo() != LastVal && |
| 1382 | "Don't support value assigned to multiple locs yet"); |
| 1383 | LastVal = VA.getValNo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1385 | if (VA.isRegLoc()) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1386 | MVT RegVT = VA.getLocVT(); |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 1387 | TargetRegisterClass *RC = NULL; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1388 | if (RegVT == MVT::i32) |
| 1389 | RC = X86::GR32RegisterClass; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1390 | else if (Is64Bit && RegVT == MVT::i64) |
| 1391 | RC = X86::GR64RegisterClass; |
Dale Johannesen | e672af1 | 2008-02-05 20:46:33 +0000 | [diff] [blame] | 1392 | else if (RegVT == MVT::f32) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1393 | RC = X86::FR32RegisterClass; |
Dale Johannesen | e672af1 | 2008-02-05 20:46:33 +0000 | [diff] [blame] | 1394 | else if (RegVT == MVT::f64) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1395 | RC = X86::FR64RegisterClass; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1396 | else if (RegVT.isVector() && RegVT.getSizeInBits() == 128) |
Evan Cheng | ee472b1 | 2008-04-25 07:56:45 +0000 | [diff] [blame] | 1397 | RC = X86::VR128RegisterClass; |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1398 | else if (RegVT.isVector() && RegVT.getSizeInBits() == 64) |
| 1399 | RC = X86::VR64RegisterClass; |
| 1400 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1401 | llvm_unreachable("Unknown argument type!"); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1402 | |
Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1403 | unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1404 | ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1405 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1406 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 1407 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 1408 | // right size. |
| 1409 | if (VA.getLocInfo() == CCValAssign::SExt) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1410 | ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1411 | DAG.getValueType(VA.getValVT())); |
| 1412 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1413 | ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1414 | DAG.getValueType(VA.getValVT())); |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1415 | else if (VA.getLocInfo() == CCValAssign::BCvt) |
Anton Korobeynikov | 6dde14b | 2009-08-03 08:14:14 +0000 | [diff] [blame] | 1416 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1417 | |
Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1418 | if (VA.isExtInLoc()) { |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1419 | // Handle MMX values passed in XMM regs. |
| 1420 | if (RegVT.isVector()) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1421 | ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, |
| 1422 | ArgValue, DAG.getConstant(0, MVT::i64)); |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1423 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue); |
| 1424 | } else |
| 1425 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
Evan Cheng | 44c0fd1 | 2008-04-25 20:13:28 +0000 | [diff] [blame] | 1426 | } |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1427 | } else { |
| 1428 | assert(VA.isMemLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1429 | ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1430 | } |
Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1431 | |
| 1432 | // If value is passed via pointer - do a load. |
| 1433 | if (VA.getLocInfo() == CCValAssign::Indirect) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1434 | ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0); |
Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1435 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1436 | InVals.push_back(ArgValue); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1437 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1438 | |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1439 | // The x86-64 ABI for returning structs by value requires that we copy |
| 1440 | // the sret argument into %rax for the return. Save the argument into |
| 1441 | // a virtual register so that we can access it from the return points. |
Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1442 | if (Is64Bit && MF.getFunction()->hasStructRetAttr()) { |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1443 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1444 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| 1445 | if (!Reg) { |
| 1446 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64)); |
| 1447 | FuncInfo->setSRetReturnReg(Reg); |
| 1448 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1449 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]); |
| 1450 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain); |
Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1453 | unsigned StackSize = CCInfo.getNextStackOffset(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1454 | // align stack specially for tail calls |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1455 | if (PerformTailCallOpt && CallConv == CallingConv::Fast) |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1456 | StackSize = GetAlignedArgumentStackSize(StackSize, DAG); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1457 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1458 | // If the function takes variable number of arguments, make a frame index for |
| 1459 | // 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] | 1460 | if (isVarArg) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1461 | if (Is64Bit || CallConv != CallingConv::X86_FastCall) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1462 | VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); |
| 1463 | } |
| 1464 | if (Is64Bit) { |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1465 | unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0; |
| 1466 | |
| 1467 | // FIXME: We should really autogenerate these arrays |
| 1468 | static const unsigned GPR64ArgRegsWin64[] = { |
| 1469 | X86::RCX, X86::RDX, X86::R8, X86::R9 |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1470 | }; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1471 | static const unsigned XMMArgRegsWin64[] = { |
| 1472 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
| 1473 | }; |
| 1474 | static const unsigned GPR64ArgRegs64Bit[] = { |
| 1475 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1476 | }; |
| 1477 | static const unsigned XMMArgRegs64Bit[] = { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1478 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1479 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1480 | }; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1481 | const unsigned *GPR64ArgRegs, *XMMArgRegs; |
| 1482 | |
| 1483 | if (IsWin64) { |
| 1484 | TotalNumIntRegs = 4; TotalNumXMMRegs = 4; |
| 1485 | GPR64ArgRegs = GPR64ArgRegsWin64; |
| 1486 | XMMArgRegs = XMMArgRegsWin64; |
| 1487 | } else { |
| 1488 | TotalNumIntRegs = 6; TotalNumXMMRegs = 8; |
| 1489 | GPR64ArgRegs = GPR64ArgRegs64Bit; |
| 1490 | XMMArgRegs = XMMArgRegs64Bit; |
| 1491 | } |
| 1492 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, |
| 1493 | TotalNumIntRegs); |
| 1494 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, |
| 1495 | TotalNumXMMRegs); |
| 1496 | |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 1497 | bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat); |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 1498 | assert(!(NumXMMRegs && !Subtarget->hasSSE1()) && |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1499 | "SSE register cannot be used when SSE is disabled!"); |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 1500 | assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) && |
Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 1501 | "SSE register cannot be used when SSE is disabled!"); |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 1502 | if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1()) |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1503 | // Kernel mode asks for SSE to be disabled, so don't push them |
| 1504 | // on the stack. |
| 1505 | TotalNumXMMRegs = 0; |
Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 1506 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1507 | // For X86-64, if there are vararg parameters that are passed via |
| 1508 | // registers, then we must store them to their spots on the stack so they |
| 1509 | // may be loaded by deferencing the result of va_next. |
| 1510 | VarArgsGPOffset = NumIntRegs * 8; |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1511 | VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16; |
| 1512 | RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 + |
| 1513 | TotalNumXMMRegs * 16, 16); |
| 1514 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1515 | // Store the integer parameter registers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1516 | SmallVector<SDValue, 8> MemOps; |
| 1517 | SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1518 | SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1519 | DAG.getIntPtrConstant(VarArgsGPOffset)); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1520 | for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) { |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 1521 | unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs], |
| 1522 | X86::GR64RegisterClass); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1523 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1524 | SDValue Store = |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1525 | DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1526 | PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1527 | MemOps.push_back(Store); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1528 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1529 | DAG.getIntPtrConstant(8)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1530 | } |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1531 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1532 | // Now store the XMM (fp + vector) parameter registers. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1533 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1534 | DAG.getIntPtrConstant(VarArgsFPOffset)); |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1535 | for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) { |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 1536 | unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs], |
| 1537 | X86::VR128RegisterClass); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1538 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1539 | SDValue Store = |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1540 | DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1541 | PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1542 | MemOps.push_back(Store); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1543 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1544 | DAG.getIntPtrConstant(16)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1545 | } |
| 1546 | if (!MemOps.empty()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1547 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1548 | &MemOps[0], MemOps.size()); |
| 1549 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1550 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1551 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1552 | // Some CCs need callee pop. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1553 | if (IsCalleePop(isVarArg, CallConv)) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1554 | BytesToPopOnReturn = StackSize; // Callee pops everything. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1555 | BytesCallerReserves = 0; |
| 1556 | } else { |
Anton Korobeynikov | 1d9bacc | 2007-03-06 08:12:33 +0000 | [diff] [blame] | 1557 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1558 | // If this is an sret function, the return should pop the hidden pointer. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1559 | if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1560 | BytesToPopOnReturn = 4; |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1561 | BytesCallerReserves = StackSize; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1562 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1563 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1564 | if (!Is64Bit) { |
| 1565 | RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1566 | if (CallConv == CallingConv::X86_FastCall) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1567 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 1568 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1569 | |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1570 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1571 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1572 | return Chain; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1575 | SDValue |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1576 | X86TargetLowering::LowerMemOpCallTo(SDValue Chain, |
| 1577 | SDValue StackPtr, SDValue Arg, |
| 1578 | DebugLoc dl, SelectionDAG &DAG, |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1579 | const CCValAssign &VA, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1580 | ISD::ArgFlagsTy Flags) { |
Anton Korobeynikov | cf6b739 | 2009-08-03 08:12:53 +0000 | [diff] [blame] | 1581 | const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0); |
Anton Korobeynikov | cf6b739 | 2009-08-03 08:12:53 +0000 | [diff] [blame] | 1582 | unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1583 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1584 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1585 | if (Flags.isByVal()) { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1586 | return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl); |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1587 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1588 | return DAG.getStore(Chain, dl, Arg, PtrOff, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1589 | PseudoSourceValue::getStack(), LocMemOffset); |
Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 1592 | /// EmitTailCallLoadRetAddr - Emit a load of return address if tail call |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1593 | /// optimization is performed and it is required. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1594 | SDValue |
| 1595 | X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1596 | SDValue &OutRetAddr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1597 | SDValue Chain, |
| 1598 | bool IsTailCall, |
| 1599 | bool Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1600 | int FPDiff, |
| 1601 | DebugLoc dl) { |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1602 | if (!IsTailCall || FPDiff==0) return Chain; |
| 1603 | |
| 1604 | // Adjust the Return address stack slot. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1605 | MVT VT = getPointerTy(); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1606 | OutRetAddr = getReturnAddressFrameIndex(DAG); |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 1607 | |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1608 | // Load the "old" Return address. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1609 | OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1610 | return SDValue(OutRetAddr.getNode(), 1); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call |
| 1614 | /// optimization is performed and it is required (FPDiff!=0). |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1615 | static SDValue |
| 1616 | EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1617 | SDValue Chain, SDValue RetAddrFrIdx, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1618 | bool Is64Bit, int FPDiff, DebugLoc dl) { |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1619 | // Store the return address to the appropriate stack slot. |
| 1620 | if (!FPDiff) return Chain; |
| 1621 | // Calculate the new stack slot for the return address. |
| 1622 | int SlotSize = Is64Bit ? 8 : 4; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1623 | int NewReturnAddrFI = |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1624 | MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1625 | MVT VT = Is64Bit ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1626 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1627 | Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1628 | PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1629 | return Chain; |
| 1630 | } |
| 1631 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1632 | SDValue |
| 1633 | X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee, |
| 1634 | unsigned CallConv, bool isVarArg, bool isTailCall, |
| 1635 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1636 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 1637 | DebugLoc dl, SelectionDAG &DAG, |
| 1638 | SmallVectorImpl<SDValue> &InVals) { |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1639 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1640 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1641 | bool Is64Bit = Subtarget->is64Bit(); |
| 1642 | bool IsStructRet = CallIsStructReturn(Outs); |
| 1643 | |
| 1644 | assert((!isTailCall || |
| 1645 | (CallConv == CallingConv::Fast && PerformTailCallOpt)) && |
| 1646 | "IsEligibleForTailCallOptimization missed a case!"); |
| 1647 | assert(!(isVarArg && CallConv == CallingConv::Fast) && |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1648 | "Var args not supported with calling convention fastcc"); |
| 1649 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1650 | // Analyze operands of the call, assigning locations to each operand. |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1651 | SmallVector<CCValAssign, 16> ArgLocs; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1652 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), |
| 1653 | ArgLocs, *DAG.getContext()); |
| 1654 | CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1656 | // Get a count of how many bytes are to be pushed on the stack. |
| 1657 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1658 | if (PerformTailCallOpt && CallConv == CallingConv::Fast) |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1659 | NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1660 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1661 | int FPDiff = 0; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1662 | if (isTailCall) { |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1663 | // Lower arguments at fp - stackoffset + fpdiff. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1664 | unsigned NumBytesCallerPushed = |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1665 | MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn(); |
| 1666 | FPDiff = NumBytesCallerPushed - NumBytes; |
| 1667 | |
| 1668 | // Set the delta of movement of the returnaddr stackslot. |
| 1669 | // But only set if delta is greater than previous delta. |
| 1670 | if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta())) |
| 1671 | MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff); |
| 1672 | } |
| 1673 | |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 1674 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1675 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1676 | SDValue RetAddrFrIdx; |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1677 | // Load return adress for tail calls. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1678 | Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1679 | FPDiff, dl); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1680 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1681 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 1682 | SmallVector<SDValue, 8> MemOpChains; |
| 1683 | SDValue StackPtr; |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1684 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1685 | // Walk the register/memloc assignments, inserting copies/loads. In the case |
| 1686 | // of tail call optimization arguments are handle later. |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1687 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1688 | CCValAssign &VA = ArgLocs[i]; |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1689 | MVT RegVT = VA.getLocVT(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1690 | SDValue Arg = Outs[i].Val; |
| 1691 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1692 | bool isByVal = Flags.isByVal(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1694 | // Promote the value if needed. |
| 1695 | switch (VA.getLocInfo()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1696 | default: llvm_unreachable("Unknown loc info!"); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1697 | case CCValAssign::Full: break; |
| 1698 | case CCValAssign::SExt: |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1699 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | case CCValAssign::ZExt: |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1702 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1703 | break; |
| 1704 | case CCValAssign::AExt: |
Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 1705 | if (RegVT.isVector() && RegVT.getSizeInBits() == 128) { |
| 1706 | // Special case: passing MMX values in XMM registers. |
| 1707 | Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg); |
| 1708 | Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg); |
| 1709 | Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg); |
| 1710 | } else |
| 1711 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); |
| 1712 | break; |
| 1713 | case CCValAssign::BCvt: |
| 1714 | Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg); |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1715 | break; |
Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1716 | case CCValAssign::Indirect: { |
| 1717 | // Store the argument. |
| 1718 | SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); |
| 1719 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| 1720 | Chain = DAG.getStore(Chain, dl, Arg, SpillSlot, |
| 1721 | PseudoSourceValue::getFixedStack(FI), 0); |
| 1722 | Arg = SpillSlot; |
| 1723 | break; |
| 1724 | } |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1725 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 1727 | if (VA.isRegLoc()) { |
| 1728 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 1729 | } else { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1730 | if (!isTailCall || (isTailCall && isByVal)) { |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1731 | assert(VA.isMemLoc()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1732 | if (StackPtr.getNode() == 0) |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1733 | StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1734 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1735 | MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, |
| 1736 | dl, DAG, VA, Flags)); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1737 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1738 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1739 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1740 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1741 | if (!MemOpChains.empty()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1742 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1743 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1744 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1745 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1746 | // and flag operands which copy the outgoing args into registers. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1747 | SDValue InFlag; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1748 | // Tail call byval lowering might overwrite argument registers so in case of |
| 1749 | // tail call optimization the copies to registers are lowered later. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1750 | if (!isTailCall) |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1751 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1752 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1753 | RegsToPass[i].second, InFlag); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1754 | InFlag = Chain.getValue(1); |
| 1755 | } |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | 951bf7d | 2009-07-09 02:44:11 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 1758 | if (Subtarget->isPICStyleGOT()) { |
Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 1759 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 1760 | // GOT pointer. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1761 | if (!isTailCall) { |
Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 1762 | Chain = DAG.getCopyToReg(Chain, dl, X86::EBX, |
| 1763 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 1764 | DebugLoc::getUnknownLoc(), |
| 1765 | getPointerTy()), |
| 1766 | InFlag); |
| 1767 | InFlag = Chain.getValue(1); |
| 1768 | } else { |
| 1769 | // If we are tail calling and generating PIC/GOT style code load the |
| 1770 | // address of the callee into ECX. The value in ecx is used as target of |
| 1771 | // the tail jump. This is done to circumvent the ebx/callee-saved problem |
| 1772 | // for tail calls on PIC/GOT architectures. Normally we would just put the |
| 1773 | // address of GOT into ebx and then call target@PLT. But for tail calls |
| 1774 | // ebx would be restored (since ebx is callee saved) before jumping to the |
| 1775 | // target@PLT. |
| 1776 | |
| 1777 | // Note: The actual moving to ECX is done further down. |
| 1778 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); |
| 1779 | if (G && !G->getGlobal()->hasHiddenVisibility() && |
| 1780 | !G->getGlobal()->hasProtectedVisibility()) |
| 1781 | Callee = LowerGlobalAddress(Callee, DAG); |
| 1782 | else if (isa<ExternalSymbolSDNode>(Callee)) |
Chris Lattner | 15a380a | 2009-07-09 04:39:06 +0000 | [diff] [blame] | 1783 | Callee = LowerExternalSymbol(Callee, DAG); |
Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 1784 | } |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1785 | } |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1786 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1787 | if (Is64Bit && isVarArg) { |
| 1788 | // From AMD64 ABI document: |
| 1789 | // For calls that may call functions that use varargs or stdargs |
| 1790 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 1791 | // the declaration) %al is used as hidden argument to specify the number |
| 1792 | // of SSE registers used. The contents of %al do not need to match exactly |
| 1793 | // the number of registers, but must be an ubound on the number of SSE |
| 1794 | // registers used and is in the range 0 - 8 inclusive. |
Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1795 | |
| 1796 | // FIXME: Verify this on Win64 |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1797 | // Count the number of XMM registers allocated. |
| 1798 | static const unsigned XMMArgRegs[] = { |
| 1799 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1800 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1801 | }; |
| 1802 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1803 | assert((Subtarget->hasSSE1() || !NumXMMRegs) |
Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1804 | && "SSE registers cannot be used when SSE is disabled"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1805 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1806 | Chain = DAG.getCopyToReg(Chain, dl, X86::AL, |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1807 | DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); |
| 1808 | InFlag = Chain.getValue(1); |
| 1809 | } |
| 1810 | |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1811 | |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1812 | // For tail calls lower the arguments to the 'real' stack slot. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1813 | if (isTailCall) { |
| 1814 | // Force all the incoming stack arguments to be loaded from the stack |
| 1815 | // before any new outgoing arguments are stored to the stack, because the |
| 1816 | // outgoing stack slots may alias the incoming argument stack slots, and |
| 1817 | // the alias isn't otherwise explicit. This is slightly more conservative |
| 1818 | // than necessary, because it means that each store effectively depends |
| 1819 | // on every argument instead of just those arguments it would clobber. |
| 1820 | SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain); |
| 1821 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1822 | SmallVector<SDValue, 8> MemOpChains2; |
| 1823 | SDValue FIN; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1824 | int FI = 0; |
Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1825 | // Do not flag preceeding copytoreg stuff together with the following stuff. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1826 | InFlag = SDValue(); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1827 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1828 | CCValAssign &VA = ArgLocs[i]; |
| 1829 | if (!VA.isRegLoc()) { |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1830 | assert(VA.isMemLoc()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1831 | SDValue Arg = Outs[i].Val; |
| 1832 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1833 | // Create frame index. |
| 1834 | int32_t Offset = VA.getLocMemOffset()+FPDiff; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1835 | uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1836 | FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1837 | FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1838 | |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1839 | if (Flags.isByVal()) { |
Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1840 | // Copy relative to framepointer. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1841 | SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1842 | if (StackPtr.getNode() == 0) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1843 | StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1844 | getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1845 | Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1846 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1847 | MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, |
| 1848 | ArgChain, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1849 | Flags, DAG, dl)); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1850 | } else { |
Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 1851 | // Store relative to framepointer. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1852 | MemOpChains2.push_back( |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1853 | DAG.getStore(ArgChain, dl, Arg, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 1854 | PseudoSourceValue::getFixedStack(FI), 0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1855 | } |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | if (!MemOpChains2.empty()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1860 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Arnold Schwaighofer | 719eb02 | 2008-01-11 14:34:56 +0000 | [diff] [blame] | 1861 | &MemOpChains2[0], MemOpChains2.size()); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1862 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1863 | // Copy arguments to their registers. |
| 1864 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1865 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1866 | RegsToPass[i].second, InFlag); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1867 | InFlag = Chain.getValue(1); |
| 1868 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1869 | InFlag =SDValue(); |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1870 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1871 | // Store the return address to the appropriate stack slot. |
Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1872 | Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1873 | FPDiff, dl); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1876 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1877 | // 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] | 1878 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 1879 | // We should use extra load for direct calls to dllimported functions in |
| 1880 | // non-JIT mode. |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1881 | GlobalValue *GV = G->getGlobal(); |
Chris Lattner | 754b765 | 2009-07-10 05:48:03 +0000 | [diff] [blame] | 1882 | if (!GV->hasDLLImportLinkage()) { |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1883 | unsigned char OpFlags = 0; |
| 1884 | |
| 1885 | // On ELF targets, in both X86-64 and X86-32 mode, direct calls to |
| 1886 | // external symbols most go through the PLT in PIC mode. If the symbol |
| 1887 | // has hidden or protected visibility, or if it is static or local, then |
| 1888 | // we don't need to use the PLT - we can directly call it. |
| 1889 | if (Subtarget->isTargetELF() && |
| 1890 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1891 | GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) { |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1892 | OpFlags = X86II::MO_PLT; |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 1893 | } else if (Subtarget->isPICStyleStubAny() && |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1894 | (GV->isDeclaration() || GV->isWeakForLinker()) && |
| 1895 | Subtarget->getDarwinVers() < 9) { |
| 1896 | // PC-relative references to external symbols should go through $stub, |
| 1897 | // unless we're building with the leopard linker or later, which |
| 1898 | // automatically synthesizes these stubs. |
| 1899 | OpFlags = X86II::MO_DARWIN_STUB; |
| 1900 | } |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1902 | Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(), |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1903 | G->getOffset(), OpFlags); |
| 1904 | } |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1905 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1906 | unsigned char OpFlags = 0; |
| 1907 | |
| 1908 | // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external |
| 1909 | // symbols should go through the PLT. |
| 1910 | if (Subtarget->isTargetELF() && |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1911 | getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1912 | OpFlags = X86II::MO_PLT; |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 1913 | } else if (Subtarget->isPICStyleStubAny() && |
Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 1914 | Subtarget->getDarwinVers() < 9) { |
| 1915 | // PC-relative references to external symbols should go through $stub, |
| 1916 | // unless we're building with the leopard linker or later, which |
| 1917 | // automatically synthesizes these stubs. |
| 1918 | OpFlags = X86II::MO_DARWIN_STUB; |
| 1919 | } |
| 1920 | |
Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 1921 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(), |
| 1922 | OpFlags); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1923 | } else if (isTailCall) { |
Arnold Schwaighofer | bbd8c33 | 2009-06-12 16:26:57 +0000 | [diff] [blame] | 1924 | unsigned Opc = Is64Bit ? X86::R11 : X86::EAX; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1925 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1926 | Chain = DAG.getCopyToReg(Chain, dl, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1927 | DAG.getRegister(Opc, getPointerTy()), |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1928 | Callee,InFlag); |
| 1929 | Callee = DAG.getRegister(Opc, getPointerTy()); |
| 1930 | // Add register as live out. |
Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1931 | MF.getRegInfo().addLiveOut(Opc); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1932 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1933 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 1934 | // Returns a chain & a flag for retval copy to use. |
| 1935 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1936 | SmallVector<SDValue, 8> Ops; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1937 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1938 | if (isTailCall) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1939 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 1940 | DAG.getIntPtrConstant(0, true), InFlag); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1941 | InFlag = Chain.getValue(1); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1942 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1943 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1944 | Ops.push_back(Chain); |
| 1945 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1946 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1947 | if (isTailCall) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1948 | Ops.push_back(DAG.getConstant(FPDiff, MVT::i32)); |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1949 | |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1950 | // Add argument registers to the end of the list so that they are known live |
| 1951 | // into the call. |
Evan Cheng | 9b44944 | 2008-01-07 23:08:23 +0000 | [diff] [blame] | 1952 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 1953 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 1954 | RegsToPass[i].second.getValueType())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1955 | |
Evan Cheng | 586ccac | 2008-03-18 23:36:35 +0000 | [diff] [blame] | 1956 | // Add an implicit use GOT pointer in EBX. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1957 | if (!isTailCall && Subtarget->isPICStyleGOT()) |
Evan Cheng | 586ccac | 2008-03-18 23:36:35 +0000 | [diff] [blame] | 1958 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
| 1959 | |
| 1960 | // Add an implicit use of AL for x86 vararg functions. |
| 1961 | if (Is64Bit && isVarArg) |
| 1962 | Ops.push_back(DAG.getRegister(X86::AL, MVT::i8)); |
| 1963 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1964 | if (InFlag.getNode()) |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1965 | Ops.push_back(InFlag); |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1966 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1967 | if (isTailCall) { |
| 1968 | // If this is the first return lowered for this function, add the regs |
| 1969 | // to the liveout set for the function. |
| 1970 | if (MF.getRegInfo().liveout_empty()) { |
| 1971 | SmallVector<CCValAssign, 16> RVLocs; |
| 1972 | CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs, |
| 1973 | *DAG.getContext()); |
| 1974 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); |
| 1975 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 1976 | if (RVLocs[i].isRegLoc()) |
| 1977 | MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
| 1978 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1979 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1980 | assert(((Callee.getOpcode() == ISD::Register && |
| 1981 | (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX || |
| 1982 | cast<RegisterSDNode>(Callee)->getReg() == X86::R9)) || |
| 1983 | Callee.getOpcode() == ISD::TargetExternalSymbol || |
| 1984 | Callee.getOpcode() == ISD::TargetGlobalAddress) && |
| 1985 | "Expecting an global address, external symbol, or register"); |
| 1986 | |
| 1987 | return DAG.getNode(X86ISD::TC_RETURN, dl, |
| 1988 | NodeTys, &Ops[0], Ops.size()); |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 1991 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1992 | InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 1994 | // Create the CALLSEQ_END node. |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1995 | unsigned NumBytesForCalleeToPush; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1996 | if (IsCalleePop(isVarArg, CallConv)) |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1997 | NumBytesForCalleeToPush = NumBytes; // Callee pops everything |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1998 | else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet) |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1999 | // If this is is a call to a struct-return function, the callee |
| 2000 | // pops the hidden struct pointer, so we have to push it back. |
| 2001 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2002 | NumBytesForCalleeToPush = 4; |
Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2003 | else |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2004 | NumBytesForCalleeToPush = 0; // Callee pops nothing. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2005 | |
Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2006 | // Returns a flag for retval copy to use. |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 2007 | Chain = DAG.getCALLSEQ_END(Chain, |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 2008 | DAG.getIntPtrConstant(NumBytes, true), |
| 2009 | DAG.getIntPtrConstant(NumBytesForCalleeToPush, |
| 2010 | true), |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 2011 | InFlag); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 2012 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 2014 | // Handle result values, copying them out of physregs into vregs that we |
| 2015 | // return. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2016 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 2017 | Ins, dl, DAG, InVals); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2020 | |
| 2021 | //===----------------------------------------------------------------------===// |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2022 | // Fast Calling Convention (tail call) implementation |
| 2023 | //===----------------------------------------------------------------------===// |
| 2024 | |
| 2025 | // Like std call, callee cleans arguments, convention except that ECX is |
| 2026 | // reserved for storing the tail called function address. Only 2 registers are |
| 2027 | // free for argument passing (inreg). Tail call optimization is performed |
| 2028 | // provided: |
| 2029 | // * tailcallopt is enabled |
| 2030 | // * caller/callee are fastcc |
Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 2031 | // On X86_64 architecture with GOT-style position independent code only local |
| 2032 | // (within module) calls are supported at the moment. |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 2033 | // To keep the stack aligned according to platform abi the function |
| 2034 | // GetAlignedArgumentStackSize ensures that argument delta is always multiples |
| 2035 | // of stack alignment. (Dynamic linkers need this - darwin's dyld for example) |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2036 | // If a tail called function callee has more arguments than the caller the |
| 2037 | // 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] | 2038 | // 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] | 2039 | // original REtADDR, but before the saved framepointer or the spilled registers |
| 2040 | // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) |
| 2041 | // stack layout: |
| 2042 | // arg1 |
| 2043 | // arg2 |
| 2044 | // RETADDR |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2045 | // [ new RETADDR |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2046 | // move area ] |
| 2047 | // (possible EBP) |
| 2048 | // ESI |
| 2049 | // EDI |
| 2050 | // local1 .. |
| 2051 | |
| 2052 | /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned |
| 2053 | /// for a 16 byte align requirement. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2054 | unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2055 | SelectionDAG& DAG) { |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2056 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2057 | const TargetMachine &TM = MF.getTarget(); |
| 2058 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 2059 | unsigned StackAlignment = TFI.getStackAlignment(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2060 | uint64_t AlignMask = StackAlignment - 1; |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2061 | int64_t Offset = StackSize; |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 2062 | uint64_t SlotSize = TD->getPointerSize(); |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2063 | if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) { |
| 2064 | // Number smaller than 12 so just add the difference. |
| 2065 | Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask)); |
| 2066 | } else { |
| 2067 | // Mask out lower bits, add stackalignment once plus the 12 bytes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2068 | Offset = ((~AlignMask) & Offset) + StackAlignment + |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2069 | (StackAlignment-SlotSize); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2070 | } |
Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2071 | return Offset; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2074 | /// IsEligibleForTailCallOptimization - Check whether the call is eligible |
| 2075 | /// for tail call optimization. Targets which want to do tail call |
| 2076 | /// optimization should implement this function. |
| 2077 | bool |
| 2078 | X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
| 2079 | unsigned CalleeCC, |
| 2080 | bool isVarArg, |
| 2081 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 2082 | SelectionDAG& DAG) const { |
| 2083 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2084 | unsigned CallerCC = MF.getFunction()->getCallingConv(); |
| 2085 | return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 2088 | FastISel * |
| 2089 | X86TargetLowering::createFastISel(MachineFunction &mf, |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 2090 | MachineModuleInfo *mmo, |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 2091 | DwarfWriter *dw, |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 2092 | DenseMap<const Value *, unsigned> &vm, |
| 2093 | DenseMap<const BasicBlock *, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 2094 | MachineBasicBlock *> &bm, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 2095 | DenseMap<const AllocaInst *, int> &am |
| 2096 | #ifndef NDEBUG |
| 2097 | , SmallSet<Instruction*, 8> &cil |
| 2098 | #endif |
| 2099 | ) { |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 2100 | return X86::createFastISel(mf, mmo, dw, vm, bm, am |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 2101 | #ifndef NDEBUG |
| 2102 | , cil |
| 2103 | #endif |
| 2104 | ); |
Dan Gohman | d9f3c48 | 2008-08-19 21:32:53 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 2108 | //===----------------------------------------------------------------------===// |
| 2109 | // Other Lowering Hooks |
| 2110 | //===----------------------------------------------------------------------===// |
| 2111 | |
| 2112 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2113 | SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 2114 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2115 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 2116 | int ReturnAddrIndex = FuncInfo->getRAIndex(); |
| 2117 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2118 | if (ReturnAddrIndex == 0) { |
| 2119 | // Set up a frame object for the return address. |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 2120 | uint64_t SlotSize = TD->getPointerSize(); |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 2121 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize); |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 2122 | FuncInfo->setRAIndex(ReturnAddrIndex); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2125 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2129 | /// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 2130 | /// specific condition code, returning the condition code and the LHS/RHS of the |
| 2131 | /// comparison to make. |
| 2132 | static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
| 2133 | SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) { |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2134 | if (!isFP) { |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2135 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 2136 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 2137 | // X > -1 -> X == 0, jump !sign. |
| 2138 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2139 | return X86::COND_NS; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2140 | } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| 2141 | // X < 0 -> X == 0, jump on sign. |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2142 | return X86::COND_S; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2143 | } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) { |
Dan Gohman | 5f6913c | 2007-09-17 14:49:27 +0000 | [diff] [blame] | 2144 | // X < 1 -> X <= 0 |
| 2145 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2146 | return X86::COND_LE; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 2147 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 2148 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2149 | |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2150 | switch (SetCCOpcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2151 | default: llvm_unreachable("Invalid integer condition!"); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2152 | case ISD::SETEQ: return X86::COND_E; |
| 2153 | case ISD::SETGT: return X86::COND_G; |
| 2154 | case ISD::SETGE: return X86::COND_GE; |
| 2155 | case ISD::SETLT: return X86::COND_L; |
| 2156 | case ISD::SETLE: return X86::COND_LE; |
| 2157 | case ISD::SETNE: return X86::COND_NE; |
| 2158 | case ISD::SETULT: return X86::COND_B; |
| 2159 | case ISD::SETUGT: return X86::COND_A; |
| 2160 | case ISD::SETULE: return X86::COND_BE; |
| 2161 | case ISD::SETUGE: return X86::COND_AE; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2162 | } |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2163 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2164 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2165 | // 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] | 2166 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2167 | // If LHS is a foldable load, but RHS is not, flip the condition. |
| 2168 | if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) && |
| 2169 | !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) { |
| 2170 | SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode); |
| 2171 | std::swap(LHS, RHS); |
Evan Cheng | 4d46d0a | 2008-08-28 23:48:31 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2174 | switch (SetCCOpcode) { |
| 2175 | default: break; |
| 2176 | case ISD::SETOLT: |
| 2177 | case ISD::SETOLE: |
| 2178 | case ISD::SETUGT: |
| 2179 | case ISD::SETUGE: |
| 2180 | std::swap(LHS, RHS); |
| 2181 | break; |
| 2182 | } |
| 2183 | |
| 2184 | // On a floating point condition, the flags are set as follows: |
| 2185 | // ZF PF CF op |
| 2186 | // 0 | 0 | 0 | X > Y |
| 2187 | // 0 | 0 | 1 | X < Y |
| 2188 | // 1 | 0 | 0 | X == Y |
| 2189 | // 1 | 1 | 1 | unordered |
| 2190 | switch (SetCCOpcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2191 | default: llvm_unreachable("Condcode should be pre-legalized away"); |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2192 | case ISD::SETUEQ: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2193 | case ISD::SETEQ: return X86::COND_E; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2194 | case ISD::SETOLT: // flipped |
| 2195 | case ISD::SETOGT: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2196 | case ISD::SETGT: return X86::COND_A; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2197 | case ISD::SETOLE: // flipped |
| 2198 | case ISD::SETOGE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2199 | case ISD::SETGE: return X86::COND_AE; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2200 | case ISD::SETUGT: // flipped |
| 2201 | case ISD::SETULT: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2202 | case ISD::SETLT: return X86::COND_B; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2203 | case ISD::SETUGE: // flipped |
| 2204 | case ISD::SETULE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2205 | case ISD::SETLE: return X86::COND_BE; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2206 | case ISD::SETONE: |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 2207 | case ISD::SETNE: return X86::COND_NE; |
| 2208 | case ISD::SETUO: return X86::COND_P; |
| 2209 | case ISD::SETO: return X86::COND_NP; |
Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 2210 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2213 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 2214 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2215 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2216 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2217 | switch (X86CC) { |
| 2218 | default: |
| 2219 | return false; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2220 | case X86::COND_B: |
| 2221 | case X86::COND_BE: |
| 2222 | case X86::COND_E: |
| 2223 | case X86::COND_P: |
| 2224 | case X86::COND_A: |
| 2225 | case X86::COND_AE: |
| 2226 | case X86::COND_NE: |
| 2227 | case X86::COND_NP: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2228 | return true; |
| 2229 | } |
| 2230 | } |
| 2231 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2232 | /// isUndefOrInRange - Return true if Val is undef or if its value falls within |
| 2233 | /// the specified range (L, H]. |
| 2234 | static bool isUndefOrInRange(int Val, int Low, int Hi) { |
| 2235 | return (Val < 0) || (Val >= Low && Val < Hi); |
| 2236 | } |
| 2237 | |
| 2238 | /// isUndefOrEqual - Val is either less than zero (undef) or equal to the |
| 2239 | /// specified value. |
| 2240 | static bool isUndefOrEqual(int Val, int CmpVal) { |
| 2241 | if (Val < 0 || Val == CmpVal) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2242 | return true; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2243 | return false; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2246 | /// isPSHUFDMask - Return true if the node specifies a shuffle of elements that |
| 2247 | /// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference |
| 2248 | /// the second operand. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2249 | static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2250 | if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16) |
| 2251 | return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4); |
| 2252 | if (VT == MVT::v2f64 || VT == MVT::v2i64) |
| 2253 | return (Mask[0] < 2 && Mask[1] < 2); |
| 2254 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2257 | bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) { |
| 2258 | SmallVector<int, 8> M; |
| 2259 | N->getMask(M); |
| 2260 | return ::isPSHUFDMask(M, N->getValueType(0)); |
| 2261 | } |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2262 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2263 | /// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that |
| 2264 | /// is suitable for input to PSHUFHW. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2265 | static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2266 | if (VT != MVT::v8i16) |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2267 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2268 | |
| 2269 | // Lower quadword copied in order or undef. |
| 2270 | for (int i = 0; i != 4; ++i) |
| 2271 | if (Mask[i] >= 0 && Mask[i] != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2272 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2273 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2274 | // Upper quadword shuffled. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2275 | for (int i = 4; i != 8; ++i) |
| 2276 | if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2277 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2278 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2279 | return true; |
| 2280 | } |
| 2281 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2282 | bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) { |
| 2283 | SmallVector<int, 8> M; |
| 2284 | N->getMask(M); |
| 2285 | return ::isPSHUFHWMask(M, N->getValueType(0)); |
| 2286 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2287 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2288 | /// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that |
| 2289 | /// is suitable for input to PSHUFLW. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2290 | static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2291 | if (VT != MVT::v8i16) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2292 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2293 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2294 | // Upper quadword copied in order. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2295 | for (int i = 4; i != 8; ++i) |
| 2296 | if (Mask[i] >= 0 && Mask[i] != i) |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2297 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2298 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2299 | // Lower quadword shuffled. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2300 | for (int i = 0; i != 4; ++i) |
| 2301 | if (Mask[i] >= 4) |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2302 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2303 | |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2304 | return true; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2307 | bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) { |
| 2308 | SmallVector<int, 8> M; |
| 2309 | N->getMask(M); |
| 2310 | return ::isPSHUFLWMask(M, N->getValueType(0)); |
| 2311 | } |
| 2312 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2313 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2314 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2315 | static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2316 | int NumElems = VT.getVectorNumElements(); |
| 2317 | if (NumElems != 2 && NumElems != 4) |
| 2318 | return false; |
| 2319 | |
| 2320 | int Half = NumElems / 2; |
| 2321 | for (int i = 0; i < Half; ++i) |
| 2322 | if (!isUndefOrInRange(Mask[i], 0, NumElems)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2323 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2324 | for (int i = Half; i < NumElems; ++i) |
| 2325 | if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2326 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2327 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2328 | return true; |
| 2329 | } |
| 2330 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2331 | bool X86::isSHUFPMask(ShuffleVectorSDNode *N) { |
| 2332 | SmallVector<int, 8> M; |
| 2333 | N->getMask(M); |
| 2334 | return ::isSHUFPMask(M, N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2337 | /// isCommutedSHUFP - Returns true if the shuffle mask is exactly |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2338 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 2339 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 2340 | /// the upper half to come from vector 2. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2341 | static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2342 | int NumElems = VT.getVectorNumElements(); |
| 2343 | |
| 2344 | if (NumElems != 2 && NumElems != 4) |
| 2345 | return false; |
| 2346 | |
| 2347 | int Half = NumElems / 2; |
| 2348 | for (int i = 0; i < Half; ++i) |
| 2349 | if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2350 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2351 | for (int i = Half; i < NumElems; ++i) |
| 2352 | if (!isUndefOrInRange(Mask[i], 0, NumElems)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2353 | return false; |
| 2354 | return true; |
| 2355 | } |
| 2356 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2357 | static bool isCommutedSHUFP(ShuffleVectorSDNode *N) { |
| 2358 | SmallVector<int, 8> M; |
| 2359 | N->getMask(M); |
| 2360 | return isCommutedSHUFPMask(M, N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2363 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2364 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2365 | bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) { |
| 2366 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2367 | return false; |
| 2368 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 2369 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2370 | return isUndefOrEqual(N->getMaskElt(0), 6) && |
| 2371 | isUndefOrEqual(N->getMaskElt(1), 7) && |
| 2372 | isUndefOrEqual(N->getMaskElt(2), 2) && |
| 2373 | isUndefOrEqual(N->getMaskElt(3), 3); |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2376 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2377 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2378 | bool X86::isMOVLPMask(ShuffleVectorSDNode *N) { |
| 2379 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2380 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2381 | if (NumElems != 2 && NumElems != 4) |
| 2382 | return false; |
| 2383 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2384 | for (unsigned i = 0; i < NumElems/2; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2385 | if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2386 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2387 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2388 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2389 | if (!isUndefOrEqual(N->getMaskElt(i), i)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2390 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2391 | |
| 2392 | return true; |
| 2393 | } |
| 2394 | |
| 2395 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2396 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 2397 | /// and MOVLHPS. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2398 | bool X86::isMOVHPMask(ShuffleVectorSDNode *N) { |
| 2399 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2400 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2401 | if (NumElems != 2 && NumElems != 4) |
| 2402 | return false; |
| 2403 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2404 | for (unsigned i = 0; i < NumElems/2; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2405 | if (!isUndefOrEqual(N->getMaskElt(i), i)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2406 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2407 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2408 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2409 | if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2410 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2411 | |
| 2412 | return true; |
| 2413 | } |
| 2414 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2415 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 2416 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 2417 | /// <2, 3, 2, 3> |
| 2418 | bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2419 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
| 2420 | |
| 2421 | if (NumElems != 4) |
| 2422 | return false; |
| 2423 | |
| 2424 | return isUndefOrEqual(N->getMaskElt(0), 2) && |
| 2425 | isUndefOrEqual(N->getMaskElt(1), 3) && |
| 2426 | isUndefOrEqual(N->getMaskElt(2), 2) && |
| 2427 | isUndefOrEqual(N->getMaskElt(3), 3); |
| 2428 | } |
| 2429 | |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2430 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2431 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2432 | static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT, |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2433 | bool V2IsSplat = false) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2434 | int NumElts = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2435 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2436 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2437 | |
| 2438 | for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2439 | int BitI = Mask[i]; |
| 2440 | int BitI1 = Mask[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2441 | if (!isUndefOrEqual(BitI, j)) |
| 2442 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2443 | if (V2IsSplat) { |
Mon P Wang | 7bcaefa | 2009-02-04 01:16:59 +0000 | [diff] [blame] | 2444 | if (!isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2445 | return false; |
| 2446 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2447 | if (!isUndefOrEqual(BitI1, j + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2448 | return false; |
| 2449 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2450 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2451 | return true; |
| 2452 | } |
| 2453 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2454 | bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) { |
| 2455 | SmallVector<int, 8> M; |
| 2456 | N->getMask(M); |
| 2457 | return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2460 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2461 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2462 | static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT, |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2463 | bool V2IsSplat = false) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2464 | int NumElts = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2465 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2466 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2467 | |
| 2468 | for (int i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 2469 | int BitI = Mask[i]; |
| 2470 | int BitI1 = Mask[i+1]; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2471 | if (!isUndefOrEqual(BitI, j + NumElts/2)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2472 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2473 | if (V2IsSplat) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2474 | if (isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2475 | return false; |
| 2476 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2477 | if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2478 | return false; |
| 2479 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2480 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2481 | return true; |
| 2482 | } |
| 2483 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2484 | bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) { |
| 2485 | SmallVector<int, 8> M; |
| 2486 | N->getMask(M); |
| 2487 | return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2490 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 2491 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 2492 | /// <0, 0, 1, 1> |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2493 | static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2494 | int NumElems = VT.getVectorNumElements(); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2495 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2496 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2497 | |
| 2498 | for (int i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 2499 | int BitI = Mask[i]; |
| 2500 | int BitI1 = Mask[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2501 | if (!isUndefOrEqual(BitI, j)) |
| 2502 | return false; |
| 2503 | if (!isUndefOrEqual(BitI1, j)) |
| 2504 | return false; |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2505 | } |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2506 | return true; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2509 | bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2510 | SmallVector<int, 8> M; |
| 2511 | N->getMask(M); |
| 2512 | return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0)); |
| 2513 | } |
| 2514 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2515 | /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form |
| 2516 | /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, |
| 2517 | /// <2, 2, 3, 3> |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2518 | static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2519 | int NumElems = VT.getVectorNumElements(); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2520 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2521 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2522 | |
| 2523 | for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) { |
| 2524 | int BitI = Mask[i]; |
| 2525 | int BitI1 = Mask[i+1]; |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2526 | if (!isUndefOrEqual(BitI, j)) |
| 2527 | return false; |
| 2528 | if (!isUndefOrEqual(BitI1, j)) |
| 2529 | return false; |
| 2530 | } |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2531 | return true; |
Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2534 | bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) { |
| 2535 | SmallVector<int, 8> M; |
| 2536 | N->getMask(M); |
| 2537 | return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0)); |
| 2538 | } |
| 2539 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2540 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2541 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 2542 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2543 | static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) { |
Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 2544 | if (VT.getVectorElementType().getSizeInBits() < 32) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2545 | return false; |
Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 2546 | |
| 2547 | int NumElts = VT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2548 | |
| 2549 | if (!isUndefOrEqual(Mask[0], NumElts)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2550 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2551 | |
| 2552 | for (int i = 1; i < NumElts; ++i) |
| 2553 | if (!isUndefOrEqual(Mask[i], i)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2554 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2555 | |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2556 | return true; |
| 2557 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2558 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2559 | bool X86::isMOVLMask(ShuffleVectorSDNode *N) { |
| 2560 | SmallVector<int, 8> M; |
| 2561 | N->getMask(M); |
| 2562 | return ::isMOVLMask(M, N->getValueType(0)); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2565 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 2566 | /// 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] | 2567 | /// element of vector 2 and the other elements to come from vector 1 in order. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2568 | static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2569 | bool V2IsSplat = false, bool V2IsUndef = false) { |
| 2570 | int NumOps = VT.getVectorNumElements(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2571 | if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2572 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2573 | |
| 2574 | if (!isUndefOrEqual(Mask[0], 0)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2575 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2576 | |
| 2577 | for (int i = 1; i < NumOps; ++i) |
| 2578 | if (!(isUndefOrEqual(Mask[i], i+NumOps) || |
| 2579 | (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) || |
| 2580 | (V2IsSplat && isUndefOrEqual(Mask[i], NumOps)))) |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2581 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2582 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2583 | return true; |
| 2584 | } |
| 2585 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2586 | static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false, |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2587 | bool V2IsUndef = false) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2588 | SmallVector<int, 8> M; |
| 2589 | N->getMask(M); |
| 2590 | return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2593 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2594 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2595 | bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) { |
| 2596 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2597 | return false; |
| 2598 | |
| 2599 | // Expect 1, 1, 3, 3 |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2600 | for (unsigned i = 0; i < 2; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2601 | int Elt = N->getMaskElt(i); |
| 2602 | if (Elt >= 0 && Elt != 1) |
| 2603 | return false; |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2604 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2605 | |
| 2606 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2607 | for (unsigned i = 2; i < 4; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2608 | int Elt = N->getMaskElt(i); |
| 2609 | if (Elt >= 0 && Elt != 3) |
| 2610 | return false; |
| 2611 | if (Elt == 3) |
| 2612 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2613 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2614 | // Don't use movshdup if it can be done with a shufps. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2615 | // FIXME: verify that matching u, u, 3, 3 is what we want. |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2616 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2620 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2621 | bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) { |
| 2622 | if (N->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2623 | return false; |
| 2624 | |
| 2625 | // Expect 0, 0, 2, 2 |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2626 | for (unsigned i = 0; i < 2; ++i) |
| 2627 | if (N->getMaskElt(i) > 0) |
| 2628 | return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2629 | |
| 2630 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2631 | for (unsigned i = 2; i < 4; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2632 | int Elt = N->getMaskElt(i); |
| 2633 | if (Elt >= 0 && Elt != 2) |
| 2634 | return false; |
| 2635 | if (Elt == 2) |
| 2636 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2637 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2638 | // Don't use movsldup if it can be done with a shufps. |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2639 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2642 | /// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2643 | /// specifies a shuffle of elements that is suitable for input to MOVDDUP. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2644 | bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) { |
| 2645 | int e = N->getValueType(0).getVectorNumElements() / 2; |
| 2646 | |
| 2647 | for (int i = 0; i < e; ++i) |
| 2648 | if (!isUndefOrEqual(N->getMaskElt(i), i)) |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2649 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2650 | for (int i = 0; i < e; ++i) |
| 2651 | if (!isUndefOrEqual(N->getMaskElt(e+i), i)) |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2652 | return false; |
| 2653 | return true; |
| 2654 | } |
| 2655 | |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2656 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 2657 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 2658 | /// instructions. |
| 2659 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2660 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 2661 | int NumOperands = SVOp->getValueType(0).getVectorNumElements(); |
| 2662 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2663 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 2664 | unsigned Mask = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2665 | for (int i = 0; i < NumOperands; ++i) { |
| 2666 | int Val = SVOp->getMaskElt(NumOperands-i-1); |
| 2667 | if (Val < 0) Val = 0; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2668 | if (Val >= NumOperands) Val -= NumOperands; |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2669 | Mask |= Val; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2670 | if (i != NumOperands - 1) |
| 2671 | Mask <<= Shift; |
| 2672 | } |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2673 | return Mask; |
| 2674 | } |
| 2675 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2676 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 2677 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 2678 | /// instructions. |
| 2679 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2680 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2681 | unsigned Mask = 0; |
| 2682 | // 8 nodes, but we only care about the last 4. |
| 2683 | for (unsigned i = 7; i >= 4; --i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2684 | int Val = SVOp->getMaskElt(i); |
| 2685 | if (Val >= 0) |
Mon P Wang | 7bcaefa | 2009-02-04 01:16:59 +0000 | [diff] [blame] | 2686 | Mask |= (Val - 4); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2687 | if (i != 4) |
| 2688 | Mask <<= 2; |
| 2689 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2690 | return Mask; |
| 2691 | } |
| 2692 | |
| 2693 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2694 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2695 | /// instructions. |
| 2696 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2697 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2698 | unsigned Mask = 0; |
| 2699 | // 8 nodes, but we only care about the first 4. |
| 2700 | for (int i = 3; i >= 0; --i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2701 | int Val = SVOp->getMaskElt(i); |
| 2702 | if (Val >= 0) |
| 2703 | Mask |= Val; |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2704 | if (i != 0) |
| 2705 | Mask <<= 2; |
| 2706 | } |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2707 | return Mask; |
| 2708 | } |
| 2709 | |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 2710 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2711 | /// constant +0.0. |
| 2712 | bool X86::isZeroNode(SDValue Elt) { |
| 2713 | return ((isa<ConstantSDNode>(Elt) && |
| 2714 | cast<ConstantSDNode>(Elt)->getZExtValue() == 0) || |
| 2715 | (isa<ConstantFPSDNode>(Elt) && |
| 2716 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
| 2717 | } |
| 2718 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2719 | /// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in |
| 2720 | /// their permute mask. |
| 2721 | static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp, |
| 2722 | SelectionDAG &DAG) { |
| 2723 | MVT VT = SVOp->getValueType(0); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2724 | unsigned NumElems = VT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2725 | SmallVector<int, 8> MaskVec; |
| 2726 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2727 | for (unsigned i = 0; i != NumElems; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2728 | int idx = SVOp->getMaskElt(i); |
| 2729 | if (idx < 0) |
| 2730 | MaskVec.push_back(idx); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2731 | else if (idx < (int)NumElems) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2732 | MaskVec.push_back(idx + NumElems); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2733 | else |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2734 | MaskVec.push_back(idx - NumElems); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2735 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2736 | return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1), |
| 2737 | SVOp->getOperand(0), &MaskVec[0]); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Evan Cheng | 779ccea | 2007-12-07 21:30:01 +0000 | [diff] [blame] | 2740 | /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming |
| 2741 | /// the two vector operands have swapped position. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2742 | static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2743 | unsigned NumElems = VT.getVectorNumElements(); |
| 2744 | for (unsigned i = 0; i != NumElems; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2745 | int idx = Mask[i]; |
| 2746 | if (idx < 0) |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2747 | continue; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2748 | else if (idx < (int)NumElems) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2749 | Mask[i] = idx + NumElems; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2750 | else |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2751 | Mask[i] = idx - NumElems; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2752 | } |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2755 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2756 | /// match movhlps. The lower half elements should come from upper half of |
| 2757 | /// 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] | 2758 | /// half of V2 (and in order). |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2759 | static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) { |
| 2760 | if (Op->getValueType(0).getVectorNumElements() != 4) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2761 | return false; |
| 2762 | for (unsigned i = 0, e = 2; i != e; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2763 | if (!isUndefOrEqual(Op->getMaskElt(i), i+2)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2764 | return false; |
| 2765 | for (unsigned i = 2; i != 4; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2766 | if (!isUndefOrEqual(Op->getMaskElt(i), i+4)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2767 | return false; |
| 2768 | return true; |
| 2769 | } |
| 2770 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2771 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 2772 | /// is promoted to a vector. It also returns the LoadSDNode by reference if |
| 2773 | /// required. |
| 2774 | static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) { |
Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 2775 | if (N->getOpcode() != ISD::SCALAR_TO_VECTOR) |
| 2776 | return false; |
| 2777 | N = N->getOperand(0).getNode(); |
| 2778 | if (!ISD::isNON_EXTLoad(N)) |
| 2779 | return false; |
| 2780 | if (LD) |
| 2781 | *LD = cast<LoadSDNode>(N); |
| 2782 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2785 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2786 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2787 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2788 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2789 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2790 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, |
| 2791 | ShuffleVectorSDNode *Op) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2792 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2793 | return false; |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2794 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 2795 | // load folding shufps op. |
| 2796 | if (ISD::isNON_EXTLoad(V2)) |
| 2797 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2798 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2799 | unsigned NumElems = Op->getValueType(0).getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2800 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2801 | if (NumElems != 2 && NumElems != 4) |
| 2802 | return false; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2803 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2804 | if (!isUndefOrEqual(Op->getMaskElt(i), i)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2805 | return false; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2806 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2807 | if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2808 | return false; |
| 2809 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2812 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2813 | /// all the same. |
| 2814 | static bool isSplatVector(SDNode *N) { |
| 2815 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2816 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2817 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2818 | SDValue SplatValue = N->getOperand(0); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2819 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2820 | if (N->getOperand(i) != SplatValue) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2821 | return false; |
| 2822 | return true; |
| 2823 | } |
| 2824 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2825 | /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2826 | /// to an zero vector. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2827 | /// FIXME: move to dag combiner / method on ShuffleVectorSDNode |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2828 | static bool isZeroShuffle(ShuffleVectorSDNode *N) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2829 | SDValue V1 = N->getOperand(0); |
| 2830 | SDValue V2 = N->getOperand(1); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2831 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
| 2832 | for (unsigned i = 0; i != NumElems; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2833 | int Idx = N->getMaskElt(i); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2834 | if (Idx >= (int)NumElems) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2835 | unsigned Opc = V2.getOpcode(); |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2836 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode())) |
| 2837 | continue; |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 2838 | if (Opc != ISD::BUILD_VECTOR || |
| 2839 | !X86::isZeroNode(V2.getOperand(Idx-NumElems))) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2840 | return false; |
| 2841 | } else if (Idx >= 0) { |
| 2842 | unsigned Opc = V1.getOpcode(); |
| 2843 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode())) |
| 2844 | continue; |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 2845 | if (Opc != ISD::BUILD_VECTOR || |
| 2846 | !X86::isZeroNode(V1.getOperand(Idx))) |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2847 | return false; |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | return true; |
| 2851 | } |
| 2852 | |
| 2853 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2854 | /// |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2855 | static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG, |
| 2856 | DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2857 | assert(VT.isVector() && "Expected a vector type"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2858 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2859 | // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2860 | // type. This ensures they get CSE'd. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2861 | SDValue Vec; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2862 | if (VT.getSizeInBits() == 64) { // MMX |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2863 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2864 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2865 | } else if (HasSSE2) { // SSE2 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2866 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2867 | 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] | 2868 | } else { // SSE1 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2869 | SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2870 | 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] | 2871 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2872 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2875 | /// getOnesVector - Returns a vector of specified type with all bits set. |
| 2876 | /// |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2877 | static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2878 | assert(VT.isVector() && "Expected a vector type"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2879 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2880 | // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest |
| 2881 | // type. This ensures they get CSE'd. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2882 | SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32); |
| 2883 | SDValue Vec; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2884 | if (VT.getSizeInBits() == 64) // MMX |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2885 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2886 | else // SSE |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2887 | 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] | 2888 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2892 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2893 | /// that point to V2 points to its first element. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2894 | static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| 2895 | MVT VT = SVOp->getValueType(0); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2896 | unsigned NumElems = VT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2897 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2898 | bool Changed = false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2899 | SmallVector<int, 8> MaskVec; |
| 2900 | SVOp->getMask(MaskVec); |
| 2901 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 2902 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2903 | if (MaskVec[i] > (int)NumElems) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2904 | MaskVec[i] = NumElems; |
| 2905 | Changed = true; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2906 | } |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2907 | } |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2908 | if (Changed) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2909 | return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0), |
| 2910 | SVOp->getOperand(1), &MaskVec[0]); |
| 2911 | return SDValue(SVOp, 0); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2914 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2915 | /// operation of specified width. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2916 | static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2917 | SDValue V2) { |
| 2918 | unsigned NumElems = VT.getVectorNumElements(); |
| 2919 | SmallVector<int, 8> Mask; |
| 2920 | Mask.push_back(NumElems); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2921 | for (unsigned i = 1; i != NumElems; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2922 | Mask.push_back(i); |
| 2923 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2926 | /// getUnpackl - Returns a vector_shuffle node for an unpackl operation. |
| 2927 | static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2928 | SDValue V2) { |
| 2929 | unsigned NumElems = VT.getVectorNumElements(); |
| 2930 | SmallVector<int, 8> Mask; |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2931 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2932 | Mask.push_back(i); |
| 2933 | Mask.push_back(i + NumElems); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2934 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2935 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2938 | /// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation. |
| 2939 | static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1, |
| 2940 | SDValue V2) { |
| 2941 | unsigned NumElems = VT.getVectorNumElements(); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2942 | unsigned Half = NumElems/2; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2943 | SmallVector<int, 8> Mask; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2944 | for (unsigned i = 0; i != Half; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2945 | Mask.push_back(i + Half); |
| 2946 | Mask.push_back(i + NumElems + Half); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2947 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2948 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Evan Cheng | 0c0f83f | 2008-04-05 00:30:36 +0000 | [diff] [blame] | 2951 | /// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2952 | static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG, |
| 2953 | bool HasSSE2) { |
| 2954 | if (SV->getValueType(0).getVectorNumElements() <= 4) |
| 2955 | return SDValue(SV, 0); |
| 2956 | |
| 2957 | MVT PVT = MVT::v4f32; |
| 2958 | MVT VT = SV->getValueType(0); |
| 2959 | DebugLoc dl = SV->getDebugLoc(); |
| 2960 | SDValue V1 = SV->getOperand(0); |
| 2961 | int NumElems = VT.getVectorNumElements(); |
| 2962 | int EltNo = SV->getSplatIndex(); |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 2963 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2964 | // unpack elements to the correct location |
| 2965 | while (NumElems > 4) { |
| 2966 | if (EltNo < NumElems/2) { |
| 2967 | V1 = getUnpackl(DAG, dl, VT, V1, V1); |
| 2968 | } else { |
| 2969 | V1 = getUnpackh(DAG, dl, VT, V1, V1); |
| 2970 | EltNo -= NumElems/2; |
| 2971 | } |
| 2972 | NumElems >>= 1; |
| 2973 | } |
| 2974 | |
| 2975 | // Perform the splat. |
| 2976 | int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2977 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2978 | V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]); |
| 2979 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2982 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2983 | /// vector of zero or undef vector. This produces a shuffle where the low |
| 2984 | /// element of V2 is swizzled into the zero/undef vector, landing at element |
| 2985 | /// 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] | 2986 | static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx, |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 2987 | bool isZero, bool HasSSE2, |
| 2988 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2989 | MVT VT = V2.getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2990 | SDValue V1 = isZero |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2991 | ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT); |
| 2992 | unsigned NumElems = VT.getVectorNumElements(); |
| 2993 | SmallVector<int, 16> MaskVec; |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 2994 | for (unsigned i = 0; i != NumElems; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2995 | // If this is the insertion idx, put the low elt of V2 here. |
| 2996 | MaskVec.push_back(i == Idx ? NumElems : i); |
| 2997 | return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3000 | /// getNumOfConsecutiveZeros - Return the number of elements in a result of |
| 3001 | /// a shuffle that is zero. |
| 3002 | static |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3003 | unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems, |
| 3004 | bool Low, SelectionDAG &DAG) { |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3005 | unsigned NumZeros = 0; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3006 | for (int i = 0; i < NumElems; ++i) { |
Evan Cheng | ab26227 | 2008-06-25 20:52:59 +0000 | [diff] [blame] | 3007 | unsigned Index = Low ? i : NumElems-i-1; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3008 | int Idx = SVOp->getMaskElt(Index); |
| 3009 | if (Idx < 0) { |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3010 | ++NumZeros; |
| 3011 | continue; |
| 3012 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3013 | SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index); |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 3014 | if (Elt.getNode() && X86::isZeroNode(Elt)) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3015 | ++NumZeros; |
| 3016 | else |
| 3017 | break; |
| 3018 | } |
| 3019 | return NumZeros; |
| 3020 | } |
| 3021 | |
| 3022 | /// isVectorShift - Returns true if the shuffle can be implemented as a |
| 3023 | /// logical left or right shift of a vector. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3024 | /// FIXME: split into pslldqi, psrldqi, palignr variants. |
| 3025 | static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3026 | bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3027 | int NumElems = SVOp->getValueType(0).getVectorNumElements(); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3028 | |
| 3029 | isLeft = true; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3030 | unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3031 | if (!NumZeros) { |
| 3032 | isLeft = false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3033 | NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3034 | if (!NumZeros) |
| 3035 | return false; |
| 3036 | } |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3037 | bool SeenV1 = false; |
| 3038 | bool SeenV2 = false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3039 | for (int i = NumZeros; i < NumElems; ++i) { |
| 3040 | int Val = isLeft ? (i - NumZeros) : i; |
| 3041 | int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros)); |
| 3042 | if (Idx < 0) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3043 | continue; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3044 | if (Idx < NumElems) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3045 | SeenV1 = true; |
| 3046 | else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3047 | Idx -= NumElems; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3048 | SeenV2 = true; |
| 3049 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3050 | if (Idx != Val) |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3051 | return false; |
| 3052 | } |
| 3053 | if (SeenV1 && SeenV2) |
| 3054 | return false; |
| 3055 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3056 | ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3057 | ShAmt = NumZeros; |
| 3058 | return true; |
| 3059 | } |
| 3060 | |
| 3061 | |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3062 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 3063 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3064 | static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3065 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3066 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3067 | if (NumNonZero > 8) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3068 | return SDValue(); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3069 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3070 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3071 | SDValue V(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3072 | bool First = true; |
| 3073 | for (unsigned i = 0; i < 16; ++i) { |
| 3074 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 3075 | if (ThisIsNonZero && First) { |
| 3076 | if (NumZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3077 | V = getZeroVector(MVT::v8i16, true, DAG, dl); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3078 | else |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3079 | V = DAG.getUNDEF(MVT::v8i16); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3080 | First = false; |
| 3081 | } |
| 3082 | |
| 3083 | if ((i & 1) != 0) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3084 | SDValue ThisElt(0, 0), LastElt(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3085 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 3086 | if (LastIsNonZero) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3087 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3088 | MVT::i16, Op.getOperand(i-1)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3089 | } |
| 3090 | if (ThisIsNonZero) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3091 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i)); |
| 3092 | ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3093 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 3094 | if (LastIsNonZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3095 | ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3096 | } else |
| 3097 | ThisElt = LastElt; |
| 3098 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3099 | if (ThisElt.getNode()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3100 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3101 | DAG.getIntPtrConstant(i/2)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3102 | } |
| 3103 | } |
| 3104 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3105 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 3108 | /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3109 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3110 | static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros, |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3111 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3112 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3113 | if (NumNonZero > 4) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3114 | return SDValue(); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3115 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3116 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3117 | SDValue V(0, 0); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3118 | bool First = true; |
| 3119 | for (unsigned i = 0; i < 8; ++i) { |
| 3120 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 3121 | if (isNonZero) { |
| 3122 | if (First) { |
| 3123 | if (NumZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3124 | V = getZeroVector(MVT::v8i16, true, DAG, dl); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3125 | else |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3126 | V = DAG.getUNDEF(MVT::v8i16); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3127 | First = false; |
| 3128 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3129 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3130 | MVT::v8i16, V, Op.getOperand(i), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3131 | DAG.getIntPtrConstant(i)); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | return V; |
| 3136 | } |
| 3137 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3138 | /// getVShift - Return a vector logical shift node. |
| 3139 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3140 | static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3141 | unsigned NumBits, SelectionDAG &DAG, |
| 3142 | const TargetLowering &TLI, DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3143 | bool isMMX = VT.getSizeInBits() == 64; |
| 3144 | MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3145 | unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3146 | SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp); |
| 3147 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3148 | DAG.getNode(Opc, dl, ShVT, SrcOp, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 3149 | DAG.getConstant(NumBits, TLI.getShiftAmountTy()))); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3152 | SDValue |
| 3153 | X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3154 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3155 | // 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] | 3156 | if (ISD::isBuildVectorAllZeros(Op.getNode()) |
| 3157 | || ISD::isBuildVectorAllOnes(Op.getNode())) { |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3158 | // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to |
| 3159 | // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are |
| 3160 | // eliminated on x86-32 hosts. |
| 3161 | if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32) |
| 3162 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3163 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3164 | if (ISD::isBuildVectorAllOnes(Op.getNode())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3165 | return getOnesVector(Op.getValueType(), DAG, dl); |
| 3166 | return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl); |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3167 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3168 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3169 | MVT VT = Op.getValueType(); |
| 3170 | MVT EVT = VT.getVectorElementType(); |
| 3171 | unsigned EVTBits = EVT.getSizeInBits(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3172 | |
| 3173 | unsigned NumElems = Op.getNumOperands(); |
| 3174 | unsigned NumZero = 0; |
| 3175 | unsigned NumNonZero = 0; |
| 3176 | unsigned NonZeros = 0; |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3177 | bool IsAllConstants = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3178 | SmallSet<SDValue, 8> Values; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3179 | for (unsigned i = 0; i < NumElems; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3180 | SDValue Elt = Op.getOperand(i); |
Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 3181 | if (Elt.getOpcode() == ISD::UNDEF) |
| 3182 | continue; |
| 3183 | Values.insert(Elt); |
| 3184 | if (Elt.getOpcode() != ISD::Constant && |
| 3185 | Elt.getOpcode() != ISD::ConstantFP) |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3186 | IsAllConstants = false; |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 3187 | if (X86::isZeroNode(Elt)) |
Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 3188 | NumZero++; |
| 3189 | else { |
| 3190 | NonZeros |= (1 << i); |
| 3191 | NumNonZero++; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3195 | if (NumNonZero == 0) { |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 3196 | // All undef vector. Return an UNDEF. All zero vectors were handled above. |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3197 | return DAG.getUNDEF(VT); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3198 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3199 | |
Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 3200 | // Special case for single non-zero, non-undef, element. |
Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 3201 | if (NumNonZero == 1) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3202 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3203 | SDValue Item = Op.getOperand(Idx); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3205 | // If this is an insertion of an i64 value on x86-32, and if the top bits of |
| 3206 | // the value are obviously zero, truncate the value to i32 and do the |
| 3207 | // insertion that way. Only do this if the value is non-constant or if the |
| 3208 | // value is a constant being inserted into element 0. It is cheaper to do |
| 3209 | // a constant pool load than it is to do a movd + shuffle. |
| 3210 | if (EVT == MVT::i64 && !Subtarget->is64Bit() && |
| 3211 | (!IsAllConstants || Idx == 0)) { |
| 3212 | if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) { |
| 3213 | // Handle MMX and SSE both. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3214 | MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32; |
| 3215 | unsigned VecElts = VT == MVT::v2i64 ? 4 : 2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3216 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3217 | // Truncate the value (which may itself be a constant) to i32, and |
| 3218 | // convert it to a vector with movd (S2V+shuffle to zero extend). |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3219 | Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item); |
| 3220 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3221 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, |
| 3222 | Subtarget->hasSSE2(), DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3223 | |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3224 | // Now we have our 32-bit value zero extended in the low element of |
| 3225 | // a vector. If Idx != 0, swizzle it into place. |
| 3226 | if (Idx != 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3227 | SmallVector<int, 4> Mask; |
| 3228 | Mask.push_back(Idx); |
| 3229 | for (unsigned i = 1; i != VecElts; ++i) |
| 3230 | Mask.push_back(i); |
| 3231 | Item = DAG.getVectorShuffle(VecVT, dl, Item, |
| 3232 | DAG.getUNDEF(Item.getValueType()), |
| 3233 | &Mask[0]); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3234 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3235 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item); |
Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3238 | |
Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 3239 | // If we have a constant or non-constant insertion into the low element of |
| 3240 | // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into |
| 3241 | // the rest of the elements. This will be matched as movd/movq/movss/movsd |
Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 3242 | // depending on what the source datatype is. |
| 3243 | if (Idx == 0) { |
| 3244 | if (NumZero == 0) { |
| 3245 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
| 3246 | } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 || |
| 3247 | (EVT == MVT::i64 && Subtarget->is64Bit())) { |
| 3248 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
| 3249 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| 3250 | return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(), |
| 3251 | DAG); |
| 3252 | } else if (EVT == MVT::i16 || EVT == MVT::i8) { |
| 3253 | Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item); |
| 3254 | MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32; |
| 3255 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item); |
| 3256 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, |
| 3257 | Subtarget->hasSSE2(), DAG); |
| 3258 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item); |
| 3259 | } |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3260 | } |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3261 | |
| 3262 | // Is it a vector logical left shift? |
| 3263 | if (NumElems == 2 && Idx == 1 && |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 3264 | X86::isZeroNode(Op.getOperand(0)) && |
| 3265 | !X86::isZeroNode(Op.getOperand(1))) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3266 | unsigned NumBits = VT.getSizeInBits(); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3267 | return getVShift(true, VT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3268 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 3269 | VT, Op.getOperand(1)), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3270 | NumBits/2, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 3271 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3272 | |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3273 | if (IsAllConstants) // Otherwise, it's better to do a constpool load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3274 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3275 | |
Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 3276 | // Otherwise, if this is a vector with i32 or f32 elements, and the element |
| 3277 | // is a non-constant being inserted into an element other than the low one, |
| 3278 | // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka |
| 3279 | // movd/movss) to move this into the low element, then shuffle it into |
| 3280 | // place. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3281 | if (EVTBits == 32) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3282 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3283 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3284 | // 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] | 3285 | Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, |
| 3286 | Subtarget->hasSSE2(), DAG); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3287 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3288 | for (unsigned i = 0; i < NumElems; i++) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3289 | MaskVec.push_back(i == Idx ? 0 : 1); |
| 3290 | return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3291 | } |
| 3292 | } |
| 3293 | |
Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 3294 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 3295 | if (Values.size() == 1) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3296 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3297 | |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 3298 | // A vector full of immediates; various special cases are already |
| 3299 | // handled, so this is best done with a single constant-pool load. |
Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 3300 | if (IsAllConstants) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3301 | return SDValue(); |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 3302 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 3303 | // Let legalizer expand 2-wide build_vectors. |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3304 | if (EVTBits == 64) { |
| 3305 | if (NumNonZero == 1) { |
| 3306 | // One half is zero or undef. |
| 3307 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3308 | SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3309 | Op.getOperand(Idx)); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3310 | return getShuffleVectorZeroOrUndef(V2, Idx, true, |
| 3311 | Subtarget->hasSSE2(), DAG); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3312 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3313 | return SDValue(); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3314 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3315 | |
| 3316 | // 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] | 3317 | if (EVTBits == 8 && NumElems == 16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3318 | SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3319 | *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3320 | if (V.getNode()) return V; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 3323 | if (EVTBits == 16 && NumElems == 8) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3324 | SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3325 | *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3326 | if (V.getNode()) return V; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | // 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] | 3330 | SmallVector<SDValue, 8> V; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3331 | V.resize(NumElems); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3332 | if (NumElems == 4 && NumZero > 0) { |
| 3333 | for (unsigned i = 0; i < 4; ++i) { |
| 3334 | bool isZero = !(NonZeros & (1 << i)); |
| 3335 | if (isZero) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3336 | V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3337 | else |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3338 | 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] | 3339 | } |
| 3340 | |
| 3341 | for (unsigned i = 0; i < 2; ++i) { |
| 3342 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 3343 | default: break; |
| 3344 | case 0: |
| 3345 | V[i] = V[i*2]; // Must be a zero vector. |
| 3346 | break; |
| 3347 | case 1: |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3348 | 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] | 3349 | break; |
| 3350 | case 2: |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3351 | 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] | 3352 | break; |
| 3353 | case 3: |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3354 | 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] | 3355 | break; |
| 3356 | } |
| 3357 | } |
| 3358 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3359 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3360 | bool Reverse = (NonZeros & 0x3) == 2; |
| 3361 | for (unsigned i = 0; i < 2; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3362 | MaskVec.push_back(Reverse ? 1-i : i); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3363 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 3364 | for (unsigned i = 0; i < 2; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3365 | MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems); |
| 3366 | return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | if (Values.size() > 2) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3370 | // If we have SSE 4.1, Expand into a number of inserts unless the number of |
| 3371 | // values to be inserted is equal to the number of elements, in which case |
| 3372 | // use the unpack code below in the hopes of matching the consecutive elts |
| 3373 | // load merge pattern for shuffles. |
| 3374 | // FIXME: We could probably just check that here directly. |
| 3375 | if (Values.size() < NumElems && VT.getSizeInBits() == 128 && |
| 3376 | getSubtarget()->hasSSE41()) { |
| 3377 | V[0] = DAG.getUNDEF(VT); |
| 3378 | for (unsigned i = 0; i < NumElems; ++i) |
| 3379 | if (Op.getOperand(i).getOpcode() != ISD::UNDEF) |
| 3380 | V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0], |
| 3381 | Op.getOperand(i), DAG.getIntPtrConstant(i)); |
| 3382 | return V[0]; |
| 3383 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3384 | // Expand into a number of unpckl*. |
| 3385 | // e.g. for v4f32 |
| 3386 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 3387 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 3388 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3389 | for (unsigned i = 0; i < NumElems; ++i) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3390 | 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] | 3391 | NumElems >>= 1; |
| 3392 | while (NumElems != 0) { |
| 3393 | for (unsigned i = 0; i < NumElems; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3394 | V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3395 | NumElems >>= 1; |
| 3396 | } |
| 3397 | return V[0]; |
| 3398 | } |
| 3399 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3400 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3403 | // v8i16 shuffles - Prefer shuffles in the following order: |
| 3404 | // 1. [all] pshuflw, pshufhw, optional move |
| 3405 | // 2. [ssse3] 1 x pshufb |
| 3406 | // 3. [ssse3] 2 x pshufb + 1 x por |
| 3407 | // 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw) |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3408 | static |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3409 | SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp, |
| 3410 | SelectionDAG &DAG, X86TargetLowering &TLI) { |
| 3411 | SDValue V1 = SVOp->getOperand(0); |
| 3412 | SDValue V2 = SVOp->getOperand(1); |
| 3413 | DebugLoc dl = SVOp->getDebugLoc(); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3414 | SmallVector<int, 8> MaskVals; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3415 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3416 | // Determine if more than 1 of the words in each of the low and high quadwords |
| 3417 | // of the result come from the same quadword of one of the two inputs. Undef |
| 3418 | // mask values count as coming from any quadword, for better codegen. |
| 3419 | SmallVector<unsigned, 4> LoQuad(4); |
| 3420 | SmallVector<unsigned, 4> HiQuad(4); |
| 3421 | BitVector InputQuads(4); |
| 3422 | for (unsigned i = 0; i < 8; ++i) { |
| 3423 | SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3424 | int EltIdx = SVOp->getMaskElt(i); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3425 | MaskVals.push_back(EltIdx); |
| 3426 | if (EltIdx < 0) { |
| 3427 | ++Quad[0]; |
| 3428 | ++Quad[1]; |
| 3429 | ++Quad[2]; |
| 3430 | ++Quad[3]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3431 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3432 | } |
| 3433 | ++Quad[EltIdx / 4]; |
| 3434 | InputQuads.set(EltIdx / 4); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3435 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3436 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3437 | int BestLoQuad = -1; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3438 | unsigned MaxQuad = 1; |
| 3439 | for (unsigned i = 0; i < 4; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3440 | if (LoQuad[i] > MaxQuad) { |
| 3441 | BestLoQuad = i; |
| 3442 | MaxQuad = LoQuad[i]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3443 | } |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3446 | int BestHiQuad = -1; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3447 | MaxQuad = 1; |
| 3448 | for (unsigned i = 0; i < 4; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3449 | if (HiQuad[i] > MaxQuad) { |
| 3450 | BestHiQuad = i; |
| 3451 | MaxQuad = HiQuad[i]; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3455 | // For SSSE3, If all 8 words of the result come from only 1 quadword of each |
| 3456 | // of the two input vectors, shuffle them into one input vector so only a |
| 3457 | // single pshufb instruction is necessary. If There are more than 2 input |
| 3458 | // quads, disable the next transformation since it does not help SSSE3. |
| 3459 | bool V1Used = InputQuads[0] || InputQuads[1]; |
| 3460 | bool V2Used = InputQuads[2] || InputQuads[3]; |
| 3461 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3462 | if (InputQuads.count() == 2 && V1Used && V2Used) { |
| 3463 | BestLoQuad = InputQuads.find_first(); |
| 3464 | BestHiQuad = InputQuads.find_next(BestLoQuad); |
| 3465 | } |
| 3466 | if (InputQuads.count() > 2) { |
| 3467 | BestLoQuad = -1; |
| 3468 | BestHiQuad = -1; |
| 3469 | } |
| 3470 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3471 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3472 | // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update |
| 3473 | // the shuffle mask. If a quad is scored as -1, that means that it contains |
| 3474 | // words from all 4 input quadwords. |
| 3475 | SDValue NewV; |
| 3476 | if (BestLoQuad >= 0 || BestHiQuad >= 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3477 | SmallVector<int, 8> MaskV; |
| 3478 | MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad); |
| 3479 | MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad); |
| 3480 | NewV = DAG.getVectorShuffle(MVT::v2i64, dl, |
| 3481 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1), |
| 3482 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3483 | NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3484 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3485 | // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the |
| 3486 | // source words for the shuffle, to aid later transformations. |
| 3487 | bool AllWordsInNewV = true; |
Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 3488 | bool InOrder[2] = { true, true }; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3489 | for (unsigned i = 0; i != 8; ++i) { |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3490 | int idx = MaskVals[i]; |
Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 3491 | if (idx != (int)i) |
| 3492 | InOrder[i/4] = false; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3493 | if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3494 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3495 | AllWordsInNewV = false; |
| 3496 | break; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3497 | } |
Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 3498 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3499 | bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV; |
| 3500 | if (AllWordsInNewV) { |
| 3501 | for (int i = 0; i != 8; ++i) { |
| 3502 | int idx = MaskVals[i]; |
| 3503 | if (idx < 0) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3504 | continue; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3505 | idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4; |
| 3506 | if ((idx != i) && idx < 4) |
| 3507 | pshufhw = false; |
| 3508 | if ((idx != i) && idx > 3) |
| 3509 | pshuflw = false; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3510 | } |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3511 | V1 = NewV; |
| 3512 | V2Used = false; |
| 3513 | BestLoQuad = 0; |
| 3514 | BestHiQuad = 1; |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3515 | } |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3516 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3517 | // If we've eliminated the use of V2, and the new mask is a pshuflw or |
| 3518 | // 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] | 3519 | if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3520 | return DAG.getVectorShuffle(MVT::v8i16, dl, NewV, |
| 3521 | DAG.getUNDEF(MVT::v8i16), &MaskVals[0]); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3522 | } |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3523 | } |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3524 | |
| 3525 | // If we have SSSE3, and all words of the result are from 1 input vector, |
| 3526 | // case 2 is generated, otherwise case 3 is generated. If no SSSE3 |
| 3527 | // is present, fall back to case 4. |
| 3528 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3529 | SmallVector<SDValue,16> pshufbMask; |
| 3530 | |
| 3531 | // If we have elements from both input vectors, set the high bit of the |
| 3532 | // shuffle mask element to zero out elements that come from V2 in the V1 |
| 3533 | // mask, and elements that come from V1 in the V2 mask, so that the two |
| 3534 | // results can be OR'd together. |
| 3535 | bool TwoInputs = V1Used && V2Used; |
| 3536 | for (unsigned i = 0; i != 8; ++i) { |
| 3537 | int EltIdx = MaskVals[i] * 2; |
| 3538 | if (TwoInputs && (EltIdx >= 16)) { |
| 3539 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3540 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3541 | continue; |
| 3542 | } |
| 3543 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| 3544 | pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8)); |
| 3545 | } |
| 3546 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1); |
| 3547 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3548 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3549 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3550 | if (!TwoInputs) |
| 3551 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3552 | |
| 3553 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 3554 | // OR it with the first shuffled input. |
| 3555 | pshufbMask.clear(); |
| 3556 | for (unsigned i = 0; i != 8; ++i) { |
| 3557 | int EltIdx = MaskVals[i] * 2; |
| 3558 | if (EltIdx < 16) { |
| 3559 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3560 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3561 | continue; |
| 3562 | } |
| 3563 | pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); |
| 3564 | pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8)); |
| 3565 | } |
| 3566 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2); |
| 3567 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3568 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3569 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3570 | V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| 3571 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3572 | } |
| 3573 | |
| 3574 | // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order, |
| 3575 | // and update MaskVals with new element order. |
| 3576 | BitVector InOrder(8); |
| 3577 | if (BestLoQuad >= 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3578 | SmallVector<int, 8> MaskV; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3579 | for (int i = 0; i != 4; ++i) { |
| 3580 | int idx = MaskVals[i]; |
| 3581 | if (idx < 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3582 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3583 | InOrder.set(i); |
| 3584 | } else if ((idx / 4) == BestLoQuad) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3585 | MaskV.push_back(idx & 3); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3586 | InOrder.set(i); |
| 3587 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3588 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | for (unsigned i = 4; i != 8; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3592 | MaskV.push_back(i); |
| 3593 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| 3594 | &MaskV[0]); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | // If BestHi >= 0, generate a pshufhw to put the high elements in order, |
| 3598 | // and update MaskVals with the new element order. |
| 3599 | if (BestHiQuad >= 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3600 | SmallVector<int, 8> MaskV; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3601 | for (unsigned i = 0; i != 4; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3602 | MaskV.push_back(i); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3603 | for (unsigned i = 4; i != 8; ++i) { |
| 3604 | int idx = MaskVals[i]; |
| 3605 | if (idx < 0) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3606 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3607 | InOrder.set(i); |
| 3608 | } else if ((idx / 4) == BestHiQuad) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3609 | MaskV.push_back((idx & 3) + 4); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3610 | InOrder.set(i); |
| 3611 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3612 | MaskV.push_back(-1); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3613 | } |
| 3614 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3615 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| 3616 | &MaskV[0]); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | // In case BestHi & BestLo were both -1, which means each quadword has a word |
| 3620 | // from each of the four input quadwords, calculate the InOrder bitvector now |
| 3621 | // before falling through to the insert/extract cleanup. |
| 3622 | if (BestLoQuad == -1 && BestHiQuad == -1) { |
| 3623 | NewV = V1; |
| 3624 | for (int i = 0; i != 8; ++i) |
| 3625 | if (MaskVals[i] < 0 || MaskVals[i] == i) |
| 3626 | InOrder.set(i); |
| 3627 | } |
| 3628 | |
| 3629 | // The other elements are put in the right place using pextrw and pinsrw. |
| 3630 | for (unsigned i = 0; i != 8; ++i) { |
| 3631 | if (InOrder[i]) |
| 3632 | continue; |
| 3633 | int EltIdx = MaskVals[i]; |
| 3634 | if (EltIdx < 0) |
| 3635 | continue; |
| 3636 | SDValue ExtOp = (EltIdx < 8) |
| 3637 | ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1, |
| 3638 | DAG.getIntPtrConstant(EltIdx)) |
| 3639 | : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2, |
| 3640 | DAG.getIntPtrConstant(EltIdx - 8)); |
| 3641 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp, |
| 3642 | DAG.getIntPtrConstant(i)); |
| 3643 | } |
| 3644 | return NewV; |
| 3645 | } |
| 3646 | |
| 3647 | // v16i8 shuffles - Prefer shuffles in the following order: |
| 3648 | // 1. [ssse3] 1 x pshufb |
| 3649 | // 2. [ssse3] 2 x pshufb + 1 x por |
| 3650 | // 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw |
| 3651 | static |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3652 | SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp, |
| 3653 | SelectionDAG &DAG, X86TargetLowering &TLI) { |
| 3654 | SDValue V1 = SVOp->getOperand(0); |
| 3655 | SDValue V2 = SVOp->getOperand(1); |
| 3656 | DebugLoc dl = SVOp->getDebugLoc(); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3657 | SmallVector<int, 16> MaskVals; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3658 | SVOp->getMask(MaskVals); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3659 | |
| 3660 | // If we have SSSE3, case 1 is generated when all result bytes come from |
| 3661 | // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is |
| 3662 | // present, fall back to case 3. |
| 3663 | // FIXME: kill V2Only once shuffles are canonizalized by getNode. |
| 3664 | bool V1Only = true; |
| 3665 | bool V2Only = true; |
| 3666 | for (unsigned i = 0; i < 16; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3667 | int EltIdx = MaskVals[i]; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3668 | if (EltIdx < 0) |
| 3669 | continue; |
| 3670 | if (EltIdx < 16) |
| 3671 | V2Only = false; |
| 3672 | else |
| 3673 | V1Only = false; |
| 3674 | } |
| 3675 | |
| 3676 | // If SSSE3, use 1 pshufb instruction per vector with elements in the result. |
| 3677 | if (TLI.getSubtarget()->hasSSSE3()) { |
| 3678 | SmallVector<SDValue,16> pshufbMask; |
| 3679 | |
| 3680 | // If all result elements are from one input vector, then only translate |
| 3681 | // undef mask values to 0x80 (zero out result) in the pshufb mask. |
| 3682 | // |
| 3683 | // Otherwise, we have elements from both input vectors, and must zero out |
| 3684 | // elements that come from V2 in the first mask, and V1 in the second mask |
| 3685 | // so that we can OR them together. |
| 3686 | bool TwoInputs = !(V1Only || V2Only); |
| 3687 | for (unsigned i = 0; i != 16; ++i) { |
| 3688 | int EltIdx = MaskVals[i]; |
| 3689 | if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) { |
| 3690 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3691 | continue; |
| 3692 | } |
| 3693 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| 3694 | } |
| 3695 | // If all the elements are from V2, assign it to V1 and return after |
| 3696 | // building the first pshufb. |
| 3697 | if (V2Only) |
| 3698 | V1 = V2; |
| 3699 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3700 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3701 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3702 | if (!TwoInputs) |
| 3703 | return V1; |
| 3704 | |
| 3705 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 3706 | // OR it with the first shuffled input. |
| 3707 | pshufbMask.clear(); |
| 3708 | for (unsigned i = 0; i != 16; ++i) { |
| 3709 | int EltIdx = MaskVals[i]; |
| 3710 | if (EltIdx < 16) { |
| 3711 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 3712 | continue; |
| 3713 | } |
| 3714 | pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8)); |
| 3715 | } |
| 3716 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3717 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 3718 | MVT::v16i8, &pshufbMask[0], 16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3719 | return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| 3720 | } |
| 3721 | |
| 3722 | // No SSSE3 - Calculate in place words and then fix all out of place words |
| 3723 | // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from |
| 3724 | // the 16 different words that comprise the two doublequadword input vectors. |
| 3725 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1); |
| 3726 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2); |
| 3727 | SDValue NewV = V2Only ? V2 : V1; |
| 3728 | for (int i = 0; i != 8; ++i) { |
| 3729 | int Elt0 = MaskVals[i*2]; |
| 3730 | int Elt1 = MaskVals[i*2+1]; |
| 3731 | |
| 3732 | // This word of the result is all undef, skip it. |
| 3733 | if (Elt0 < 0 && Elt1 < 0) |
| 3734 | continue; |
| 3735 | |
| 3736 | // This word of the result is already in the correct place, skip it. |
| 3737 | if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1)) |
| 3738 | continue; |
| 3739 | if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17)) |
| 3740 | continue; |
| 3741 | |
| 3742 | SDValue Elt0Src = Elt0 < 16 ? V1 : V2; |
| 3743 | SDValue Elt1Src = Elt1 < 16 ? V1 : V2; |
| 3744 | SDValue InsElt; |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3745 | |
| 3746 | // If Elt0 and Elt1 are defined, are consecutive, and can be load |
| 3747 | // using a single extract together, load it and store it. |
| 3748 | if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) { |
| 3749 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| 3750 | DAG.getIntPtrConstant(Elt1 / 2)); |
| 3751 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| 3752 | DAG.getIntPtrConstant(i)); |
| 3753 | continue; |
| 3754 | } |
| 3755 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3756 | // 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] | 3757 | // source byte is not also odd, shift the extracted word left 8 bits |
| 3758 | // 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] | 3759 | if (Elt1 >= 0) { |
| 3760 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| 3761 | DAG.getIntPtrConstant(Elt1 / 2)); |
| 3762 | if ((Elt1 & 1) == 0) |
| 3763 | InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt, |
| 3764 | DAG.getConstant(8, TLI.getShiftAmountTy())); |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3765 | else if (Elt0 >= 0) |
| 3766 | InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt, |
| 3767 | DAG.getConstant(0xFF00, MVT::i16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3768 | } |
| 3769 | // If Elt0 is defined, extract it from the appropriate source. If the |
| 3770 | // source byte is not also even, shift the extracted word right 8 bits. If |
| 3771 | // Elt1 was also defined, OR the extracted values together before |
| 3772 | // inserting them in the result. |
| 3773 | if (Elt0 >= 0) { |
| 3774 | SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, |
| 3775 | Elt0Src, DAG.getIntPtrConstant(Elt0 / 2)); |
| 3776 | if ((Elt0 & 1) != 0) |
| 3777 | InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0, |
| 3778 | DAG.getConstant(8, TLI.getShiftAmountTy())); |
Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 3779 | else if (Elt1 >= 0) |
| 3780 | InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0, |
| 3781 | DAG.getConstant(0x00FF, MVT::i16)); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 3782 | InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0) |
| 3783 | : InsElt0; |
| 3784 | } |
| 3785 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| 3786 | DAG.getIntPtrConstant(i)); |
| 3787 | } |
| 3788 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3791 | /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide |
| 3792 | /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be |
| 3793 | /// done when every pair / quad of shuffle mask elements point to elements in |
| 3794 | /// the right sequence. e.g. |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3795 | /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15> |
| 3796 | static |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3797 | SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp, |
| 3798 | SelectionDAG &DAG, |
| 3799 | TargetLowering &TLI, DebugLoc dl) { |
| 3800 | MVT VT = SVOp->getValueType(0); |
| 3801 | SDValue V1 = SVOp->getOperand(0); |
| 3802 | SDValue V2 = SVOp->getOperand(1); |
| 3803 | unsigned NumElems = VT.getVectorNumElements(); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3804 | unsigned NewWidth = (NumElems == 4) ? 2 : 4; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3805 | MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3806 | MVT MaskEltVT = MaskVT.getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3807 | MVT NewVT = MaskVT; |
| 3808 | switch (VT.getSimpleVT()) { |
| 3809 | default: assert(false && "Unexpected!"); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3810 | case MVT::v4f32: NewVT = MVT::v2f64; break; |
| 3811 | case MVT::v4i32: NewVT = MVT::v2i64; break; |
| 3812 | case MVT::v8i16: NewVT = MVT::v4i32; break; |
| 3813 | case MVT::v16i8: NewVT = MVT::v4i32; break; |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 3816 | if (NewWidth == 2) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3817 | if (VT.isInteger()) |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 3818 | NewVT = MVT::v2i64; |
| 3819 | else |
| 3820 | NewVT = MVT::v2f64; |
Anton Korobeynikov | 7c1c261 | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 3821 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3822 | int Scale = NumElems / NewWidth; |
| 3823 | SmallVector<int, 8> MaskVec; |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3824 | for (unsigned i = 0; i < NumElems; i += Scale) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3825 | int StartIdx = -1; |
| 3826 | for (int j = 0; j < Scale; ++j) { |
| 3827 | int EltIdx = SVOp->getMaskElt(i+j); |
| 3828 | if (EltIdx < 0) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3829 | continue; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3830 | if (StartIdx == -1) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3831 | StartIdx = EltIdx - (EltIdx % Scale); |
| 3832 | if (EltIdx != StartIdx + j) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3833 | return SDValue(); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3834 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3835 | if (StartIdx == -1) |
| 3836 | MaskVec.push_back(-1); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 3837 | else |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3838 | MaskVec.push_back(StartIdx / Scale); |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3841 | V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1); |
| 3842 | V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3843 | return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]); |
Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 3846 | /// getVZextMovL - Return a zero-extending vector move low node. |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3847 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3848 | static SDValue getVZextMovL(MVT VT, MVT OpVT, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3849 | SDValue SrcOp, SelectionDAG &DAG, |
| 3850 | const X86Subtarget *Subtarget, DebugLoc dl) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3851 | if (VT == MVT::v2f64 || VT == MVT::v4f32) { |
| 3852 | LoadSDNode *LD = NULL; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3853 | if (!isScalarLoadToVector(SrcOp.getNode(), &LD)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3854 | LD = dyn_cast<LoadSDNode>(SrcOp); |
| 3855 | if (!LD) { |
| 3856 | // movssrr and movsdrr do not clear top bits. Try to use movd, movq |
| 3857 | // instead. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3858 | MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3859 | if ((EVT != MVT::i64 || Subtarget->is64Bit()) && |
| 3860 | SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 3861 | SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT && |
| 3862 | SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) { |
| 3863 | // PR2108 |
| 3864 | OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3865 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3866 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
| 3867 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| 3868 | OpVT, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 3869 | SrcOp.getOperand(0) |
| 3870 | .getOperand(0)))); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3875 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 3876 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3877 | DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 3878 | OpVT, SrcOp))); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3881 | /// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of |
| 3882 | /// shuffles. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3883 | static SDValue |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3884 | LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| 3885 | SDValue V1 = SVOp->getOperand(0); |
| 3886 | SDValue V2 = SVOp->getOperand(1); |
| 3887 | DebugLoc dl = SVOp->getDebugLoc(); |
| 3888 | MVT VT = SVOp->getValueType(0); |
| 3889 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3890 | SmallVector<std::pair<int, int>, 8> Locs; |
Rafael Espindola | 833a990 | 2008-08-28 18:32:53 +0000 | [diff] [blame] | 3891 | Locs.resize(4); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3892 | SmallVector<int, 8> Mask1(4U, -1); |
| 3893 | SmallVector<int, 8> PermMask; |
| 3894 | SVOp->getMask(PermMask); |
| 3895 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3896 | unsigned NumHi = 0; |
| 3897 | unsigned NumLo = 0; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3898 | for (unsigned i = 0; i != 4; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3899 | int Idx = PermMask[i]; |
| 3900 | if (Idx < 0) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3901 | Locs[i] = std::make_pair(-1, -1); |
| 3902 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3903 | assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!"); |
| 3904 | if (Idx < 4) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3905 | Locs[i] = std::make_pair(0, NumLo); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3906 | Mask1[NumLo] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3907 | NumLo++; |
| 3908 | } else { |
| 3909 | Locs[i] = std::make_pair(1, NumHi); |
| 3910 | if (2+NumHi < 4) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3911 | Mask1[2+NumHi] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3912 | NumHi++; |
| 3913 | } |
| 3914 | } |
| 3915 | } |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3916 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3917 | if (NumLo <= 2 && NumHi <= 2) { |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3918 | // If no more than two elements come from either vector. This can be |
| 3919 | // implemented with two shuffles. First shuffle gather the elements. |
| 3920 | // The second shuffle, which takes the first shuffle as both of its |
| 3921 | // vector operands, put the elements into the right order. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3922 | V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3923 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3924 | SmallVector<int, 8> Mask2(4U, -1); |
| 3925 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3926 | for (unsigned i = 0; i != 4; ++i) { |
| 3927 | if (Locs[i].first == -1) |
| 3928 | continue; |
| 3929 | else { |
| 3930 | unsigned Idx = (i < 2) ? 0 : 4; |
| 3931 | Idx += Locs[i].first * 2 + Locs[i].second; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3932 | Mask2[i] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3933 | } |
| 3934 | } |
| 3935 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3936 | return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3937 | } else if (NumLo == 3 || NumHi == 3) { |
| 3938 | // Otherwise, we must have three elements from one vector, call it X, and |
| 3939 | // one element from the other, call it Y. First, use a shufps to build an |
| 3940 | // intermediate vector with the one element from Y and the element from X |
| 3941 | // that will be in the same half in the final destination (the indexes don't |
| 3942 | // matter). Then, use a shufps to build the final vector, taking the half |
| 3943 | // containing the element from Y from the intermediate, and the other half |
| 3944 | // from X. |
| 3945 | if (NumHi == 3) { |
| 3946 | // Normalize it so the 3 elements come from V1. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3947 | CommuteVectorShuffleMask(PermMask, VT); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3948 | std::swap(V1, V2); |
| 3949 | } |
| 3950 | |
| 3951 | // Find the element from V2. |
| 3952 | unsigned HiIndex; |
| 3953 | for (HiIndex = 0; HiIndex < 3; ++HiIndex) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3954 | int Val = PermMask[HiIndex]; |
| 3955 | if (Val < 0) |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3956 | continue; |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3957 | if (Val >= 4) |
| 3958 | break; |
| 3959 | } |
| 3960 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3961 | Mask1[0] = PermMask[HiIndex]; |
| 3962 | Mask1[1] = -1; |
| 3963 | Mask1[2] = PermMask[HiIndex^1]; |
| 3964 | Mask1[3] = -1; |
| 3965 | V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3966 | |
| 3967 | if (HiIndex >= 2) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3968 | Mask1[0] = PermMask[0]; |
| 3969 | Mask1[1] = PermMask[1]; |
| 3970 | Mask1[2] = HiIndex & 1 ? 6 : 4; |
| 3971 | Mask1[3] = HiIndex & 1 ? 4 : 6; |
| 3972 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3973 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3974 | Mask1[0] = HiIndex & 1 ? 2 : 0; |
| 3975 | Mask1[1] = HiIndex & 1 ? 0 : 2; |
| 3976 | Mask1[2] = PermMask[2]; |
| 3977 | Mask1[3] = PermMask[3]; |
| 3978 | if (Mask1[2] >= 0) |
| 3979 | Mask1[2] += 4; |
| 3980 | if (Mask1[3] >= 0) |
| 3981 | Mask1[3] += 4; |
| 3982 | return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]); |
Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 3983 | } |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 3987 | Locs.clear(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3988 | SmallVector<int,8> LoMask(4U, -1); |
| 3989 | SmallVector<int,8> HiMask(4U, -1); |
| 3990 | |
| 3991 | SmallVector<int,8> *MaskPtr = &LoMask; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 3992 | unsigned MaskIdx = 0; |
| 3993 | unsigned LoIdx = 0; |
| 3994 | unsigned HiIdx = 2; |
| 3995 | for (unsigned i = 0; i != 4; ++i) { |
| 3996 | if (i == 2) { |
| 3997 | MaskPtr = &HiMask; |
| 3998 | MaskIdx = 1; |
| 3999 | LoIdx = 0; |
| 4000 | HiIdx = 2; |
| 4001 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4002 | int Idx = PermMask[i]; |
| 4003 | if (Idx < 0) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4004 | Locs[i] = std::make_pair(-1, -1); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4005 | } else if (Idx < 4) { |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4006 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4007 | (*MaskPtr)[LoIdx] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4008 | LoIdx++; |
| 4009 | } else { |
| 4010 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4011 | (*MaskPtr)[HiIdx] = Idx; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4012 | HiIdx++; |
| 4013 | } |
| 4014 | } |
| 4015 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4016 | SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]); |
| 4017 | SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]); |
| 4018 | SmallVector<int, 8> MaskOps; |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4019 | for (unsigned i = 0; i != 4; ++i) { |
| 4020 | if (Locs[i].first == -1) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4021 | MaskOps.push_back(-1); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4022 | } else { |
| 4023 | unsigned Idx = Locs[i].first * 4 + Locs[i].second; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4024 | MaskOps.push_back(Idx); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4025 | } |
| 4026 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4027 | return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]); |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4030 | SDValue |
| 4031 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4032 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4033 | SDValue V1 = Op.getOperand(0); |
| 4034 | SDValue V2 = Op.getOperand(1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4035 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4036 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4037 | unsigned NumElems = VT.getVectorNumElements(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4038 | bool isMMX = VT.getSizeInBits() == 64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4039 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 4040 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 4041 | bool V1IsSplat = false; |
| 4042 | bool V2IsSplat = false; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4043 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4044 | if (isZeroShuffle(SVOp)) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4045 | return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 4046 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4047 | // Promote splats to v4f32. |
| 4048 | if (SVOp->isSplat()) { |
| 4049 | if (isMMX || NumElems < 4) |
| 4050 | return Op; |
| 4051 | return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 4054 | // If the shuffle can be profitably rewritten as a narrower shuffle, then |
| 4055 | // do it! |
| 4056 | if (VT == MVT::v8i16 || VT == MVT::v16i8) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4057 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4058 | if (NewOp.getNode()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4059 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4060 | LowerVECTOR_SHUFFLE(NewOp, DAG)); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 4061 | } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) { |
| 4062 | // FIXME: Figure out a cleaner way to do this. |
| 4063 | // Try to make use of movq to zero out the top part. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4064 | if (ISD::isBuildVectorAllZeros(V2.getNode())) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4065 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4066 | if (NewOp.getNode()) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4067 | if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false)) |
| 4068 | return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0), |
| 4069 | DAG, Subtarget, dl); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 4070 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4071 | } else if (ISD::isBuildVectorAllZeros(V1.getNode())) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4072 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl); |
| 4073 | if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp))) |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 4074 | return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1), |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4075 | DAG, Subtarget, dl); |
Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 4076 | } |
| 4077 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4078 | |
| 4079 | if (X86::isPSHUFDMask(SVOp)) |
| 4080 | return Op; |
| 4081 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4082 | // Check if this can be converted into a logical shift. |
| 4083 | bool isLeft = false; |
| 4084 | unsigned ShAmt = 0; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4085 | SDValue ShVal; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4086 | bool isShift = getSubtarget()->hasSSE2() && |
| 4087 | isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4088 | if (isShift && ShVal.hasOneUse()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4089 | // 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] | 4090 | // v_set0 + movlhps or movhlps, etc. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4091 | MVT EVT = VT.getVectorElementType(); |
| 4092 | ShAmt *= EVT.getSizeInBits(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4093 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4094 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4095 | |
| 4096 | if (X86::isMOVLMask(SVOp)) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 4097 | if (V1IsUndef) |
| 4098 | return V2; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4099 | if (ISD::isBuildVectorAllZeros(V1.getNode())) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4100 | return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 4101 | if (!isMMX) |
| 4102 | return Op; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 4103 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4104 | |
| 4105 | // FIXME: fold these into legal mask. |
| 4106 | if (!isMMX && (X86::isMOVSHDUPMask(SVOp) || |
| 4107 | X86::isMOVSLDUPMask(SVOp) || |
| 4108 | X86::isMOVHLPSMask(SVOp) || |
| 4109 | X86::isMOVHPMask(SVOp) || |
| 4110 | X86::isMOVLPMask(SVOp))) |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4111 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4112 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4113 | if (ShouldXformToMOVHLPS(SVOp) || |
| 4114 | ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp)) |
| 4115 | return CommuteVectorShuffle(SVOp, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4116 | |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4117 | if (isShift) { |
| 4118 | // No better options. Use a vshl / vsrl. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4119 | MVT EVT = VT.getVectorElementType(); |
| 4120 | ShAmt *= EVT.getSizeInBits(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4121 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4122 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4123 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4124 | bool Commuted = false; |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4125 | // FIXME: This should also accept a bitcast of a splat? Be careful, not |
| 4126 | // 1,1,1,1 -> v8i16 though. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4127 | V1IsSplat = isSplatVector(V1.getNode()); |
| 4128 | V2IsSplat = isSplatVector(V2.getNode()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4129 | |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4130 | // Canonicalize the splat or undef, if present, to be on the RHS. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4131 | if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4132 | Op = CommuteVectorShuffle(SVOp, DAG); |
| 4133 | SVOp = cast<ShuffleVectorSDNode>(Op); |
| 4134 | V1 = SVOp->getOperand(0); |
| 4135 | V2 = SVOp->getOperand(1); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4136 | std::swap(V1IsSplat, V2IsSplat); |
| 4137 | std::swap(V1IsUndef, V2IsUndef); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4138 | Commuted = true; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4141 | if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) { |
| 4142 | // Shuffling low element of v1 into undef, just return v1. |
| 4143 | if (V2IsUndef) |
| 4144 | return V1; |
| 4145 | // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which |
| 4146 | // the instruction selector will not match, so get a canonical MOVL with |
| 4147 | // swapped operands to undo the commute. |
| 4148 | return getMOVL(DAG, dl, VT, V2, V1); |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 4149 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4150 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4151 | if (X86::isUNPCKL_v_undef_Mask(SVOp) || |
| 4152 | X86::isUNPCKH_v_undef_Mask(SVOp) || |
| 4153 | X86::isUNPCKLMask(SVOp) || |
| 4154 | X86::isUNPCKHMask(SVOp)) |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 4155 | return Op; |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 4156 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 4157 | if (V2IsSplat) { |
| 4158 | // 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] | 4159 | // 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] | 4160 | // new vector_shuffle with the corrected mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4161 | SDValue NewMask = NormalizeMask(SVOp, DAG); |
| 4162 | ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask); |
| 4163 | if (NSVOp != SVOp) { |
| 4164 | if (X86::isUNPCKLMask(NSVOp, true)) { |
| 4165 | return NewMask; |
| 4166 | } else if (X86::isUNPCKHMask(NSVOp, true)) { |
| 4167 | return NewMask; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4168 | } |
| 4169 | } |
| 4170 | } |
| 4171 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4172 | if (Commuted) { |
| 4173 | // Commute is back and try unpck* again. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4174 | // FIXME: this seems wrong. |
| 4175 | SDValue NewOp = CommuteVectorShuffle(SVOp, DAG); |
| 4176 | ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp); |
| 4177 | if (X86::isUNPCKL_v_undef_Mask(NewSVOp) || |
| 4178 | X86::isUNPCKH_v_undef_Mask(NewSVOp) || |
| 4179 | X86::isUNPCKLMask(NewSVOp) || |
| 4180 | X86::isUNPCKHMask(NewSVOp)) |
| 4181 | return NewOp; |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 4182 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4183 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4184 | // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4185 | |
| 4186 | // Normalize the node to match x86 shuffle ops if needed |
| 4187 | if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp)) |
| 4188 | return CommuteVectorShuffle(SVOp, DAG); |
| 4189 | |
| 4190 | // Check for legal shuffle and return? |
| 4191 | SmallVector<int, 16> PermMask; |
| 4192 | SVOp->getMask(PermMask); |
| 4193 | if (isShuffleMaskLegal(PermMask, VT)) |
Evan Cheng | 0c0f83f | 2008-04-05 00:30:36 +0000 | [diff] [blame] | 4194 | return Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4195 | |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4196 | // Handle v8i16 specifically since SSE can do byte extraction and insertion. |
| 4197 | if (VT == MVT::v8i16) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4198 | SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4199 | if (NewOp.getNode()) |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4200 | return NewOp; |
| 4201 | } |
| 4202 | |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4203 | if (VT == MVT::v16i8) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4204 | SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this); |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4205 | if (NewOp.getNode()) |
| 4206 | return NewOp; |
| 4207 | } |
| 4208 | |
Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 4209 | // Handle all 4 wide cases with a number of shuffles except for MMX. |
| 4210 | if (NumElems == 4 && !isMMX) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4211 | return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4212 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4213 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4216 | SDValue |
| 4217 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4218 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4219 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4220 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4221 | if (VT.getSizeInBits() == 8) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4222 | SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4223 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4224 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4225 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4226 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4227 | } else if (VT.getSizeInBits() == 16) { |
Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 4228 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 4229 | // If Idx is 0, it's cheaper to do a move instead of a pextrw. |
| 4230 | if (Idx == 0) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4231 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 4232 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
| 4233 | DAG.getNode(ISD::BIT_CONVERT, dl, |
| 4234 | MVT::v4i32, |
Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 4235 | Op.getOperand(0)), |
| 4236 | Op.getOperand(1))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4237 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4238 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4239 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4240 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4241 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4242 | } else if (VT == MVT::f32) { |
| 4243 | // EXTRACTPS outputs to a GPR32 register which will require a movd to copy |
| 4244 | // 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] | 4245 | // result has a single use which is a store or a bitcast to i32. And in |
| 4246 | // the case of a store, it's not worth it if the index is a constant 0, |
| 4247 | // because a MOVSSmr can be used instead, which is smaller and faster. |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4248 | if (!Op.hasOneUse()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4249 | return SDValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4250 | SDNode *User = *Op.getNode()->use_begin(); |
Dan Gohman | d17cfbe | 2008-10-31 00:57:24 +0000 | [diff] [blame] | 4251 | if ((User->getOpcode() != ISD::STORE || |
| 4252 | (isa<ConstantSDNode>(Op.getOperand(1)) && |
| 4253 | cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) && |
Dan Gohman | 171c11e | 2008-04-16 02:32:24 +0000 | [diff] [blame] | 4254 | (User->getOpcode() != ISD::BIT_CONVERT || |
| 4255 | User->getValueType(0) != MVT::i32)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4256 | return SDValue(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4257 | SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4258 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4259 | Op.getOperand(0)), |
| 4260 | Op.getOperand(1)); |
| 4261 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract); |
Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 4262 | } else if (VT == MVT::i32) { |
| 4263 | // ExtractPS works with constant index. |
| 4264 | if (isa<ConstantSDNode>(Op.getOperand(1))) |
| 4265 | return Op; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4266 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4267 | return SDValue(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4271 | SDValue |
| 4272 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4273 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4274 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4275 | |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4276 | if (Subtarget->hasSSE41()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4277 | SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4278 | if (Res.getNode()) |
Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 4279 | return Res; |
| 4280 | } |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4281 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4282 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4283 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4284 | // TODO: handle v16i8. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4285 | if (VT.getSizeInBits() == 16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4286 | SDValue Vec = Op.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4287 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4288 | if (Idx == 0) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4289 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 4290 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4291 | DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4292 | MVT::v4i32, Vec), |
Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 4293 | Op.getOperand(1))); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4294 | // Transform it so it match pextrw which produces a 32-bit result. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4295 | MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4296 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4297 | Op.getOperand(0), Op.getOperand(1)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4298 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4299 | DAG.getValueType(VT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4300 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4301 | } else if (VT.getSizeInBits() == 32) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4302 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4303 | if (Idx == 0) |
| 4304 | return Op; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4305 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4306 | // SHUFPS the element to the lowest double word, then movss. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4307 | int Mask[4] = { Idx, -1, -1, -1 }; |
| 4308 | MVT VVT = Op.getOperand(0).getValueType(); |
| 4309 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| 4310 | DAG.getUNDEF(VVT), Mask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4311 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4312 | DAG.getIntPtrConstant(0)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4313 | } else if (VT.getSizeInBits() == 64) { |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4314 | // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b |
| 4315 | // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught |
| 4316 | // to match extract_elt for f64. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4317 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4318 | if (Idx == 0) |
| 4319 | return Op; |
| 4320 | |
| 4321 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 4322 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 4323 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4324 | int Mask[2] = { 1, -1 }; |
| 4325 | MVT VVT = Op.getOperand(0).getValueType(); |
| 4326 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| 4327 | DAG.getUNDEF(VVT), Mask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4328 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4329 | DAG.getIntPtrConstant(0)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4332 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4335 | SDValue |
| 4336 | X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){ |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4337 | MVT VT = Op.getValueType(); |
| 4338 | MVT EVT = VT.getVectorElementType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4339 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4340 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4341 | SDValue N0 = Op.getOperand(0); |
| 4342 | SDValue N1 = Op.getOperand(1); |
| 4343 | SDValue N2 = Op.getOperand(2); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4344 | |
Dan Gohman | ef521f1 | 2008-08-14 22:53:18 +0000 | [diff] [blame] | 4345 | if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) && |
| 4346 | isa<ConstantSDNode>(N2)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4347 | unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 4348 | : X86ISD::PINSRW; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4349 | // Transform it so it match pinsr{b,w} which expects a GR32 as its second |
| 4350 | // argument. |
| 4351 | if (N1.getValueType() != MVT::i32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4352 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4353 | if (N2.getValueType() != MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4354 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4355 | return DAG.getNode(Opc, dl, VT, N0, N1, N2); |
Dan Gohman | c0573b1 | 2008-08-14 22:43:26 +0000 | [diff] [blame] | 4356 | } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) { |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4357 | // Bits [7:6] of the constant are the source select. This will always be |
| 4358 | // zero here. The DAG Combiner may combine an extract_elt index into these |
| 4359 | // bits. For example (insert (extract, 3), 2) could be matched by putting |
| 4360 | // the '3' into bits [7:6] of X86ISD::INSERTPS. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4361 | // 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] | 4362 | // value of the incoming immediate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4363 | // 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] | 4364 | // 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] | 4365 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4); |
Eric Christopher | fbd6687 | 2009-07-24 00:33:09 +0000 | [diff] [blame] | 4366 | // Create this as a scalar to vector.. |
| 4367 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4368 | return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2); |
Eric Christopher | fbd6687 | 2009-07-24 00:33:09 +0000 | [diff] [blame] | 4369 | } else if (EVT == MVT::i32 && isa<ConstantSDNode>(N2)) { |
| 4370 | // PINSR* works with constant index. |
| 4371 | return Op; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4372 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4373 | return SDValue(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4376 | SDValue |
| 4377 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4378 | MVT VT = Op.getValueType(); |
| 4379 | MVT EVT = VT.getVectorElementType(); |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 4380 | |
| 4381 | if (Subtarget->hasSSE41()) |
| 4382 | return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG); |
| 4383 | |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4384 | if (EVT == MVT::i8) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4385 | return SDValue(); |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4386 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4387 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4388 | SDValue N0 = Op.getOperand(0); |
| 4389 | SDValue N1 = Op.getOperand(1); |
| 4390 | SDValue N2 = Op.getOperand(2); |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4391 | |
Eli Friedman | 30e71eb | 2009-06-06 06:32:50 +0000 | [diff] [blame] | 4392 | if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) { |
Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 4393 | // Transform it so it match pinsrw which expects a 16-bit value in a GR32 |
| 4394 | // as its second argument. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4395 | if (N1.getValueType() != MVT::i32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4396 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4397 | if (N2.getValueType() != MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4398 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4399 | return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4400 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4401 | return SDValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4404 | SDValue |
| 4405 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4406 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 4407 | if (Op.getValueType() == MVT::v2f32) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4408 | return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32, |
| 4409 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32, |
| 4410 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, |
Evan Cheng | 52672b8 | 2008-07-22 18:39:19 +0000 | [diff] [blame] | 4411 | Op.getOperand(0)))); |
| 4412 | |
Rafael Espindola | def390a | 2009-08-03 02:45:34 +0000 | [diff] [blame] | 4413 | if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64) |
Rafael Espindola | cc2b67a | 2009-08-03 03:00:05 +0000 | [diff] [blame] | 4414 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0)); |
Rafael Espindola | def390a | 2009-08-03 02:45:34 +0000 | [diff] [blame] | 4415 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4416 | 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] | 4417 | MVT VT = MVT::v2i32; |
| 4418 | switch (Op.getValueType().getSimpleVT()) { |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 4419 | default: break; |
| 4420 | case MVT::v16i8: |
| 4421 | case MVT::v8i16: |
| 4422 | VT = MVT::v4i32; |
| 4423 | break; |
| 4424 | } |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4425 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), |
| 4426 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 4429 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 4430 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 4431 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 4432 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 4433 | // be used to form addressing mode. These wrapped nodes will be selected |
| 4434 | // into MOV32ri. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4435 | SDValue |
| 4436 | X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4437 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 4438 | |
| 4439 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 4440 | // global base reg. |
| 4441 | unsigned char OpFlag = 0; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4442 | unsigned WrapperKind = X86ISD::Wrapper; |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4443 | |
Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 4444 | if (Subtarget->isPICStyleRIPRel() && |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4445 | getTargetMachine().getCodeModel() == CodeModel::Small) |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4446 | WrapperKind = X86ISD::WrapperRIP; |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4447 | else if (Subtarget->isPICStyleGOT()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4448 | OpFlag = X86II::MO_GOTOFF; |
Chris Lattner | e2c9208 | 2009-07-10 21:00:45 +0000 | [diff] [blame] | 4449 | else if (Subtarget->isPICStyleStubPIC()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4450 | OpFlag = X86II::MO_PIC_BASE_OFFSET; |
Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 4451 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4452 | SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(), |
Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 4453 | CP->getAlignment(), |
| 4454 | CP->getOffset(), OpFlag); |
| 4455 | DebugLoc DL = CP->getDebugLoc(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4456 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4457 | // With PIC, the address is actually $g + Offset. |
Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 4458 | if (OpFlag) { |
| 4459 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4460 | DAG.getNode(X86ISD::GlobalBaseReg, |
Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 4461 | DebugLoc::getUnknownLoc(), getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4462 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | return Result; |
| 4466 | } |
| 4467 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4468 | SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) { |
| 4469 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 4470 | |
| 4471 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 4472 | // global base reg. |
| 4473 | unsigned char OpFlag = 0; |
| 4474 | unsigned WrapperKind = X86ISD::Wrapper; |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4475 | |
Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 4476 | if (Subtarget->isPICStyleRIPRel() && |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4477 | getTargetMachine().getCodeModel() == CodeModel::Small) |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4478 | WrapperKind = X86ISD::WrapperRIP; |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4479 | else if (Subtarget->isPICStyleGOT()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4480 | OpFlag = X86II::MO_GOTOFF; |
Chris Lattner | e2c9208 | 2009-07-10 21:00:45 +0000 | [diff] [blame] | 4481 | else if (Subtarget->isPICStyleStubPIC()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4482 | OpFlag = X86II::MO_PIC_BASE_OFFSET; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4483 | |
| 4484 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(), |
| 4485 | OpFlag); |
| 4486 | DebugLoc DL = JT->getDebugLoc(); |
| 4487 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| 4488 | |
| 4489 | // With PIC, the address is actually $g + Offset. |
| 4490 | if (OpFlag) { |
| 4491 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| 4492 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 4493 | DebugLoc::getUnknownLoc(), getPointerTy()), |
| 4494 | Result); |
| 4495 | } |
| 4496 | |
| 4497 | return Result; |
| 4498 | } |
| 4499 | |
| 4500 | SDValue |
| 4501 | X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) { |
| 4502 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 4503 | |
| 4504 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 4505 | // global base reg. |
| 4506 | unsigned char OpFlag = 0; |
| 4507 | unsigned WrapperKind = X86ISD::Wrapper; |
Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 4508 | if (Subtarget->isPICStyleRIPRel() && |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4509 | getTargetMachine().getCodeModel() == CodeModel::Small) |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4510 | WrapperKind = X86ISD::WrapperRIP; |
Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 4511 | else if (Subtarget->isPICStyleGOT()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4512 | OpFlag = X86II::MO_GOTOFF; |
Chris Lattner | e2c9208 | 2009-07-10 21:00:45 +0000 | [diff] [blame] | 4513 | else if (Subtarget->isPICStyleStubPIC()) |
Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 4514 | OpFlag = X86II::MO_PIC_BASE_OFFSET; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4515 | |
| 4516 | SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag); |
| 4517 | |
| 4518 | DebugLoc DL = Op.getDebugLoc(); |
| 4519 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| 4520 | |
| 4521 | |
| 4522 | // With PIC, the address is actually $g + Offset. |
| 4523 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 4524 | !Subtarget->is64Bit()) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4525 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| 4526 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 4527 | DebugLoc::getUnknownLoc(), |
| 4528 | getPointerTy()), |
| 4529 | Result); |
| 4530 | } |
| 4531 | |
| 4532 | return Result; |
| 4533 | } |
| 4534 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4535 | SDValue |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4536 | X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4537 | int64_t Offset, |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4538 | SelectionDAG &DAG) const { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4539 | // Create the TargetGlobalAddress node, folding in the constant |
| 4540 | // offset if it is legal. |
Chris Lattner | d392bd9 | 2009-07-10 07:20:05 +0000 | [diff] [blame] | 4541 | unsigned char OpFlags = |
| 4542 | Subtarget->ClassifyGlobalReference(GV, getTargetMachine()); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4543 | SDValue Result; |
Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 4544 | if (OpFlags == X86II::MO_NO_FLAG && isInt32(Offset)) { |
Chris Lattner | 4aa21aa | 2009-07-09 00:58:53 +0000 | [diff] [blame] | 4545 | // A direct static reference to a global. |
Dale Johannesen | 60b3ba0 | 2009-07-21 00:12:29 +0000 | [diff] [blame] | 4546 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4547 | Offset = 0; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4548 | } else { |
Chris Lattner | b1acd68 | 2009-06-27 05:39:56 +0000 | [diff] [blame] | 4549 | Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 4552 | if (Subtarget->isPICStyleRIPRel() && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4553 | getTargetMachine().getCodeModel() == CodeModel::Small) |
| 4554 | Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result); |
| 4555 | else |
| 4556 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4557 | |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4558 | // With PIC, the address is actually $g + Offset. |
Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 4559 | if (isGlobalRelativeToPICBase(OpFlags)) { |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4560 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 4561 | DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()), |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 4562 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4563 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4564 | |
Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 4565 | // For globals that require a load from a stub to get the address, emit the |
| 4566 | // load. |
| 4567 | if (isGlobalStubReference(OpFlags)) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4568 | Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4569 | PseudoSourceValue::getGOT(), 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4570 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4571 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 4572 | // addition for it. |
| 4573 | if (Offset != 0) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4574 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result, |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4575 | DAG.getConstant(Offset, getPointerTy())); |
| 4576 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4577 | return Result; |
| 4578 | } |
| 4579 | |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4580 | SDValue |
| 4581 | X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) { |
| 4582 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 4583 | int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4584 | return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG); |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4587 | static SDValue |
| 4588 | GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA, |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4589 | SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg, |
| 4590 | unsigned char OperandFlags) { |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4591 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 4592 | DebugLoc dl = GA->getDebugLoc(); |
| 4593 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 4594 | GA->getValueType(0), |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4595 | GA->getOffset(), |
| 4596 | OperandFlags); |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4597 | if (InFlag) { |
| 4598 | SDValue Ops[] = { Chain, TGA, *InFlag }; |
Rafael Espindola | 15f1b66 | 2009-04-24 12:59:40 +0000 | [diff] [blame] | 4599 | Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3); |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4600 | } else { |
| 4601 | SDValue Ops[] = { Chain, TGA }; |
Rafael Espindola | 15f1b66 | 2009-04-24 12:59:40 +0000 | [diff] [blame] | 4602 | Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2); |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4603 | } |
Rafael Espindola | 15f1b66 | 2009-04-24 12:59:40 +0000 | [diff] [blame] | 4604 | SDValue Flag = Chain.getValue(1); |
| 4605 | return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag); |
Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4608 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4609 | static SDValue |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4610 | LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4611 | const MVT PtrVT) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4612 | SDValue InFlag; |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 4613 | DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better |
| 4614 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4615 | DAG.getNode(X86ISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 4616 | DebugLoc::getUnknownLoc(), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4617 | PtrVT), InFlag); |
| 4618 | InFlag = Chain.getValue(1); |
| 4619 | |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4620 | return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4623 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4624 | static SDValue |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4625 | LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4626 | const MVT PtrVT) { |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4627 | return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, |
| 4628 | X86::RAX, X86II::MO_TLSGD); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4631 | // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or |
| 4632 | // "local exec" model. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4633 | static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4634 | const MVT PtrVT, TLSModel::Model model, |
| 4635 | bool is64Bit) { |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4636 | DebugLoc dl = GA->getDebugLoc(); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4637 | // Get the Thread Pointer |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 4638 | SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress, |
| 4639 | DebugLoc::getUnknownLoc(), PtrVT, |
Rafael Espindola | 7ff5bff | 2009-04-13 13:02:49 +0000 | [diff] [blame] | 4640 | DAG.getRegister(is64Bit? X86::FS : X86::GS, |
| 4641 | MVT::i32)); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 4642 | |
| 4643 | SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base, |
| 4644 | NULL, 0); |
| 4645 | |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4646 | unsigned char OperandFlags = 0; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4647 | // Most TLS accesses are not RIP relative, even on x86-64. One exception is |
| 4648 | // initialexec. |
| 4649 | unsigned WrapperKind = X86ISD::Wrapper; |
| 4650 | if (model == TLSModel::LocalExec) { |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4651 | OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4652 | } else if (is64Bit) { |
| 4653 | assert(model == TLSModel::InitialExec); |
| 4654 | OperandFlags = X86II::MO_GOTTPOFF; |
| 4655 | WrapperKind = X86ISD::WrapperRIP; |
| 4656 | } else { |
| 4657 | assert(model == TLSModel::InitialExec); |
| 4658 | OperandFlags = X86II::MO_INDNTPOFF; |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4659 | } |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4660 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4661 | // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial |
| 4662 | // exec) |
Chris Lattner | 4150c08 | 2009-06-21 02:22:34 +0000 | [diff] [blame] | 4663 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4664 | GA->getOffset(), OperandFlags); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 4665 | SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); |
Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 4666 | |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4667 | if (model == TLSModel::InitialExec) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4668 | Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4669 | PseudoSourceValue::getGOT(), 0); |
Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 4670 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4671 | // The address of the thread local variable is the add of the thread |
| 4672 | // pointer with the offset of the variable. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4673 | return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4676 | SDValue |
| 4677 | X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) { |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4678 | // TODO: implement the "local dynamic" model |
Lauro Ramos Venancio | 2c5c111 | 2007-04-21 20:56:26 +0000 | [diff] [blame] | 4679 | // TODO: implement the "initial exec"model for pic executables |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4680 | assert(Subtarget->isTargetELF() && |
| 4681 | "TLS not implemented for non-ELF targets"); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4682 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4683 | const GlobalValue *GV = GA->getGlobal(); |
| 4684 | |
| 4685 | // If GV is an alias then use the aliasee for determining |
| 4686 | // thread-localness. |
| 4687 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
| 4688 | GV = GA->resolveAliasedGlobal(false); |
| 4689 | |
| 4690 | TLSModel::Model model = getTLSModel(GV, |
| 4691 | getTargetMachine().getRelocationModel()); |
| 4692 | |
| 4693 | switch (model) { |
| 4694 | case TLSModel::GeneralDynamic: |
| 4695 | case TLSModel::LocalDynamic: // not implemented |
| 4696 | if (Subtarget->is64Bit()) |
Rafael Espindola | 9a58023 | 2009-02-27 13:37:18 +0000 | [diff] [blame] | 4697 | return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy()); |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4698 | return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy()); |
| 4699 | |
| 4700 | case TLSModel::InitialExec: |
| 4701 | case TLSModel::LocalExec: |
| 4702 | return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, |
| 4703 | Subtarget->is64Bit()); |
Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 4704 | } |
Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 4705 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4706 | llvm_unreachable("Unreachable"); |
Chris Lattner | 5867de1 | 2009-04-01 22:14:45 +0000 | [diff] [blame] | 4707 | return SDValue(); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4710 | |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4711 | /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4712 | /// take a 2 x i32 value to shift plus a shift amount. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4713 | SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 4714 | assert(Op.getNumOperands() == 3 && "Not a double-shift!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4715 | MVT VT = Op.getValueType(); |
| 4716 | unsigned VTBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4717 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4718 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4719 | SDValue ShOpLo = Op.getOperand(0); |
| 4720 | SDValue ShOpHi = Op.getOperand(1); |
| 4721 | SDValue ShAmt = Op.getOperand(2); |
Chris Lattner | 31dcfe6 | 2009-07-29 05:48:09 +0000 | [diff] [blame] | 4722 | SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi, |
| 4723 | DAG.getConstant(VTBits - 1, MVT::i8)) |
| 4724 | : DAG.getConstant(0, VT); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4725 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4726 | SDValue Tmp2, Tmp3; |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4727 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4728 | Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt); |
| 4729 | Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4730 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4731 | Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt); |
| 4732 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4733 | } |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4734 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4735 | SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt, |
Chris Lattner | 31dcfe6 | 2009-07-29 05:48:09 +0000 | [diff] [blame] | 4736 | DAG.getConstant(VTBits, MVT::i8)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4737 | SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT, |
Chris Lattner | 31dcfe6 | 2009-07-29 05:48:09 +0000 | [diff] [blame] | 4738 | AndNode, DAG.getConstant(0, MVT::i8)); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4739 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4740 | SDValue Hi, Lo; |
| 4741 | SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| 4742 | SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond }; |
| 4743 | SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond }; |
Duncan Sands | f951620 | 2008-06-30 10:19:09 +0000 | [diff] [blame] | 4744 | |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4745 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4746 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 4747 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4748 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4749 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 4750 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4753 | SDValue Ops[2] = { Lo, Hi }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4754 | return DAG.getMergeValues(Ops, 2, dl); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4755 | } |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4756 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4757 | SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4758 | MVT SrcVT = Op.getOperand(0).getValueType(); |
Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 4759 | |
| 4760 | if (SrcVT.isVector()) { |
| 4761 | if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) { |
| 4762 | return Op; |
| 4763 | } |
| 4764 | return SDValue(); |
| 4765 | } |
| 4766 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 4767 | assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 && |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4768 | "Unknown SINT_TO_FP to lower!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4769 | |
Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 4770 | // These are really Legal; return the operand so the caller accepts it as |
| 4771 | // Legal. |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4772 | if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType())) |
Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 4773 | return Op; |
| 4774 | if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) && |
| 4775 | Subtarget->is64Bit()) { |
| 4776 | return Op; |
| 4777 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4778 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4779 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4780 | unsigned Size = SrcVT.getSizeInBits()/8; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4781 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4782 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4783 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4784 | SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 4785 | StackSlot, |
| 4786 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4787 | return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG); |
| 4788 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4789 | |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4790 | SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain, |
| 4791 | SDValue StackSlot, |
| 4792 | SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4793 | // Build the FILD |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4794 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4795 | SDVTList Tys; |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 4796 | bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType()); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4797 | if (useSSE) |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4798 | Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); |
| 4799 | else |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 4800 | Tys = DAG.getVTList(Op.getValueType(), MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4801 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4802 | Ops.push_back(Chain); |
| 4803 | Ops.push_back(StackSlot); |
| 4804 | Ops.push_back(DAG.getValueType(SrcVT)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4805 | SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl, |
Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 4806 | Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4807 | |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4808 | if (useSSE) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4809 | Chain = Result.getValue(1); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4810 | SDValue InFlag = Result.getValue(2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4811 | |
| 4812 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 4813 | // shouldn't be necessary except that RFP cannot be live across |
| 4814 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4815 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4816 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4817 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4818 | Tys = DAG.getVTList(MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4819 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4820 | Ops.push_back(Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4821 | Ops.push_back(Result); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4822 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4823 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 4824 | Ops.push_back(InFlag); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4825 | Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size()); |
| 4826 | Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 4827 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4828 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4829 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4830 | return Result; |
| 4831 | } |
| 4832 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4833 | // LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion. |
| 4834 | SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) { |
| 4835 | // This algorithm is not obvious. Here it is in C code, more or less: |
| 4836 | /* |
| 4837 | double uint64_to_double( uint32_t hi, uint32_t lo ) { |
| 4838 | static const __m128i exp = { 0x4330000045300000ULL, 0 }; |
| 4839 | static const __m128d bias = { 0x1.0p84, 0x1.0p52 }; |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4840 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4841 | // Copy ints to xmm registers. |
| 4842 | __m128i xh = _mm_cvtsi32_si128( hi ); |
| 4843 | __m128i xl = _mm_cvtsi32_si128( lo ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4844 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4845 | // Combine into low half of a single xmm register. |
| 4846 | __m128i x = _mm_unpacklo_epi32( xh, xl ); |
| 4847 | __m128d d; |
| 4848 | double sd; |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4849 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4850 | // Merge in appropriate exponents to give the integer bits the right |
| 4851 | // magnitude. |
| 4852 | x = _mm_unpacklo_epi32( x, exp ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4853 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4854 | // Subtract away the biases to deal with the IEEE-754 double precision |
| 4855 | // implicit 1. |
| 4856 | d = _mm_sub_pd( (__m128d) x, bias ); |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4857 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4858 | // All conversions up to here are exact. The correctly rounded result is |
| 4859 | // calculated using the current rounding mode using the following |
| 4860 | // horizontal add. |
| 4861 | d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) ); |
| 4862 | _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this |
| 4863 | // store doesn't really need to be here (except |
| 4864 | // maybe to zero the other double) |
| 4865 | return sd; |
| 4866 | } |
| 4867 | */ |
Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 4868 | |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4869 | DebugLoc dl = Op.getDebugLoc(); |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 4870 | LLVMContext *Context = DAG.getContext(); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4871 | |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4872 | // Build some magic constants. |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4873 | std::vector<Constant*> CV0; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4874 | CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000))); |
| 4875 | CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000))); |
| 4876 | CV0.push_back(ConstantInt::get(*Context, APInt(32, 0))); |
| 4877 | CV0.push_back(ConstantInt::get(*Context, APInt(32, 0))); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 4878 | Constant *C0 = ConstantVector::get(CV0); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4879 | SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4880 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4881 | std::vector<Constant*> CV1; |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 4882 | CV1.push_back( |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 4883 | ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL)))); |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 4884 | CV1.push_back( |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 4885 | ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL)))); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 4886 | Constant *C1 = ConstantVector::get(CV1); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 4887 | SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4888 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4889 | SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4890 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 6b6aeb3 | 2008-10-22 11:24:12 +0000 | [diff] [blame] | 4891 | Op.getOperand(0), |
| 4892 | DAG.getIntPtrConstant(1))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4893 | SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4894 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 6b6aeb3 | 2008-10-22 11:24:12 +0000 | [diff] [blame] | 4895 | Op.getOperand(0), |
| 4896 | DAG.getIntPtrConstant(0))); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4897 | SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4898 | SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4899 | PseudoSourceValue::getConstantPool(), 0, |
| 4900 | false, 16); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4901 | SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4902 | SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2); |
| 4903 | SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4904 | PseudoSourceValue::getConstantPool(), 0, |
| 4905 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4906 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4907 | |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4908 | // Add the halves; easiest way is to swap them into another reg first. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4909 | int ShufMask[2] = { 1, -1 }; |
| 4910 | SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub, |
| 4911 | DAG.getUNDEF(MVT::v2f64), ShufMask); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4912 | SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub); |
| 4913 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add, |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 4914 | DAG.getIntPtrConstant(0)); |
| 4915 | } |
| 4916 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4917 | // LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion. |
| 4918 | SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4919 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4920 | // FP constant to bias correct the final result. |
| 4921 | SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), |
| 4922 | MVT::f64); |
| 4923 | |
| 4924 | // Load the 32-bit value into an XMM register. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4925 | SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| 4926 | DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4927 | Op.getOperand(0), |
| 4928 | DAG.getIntPtrConstant(0))); |
| 4929 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4930 | Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| 4931 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load), |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4932 | DAG.getIntPtrConstant(0)); |
| 4933 | |
| 4934 | // Or the load with the bias. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4935 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, |
| 4936 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
| 4937 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Evan Cheng | 50c3dfe | 2009-01-19 08:19:57 +0000 | [diff] [blame] | 4938 | MVT::v2f64, Load)), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4939 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
| 4940 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
Evan Cheng | 50c3dfe | 2009-01-19 08:19:57 +0000 | [diff] [blame] | 4941 | MVT::v2f64, Bias))); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4942 | Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| 4943 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or), |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4944 | DAG.getIntPtrConstant(0)); |
| 4945 | |
| 4946 | // Subtract the bias. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4947 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4948 | |
| 4949 | // Handle final rounding. |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4950 | MVT DestVT = Op.getValueType(); |
| 4951 | |
| 4952 | if (DestVT.bitsLT(MVT::f64)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4953 | return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4954 | DAG.getIntPtrConstant(0)); |
| 4955 | } else if (DestVT.bitsGT(MVT::f64)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4956 | return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | // Handle final rounding. |
| 4960 | return Sub; |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4964 | SDValue N0 = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4965 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4966 | |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4967 | // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't |
| 4968 | // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform |
| 4969 | // the optimization here. |
| 4970 | if (DAG.SignBitIsZero(N0)) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4971 | return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0); |
Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 4972 | |
| 4973 | MVT SrcVT = N0.getValueType(); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4974 | if (SrcVT == MVT::i64) { |
Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 4975 | // We only handle SSE2 f64 target here; caller can expand the rest. |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4976 | if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64) |
Daniel Dunbar | 8220557 | 2009-05-26 21:27:02 +0000 | [diff] [blame] | 4977 | return SDValue(); |
Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 4978 | |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4979 | return LowerUINT_TO_FP_i64(Op, DAG); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4980 | } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) { |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4981 | return LowerUINT_TO_FP_i32(Op, DAG); |
| 4982 | } |
| 4983 | |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4984 | assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!"); |
| 4985 | |
| 4986 | // Make a 64-bit buffer, and use it to build an FILD. |
| 4987 | SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64); |
| 4988 | SDValue WordOff = DAG.getConstant(4, getPointerTy()); |
| 4989 | SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl, |
| 4990 | getPointerTy(), StackSlot, WordOff); |
| 4991 | SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
| 4992 | StackSlot, NULL, 0); |
| 4993 | SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32), |
| 4994 | OffsetSlot, NULL, 0); |
| 4995 | return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG); |
Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4998 | std::pair<SDValue,SDValue> X86TargetLowering:: |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 4999 | FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5000 | DebugLoc dl = Op.getDebugLoc(); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5001 | |
| 5002 | MVT DstTy = Op.getValueType(); |
| 5003 | |
| 5004 | if (!IsSigned) { |
| 5005 | assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT"); |
| 5006 | DstTy = MVT::i64; |
| 5007 | } |
| 5008 | |
| 5009 | assert(DstTy.getSimpleVT() <= MVT::i64 && |
| 5010 | DstTy.getSimpleVT() >= MVT::i16 && |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5011 | "Unknown FP_TO_SINT to lower!"); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5012 | |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 5013 | // These are really Legal. |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5014 | if (DstTy == MVT::i32 && |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 5015 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5016 | return std::make_pair(SDValue(), SDValue()); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 5017 | if (Subtarget->is64Bit() && |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5018 | DstTy == MVT::i64 && |
Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 5019 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5020 | return std::make_pair(SDValue(), SDValue()); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 5021 | |
Evan Cheng | 87c8935 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 5022 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 5023 | // stack slot. |
| 5024 | MachineFunction &MF = DAG.getMachineFunction(); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5025 | unsigned MemSize = DstTy.getSizeInBits()/8; |
Evan Cheng | 87c8935 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 5026 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5027 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5028 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5029 | unsigned Opc; |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5030 | switch (DstTy.getSimpleVT()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5031 | default: llvm_unreachable("Invalid FP_TO_SINT to lower!"); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5032 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 5033 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 5034 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5035 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 5036 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5037 | SDValue Chain = DAG.getEntryNode(); |
| 5038 | SDValue Value = Op.getOperand(0); |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 5039 | if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) { |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5040 | assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5041 | Chain = DAG.getStore(Chain, dl, Value, StackSlot, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 5042 | PseudoSourceValue::getFixedStack(SSFI), 0); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 5043 | SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5044 | SDValue Ops[] = { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 5045 | Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) |
| 5046 | }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5047 | Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5048 | Chain = Value.getValue(1); |
| 5049 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 5050 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 5051 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 5052 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5053 | // Build the FP_TO_INT*_IN_MEM |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5054 | SDValue Ops[] = { Chain, Value, StackSlot }; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5055 | SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 5056 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5057 | return std::make_pair(FIST, StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5060 | SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) { |
Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 5061 | if (Op.getValueType().isVector()) { |
| 5062 | if (Op.getValueType() == MVT::v2i32 && |
| 5063 | Op.getOperand(0).getValueType() == MVT::v2f64) { |
| 5064 | return Op; |
| 5065 | } |
| 5066 | return SDValue(); |
| 5067 | } |
| 5068 | |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5069 | std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5070 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 5071 | // If FP_TO_INTHelper failed, the node is actually supposed to be Legal. |
| 5072 | if (FIST.getNode() == 0) return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5073 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5074 | // Load the result. |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5075 | return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(), |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5076 | FIST, StackSlot, NULL, 0); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 5079 | SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) { |
| 5080 | std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false); |
| 5081 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| 5082 | assert(FIST.getNode() && "Unexpected failure"); |
| 5083 | |
| 5084 | // Load the result. |
| 5085 | return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(), |
| 5086 | FIST, StackSlot, NULL, 0); |
| 5087 | } |
| 5088 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5089 | SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) { |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 5090 | LLVMContext *Context = DAG.getContext(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5091 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5092 | MVT VT = Op.getValueType(); |
| 5093 | MVT EltVT = VT; |
| 5094 | if (VT.isVector()) |
| 5095 | EltVT = VT.getVectorElementType(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5096 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5097 | if (EltVT == MVT::f64) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5098 | Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5099 | CV.push_back(C); |
| 5100 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5101 | } else { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5102 | Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5103 | CV.push_back(C); |
| 5104 | CV.push_back(C); |
| 5105 | CV.push_back(C); |
| 5106 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5107 | } |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 5108 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5109 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5110 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5111 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5112 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5113 | return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5116 | SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) { |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 5117 | LLVMContext *Context = DAG.getContext(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5118 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5119 | MVT VT = Op.getValueType(); |
| 5120 | MVT EltVT = VT; |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 5121 | unsigned EltNum = 1; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5122 | if (VT.isVector()) { |
| 5123 | EltVT = VT.getVectorElementType(); |
| 5124 | EltNum = VT.getVectorNumElements(); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 5125 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5126 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5127 | if (EltVT == MVT::f64) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5128 | Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5129 | CV.push_back(C); |
| 5130 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5131 | } else { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5132 | Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 5133 | CV.push_back(C); |
| 5134 | CV.push_back(C); |
| 5135 | CV.push_back(C); |
| 5136 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5137 | } |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 5138 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5139 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5140 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5141 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5142 | false, 16); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5143 | if (VT.isVector()) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5144 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 5145 | DAG.getNode(ISD::XOR, dl, MVT::v2i64, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5146 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5147 | Op.getOperand(0)), |
| 5148 | DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask))); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 5149 | } else { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5150 | return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 5151 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5154 | SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) { |
Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 5155 | LLVMContext *Context = DAG.getContext(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5156 | SDValue Op0 = Op.getOperand(0); |
| 5157 | SDValue Op1 = Op.getOperand(1); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5158 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5159 | MVT VT = Op.getValueType(); |
| 5160 | MVT SrcVT = Op1.getValueType(); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5161 | |
| 5162 | // If second operand is smaller, extend it first. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5163 | if (SrcVT.bitsLT(VT)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5164 | Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5165 | SrcVT = VT; |
| 5166 | } |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 5167 | // And if it is bigger, shrink it first. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5168 | if (SrcVT.bitsGT(VT)) { |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5169 | Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1)); |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 5170 | SrcVT = VT; |
Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | // At this point the operands and the result should have the same |
| 5174 | // 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] | 5175 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5176 | // First get the sign bit of second operand. |
| 5177 | std::vector<Constant*> CV; |
| 5178 | if (SrcVT == MVT::f64) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5179 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)))); |
| 5180 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0)))); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5181 | } else { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5182 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)))); |
| 5183 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
| 5184 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
| 5185 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5186 | } |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 5187 | Constant *C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5188 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5189 | SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5190 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5191 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5192 | SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5193 | |
| 5194 | // 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] | 5195 | if (SrcVT.bitsGT(VT)) { |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5196 | // Op0 is MVT::f32, Op1 is MVT::f64. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5197 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit); |
| 5198 | SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit, |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5199 | DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5200 | SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit); |
| 5201 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5202 | DAG.getIntPtrConstant(0)); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5205 | // Clear first operand sign bit. |
| 5206 | CV.clear(); |
| 5207 | if (VT == MVT::f64) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5208 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))))); |
| 5209 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0)))); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5210 | } else { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 5211 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))))); |
| 5212 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
| 5213 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
| 5214 | CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0)))); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5215 | } |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 5216 | C = ConstantVector::get(CV); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 5217 | CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5218 | SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5219 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 5220 | false, 16); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5221 | SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 5222 | |
| 5223 | // Or the value with the sign bit. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5224 | return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5227 | /// Emit nodes that will be selected as "test Op0,Op0", or something |
| 5228 | /// equivalent. |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5229 | SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC, |
| 5230 | SelectionDAG &DAG) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5231 | DebugLoc dl = Op.getDebugLoc(); |
| 5232 | |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5233 | // CF and OF aren't always set the way we want. Determine which |
| 5234 | // of these we need. |
| 5235 | bool NeedCF = false; |
| 5236 | bool NeedOF = false; |
| 5237 | switch (X86CC) { |
| 5238 | case X86::COND_A: case X86::COND_AE: |
| 5239 | case X86::COND_B: case X86::COND_BE: |
| 5240 | NeedCF = true; |
| 5241 | break; |
| 5242 | case X86::COND_G: case X86::COND_GE: |
| 5243 | case X86::COND_L: case X86::COND_LE: |
| 5244 | case X86::COND_O: case X86::COND_NO: |
| 5245 | NeedOF = true; |
| 5246 | break; |
| 5247 | default: break; |
| 5248 | } |
| 5249 | |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5250 | // 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] | 5251 | // doing a separate TEST. TEST always sets OF and CF to 0, so unless |
| 5252 | // we prove that the arithmetic won't overflow, we can't use OF or CF. |
| 5253 | if (Op.getResNo() == 0 && !NeedOF && !NeedCF) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5254 | unsigned Opcode = 0; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5255 | unsigned NumOperands = 0; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5256 | switch (Op.getNode()->getOpcode()) { |
| 5257 | case ISD::ADD: |
| 5258 | // Due to an isel shortcoming, be conservative if this add is likely to |
| 5259 | // be selected as part of a load-modify-store instruction. When the root |
| 5260 | // node in a match is a store, isel doesn't know how to remap non-chain |
| 5261 | // non-flag uses of other nodes in the match, such as the ADD in this |
| 5262 | // case. This leads to the ADD being left around and reselected, with |
| 5263 | // the result being two adds in the output. |
| 5264 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 5265 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 5266 | if (UI->getOpcode() == ISD::STORE) |
| 5267 | goto default_case; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5268 | if (ConstantSDNode *C = |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5269 | dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) { |
| 5270 | // An add of one will be selected as an INC. |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5271 | if (C->getAPIntValue() == 1) { |
| 5272 | Opcode = X86ISD::INC; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5273 | NumOperands = 1; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5274 | break; |
| 5275 | } |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5276 | // An add of negative one (subtract of one) will be selected as a DEC. |
| 5277 | if (C->getAPIntValue().isAllOnesValue()) { |
| 5278 | Opcode = X86ISD::DEC; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5279 | NumOperands = 1; |
Dan Gohman | 4bfcf2a | 2009-03-05 19:32:48 +0000 | [diff] [blame] | 5280 | break; |
| 5281 | } |
| 5282 | } |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5283 | // Otherwise use a regular EFLAGS-setting add. |
| 5284 | Opcode = X86ISD::ADD; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5285 | NumOperands = 2; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5286 | break; |
| 5287 | case ISD::SUB: |
| 5288 | // Due to the ISEL shortcoming noted above, be conservative if this sub is |
| 5289 | // likely to be selected as part of a load-modify-store instruction. |
| 5290 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 5291 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 5292 | if (UI->getOpcode() == ISD::STORE) |
| 5293 | goto default_case; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5294 | // Otherwise use a regular EFLAGS-setting sub. |
| 5295 | Opcode = X86ISD::SUB; |
Dan Gohman | 51bb474 | 2009-03-05 21:29:28 +0000 | [diff] [blame] | 5296 | NumOperands = 2; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5297 | break; |
| 5298 | case X86ISD::ADD: |
| 5299 | case X86ISD::SUB: |
| 5300 | case X86ISD::INC: |
| 5301 | case X86ISD::DEC: |
| 5302 | return SDValue(Op.getNode(), 1); |
| 5303 | default: |
| 5304 | default_case: |
| 5305 | break; |
| 5306 | } |
| 5307 | if (Opcode != 0) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5308 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5309 | SmallVector<SDValue, 4> Ops; |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5310 | for (unsigned i = 0; i != NumOperands; ++i) |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5311 | Ops.push_back(Op.getOperand(i)); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5312 | SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5313 | DAG.ReplaceAllUsesWith(Op, New); |
| 5314 | return SDValue(New.getNode(), 1); |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | // Otherwise just emit a CMP with 0, which is the TEST pattern. |
| 5319 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, |
| 5320 | DAG.getConstant(0, Op.getValueType())); |
| 5321 | } |
| 5322 | |
| 5323 | /// Emit nodes that will be selected as "cmp Op0,Op1", or something |
| 5324 | /// equivalent. |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5325 | SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC, |
| 5326 | SelectionDAG &DAG) { |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5327 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1)) |
| 5328 | if (C->getAPIntValue() == 0) |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5329 | return EmitTest(Op0, X86CC, DAG); |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5330 | |
| 5331 | DebugLoc dl = Op0.getDebugLoc(); |
| 5332 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1); |
| 5333 | } |
| 5334 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5335 | SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5336 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5337 | SDValue Op0 = Op.getOperand(0); |
| 5338 | SDValue Op1 = Op.getOperand(1); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5339 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5340 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5341 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5342 | // Lower (X & (1 << N)) == 0 to BT(X, N). |
| 5343 | // Lower ((X >>u N) & 1) != 0 to BT(X, N). |
| 5344 | // Lower ((X >>s N) & 1) != 0 to BT(X, N). |
Dan Gohman | 286575c | 2009-01-13 23:25:30 +0000 | [diff] [blame] | 5345 | if (Op0.getOpcode() == ISD::AND && |
| 5346 | Op0.hasOneUse() && |
| 5347 | Op1.getOpcode() == ISD::Constant && |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5348 | cast<ConstantSDNode>(Op1)->getZExtValue() == 0 && |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5349 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5350 | SDValue LHS, RHS; |
| 5351 | if (Op0.getOperand(1).getOpcode() == ISD::SHL) { |
| 5352 | if (ConstantSDNode *Op010C = |
| 5353 | dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0))) |
| 5354 | if (Op010C->getZExtValue() == 1) { |
| 5355 | LHS = Op0.getOperand(0); |
| 5356 | RHS = Op0.getOperand(1).getOperand(1); |
| 5357 | } |
| 5358 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) { |
| 5359 | if (ConstantSDNode *Op000C = |
| 5360 | dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0))) |
| 5361 | if (Op000C->getZExtValue() == 1) { |
| 5362 | LHS = Op0.getOperand(1); |
| 5363 | RHS = Op0.getOperand(0).getOperand(1); |
| 5364 | } |
| 5365 | } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) { |
| 5366 | ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1)); |
| 5367 | SDValue AndLHS = Op0.getOperand(0); |
| 5368 | if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) { |
| 5369 | LHS = AndLHS.getOperand(0); |
| 5370 | RHS = AndLHS.getOperand(1); |
| 5371 | } |
| 5372 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5373 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5374 | if (LHS.getNode()) { |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5375 | // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT |
| 5376 | // instruction. Since the shift amount is in-range-or-undefined, we know |
| 5377 | // that doing a bittest on the i16 value is ok. We extend to i32 because |
| 5378 | // the encoding for the i16 version is larger than the i32 version. |
| 5379 | if (LHS.getValueType() == MVT::i8) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5380 | LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5381 | |
| 5382 | // If the operand types disagree, extend the shift amount to match. Since |
| 5383 | // BT ignores high bits (like shifts) we can use anyextend. |
| 5384 | if (LHS.getValueType() != RHS.getValueType()) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5385 | RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 5386 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5387 | SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS); |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 5388 | unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5389 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5390 | DAG.getConstant(Cond, MVT::i8), BT); |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); |
| 5395 | unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5396 | |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5397 | SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5398 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Chris Lattner | 4328708 | 2008-12-24 00:11:37 +0000 | [diff] [blame] | 5399 | DAG.getConstant(X86CC, MVT::i8), Cond); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5402 | SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) { |
| 5403 | SDValue Cond; |
| 5404 | SDValue Op0 = Op.getOperand(0); |
| 5405 | SDValue Op1 = Op.getOperand(1); |
| 5406 | SDValue CC = Op.getOperand(2); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5407 | MVT VT = Op.getValueType(); |
| 5408 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| 5409 | bool isFP = Op.getOperand(1).getValueType().isFloatingPoint(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5410 | DebugLoc dl = Op.getDebugLoc(); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5411 | |
| 5412 | if (isFP) { |
| 5413 | unsigned SSECC = 8; |
Evan Cheng | e9d5035 | 2008-08-05 22:19:15 +0000 | [diff] [blame] | 5414 | MVT VT0 = Op0.getValueType(); |
| 5415 | assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64); |
| 5416 | unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD; |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5417 | bool Swap = false; |
| 5418 | |
| 5419 | switch (SetCCOpcode) { |
| 5420 | default: break; |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5421 | case ISD::SETOEQ: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5422 | case ISD::SETEQ: SSECC = 0; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5423 | case ISD::SETOGT: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5424 | case ISD::SETGT: Swap = true; // Fallthrough |
| 5425 | case ISD::SETLT: |
| 5426 | case ISD::SETOLT: SSECC = 1; break; |
| 5427 | case ISD::SETOGE: |
| 5428 | case ISD::SETGE: Swap = true; // Fallthrough |
| 5429 | case ISD::SETLE: |
| 5430 | case ISD::SETOLE: SSECC = 2; break; |
| 5431 | case ISD::SETUO: SSECC = 3; break; |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5432 | case ISD::SETUNE: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5433 | case ISD::SETNE: SSECC = 4; break; |
| 5434 | case ISD::SETULE: Swap = true; |
| 5435 | case ISD::SETUGE: SSECC = 5; break; |
| 5436 | case ISD::SETULT: Swap = true; |
| 5437 | case ISD::SETUGT: SSECC = 6; break; |
| 5438 | case ISD::SETO: SSECC = 7; break; |
| 5439 | } |
| 5440 | if (Swap) |
| 5441 | std::swap(Op0, Op1); |
| 5442 | |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5443 | // In the two special cases we can't handle, emit two comparisons. |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5444 | if (SSECC == 8) { |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5445 | if (SetCCOpcode == ISD::SETUEQ) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5446 | SDValue UNORD, EQ; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5447 | UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8)); |
| 5448 | EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8)); |
| 5449 | return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5450 | } |
| 5451 | else if (SetCCOpcode == ISD::SETONE) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5452 | SDValue ORD, NEQ; |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5453 | ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8)); |
| 5454 | NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8)); |
| 5455 | return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ); |
Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 5456 | } |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5457 | llvm_unreachable("Illegal FP comparison"); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5458 | } |
| 5459 | // Handle all other FP comparisons here. |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5460 | 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] | 5461 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5462 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5463 | // We are handling one of the integer comparisons here. Since SSE only has |
| 5464 | // GT and EQ comparisons for integer, swapping operands and multiple |
| 5465 | // operations may be required for some comparisons. |
| 5466 | unsigned Opc = 0, EQOpc = 0, GTOpc = 0; |
| 5467 | bool Swap = false, Invert = false, FlipSigns = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5468 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5469 | switch (VT.getSimpleVT()) { |
| 5470 | default: break; |
Eli Friedman | 3dae284 | 2009-07-22 01:06:52 +0000 | [diff] [blame] | 5471 | case MVT::v8i8: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5472 | case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break; |
Eli Friedman | 3dae284 | 2009-07-22 01:06:52 +0000 | [diff] [blame] | 5473 | case MVT::v4i16: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5474 | case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break; |
Eli Friedman | 3dae284 | 2009-07-22 01:06:52 +0000 | [diff] [blame] | 5475 | case MVT::v2i32: |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5476 | case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break; |
| 5477 | case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break; |
| 5478 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5479 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5480 | switch (SetCCOpcode) { |
| 5481 | default: break; |
| 5482 | case ISD::SETNE: Invert = true; |
| 5483 | case ISD::SETEQ: Opc = EQOpc; break; |
| 5484 | case ISD::SETLT: Swap = true; |
| 5485 | case ISD::SETGT: Opc = GTOpc; break; |
| 5486 | case ISD::SETGE: Swap = true; |
| 5487 | case ISD::SETLE: Opc = GTOpc; Invert = true; break; |
| 5488 | case ISD::SETULT: Swap = true; |
| 5489 | case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break; |
| 5490 | case ISD::SETUGE: Swap = true; |
| 5491 | case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break; |
| 5492 | } |
| 5493 | if (Swap) |
| 5494 | std::swap(Op0, Op1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5495 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5496 | // Since SSE has no unsigned integer comparisons, we need to flip the sign |
| 5497 | // bits of the inputs before performing those operations. |
| 5498 | if (FlipSigns) { |
| 5499 | MVT EltVT = VT.getVectorElementType(); |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 5500 | SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), |
| 5501 | EltVT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5502 | std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5503 | SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0], |
| 5504 | SignBits.size()); |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5505 | Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec); |
| 5506 | Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5507 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5508 | |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5509 | SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5510 | |
| 5511 | // If the logical-not of the result is required, perform that now. |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 5512 | if (Invert) |
Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5513 | Result = DAG.getNOT(dl, Result, VT); |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 5514 | |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 5515 | return Result; |
| 5516 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5517 | |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5518 | // isX86LogicalCmp - Return true if opcode is a X86 logical comparison. |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5519 | static bool isX86LogicalCmp(SDValue Op) { |
| 5520 | unsigned Opc = Op.getNode()->getOpcode(); |
| 5521 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) |
| 5522 | return true; |
| 5523 | if (Op.getResNo() == 1 && |
| 5524 | (Opc == X86ISD::ADD || |
| 5525 | Opc == X86ISD::SUB || |
| 5526 | Opc == X86ISD::SMUL || |
| 5527 | Opc == X86ISD::UMUL || |
| 5528 | Opc == X86ISD::INC || |
| 5529 | Opc == X86ISD::DEC)) |
| 5530 | return true; |
| 5531 | |
| 5532 | return false; |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5535 | SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5536 | bool addTest = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5537 | SDValue Cond = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5538 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5539 | SDValue CC; |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 5540 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5541 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5542 | Cond = LowerSETCC(Cond, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5543 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5544 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 5545 | // setting operand in place of the X86ISD::SETCC. |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5546 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 5547 | CC = Cond.getOperand(0); |
| 5548 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5549 | SDValue Cmp = Cond.getOperand(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5550 | unsigned Opc = Cmp.getOpcode(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5551 | MVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5552 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5553 | bool IllegalFPCMov = false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5554 | if (VT.isFloatingPoint() && !VT.isVector() && |
Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 5555 | !isScalarFPTypeInSSEReg(VT)) // FPStack? |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 5556 | IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5557 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 5558 | if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) || |
| 5559 | Opc == X86ISD::BT) { // FIXME |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5560 | Cond = Cmp; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5561 | addTest = false; |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | if (addTest) { |
| 5566 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5567 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5570 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5571 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5572 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 5573 | // condition is true. |
| 5574 | Ops.push_back(Op.getOperand(2)); |
| 5575 | Ops.push_back(Op.getOperand(1)); |
| 5576 | Ops.push_back(CC); |
| 5577 | Ops.push_back(Cond); |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5578 | return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size()); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5581 | // isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or |
| 5582 | // ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart |
| 5583 | // from the AND / OR. |
| 5584 | static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) { |
| 5585 | Opc = Op.getOpcode(); |
| 5586 | if (Opc != ISD::OR && Opc != ISD::AND) |
| 5587 | return false; |
| 5588 | return (Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 5589 | Op.getOperand(0).hasOneUse() && |
| 5590 | Op.getOperand(1).getOpcode() == X86ISD::SETCC && |
| 5591 | Op.getOperand(1).hasOneUse()); |
| 5592 | } |
| 5593 | |
Evan Cheng | 961d6d4 | 2009-02-02 08:19:07 +0000 | [diff] [blame] | 5594 | // isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and |
| 5595 | // 1 and that the SETCC node has a single use. |
Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 5596 | static bool isXor1OfSetCC(SDValue Op) { |
| 5597 | if (Op.getOpcode() != ISD::XOR) |
| 5598 | return false; |
| 5599 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5600 | if (N1C && N1C->getAPIntValue() == 1) { |
| 5601 | return Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 5602 | Op.getOperand(0).hasOneUse(); |
| 5603 | } |
| 5604 | return false; |
| 5605 | } |
| 5606 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5607 | SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5608 | bool addTest = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5609 | SDValue Chain = Op.getOperand(0); |
| 5610 | SDValue Cond = Op.getOperand(1); |
| 5611 | SDValue Dest = Op.getOperand(2); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5612 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5613 | SDValue CC; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5614 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5615 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 5616 | Cond = LowerSETCC(Cond, DAG); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5617 | #if 0 |
| 5618 | // FIXME: LowerXALUO doesn't handle these!! |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 5619 | else if (Cond.getOpcode() == X86ISD::ADD || |
| 5620 | Cond.getOpcode() == X86ISD::SUB || |
| 5621 | Cond.getOpcode() == X86ISD::SMUL || |
| 5622 | Cond.getOpcode() == X86ISD::UMUL) |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 5623 | Cond = LowerXALUO(Cond, DAG); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5624 | #endif |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5625 | |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5626 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 5627 | // setting operand in place of the X86ISD::SETCC. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5628 | if (Cond.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5629 | CC = Cond.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5630 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5631 | SDValue Cmp = Cond.getOperand(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 5632 | unsigned Opc = Cmp.getOpcode(); |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5633 | // FIXME: WHY THE SPECIAL CASING OF LogicalCmp?? |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5634 | if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) { |
Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 5635 | Cond = Cmp; |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5636 | addTest = false; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 5637 | } else { |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5638 | switch (cast<ConstantSDNode>(CC)->getZExtValue()) { |
Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 5639 | default: break; |
| 5640 | case X86::COND_O: |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 5641 | case X86::COND_B: |
Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 5642 | // These can only come from an arithmetic instruction with overflow, |
| 5643 | // e.g. SADDO, UADDO. |
Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 5644 | Cond = Cond.getNode()->getOperand(1); |
| 5645 | addTest = false; |
| 5646 | break; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 5647 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5648 | } |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5649 | } else { |
| 5650 | unsigned CondOpc; |
| 5651 | if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) { |
| 5652 | SDValue Cmp = Cond.getOperand(0).getOperand(1); |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5653 | if (CondOpc == ISD::OR) { |
| 5654 | // Also, recognize the pattern generated by an FCMP_UNE. We can emit |
| 5655 | // two branches instead of an explicit OR instruction with a |
| 5656 | // separate test. |
| 5657 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5658 | isX86LogicalCmp(Cmp)) { |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5659 | CC = Cond.getOperand(0).getOperand(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5660 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5661 | Chain, Dest, CC, Cmp); |
| 5662 | CC = Cond.getOperand(1).getOperand(0); |
| 5663 | Cond = Cmp; |
| 5664 | addTest = false; |
| 5665 | } |
| 5666 | } else { // ISD::AND |
| 5667 | // Also, recognize the pattern generated by an FCMP_OEQ. We can emit |
| 5668 | // two branches instead of an explicit AND instruction with a |
| 5669 | // separate test. However, we only do this if this block doesn't |
| 5670 | // have a fall-through edge, because this requires an explicit |
| 5671 | // jmp when the condition is false. |
| 5672 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 5673 | isX86LogicalCmp(Cmp) && |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5674 | Op.getNode()->hasOneUse()) { |
| 5675 | X86::CondCode CCode = |
| 5676 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 5677 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5678 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5679 | SDValue User = SDValue(*Op.getNode()->use_begin(), 0); |
| 5680 | // Look for an unconditional branch following this conditional branch. |
| 5681 | // We need this because we need to reverse the successors in order |
| 5682 | // to implement FCMP_OEQ. |
| 5683 | if (User.getOpcode() == ISD::BR) { |
| 5684 | SDValue FalseBB = User.getOperand(1); |
| 5685 | SDValue NewBR = |
| 5686 | DAG.UpdateNodeOperands(User, User.getOperand(0), Dest); |
| 5687 | assert(NewBR == User); |
| 5688 | Dest = FalseBB; |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5689 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5690 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 5691 | Chain, Dest, CC, Cmp); |
| 5692 | X86::CondCode CCode = |
| 5693 | (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0); |
| 5694 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5695 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5696 | Cond = Cmp; |
| 5697 | addTest = false; |
| 5698 | } |
| 5699 | } |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5700 | } |
Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 5701 | } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) { |
| 5702 | // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition. |
| 5703 | // It should be transformed during dag combiner except when the condition |
| 5704 | // is set by a arithmetics with overflow node. |
| 5705 | X86::CondCode CCode = |
| 5706 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 5707 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 5708 | CC = DAG.getConstant(CCode, MVT::i8); |
| 5709 | Cond = Cond.getOperand(0).getOperand(1); |
| 5710 | addTest = false; |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5711 | } |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | if (addTest) { |
| 5715 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 5716 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5717 | } |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5718 | return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 5719 | Chain, Dest, CC, Cond); |
Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 5722 | |
| 5723 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. |
| 5724 | // Calls to _alloca is needed to probe the stack when allocating more than 4k |
| 5725 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure |
| 5726 | // that the guard pages used by the OS virtual memory manager are allocated in |
| 5727 | // correct sequence. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5728 | SDValue |
| 5729 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5730 | SelectionDAG &DAG) { |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 5731 | assert(Subtarget->isTargetCygMing() && |
| 5732 | "This should be used only on Cygwin/Mingw targets"); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5733 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5734 | |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5735 | // Get the inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5736 | SDValue Chain = Op.getOperand(0); |
| 5737 | SDValue Size = Op.getOperand(1); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5738 | // FIXME: Ensure alignment here |
| 5739 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5740 | SDValue Flag; |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5741 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5742 | MVT IntPtr = getPointerTy(); |
| 5743 | MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5744 | |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 5745 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5746 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 5747 | Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag); |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5748 | Flag = Chain.getValue(1); |
| 5749 | |
| 5750 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5751 | SDValue Ops[] = { Chain, |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 5752 | DAG.getTargetExternalSymbol("_alloca", IntPtr), |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5753 | DAG.getRegister(X86::EAX, IntPtr), |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5754 | DAG.getRegister(X86StackPtr, SPTy), |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5755 | Flag }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5756 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5); |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 5757 | Flag = Chain.getValue(1); |
| 5758 | |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5759 | Chain = DAG.getCALLSEQ_END(Chain, |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 5760 | DAG.getIntPtrConstant(0, true), |
| 5761 | DAG.getIntPtrConstant(0, true), |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5762 | Flag); |
| 5763 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 5764 | Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1); |
Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 5765 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5766 | SDValue Ops1[2] = { Chain.getValue(0), Chain }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5767 | return DAG.getMergeValues(Ops1, 2, dl); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5770 | SDValue |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5771 | X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl, |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5772 | SDValue Chain, |
| 5773 | SDValue Dst, SDValue Src, |
| 5774 | SDValue Size, unsigned Align, |
| 5775 | const Value *DstSV, |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5776 | uint64_t DstSVOff) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5777 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5778 | |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5779 | // If not DWORD aligned or size is more than the threshold, call the library. |
| 5780 | // The libc version is likely to be faster for these cases. It can use the |
| 5781 | // address value and run time information about the CPU. |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5782 | if ((Align & 3) != 0 || |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5783 | !ConstantSize || |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5784 | ConstantSize->getZExtValue() > |
| 5785 | getSubtarget()->getMaxInlineSizeThreshold()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5786 | SDValue InFlag(0, 0); |
Dan Gohman | 68d599d | 2008-04-01 20:38:36 +0000 | [diff] [blame] | 5787 | |
| 5788 | // 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] | 5789 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src); |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 5790 | |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5791 | if (const char *bzeroEntry = V && |
| 5792 | V->isNullValue() ? Subtarget->getBZeroEntry() : 0) { |
| 5793 | MVT IntPtr = getPointerTy(); |
| 5794 | const Type *IntPtrTy = TD->getIntPtrType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5795 | TargetLowering::ArgListTy Args; |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5796 | TargetLowering::ArgListEntry Entry; |
| 5797 | Entry.Node = Dst; |
| 5798 | Entry.Ty = IntPtrTy; |
| 5799 | Args.push_back(Entry); |
| 5800 | Entry.Node = Size; |
| 5801 | Args.push_back(Entry); |
| 5802 | std::pair<SDValue,SDValue> CallResult = |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5803 | LowerCallTo(Chain, Type::VoidTy, false, false, false, false, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 5804 | 0, CallingConv::C, false, /*isReturnValueUsed=*/false, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5805 | DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl); |
Bill Wendling | 6158d84 | 2008-10-01 00:59:58 +0000 | [diff] [blame] | 5806 | return CallResult.second; |
Dan Gohman | 68d599d | 2008-04-01 20:38:36 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5809 | // Otherwise have the target-independent code call memset. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5810 | return SDValue(); |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 5811 | } |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 5812 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5813 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5814 | SDValue InFlag(0, 0); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5815 | MVT AVT; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5816 | SDValue Count; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5817 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5818 | unsigned BytesLeft = 0; |
| 5819 | bool TwoRepStos = false; |
| 5820 | if (ValC) { |
| 5821 | unsigned ValReg; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5822 | uint64_t Val = ValC->getZExtValue() & 255; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 5823 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5824 | // If the value is a constant, then we can potentially use larger sets. |
| 5825 | switch (Align & 3) { |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5826 | case 2: // WORD aligned |
| 5827 | AVT = MVT::i16; |
| 5828 | ValReg = X86::AX; |
| 5829 | Val = (Val << 8) | Val; |
| 5830 | break; |
| 5831 | case 0: // DWORD aligned |
| 5832 | AVT = MVT::i32; |
| 5833 | ValReg = X86::EAX; |
| 5834 | Val = (Val << 8) | Val; |
| 5835 | Val = (Val << 16) | Val; |
| 5836 | if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned |
| 5837 | AVT = MVT::i64; |
| 5838 | ValReg = X86::RAX; |
| 5839 | Val = (Val << 32) | Val; |
| 5840 | } |
| 5841 | break; |
| 5842 | default: // Byte aligned |
| 5843 | AVT = MVT::i8; |
| 5844 | ValReg = X86::AL; |
| 5845 | Count = DAG.getIntPtrConstant(SizeVal); |
| 5846 | break; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 5849 | if (AVT.bitsGT(MVT::i8)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5850 | unsigned UBytes = AVT.getSizeInBits() / 8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5851 | Count = DAG.getIntPtrConstant(SizeVal / UBytes); |
| 5852 | BytesLeft = SizeVal % UBytes; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5855 | Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT), |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5856 | InFlag); |
| 5857 | InFlag = Chain.getValue(1); |
| 5858 | } else { |
| 5859 | AVT = MVT::i8; |
Dan Gohman | bcda285 | 2008-04-16 01:32:32 +0000 | [diff] [blame] | 5860 | Count = DAG.getIntPtrConstant(SizeVal); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5861 | Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5862 | InFlag = Chain.getValue(1); |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 5863 | } |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 5864 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5865 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5866 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5867 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5868 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5869 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5870 | X86::EDI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5871 | Dst, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5872 | InFlag = Chain.getValue(1); |
Evan Cheng | a0b3afb | 2006-03-27 07:00:16 +0000 | [diff] [blame] | 5873 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5874 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5875 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5876 | Ops.push_back(Chain); |
| 5877 | Ops.push_back(DAG.getValueType(AVT)); |
| 5878 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5879 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 5880 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5881 | if (TwoRepStos) { |
| 5882 | InFlag = Chain.getValue(1); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5883 | Count = Size; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5884 | MVT CVT = Count.getValueType(); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5885 | SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5886 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5887 | Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5888 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5889 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5890 | InFlag = Chain.getValue(1); |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5891 | Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5892 | Ops.clear(); |
| 5893 | Ops.push_back(Chain); |
| 5894 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 5895 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5896 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5897 | } else if (BytesLeft) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5898 | // Handle the last 1 - 7 bytes. |
| 5899 | unsigned Offset = SizeVal - BytesLeft; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5900 | MVT AddrVT = Dst.getValueType(); |
| 5901 | MVT SizeVT = Size.getValueType(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5902 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5903 | Chain = DAG.getMemset(Chain, dl, |
| 5904 | DAG.getNode(ISD::ADD, dl, AddrVT, Dst, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5905 | DAG.getConstant(Offset, AddrVT)), |
| 5906 | Src, |
| 5907 | DAG.getConstant(BytesLeft, SizeVT), |
Dan Gohman | 1f13c68 | 2008-04-28 17:15:20 +0000 | [diff] [blame] | 5908 | Align, DstSV, DstSVOff + Offset); |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 5909 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 5910 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5911 | // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5912 | return Chain; |
| 5913 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 5914 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5915 | SDValue |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5916 | X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl, |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5917 | SDValue Chain, SDValue Dst, SDValue Src, |
| 5918 | SDValue Size, unsigned Align, |
| 5919 | bool AlwaysInline, |
| 5920 | const Value *DstSV, uint64_t DstSVOff, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5921 | const Value *SrcSV, uint64_t SrcSVOff) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5922 | // This requires the copy size to be a constant, preferrably |
| 5923 | // within a subtarget-specific limit. |
| 5924 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 5925 | if (!ConstantSize) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5926 | return SDValue(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5927 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5928 | if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5929 | return SDValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5930 | |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5931 | /// If not DWORD aligned, call the library. |
| 5932 | if ((Align & 3) != 0) |
| 5933 | return SDValue(); |
| 5934 | |
| 5935 | // DWORD aligned |
| 5936 | MVT AVT = MVT::i32; |
| 5937 | if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5938 | AVT = MVT::i64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5939 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5940 | unsigned UBytes = AVT.getSizeInBits() / 8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5941 | unsigned CountVal = SizeVal / UBytes; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5942 | SDValue Count = DAG.getIntPtrConstant(CountVal); |
Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 5943 | unsigned BytesLeft = SizeVal % UBytes; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5944 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5945 | SDValue InFlag(0, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5946 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5947 | X86::ECX, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5948 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5949 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5950 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5951 | X86::EDI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5952 | Dst, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5953 | InFlag = Chain.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5954 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI : |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5955 | X86::ESI, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5956 | Src, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5957 | InFlag = Chain.getValue(1); |
| 5958 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 5959 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5960 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5961 | Ops.push_back(Chain); |
| 5962 | Ops.push_back(DAG.getValueType(AVT)); |
| 5963 | Ops.push_back(InFlag); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5964 | 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] | 5965 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5966 | SmallVector<SDValue, 4> Results; |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5967 | Results.push_back(RepMovs); |
Rafael Espindola | 068317b | 2007-09-28 12:53:01 +0000 | [diff] [blame] | 5968 | if (BytesLeft) { |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5969 | // Handle the last 1 - 7 bytes. |
| 5970 | unsigned Offset = SizeVal - BytesLeft; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5971 | MVT DstVT = Dst.getValueType(); |
| 5972 | MVT SrcVT = Src.getValueType(); |
| 5973 | MVT SizeVT = Size.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5974 | Results.push_back(DAG.getMemcpy(Chain, dl, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5975 | DAG.getNode(ISD::ADD, dl, DstVT, Dst, |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5976 | DAG.getConstant(Offset, DstVT)), |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5977 | DAG.getNode(ISD::ADD, dl, SrcVT, Src, |
Evan Cheng | 2749c72 | 2008-04-25 00:26:43 +0000 | [diff] [blame] | 5978 | DAG.getConstant(Offset, SrcVT)), |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 5979 | DAG.getConstant(BytesLeft, SizeVT), |
| 5980 | Align, AlwaysInline, |
Dan Gohman | 1f13c68 | 2008-04-28 17:15:20 +0000 | [diff] [blame] | 5981 | DstSV, DstSVOff + Offset, |
| 5982 | SrcSV, SrcSVOff + Offset)); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 5983 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5984 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5985 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 5986 | &Results[0], Results.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5989 | SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5990 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5991 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5992 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5993 | if (!Subtarget->is64Bit()) { |
| 5994 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 5995 | // memory location argument. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5996 | SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 5997 | 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] | 5998 | } |
| 5999 | |
| 6000 | // __va_list_tag: |
| 6001 | // gp_offset (0 - 6 * 8) |
| 6002 | // fp_offset (48 - 48 + 8 * 16) |
| 6003 | // overflow_arg_area (point to parameters coming in memory). |
| 6004 | // reg_save_area |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6005 | SmallVector<SDValue, 8> MemOps; |
| 6006 | SDValue FIN = Op.getOperand(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 6007 | // Store gp_offset |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6008 | SDValue Store = DAG.getStore(Op.getOperand(0), dl, |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 6009 | DAG.getConstant(VarArgsGPOffset, MVT::i32), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6010 | FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 6011 | MemOps.push_back(Store); |
| 6012 | |
| 6013 | // Store fp_offset |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6014 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6015 | FIN, DAG.getIntPtrConstant(4)); |
| 6016 | Store = DAG.getStore(Op.getOperand(0), dl, |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 6017 | DAG.getConstant(VarArgsFPOffset, MVT::i32), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6018 | FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 6019 | MemOps.push_back(Store); |
| 6020 | |
| 6021 | // Store ptr to overflow_arg_area |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6022 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6023 | FIN, DAG.getIntPtrConstant(4)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6024 | SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6025 | Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 6026 | MemOps.push_back(Store); |
| 6027 | |
| 6028 | // Store ptr to reg_save_area. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6029 | FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6030 | FIN, DAG.getIntPtrConstant(8)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6031 | SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6032 | Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 6033 | MemOps.push_back(Store); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6034 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6035 | &MemOps[0], MemOps.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6038 | SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 6039 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
| 6040 | assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6041 | SDValue Chain = Op.getOperand(0); |
| 6042 | SDValue SrcPtr = Op.getOperand(1); |
| 6043 | SDValue SrcSV = Op.getOperand(2); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 6044 | |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 6045 | llvm_report_error("VAArgInst is not yet implemented for x86-64!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6046 | return SDValue(); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6049 | SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 6050 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
Dan Gohman | 2826913 | 2008-04-18 20:55:41 +0000 | [diff] [blame] | 6051 | assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6052 | SDValue Chain = Op.getOperand(0); |
| 6053 | SDValue DstPtr = Op.getOperand(1); |
| 6054 | SDValue SrcPtr = Op.getOperand(2); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6055 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); |
| 6056 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6057 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 6058 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6059 | return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr, |
Dan Gohman | 2826913 | 2008-04-18 20:55:41 +0000 | [diff] [blame] | 6060 | DAG.getIntPtrConstant(24), 8, false, |
| 6061 | DstSV, 0, SrcSV, 0); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6064 | SDValue |
| 6065 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6066 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6067 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6068 | switch (IntNo) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6069 | default: return SDValue(); // Don't custom lower most intrinsics. |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6070 | // Comparison intrinsics. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6071 | case Intrinsic::x86_sse_comieq_ss: |
| 6072 | case Intrinsic::x86_sse_comilt_ss: |
| 6073 | case Intrinsic::x86_sse_comile_ss: |
| 6074 | case Intrinsic::x86_sse_comigt_ss: |
| 6075 | case Intrinsic::x86_sse_comige_ss: |
| 6076 | case Intrinsic::x86_sse_comineq_ss: |
| 6077 | case Intrinsic::x86_sse_ucomieq_ss: |
| 6078 | case Intrinsic::x86_sse_ucomilt_ss: |
| 6079 | case Intrinsic::x86_sse_ucomile_ss: |
| 6080 | case Intrinsic::x86_sse_ucomigt_ss: |
| 6081 | case Intrinsic::x86_sse_ucomige_ss: |
| 6082 | case Intrinsic::x86_sse_ucomineq_ss: |
| 6083 | case Intrinsic::x86_sse2_comieq_sd: |
| 6084 | case Intrinsic::x86_sse2_comilt_sd: |
| 6085 | case Intrinsic::x86_sse2_comile_sd: |
| 6086 | case Intrinsic::x86_sse2_comigt_sd: |
| 6087 | case Intrinsic::x86_sse2_comige_sd: |
| 6088 | case Intrinsic::x86_sse2_comineq_sd: |
| 6089 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 6090 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 6091 | case Intrinsic::x86_sse2_ucomile_sd: |
| 6092 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 6093 | case Intrinsic::x86_sse2_ucomige_sd: |
| 6094 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 6095 | unsigned Opc = 0; |
| 6096 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 6097 | switch (IntNo) { |
| 6098 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 6099 | case Intrinsic::x86_sse_comieq_ss: |
| 6100 | case Intrinsic::x86_sse2_comieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6101 | Opc = X86ISD::COMI; |
| 6102 | CC = ISD::SETEQ; |
| 6103 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6104 | case Intrinsic::x86_sse_comilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6105 | case Intrinsic::x86_sse2_comilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6106 | Opc = X86ISD::COMI; |
| 6107 | CC = ISD::SETLT; |
| 6108 | break; |
| 6109 | case Intrinsic::x86_sse_comile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6110 | case Intrinsic::x86_sse2_comile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6111 | Opc = X86ISD::COMI; |
| 6112 | CC = ISD::SETLE; |
| 6113 | break; |
| 6114 | case Intrinsic::x86_sse_comigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6115 | case Intrinsic::x86_sse2_comigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6116 | Opc = X86ISD::COMI; |
| 6117 | CC = ISD::SETGT; |
| 6118 | break; |
| 6119 | case Intrinsic::x86_sse_comige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6120 | case Intrinsic::x86_sse2_comige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6121 | Opc = X86ISD::COMI; |
| 6122 | CC = ISD::SETGE; |
| 6123 | break; |
| 6124 | case Intrinsic::x86_sse_comineq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6125 | case Intrinsic::x86_sse2_comineq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6126 | Opc = X86ISD::COMI; |
| 6127 | CC = ISD::SETNE; |
| 6128 | break; |
| 6129 | case Intrinsic::x86_sse_ucomieq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6130 | case Intrinsic::x86_sse2_ucomieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6131 | Opc = X86ISD::UCOMI; |
| 6132 | CC = ISD::SETEQ; |
| 6133 | break; |
| 6134 | case Intrinsic::x86_sse_ucomilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6135 | case Intrinsic::x86_sse2_ucomilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6136 | Opc = X86ISD::UCOMI; |
| 6137 | CC = ISD::SETLT; |
| 6138 | break; |
| 6139 | case Intrinsic::x86_sse_ucomile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6140 | case Intrinsic::x86_sse2_ucomile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6141 | Opc = X86ISD::UCOMI; |
| 6142 | CC = ISD::SETLE; |
| 6143 | break; |
| 6144 | case Intrinsic::x86_sse_ucomigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6145 | case Intrinsic::x86_sse2_ucomigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6146 | Opc = X86ISD::UCOMI; |
| 6147 | CC = ISD::SETGT; |
| 6148 | break; |
| 6149 | case Intrinsic::x86_sse_ucomige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6150 | case Intrinsic::x86_sse2_ucomige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6151 | Opc = X86ISD::UCOMI; |
| 6152 | CC = ISD::SETGE; |
| 6153 | break; |
| 6154 | case Intrinsic::x86_sse_ucomineq_ss: |
| 6155 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 6156 | Opc = X86ISD::UCOMI; |
| 6157 | CC = ISD::SETNE; |
| 6158 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6159 | } |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 6160 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6161 | SDValue LHS = Op.getOperand(1); |
| 6162 | SDValue RHS = Op.getOperand(2); |
Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 6163 | unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6164 | SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS); |
| 6165 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
Evan Cheng | 0ac3fc2 | 2008-08-17 19:22:34 +0000 | [diff] [blame] | 6166 | DAG.getConstant(X86CC, MVT::i8), Cond); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6167 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6168 | } |
Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 6169 | // ptest intrinsics. The intrinsic these come from are designed to return |
Eric Christopher | 794bfed | 2009-07-29 01:01:19 +0000 | [diff] [blame] | 6170 | // an integer value, not just an instruction so lower it to the ptest |
| 6171 | // pattern and a setcc for the result. |
Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 6172 | case Intrinsic::x86_sse41_ptestz: |
| 6173 | case Intrinsic::x86_sse41_ptestc: |
| 6174 | case Intrinsic::x86_sse41_ptestnzc:{ |
| 6175 | unsigned X86CC = 0; |
| 6176 | switch (IntNo) { |
Eric Christopher | 978dae3 | 2009-07-29 18:14:04 +0000 | [diff] [blame] | 6177 | default: llvm_unreachable("Bad fallthrough in Intrinsic lowering."); |
Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 6178 | case Intrinsic::x86_sse41_ptestz: |
| 6179 | // ZF = 1 |
| 6180 | X86CC = X86::COND_E; |
| 6181 | break; |
| 6182 | case Intrinsic::x86_sse41_ptestc: |
| 6183 | // CF = 1 |
| 6184 | X86CC = X86::COND_B; |
| 6185 | break; |
| 6186 | case Intrinsic::x86_sse41_ptestnzc: |
| 6187 | // ZF and CF = 0 |
| 6188 | X86CC = X86::COND_A; |
| 6189 | break; |
| 6190 | } |
| 6191 | |
| 6192 | SDValue LHS = Op.getOperand(1); |
| 6193 | SDValue RHS = Op.getOperand(2); |
| 6194 | SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS); |
| 6195 | SDValue CC = DAG.getConstant(X86CC, MVT::i8); |
| 6196 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test); |
| 6197 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
| 6198 | } |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Fix vector shift instructions where the last operand is a non-immediate |
| 6201 | // i32 value. |
| 6202 | case Intrinsic::x86_sse2_pslli_w: |
| 6203 | case Intrinsic::x86_sse2_pslli_d: |
| 6204 | case Intrinsic::x86_sse2_pslli_q: |
| 6205 | case Intrinsic::x86_sse2_psrli_w: |
| 6206 | case Intrinsic::x86_sse2_psrli_d: |
| 6207 | case Intrinsic::x86_sse2_psrli_q: |
| 6208 | case Intrinsic::x86_sse2_psrai_w: |
| 6209 | case Intrinsic::x86_sse2_psrai_d: |
| 6210 | case Intrinsic::x86_mmx_pslli_w: |
| 6211 | case Intrinsic::x86_mmx_pslli_d: |
| 6212 | case Intrinsic::x86_mmx_pslli_q: |
| 6213 | case Intrinsic::x86_mmx_psrli_w: |
| 6214 | case Intrinsic::x86_mmx_psrli_d: |
| 6215 | case Intrinsic::x86_mmx_psrli_q: |
| 6216 | case Intrinsic::x86_mmx_psrai_w: |
| 6217 | case Intrinsic::x86_mmx_psrai_d: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6218 | SDValue ShAmt = Op.getOperand(2); |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6219 | if (isa<ConstantSDNode>(ShAmt)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6220 | return SDValue(); |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6221 | |
| 6222 | unsigned NewIntNo = 0; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6223 | MVT ShAmtVT = MVT::v4i32; |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6224 | switch (IntNo) { |
| 6225 | case Intrinsic::x86_sse2_pslli_w: |
| 6226 | NewIntNo = Intrinsic::x86_sse2_psll_w; |
| 6227 | break; |
| 6228 | case Intrinsic::x86_sse2_pslli_d: |
| 6229 | NewIntNo = Intrinsic::x86_sse2_psll_d; |
| 6230 | break; |
| 6231 | case Intrinsic::x86_sse2_pslli_q: |
| 6232 | NewIntNo = Intrinsic::x86_sse2_psll_q; |
| 6233 | break; |
| 6234 | case Intrinsic::x86_sse2_psrli_w: |
| 6235 | NewIntNo = Intrinsic::x86_sse2_psrl_w; |
| 6236 | break; |
| 6237 | case Intrinsic::x86_sse2_psrli_d: |
| 6238 | NewIntNo = Intrinsic::x86_sse2_psrl_d; |
| 6239 | break; |
| 6240 | case Intrinsic::x86_sse2_psrli_q: |
| 6241 | NewIntNo = Intrinsic::x86_sse2_psrl_q; |
| 6242 | break; |
| 6243 | case Intrinsic::x86_sse2_psrai_w: |
| 6244 | NewIntNo = Intrinsic::x86_sse2_psra_w; |
| 6245 | break; |
| 6246 | case Intrinsic::x86_sse2_psrai_d: |
| 6247 | NewIntNo = Intrinsic::x86_sse2_psra_d; |
| 6248 | break; |
| 6249 | default: { |
| 6250 | ShAmtVT = MVT::v2i32; |
| 6251 | switch (IntNo) { |
| 6252 | case Intrinsic::x86_mmx_pslli_w: |
| 6253 | NewIntNo = Intrinsic::x86_mmx_psll_w; |
| 6254 | break; |
| 6255 | case Intrinsic::x86_mmx_pslli_d: |
| 6256 | NewIntNo = Intrinsic::x86_mmx_psll_d; |
| 6257 | break; |
| 6258 | case Intrinsic::x86_mmx_pslli_q: |
| 6259 | NewIntNo = Intrinsic::x86_mmx_psll_q; |
| 6260 | break; |
| 6261 | case Intrinsic::x86_mmx_psrli_w: |
| 6262 | NewIntNo = Intrinsic::x86_mmx_psrl_w; |
| 6263 | break; |
| 6264 | case Intrinsic::x86_mmx_psrli_d: |
| 6265 | NewIntNo = Intrinsic::x86_mmx_psrl_d; |
| 6266 | break; |
| 6267 | case Intrinsic::x86_mmx_psrli_q: |
| 6268 | NewIntNo = Intrinsic::x86_mmx_psrl_q; |
| 6269 | break; |
| 6270 | case Intrinsic::x86_mmx_psrai_w: |
| 6271 | NewIntNo = Intrinsic::x86_mmx_psra_w; |
| 6272 | break; |
| 6273 | case Intrinsic::x86_mmx_psrai_d: |
| 6274 | NewIntNo = Intrinsic::x86_mmx_psra_d; |
| 6275 | break; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6276 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6277 | } |
| 6278 | break; |
| 6279 | } |
| 6280 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6281 | MVT VT = Op.getValueType(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6282 | ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, |
| 6283 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt)); |
| 6284 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 6285 | DAG.getConstant(NewIntNo, MVT::i32), |
| 6286 | Op.getOperand(1), ShAmt); |
| 6287 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 6288 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 6289 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6290 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6291 | SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) { |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6292 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6293 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6294 | |
| 6295 | if (Depth > 0) { |
| 6296 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); |
| 6297 | SDValue Offset = |
| 6298 | DAG.getConstant(TD->getPointerSize(), |
| 6299 | Subtarget->is64Bit() ? MVT::i64 : MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6300 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6301 | DAG.getNode(ISD::ADD, dl, getPointerTy(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6302 | FrameAddr, Offset), |
Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 6303 | NULL, 0); |
| 6304 | } |
| 6305 | |
| 6306 | // Just load the return address. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6307 | SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6308 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6309 | RetAddrFI, NULL, 0); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6312 | SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6313 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 6314 | MFI->setFrameAddressIsTaken(true); |
| 6315 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6316 | DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6317 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 6318 | unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP; |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6319 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6320 | while (Depth--) |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6321 | FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0); |
Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 6322 | return FrameAddr; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6325 | SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op, |
Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 6326 | SelectionDAG &DAG) { |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6327 | return DAG.getIntPtrConstant(2*TD->getPointerSize()); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6330 | SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6331 | { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6332 | MachineFunction &MF = DAG.getMachineFunction(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6333 | SDValue Chain = Op.getOperand(0); |
| 6334 | SDValue Offset = Op.getOperand(1); |
| 6335 | SDValue Handler = Op.getOperand(2); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6336 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6337 | |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6338 | SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP, |
| 6339 | getPointerTy()); |
| 6340 | unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6341 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6342 | SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame, |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6343 | DAG.getIntPtrConstant(-TD->getPointerSize())); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6344 | StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset); |
| 6345 | Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6346 | Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr); |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6347 | MF.getRegInfo().addLiveOut(StoreAddrReg); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6348 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6349 | return DAG.getNode(X86ISD::EH_RETURN, dl, |
Anton Korobeynikov | b84c167 | 2008-09-08 21:12:47 +0000 | [diff] [blame] | 6350 | MVT::Other, |
| 6351 | Chain, DAG.getRegister(StoreAddrReg, getPointerTy())); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6354 | SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op, |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6355 | SelectionDAG &DAG) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6356 | SDValue Root = Op.getOperand(0); |
| 6357 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 6358 | SDValue FPtr = Op.getOperand(2); // nested function |
| 6359 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6360 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6361 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6362 | const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6363 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6364 | const X86InstrInfo *TII = |
| 6365 | ((X86TargetMachine&)getTargetMachine()).getInstrInfo(); |
| 6366 | |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6367 | if (Subtarget->is64Bit()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6368 | SDValue OutChains[6]; |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6369 | |
| 6370 | // Large code-model. |
| 6371 | |
| 6372 | const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r); |
| 6373 | const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri); |
| 6374 | |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 6375 | const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10); |
| 6376 | const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6377 | |
| 6378 | const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix |
| 6379 | |
| 6380 | // Load the pointer to the nested function into R11. |
| 6381 | unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6382 | SDValue Addr = Trmp; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6383 | OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6384 | Addr, TrmpAddr, 0); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6385 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6386 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6387 | DAG.getConstant(2, MVT::i64)); |
| 6388 | OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6389 | |
| 6390 | // Load the 'nest' parameter value into R10. |
| 6391 | // R10 is specified in X86CallingConv.td |
| 6392 | OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6393 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6394 | DAG.getConstant(10, MVT::i64)); |
| 6395 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6396 | Addr, TrmpAddr, 10); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6397 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6398 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6399 | DAG.getConstant(12, MVT::i64)); |
| 6400 | OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6401 | |
| 6402 | // Jump to the nested function. |
| 6403 | OpCode = (JMP64r << 8) | REX_WB; // jmpq *... |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6404 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6405 | DAG.getConstant(20, MVT::i64)); |
| 6406 | OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| 6407 | Addr, TrmpAddr, 20); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6408 | |
| 6409 | unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6410 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6411 | DAG.getConstant(22, MVT::i64)); |
| 6412 | OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6413 | TrmpAddr, 22); |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6414 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6415 | SDValue Ops[] = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6416 | { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) }; |
| 6417 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6418 | } else { |
Dan Gohman | bbfb9c5 | 2008-01-31 01:01:48 +0000 | [diff] [blame] | 6419 | const Function *Func = |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6420 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); |
| 6421 | unsigned CC = Func->getCallingConv(); |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6422 | unsigned NestReg; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6423 | |
| 6424 | switch (CC) { |
| 6425 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6426 | llvm_unreachable("Unsupported calling convention"); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6427 | case CallingConv::C: |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6428 | case CallingConv::X86_StdCall: { |
| 6429 | // Pass 'nest' parameter in ECX. |
| 6430 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6431 | NestReg = X86::ECX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6432 | |
| 6433 | // Check that ECX wasn't needed by an 'inreg' parameter. |
| 6434 | const FunctionType *FTy = Func->getFunctionType(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6435 | const AttrListPtr &Attrs = Func->getAttributes(); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6436 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 6437 | if (!Attrs.isEmpty() && !Func->isVarArg()) { |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6438 | unsigned InRegCount = 0; |
| 6439 | unsigned Idx = 1; |
| 6440 | |
| 6441 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 6442 | E = FTy->param_end(); I != E; ++I, ++Idx) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 6443 | if (Attrs.paramHasAttr(Idx, Attribute::InReg)) |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6444 | // FIXME: should only count parameters that are lowered to integers. |
Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 6445 | InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6446 | |
| 6447 | if (InRegCount > 2) { |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 6448 | llvm_report_error("Nest register in use - reduce number of inreg parameters!"); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6449 | } |
| 6450 | } |
| 6451 | break; |
| 6452 | } |
| 6453 | case CallingConv::X86_FastCall: |
Duncan Sands | bf53c29 | 2008-09-10 13:22:10 +0000 | [diff] [blame] | 6454 | case CallingConv::Fast: |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6455 | // Pass 'nest' parameter in EAX. |
| 6456 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 6457 | NestReg = X86::EAX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6458 | break; |
| 6459 | } |
| 6460 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6461 | SDValue OutChains[4]; |
| 6462 | SDValue Addr, Disp; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6463 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6464 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6465 | DAG.getConstant(10, MVT::i32)); |
| 6466 | Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6467 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6468 | const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri); |
Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 6469 | const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6470 | OutChains[0] = DAG.getStore(Root, dl, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6471 | DAG.getConstant(MOV32ri|N86Reg, MVT::i8), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6472 | Trmp, TrmpAddr, 0); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6473 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6474 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6475 | DAG.getConstant(1, MVT::i32)); |
| 6476 | OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6477 | |
Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 6478 | const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6479 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6480 | DAG.getConstant(5, MVT::i32)); |
| 6481 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6482 | TrmpAddr, 5, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6483 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6484 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6485 | DAG.getConstant(6, MVT::i32)); |
| 6486 | OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6487 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6488 | SDValue Ops[] = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6489 | { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) }; |
| 6490 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6491 | } |
| 6492 | } |
| 6493 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6494 | SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) { |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6495 | /* |
| 6496 | The rounding mode is in bits 11:10 of FPSR, and has the following |
| 6497 | settings: |
| 6498 | 00 Round to nearest |
| 6499 | 01 Round to -inf |
| 6500 | 10 Round to +inf |
| 6501 | 11 Round to 0 |
| 6502 | |
| 6503 | FLT_ROUNDS, on the other hand, expects the following: |
| 6504 | -1 Undefined |
| 6505 | 0 Round to 0 |
| 6506 | 1 Round to nearest |
| 6507 | 2 Round to +inf |
| 6508 | 3 Round to -inf |
| 6509 | |
| 6510 | To perform the conversion, we do: |
| 6511 | (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3) |
| 6512 | */ |
| 6513 | |
| 6514 | MachineFunction &MF = DAG.getMachineFunction(); |
| 6515 | const TargetMachine &TM = MF.getTarget(); |
| 6516 | const TargetFrameInfo &TFI = *TM.getFrameInfo(); |
| 6517 | unsigned StackAlignment = TFI.getStackAlignment(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6518 | MVT VT = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6519 | DebugLoc dl = Op.getDebugLoc(); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6520 | |
| 6521 | // Save FP Control Word to stack slot |
| 6522 | int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6523 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6524 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6525 | SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other, |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 6526 | DAG.getEntryNode(), StackSlot); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6527 | |
| 6528 | // Load FP Control Word from stack slot |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6529 | SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6530 | |
| 6531 | // Transform as necessary |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6532 | SDValue CWD1 = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6533 | DAG.getNode(ISD::SRL, dl, MVT::i16, |
| 6534 | DAG.getNode(ISD::AND, dl, MVT::i16, |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6535 | CWD, DAG.getConstant(0x800, MVT::i16)), |
| 6536 | DAG.getConstant(11, MVT::i8)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6537 | SDValue CWD2 = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6538 | DAG.getNode(ISD::SRL, dl, MVT::i16, |
| 6539 | DAG.getNode(ISD::AND, dl, MVT::i16, |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6540 | CWD, DAG.getConstant(0x400, MVT::i16)), |
| 6541 | DAG.getConstant(9, MVT::i8)); |
| 6542 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6543 | SDValue RetVal = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6544 | DAG.getNode(ISD::AND, dl, MVT::i16, |
| 6545 | DAG.getNode(ISD::ADD, dl, MVT::i16, |
| 6546 | DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2), |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6547 | DAG.getConstant(1, MVT::i16)), |
| 6548 | DAG.getConstant(3, MVT::i16)); |
| 6549 | |
| 6550 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6551 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 6552 | ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6555 | SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6556 | MVT VT = Op.getValueType(); |
| 6557 | MVT OpVT = VT; |
| 6558 | unsigned NumBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6559 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6560 | |
| 6561 | Op = Op.getOperand(0); |
| 6562 | if (VT == MVT::i8) { |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6563 | // Zero extend to i32 since there is not an i8 bsr. |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6564 | OpVT = MVT::i32; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6565 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6566 | } |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6567 | |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6568 | // Issue a bsr (scan bits in reverse) which also sets EFLAGS. |
| 6569 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6570 | Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6571 | |
| 6572 | // If src is zero (i.e. bsr sets ZF), returns NumBits. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6573 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6574 | Ops.push_back(Op); |
| 6575 | Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT)); |
| 6576 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 6577 | Ops.push_back(Op.getValue(1)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6578 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6579 | |
| 6580 | // Finally xor with NumBits-1. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6581 | 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] | 6582 | |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6583 | if (VT == MVT::i8) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6584 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6585 | return Op; |
| 6586 | } |
| 6587 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6588 | SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6589 | MVT VT = Op.getValueType(); |
| 6590 | MVT OpVT = VT; |
| 6591 | unsigned NumBits = VT.getSizeInBits(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6592 | DebugLoc dl = Op.getDebugLoc(); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6593 | |
| 6594 | Op = Op.getOperand(0); |
| 6595 | if (VT == MVT::i8) { |
| 6596 | OpVT = MVT::i32; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6597 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6598 | } |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6599 | |
| 6600 | // Issue a bsf (scan bits forward) which also sets EFLAGS. |
| 6601 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6602 | Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6603 | |
| 6604 | // If src is zero (i.e. bsf sets ZF), returns NumBits. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6605 | SmallVector<SDValue, 4> Ops; |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6606 | Ops.push_back(Op); |
| 6607 | Ops.push_back(DAG.getConstant(NumBits, OpVT)); |
| 6608 | Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8)); |
| 6609 | Ops.push_back(Op.getValue(1)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6610 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4); |
Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 6611 | |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6612 | if (VT == MVT::i8) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6613 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6614 | return Op; |
| 6615 | } |
| 6616 | |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6617 | SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) { |
| 6618 | MVT VT = Op.getValueType(); |
| 6619 | assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply"); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6620 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6621 | |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6622 | // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32); |
| 6623 | // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32); |
| 6624 | // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b ); |
| 6625 | // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi ); |
| 6626 | // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b ); |
| 6627 | // |
| 6628 | // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 ); |
| 6629 | // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 ); |
| 6630 | // return AloBlo + AloBhi + AhiBlo; |
| 6631 | |
| 6632 | SDValue A = Op.getOperand(0); |
| 6633 | SDValue B = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6634 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6635 | SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6636 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 6637 | A, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6638 | SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6639 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 6640 | B, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6641 | SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6642 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6643 | A, B); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6644 | SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6645 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6646 | A, Bhi); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6647 | SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6648 | DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32), |
| 6649 | Ahi, B); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6650 | AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6651 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 6652 | AloBhi, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6653 | AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6654 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 6655 | AhiBlo, DAG.getConstant(32, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6656 | SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi); |
| 6657 | Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6658 | return Res; |
| 6659 | } |
| 6660 | |
| 6661 | |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6662 | SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) { |
| 6663 | // Lower the "add/sub/mul with overflow" instruction into a regular ins plus |
| 6664 | // a "setcc" instruction that checks the overflow flag. The "brcond" lowering |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6665 | // looks for this combo and may remove the "setcc" instruction if the "setcc" |
| 6666 | // has only one use. |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6667 | SDNode *N = Op.getNode(); |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6668 | SDValue LHS = N->getOperand(0); |
| 6669 | SDValue RHS = N->getOperand(1); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6670 | unsigned BaseOp = 0; |
| 6671 | unsigned Cond = 0; |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6672 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6673 | |
| 6674 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6675 | default: llvm_unreachable("Unknown ovf instruction!"); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6676 | case ISD::SADDO: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 6677 | // A subtract of one will be selected as a INC. Note that INC doesn't |
| 6678 | // set CF, so we can't do this for UADDO. |
| 6679 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) |
| 6680 | if (C->getAPIntValue() == 1) { |
| 6681 | BaseOp = X86ISD::INC; |
| 6682 | Cond = X86::COND_O; |
| 6683 | break; |
| 6684 | } |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6685 | BaseOp = X86ISD::ADD; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6686 | Cond = X86::COND_O; |
| 6687 | break; |
| 6688 | case ISD::UADDO: |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6689 | BaseOp = X86ISD::ADD; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6690 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6691 | break; |
| 6692 | case ISD::SSUBO: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 6693 | // A subtract of one will be selected as a DEC. Note that DEC doesn't |
| 6694 | // set CF, so we can't do this for USUBO. |
| 6695 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) |
| 6696 | if (C->getAPIntValue() == 1) { |
| 6697 | BaseOp = X86ISD::DEC; |
| 6698 | Cond = X86::COND_O; |
| 6699 | break; |
| 6700 | } |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6701 | BaseOp = X86ISD::SUB; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6702 | Cond = X86::COND_O; |
| 6703 | break; |
| 6704 | case ISD::USUBO: |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 6705 | BaseOp = X86ISD::SUB; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6706 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6707 | break; |
| 6708 | case ISD::SMULO: |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 6709 | BaseOp = X86ISD::SMUL; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6710 | Cond = X86::COND_O; |
| 6711 | break; |
| 6712 | case ISD::UMULO: |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 6713 | BaseOp = X86ISD::UMUL; |
Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 6714 | Cond = X86::COND_B; |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6715 | break; |
| 6716 | } |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6717 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6718 | // Also sets EFLAGS. |
| 6719 | SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6720 | SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS); |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6721 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6722 | SDValue SetCC = |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6723 | DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1), |
Bill Wendling | bc5e15e | 2008-12-10 02:01:32 +0000 | [diff] [blame] | 6724 | DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1)); |
Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 6725 | |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6726 | DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC); |
| 6727 | return Sum; |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6730 | SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) { |
Dan Gohman | fd4418f | 2008-06-25 16:07:49 +0000 | [diff] [blame] | 6731 | MVT T = Op.getValueType(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6732 | DebugLoc dl = Op.getDebugLoc(); |
Andrew Lenharth | a76e2f0 | 2008-03-04 21:13:33 +0000 | [diff] [blame] | 6733 | unsigned Reg = 0; |
| 6734 | unsigned size = 0; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6735 | switch(T.getSimpleVT()) { |
| 6736 | default: |
| 6737 | assert(false && "Invalid value type!"); |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6738 | case MVT::i8: Reg = X86::AL; size = 1; break; |
| 6739 | case MVT::i16: Reg = X86::AX; size = 2; break; |
| 6740 | case MVT::i32: Reg = X86::EAX; size = 4; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6741 | case MVT::i64: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6742 | assert(Subtarget->is64Bit() && "Node not type legal!"); |
| 6743 | Reg = X86::RAX; size = 8; |
Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 6744 | break; |
Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 6745 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6746 | SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg, |
Dale Johannesen | d18a462 | 2008-09-11 03:12:59 +0000 | [diff] [blame] | 6747 | Op.getOperand(2), SDValue()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6748 | SDValue Ops[] = { cpIn.getValue(0), |
Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 6749 | Op.getOperand(1), |
| 6750 | Op.getOperand(3), |
| 6751 | DAG.getTargetConstant(size, MVT::i8), |
| 6752 | cpIn.getValue(1) }; |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6753 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6754 | SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6755 | SDValue cpOut = |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6756 | DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1)); |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 6757 | return cpOut; |
| 6758 | } |
| 6759 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6760 | SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op, |
Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 6761 | SelectionDAG &DAG) { |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6762 | assert(Subtarget->is64Bit() && "Result not type legalized?"); |
Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 6763 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6764 | SDValue TheChain = Op.getOperand(0); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 6765 | DebugLoc dl = Op.getDebugLoc(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6766 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6767 | SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1)); |
| 6768 | SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6769 | rax.getValue(2)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6770 | SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6771 | DAG.getConstant(32, MVT::i8)); |
| 6772 | SDValue Ops[] = { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6773 | DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6774 | rdx.getValue(1) |
| 6775 | }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6776 | return DAG.getMergeValues(Ops, 2, dl); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6779 | SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) { |
| 6780 | SDNode *Node = Op.getNode(); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6781 | DebugLoc dl = Node->getDebugLoc(); |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6782 | MVT T = Node->getValueType(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6783 | SDValue negOp = DAG.getNode(ISD::SUB, dl, T, |
Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 6784 | DAG.getConstant(0, T), Node->getOperand(2)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6785 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl, |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6786 | cast<AtomicSDNode>(Node)->getMemoryVT(), |
Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 6787 | Node->getOperand(0), |
| 6788 | Node->getOperand(1), negOp, |
| 6789 | cast<AtomicSDNode>(Node)->getSrcValue(), |
| 6790 | cast<AtomicSDNode>(Node)->getAlignment()); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6793 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 6794 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6795 | SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6796 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6797 | default: llvm_unreachable("Should not custom lower this!"); |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6798 | case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG); |
| 6799 | case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6800 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 6801 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 6802 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 6803 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 6804 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 6805 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 6806 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 6807 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 6808 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6809 | case ISD::SHL_PARTS: |
| 6810 | case ISD::SRA_PARTS: |
| 6811 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 6812 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 6813 | case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6814 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 6815 | case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6816 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 6817 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6818 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 6819 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 6820 | case ISD::VSETCC: return LowerVSETCC(Op, DAG); |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 6821 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 6822 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6823 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6824 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 6825 | case ISD::VAARG: return LowerVAARG(Op, DAG); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 6826 | case ISD::VACOPY: return LowerVACOPY(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6827 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 6828 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 6829 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6830 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 6831 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 6832 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 6833 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 6834 | case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 6835 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6836 | case ISD::CTLZ: return LowerCTLZ(Op, DAG); |
| 6837 | case ISD::CTTZ: return LowerCTTZ(Op, DAG); |
Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 6838 | case ISD::MUL: return LowerMUL_V2I64(Op, DAG); |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 6839 | case ISD::SADDO: |
| 6840 | case ISD::UADDO: |
| 6841 | case ISD::SSUBO: |
| 6842 | case ISD::USUBO: |
| 6843 | case ISD::SMULO: |
| 6844 | case ISD::UMULO: return LowerXALUO(Op, DAG); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6845 | case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6846 | } |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6849 | void X86TargetLowering:: |
| 6850 | ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results, |
| 6851 | SelectionDAG &DAG, unsigned NewOp) { |
| 6852 | MVT T = Node->getValueType(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6853 | DebugLoc dl = Node->getDebugLoc(); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6854 | assert (T == MVT::i64 && "Only know how to expand i64 atomics"); |
| 6855 | |
| 6856 | SDValue Chain = Node->getOperand(0); |
| 6857 | SDValue In1 = Node->getOperand(1); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6858 | SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6859 | Node->getOperand(2), DAG.getIntPtrConstant(0)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6860 | SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6861 | Node->getOperand(2), DAG.getIntPtrConstant(1)); |
| 6862 | // This is a generalized SDNode, not an AtomicSDNode, so it doesn't |
| 6863 | // have a MemOperand. Pass the info through as a normal operand. |
| 6864 | SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand()); |
| 6865 | SDValue Ops[] = { Chain, In1, In2L, In2H, LSI }; |
| 6866 | SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6867 | SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6868 | SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)}; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6869 | 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] | 6870 | Results.push_back(Result.getValue(2)); |
| 6871 | } |
| 6872 | |
Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 6873 | /// ReplaceNodeResults - Replace a node with an illegal result type |
| 6874 | /// with a new node built out of custom code. |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6875 | void X86TargetLowering::ReplaceNodeResults(SDNode *N, |
| 6876 | SmallVectorImpl<SDValue>&Results, |
| 6877 | SelectionDAG &DAG) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6878 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6879 | switch (N->getOpcode()) { |
Duncan Sands | ed294c4 | 2008-10-20 15:56:33 +0000 | [diff] [blame] | 6880 | default: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6881 | assert(false && "Do not know how to custom type legalize this operation!"); |
| 6882 | return; |
| 6883 | case ISD::FP_TO_SINT: { |
Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 6884 | std::pair<SDValue,SDValue> Vals = |
| 6885 | FP_TO_INTHelper(SDValue(N, 0), DAG, true); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6886 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| 6887 | if (FIST.getNode() != 0) { |
| 6888 | MVT VT = N->getValueType(0); |
| 6889 | // Return a load from the stack slot. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6890 | Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6891 | } |
| 6892 | return; |
| 6893 | } |
| 6894 | case ISD::READCYCLECOUNTER: { |
| 6895 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 6896 | SDValue TheChain = N->getOperand(0); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6897 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6898 | SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32, |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6899 | rd.getValue(1)); |
| 6900 | SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6901 | eax.getValue(2)); |
| 6902 | // Use a buildpair to merge the two 32-bit values into a 64-bit one. |
| 6903 | SDValue Ops[] = { eax, edx }; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6904 | 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] | 6905 | Results.push_back(edx.getValue(1)); |
| 6906 | return; |
| 6907 | } |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6908 | case ISD::ATOMIC_CMP_SWAP: { |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6909 | MVT T = N->getValueType(0); |
| 6910 | assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap"); |
| 6911 | SDValue cpInL, cpInH; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6912 | cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6913 | DAG.getConstant(0, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6914 | cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6915 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6916 | cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue()); |
| 6917 | cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6918 | cpInL.getValue(1)); |
| 6919 | SDValue swapInL, swapInH; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6920 | swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6921 | DAG.getConstant(0, MVT::i32)); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6922 | swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6923 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6924 | swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6925 | cpInH.getValue(1)); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6926 | swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6927 | swapInL.getValue(1)); |
| 6928 | SDValue Ops[] = { swapInH.getValue(0), |
| 6929 | N->getOperand(1), |
| 6930 | swapInH.getValue(1) }; |
| 6931 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6932 | SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 6933 | SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX, |
| 6934 | MVT::i32, Result.getValue(1)); |
| 6935 | SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX, |
| 6936 | MVT::i32, cpOutL.getValue(2)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6937 | SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)}; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 6938 | 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] | 6939 | Results.push_back(cpOutH.getValue(1)); |
| 6940 | return; |
| 6941 | } |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6942 | case ISD::ATOMIC_LOAD_ADD: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6943 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG); |
| 6944 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6945 | case ISD::ATOMIC_LOAD_AND: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6946 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG); |
| 6947 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6948 | case ISD::ATOMIC_LOAD_NAND: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6949 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG); |
| 6950 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6951 | case ISD::ATOMIC_LOAD_OR: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6952 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG); |
| 6953 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6954 | case ISD::ATOMIC_LOAD_SUB: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6955 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG); |
| 6956 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6957 | case ISD::ATOMIC_LOAD_XOR: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6958 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG); |
| 6959 | return; |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 6960 | case ISD::ATOMIC_SWAP: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 6961 | ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG); |
| 6962 | return; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6963 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6966 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 6967 | switch (Opcode) { |
| 6968 | default: return NULL; |
Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 6969 | case X86ISD::BSF: return "X86ISD::BSF"; |
| 6970 | case X86ISD::BSR: return "X86ISD::BSR"; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 6971 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 6972 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 6973 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6974 | case X86ISD::FOR: return "X86ISD::FOR"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 6975 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 6976 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 6977 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 6978 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6979 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 6980 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 6981 | 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] | 6982 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 6983 | case X86ISD::FST: return "X86ISD::FST"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6984 | case X86ISD::CALL: return "X86ISD::CALL"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6985 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
Dan Gohman | c7a37d4 | 2008-12-23 22:45:23 +0000 | [diff] [blame] | 6986 | case X86ISD::BT: return "X86ISD::BT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6987 | case X86ISD::CMP: return "X86ISD::CMP"; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 6988 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 6989 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 6990 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 6991 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 6992 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 6993 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 6994 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 6995 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 6996 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 6997 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 6998 | case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP"; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 6999 | case X86ISD::PEXTRB: return "X86ISD::PEXTRB"; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 7000 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7001 | case X86ISD::INSERTPS: return "X86ISD::INSERTPS"; |
| 7002 | case X86ISD::PINSRB: return "X86ISD::PINSRB"; |
Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 7003 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 7004 | case X86ISD::PSHUFB: return "X86ISD::PSHUFB"; |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 7005 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 7006 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 7007 | case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; |
| 7008 | case X86ISD::FRCP: return "X86ISD::FRCP"; |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7009 | case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7010 | case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress"; |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 7011 | case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 7012 | case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN"; |
Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 7013 | case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m"; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7014 | case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG"; |
| 7015 | case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG"; |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7016 | case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG"; |
| 7017 | case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG"; |
| 7018 | case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG"; |
| 7019 | case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG"; |
| 7020 | case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG"; |
| 7021 | case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG"; |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7022 | case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL"; |
| 7023 | case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD"; |
Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 7024 | case X86ISD::VSHL: return "X86ISD::VSHL"; |
| 7025 | case X86ISD::VSRL: return "X86ISD::VSRL"; |
Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 7026 | case X86ISD::CMPPD: return "X86ISD::CMPPD"; |
| 7027 | case X86ISD::CMPPS: return "X86ISD::CMPPS"; |
| 7028 | case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB"; |
| 7029 | case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW"; |
| 7030 | case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD"; |
| 7031 | case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ"; |
| 7032 | case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB"; |
| 7033 | case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW"; |
| 7034 | case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD"; |
| 7035 | case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ"; |
Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 7036 | case X86ISD::ADD: return "X86ISD::ADD"; |
| 7037 | case X86ISD::SUB: return "X86ISD::SUB"; |
Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 7038 | case X86ISD::SMUL: return "X86ISD::SMUL"; |
| 7039 | case X86ISD::UMUL: return "X86ISD::UMUL"; |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 7040 | case X86ISD::INC: return "X86ISD::INC"; |
| 7041 | case X86ISD::DEC: return "X86ISD::DEC"; |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 7042 | case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM"; |
Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 7043 | case X86ISD::PTEST: return "X86ISD::PTEST"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 7044 | } |
| 7045 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7046 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7047 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 7048 | // 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] | 7049 | bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7050 | const Type *Ty) const { |
| 7051 | // X86 supports extremely general addressing modes. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7052 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7053 | // X86 allows a sign-extended 32-bit immediate field as a displacement. |
| 7054 | if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1) |
| 7055 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7056 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7057 | if (AM.BaseGV) { |
Chris Lattner | dfed413 | 2009-07-10 07:38:24 +0000 | [diff] [blame] | 7058 | unsigned GVFlags = |
| 7059 | Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine()); |
| 7060 | |
| 7061 | // If a reference to this global requires an extra load, we can't fold it. |
| 7062 | if (isGlobalStubReference(GVFlags)) |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7063 | return false; |
Chris Lattner | dfed413 | 2009-07-10 07:38:24 +0000 | [diff] [blame] | 7064 | |
| 7065 | // If BaseGV requires a register for the PIC base, we cannot also have a |
| 7066 | // BaseReg specified. |
| 7067 | if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags)) |
Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 7068 | return false; |
Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 7069 | |
| 7070 | // X86-64 only supports addr of globals in small code model. |
| 7071 | if (Subtarget->is64Bit()) { |
| 7072 | if (getTargetMachine().getCodeModel() != CodeModel::Small) |
| 7073 | return false; |
| 7074 | // If lower 4G is not available, then we must use rip-relative addressing. |
| 7075 | if (AM.BaseOffs || AM.Scale > 1) |
| 7076 | return false; |
| 7077 | } |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7078 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7079 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7080 | switch (AM.Scale) { |
| 7081 | case 0: |
| 7082 | case 1: |
| 7083 | case 2: |
| 7084 | case 4: |
| 7085 | case 8: |
| 7086 | // These scales always work. |
| 7087 | break; |
| 7088 | case 3: |
| 7089 | case 5: |
| 7090 | case 9: |
| 7091 | // These scales are formed with basereg+scalereg. Only accept if there is |
| 7092 | // no basereg yet. |
| 7093 | if (AM.HasBaseReg) |
| 7094 | return false; |
| 7095 | break; |
| 7096 | default: // Other stuff never works. |
| 7097 | return false; |
| 7098 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7099 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7100 | return true; |
| 7101 | } |
| 7102 | |
| 7103 | |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 7104 | bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const { |
| 7105 | if (!Ty1->isInteger() || !Ty2->isInteger()) |
| 7106 | return false; |
Evan Cheng | e127a73 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 7107 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); |
| 7108 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 7109 | if (NumBits1 <= NumBits2) |
Evan Cheng | e127a73 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 7110 | return false; |
| 7111 | return Subtarget->is64Bit() || NumBits1 < 64; |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7114 | bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const { |
| 7115 | if (!VT1.isInteger() || !VT2.isInteger()) |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 7116 | return false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7117 | unsigned NumBits1 = VT1.getSizeInBits(); |
| 7118 | unsigned NumBits2 = VT2.getSizeInBits(); |
Evan Cheng | 260e07e | 2008-03-20 02:18:41 +0000 | [diff] [blame] | 7119 | if (NumBits1 <= NumBits2) |
Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 7120 | return false; |
| 7121 | return Subtarget->is64Bit() || NumBits1 < 64; |
| 7122 | } |
Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 7123 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 7124 | bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const { |
Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 7125 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 7126 | return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit(); |
| 7127 | } |
| 7128 | |
| 7129 | bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const { |
Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 7130 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 7131 | return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit(); |
| 7132 | } |
| 7133 | |
Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 7134 | bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const { |
| 7135 | // i16 instructions are longer (0x66 prefix) and potentially slower. |
| 7136 | return !(VT1 == MVT::i32 && VT2 == MVT::i16); |
| 7137 | } |
| 7138 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7139 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 7140 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 7141 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 7142 | /// are assumed to be legal. |
| 7143 | bool |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7144 | X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, |
| 7145 | MVT VT) const { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7146 | // Only do shuffles on 128-bit vector types for now. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7147 | if (VT.getSizeInBits() == 64) |
| 7148 | return false; |
| 7149 | |
| 7150 | // FIXME: pshufb, blends, palignr, shifts. |
| 7151 | return (VT.getVectorNumElements() == 2 || |
| 7152 | ShuffleVectorSDNode::isSplatMask(&M[0], VT) || |
| 7153 | isMOVLMask(M, VT) || |
| 7154 | isSHUFPMask(M, VT) || |
| 7155 | isPSHUFDMask(M, VT) || |
| 7156 | isPSHUFHWMask(M, VT) || |
| 7157 | isPSHUFLWMask(M, VT) || |
| 7158 | isUNPCKLMask(M, VT) || |
| 7159 | isUNPCKHMask(M, VT) || |
| 7160 | isUNPCKL_v_undef_Mask(M, VT) || |
| 7161 | isUNPCKH_v_undef_Mask(M, VT)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Dan Gohman | 7d8143f | 2008-04-09 20:09:42 +0000 | [diff] [blame] | 7164 | bool |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 7165 | X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7166 | MVT VT) const { |
| 7167 | unsigned NumElts = VT.getVectorNumElements(); |
| 7168 | // FIXME: This collection of masks seems suspect. |
| 7169 | if (NumElts == 2) |
| 7170 | return true; |
| 7171 | if (NumElts == 4 && VT.getSizeInBits() == 128) { |
| 7172 | return (isMOVLMask(Mask, VT) || |
| 7173 | isCommutedMOVLMask(Mask, VT, true) || |
| 7174 | isSHUFPMask(Mask, VT) || |
| 7175 | isCommutedSHUFPMask(Mask, VT)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7176 | } |
| 7177 | return false; |
| 7178 | } |
| 7179 | |
| 7180 | //===----------------------------------------------------------------------===// |
| 7181 | // X86 Scheduler Hooks |
| 7182 | //===----------------------------------------------------------------------===// |
| 7183 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7184 | // private utility function |
| 7185 | MachineBasicBlock * |
| 7186 | X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr, |
| 7187 | MachineBasicBlock *MBB, |
| 7188 | unsigned regOpc, |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7189 | unsigned immOpc, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7190 | unsigned LoadOpc, |
| 7191 | unsigned CXchgOpc, |
| 7192 | unsigned copyOpc, |
| 7193 | unsigned notOpc, |
| 7194 | unsigned EAXreg, |
| 7195 | TargetRegisterClass *RC, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7196 | bool invSrc) const { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7197 | // For the atomic bitwise operator, we generate |
| 7198 | // thisMBB: |
| 7199 | // newMBB: |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7200 | // ld t1 = [bitinstr.addr] |
| 7201 | // op t2 = t1, [bitinstr.val] |
| 7202 | // mov EAX = t1 |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7203 | // lcs dest = [bitinstr.addr], t2 [EAX is implicit] |
| 7204 | // bz newMBB |
| 7205 | // fallthrough -->nextMBB |
| 7206 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 7207 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7208 | MachineFunction::iterator MBBIter = MBB; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7209 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7210 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7211 | /// First build the CFG |
| 7212 | MachineFunction *F = MBB->getParent(); |
| 7213 | MachineBasicBlock *thisMBB = MBB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7214 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7215 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7216 | F->insert(MBBIter, newMBB); |
| 7217 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7218 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7219 | // Move all successors to thisMBB to nextMBB |
| 7220 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7221 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7222 | // Update thisMBB to fall through to newMBB |
| 7223 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7224 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7225 | // newMBB jumps to itself and fall through to nextMBB |
| 7226 | newMBB->addSuccessor(nextMBB); |
| 7227 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7228 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7229 | // Insert instructions into newMBB based on incoming instruction |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7230 | assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 && |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7231 | "unexpected number of operands"); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7232 | DebugLoc dl = bInstr->getDebugLoc(); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7233 | MachineOperand& destOper = bInstr->getOperand(0); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7234 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7235 | int numArgs = bInstr->getNumOperands() - 1; |
| 7236 | for (int i=0; i < numArgs; ++i) |
| 7237 | argOpers[i] = &bInstr->getOperand(i+1); |
| 7238 | |
| 7239 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7240 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
| 7241 | int valArgIndx = lastAddrIndx + 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7242 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7243 | unsigned t1 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7244 | MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7245 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7246 | (*MIB).addOperand(*argOpers[i]); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7247 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7248 | unsigned tt = F->getRegInfo().createVirtualRegister(RC); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7249 | if (invSrc) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7250 | MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7251 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7252 | else |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7253 | tt = t1; |
| 7254 | |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7255 | unsigned t2 = F->getRegInfo().createVirtualRegister(RC); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7256 | assert((argOpers[valArgIndx]->isReg() || |
| 7257 | argOpers[valArgIndx]->isImm()) && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 7258 | "invalid operand"); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7259 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7260 | MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7261 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7262 | MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7263 | MIB.addReg(tt); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7264 | (*MIB).addOperand(*argOpers[valArgIndx]); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7265 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7266 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7267 | MIB.addReg(t1); |
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 | MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7270 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7271 | (*MIB).addOperand(*argOpers[i]); |
| 7272 | MIB.addReg(t2); |
Mon P Wang | f595266 | 2008-07-17 04:54:06 +0000 | [diff] [blame] | 7273 | assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7274 | (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); |
| 7275 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7276 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg()); |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7277 | MIB.addReg(EAXreg); |
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 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7280 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7281 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7282 | F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7283 | return nextMBB; |
| 7284 | } |
| 7285 | |
Dale Johannesen | 1b54c7f | 2008-10-03 19:41:08 +0000 | [diff] [blame] | 7286 | // private utility function: 64 bit atomics on 32 bit host. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7287 | MachineBasicBlock * |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7288 | X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr, |
| 7289 | MachineBasicBlock *MBB, |
| 7290 | unsigned regOpcL, |
| 7291 | unsigned regOpcH, |
| 7292 | unsigned immOpcL, |
| 7293 | unsigned immOpcH, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7294 | bool invSrc) const { |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7295 | // For the atomic bitwise operator, we generate |
| 7296 | // thisMBB (instructions are in pairs, except cmpxchg8b) |
| 7297 | // ld t1,t2 = [bitinstr.addr] |
| 7298 | // newMBB: |
| 7299 | // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4) |
| 7300 | // op t5, t6 <- out1, out2, [bitinstr.val] |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7301 | // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val]) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7302 | // mov ECX, EBX <- t5, t6 |
| 7303 | // mov EAX, EDX <- t1, t2 |
| 7304 | // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit] |
| 7305 | // mov t3, t4 <- EAX, EDX |
| 7306 | // bz newMBB |
| 7307 | // result in out1, out2 |
| 7308 | // fallthrough -->nextMBB |
| 7309 | |
| 7310 | const TargetRegisterClass *RC = X86::GR32RegisterClass; |
| 7311 | const unsigned LoadOpc = X86::MOV32rm; |
| 7312 | const unsigned copyOpc = X86::MOV32rr; |
| 7313 | const unsigned NotOpc = X86::NOT32r; |
| 7314 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 7315 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 7316 | MachineFunction::iterator MBBIter = MBB; |
| 7317 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7318 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7319 | /// First build the CFG |
| 7320 | MachineFunction *F = MBB->getParent(); |
| 7321 | MachineBasicBlock *thisMBB = MBB; |
| 7322 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7323 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7324 | F->insert(MBBIter, newMBB); |
| 7325 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7326 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7327 | // Move all successors to thisMBB to nextMBB |
| 7328 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7329 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7330 | // Update thisMBB to fall through to newMBB |
| 7331 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7332 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7333 | // newMBB jumps to itself and fall through to nextMBB |
| 7334 | newMBB->addSuccessor(nextMBB); |
| 7335 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7336 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7337 | DebugLoc dl = bInstr->getDebugLoc(); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7338 | // Insert instructions into newMBB based on incoming instruction |
| 7339 | // There are 8 "real" operands plus 9 implicit def/uses, ignored here. |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7340 | assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 && |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7341 | "unexpected number of operands"); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7342 | MachineOperand& dest1Oper = bInstr->getOperand(0); |
| 7343 | MachineOperand& dest2Oper = bInstr->getOperand(1); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7344 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
| 7345 | for (int i=0; i < 2 + X86AddrNumOperands; ++i) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7346 | argOpers[i] = &bInstr->getOperand(i+2); |
| 7347 | |
| 7348 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7349 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7350 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7351 | unsigned t1 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7352 | MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7353 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7354 | (*MIB).addOperand(*argOpers[i]); |
| 7355 | unsigned t2 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7356 | MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7357 | // add 4 to displacement. |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7358 | for (int i=0; i <= lastAddrIndx-2; ++i) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7359 | (*MIB).addOperand(*argOpers[i]); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7360 | MachineOperand newOp3 = *(argOpers[3]); |
| 7361 | if (newOp3.isImm()) |
| 7362 | newOp3.setImm(newOp3.getImm()+4); |
| 7363 | else |
| 7364 | newOp3.setOffset(newOp3.getOffset()+4); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7365 | (*MIB).addOperand(newOp3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7366 | (*MIB).addOperand(*argOpers[lastAddrIndx]); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7367 | |
| 7368 | // t3/4 are defined later, at the bottom of the loop |
| 7369 | unsigned t3 = F->getRegInfo().createVirtualRegister(RC); |
| 7370 | unsigned t4 = F->getRegInfo().createVirtualRegister(RC); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7371 | BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg()) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7372 | .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7373 | BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg()) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7374 | .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB); |
| 7375 | |
| 7376 | unsigned tt1 = F->getRegInfo().createVirtualRegister(RC); |
| 7377 | unsigned tt2 = F->getRegInfo().createVirtualRegister(RC); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7378 | if (invSrc) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7379 | MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1); |
| 7380 | MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7381 | } else { |
| 7382 | tt1 = t1; |
| 7383 | tt2 = t2; |
| 7384 | } |
| 7385 | |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7386 | int valArgIndx = lastAddrIndx + 1; |
| 7387 | assert((argOpers[valArgIndx]->isReg() || |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7388 | argOpers[valArgIndx]->isImm()) && |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7389 | "invalid operand"); |
| 7390 | unsigned t5 = F->getRegInfo().createVirtualRegister(RC); |
| 7391 | unsigned t6 = F->getRegInfo().createVirtualRegister(RC); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7392 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7393 | MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7394 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7395 | MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7396 | if (regOpcL != X86::MOV32rr) |
| 7397 | MIB.addReg(tt1); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7398 | (*MIB).addOperand(*argOpers[valArgIndx]); |
| 7399 | assert(argOpers[valArgIndx + 1]->isReg() == |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7400 | argOpers[valArgIndx]->isReg()); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7401 | assert(argOpers[valArgIndx + 1]->isImm() == |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7402 | argOpers[valArgIndx]->isImm()); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7403 | if (argOpers[valArgIndx + 1]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7404 | MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7405 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7406 | MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7407 | if (regOpcH != X86::MOV32rr) |
| 7408 | MIB.addReg(tt2); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7409 | (*MIB).addOperand(*argOpers[valArgIndx + 1]); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7410 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7411 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7412 | MIB.addReg(t1); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7413 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7414 | MIB.addReg(t2); |
| 7415 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7416 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7417 | MIB.addReg(t5); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7418 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7419 | MIB.addReg(t6); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7420 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7421 | MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B)); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7422 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7423 | (*MIB).addOperand(*argOpers[i]); |
| 7424 | |
| 7425 | assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7426 | (*MIB).addMemOperand(*F, *bInstr->memoperands_begin()); |
| 7427 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7428 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7429 | MIB.addReg(X86::EAX); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7430 | MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7431 | MIB.addReg(X86::EDX); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7432 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7433 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7434 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7435 | |
| 7436 | F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now. |
| 7437 | return nextMBB; |
| 7438 | } |
| 7439 | |
| 7440 | // private utility function |
| 7441 | MachineBasicBlock * |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7442 | X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr, |
| 7443 | MachineBasicBlock *MBB, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7444 | unsigned cmovOpc) const { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7445 | // For the atomic min/max operator, we generate |
| 7446 | // thisMBB: |
| 7447 | // newMBB: |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7448 | // ld t1 = [min/max.addr] |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7449 | // mov t2 = [min/max.val] |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7450 | // cmp t1, t2 |
| 7451 | // cmov[cond] t2 = t1 |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7452 | // mov EAX = t1 |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7453 | // lcs dest = [bitinstr.addr], t2 [EAX is implicit] |
| 7454 | // bz newMBB |
| 7455 | // fallthrough -->nextMBB |
| 7456 | // |
| 7457 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 7458 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7459 | MachineFunction::iterator MBBIter = MBB; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7460 | ++MBBIter; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7461 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7462 | /// First build the CFG |
| 7463 | MachineFunction *F = MBB->getParent(); |
| 7464 | MachineBasicBlock *thisMBB = MBB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7465 | MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7466 | MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7467 | F->insert(MBBIter, newMBB); |
| 7468 | F->insert(MBBIter, nextMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7469 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7470 | // Move all successors to thisMBB to nextMBB |
| 7471 | nextMBB->transferSuccessors(thisMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7472 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7473 | // Update thisMBB to fall through to newMBB |
| 7474 | thisMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7475 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7476 | // newMBB jumps to newMBB and fall through to nextMBB |
| 7477 | newMBB->addSuccessor(nextMBB); |
| 7478 | newMBB->addSuccessor(newMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7479 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7480 | DebugLoc dl = mInstr->getDebugLoc(); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7481 | // Insert instructions into newMBB based on incoming instruction |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7482 | assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 && |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 7483 | "unexpected number of operands"); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7484 | MachineOperand& destOper = mInstr->getOperand(0); |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7485 | MachineOperand* argOpers[2 + X86AddrNumOperands]; |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7486 | int numArgs = mInstr->getNumOperands() - 1; |
| 7487 | for (int i=0; i < numArgs; ++i) |
| 7488 | argOpers[i] = &mInstr->getOperand(i+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7489 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7490 | // x86 address has 4 operands: base, index, scale, and displacement |
Rafael Espindola | a82dfca | 2009-03-27 15:26:30 +0000 | [diff] [blame] | 7491 | int lastAddrIndx = X86AddrNumOperands - 1; // [0,3] |
| 7492 | int valArgIndx = lastAddrIndx + 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7493 | |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7494 | unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7495 | MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7496 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7497 | (*MIB).addOperand(*argOpers[i]); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7498 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7499 | // We only support register and immediate values |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7500 | assert((argOpers[valArgIndx]->isReg() || |
| 7501 | argOpers[valArgIndx]->isImm()) && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 7502 | "invalid operand"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7503 | |
| 7504 | unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7505 | if (argOpers[valArgIndx]->isReg()) |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7506 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7507 | else |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7508 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7509 | (*MIB).addOperand(*argOpers[valArgIndx]); |
| 7510 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7511 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX); |
Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 7512 | MIB.addReg(t1); |
| 7513 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7514 | MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7515 | MIB.addReg(t1); |
| 7516 | MIB.addReg(t2); |
| 7517 | |
| 7518 | // Generate movc |
| 7519 | unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7520 | MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7521 | MIB.addReg(t2); |
| 7522 | MIB.addReg(t1); |
| 7523 | |
| 7524 | // Cmp and exchange if none has modified the memory location |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7525 | MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32)); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7526 | for (int i=0; i <= lastAddrIndx; ++i) |
| 7527 | (*MIB).addOperand(*argOpers[i]); |
| 7528 | MIB.addReg(t3); |
Mon P Wang | f595266 | 2008-07-17 04:54:06 +0000 | [diff] [blame] | 7529 | assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand"); |
| 7530 | (*MIB).addMemOperand(*F, *mInstr->memoperands_begin()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7531 | |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7532 | MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg()); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7533 | MIB.addReg(X86::EAX); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7534 | |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7535 | // insert branch |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7536 | BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7537 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7538 | F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now. |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7539 | return nextMBB; |
| 7540 | } |
| 7541 | |
| 7542 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7543 | MachineBasicBlock * |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 7544 | X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 7545 | MachineBasicBlock *BB) const { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7546 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7547 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7548 | switch (MI->getOpcode()) { |
| 7549 | default: assert(false && "Unexpected instr type to insert"); |
Mon P Wang | 9e5ecb8 | 2008-12-12 01:25:51 +0000 | [diff] [blame] | 7550 | case X86::CMOV_V1I64: |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7551 | case X86::CMOV_FR32: |
| 7552 | case X86::CMOV_FR64: |
| 7553 | case X86::CMOV_V4F32: |
| 7554 | case X86::CMOV_V2F64: |
Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 7555 | case X86::CMOV_V2I64: { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7556 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 7557 | // diamond control-flow pattern. The incoming instruction knows the |
| 7558 | // destination vreg to set, the condition code register to branch on, the |
| 7559 | // true/false values to select between, and a branch opcode to use. |
| 7560 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7561 | MachineFunction::iterator It = BB; |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7562 | ++It; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7563 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7564 | // thisMBB: |
| 7565 | // ... |
| 7566 | // TrueVal = ... |
| 7567 | // cmpTY ccX, r1, r2 |
| 7568 | // bCC copy1MBB |
| 7569 | // fallthrough --> copy0MBB |
| 7570 | MachineBasicBlock *thisMBB = BB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7571 | MachineFunction *F = BB->getParent(); |
| 7572 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 7573 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7574 | unsigned Opc = |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7575 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7576 | BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7577 | F->insert(It, copy0MBB); |
| 7578 | F->insert(It, sinkMBB); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7579 | // Update machine-CFG edges by transferring all successors of the current |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7580 | // 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] | 7581 | sinkMBB->transferSuccessors(BB); |
| 7582 | |
| 7583 | // Add the true and fallthrough blocks as its successors. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7584 | BB->addSuccessor(copy0MBB); |
| 7585 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7586 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7587 | // copy0MBB: |
| 7588 | // %FalseValue = ... |
| 7589 | // # fallthrough to sinkMBB |
| 7590 | BB = copy0MBB; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7591 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7592 | // Update machine-CFG edges |
| 7593 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7594 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7595 | // sinkMBB: |
| 7596 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 7597 | // ... |
| 7598 | BB = sinkMBB; |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7599 | BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7600 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 7601 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 7602 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7603 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7604 | return BB; |
| 7605 | } |
| 7606 | |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 7607 | case X86::FP32_TO_INT16_IN_MEM: |
| 7608 | case X86::FP32_TO_INT32_IN_MEM: |
| 7609 | case X86::FP32_TO_INT64_IN_MEM: |
| 7610 | case X86::FP64_TO_INT16_IN_MEM: |
| 7611 | case X86::FP64_TO_INT32_IN_MEM: |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 7612 | case X86::FP64_TO_INT64_IN_MEM: |
| 7613 | case X86::FP80_TO_INT16_IN_MEM: |
| 7614 | case X86::FP80_TO_INT32_IN_MEM: |
| 7615 | case X86::FP80_TO_INT64_IN_MEM: { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7616 | // Change the floating point control register to use "round towards zero" |
| 7617 | // mode when truncating to an integer value. |
| 7618 | MachineFunction *F = BB->getParent(); |
| 7619 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7620 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7621 | |
| 7622 | // Load the old value of the high byte of the control word... |
| 7623 | unsigned OldCW = |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 7624 | F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7625 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW), |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7626 | CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7627 | |
| 7628 | // Set the high part to be round to zero... |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7629 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7630 | .addImm(0xC7F); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7631 | |
| 7632 | // Reload the modified control word now... |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7633 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7634 | |
| 7635 | // Restore the memory image of control word to original value |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7636 | addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 7637 | .addReg(OldCW); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7638 | |
| 7639 | // Get the X86 opcode to use. |
| 7640 | unsigned Opc; |
| 7641 | switch (MI->getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 7642 | default: llvm_unreachable("illegal opcode!"); |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 7643 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; |
| 7644 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; |
| 7645 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; |
| 7646 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; |
| 7647 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; |
| 7648 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 7649 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; |
| 7650 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; |
| 7651 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | X86AddressMode AM; |
| 7655 | MachineOperand &Op = MI->getOperand(0); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7656 | if (Op.isReg()) { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7657 | AM.BaseType = X86AddressMode::RegBase; |
| 7658 | AM.Base.Reg = Op.getReg(); |
| 7659 | } else { |
| 7660 | AM.BaseType = X86AddressMode::FrameIndexBase; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 7661 | AM.Base.FrameIndex = Op.getIndex(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7662 | } |
| 7663 | Op = MI->getOperand(1); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7664 | if (Op.isImm()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7665 | AM.Scale = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7666 | Op = MI->getOperand(2); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7667 | if (Op.isImm()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7668 | AM.IndexReg = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7669 | Op = MI->getOperand(3); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 7670 | if (Op.isGlobal()) { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7671 | AM.GV = Op.getGlobal(); |
| 7672 | } else { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 7673 | AM.Disp = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7674 | } |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7675 | addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM) |
Rafael Espindola | 8ef2b89 | 2009-04-08 08:09:33 +0000 | [diff] [blame] | 7676 | .addReg(MI->getOperand(X86AddrNumOperands).getReg()); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7677 | |
| 7678 | // Reload the original control word now. |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7679 | addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7680 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 7681 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7682 | return BB; |
| 7683 | } |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7684 | case X86::ATOMAND32: |
| 7685 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7686 | X86::AND32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7687 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7688 | X86::NOT32r, X86::EAX, |
| 7689 | X86::GR32RegisterClass); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7690 | case X86::ATOMOR32: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7691 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr, |
| 7692 | X86::OR32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7693 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7694 | X86::NOT32r, X86::EAX, |
| 7695 | X86::GR32RegisterClass); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7696 | case X86::ATOMXOR32: |
| 7697 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7698 | X86::XOR32ri, X86::MOV32rm, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7699 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7700 | X86::NOT32r, X86::EAX, |
| 7701 | X86::GR32RegisterClass); |
Andrew Lenharth | 507a58a | 2008-06-14 05:48:15 +0000 | [diff] [blame] | 7702 | case X86::ATOMNAND32: |
| 7703 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7704 | X86::AND32ri, X86::MOV32rm, |
| 7705 | X86::LCMPXCHG32, X86::MOV32rr, |
| 7706 | X86::NOT32r, X86::EAX, |
| 7707 | X86::GR32RegisterClass, true); |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 7708 | case X86::ATOMMIN32: |
| 7709 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr); |
| 7710 | case X86::ATOMMAX32: |
| 7711 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr); |
| 7712 | case X86::ATOMUMIN32: |
| 7713 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr); |
| 7714 | case X86::ATOMUMAX32: |
| 7715 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr); |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7716 | |
| 7717 | case X86::ATOMAND16: |
| 7718 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, |
| 7719 | X86::AND16ri, X86::MOV16rm, |
| 7720 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7721 | X86::NOT16r, X86::AX, |
| 7722 | X86::GR16RegisterClass); |
| 7723 | case X86::ATOMOR16: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7724 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7725 | X86::OR16ri, X86::MOV16rm, |
| 7726 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7727 | X86::NOT16r, X86::AX, |
| 7728 | X86::GR16RegisterClass); |
| 7729 | case X86::ATOMXOR16: |
| 7730 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr, |
| 7731 | X86::XOR16ri, X86::MOV16rm, |
| 7732 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7733 | X86::NOT16r, X86::AX, |
| 7734 | X86::GR16RegisterClass); |
| 7735 | case X86::ATOMNAND16: |
| 7736 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr, |
| 7737 | X86::AND16ri, X86::MOV16rm, |
| 7738 | X86::LCMPXCHG16, X86::MOV16rr, |
| 7739 | X86::NOT16r, X86::AX, |
| 7740 | X86::GR16RegisterClass, true); |
| 7741 | case X86::ATOMMIN16: |
| 7742 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr); |
| 7743 | case X86::ATOMMAX16: |
| 7744 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr); |
| 7745 | case X86::ATOMUMIN16: |
| 7746 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr); |
| 7747 | case X86::ATOMUMAX16: |
| 7748 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr); |
| 7749 | |
| 7750 | case X86::ATOMAND8: |
| 7751 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, |
| 7752 | X86::AND8ri, X86::MOV8rm, |
| 7753 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7754 | X86::NOT8r, X86::AL, |
| 7755 | X86::GR8RegisterClass); |
| 7756 | case X86::ATOMOR8: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7757 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr, |
Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 7758 | X86::OR8ri, X86::MOV8rm, |
| 7759 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7760 | X86::NOT8r, X86::AL, |
| 7761 | X86::GR8RegisterClass); |
| 7762 | case X86::ATOMXOR8: |
| 7763 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr, |
| 7764 | X86::XOR8ri, X86::MOV8rm, |
| 7765 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7766 | X86::NOT8r, X86::AL, |
| 7767 | X86::GR8RegisterClass); |
| 7768 | case X86::ATOMNAND8: |
| 7769 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr, |
| 7770 | X86::AND8ri, X86::MOV8rm, |
| 7771 | X86::LCMPXCHG8, X86::MOV8rr, |
| 7772 | X86::NOT8r, X86::AL, |
| 7773 | X86::GR8RegisterClass, true); |
| 7774 | // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way. |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7775 | // This group is for 64-bit host. |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7776 | case X86::ATOMAND64: |
| 7777 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7778 | X86::AND64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7779 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7780 | X86::NOT64r, X86::RAX, |
| 7781 | X86::GR64RegisterClass); |
| 7782 | case X86::ATOMOR64: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7783 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr, |
| 7784 | X86::OR64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7785 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7786 | X86::NOT64r, X86::RAX, |
| 7787 | X86::GR64RegisterClass); |
| 7788 | case X86::ATOMXOR64: |
| 7789 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7790 | X86::XOR64ri32, X86::MOV64rm, |
Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 7791 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7792 | X86::NOT64r, X86::RAX, |
| 7793 | X86::GR64RegisterClass); |
| 7794 | case X86::ATOMNAND64: |
| 7795 | return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr, |
| 7796 | X86::AND64ri32, X86::MOV64rm, |
| 7797 | X86::LCMPXCHG64, X86::MOV64rr, |
| 7798 | X86::NOT64r, X86::RAX, |
| 7799 | X86::GR64RegisterClass, true); |
| 7800 | case X86::ATOMMIN64: |
| 7801 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr); |
| 7802 | case X86::ATOMMAX64: |
| 7803 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr); |
| 7804 | case X86::ATOMUMIN64: |
| 7805 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr); |
| 7806 | case X86::ATOMUMAX64: |
| 7807 | return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7808 | |
| 7809 | // This group does 64-bit operations on a 32-bit host. |
| 7810 | case X86::ATOMAND6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7811 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7812 | X86::AND32rr, X86::AND32rr, |
| 7813 | X86::AND32ri, X86::AND32ri, |
| 7814 | false); |
| 7815 | case X86::ATOMOR6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7816 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7817 | X86::OR32rr, X86::OR32rr, |
| 7818 | X86::OR32ri, X86::OR32ri, |
| 7819 | false); |
| 7820 | case X86::ATOMXOR6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7821 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7822 | X86::XOR32rr, X86::XOR32rr, |
| 7823 | X86::XOR32ri, X86::XOR32ri, |
| 7824 | false); |
| 7825 | case X86::ATOMNAND6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7826 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7827 | X86::AND32rr, X86::AND32rr, |
| 7828 | X86::AND32ri, X86::AND32ri, |
| 7829 | true); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7830 | case X86::ATOMADD6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7831 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7832 | X86::ADD32rr, X86::ADC32rr, |
| 7833 | X86::ADD32ri, X86::ADC32ri, |
| 7834 | false); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7835 | case X86::ATOMSUB6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7836 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 7837 | X86::SUB32rr, X86::SBB32rr, |
| 7838 | X86::SUB32ri, X86::SBB32ri, |
| 7839 | false); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7840 | case X86::ATOMSWAP6432: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7841 | return EmitAtomicBit6432WithCustomInserter(MI, BB, |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 7842 | X86::MOV32rr, X86::MOV32rr, |
| 7843 | X86::MOV32ri, X86::MOV32ri, |
| 7844 | false); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 7845 | } |
| 7846 | } |
| 7847 | |
| 7848 | //===----------------------------------------------------------------------===// |
| 7849 | // X86 Optimization Hooks |
| 7850 | //===----------------------------------------------------------------------===// |
| 7851 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7852 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 7853 | const APInt &Mask, |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 7854 | APInt &KnownZero, |
| 7855 | APInt &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 7856 | const SelectionDAG &DAG, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 7857 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7858 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 7859 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 7860 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 7861 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 7862 | Opc == ISD::INTRINSIC_VOID) && |
| 7863 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 7864 | " is a target node!"); |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7865 | |
Dan Gohman | f4f92f5 | 2008-02-13 23:07:24 +0000 | [diff] [blame] | 7866 | KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7867 | switch (Opc) { |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 7868 | default: break; |
Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 7869 | case X86ISD::ADD: |
| 7870 | case X86ISD::SUB: |
| 7871 | case X86ISD::SMUL: |
| 7872 | case X86ISD::UMUL: |
Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 7873 | case X86ISD::INC: |
| 7874 | case X86ISD::DEC: |
Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 7875 | // These nodes' second result is a boolean. |
| 7876 | if (Op.getResNo() == 0) |
| 7877 | break; |
| 7878 | // Fallthrough |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 7879 | case X86ISD::SETCC: |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 7880 | KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(), |
| 7881 | Mask.getBitWidth() - 1); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 7882 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7883 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 7884 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 7885 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7886 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7887 | /// node is a GlobalAddress + offset. |
| 7888 | bool X86TargetLowering::isGAPlusOffset(SDNode *N, |
| 7889 | GlobalValue* &GA, int64_t &Offset) const{ |
| 7890 | if (N->getOpcode() == X86ISD::Wrapper) { |
| 7891 | if (isa<GlobalAddressSDNode>(N->getOperand(0))) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7892 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7893 | Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7894 | return true; |
| 7895 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7896 | } |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7897 | return TargetLowering::isGAPlusOffset(N, GA, Offset); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7900 | static bool isBaseAlignmentOfN(unsigned N, SDNode *Base, |
| 7901 | const TargetLowering &TLI) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7902 | GlobalValue *GV; |
Nick Lewycky | 916a9f0 | 2008-02-02 08:29:58 +0000 | [diff] [blame] | 7903 | int64_t Offset = 0; |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7904 | if (TLI.isGAPlusOffset(Base, GV, Offset)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7905 | return (GV->getAlignment() >= N && (Offset % N) == 0); |
Chris Lattner | ba96fbc | 2008-01-26 20:07:42 +0000 | [diff] [blame] | 7906 | // DAG combine handles the stack object case. |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7907 | return false; |
| 7908 | } |
| 7909 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7910 | static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems, |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7911 | MVT EVT, LoadSDNode *&LDBase, |
| 7912 | unsigned &LastLoadedElt, |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 7913 | SelectionDAG &DAG, MachineFrameInfo *MFI, |
| 7914 | const TargetLowering &TLI) { |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7915 | LDBase = NULL; |
Anton Korobeynikov | b51b6cf | 2009-06-09 23:00:39 +0000 | [diff] [blame] | 7916 | LastLoadedElt = -1U; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7917 | for (unsigned i = 0; i < NumElems; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7918 | if (N->getMaskElt(i) < 0) { |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7919 | if (!LDBase) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7920 | return false; |
| 7921 | continue; |
| 7922 | } |
| 7923 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7924 | SDValue Elt = DAG.getShuffleScalarElt(N, i); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7925 | if (!Elt.getNode() || |
| 7926 | (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode()))) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7927 | return false; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7928 | if (!LDBase) { |
| 7929 | if (Elt.getNode()->getOpcode() == ISD::UNDEF) |
Evan Cheng | 50d9e72 | 2008-05-10 06:46:49 +0000 | [diff] [blame] | 7930 | return false; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7931 | LDBase = cast<LoadSDNode>(Elt.getNode()); |
| 7932 | LastLoadedElt = i; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7933 | continue; |
| 7934 | } |
| 7935 | if (Elt.getOpcode() == ISD::UNDEF) |
| 7936 | continue; |
| 7937 | |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 7938 | LoadSDNode *LD = cast<LoadSDNode>(Elt); |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 7939 | if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7940 | return false; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7941 | LastLoadedElt = i; |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 7942 | } |
| 7943 | return true; |
| 7944 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7945 | |
| 7946 | /// PerformShuffleCombine - Combine a vector_shuffle that is equal to |
| 7947 | /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load |
| 7948 | /// 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] | 7949 | /// order. In the case of v2i64, it will see if it can rewrite the |
| 7950 | /// shuffle to be an appropriate build vector so it can take advantage of |
| 7951 | // performBuildVectorCombine. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7952 | static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7953 | const TargetLowering &TLI) { |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7954 | DebugLoc dl = N->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7955 | MVT VT = N->getValueType(0); |
| 7956 | MVT EVT = VT.getVectorElementType(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7957 | ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
| 7958 | unsigned NumElems = VT.getVectorNumElements(); |
Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 7959 | |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7960 | if (VT.getSizeInBits() != 128) |
| 7961 | return SDValue(); |
| 7962 | |
Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 7963 | // Try to combine a vector_shuffle into a 128-bit load. |
| 7964 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7965 | LoadSDNode *LD = NULL; |
| 7966 | unsigned LastLoadedElt; |
| 7967 | if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG, |
| 7968 | MFI, TLI)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7969 | return SDValue(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 7970 | |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7971 | if (LastLoadedElt == NumElems - 1) { |
| 7972 | if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI)) |
| 7973 | return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), |
| 7974 | LD->getSrcValue(), LD->getSrcValueOffset(), |
| 7975 | LD->isVolatile()); |
Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 7976 | return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7977 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 7978 | LD->isVolatile(), LD->getAlignment()); |
| 7979 | } else if (NumElems == 4 && LastLoadedElt == 1) { |
| 7980 | SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other); |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 7981 | SDValue Ops[] = { LD->getChain(), LD->getBasePtr() }; |
| 7982 | SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2); |
Nate Begeman | abc0199 | 2009-06-05 21:37:30 +0000 | [diff] [blame] | 7983 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode); |
| 7984 | } |
| 7985 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7986 | } |
Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 7987 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7988 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7989 | static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7990 | const X86Subtarget *Subtarget) { |
| 7991 | DebugLoc DL = N->getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7992 | SDValue Cond = N->getOperand(0); |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7993 | // Get the LHS/RHS of the select. |
| 7994 | SDValue LHS = N->getOperand(1); |
| 7995 | SDValue RHS = N->getOperand(2); |
| 7996 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 7997 | // If we have SSE[12] support, try to form min/max nodes. |
| 7998 | if (Subtarget->hasSSE2() && |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 7999 | (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) && |
| 8000 | Cond.getOpcode() == ISD::SETCC) { |
| 8001 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8002 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8003 | unsigned Opcode = 0; |
| 8004 | if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { |
| 8005 | switch (CC) { |
| 8006 | default: break; |
| 8007 | case ISD::SETOLE: // (X <= Y) ? X : Y -> min |
| 8008 | case ISD::SETULE: |
| 8009 | case ISD::SETLE: |
| 8010 | if (!UnsafeFPMath) break; |
| 8011 | // FALL THROUGH. |
| 8012 | case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min |
| 8013 | case ISD::SETLT: |
| 8014 | Opcode = X86ISD::FMIN; |
| 8015 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8016 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8017 | case ISD::SETOGT: // (X > Y) ? X : Y -> max |
| 8018 | case ISD::SETUGT: |
| 8019 | case ISD::SETGT: |
| 8020 | if (!UnsafeFPMath) break; |
| 8021 | // FALL THROUGH. |
| 8022 | case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max |
| 8023 | case ISD::SETGE: |
| 8024 | Opcode = X86ISD::FMAX; |
| 8025 | break; |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8026 | } |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8027 | } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { |
| 8028 | switch (CC) { |
| 8029 | default: break; |
| 8030 | case ISD::SETOGT: // (X > Y) ? Y : X -> min |
| 8031 | case ISD::SETUGT: |
| 8032 | case ISD::SETGT: |
| 8033 | if (!UnsafeFPMath) break; |
| 8034 | // FALL THROUGH. |
| 8035 | case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min |
| 8036 | case ISD::SETGE: |
| 8037 | Opcode = X86ISD::FMIN; |
| 8038 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8039 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8040 | case ISD::SETOLE: // (X <= Y) ? Y : X -> max |
| 8041 | case ISD::SETULE: |
| 8042 | case ISD::SETLE: |
| 8043 | if (!UnsafeFPMath) break; |
| 8044 | // FALL THROUGH. |
| 8045 | case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max |
| 8046 | case ISD::SETLT: |
| 8047 | Opcode = X86ISD::FMAX; |
| 8048 | break; |
| 8049 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8050 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8051 | |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8052 | if (Opcode) |
| 8053 | return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8054 | } |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8055 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8056 | // If this is a select between two integer constants, try to do some |
| 8057 | // optimizations. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8058 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) { |
| 8059 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS)) |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8060 | // Don't do this for crazy integer types. |
| 8061 | if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) { |
| 8062 | // If this is efficiently invertible, canonicalize the LHSC/RHSC values |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8063 | // so that TrueC (the true value) is larger than FalseC. |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8064 | bool NeedsCondInvert = false; |
| 8065 | |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8066 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) && |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8067 | // Efficiently invertible. |
| 8068 | (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible. |
| 8069 | (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible. |
| 8070 | isa<ConstantSDNode>(Cond.getOperand(1))))) { |
| 8071 | NeedsCondInvert = true; |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8072 | std::swap(TrueC, FalseC); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8076 | if (FalseC->getAPIntValue() == 0 && |
| 8077 | TrueC->getAPIntValue().isPowerOf2()) { |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8078 | if (NeedsCondInvert) // Invert the condition if needed. |
| 8079 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 8080 | DAG.getConstant(1, Cond.getValueType())); |
| 8081 | |
| 8082 | // Zero extend the condition if needed. |
| 8083 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond); |
| 8084 | |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8085 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8086 | return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond, |
| 8087 | DAG.getConstant(ShAmt, MVT::i8)); |
| 8088 | } |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8089 | |
| 8090 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8091 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8092 | if (NeedsCondInvert) // Invert the condition if needed. |
| 8093 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 8094 | DAG.getConstant(1, Cond.getValueType())); |
| 8095 | |
| 8096 | // Zero extend the condition if needed. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8097 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 8098 | FalseC->getValueType(0), Cond); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8099 | return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8100 | SDValue(FalseC, 0)); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8101 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8102 | |
| 8103 | // Optimize cases that will turn into an LEA instruction. This requires |
| 8104 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| 8105 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| 8106 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| 8107 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| 8108 | |
| 8109 | bool isFastMultiplier = false; |
| 8110 | if (Diff < 10) { |
| 8111 | switch ((unsigned char)Diff) { |
| 8112 | default: break; |
| 8113 | case 1: // result = add base, cond |
| 8114 | case 2: // result = lea base( , cond*2) |
| 8115 | case 3: // result = lea base(cond, cond*2) |
| 8116 | case 4: // result = lea base( , cond*4) |
| 8117 | case 5: // result = lea base(cond, cond*4) |
| 8118 | case 8: // result = lea base( , cond*8) |
| 8119 | case 9: // result = lea base(cond, cond*8) |
| 8120 | isFastMultiplier = true; |
| 8121 | break; |
| 8122 | } |
| 8123 | } |
| 8124 | |
| 8125 | if (isFastMultiplier) { |
| 8126 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| 8127 | if (NeedsCondInvert) // Invert the condition if needed. |
| 8128 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 8129 | DAG.getConstant(1, Cond.getValueType())); |
| 8130 | |
| 8131 | // Zero extend the condition if needed. |
| 8132 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 8133 | Cond); |
| 8134 | // Scale the condition by the difference. |
| 8135 | if (Diff != 1) |
| 8136 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 8137 | DAG.getConstant(Diff, Cond.getValueType())); |
| 8138 | |
| 8139 | // Add the base if non-zero. |
| 8140 | if (FalseC->getAPIntValue() != 0) |
| 8141 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 8142 | SDValue(FalseC, 0)); |
| 8143 | return Cond; |
| 8144 | } |
| 8145 | } |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8146 | } |
| 8147 | } |
| 8148 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8149 | return SDValue(); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8150 | } |
| 8151 | |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8152 | /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL] |
| 8153 | static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG, |
| 8154 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8155 | DebugLoc DL = N->getDebugLoc(); |
| 8156 | |
| 8157 | // If the flag operand isn't dead, don't touch this CMOV. |
| 8158 | if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty()) |
| 8159 | return SDValue(); |
| 8160 | |
| 8161 | // If this is a select between two integer constants, try to do some |
| 8162 | // optimizations. Note that the operands are ordered the opposite of SELECT |
| 8163 | // operands. |
| 8164 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) { |
| 8165 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
| 8166 | // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is |
| 8167 | // larger than FalseC (the false value). |
| 8168 | X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2); |
| 8169 | |
| 8170 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) { |
| 8171 | CC = X86::GetOppositeBranchCondition(CC); |
| 8172 | std::swap(TrueC, FalseC); |
| 8173 | } |
| 8174 | |
| 8175 | // 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] | 8176 | // This is efficient for any integer data type (including i8/i16) and |
| 8177 | // shift amount. |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8178 | if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) { |
| 8179 | SDValue Cond = N->getOperand(3); |
| 8180 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8181 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8182 | |
| 8183 | // Zero extend the condition if needed. |
| 8184 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond); |
| 8185 | |
| 8186 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
| 8187 | Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond, |
| 8188 | DAG.getConstant(ShAmt, MVT::i8)); |
| 8189 | if (N->getNumValues() == 2) // Dead flag value? |
| 8190 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8191 | return Cond; |
| 8192 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8193 | |
| 8194 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient |
| 8195 | // for any integer data type, including i8/i16. |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8196 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
| 8197 | SDValue Cond = N->getOperand(3); |
| 8198 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8199 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8200 | |
| 8201 | // Zero extend the condition if needed. |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8202 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 8203 | FalseC->getValueType(0), Cond); |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8204 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 8205 | SDValue(FalseC, 0)); |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8206 | |
Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 8207 | if (N->getNumValues() == 2) // Dead flag value? |
| 8208 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8209 | return Cond; |
| 8210 | } |
Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 8211 | |
| 8212 | // Optimize cases that will turn into an LEA instruction. This requires |
| 8213 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| 8214 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| 8215 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| 8216 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| 8217 | |
| 8218 | bool isFastMultiplier = false; |
| 8219 | if (Diff < 10) { |
| 8220 | switch ((unsigned char)Diff) { |
| 8221 | default: break; |
| 8222 | case 1: // result = add base, cond |
| 8223 | case 2: // result = lea base( , cond*2) |
| 8224 | case 3: // result = lea base(cond, cond*2) |
| 8225 | case 4: // result = lea base( , cond*4) |
| 8226 | case 5: // result = lea base(cond, cond*4) |
| 8227 | case 8: // result = lea base( , cond*8) |
| 8228 | case 9: // result = lea base(cond, cond*8) |
| 8229 | isFastMultiplier = true; |
| 8230 | break; |
| 8231 | } |
| 8232 | } |
| 8233 | |
| 8234 | if (isFastMultiplier) { |
| 8235 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| 8236 | SDValue Cond = N->getOperand(3); |
| 8237 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 8238 | DAG.getConstant(CC, MVT::i8), Cond); |
| 8239 | // Zero extend the condition if needed. |
| 8240 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 8241 | Cond); |
| 8242 | // Scale the condition by the difference. |
| 8243 | if (Diff != 1) |
| 8244 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 8245 | DAG.getConstant(Diff, Cond.getValueType())); |
| 8246 | |
| 8247 | // Add the base if non-zero. |
| 8248 | if (FalseC->getAPIntValue() != 0) |
| 8249 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 8250 | SDValue(FalseC, 0)); |
| 8251 | if (N->getNumValues() == 2) // Dead flag value? |
| 8252 | return DCI.CombineTo(N, Cond, SDValue()); |
| 8253 | return Cond; |
| 8254 | } |
| 8255 | } |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8256 | } |
| 8257 | } |
| 8258 | return SDValue(); |
| 8259 | } |
| 8260 | |
| 8261 | |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8262 | /// PerformMulCombine - Optimize a single multiply with constant into two |
| 8263 | /// in order to implement it with two cheaper instructions, e.g. |
| 8264 | /// LEA + SHL, LEA + LEA. |
| 8265 | static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG, |
| 8266 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8267 | if (DAG.getMachineFunction(). |
| 8268 | getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
| 8269 | return SDValue(); |
| 8270 | |
| 8271 | if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) |
| 8272 | return SDValue(); |
| 8273 | |
| 8274 | MVT VT = N->getValueType(0); |
| 8275 | if (VT != MVT::i64) |
| 8276 | return SDValue(); |
| 8277 | |
| 8278 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 8279 | if (!C) |
| 8280 | return SDValue(); |
| 8281 | uint64_t MulAmt = C->getZExtValue(); |
| 8282 | if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9) |
| 8283 | return SDValue(); |
| 8284 | |
| 8285 | uint64_t MulAmt1 = 0; |
| 8286 | uint64_t MulAmt2 = 0; |
| 8287 | if ((MulAmt % 9) == 0) { |
| 8288 | MulAmt1 = 9; |
| 8289 | MulAmt2 = MulAmt / 9; |
| 8290 | } else if ((MulAmt % 5) == 0) { |
| 8291 | MulAmt1 = 5; |
| 8292 | MulAmt2 = MulAmt / 5; |
| 8293 | } else if ((MulAmt % 3) == 0) { |
| 8294 | MulAmt1 = 3; |
| 8295 | MulAmt2 = MulAmt / 3; |
| 8296 | } |
| 8297 | if (MulAmt2 && |
| 8298 | (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){ |
| 8299 | DebugLoc DL = N->getDebugLoc(); |
| 8300 | |
| 8301 | if (isPowerOf2_64(MulAmt2) && |
| 8302 | !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD)) |
| 8303 | // If second multiplifer is pow2, issue it first. We want the multiply by |
| 8304 | // 3, 5, or 9 to be folded into the addressing mode unless the lone use |
| 8305 | // is an add. |
| 8306 | std::swap(MulAmt1, MulAmt2); |
| 8307 | |
| 8308 | SDValue NewMul; |
| 8309 | if (isPowerOf2_64(MulAmt1)) |
| 8310 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), |
| 8311 | DAG.getConstant(Log2_64(MulAmt1), MVT::i8)); |
| 8312 | else |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 8313 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8314 | DAG.getConstant(MulAmt1, VT)); |
| 8315 | |
| 8316 | if (isPowerOf2_64(MulAmt2)) |
| 8317 | NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul, |
| 8318 | DAG.getConstant(Log2_64(MulAmt2), MVT::i8)); |
| 8319 | else |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 8320 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul, |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8321 | DAG.getConstant(MulAmt2, VT)); |
| 8322 | |
| 8323 | // Do not add new nodes to DAG combiner worklist. |
| 8324 | DCI.CombineTo(N, NewMul, false); |
| 8325 | } |
| 8326 | return SDValue(); |
| 8327 | } |
| 8328 | |
| 8329 | |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8330 | /// PerformShiftCombine - Transforms vector shift nodes to use vector shifts |
| 8331 | /// when possible. |
| 8332 | static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG, |
| 8333 | const X86Subtarget *Subtarget) { |
| 8334 | // On X86 with SSE2 support, we can transform this to a vector shift if |
| 8335 | // all elements are shifted by the same amount. We can't do this in legalize |
| 8336 | // because the a constant vector is typically transformed to a constant pool |
| 8337 | // so we have no knowledge of the shift amount. |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8338 | if (!Subtarget->hasSSE2()) |
| 8339 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8340 | |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8341 | MVT VT = N->getValueType(0); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8342 | if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16) |
| 8343 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8344 | |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8345 | SDValue ShAmtOp = N->getOperand(1); |
| 8346 | MVT EltVT = VT.getVectorElementType(); |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8347 | DebugLoc DL = N->getDebugLoc(); |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8348 | SDValue BaseShAmt; |
| 8349 | if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) { |
| 8350 | unsigned NumElts = VT.getVectorNumElements(); |
| 8351 | unsigned i = 0; |
| 8352 | for (; i != NumElts; ++i) { |
| 8353 | SDValue Arg = ShAmtOp.getOperand(i); |
| 8354 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 8355 | BaseShAmt = Arg; |
| 8356 | break; |
| 8357 | } |
| 8358 | for (; i != NumElts; ++i) { |
| 8359 | SDValue Arg = ShAmtOp.getOperand(i); |
| 8360 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 8361 | if (Arg != BaseShAmt) { |
| 8362 | return SDValue(); |
| 8363 | } |
| 8364 | } |
| 8365 | } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE && |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 8366 | cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) { |
| 8367 | BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp, |
| 8368 | DAG.getIntPtrConstant(0)); |
Mon P Wang | 3becd09 | 2009-01-28 08:12:05 +0000 | [diff] [blame] | 8369 | } else |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8370 | return SDValue(); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8371 | |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8372 | if (EltVT.bitsGT(MVT::i32)) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8373 | BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8374 | else if (EltVT.bitsLT(MVT::i32)) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8375 | BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8376 | |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8377 | // The shift amount is identical so we can do a vector shift. |
| 8378 | SDValue ValOp = N->getOperand(0); |
| 8379 | switch (N->getOpcode()) { |
| 8380 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8381 | llvm_unreachable("Unknown shift opcode!"); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8382 | break; |
| 8383 | case ISD::SHL: |
| 8384 | if (VT == MVT::v2i64) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8385 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8386 | DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32), |
| 8387 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8388 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8389 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8390 | DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32), |
| 8391 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8392 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8393 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8394 | DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), |
| 8395 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8396 | break; |
| 8397 | case ISD::SRA: |
| 8398 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8399 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8400 | DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32), |
| 8401 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8402 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8403 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8404 | DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32), |
| 8405 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8406 | break; |
| 8407 | case ISD::SRL: |
| 8408 | if (VT == MVT::v2i64) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8409 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8410 | DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32), |
| 8411 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8412 | if (VT == MVT::v4i32) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8413 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8414 | DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32), |
| 8415 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8416 | if (VT == MVT::v8i16) |
Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 8417 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8418 | DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32), |
| 8419 | ValOp, BaseShAmt); |
Nate Begeman | c2fd67f | 2009-01-26 03:15:31 +0000 | [diff] [blame] | 8420 | break; |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8421 | } |
| 8422 | return SDValue(); |
| 8423 | } |
| 8424 | |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8425 | /// PerformSTORECombine - Do target-specific dag combines on STORE nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8426 | static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG, |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8427 | const X86Subtarget *Subtarget) { |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8428 | // Turn load->store of MMX types into GPR load/stores. This avoids clobbering |
| 8429 | // the FP state in cases where an emms may be missing. |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8430 | // A preferable solution to the general problem is to figure out the right |
| 8431 | // places to insert EMMS. This qualifies as a quick hack. |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8432 | |
| 8433 | // 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] | 8434 | StoreSDNode *St = cast<StoreSDNode>(N); |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8435 | MVT VT = St->getValue().getValueType(); |
| 8436 | if (VT.getSizeInBits() != 64) |
| 8437 | return SDValue(); |
| 8438 | |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 8439 | const Function *F = DAG.getMachineFunction().getFunction(); |
| 8440 | bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat); |
| 8441 | bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps |
| 8442 | && Subtarget->hasSSE2(); |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8443 | if ((VT.isVector() || |
| 8444 | (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) && |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8445 | isa<LoadSDNode>(St->getValue()) && |
| 8446 | !cast<LoadSDNode>(St->getValue())->isVolatile() && |
| 8447 | St->getChain().hasOneUse() && !St->isVolatile()) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8448 | SDNode* LdVal = St->getValue().getNode(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8449 | LoadSDNode *Ld = 0; |
| 8450 | int TokenFactorIndex = -1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8451 | SmallVector<SDValue, 8> Ops; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8452 | SDNode* ChainVal = St->getChain().getNode(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8453 | // Must be a store of a load. We currently handle two cases: the load |
| 8454 | // is a direct child, and it's under an intervening TokenFactor. It is |
| 8455 | // possible to dig deeper under nested TokenFactors. |
Dale Johannesen | 14e2ea9 | 2008-02-25 22:29:22 +0000 | [diff] [blame] | 8456 | if (ChainVal == LdVal) |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8457 | Ld = cast<LoadSDNode>(St->getChain()); |
| 8458 | else if (St->getValue().hasOneUse() && |
| 8459 | ChainVal->getOpcode() == ISD::TokenFactor) { |
| 8460 | for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8461 | if (ChainVal->getOperand(i).getNode() == LdVal) { |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8462 | TokenFactorIndex = i; |
| 8463 | Ld = cast<LoadSDNode>(St->getValue()); |
| 8464 | } else |
| 8465 | Ops.push_back(ChainVal->getOperand(i)); |
| 8466 | } |
| 8467 | } |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8468 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8469 | if (!Ld || !ISD::isNormalLoad(Ld)) |
| 8470 | return SDValue(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8471 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8472 | // If this is not the MMX case, i.e. we are just turning i64 load/store |
| 8473 | // into f64 load/store, avoid the transformation if there are multiple |
| 8474 | // uses of the loaded value. |
| 8475 | if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0)) |
| 8476 | return SDValue(); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8477 | |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8478 | DebugLoc LdDL = Ld->getDebugLoc(); |
| 8479 | DebugLoc StDL = N->getDebugLoc(); |
| 8480 | // If we are a 64-bit capable x86, lower to a single movq load/store pair. |
| 8481 | // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store |
| 8482 | // pair instead. |
| 8483 | if (Subtarget->is64Bit() || F64IsLegal) { |
| 8484 | MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64; |
| 8485 | SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), |
| 8486 | Ld->getBasePtr(), Ld->getSrcValue(), |
| 8487 | Ld->getSrcValueOffset(), Ld->isVolatile(), |
| 8488 | Ld->getAlignment()); |
| 8489 | SDValue NewChain = NewLd.getValue(1); |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8490 | if (TokenFactorIndex != -1) { |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8491 | Ops.push_back(NewChain); |
| 8492 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 8493 | Ops.size()); |
| 8494 | } |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8495 | return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(), |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8496 | St->getSrcValue(), St->getSrcValueOffset(), |
| 8497 | St->isVolatile(), St->getAlignment()); |
| 8498 | } |
Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 8499 | |
| 8500 | // Otherwise, lower to two pairs of 32-bit loads / stores. |
| 8501 | SDValue LoAddr = Ld->getBasePtr(); |
| 8502 | SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr, |
| 8503 | DAG.getConstant(4, MVT::i32)); |
| 8504 | |
| 8505 | SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr, |
| 8506 | Ld->getSrcValue(), Ld->getSrcValueOffset(), |
| 8507 | Ld->isVolatile(), Ld->getAlignment()); |
| 8508 | SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr, |
| 8509 | Ld->getSrcValue(), Ld->getSrcValueOffset()+4, |
| 8510 | Ld->isVolatile(), |
| 8511 | MinAlign(Ld->getAlignment(), 4)); |
| 8512 | |
| 8513 | SDValue NewChain = LoLd.getValue(1); |
| 8514 | if (TokenFactorIndex != -1) { |
| 8515 | Ops.push_back(LoLd); |
| 8516 | Ops.push_back(HiLd); |
| 8517 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
| 8518 | Ops.size()); |
| 8519 | } |
| 8520 | |
| 8521 | LoAddr = St->getBasePtr(); |
| 8522 | HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr, |
| 8523 | DAG.getConstant(4, MVT::i32)); |
| 8524 | |
| 8525 | SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr, |
| 8526 | St->getSrcValue(), St->getSrcValueOffset(), |
| 8527 | St->isVolatile(), St->getAlignment()); |
| 8528 | SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr, |
| 8529 | St->getSrcValue(), |
| 8530 | St->getSrcValueOffset() + 4, |
| 8531 | St->isVolatile(), |
| 8532 | MinAlign(St->getAlignment(), 4)); |
| 8533 | return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8534 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8535 | return SDValue(); |
Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 8536 | } |
| 8537 | |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8538 | /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and |
| 8539 | /// X86ISD::FXOR nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8540 | static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) { |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8541 | assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR); |
| 8542 | // F[X]OR(0.0, x) -> x |
| 8543 | // F[X]OR(x, 0.0) -> x |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8544 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 8545 | if (C->getValueAPF().isPosZero()) |
| 8546 | return N->getOperand(1); |
| 8547 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 8548 | if (C->getValueAPF().isPosZero()) |
| 8549 | return N->getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8550 | return SDValue(); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8551 | } |
| 8552 | |
| 8553 | /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8554 | static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) { |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8555 | // FAND(0.0, x) -> 0.0 |
| 8556 | // FAND(x, 0.0) -> 0.0 |
| 8557 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 8558 | if (C->getValueAPF().isPosZero()) |
| 8559 | return N->getOperand(0); |
| 8560 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 8561 | if (C->getValueAPF().isPosZero()) |
| 8562 | return N->getOperand(1); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8563 | return SDValue(); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8564 | } |
| 8565 | |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 8566 | static SDValue PerformBTCombine(SDNode *N, |
| 8567 | SelectionDAG &DAG, |
| 8568 | TargetLowering::DAGCombinerInfo &DCI) { |
| 8569 | // BT ignores high bits in the bit index operand. |
| 8570 | SDValue Op1 = N->getOperand(1); |
| 8571 | if (Op1.hasOneUse()) { |
| 8572 | unsigned BitWidth = Op1.getValueSizeInBits(); |
| 8573 | APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth)); |
| 8574 | APInt KnownZero, KnownOne; |
| 8575 | TargetLowering::TargetLoweringOpt TLO(DAG); |
| 8576 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 8577 | if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) || |
| 8578 | TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO)) |
| 8579 | DCI.CommitTargetLoweringOpt(TLO); |
| 8580 | } |
| 8581 | return SDValue(); |
| 8582 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 8583 | |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 8584 | static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) { |
| 8585 | SDValue Op = N->getOperand(0); |
| 8586 | if (Op.getOpcode() == ISD::BIT_CONVERT) |
| 8587 | Op = Op.getOperand(0); |
| 8588 | MVT VT = N->getValueType(0), OpVT = Op.getValueType(); |
| 8589 | if (Op.getOpcode() == X86ISD::VZEXT_LOAD && |
| 8590 | VT.getVectorElementType().getSizeInBits() == |
| 8591 | OpVT.getVectorElementType().getSizeInBits()) { |
| 8592 | return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op); |
| 8593 | } |
| 8594 | return SDValue(); |
| 8595 | } |
| 8596 | |
Owen Anderson | 9917700 | 2009-06-29 18:04:45 +0000 | [diff] [blame] | 8597 | // On X86 and X86-64, atomic operations are lowered to locked instructions. |
| 8598 | // Locked instructions, in turn, have implicit fence semantics (all memory |
| 8599 | // operations are flushed before issuing the locked instruction, and the |
| 8600 | // are not buffered), so we can fold away the common pattern of |
| 8601 | // fence-atomic-fence. |
| 8602 | static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) { |
| 8603 | SDValue atomic = N->getOperand(0); |
| 8604 | switch (atomic.getOpcode()) { |
| 8605 | case ISD::ATOMIC_CMP_SWAP: |
| 8606 | case ISD::ATOMIC_SWAP: |
| 8607 | case ISD::ATOMIC_LOAD_ADD: |
| 8608 | case ISD::ATOMIC_LOAD_SUB: |
| 8609 | case ISD::ATOMIC_LOAD_AND: |
| 8610 | case ISD::ATOMIC_LOAD_OR: |
| 8611 | case ISD::ATOMIC_LOAD_XOR: |
| 8612 | case ISD::ATOMIC_LOAD_NAND: |
| 8613 | case ISD::ATOMIC_LOAD_MIN: |
| 8614 | case ISD::ATOMIC_LOAD_MAX: |
| 8615 | case ISD::ATOMIC_LOAD_UMIN: |
| 8616 | case ISD::ATOMIC_LOAD_UMAX: |
| 8617 | break; |
| 8618 | default: |
| 8619 | return SDValue(); |
| 8620 | } |
| 8621 | |
| 8622 | SDValue fence = atomic.getOperand(0); |
| 8623 | if (fence.getOpcode() != ISD::MEMBARRIER) |
| 8624 | return SDValue(); |
| 8625 | |
| 8626 | switch (atomic.getOpcode()) { |
| 8627 | case ISD::ATOMIC_CMP_SWAP: |
| 8628 | return DAG.UpdateNodeOperands(atomic, fence.getOperand(0), |
| 8629 | atomic.getOperand(1), atomic.getOperand(2), |
| 8630 | atomic.getOperand(3)); |
| 8631 | case ISD::ATOMIC_SWAP: |
| 8632 | case ISD::ATOMIC_LOAD_ADD: |
| 8633 | case ISD::ATOMIC_LOAD_SUB: |
| 8634 | case ISD::ATOMIC_LOAD_AND: |
| 8635 | case ISD::ATOMIC_LOAD_OR: |
| 8636 | case ISD::ATOMIC_LOAD_XOR: |
| 8637 | case ISD::ATOMIC_LOAD_NAND: |
| 8638 | case ISD::ATOMIC_LOAD_MIN: |
| 8639 | case ISD::ATOMIC_LOAD_MAX: |
| 8640 | case ISD::ATOMIC_LOAD_UMIN: |
| 8641 | case ISD::ATOMIC_LOAD_UMAX: |
| 8642 | return DAG.UpdateNodeOperands(atomic, fence.getOperand(0), |
| 8643 | atomic.getOperand(1), atomic.getOperand(2)); |
| 8644 | default: |
| 8645 | return SDValue(); |
| 8646 | } |
| 8647 | } |
| 8648 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8649 | SDValue X86TargetLowering::PerformDAGCombine(SDNode *N, |
Evan Cheng | 9dd93b3 | 2008-11-05 06:03:38 +0000 | [diff] [blame] | 8650 | DAGCombinerInfo &DCI) const { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8651 | SelectionDAG &DAG = DCI.DAG; |
| 8652 | switch (N->getOpcode()) { |
| 8653 | default: break; |
Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 8654 | case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this); |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8655 | case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget); |
Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 8656 | case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI); |
Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 8657 | case ISD::MUL: return PerformMulCombine(N, DAG, DCI); |
Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 8658 | case ISD::SHL: |
| 8659 | case ISD::SRA: |
| 8660 | case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 8661 | case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget); |
Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 8662 | case X86ISD::FXOR: |
Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 8663 | case X86ISD::FOR: return PerformFORCombine(N, DAG); |
| 8664 | case X86ISD::FAND: return PerformFANDCombine(N, DAG); |
Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 8665 | case X86ISD::BT: return PerformBTCombine(N, DAG, DCI); |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 8666 | case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG); |
Owen Anderson | 9917700 | 2009-06-29 18:04:45 +0000 | [diff] [blame] | 8667 | case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8668 | } |
| 8669 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8670 | return SDValue(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 8671 | } |
| 8672 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 8673 | //===----------------------------------------------------------------------===// |
| 8674 | // X86 Inline Assembly Support |
| 8675 | //===----------------------------------------------------------------------===// |
| 8676 | |
Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 8677 | static bool LowerToBSwap(CallInst *CI) { |
| 8678 | // FIXME: this should verify that we are targetting a 486 or better. If not, |
| 8679 | // we will turn this bswap into something that will be lowered to logical ops |
| 8680 | // instead of emitting the bswap asm. For now, we don't support 486 or lower |
| 8681 | // so don't worry about this. |
| 8682 | |
| 8683 | // Verify this is a simple bswap. |
| 8684 | if (CI->getNumOperands() != 2 || |
| 8685 | CI->getType() != CI->getOperand(1)->getType() || |
| 8686 | !CI->getType()->isInteger()) |
| 8687 | return false; |
| 8688 | |
| 8689 | const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); |
| 8690 | if (!Ty || Ty->getBitWidth() % 16 != 0) |
| 8691 | return false; |
| 8692 | |
| 8693 | // Okay, we can do this xform, do so now. |
| 8694 | const Type *Tys[] = { Ty }; |
| 8695 | Module *M = CI->getParent()->getParent()->getParent(); |
| 8696 | Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
| 8697 | |
| 8698 | Value *Op = CI->getOperand(1); |
| 8699 | Op = CallInst::Create(Int, Op, CI->getName(), CI); |
| 8700 | |
| 8701 | CI->replaceAllUsesWith(Op); |
| 8702 | CI->eraseFromParent(); |
| 8703 | return true; |
| 8704 | } |
| 8705 | |
| 8706 | bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const { |
| 8707 | InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); |
| 8708 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
| 8709 | |
| 8710 | std::string AsmStr = IA->getAsmString(); |
| 8711 | |
| 8712 | // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" |
| 8713 | std::vector<std::string> AsmPieces; |
| 8714 | SplitString(AsmStr, AsmPieces, "\n"); // ; as separator? |
| 8715 | |
| 8716 | switch (AsmPieces.size()) { |
| 8717 | default: return false; |
| 8718 | case 1: |
| 8719 | AsmStr = AsmPieces[0]; |
| 8720 | AsmPieces.clear(); |
| 8721 | SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace. |
| 8722 | |
| 8723 | // bswap $0 |
| 8724 | if (AsmPieces.size() == 2 && |
| 8725 | (AsmPieces[0] == "bswap" || |
| 8726 | AsmPieces[0] == "bswapq" || |
| 8727 | AsmPieces[0] == "bswapl") && |
| 8728 | (AsmPieces[1] == "$0" || |
| 8729 | AsmPieces[1] == "${0:q}")) { |
| 8730 | // No need to check constraints, nothing other than the equivalent of |
| 8731 | // "=r,0" would be valid here. |
| 8732 | return LowerToBSwap(CI); |
| 8733 | } |
| 8734 | // rorw $$8, ${0:w} --> llvm.bswap.i16 |
| 8735 | if (CI->getType() == Type::Int16Ty && |
| 8736 | AsmPieces.size() == 3 && |
| 8737 | AsmPieces[0] == "rorw" && |
| 8738 | AsmPieces[1] == "$$8," && |
| 8739 | AsmPieces[2] == "${0:w}" && |
| 8740 | IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") { |
| 8741 | return LowerToBSwap(CI); |
| 8742 | } |
| 8743 | break; |
| 8744 | case 3: |
| 8745 | if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 && |
| 8746 | Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && |
| 8747 | Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { |
| 8748 | // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 |
| 8749 | std::vector<std::string> Words; |
| 8750 | SplitString(AsmPieces[0], Words, " \t"); |
| 8751 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") { |
| 8752 | Words.clear(); |
| 8753 | SplitString(AsmPieces[1], Words, " \t"); |
| 8754 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") { |
| 8755 | Words.clear(); |
| 8756 | SplitString(AsmPieces[2], Words, " \t,"); |
| 8757 | if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" && |
| 8758 | Words[2] == "%edx") { |
| 8759 | return LowerToBSwap(CI); |
| 8760 | } |
| 8761 | } |
| 8762 | } |
| 8763 | } |
| 8764 | break; |
| 8765 | } |
| 8766 | return false; |
| 8767 | } |
| 8768 | |
| 8769 | |
| 8770 | |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8771 | /// getConstraintType - Given a constraint letter, return the type of |
| 8772 | /// constraint it is for this target. |
| 8773 | X86TargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8774 | X86TargetLowering::getConstraintType(const std::string &Constraint) const { |
| 8775 | if (Constraint.size() == 1) { |
| 8776 | switch (Constraint[0]) { |
| 8777 | case 'A': |
Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 8778 | return C_Register; |
Chris Lattner | fce84ac | 2008-03-11 19:06:29 +0000 | [diff] [blame] | 8779 | case 'f': |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8780 | case 'r': |
| 8781 | case 'R': |
| 8782 | case 'l': |
| 8783 | case 'q': |
| 8784 | case 'Q': |
| 8785 | case 'x': |
Dale Johannesen | 2ffbcac | 2008-04-01 00:57:48 +0000 | [diff] [blame] | 8786 | case 'y': |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8787 | case 'Y': |
| 8788 | return C_RegisterClass; |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8789 | case 'e': |
| 8790 | case 'Z': |
| 8791 | return C_Other; |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8792 | default: |
| 8793 | break; |
| 8794 | } |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8795 | } |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 8796 | return TargetLowering::getConstraintType(Constraint); |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8797 | } |
| 8798 | |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8799 | /// LowerXConstraint - try to replace an X constraint, which matches anything, |
| 8800 | /// with another that has more specific requirements based on the type of the |
| 8801 | /// corresponding operand. |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8802 | const char *X86TargetLowering:: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8803 | LowerXConstraint(MVT ConstraintVT) const { |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8804 | // FP X constraints get lowered to SSE1/2 registers if available, otherwise |
| 8805 | // 'f' like normal targets. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8806 | if (ConstraintVT.isFloatingPoint()) { |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8807 | if (Subtarget->hasSSE2()) |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8808 | return "Y"; |
| 8809 | if (Subtarget->hasSSE1()) |
| 8810 | return "x"; |
| 8811 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8812 | |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8813 | return TargetLowering::LowerXConstraint(ConstraintVT); |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 8814 | } |
| 8815 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8816 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 8817 | /// vector. If it is invalid, don't add anything to Ops. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8818 | void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8819 | char Constraint, |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 8820 | bool hasMemory, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8821 | std::vector<SDValue>&Ops, |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 8822 | SelectionDAG &DAG) const { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8823 | SDValue Result(0, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8824 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8825 | switch (Constraint) { |
| 8826 | default: break; |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 8827 | case 'I': |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8828 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8829 | if (C->getZExtValue() <= 31) { |
| 8830 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8831 | break; |
| 8832 | } |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 8833 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8834 | return; |
Evan Cheng | 364091e | 2008-09-22 23:57:37 +0000 | [diff] [blame] | 8835 | case 'J': |
| 8836 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | 2e06dd2 | 2009-06-15 04:39:05 +0000 | [diff] [blame] | 8837 | if (C->getZExtValue() <= 63) { |
Chris Lattner | e493515 | 2009-06-15 04:01:39 +0000 | [diff] [blame] | 8838 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 8839 | break; |
| 8840 | } |
| 8841 | } |
| 8842 | return; |
| 8843 | case 'K': |
| 8844 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | 2e06dd2 | 2009-06-15 04:39:05 +0000 | [diff] [blame] | 8845 | if ((int8_t)C->getSExtValue() == C->getSExtValue()) { |
Evan Cheng | 364091e | 2008-09-22 23:57:37 +0000 | [diff] [blame] | 8846 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 8847 | break; |
| 8848 | } |
| 8849 | } |
| 8850 | return; |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8851 | case 'N': |
| 8852 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 8853 | if (C->getZExtValue() <= 255) { |
| 8854 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8855 | break; |
| 8856 | } |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 8857 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8858 | return; |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8859 | case 'e': { |
| 8860 | // 32-bit signed value |
| 8861 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 8862 | const ConstantInt *CI = C->getConstantIntValue(); |
| 8863 | if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) { |
| 8864 | // Widen to 64 bits here to get it sign extended. |
| 8865 | Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64); |
| 8866 | break; |
| 8867 | } |
| 8868 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 8869 | // memory models; it's complicated. |
| 8870 | } |
| 8871 | return; |
| 8872 | } |
| 8873 | case 'Z': { |
| 8874 | // 32-bit unsigned value |
| 8875 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| 8876 | const ConstantInt *CI = C->getConstantIntValue(); |
| 8877 | if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) { |
| 8878 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 8879 | break; |
| 8880 | } |
| 8881 | } |
| 8882 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 8883 | // memory models; it's complicated. |
| 8884 | return; |
| 8885 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8886 | case 'i': { |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8887 | // Literal immediates are always ok. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8888 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { |
Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 8889 | // Widen to 64 bits here to get it sign extended. |
| 8890 | Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8891 | break; |
| 8892 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8893 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8894 | // If we are in non-pic codegen mode, we allow the address of a global (with |
| 8895 | // an optional displacement) to be used with 'i'. |
Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 8896 | GlobalAddressSDNode *GA = 0; |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8897 | int64_t Offset = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8898 | |
Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 8899 | // Match either (GA), (GA+C), (GA+C1+C2), etc. |
| 8900 | while (1) { |
| 8901 | if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) { |
| 8902 | Offset += GA->getOffset(); |
| 8903 | break; |
| 8904 | } else if (Op.getOpcode() == ISD::ADD) { |
| 8905 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 8906 | Offset += C->getZExtValue(); |
| 8907 | Op = Op.getOperand(0); |
| 8908 | continue; |
| 8909 | } |
| 8910 | } else if (Op.getOpcode() == ISD::SUB) { |
| 8911 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 8912 | Offset += -C->getZExtValue(); |
| 8913 | Op = Op.getOperand(0); |
| 8914 | continue; |
| 8915 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8916 | } |
Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 8917 | |
Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 8918 | // Otherwise, this isn't something we can handle, reject it. |
| 8919 | return; |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8920 | } |
Chris Lattner | 3b6b36d | 2009-07-10 06:29:59 +0000 | [diff] [blame] | 8921 | |
Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 8922 | GlobalValue *GV = GA->getGlobal(); |
Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 8923 | // If we require an extra load to get this address, as in PIC mode, we |
| 8924 | // can't accept it. |
Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 8925 | if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV, |
| 8926 | getTargetMachine()))) |
Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 8927 | return; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8928 | |
Dale Johannesen | 60b3ba0 | 2009-07-21 00:12:29 +0000 | [diff] [blame] | 8929 | if (hasMemory) |
| 8930 | Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG); |
| 8931 | else |
| 8932 | Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset); |
Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 8933 | Result = Op; |
| 8934 | break; |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8935 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 8936 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8937 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 8938 | if (Result.getNode()) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 8939 | Ops.push_back(Result); |
| 8940 | return; |
| 8941 | } |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 8942 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory, |
| 8943 | Ops, DAG); |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8946 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 8947 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8948 | MVT VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8949 | if (Constraint.size() == 1) { |
| 8950 | // FIXME: not handling fp-stack yet! |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8951 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 8952 | default: break; // Unknown constraint letter |
Evan Cheng | 47e9fab | 2009-07-17 22:13:25 +0000 | [diff] [blame] | 8953 | case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode. |
| 8954 | if (Subtarget->is64Bit()) { |
| 8955 | if (VT == MVT::i32) |
| 8956 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, |
| 8957 | X86::ESI, X86::EDI, X86::R8D, X86::R9D, |
| 8958 | X86::R10D,X86::R11D,X86::R12D, |
| 8959 | X86::R13D,X86::R14D,X86::R15D, |
| 8960 | X86::EBP, X86::ESP, 0); |
| 8961 | else if (VT == MVT::i16) |
| 8962 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, |
| 8963 | X86::SI, X86::DI, X86::R8W,X86::R9W, |
| 8964 | X86::R10W,X86::R11W,X86::R12W, |
| 8965 | X86::R13W,X86::R14W,X86::R15W, |
| 8966 | X86::BP, X86::SP, 0); |
| 8967 | else if (VT == MVT::i8) |
| 8968 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, |
| 8969 | X86::SIL, X86::DIL, X86::R8B,X86::R9B, |
| 8970 | X86::R10B,X86::R11B,X86::R12B, |
| 8971 | X86::R13B,X86::R14B,X86::R15B, |
| 8972 | X86::BPL, X86::SPL, 0); |
| 8973 | |
| 8974 | else if (VT == MVT::i64) |
| 8975 | return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, |
| 8976 | X86::RSI, X86::RDI, X86::R8, X86::R9, |
| 8977 | X86::R10, X86::R11, X86::R12, |
| 8978 | X86::R13, X86::R14, X86::R15, |
| 8979 | X86::RBP, X86::RSP, 0); |
| 8980 | |
| 8981 | break; |
| 8982 | } |
| 8983 | // 32-bit fallthrough |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8984 | case 'Q': // Q_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 8985 | if (VT == MVT::i32) |
| 8986 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 8987 | else if (VT == MVT::i16) |
| 8988 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 8989 | else if (VT == MVT::i8) |
Evan Cheng | 1291438 | 2007-08-13 23:27:11 +0000 | [diff] [blame] | 8990 | 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] | 8991 | else if (VT == MVT::i64) |
| 8992 | return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0); |
| 8993 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8994 | } |
| 8995 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 8996 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 8997 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 8998 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 8999 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 9000 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9001 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9002 | MVT VT) const { |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 9003 | // First, see if this is a constraint that directly corresponds to an LLVM |
| 9004 | // register class. |
| 9005 | if (Constraint.size() == 1) { |
| 9006 | // GCC Constraint Letters |
| 9007 | switch (Constraint[0]) { |
| 9008 | default: break; |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9009 | case 'r': // GENERAL_REGS |
| 9010 | case 'R': // LEGACY_REGS |
| 9011 | case 'l': // INDEX_REGS |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 9012 | if (VT == MVT::i8) |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9013 | return std::make_pair(0U, X86::GR8RegisterClass); |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 9014 | if (VT == MVT::i16) |
| 9015 | return std::make_pair(0U, X86::GR16RegisterClass); |
| 9016 | if (VT == MVT::i32 || !Subtarget->is64Bit()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9017 | return std::make_pair(0U, X86::GR32RegisterClass); |
Chris Lattner | 1fa7198 | 2008-10-17 18:15:05 +0000 | [diff] [blame] | 9018 | return std::make_pair(0U, X86::GR64RegisterClass); |
Chris Lattner | fce84ac | 2008-03-11 19:06:29 +0000 | [diff] [blame] | 9019 | case 'f': // FP Stack registers. |
| 9020 | // If SSE is enabled for this VT, use f80 to ensure the isel moves the |
| 9021 | // value to the correct fpstack register class. |
| 9022 | if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT)) |
| 9023 | return std::make_pair(0U, X86::RFP32RegisterClass); |
| 9024 | if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT)) |
| 9025 | return std::make_pair(0U, X86::RFP64RegisterClass); |
| 9026 | return std::make_pair(0U, X86::RFP80RegisterClass); |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 9027 | case 'y': // MMX_REGS if MMX allowed. |
| 9028 | if (!Subtarget->hasMMX()) break; |
| 9029 | return std::make_pair(0U, X86::VR64RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9030 | case 'Y': // SSE_REGS if SSE2 allowed |
| 9031 | if (!Subtarget->hasSSE2()) break; |
| 9032 | // FALL THROUGH. |
| 9033 | case 'x': // SSE_REGS if SSE1 allowed |
| 9034 | if (!Subtarget->hasSSE1()) break; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9035 | |
| 9036 | switch (VT.getSimpleVT()) { |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9037 | default: break; |
| 9038 | // Scalar SSE types. |
| 9039 | case MVT::f32: |
| 9040 | case MVT::i32: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 9041 | return std::make_pair(0U, X86::FR32RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9042 | case MVT::f64: |
| 9043 | case MVT::i64: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 9044 | return std::make_pair(0U, X86::FR64RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9045 | // Vector types. |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 9046 | case MVT::v16i8: |
| 9047 | case MVT::v8i16: |
| 9048 | case MVT::v4i32: |
| 9049 | case MVT::v2i64: |
| 9050 | case MVT::v4f32: |
| 9051 | case MVT::v2f64: |
| 9052 | return std::make_pair(0U, X86::VR128RegisterClass); |
| 9053 | } |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 9054 | break; |
| 9055 | } |
| 9056 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9057 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9058 | // Use the default implementation in TargetLowering to convert the register |
| 9059 | // constraint into a member of a register class. |
| 9060 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 9061 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 9062 | |
| 9063 | // Not found as a standard register? |
| 9064 | if (Res.second == 0) { |
| 9065 | // GCC calls "st(0)" just plain "st". |
| 9066 | if (StringsEqualNoCase("{st}", Constraint)) { |
| 9067 | Res.first = X86::ST0; |
Chris Lattner | 9b4baf1 | 2007-09-24 05:27:37 +0000 | [diff] [blame] | 9068 | Res.second = X86::RFP80RegisterClass; |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 9069 | } |
Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 9070 | // 'A' means EAX + EDX. |
| 9071 | if (Constraint == "A") { |
| 9072 | Res.first = X86::EAX; |
Dan Gohman | 68a31c2 | 2009-07-30 17:02:08 +0000 | [diff] [blame] | 9073 | Res.second = X86::GR32_ADRegisterClass; |
Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 9074 | } |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 9075 | return Res; |
| 9076 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 9077 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9078 | // Otherwise, check to see if this is a register class of the wrong value |
| 9079 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 9080 | // turn into {ax},{dx}. |
| 9081 | if (Res.second->hasType(VT)) |
| 9082 | return Res; // Correct type already, nothing to do. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 9083 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9084 | // All of the single-register GCC register classes map their values onto |
| 9085 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 9086 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 9087 | // class and return the appropriate register. |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 9088 | if (Res.second == X86::GR16RegisterClass) { |
| 9089 | if (VT == MVT::i8) { |
| 9090 | unsigned DestReg = 0; |
| 9091 | switch (Res.first) { |
| 9092 | default: break; |
| 9093 | case X86::AX: DestReg = X86::AL; break; |
| 9094 | case X86::DX: DestReg = X86::DL; break; |
| 9095 | case X86::CX: DestReg = X86::CL; break; |
| 9096 | case X86::BX: DestReg = X86::BL; break; |
| 9097 | } |
| 9098 | if (DestReg) { |
| 9099 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 9100 | Res.second = X86::GR8RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 9101 | } |
| 9102 | } else if (VT == MVT::i32) { |
| 9103 | unsigned DestReg = 0; |
| 9104 | switch (Res.first) { |
| 9105 | default: break; |
| 9106 | case X86::AX: DestReg = X86::EAX; break; |
| 9107 | case X86::DX: DestReg = X86::EDX; break; |
| 9108 | case X86::CX: DestReg = X86::ECX; break; |
| 9109 | case X86::BX: DestReg = X86::EBX; break; |
| 9110 | case X86::SI: DestReg = X86::ESI; break; |
| 9111 | case X86::DI: DestReg = X86::EDI; break; |
| 9112 | case X86::BP: DestReg = X86::EBP; break; |
| 9113 | case X86::SP: DestReg = X86::ESP; break; |
| 9114 | } |
| 9115 | if (DestReg) { |
| 9116 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 9117 | Res.second = X86::GR32RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 9118 | } |
| 9119 | } else if (VT == MVT::i64) { |
| 9120 | unsigned DestReg = 0; |
| 9121 | switch (Res.first) { |
| 9122 | default: break; |
| 9123 | case X86::AX: DestReg = X86::RAX; break; |
| 9124 | case X86::DX: DestReg = X86::RDX; break; |
| 9125 | case X86::CX: DestReg = X86::RCX; break; |
| 9126 | case X86::BX: DestReg = X86::RBX; break; |
| 9127 | case X86::SI: DestReg = X86::RSI; break; |
| 9128 | case X86::DI: DestReg = X86::RDI; break; |
| 9129 | case X86::BP: DestReg = X86::RBP; break; |
| 9130 | case X86::SP: DestReg = X86::RSP; break; |
| 9131 | } |
| 9132 | if (DestReg) { |
| 9133 | Res.first = DestReg; |
Duncan Sands | 005e798 | 2009-04-21 09:44:39 +0000 | [diff] [blame] | 9134 | Res.second = X86::GR64RegisterClass; |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 9135 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9136 | } |
Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 9137 | } else if (Res.second == X86::FR32RegisterClass || |
| 9138 | Res.second == X86::FR64RegisterClass || |
| 9139 | Res.second == X86::VR128RegisterClass) { |
| 9140 | // Handle references to XMM physical registers that got mapped into the |
| 9141 | // wrong class. This can happen with constraints like {xmm0} where the |
| 9142 | // target independent register mapper will just pick the first match it can |
| 9143 | // find, ignoring the required type. |
| 9144 | if (VT == MVT::f32) |
| 9145 | Res.second = X86::FR32RegisterClass; |
| 9146 | else if (VT == MVT::f64) |
| 9147 | Res.second = X86::FR64RegisterClass; |
| 9148 | else if (X86::VR128RegisterClass->hasType(VT)) |
| 9149 | Res.second = X86::VR128RegisterClass; |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9150 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 9151 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 9152 | return Res; |
| 9153 | } |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9154 | |
| 9155 | //===----------------------------------------------------------------------===// |
| 9156 | // X86 Widen vector type |
| 9157 | //===----------------------------------------------------------------------===// |
| 9158 | |
| 9159 | /// getWidenVectorType: given a vector type, returns the type to widen |
| 9160 | /// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself. |
| 9161 | /// 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] | 9162 | /// 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] | 9163 | /// scalarizing vs using the wider vector type. |
| 9164 | |
Dan Gohman | c13cf13 | 2009-01-15 17:34:08 +0000 | [diff] [blame] | 9165 | MVT X86TargetLowering::getWidenVectorType(MVT VT) const { |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9166 | assert(VT.isVector()); |
| 9167 | if (isTypeLegal(VT)) |
| 9168 | return VT; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9169 | |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9170 | // TODO: In computeRegisterProperty, we can compute the list of legal vector |
| 9171 | // type based on element type. This would speed up our search (though |
| 9172 | // it may not be worth it since the size of the list is relatively |
| 9173 | // small). |
| 9174 | MVT EltVT = VT.getVectorElementType(); |
| 9175 | unsigned NElts = VT.getVectorNumElements(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9176 | |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9177 | // On X86, it make sense to widen any vector wider than 1 |
| 9178 | if (NElts <= 1) |
| 9179 | return MVT::Other; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9180 | |
| 9181 | for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE; |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9182 | nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) { |
| 9183 | MVT SVT = (MVT::SimpleValueType)nVT; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9184 | |
| 9185 | if (isTypeLegal(SVT) && |
| 9186 | SVT.getVectorElementType() == EltVT && |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 9187 | SVT.getVectorNumElements() > NElts) |
| 9188 | return SVT; |
| 9189 | } |
| 9190 | return MVT::Other; |
| 9191 | } |