Chris Lattner | c6d0567 | 2006-05-23 23:20:42 +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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that X86 uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86.h" |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 16 | #include "X86InstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 17 | #include "X86ISelLowering.h" |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 18 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 19 | #include "X86TargetMachine.h" |
| 20 | #include "llvm/CallingConv.h" |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/VectorExtras.h" |
| 27 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | 362e98a | 2007-02-27 04:43:02 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/CallingConvLower.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/SelectionDAG.h" |
| 33 | #include "llvm/CodeGen/SSARegMap.h" |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringExtras.h" |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 37 | #include "llvm/ParameterAttributes.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 40 | X86TargetLowering::X86TargetLowering(TargetMachine &TM) |
| 41 | : TargetLowering(TM) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 42 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 43 | X86ScalarSSE = Subtarget->hasSSE2(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 44 | X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 45 | |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 46 | RegInfo = TM.getRegisterInfo(); |
| 47 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 48 | // Set up the TargetLowering object. |
| 49 | |
| 50 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 51 | setShiftAmountType(MVT::i8); |
| 52 | setSetCCResultType(MVT::i8); |
| 53 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 54 | setSchedulingPreference(SchedulingForRegPressure); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 55 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 56 | setStackPointerRegisterToSaveRestore(X86StackPtr); |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 57 | |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 58 | if (Subtarget->isTargetDarwin()) { |
Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 59 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 60 | setUseUnderscoreSetJmp(false); |
| 61 | setUseUnderscoreLongJmp(false); |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 62 | } else if (Subtarget->isTargetMingw()) { |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 63 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 64 | setUseUnderscoreSetJmp(true); |
| 65 | setUseUnderscoreLongJmp(false); |
| 66 | } else { |
| 67 | setUseUnderscoreSetJmp(true); |
| 68 | setUseUnderscoreLongJmp(true); |
| 69 | } |
| 70 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 71 | // Set up the register classes. |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 72 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 73 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 74 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 75 | if (Subtarget->is64Bit()) |
| 76 | addRegisterClass(MVT::i64, X86::GR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 77 | |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 78 | setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 79 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 80 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 81 | // operation. |
| 82 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 83 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 84 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 85 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 86 | if (Subtarget->is64Bit()) { |
| 87 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 88 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 89 | } else { |
| 90 | if (X86ScalarSSE) |
| 91 | // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP. |
| 92 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); |
| 93 | else |
| 94 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| 95 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 96 | |
| 97 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 98 | // this operation. |
| 99 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 100 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 101 | // SSE has no i16 to fp conversion, only i32 |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 102 | if (X86ScalarSSE) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 103 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 104 | else { |
| 105 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 106 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 107 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 108 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 109 | if (!Subtarget->is64Bit()) { |
| 110 | // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode. |
| 111 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 112 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 113 | } |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 114 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 115 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 116 | // this operation. |
| 117 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 118 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 119 | |
| 120 | if (X86ScalarSSE) { |
| 121 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| 122 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 123 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 124 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 128 | // conversion. |
| 129 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 130 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 131 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 132 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 133 | if (Subtarget->is64Bit()) { |
| 134 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 135 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 136 | } else { |
| 137 | if (X86ScalarSSE && !Subtarget->hasSSE3()) |
| 138 | // Expand FP_TO_UINT into a select. |
| 139 | // FIXME: We would like to use a Custom expander here eventually to do |
| 140 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 141 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 142 | else |
| 143 | // With SSE3 we can use fisttpll to convert to a signed i64. |
| 144 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 145 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 399610a | 2006-12-05 18:22:22 +0000 | [diff] [blame] | 147 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. |
Chris Lattner | f3597a1 | 2006-12-05 18:45:06 +0000 | [diff] [blame] | 148 | if (!X86ScalarSSE) { |
| 149 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 150 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
| 151 | } |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 152 | |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 153 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 154 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 155 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 156 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 157 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 158 | if (Subtarget->is64Bit()) |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 159 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
| 160 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); |
| 161 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 162 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 163 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 164 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 165 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 166 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 167 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 168 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 169 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 170 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 171 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
| 172 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 173 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 174 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 175 | if (Subtarget->is64Bit()) { |
| 176 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
| 177 | setOperationAction(ISD::CTTZ , MVT::i64 , Expand); |
| 178 | setOperationAction(ISD::CTLZ , MVT::i64 , Expand); |
| 179 | } |
| 180 | |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 181 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 182 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 183 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 184 | // These should be promoted to a larger select which is supported. |
| 185 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 186 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 187 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 188 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 189 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 190 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 191 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
| 192 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 193 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 194 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 195 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 196 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 197 | if (Subtarget->is64Bit()) { |
| 198 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| 199 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| 200 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 201 | // X86 ret instruction may pop stack. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 202 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 203 | if (!Subtarget->is64Bit()) |
| 204 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); |
| 205 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 206 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 207 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 208 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 209 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 210 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 211 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 212 | if (Subtarget->is64Bit()) { |
| 213 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 214 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 215 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
| 216 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
| 217 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 218 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 219 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 220 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 221 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 222 | // X86 wants to expand memset / memcpy itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 223 | setOperationAction(ISD::MEMSET , MVT::Other, Custom); |
| 224 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 225 | |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 226 | // We don't have line number support yet. |
| 227 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 228 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 229 | // FIXME - use subtarget debug flags |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 230 | if (!Subtarget->isTargetDarwin() && |
| 231 | !Subtarget->isTargetELF() && |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 232 | !Subtarget->isTargetCygMing()) |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 233 | setOperationAction(ISD::LABEL, MVT::Other, Expand); |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 234 | |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 235 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); |
| 236 | setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); |
| 237 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 238 | setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); |
| 239 | if (Subtarget->is64Bit()) { |
| 240 | // FIXME: Verify |
| 241 | setExceptionPointerRegister(X86::RAX); |
| 242 | setExceptionSelectorRegister(X86::RDX); |
| 243 | } else { |
| 244 | setExceptionPointerRegister(X86::EAX); |
| 245 | setExceptionSelectorRegister(X86::EDX); |
| 246 | } |
Anton Korobeynikov | 3825262 | 2007-09-03 00:36:06 +0000 | [diff] [blame] | 247 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); |
Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 248 | |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 249 | setOperationAction(ISD::ADJUST_TRAMP, MVT::i32, Expand); |
| 250 | setOperationAction(ISD::ADJUST_TRAMP, MVT::i64, Expand); |
| 251 | setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); |
| 252 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 253 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 254 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 255 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 256 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 257 | if (Subtarget->is64Bit()) |
| 258 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
| 259 | else |
| 260 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 261 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 262 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 263 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 264 | if (Subtarget->is64Bit()) |
| 265 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 266 | if (Subtarget->isTargetCygMing()) |
| 267 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); |
| 268 | else |
| 269 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 270 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 271 | if (X86ScalarSSE) { |
| 272 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 273 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 274 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 275 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 276 | // Use ANDPD to simulate FABS. |
| 277 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 278 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 279 | |
| 280 | // Use XORP to simulate FNEG. |
| 281 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 282 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 283 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 284 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| 285 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 286 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 287 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 288 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 289 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 290 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 291 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 292 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 293 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 294 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 295 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 296 | // Expand FP immediates into loads from the stack, except for the special |
| 297 | // cases we handle. |
| 298 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 299 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 300 | addLegalFPImmediate(APFloat(+0.0)); // xorps / xorpd |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 301 | |
| 302 | // Conversions to long double (in X87) go through memory. |
| 303 | setConvertAction(MVT::f32, MVT::f80, Expand); |
| 304 | setConvertAction(MVT::f64, MVT::f80, Expand); |
| 305 | |
| 306 | // Conversions from long double (in X87) go through memory. |
| 307 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 308 | setConvertAction(MVT::f80, MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 309 | } else { |
| 310 | // Set up the FP register classes. |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 311 | addRegisterClass(MVT::f64, X86::RFP64RegisterClass); |
| 312 | addRegisterClass(MVT::f32, X86::RFP32RegisterClass); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 313 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 314 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 315 | setOperationAction(ISD::UNDEF, MVT::f32, Expand); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 316 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 317 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 318 | |
| 319 | // Floating truncations need to go through memory. |
| 320 | setConvertAction(MVT::f80, MVT::f32, Expand); |
| 321 | setConvertAction(MVT::f64, MVT::f32, Expand); |
| 322 | setConvertAction(MVT::f80, MVT::f64, Expand); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 323 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 324 | if (!UnsafeFPMath) { |
| 325 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 326 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 327 | } |
| 328 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 329 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 330 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 331 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 332 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 333 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 334 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 335 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 336 | |
Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 337 | // Long double always uses X87. |
| 338 | addRegisterClass(MVT::f80, X86::RFP80RegisterClass); |
| 339 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 340 | // First set operation action for all vector types to expand. Then we |
| 341 | // will selectively turn on ones that can be effectively codegen'd. |
Dan Gohman | fa0f77d | 2007-05-18 18:44:07 +0000 | [diff] [blame] | 342 | for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 343 | VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 344 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 345 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 346 | setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand); |
Evan Cheng | a72cb0e | 2007-06-29 00:18:15 +0000 | [diff] [blame] | 347 | setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 348 | setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 349 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 350 | setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand); |
| 351 | setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand); |
| 352 | setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand); |
| 353 | setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand); |
| 354 | setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand); |
| 355 | setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 356 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 357 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); |
Chris Lattner | 9b3bd46 | 2006-03-21 20:51:05 +0000 | [diff] [blame] | 358 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 359 | setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 360 | setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand); |
| 361 | setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand); |
| 362 | setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand); |
| 363 | setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand); |
| 364 | setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand); |
| 365 | setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand); |
| 366 | setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 369 | if (Subtarget->hasMMX()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 370 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 371 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 372 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 373 | addRegisterClass(MVT::v1i64, X86::VR64RegisterClass); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 374 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 375 | // FIXME: add MMX packed arithmetics |
Bill Wendling | bc9bffa | 2007-03-07 05:43:18 +0000 | [diff] [blame] | 376 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 377 | setOperationAction(ISD::ADD, MVT::v8i8, Legal); |
| 378 | setOperationAction(ISD::ADD, MVT::v4i16, Legal); |
| 379 | setOperationAction(ISD::ADD, MVT::v2i32, Legal); |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 380 | setOperationAction(ISD::ADD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 381 | |
Bill Wendling | c1fb047 | 2007-03-10 09:57:05 +0000 | [diff] [blame] | 382 | setOperationAction(ISD::SUB, MVT::v8i8, Legal); |
| 383 | setOperationAction(ISD::SUB, MVT::v4i16, Legal); |
| 384 | setOperationAction(ISD::SUB, MVT::v2i32, Legal); |
| 385 | |
Bill Wendling | 74027e9 | 2007-03-15 21:24:36 +0000 | [diff] [blame] | 386 | setOperationAction(ISD::MULHS, MVT::v4i16, Legal); |
| 387 | setOperationAction(ISD::MUL, MVT::v4i16, Legal); |
| 388 | |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 389 | setOperationAction(ISD::AND, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 390 | AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 391 | setOperationAction(ISD::AND, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 392 | AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64); |
| 393 | setOperationAction(ISD::AND, MVT::v2i32, Promote); |
| 394 | AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64); |
| 395 | setOperationAction(ISD::AND, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 396 | |
| 397 | setOperationAction(ISD::OR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 398 | AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 399 | setOperationAction(ISD::OR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 400 | AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64); |
| 401 | setOperationAction(ISD::OR, MVT::v2i32, Promote); |
| 402 | AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64); |
| 403 | setOperationAction(ISD::OR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 404 | |
| 405 | setOperationAction(ISD::XOR, MVT::v8i8, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 406 | AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 407 | setOperationAction(ISD::XOR, MVT::v4i16, Promote); |
Bill Wendling | ab5b49d | 2007-03-26 08:03:33 +0000 | [diff] [blame] | 408 | AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64); |
| 409 | setOperationAction(ISD::XOR, MVT::v2i32, Promote); |
| 410 | AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64); |
| 411 | setOperationAction(ISD::XOR, MVT::v1i64, Legal); |
Bill Wendling | 1b7a81d | 2007-03-16 09:44:46 +0000 | [diff] [blame] | 412 | |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 413 | setOperationAction(ISD::LOAD, MVT::v8i8, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 414 | AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 415 | setOperationAction(ISD::LOAD, MVT::v4i16, Promote); |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 416 | AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64); |
| 417 | setOperationAction(ISD::LOAD, MVT::v2i32, Promote); |
| 418 | AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64); |
| 419 | setOperationAction(ISD::LOAD, MVT::v1i64, Legal); |
Bill Wendling | 2f88dcd | 2007-03-08 22:09:11 +0000 | [diff] [blame] | 420 | |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 421 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom); |
| 422 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); |
| 423 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom); |
| 424 | setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom); |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 425 | |
| 426 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom); |
| 427 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom); |
| 428 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom); |
Bill Wendling | ccc44ad | 2007-03-27 20:22:40 +0000 | [diff] [blame] | 429 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom); |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 430 | |
| 431 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom); |
| 432 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 433 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom); |
| 434 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 437 | if (Subtarget->hasSSE1()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 438 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 439 | |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 440 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 441 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 442 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 443 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 444 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 445 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 446 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 447 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 448 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 449 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 450 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 453 | if (Subtarget->hasSSE2()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 454 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
| 455 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 456 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 457 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 458 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 459 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 460 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 461 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 462 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 463 | setOperationAction(ISD::ADD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 464 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 465 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 466 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
Evan Cheng | 37e8856 | 2007-03-12 22:58:52 +0000 | [diff] [blame] | 467 | setOperationAction(ISD::SUB, MVT::v2i64, Legal); |
Evan Cheng | f998984 | 2006-04-13 05:10:25 +0000 | [diff] [blame] | 468 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 469 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 470 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 471 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 472 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 473 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 474 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 475 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 476 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 477 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 478 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 479 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 480 | // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones. |
| 481 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 482 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 483 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| 484 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 485 | setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom); |
| 486 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom); |
| 487 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom); |
| 488 | } |
| 489 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 490 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 491 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 492 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 493 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| 494 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
| 495 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 496 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 497 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 498 | setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote); |
| 499 | AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64); |
| 500 | setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote); |
| 501 | AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64); |
| 502 | setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote); |
| 503 | AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 504 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote); |
| 505 | AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 506 | setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote); |
| 507 | AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 508 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 509 | |
| 510 | // Custom lower v2i64 and v2f64 selects. |
| 511 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 512 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 513 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 514 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 517 | // We want to custom lower some of our intrinsics. |
| 518 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 519 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 520 | // We have target-specific dag combine patterns for the following nodes: |
| 521 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 522 | setTargetDAGCombine(ISD::SELECT); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 523 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 524 | computeRegisterProperties(); |
| 525 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 526 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 527 | // be smaller when we are in optimizing for size mode. |
Evan Cheng | a03a5dc | 2006-02-14 08:38:30 +0000 | [diff] [blame] | 528 | maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores |
| 529 | maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores |
| 530 | maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 531 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 532 | } |
| 533 | |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 534 | |
| 535 | //===----------------------------------------------------------------------===// |
| 536 | // Return Value Calling Convention Implementation |
| 537 | //===----------------------------------------------------------------------===// |
| 538 | |
Chris Lattner | 59ed56b | 2007-02-28 04:55:35 +0000 | [diff] [blame] | 539 | #include "X86GenCallingConv.inc" |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 541 | /// LowerRET - Lower an ISD::RET node. |
| 542 | SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 543 | assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args"); |
| 544 | |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 545 | SmallVector<CCValAssign, 16> RVLocs; |
| 546 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 547 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
| 548 | CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs); |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 549 | CCInfo.AnalyzeReturn(Op.Val, RetCC_X86); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 551 | |
| 552 | // If this is the first return lowered for this function, add the regs to the |
| 553 | // liveout set for the function. |
| 554 | if (DAG.getMachineFunction().liveout_empty()) { |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 555 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
| 556 | if (RVLocs[i].isRegLoc()) |
| 557 | DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg()); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | SDOperand Chain = Op.getOperand(0); |
| 561 | SDOperand Flag; |
| 562 | |
| 563 | // Copy the result values into the output registers. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 564 | if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() || |
| 565 | RVLocs[0].getLocReg() != X86::ST0) { |
| 566 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 567 | CCValAssign &VA = RVLocs[i]; |
| 568 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 569 | Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), |
| 570 | Flag); |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 571 | Flag = Chain.getValue(1); |
| 572 | } |
| 573 | } else { |
| 574 | // We need to handle a destination of ST0 specially, because it isn't really |
| 575 | // a register. |
| 576 | SDOperand Value = Op.getOperand(1); |
| 577 | |
| 578 | // If this is an FP return with ScalarSSE, we need to move the value from |
| 579 | // an XMM register onto the fp-stack. |
| 580 | if (X86ScalarSSE) { |
| 581 | SDOperand MemLoc; |
| 582 | |
| 583 | // If this is a load into a scalarsse value, don't store the loaded value |
| 584 | // back to the stack, only to reload it: just replace the scalar-sse load. |
| 585 | if (ISD::isNON_EXTLoad(Value.Val) && |
| 586 | (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) { |
| 587 | Chain = Value.getOperand(0); |
| 588 | MemLoc = Value.getOperand(1); |
| 589 | } else { |
| 590 | // Spill the value to memory and reload it into top of stack. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 591 | unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8; |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 592 | MachineFunction &MF = DAG.getMachineFunction(); |
| 593 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 594 | MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 595 | Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0); |
| 596 | } |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 597 | SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other); |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 598 | SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())}; |
Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 599 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); |
| 600 | Chain = Value.getValue(1); |
| 601 | } |
| 602 | |
| 603 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 604 | SDOperand Ops[] = { Chain, Value }; |
| 605 | Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2); |
| 606 | Flag = Chain.getValue(1); |
| 607 | } |
| 608 | |
| 609 | SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16); |
| 610 | if (Flag.Val) |
| 611 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag); |
| 612 | else |
| 613 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop); |
| 614 | } |
| 615 | |
| 616 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 617 | /// LowerCallResult - Lower the result values of an ISD::CALL into the |
| 618 | /// appropriate copies out of appropriate physical registers. This assumes that |
| 619 | /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call |
| 620 | /// being lowered. The returns a SDNode with the same number of values as the |
| 621 | /// ISD::CALL. |
| 622 | SDNode *X86TargetLowering:: |
| 623 | LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall, |
| 624 | unsigned CallingConv, SelectionDAG &DAG) { |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 625 | |
| 626 | // Assign locations to each value returned by this call. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 627 | SmallVector<CCValAssign, 16> RVLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 628 | bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0; |
| 629 | CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs); |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 630 | CCInfo.AnalyzeCallResult(TheCall, RetCC_X86); |
| 631 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 632 | |
Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 633 | SmallVector<SDOperand, 8> ResultVals; |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 634 | |
| 635 | // Copy all of the result registers out of their specified physreg. |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 636 | if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) { |
| 637 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 638 | Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(), |
| 639 | RVLocs[i].getValVT(), InFlag).getValue(1); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 640 | InFlag = Chain.getValue(2); |
| 641 | ResultVals.push_back(Chain.getValue(0)); |
| 642 | } |
| 643 | } else { |
| 644 | // Copies from the FP stack are special, as ST0 isn't a valid register |
| 645 | // before the fp stackifier runs. |
| 646 | |
| 647 | // Copy ST0 into an RFP register with FP_GET_RESULT. |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 648 | SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 649 | SDOperand GROps[] = { Chain, InFlag }; |
| 650 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2); |
| 651 | Chain = RetVal.getValue(1); |
| 652 | InFlag = RetVal.getValue(2); |
| 653 | |
| 654 | // If we are using ScalarSSE, store ST(0) to the stack and reload it into |
| 655 | // an XMM register. |
| 656 | if (X86ScalarSSE) { |
| 657 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 658 | // shouldn't be necessary except that RFP cannot be live across |
| 659 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 660 | MachineFunction &MF = DAG.getMachineFunction(); |
| 661 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 662 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 663 | SDOperand Ops[] = { |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 664 | Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 665 | }; |
| 666 | Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5); |
Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 667 | RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 668 | Chain = RetVal.getValue(1); |
| 669 | } |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 670 | ResultVals.push_back(RetVal); |
| 671 | } |
| 672 | |
| 673 | // Merge everything together with a MERGE_VALUES node. |
| 674 | ResultVals.push_back(Chain); |
| 675 | return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), |
| 676 | &ResultVals[0], ResultVals.size()).Val; |
Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 680 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 681 | // C & StdCall Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 682 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 683 | // StdCall calling convention seems to be standard for many Windows' API |
| 684 | // routines and around. It differs from C calling convention just a little: |
| 685 | // callee should clean up the stack, not caller. Symbols should be also |
| 686 | // decorated in some fancy way :) It doesn't support any vector arguments. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 687 | |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 688 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 689 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 690 | /// register for it. |
| 691 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 692 | const TargetRegisterClass *RC) { |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 693 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 694 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 695 | MF.addLiveIn(PReg, VReg); |
| 696 | return VReg; |
| 697 | } |
| 698 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 699 | SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG, |
| 700 | bool isStdCall) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 701 | unsigned NumArgs = Op.Val->getNumValues() - 1; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 702 | MachineFunction &MF = DAG.getMachineFunction(); |
| 703 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 704 | SDOperand Root = Op.getOperand(0); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 705 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 707 | // Assign locations to all of the incoming arguments. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 708 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 709 | CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg, |
| 710 | getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 711 | CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C); |
| 712 | |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 713 | SmallVector<SDOperand, 8> ArgValues; |
| 714 | unsigned LastVal = ~0U; |
| 715 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 716 | CCValAssign &VA = ArgLocs[i]; |
| 717 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 718 | // places. |
| 719 | assert(VA.getValNo() != LastVal && |
| 720 | "Don't support value assigned to multiple locs yet"); |
| 721 | LastVal = VA.getValNo(); |
| 722 | |
| 723 | if (VA.isRegLoc()) { |
| 724 | MVT::ValueType RegVT = VA.getLocVT(); |
| 725 | TargetRegisterClass *RC; |
| 726 | if (RegVT == MVT::i32) |
| 727 | RC = X86::GR32RegisterClass; |
| 728 | else { |
| 729 | assert(MVT::isVector(RegVT)); |
| 730 | RC = X86::VR128RegisterClass; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 731 | } |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 732 | |
Chris Lattner | 82932a5 | 2007-03-02 05:12:29 +0000 | [diff] [blame] | 733 | unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); |
| 734 | SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT); |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 735 | |
| 736 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 737 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 738 | // right size. |
| 739 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 740 | ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, |
| 741 | DAG.getValueType(VA.getValVT())); |
| 742 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 743 | ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, |
| 744 | DAG.getValueType(VA.getValVT())); |
| 745 | |
| 746 | if (VA.getLocInfo() != CCValAssign::Full) |
| 747 | ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); |
| 748 | |
| 749 | ArgValues.push_back(ArgValue); |
| 750 | } else { |
| 751 | assert(VA.isMemLoc()); |
| 752 | |
| 753 | // Create the nodes corresponding to a load from this parameter slot. |
| 754 | int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8, |
| 755 | VA.getLocMemOffset()); |
| 756 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 757 | ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0)); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 758 | } |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 760 | |
| 761 | unsigned StackSize = CCInfo.getNextStackOffset(); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 762 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 763 | ArgValues.push_back(Root); |
| 764 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 765 | // If the function takes variable number of arguments, make a frame index for |
| 766 | // the start of the first vararg value... for expansion of llvm.va_start. |
Evan Cheng | 4db3af3 | 2006-05-23 21:08:24 +0000 | [diff] [blame] | 767 | if (isVarArg) |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 768 | VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 769 | |
| 770 | if (isStdCall && !isVarArg) { |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 771 | BytesToPopOnReturn = StackSize; // Callee pops everything.. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 772 | BytesCallerReserves = 0; |
| 773 | } else { |
Anton Korobeynikov | 1d9bacc | 2007-03-06 08:12:33 +0000 | [diff] [blame] | 774 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 775 | |
| 776 | // If this is an sret function, the return should pop the hidden pointer. |
Anton Korobeynikov | 1d9bacc | 2007-03-06 08:12:33 +0000 | [diff] [blame] | 777 | if (NumArgs && |
| 778 | (cast<ConstantSDNode>(Op.getOperand(3))->getValue() & |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 779 | ISD::ParamFlags::StructReturn)) |
Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 780 | BytesToPopOnReturn = 4; |
| 781 | |
| 782 | BytesCallerReserves = StackSize; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 783 | } |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 784 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 785 | RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 786 | |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 787 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 788 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 789 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 790 | // Return the new list of results. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 791 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), |
Chris Lattner | 14dd4c9 | 2007-02-26 07:50:02 +0000 | [diff] [blame] | 792 | &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 795 | SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 796 | unsigned CC) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 797 | SDOperand Chain = Op.getOperand(0); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 798 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 799 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 800 | SDOperand Callee = Op.getOperand(4); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 801 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 803 | // Analyze operands of the call, assigning locations to each operand. |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 804 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 805 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 806 | CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 807 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 808 | // Get a count of how many bytes are to be pushed on the stack. |
| 809 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 810 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 811 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 813 | SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; |
| 814 | SmallVector<SDOperand, 8> MemOpChains; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 816 | SDOperand StackPtr; |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 817 | |
| 818 | // Walk the register/memloc assignments, inserting copies/loads. |
| 819 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 820 | CCValAssign &VA = ArgLocs[i]; |
| 821 | SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 823 | // Promote the value if needed. |
| 824 | switch (VA.getLocInfo()) { |
| 825 | default: assert(0 && "Unknown loc info!"); |
| 826 | case CCValAssign::Full: break; |
| 827 | case CCValAssign::SExt: |
| 828 | Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); |
| 829 | break; |
| 830 | case CCValAssign::ZExt: |
| 831 | Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); |
| 832 | break; |
| 833 | case CCValAssign::AExt: |
| 834 | Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); |
| 835 | break; |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 836 | } |
Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 837 | |
| 838 | if (VA.isRegLoc()) { |
| 839 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 840 | } else { |
| 841 | assert(VA.isMemLoc()); |
| 842 | if (StackPtr.Val == 0) |
| 843 | StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy()); |
| 844 | SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy()); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 845 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 846 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 847 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | c0bdf34 | 2007-02-28 05:39:26 +0000 | [diff] [blame] | 850 | // If the first argument is an sret pointer, remember it. |
Anton Korobeynikov | 1d9bacc | 2007-03-06 08:12:33 +0000 | [diff] [blame] | 851 | bool isSRet = NumOps && |
| 852 | (cast<ConstantSDNode>(Op.getOperand(6))->getValue() & |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 853 | ISD::ParamFlags::StructReturn); |
Chris Lattner | c0bdf34 | 2007-02-28 05:39:26 +0000 | [diff] [blame] | 854 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 855 | if (!MemOpChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 856 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 857 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 858 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 859 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 860 | // and flag operands which copy the outgoing args into registers. |
| 861 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 862 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 863 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 864 | InFlag); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 865 | InFlag = Chain.getValue(1); |
| 866 | } |
| 867 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 868 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 869 | // GOT pointer. |
Evan Cheng | 706535d | 2007-01-22 21:34:25 +0000 | [diff] [blame] | 870 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 871 | Subtarget->isPICStyleGOT()) { |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 872 | Chain = DAG.getCopyToReg(Chain, X86::EBX, |
| 873 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 874 | InFlag); |
| 875 | InFlag = Chain.getValue(1); |
| 876 | } |
| 877 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 878 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 879 | // 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] | 880 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 881 | // We should use extra load for direct calls to dllimported functions in |
| 882 | // non-JIT mode. |
| 883 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 884 | getTargetMachine(), true)) |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 885 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 886 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 887 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 888 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 889 | // Returns a chain & a flag for retval copy to use. |
| 890 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 891 | SmallVector<SDOperand, 8> Ops; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 892 | Ops.push_back(Chain); |
| 893 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 894 | |
| 895 | // Add argument registers to the end of the list so that they are known live |
| 896 | // into the call. |
| 897 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 898 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 899 | RegsToPass[i].second.getValueType())); |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 900 | |
| 901 | // Add an implicit use GOT pointer in EBX. |
| 902 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 903 | Subtarget->isPICStyleGOT()) |
| 904 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 905 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 906 | if (InFlag.Val) |
| 907 | Ops.push_back(InFlag); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 908 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 909 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 910 | NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 911 | InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 912 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 913 | // Create the CALLSEQ_END node. |
| 914 | unsigned NumBytesForCalleeToPush = 0; |
| 915 | |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 916 | if (CC == CallingConv::X86_StdCall) { |
| 917 | if (isVarArg) |
Chris Lattner | c0bdf34 | 2007-02-28 05:39:26 +0000 | [diff] [blame] | 918 | NumBytesForCalleeToPush = isSRet ? 4 : 0; |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 919 | else |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 920 | NumBytesForCalleeToPush = NumBytes; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 921 | } else { |
| 922 | // If this is is a call to a struct-return function, the callee |
| 923 | // pops the hidden struct pointer, so we have to push it back. |
| 924 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
Chris Lattner | c0bdf34 | 2007-02-28 05:39:26 +0000 | [diff] [blame] | 925 | NumBytesForCalleeToPush = isSRet ? 4 : 0; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Chris Lattner | 7d53a1c | 2007-02-25 07:18:38 +0000 | [diff] [blame] | 928 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 929 | Ops.clear(); |
| 930 | Ops.push_back(Chain); |
| 931 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 932 | Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 933 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 934 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 935 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 937 | // Handle result values, copying them out of physregs into vregs that we |
| 938 | // return. |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 939 | return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 942 | |
| 943 | //===----------------------------------------------------------------------===// |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 944 | // FastCall Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 945 | //===----------------------------------------------------------------------===// |
| 946 | // |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 947 | // The X86 'fastcall' calling convention passes up to two integer arguments in |
| 948 | // registers (an appropriate portion of ECX/EDX), passes arguments in C order, |
| 949 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 950 | // tail calls), and has the same return value conventions as C calling convs. |
| 951 | // |
| 952 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 953 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 954 | // reasons. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 955 | SDOperand |
Chris Lattner | 2db39b8 | 2007-02-28 06:05:16 +0000 | [diff] [blame] | 956 | X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 957 | MachineFunction &MF = DAG.getMachineFunction(); |
| 958 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 959 | SDOperand Root = Op.getOperand(0); |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 960 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 962 | // Assign locations to all of the incoming arguments. |
Chris Lattner | fc664c1 | 2007-02-28 06:21:19 +0000 | [diff] [blame] | 963 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 964 | CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg, |
| 965 | getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 966 | CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall); |
Chris Lattner | fc664c1 | 2007-02-28 06:21:19 +0000 | [diff] [blame] | 967 | |
| 968 | SmallVector<SDOperand, 8> ArgValues; |
| 969 | unsigned LastVal = ~0U; |
| 970 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 971 | CCValAssign &VA = ArgLocs[i]; |
| 972 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 973 | // places. |
| 974 | assert(VA.getValNo() != LastVal && |
| 975 | "Don't support value assigned to multiple locs yet"); |
| 976 | LastVal = VA.getValNo(); |
| 977 | |
| 978 | if (VA.isRegLoc()) { |
| 979 | MVT::ValueType RegVT = VA.getLocVT(); |
| 980 | TargetRegisterClass *RC; |
| 981 | if (RegVT == MVT::i32) |
| 982 | RC = X86::GR32RegisterClass; |
| 983 | else { |
| 984 | assert(MVT::isVector(RegVT)); |
| 985 | RC = X86::VR128RegisterClass; |
| 986 | } |
| 987 | |
Chris Lattner | 82932a5 | 2007-03-02 05:12:29 +0000 | [diff] [blame] | 988 | unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); |
| 989 | SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT); |
Chris Lattner | fc664c1 | 2007-02-28 06:21:19 +0000 | [diff] [blame] | 990 | |
| 991 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 992 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 993 | // right size. |
| 994 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 995 | ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, |
| 996 | DAG.getValueType(VA.getValVT())); |
| 997 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 998 | ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, |
| 999 | DAG.getValueType(VA.getValVT())); |
| 1000 | |
| 1001 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1002 | ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); |
| 1003 | |
| 1004 | ArgValues.push_back(ArgValue); |
| 1005 | } else { |
| 1006 | assert(VA.isMemLoc()); |
| 1007 | |
| 1008 | // Create the nodes corresponding to a load from this parameter slot. |
| 1009 | int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8, |
| 1010 | VA.getLocMemOffset()); |
| 1011 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 1012 | ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0)); |
| 1013 | } |
| 1014 | } |
| 1015 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1016 | ArgValues.push_back(Root); |
| 1017 | |
Chris Lattner | fc664c1 | 2007-02-28 06:21:19 +0000 | [diff] [blame] | 1018 | unsigned StackSize = CCInfo.getNextStackOffset(); |
Anton Korobeynikov | 9dd9abd | 2007-03-01 16:29:22 +0000 | [diff] [blame] | 1019 | |
Anton Korobeynikov | f7dcfa8 | 2007-03-02 21:50:27 +0000 | [diff] [blame] | 1020 | if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) { |
Anton Korobeynikov | 9dd9abd | 2007-03-01 16:29:22 +0000 | [diff] [blame] | 1021 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1022 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1023 | if ((StackSize & 7) == 0) |
| 1024 | StackSize += 4; |
| 1025 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1026 | |
| 1027 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1028 | RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. |
Chris Lattner | fc664c1 | 2007-02-28 06:21:19 +0000 | [diff] [blame] | 1029 | BytesToPopOnReturn = StackSize; // Callee pops all stack arguments. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1030 | BytesCallerReserves = 0; |
| 1031 | |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1032 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1033 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1034 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1035 | // Return the new list of results. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1036 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), |
Chris Lattner | 14dd4c9 | 2007-02-26 07:50:02 +0000 | [diff] [blame] | 1037 | &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Rafael Espindola | 1b5dcc3 | 2007-08-31 15:06:30 +0000 | [diff] [blame] | 1040 | SDOperand |
| 1041 | X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG, |
| 1042 | const SDOperand &StackPtr, |
| 1043 | const CCValAssign &VA, |
| 1044 | SDOperand Chain, |
| 1045 | SDOperand Arg) { |
| 1046 | SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy()); |
| 1047 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1048 | SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo()); |
| 1049 | unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue(); |
| 1050 | if (Flags & ISD::ParamFlags::ByVal) { |
| 1051 | unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >> |
| 1052 | ISD::ParamFlags::ByValAlignOffs); |
| 1053 | |
| 1054 | assert (Align >= 8); |
| 1055 | unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >> |
| 1056 | ISD::ParamFlags::ByValSizeOffs; |
| 1057 | |
| 1058 | SDOperand AlignNode = DAG.getConstant(Align, MVT::i32); |
| 1059 | SDOperand SizeNode = DAG.getConstant(Size, MVT::i32); |
| 1060 | |
| 1061 | return DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, PtrOff, Arg, SizeNode, |
| 1062 | AlignNode); |
| 1063 | } else { |
| 1064 | return DAG.getStore(Chain, Arg, PtrOff, NULL, 0); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 1068 | SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG, |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 1069 | unsigned CC) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1070 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1071 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1072 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1073 | SDOperand Callee = Op.getOperand(4); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1075 | // Analyze operands of the call, assigning locations to each operand. |
Chris Lattner | f5d280a | 2007-02-28 06:26:33 +0000 | [diff] [blame] | 1076 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1077 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1078 | CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall); |
Chris Lattner | f5d280a | 2007-02-28 06:26:33 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Get a count of how many bytes are to be pushed on the stack. |
| 1081 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1082 | |
Anton Korobeynikov | f7dcfa8 | 2007-03-02 21:50:27 +0000 | [diff] [blame] | 1083 | if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) { |
Anton Korobeynikov | 9dd9abd | 2007-03-01 16:29:22 +0000 | [diff] [blame] | 1084 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1085 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1086 | if ((NumBytes & 7) == 0) |
| 1087 | NumBytes += 4; |
| 1088 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 1090 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | f5d280a | 2007-02-28 06:26:33 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1092 | SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; |
| 1093 | SmallVector<SDOperand, 8> MemOpChains; |
Chris Lattner | f5d280a | 2007-02-28 06:26:33 +0000 | [diff] [blame] | 1094 | |
| 1095 | SDOperand StackPtr; |
| 1096 | |
| 1097 | // Walk the register/memloc assignments, inserting copies/loads. |
| 1098 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1099 | CCValAssign &VA = ArgLocs[i]; |
| 1100 | SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); |
| 1101 | |
| 1102 | // Promote the value if needed. |
| 1103 | switch (VA.getLocInfo()) { |
| 1104 | default: assert(0 && "Unknown loc info!"); |
| 1105 | case CCValAssign::Full: break; |
| 1106 | case CCValAssign::SExt: |
| 1107 | Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); |
Chris Lattner | 2db39b8 | 2007-02-28 06:05:16 +0000 | [diff] [blame] | 1108 | break; |
Chris Lattner | f5d280a | 2007-02-28 06:26:33 +0000 | [diff] [blame] | 1109 | case CCValAssign::ZExt: |
| 1110 | Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); |
| 1111 | break; |
| 1112 | case CCValAssign::AExt: |
| 1113 | Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); |
| 1114 | break; |
| 1115 | } |
| 1116 | |
| 1117 | if (VA.isRegLoc()) { |
| 1118 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 1119 | } else { |
| 1120 | assert(VA.isMemLoc()); |
| 1121 | if (StackPtr.Val == 0) |
| 1122 | StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy()); |
| 1123 | SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1124 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1125 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1126 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1127 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1128 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1129 | if (!MemOpChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1130 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1131 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1132 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1133 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1134 | // and flag operands which copy the outgoing args into registers. |
| 1135 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1136 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1137 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1138 | InFlag); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1139 | InFlag = Chain.getValue(1); |
| 1140 | } |
| 1141 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1142 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1143 | // 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] | 1144 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 1145 | // We should use extra load for direct calls to dllimported functions in |
| 1146 | // non-JIT mode. |
| 1147 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1148 | getTargetMachine(), true)) |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1149 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 1150 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1151 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 1152 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1153 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 1154 | // GOT pointer. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1155 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1156 | Subtarget->isPICStyleGOT()) { |
| 1157 | Chain = DAG.getCopyToReg(Chain, X86::EBX, |
| 1158 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 1159 | InFlag); |
| 1160 | InFlag = Chain.getValue(1); |
| 1161 | } |
| 1162 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 1163 | // Returns a chain & a flag for retval copy to use. |
| 1164 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1165 | SmallVector<SDOperand, 8> Ops; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1166 | Ops.push_back(Chain); |
| 1167 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Add argument registers to the end of the list so that they are known live |
| 1170 | // into the call. |
| 1171 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1172 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1173 | RegsToPass[i].second.getValueType())); |
| 1174 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1175 | // Add an implicit use GOT pointer in EBX. |
| 1176 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1177 | Subtarget->isPICStyleGOT()) |
| 1178 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
| 1179 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1180 | if (InFlag.Val) |
| 1181 | Ops.push_back(InFlag); |
| 1182 | |
| 1183 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 1184 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1185 | NodeTys, &Ops[0], Ops.size()); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1186 | InFlag = Chain.getValue(1); |
| 1187 | |
Chris Lattner | 7d53a1c | 2007-02-25 07:18:38 +0000 | [diff] [blame] | 1188 | // Returns a flag for retval copy to use. |
| 1189 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1190 | Ops.clear(); |
| 1191 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1192 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 1193 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1194 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1195 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
Chris Lattner | 339b439 | 2007-02-25 09:10:05 +0000 | [diff] [blame] | 1196 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 339b439 | 2007-02-25 09:10:05 +0000 | [diff] [blame] | 1198 | // Handle result values, copying them out of physregs into vregs that we |
| 1199 | // return. |
| 1200 | return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1203 | |
| 1204 | //===----------------------------------------------------------------------===// |
| 1205 | // X86-64 C Calling Convention implementation |
| 1206 | //===----------------------------------------------------------------------===// |
| 1207 | |
| 1208 | SDOperand |
| 1209 | X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1210 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1211 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1212 | SDOperand Root = Op.getOperand(0); |
| 1213 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 1214 | |
| 1215 | static const unsigned GPR64ArgRegs[] = { |
| 1216 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1217 | }; |
| 1218 | static const unsigned XMMArgRegs[] = { |
| 1219 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1220 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1221 | }; |
| 1222 | |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Assign locations to all of the incoming arguments. |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1225 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1226 | CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg, |
| 1227 | getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1228 | CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1229 | |
| 1230 | SmallVector<SDOperand, 8> ArgValues; |
| 1231 | unsigned LastVal = ~0U; |
| 1232 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1233 | CCValAssign &VA = ArgLocs[i]; |
| 1234 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 1235 | // places. |
| 1236 | assert(VA.getValNo() != LastVal && |
| 1237 | "Don't support value assigned to multiple locs yet"); |
| 1238 | LastVal = VA.getValNo(); |
| 1239 | |
| 1240 | if (VA.isRegLoc()) { |
| 1241 | MVT::ValueType RegVT = VA.getLocVT(); |
| 1242 | TargetRegisterClass *RC; |
| 1243 | if (RegVT == MVT::i32) |
| 1244 | RC = X86::GR32RegisterClass; |
| 1245 | else if (RegVT == MVT::i64) |
| 1246 | RC = X86::GR64RegisterClass; |
| 1247 | else if (RegVT == MVT::f32) |
| 1248 | RC = X86::FR32RegisterClass; |
| 1249 | else if (RegVT == MVT::f64) |
| 1250 | RC = X86::FR64RegisterClass; |
| 1251 | else { |
| 1252 | assert(MVT::isVector(RegVT)); |
Chris Lattner | fdbe720 | 2007-06-09 05:08:10 +0000 | [diff] [blame] | 1253 | if (MVT::getSizeInBits(RegVT) == 64) { |
| 1254 | RC = X86::GR64RegisterClass; // MMX values are passed in GPRs. |
| 1255 | RegVT = MVT::i64; |
| 1256 | } else |
Chris Lattner | 6b7c21c | 2007-06-09 05:01:50 +0000 | [diff] [blame] | 1257 | RC = X86::VR128RegisterClass; |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | 82932a5 | 2007-03-02 05:12:29 +0000 | [diff] [blame] | 1259 | |
| 1260 | unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC); |
| 1261 | SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1262 | |
| 1263 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 1264 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 1265 | // right size. |
| 1266 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 1267 | ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue, |
| 1268 | DAG.getValueType(VA.getValVT())); |
| 1269 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 1270 | ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue, |
| 1271 | DAG.getValueType(VA.getValVT())); |
| 1272 | |
| 1273 | if (VA.getLocInfo() != CCValAssign::Full) |
| 1274 | ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue); |
| 1275 | |
Chris Lattner | fdbe720 | 2007-06-09 05:08:10 +0000 | [diff] [blame] | 1276 | // Handle MMX values passed in GPRs. |
| 1277 | if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass && |
| 1278 | MVT::getSizeInBits(RegVT) == 64) |
| 1279 | ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue); |
| 1280 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1281 | ArgValues.push_back(ArgValue); |
| 1282 | } else { |
| 1283 | assert(VA.isMemLoc()); |
| 1284 | |
| 1285 | // Create the nodes corresponding to a load from this parameter slot. |
| 1286 | int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8, |
| 1287 | VA.getLocMemOffset()); |
| 1288 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Rafael Espindola | 594d37e | 2007-08-10 14:44:42 +0000 | [diff] [blame] | 1289 | |
| 1290 | unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue(); |
| 1291 | if (Flags & ISD::ParamFlags::ByVal) |
| 1292 | ArgValues.push_back(FIN); |
| 1293 | else |
| 1294 | ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0)); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | unsigned StackSize = CCInfo.getNextStackOffset(); |
| 1299 | |
| 1300 | // If the function takes variable number of arguments, make a frame index for |
| 1301 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 1302 | if (isVarArg) { |
| 1303 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6); |
| 1304 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
| 1305 | |
| 1306 | // For X86-64, if there are vararg parameters that are passed via |
| 1307 | // registers, then we must store them to their spots on the stack so they |
| 1308 | // may be loaded by deferencing the result of va_next. |
| 1309 | VarArgsGPOffset = NumIntRegs * 8; |
| 1310 | VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16; |
| 1311 | VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize); |
| 1312 | RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16); |
| 1313 | |
| 1314 | // Store the integer parameter registers. |
| 1315 | SmallVector<SDOperand, 8> MemOps; |
| 1316 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
| 1317 | SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
| 1318 | DAG.getConstant(VarArgsGPOffset, getPointerTy())); |
| 1319 | for (; NumIntRegs != 6; ++NumIntRegs) { |
| 1320 | unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs], |
| 1321 | X86::GR64RegisterClass); |
| 1322 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64); |
| 1323 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
| 1324 | MemOps.push_back(Store); |
| 1325 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 1326 | DAG.getConstant(8, getPointerTy())); |
| 1327 | } |
| 1328 | |
| 1329 | // Now store the XMM (fp + vector) parameter registers. |
| 1330 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
| 1331 | DAG.getConstant(VarArgsFPOffset, getPointerTy())); |
| 1332 | for (; NumXMMRegs != 8; ++NumXMMRegs) { |
| 1333 | unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], |
| 1334 | X86::VR128RegisterClass); |
| 1335 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32); |
| 1336 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
| 1337 | MemOps.push_back(Store); |
| 1338 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 1339 | DAG.getConstant(16, getPointerTy())); |
| 1340 | } |
| 1341 | if (!MemOps.empty()) |
| 1342 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1343 | &MemOps[0], MemOps.size()); |
| 1344 | } |
| 1345 | |
| 1346 | ArgValues.push_back(Root); |
| 1347 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1348 | BytesToPopOnReturn = 0; // Callee pops nothing. |
| 1349 | BytesCallerReserves = StackSize; |
| 1350 | |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1351 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1352 | FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn); |
| 1353 | |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1354 | // Return the new list of results. |
| 1355 | return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), |
| 1356 | &ArgValues[0], ArgValues.size()).getValue(Op.ResNo); |
| 1357 | } |
| 1358 | |
| 1359 | SDOperand |
| 1360 | X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG, |
| 1361 | unsigned CC) { |
| 1362 | SDOperand Chain = Op.getOperand(0); |
| 1363 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 1364 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 1365 | SDOperand Callee = Op.getOperand(4); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Analyze operands of the call, assigning locations to each operand. |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1368 | SmallVector<CCValAssign, 16> ArgLocs; |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 1369 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs); |
Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1370 | CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Get a count of how many bytes are to be pushed on the stack. |
| 1373 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| 1374 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
| 1375 | |
| 1376 | SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass; |
| 1377 | SmallVector<SDOperand, 8> MemOpChains; |
| 1378 | |
| 1379 | SDOperand StackPtr; |
| 1380 | |
| 1381 | // Walk the register/memloc assignments, inserting copies/loads. |
| 1382 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1383 | CCValAssign &VA = ArgLocs[i]; |
| 1384 | SDOperand Arg = Op.getOperand(5+2*VA.getValNo()); |
| 1385 | |
| 1386 | // Promote the value if needed. |
| 1387 | switch (VA.getLocInfo()) { |
| 1388 | default: assert(0 && "Unknown loc info!"); |
| 1389 | case CCValAssign::Full: break; |
| 1390 | case CCValAssign::SExt: |
| 1391 | Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg); |
| 1392 | break; |
| 1393 | case CCValAssign::ZExt: |
| 1394 | Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg); |
| 1395 | break; |
| 1396 | case CCValAssign::AExt: |
| 1397 | Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg); |
| 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | if (VA.isRegLoc()) { |
| 1402 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 1403 | } else { |
| 1404 | assert(VA.isMemLoc()); |
| 1405 | if (StackPtr.Val == 0) |
| 1406 | StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy()); |
Rafael Espindola | 21485be | 2007-08-20 15:18:24 +0000 | [diff] [blame] | 1407 | |
Rafael Espindola | 1b5dcc3 | 2007-08-31 15:06:30 +0000 | [diff] [blame] | 1408 | MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain, |
| 1409 | Arg)); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if (!MemOpChains.empty()) |
| 1414 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1415 | &MemOpChains[0], MemOpChains.size()); |
| 1416 | |
| 1417 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1418 | // and flag operands which copy the outgoing args into registers. |
| 1419 | SDOperand InFlag; |
| 1420 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1421 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1422 | InFlag); |
| 1423 | InFlag = Chain.getValue(1); |
| 1424 | } |
| 1425 | |
| 1426 | if (isVarArg) { |
| 1427 | // From AMD64 ABI document: |
| 1428 | // For calls that may call functions that use varargs or stdargs |
| 1429 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 1430 | // the declaration) %al is used as hidden argument to specify the number |
| 1431 | // of SSE registers used. The contents of %al do not need to match exactly |
| 1432 | // the number of registers, but must be an ubound on the number of SSE |
| 1433 | // registers used and is in the range 0 - 8 inclusive. |
| 1434 | |
| 1435 | // Count the number of XMM registers allocated. |
| 1436 | static const unsigned XMMArgRegs[] = { |
| 1437 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1438 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1439 | }; |
| 1440 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
| 1441 | |
| 1442 | Chain = DAG.getCopyToReg(Chain, X86::AL, |
| 1443 | DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); |
| 1444 | InFlag = Chain.getValue(1); |
| 1445 | } |
| 1446 | |
| 1447 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1448 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 1449 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 1450 | // We should use extra load for direct calls to dllimported functions in |
| 1451 | // non-JIT mode. |
Evan Cheng | ba69300 | 2007-03-14 22:11:11 +0000 | [diff] [blame] | 1452 | if (getTargetMachine().getCodeModel() != CodeModel::Large |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1453 | && !Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1454 | getTargetMachine(), true)) |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1455 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 1456 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | ba69300 | 2007-03-14 22:11:11 +0000 | [diff] [blame] | 1457 | if (getTargetMachine().getCodeModel() != CodeModel::Large) |
| 1458 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Returns a chain & a flag for retval copy to use. |
| 1461 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 1462 | SmallVector<SDOperand, 8> Ops; |
| 1463 | Ops.push_back(Chain); |
| 1464 | Ops.push_back(Callee); |
| 1465 | |
| 1466 | // Add argument registers to the end of the list so that they are known live |
| 1467 | // into the call. |
| 1468 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 1469 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 1470 | RegsToPass[i].second.getValueType())); |
| 1471 | |
| 1472 | if (InFlag.Val) |
| 1473 | Ops.push_back(InFlag); |
| 1474 | |
| 1475 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
| 1476 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 1477 | NodeTys, &Ops[0], Ops.size()); |
| 1478 | InFlag = Chain.getValue(1); |
| 1479 | |
| 1480 | // Returns a flag for retval copy to use. |
| 1481 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 1482 | Ops.clear(); |
| 1483 | Ops.push_back(Chain); |
| 1484 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 1485 | Ops.push_back(DAG.getConstant(0, getPointerTy())); |
| 1486 | Ops.push_back(InFlag); |
| 1487 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
| 1488 | InFlag = Chain.getValue(1); |
| 1489 | |
| 1490 | // Handle result values, copying them out of physregs into vregs that we |
| 1491 | // return. |
| 1492 | return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo); |
| 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | //===----------------------------------------------------------------------===// |
| 1497 | // Other Lowering Hooks |
| 1498 | //===----------------------------------------------------------------------===// |
| 1499 | |
| 1500 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1501 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1502 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1503 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1504 | int ReturnAddrIndex = FuncInfo->getRAIndex(); |
| 1505 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1506 | if (ReturnAddrIndex == 0) { |
| 1507 | // Set up a frame object for the return address. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1508 | if (Subtarget->is64Bit()) |
| 1509 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8); |
| 1510 | else |
| 1511 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 1512 | |
| 1513 | FuncInfo->setRAIndex(ReturnAddrIndex); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1516 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1521 | /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 1522 | /// specific condition code. It returns a false if it cannot do a direct |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1523 | /// translation. X86CC is the translated CondCode. LHS/RHS are modified as |
| 1524 | /// needed. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1525 | static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1526 | unsigned &X86CC, SDOperand &LHS, SDOperand &RHS, |
| 1527 | SelectionDAG &DAG) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1528 | X86CC = X86::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1529 | if (!isFP) { |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1530 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 1531 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 1532 | // X > -1 -> X == 0, jump !sign. |
| 1533 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1534 | X86CC = X86::COND_NS; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1535 | return true; |
| 1536 | } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| 1537 | // X < 0 -> X == 0, jump on sign. |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1538 | X86CC = X86::COND_S; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1539 | return true; |
| 1540 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1541 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1542 | |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1543 | switch (SetCCOpcode) { |
| 1544 | default: break; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1545 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
| 1546 | case ISD::SETGT: X86CC = X86::COND_G; break; |
| 1547 | case ISD::SETGE: X86CC = X86::COND_GE; break; |
| 1548 | case ISD::SETLT: X86CC = X86::COND_L; break; |
| 1549 | case ISD::SETLE: X86CC = X86::COND_LE; break; |
| 1550 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 1551 | case ISD::SETULT: X86CC = X86::COND_B; break; |
| 1552 | case ISD::SETUGT: X86CC = X86::COND_A; break; |
| 1553 | case ISD::SETULE: X86CC = X86::COND_BE; break; |
| 1554 | case ISD::SETUGE: X86CC = X86::COND_AE; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } else { |
| 1557 | // On a floating point condition, the flags are set as follows: |
| 1558 | // ZF PF CF op |
| 1559 | // 0 | 0 | 0 | X > Y |
| 1560 | // 0 | 0 | 1 | X < Y |
| 1561 | // 1 | 0 | 0 | X == Y |
| 1562 | // 1 | 1 | 1 | unordered |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1563 | bool Flip = false; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1564 | switch (SetCCOpcode) { |
| 1565 | default: break; |
| 1566 | case ISD::SETUEQ: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1567 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1568 | case ISD::SETOLT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1569 | case ISD::SETOGT: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1570 | case ISD::SETGT: X86CC = X86::COND_A; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1571 | case ISD::SETOLE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1572 | case ISD::SETOGE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1573 | case ISD::SETGE: X86CC = X86::COND_AE; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1574 | case ISD::SETUGT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1575 | case ISD::SETULT: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1576 | case ISD::SETLT: X86CC = X86::COND_B; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1577 | case ISD::SETUGE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1578 | case ISD::SETULE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1579 | case ISD::SETLE: X86CC = X86::COND_BE; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1580 | case ISD::SETONE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1581 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 1582 | case ISD::SETUO: X86CC = X86::COND_P; break; |
| 1583 | case ISD::SETO: X86CC = X86::COND_NP; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1584 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1585 | if (Flip) |
| 1586 | std::swap(LHS, RHS); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1587 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1589 | return X86CC != X86::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1592 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 1593 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1594 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1595 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1596 | switch (X86CC) { |
| 1597 | default: |
| 1598 | return false; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1599 | case X86::COND_B: |
| 1600 | case X86::COND_BE: |
| 1601 | case X86::COND_E: |
| 1602 | case X86::COND_P: |
| 1603 | case X86::COND_A: |
| 1604 | case X86::COND_AE: |
| 1605 | case X86::COND_NE: |
| 1606 | case X86::COND_NP: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1607 | return true; |
| 1608 | } |
| 1609 | } |
| 1610 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1611 | /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1612 | /// true if Op is undef or if its value falls within the specified range (L, H]. |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1613 | static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) { |
| 1614 | if (Op.getOpcode() == ISD::UNDEF) |
| 1615 | return true; |
| 1616 | |
| 1617 | unsigned Val = cast<ConstantSDNode>(Op)->getValue(); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1618 | return (Val >= Low && Val < Hi); |
| 1619 | } |
| 1620 | |
| 1621 | /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return |
| 1622 | /// true if Op is undef or if its value equal to the specified value. |
| 1623 | static bool isUndefOrEqual(SDOperand Op, unsigned Val) { |
| 1624 | if (Op.getOpcode() == ISD::UNDEF) |
| 1625 | return true; |
| 1626 | return cast<ConstantSDNode>(Op)->getValue() == Val; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 1629 | /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1630 | /// specifies a shuffle of elements that is suitable for input to PSHUFD. |
| 1631 | bool X86::isPSHUFDMask(SDNode *N) { |
| 1632 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1633 | |
Dan Gohman | 7f55fcb | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 1634 | if (N->getNumOperands() != 2 && N->getNumOperands() != 4) |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | |
| 1637 | // Check if the value doesn't reference the second vector. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1638 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1639 | SDOperand Arg = N->getOperand(i); |
| 1640 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1641 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Dan Gohman | 7f55fcb | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 1642 | if (cast<ConstantSDNode>(Arg)->getValue() >= e) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
| 1645 | |
| 1646 | return true; |
| 1647 | } |
| 1648 | |
| 1649 | /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 1650 | /// specifies a shuffle of elements that is suitable for input to PSHUFHW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1651 | bool X86::isPSHUFHWMask(SDNode *N) { |
| 1652 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1653 | |
| 1654 | if (N->getNumOperands() != 8) |
| 1655 | return false; |
| 1656 | |
| 1657 | // Lower quadword copied in order. |
| 1658 | for (unsigned i = 0; i != 4; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1659 | SDOperand Arg = N->getOperand(i); |
| 1660 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1661 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1662 | if (cast<ConstantSDNode>(Arg)->getValue() != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1663 | return false; |
| 1664 | } |
| 1665 | |
| 1666 | // Upper quadword shuffled. |
| 1667 | for (unsigned i = 4; i != 8; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1668 | SDOperand Arg = N->getOperand(i); |
| 1669 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1670 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1671 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1672 | if (Val < 4 || Val > 7) |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
| 1679 | /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 1680 | /// specifies a shuffle of elements that is suitable for input to PSHUFLW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1681 | bool X86::isPSHUFLWMask(SDNode *N) { |
| 1682 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1683 | |
| 1684 | if (N->getNumOperands() != 8) |
| 1685 | return false; |
| 1686 | |
| 1687 | // Upper quadword copied in order. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1688 | for (unsigned i = 4; i != 8; ++i) |
| 1689 | if (!isUndefOrEqual(N->getOperand(i), i)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1690 | return false; |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Lower quadword shuffled. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1693 | for (unsigned i = 0; i != 4; ++i) |
| 1694 | if (!isUndefOrInRange(N->getOperand(i), 0, 4)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1695 | return false; |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 1696 | |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1700 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1701 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1702 | static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1703 | if (NumElems != 2 && NumElems != 4) return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1704 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1705 | unsigned Half = NumElems / 2; |
| 1706 | for (unsigned i = 0; i < Half; ++i) |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1707 | if (!isUndefOrInRange(Elems[i], 0, NumElems)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | for (unsigned i = Half; i < NumElems; ++i) |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1710 | if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1711 | return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1712 | |
| 1713 | return true; |
| 1714 | } |
| 1715 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1716 | bool X86::isSHUFPMask(SDNode *N) { |
| 1717 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1718 | return ::isSHUFPMask(N->op_begin(), N->getNumOperands()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 1721 | /// isCommutedSHUFP - Returns true if the shuffle mask is exactly |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1722 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 1723 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 1724 | /// the upper half to come from vector 2. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1725 | static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) { |
| 1726 | if (NumOps != 2 && NumOps != 4) return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1728 | unsigned Half = NumOps / 2; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1729 | for (unsigned i = 0; i < Half; ++i) |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1730 | if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1731 | return false; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1732 | for (unsigned i = Half; i < NumOps; ++i) |
| 1733 | if (!isUndefOrInRange(Ops[i], 0, NumOps)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1734 | return false; |
| 1735 | return true; |
| 1736 | } |
| 1737 | |
| 1738 | static bool isCommutedSHUFP(SDNode *N) { |
| 1739 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1740 | return isCommutedSHUFP(N->op_begin(), N->getNumOperands()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 1743 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1744 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
| 1745 | bool X86::isMOVHLPSMask(SDNode *N) { |
| 1746 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1747 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1748 | if (N->getNumOperands() != 4) |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 1749 | return false; |
| 1750 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1751 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1752 | return isUndefOrEqual(N->getOperand(0), 6) && |
| 1753 | isUndefOrEqual(N->getOperand(1), 7) && |
| 1754 | isUndefOrEqual(N->getOperand(2), 2) && |
| 1755 | isUndefOrEqual(N->getOperand(3), 3); |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 1758 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 1759 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 1760 | /// <2, 3, 2, 3> |
| 1761 | bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) { |
| 1762 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1763 | |
| 1764 | if (N->getNumOperands() != 4) |
| 1765 | return false; |
| 1766 | |
| 1767 | // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3 |
| 1768 | return isUndefOrEqual(N->getOperand(0), 2) && |
| 1769 | isUndefOrEqual(N->getOperand(1), 3) && |
| 1770 | isUndefOrEqual(N->getOperand(2), 2) && |
| 1771 | isUndefOrEqual(N->getOperand(3), 3); |
| 1772 | } |
| 1773 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1774 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1775 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
| 1776 | bool X86::isMOVLPMask(SDNode *N) { |
| 1777 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1778 | |
| 1779 | unsigned NumElems = N->getNumOperands(); |
| 1780 | if (NumElems != 2 && NumElems != 4) |
| 1781 | return false; |
| 1782 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1783 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 1784 | if (!isUndefOrEqual(N->getOperand(i), i + NumElems)) |
| 1785 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1786 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1787 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
| 1788 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 1789 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1790 | |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| 1794 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 1795 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 1796 | /// and MOVLHPS. |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1797 | bool X86::isMOVHPMask(SDNode *N) { |
| 1798 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1799 | |
| 1800 | unsigned NumElems = N->getNumOperands(); |
| 1801 | if (NumElems != 2 && NumElems != 4) |
| 1802 | return false; |
| 1803 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1804 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 1805 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 1806 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1807 | |
| 1808 | for (unsigned i = 0; i < NumElems/2; ++i) { |
| 1809 | SDOperand Arg = N->getOperand(i + NumElems/2); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1810 | if (!isUndefOrEqual(Arg, i + NumElems)) |
| 1811 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | return true; |
| 1815 | } |
| 1816 | |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1817 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1818 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1819 | bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts, |
| 1820 | bool V2IsSplat = false) { |
| 1821 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1822 | return false; |
| 1823 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1824 | for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 1825 | SDOperand BitI = Elts[i]; |
| 1826 | SDOperand BitI1 = Elts[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1827 | if (!isUndefOrEqual(BitI, j)) |
| 1828 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1829 | if (V2IsSplat) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1830 | if (isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1831 | return false; |
| 1832 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1833 | if (!isUndefOrEqual(BitI1, j + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1834 | return false; |
| 1835 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | return true; |
| 1839 | } |
| 1840 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1841 | bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) { |
| 1842 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1843 | return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1846 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1847 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1848 | bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts, |
| 1849 | bool V2IsSplat = false) { |
| 1850 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1851 | return false; |
| 1852 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1853 | for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) { |
| 1854 | SDOperand BitI = Elts[i]; |
| 1855 | SDOperand BitI1 = Elts[i+1]; |
| 1856 | if (!isUndefOrEqual(BitI, j + NumElts/2)) |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1857 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1858 | if (V2IsSplat) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1859 | if (isUndefOrEqual(BitI1, NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1860 | return false; |
| 1861 | } else { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1862 | if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts)) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | return true; |
| 1868 | } |
| 1869 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1870 | bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) { |
| 1871 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1872 | return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1875 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 1876 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 1877 | /// <0, 0, 1, 1> |
| 1878 | bool X86::isUNPCKL_v_undef_Mask(SDNode *N) { |
| 1879 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1880 | |
| 1881 | unsigned NumElems = N->getNumOperands(); |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 1882 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1883 | return false; |
| 1884 | |
| 1885 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 1886 | SDOperand BitI = N->getOperand(i); |
| 1887 | SDOperand BitI1 = N->getOperand(i+1); |
| 1888 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1889 | if (!isUndefOrEqual(BitI, j)) |
| 1890 | return false; |
| 1891 | if (!isUndefOrEqual(BitI1, j)) |
| 1892 | return false; |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | return true; |
| 1896 | } |
| 1897 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 1898 | /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form |
| 1899 | /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, |
| 1900 | /// <2, 2, 3, 3> |
| 1901 | bool X86::isUNPCKH_v_undef_Mask(SDNode *N) { |
| 1902 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1903 | |
| 1904 | unsigned NumElems = N->getNumOperands(); |
| 1905 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 1906 | return false; |
| 1907 | |
| 1908 | for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) { |
| 1909 | SDOperand BitI = N->getOperand(i); |
| 1910 | SDOperand BitI1 = N->getOperand(i + 1); |
| 1911 | |
| 1912 | if (!isUndefOrEqual(BitI, j)) |
| 1913 | return false; |
| 1914 | if (!isUndefOrEqual(BitI1, j)) |
| 1915 | return false; |
| 1916 | } |
| 1917 | |
| 1918 | return true; |
| 1919 | } |
| 1920 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1921 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1922 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 1923 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1924 | static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) { |
| 1925 | if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1926 | return false; |
| 1927 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1928 | if (!isUndefOrEqual(Elts[0], NumElts)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1929 | return false; |
| 1930 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1931 | for (unsigned i = 1; i < NumElts; ++i) { |
| 1932 | if (!isUndefOrEqual(Elts[i], i)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1933 | return false; |
| 1934 | } |
| 1935 | |
| 1936 | return true; |
| 1937 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1938 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1939 | bool X86::isMOVLMask(SDNode *N) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1940 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1941 | return ::isMOVLMask(N->op_begin(), N->getNumOperands()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1944 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 1945 | /// 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] | 1946 | /// element of vector 2 and the other elements to come from vector 1 in order. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1947 | static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps, |
| 1948 | bool V2IsSplat = false, |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 1949 | bool V2IsUndef = false) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1950 | if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1951 | return false; |
| 1952 | |
| 1953 | if (!isUndefOrEqual(Ops[0], 0)) |
| 1954 | return false; |
| 1955 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1956 | for (unsigned i = 1; i < NumOps; ++i) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1957 | SDOperand Arg = Ops[i]; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1958 | if (!(isUndefOrEqual(Arg, i+NumOps) || |
| 1959 | (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) || |
| 1960 | (V2IsSplat && isUndefOrEqual(Arg, NumOps)))) |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 1961 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | return true; |
| 1965 | } |
| 1966 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 1967 | static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false, |
| 1968 | bool V2IsUndef = false) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1969 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 1970 | return isCommutedMOVL(N->op_begin(), N->getNumOperands(), |
| 1971 | V2IsSplat, V2IsUndef); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1974 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1975 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
| 1976 | bool X86::isMOVSHDUPMask(SDNode *N) { |
| 1977 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1978 | |
| 1979 | if (N->getNumOperands() != 4) |
| 1980 | return false; |
| 1981 | |
| 1982 | // Expect 1, 1, 3, 3 |
| 1983 | for (unsigned i = 0; i < 2; ++i) { |
| 1984 | SDOperand Arg = N->getOperand(i); |
| 1985 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1986 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1987 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1988 | if (Val != 1) return false; |
| 1989 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1990 | |
| 1991 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1992 | for (unsigned i = 2; i < 4; ++i) { |
| 1993 | SDOperand Arg = N->getOperand(i); |
| 1994 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1995 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1996 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1997 | if (Val != 3) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1998 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1999 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 2000 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2001 | // Don't use movshdup if it can be done with a shufps. |
| 2002 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2006 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
| 2007 | bool X86::isMOVSLDUPMask(SDNode *N) { |
| 2008 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2009 | |
| 2010 | if (N->getNumOperands() != 4) |
| 2011 | return false; |
| 2012 | |
| 2013 | // Expect 0, 0, 2, 2 |
| 2014 | for (unsigned i = 0; i < 2; ++i) { |
| 2015 | SDOperand Arg = N->getOperand(i); |
| 2016 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2017 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2018 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2019 | if (Val != 0) return false; |
| 2020 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2021 | |
| 2022 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2023 | for (unsigned i = 2; i < 4; ++i) { |
| 2024 | SDOperand Arg = N->getOperand(i); |
| 2025 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2026 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2027 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2028 | if (Val != 2) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2029 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2030 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 2031 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2032 | // Don't use movshdup if it can be done with a shufps. |
| 2033 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Evan Cheng | 49892af | 2007-06-19 00:02:56 +0000 | [diff] [blame] | 2036 | /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2037 | /// specifies a identity operation on the LHS or RHS. |
| 2038 | static bool isIdentityMask(SDNode *N, bool RHS = false) { |
| 2039 | unsigned NumElems = N->getNumOperands(); |
| 2040 | for (unsigned i = 0; i < NumElems; ++i) |
| 2041 | if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0))) |
| 2042 | return false; |
| 2043 | return true; |
| 2044 | } |
| 2045 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2046 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2047 | /// a splat of a single element. |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2048 | static bool isSplatMask(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2049 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2050 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2051 | // This is a splat operation if each element of the permute is the same, and |
| 2052 | // if the value doesn't reference the second vector. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2053 | unsigned NumElems = N->getNumOperands(); |
| 2054 | SDOperand ElementBase; |
| 2055 | unsigned i = 0; |
| 2056 | for (; i != NumElems; ++i) { |
| 2057 | SDOperand Elt = N->getOperand(i); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 2058 | if (isa<ConstantSDNode>(Elt)) { |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2059 | ElementBase = Elt; |
| 2060 | break; |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | if (!ElementBase.Val) |
| 2065 | return false; |
| 2066 | |
| 2067 | for (; i != NumElems; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2068 | SDOperand Arg = N->getOperand(i); |
| 2069 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2070 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2071 | if (Arg != ElementBase) return false; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | // Make sure it is a splat of the first vector operand. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2075 | return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2078 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2079 | /// a splat of a single element and it's a 2 or 4 element mask. |
| 2080 | bool X86::isSplatMask(SDNode *N) { |
| 2081 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2082 | |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2083 | // We can only splat 64-bit, and 32-bit quantities with a single instruction. |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2084 | if (N->getNumOperands() != 4 && N->getNumOperands() != 2) |
| 2085 | return false; |
| 2086 | return ::isSplatMask(N); |
| 2087 | } |
| 2088 | |
Evan Cheng | f686d9b | 2006-10-27 21:08:32 +0000 | [diff] [blame] | 2089 | /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2090 | /// specifies a splat of zero element. |
| 2091 | bool X86::isSplatLoMask(SDNode *N) { |
| 2092 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2093 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2094 | for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) |
Evan Cheng | f686d9b | 2006-10-27 21:08:32 +0000 | [diff] [blame] | 2095 | if (!isUndefOrEqual(N->getOperand(i), 0)) |
| 2096 | return false; |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2100 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 2101 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 2102 | /// instructions. |
| 2103 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2104 | unsigned NumOperands = N->getNumOperands(); |
| 2105 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 2106 | unsigned Mask = 0; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2107 | for (unsigned i = 0; i < NumOperands; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2108 | unsigned Val = 0; |
| 2109 | SDOperand Arg = N->getOperand(NumOperands-i-1); |
| 2110 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2111 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2112 | if (Val >= NumOperands) Val -= NumOperands; |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2113 | Mask |= Val; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2114 | if (i != NumOperands - 1) |
| 2115 | Mask <<= Shift; |
| 2116 | } |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2117 | |
| 2118 | return Mask; |
| 2119 | } |
| 2120 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2121 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 2122 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 2123 | /// instructions. |
| 2124 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
| 2125 | unsigned Mask = 0; |
| 2126 | // 8 nodes, but we only care about the last 4. |
| 2127 | for (unsigned i = 7; i >= 4; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2128 | unsigned Val = 0; |
| 2129 | SDOperand Arg = N->getOperand(i); |
| 2130 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2131 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2132 | Mask |= (Val - 4); |
| 2133 | if (i != 4) |
| 2134 | Mask <<= 2; |
| 2135 | } |
| 2136 | |
| 2137 | return Mask; |
| 2138 | } |
| 2139 | |
| 2140 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2141 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2142 | /// instructions. |
| 2143 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
| 2144 | unsigned Mask = 0; |
| 2145 | // 8 nodes, but we only care about the first 4. |
| 2146 | for (int i = 3; i >= 0; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2147 | unsigned Val = 0; |
| 2148 | SDOperand Arg = N->getOperand(i); |
| 2149 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2150 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2151 | Mask |= Val; |
| 2152 | if (i != 0) |
| 2153 | Mask <<= 2; |
| 2154 | } |
| 2155 | |
| 2156 | return Mask; |
| 2157 | } |
| 2158 | |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 2159 | /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand |
| 2160 | /// specifies a 8 element shuffle that can be broken into a pair of |
| 2161 | /// PSHUFHW and PSHUFLW. |
| 2162 | static bool isPSHUFHW_PSHUFLWMask(SDNode *N) { |
| 2163 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2164 | |
| 2165 | if (N->getNumOperands() != 8) |
| 2166 | return false; |
| 2167 | |
| 2168 | // Lower quadword shuffled. |
| 2169 | for (unsigned i = 0; i != 4; ++i) { |
| 2170 | SDOperand Arg = N->getOperand(i); |
| 2171 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2172 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2173 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2174 | if (Val > 4) |
| 2175 | return false; |
| 2176 | } |
| 2177 | |
| 2178 | // Upper quadword shuffled. |
| 2179 | for (unsigned i = 4; i != 8; ++i) { |
| 2180 | SDOperand Arg = N->getOperand(i); |
| 2181 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2182 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2183 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2184 | if (Val < 4 || Val > 7) |
| 2185 | return false; |
| 2186 | } |
| 2187 | |
| 2188 | return true; |
| 2189 | } |
| 2190 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2191 | /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as |
| 2192 | /// values in ther permute mask. |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2193 | static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1, |
| 2194 | SDOperand &V2, SDOperand &Mask, |
| 2195 | SelectionDAG &DAG) { |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2196 | MVT::ValueType VT = Op.getValueType(); |
| 2197 | MVT::ValueType MaskVT = Mask.getValueType(); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2198 | MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2199 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2200 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2201 | |
| 2202 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2203 | SDOperand Arg = Mask.getOperand(i); |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 2204 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2205 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 2206 | continue; |
| 2207 | } |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2208 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2209 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2210 | if (Val < NumElems) |
| 2211 | MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); |
| 2212 | else |
| 2213 | MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); |
| 2214 | } |
| 2215 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2216 | std::swap(V1, V2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2217 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2218 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2221 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2222 | /// match movhlps. The lower half elements should come from upper half of |
| 2223 | /// 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] | 2224 | /// half of V2 (and in order). |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2225 | static bool ShouldXformToMOVHLPS(SDNode *Mask) { |
| 2226 | unsigned NumElems = Mask->getNumOperands(); |
| 2227 | if (NumElems != 4) |
| 2228 | return false; |
| 2229 | for (unsigned i = 0, e = 2; i != e; ++i) |
| 2230 | if (!isUndefOrEqual(Mask->getOperand(i), i+2)) |
| 2231 | return false; |
| 2232 | for (unsigned i = 2; i != 4; ++i) |
| 2233 | if (!isUndefOrEqual(Mask->getOperand(i), i+4)) |
| 2234 | return false; |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2238 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
| 2239 | /// is promoted to a vector. |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2240 | static inline bool isScalarLoadToVector(SDNode *N) { |
| 2241 | if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 2242 | N = N->getOperand(0).Val; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2243 | return ISD::isNON_EXTLoad(N); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2244 | } |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2248 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2249 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2250 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2251 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2252 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2253 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2254 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2255 | return false; |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2256 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 2257 | // load folding shufps op. |
| 2258 | if (ISD::isNON_EXTLoad(V2)) |
| 2259 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2260 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2261 | unsigned NumElems = Mask->getNumOperands(); |
| 2262 | if (NumElems != 2 && NumElems != 4) |
| 2263 | return false; |
| 2264 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| 2265 | if (!isUndefOrEqual(Mask->getOperand(i), i)) |
| 2266 | return false; |
| 2267 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
| 2268 | if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems)) |
| 2269 | return false; |
| 2270 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2273 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2274 | /// all the same. |
| 2275 | static bool isSplatVector(SDNode *N) { |
| 2276 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2277 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2278 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2279 | SDOperand SplatValue = N->getOperand(0); |
| 2280 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2281 | if (N->getOperand(i) != SplatValue) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2282 | return false; |
| 2283 | return true; |
| 2284 | } |
| 2285 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2286 | /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| 2287 | /// to an undef. |
| 2288 | static bool isUndefShuffle(SDNode *N) { |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2289 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2290 | return false; |
| 2291 | |
| 2292 | SDOperand V1 = N->getOperand(0); |
| 2293 | SDOperand V2 = N->getOperand(1); |
| 2294 | SDOperand Mask = N->getOperand(2); |
| 2295 | unsigned NumElems = Mask.getNumOperands(); |
| 2296 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2297 | SDOperand Arg = Mask.getOperand(i); |
| 2298 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2299 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2300 | if (Val < NumElems && V1.getOpcode() != ISD::UNDEF) |
| 2301 | return false; |
| 2302 | else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF) |
| 2303 | return false; |
| 2304 | } |
| 2305 | } |
| 2306 | return true; |
| 2307 | } |
| 2308 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2309 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2310 | /// constant +0.0. |
| 2311 | static inline bool isZeroNode(SDOperand Elt) { |
| 2312 | return ((isa<ConstantSDNode>(Elt) && |
| 2313 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 2314 | (isa<ConstantFPSDNode>(Elt) && |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 2315 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| 2319 | /// to an zero vector. |
| 2320 | static bool isZeroShuffle(SDNode *N) { |
| 2321 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) |
| 2322 | return false; |
| 2323 | |
| 2324 | SDOperand V1 = N->getOperand(0); |
| 2325 | SDOperand V2 = N->getOperand(1); |
| 2326 | SDOperand Mask = N->getOperand(2); |
| 2327 | unsigned NumElems = Mask.getNumOperands(); |
| 2328 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2329 | SDOperand Arg = Mask.getOperand(i); |
| 2330 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2331 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 2332 | if (Idx < NumElems) { |
| 2333 | unsigned Opc = V1.Val->getOpcode(); |
| 2334 | if (Opc == ISD::UNDEF) |
| 2335 | continue; |
| 2336 | if (Opc != ISD::BUILD_VECTOR || |
| 2337 | !isZeroNode(V1.Val->getOperand(Idx))) |
| 2338 | return false; |
| 2339 | } else if (Idx >= NumElems) { |
| 2340 | unsigned Opc = V2.Val->getOpcode(); |
| 2341 | if (Opc == ISD::UNDEF) |
| 2342 | continue; |
| 2343 | if (Opc != ISD::BUILD_VECTOR || |
| 2344 | !isZeroNode(V2.Val->getOperand(Idx - NumElems))) |
| 2345 | return false; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | return true; |
| 2350 | } |
| 2351 | |
| 2352 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2353 | /// |
| 2354 | static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) { |
| 2355 | assert(MVT::isVector(VT) && "Expected a vector type"); |
Dan Gohman | 237898a | 2007-05-24 14:33:05 +0000 | [diff] [blame] | 2356 | unsigned NumElems = MVT::getVectorNumElements(VT); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2357 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2358 | bool isFP = MVT::isFloatingPoint(EVT); |
| 2359 | SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT); |
| 2360 | SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero); |
| 2361 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size()); |
| 2362 | } |
| 2363 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2364 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2365 | /// that point to V2 points to its first element. |
| 2366 | static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) { |
| 2367 | assert(Mask.getOpcode() == ISD::BUILD_VECTOR); |
| 2368 | |
| 2369 | bool Changed = false; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2370 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2371 | unsigned NumElems = Mask.getNumOperands(); |
| 2372 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2373 | SDOperand Arg = Mask.getOperand(i); |
| 2374 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2375 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2376 | if (Val > NumElems) { |
| 2377 | Arg = DAG.getConstant(NumElems, Arg.getValueType()); |
| 2378 | Changed = true; |
| 2379 | } |
| 2380 | } |
| 2381 | MaskVec.push_back(Arg); |
| 2382 | } |
| 2383 | |
| 2384 | if (Changed) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2385 | Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), |
| 2386 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2387 | return Mask; |
| 2388 | } |
| 2389 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2390 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2391 | /// operation of specified width. |
| 2392 | static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2393 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2394 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2396 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2397 | MaskVec.push_back(DAG.getConstant(NumElems, BaseVT)); |
| 2398 | for (unsigned i = 1; i != NumElems; ++i) |
| 2399 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2400 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2403 | /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation |
| 2404 | /// of specified width. |
| 2405 | static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2406 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2407 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2408 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2409 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
| 2410 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2411 | MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT)); |
| 2412 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2413 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2416 | /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation |
| 2417 | /// of specified width. |
| 2418 | static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2419 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2420 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2421 | unsigned Half = NumElems/2; |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2422 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2423 | for (unsigned i = 0; i != Half; ++i) { |
| 2424 | MaskVec.push_back(DAG.getConstant(i + Half, BaseVT)); |
| 2425 | MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT)); |
| 2426 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2427 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2430 | /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32. |
| 2431 | /// |
| 2432 | static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) { |
| 2433 | SDOperand V1 = Op.getOperand(0); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2434 | SDOperand Mask = Op.getOperand(2); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2435 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2436 | unsigned NumElems = Mask.getNumOperands(); |
| 2437 | Mask = getUnpacklMask(NumElems, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2438 | while (NumElems != 4) { |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2439 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2440 | NumElems >>= 1; |
| 2441 | } |
| 2442 | V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1); |
| 2443 | |
| 2444 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2445 | Mask = getZeroVector(MaskVT, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2446 | SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2447 | DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2448 | return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle); |
| 2449 | } |
| 2450 | |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2451 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2452 | /// vector of zero or undef vector. |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2453 | static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2454 | unsigned NumElems, unsigned Idx, |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2455 | bool isZero, SelectionDAG &DAG) { |
| 2456 | SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2457 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2458 | MVT::ValueType EVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2459 | SDOperand Zero = DAG.getConstant(0, EVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2460 | SmallVector<SDOperand, 8> MaskVec(NumElems, Zero); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2461 | MaskVec[Idx] = DAG.getConstant(NumElems, EVT); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2462 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2463 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2464 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2467 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 2468 | /// |
| 2469 | static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros, |
| 2470 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2471 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2472 | if (NumNonZero > 8) |
| 2473 | return SDOperand(); |
| 2474 | |
| 2475 | SDOperand V(0, 0); |
| 2476 | bool First = true; |
| 2477 | for (unsigned i = 0; i < 16; ++i) { |
| 2478 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 2479 | if (ThisIsNonZero && First) { |
| 2480 | if (NumZero) |
| 2481 | V = getZeroVector(MVT::v8i16, DAG); |
| 2482 | else |
| 2483 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2484 | First = false; |
| 2485 | } |
| 2486 | |
| 2487 | if ((i & 1) != 0) { |
| 2488 | SDOperand ThisElt(0, 0), LastElt(0, 0); |
| 2489 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 2490 | if (LastIsNonZero) { |
| 2491 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1)); |
| 2492 | } |
| 2493 | if (ThisIsNonZero) { |
| 2494 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i)); |
| 2495 | ThisElt = DAG.getNode(ISD::SHL, MVT::i16, |
| 2496 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 2497 | if (LastIsNonZero) |
| 2498 | ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt); |
| 2499 | } else |
| 2500 | ThisElt = LastElt; |
| 2501 | |
| 2502 | if (ThisElt.Val) |
| 2503 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2504 | DAG.getConstant(i/2, TLI.getPointerTy())); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V); |
| 2509 | } |
| 2510 | |
Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 2511 | /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2512 | /// |
| 2513 | static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros, |
| 2514 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2515 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2516 | if (NumNonZero > 4) |
| 2517 | return SDOperand(); |
| 2518 | |
| 2519 | SDOperand V(0, 0); |
| 2520 | bool First = true; |
| 2521 | for (unsigned i = 0; i < 8; ++i) { |
| 2522 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 2523 | if (isNonZero) { |
| 2524 | if (First) { |
| 2525 | if (NumZero) |
| 2526 | V = getZeroVector(MVT::v8i16, DAG); |
| 2527 | else |
| 2528 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2529 | First = false; |
| 2530 | } |
| 2531 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i), |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2532 | DAG.getConstant(i, TLI.getPointerTy())); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | return V; |
| 2537 | } |
| 2538 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2539 | SDOperand |
| 2540 | X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 2541 | // All zero's are handled with pxor. |
| 2542 | if (ISD::isBuildVectorAllZeros(Op.Val)) |
| 2543 | return Op; |
| 2544 | |
| 2545 | // All one's are handled with pcmpeqd. |
| 2546 | if (ISD::isBuildVectorAllOnes(Op.Val)) |
| 2547 | return Op; |
| 2548 | |
| 2549 | MVT::ValueType VT = Op.getValueType(); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2550 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2551 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
| 2552 | |
| 2553 | unsigned NumElems = Op.getNumOperands(); |
| 2554 | unsigned NumZero = 0; |
| 2555 | unsigned NumNonZero = 0; |
| 2556 | unsigned NonZeros = 0; |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 2557 | unsigned NumNonZeroImms = 0; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2558 | std::set<SDOperand> Values; |
| 2559 | for (unsigned i = 0; i < NumElems; ++i) { |
| 2560 | SDOperand Elt = Op.getOperand(i); |
| 2561 | if (Elt.getOpcode() != ISD::UNDEF) { |
| 2562 | Values.insert(Elt); |
| 2563 | if (isZeroNode(Elt)) |
| 2564 | NumZero++; |
| 2565 | else { |
| 2566 | NonZeros |= (1 << i); |
| 2567 | NumNonZero++; |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 2568 | if (Elt.getOpcode() == ISD::Constant || |
| 2569 | Elt.getOpcode() == ISD::ConstantFP) |
| 2570 | NumNonZeroImms++; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2571 | } |
| 2572 | } |
| 2573 | } |
| 2574 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2575 | if (NumNonZero == 0) { |
| 2576 | if (NumZero == 0) |
| 2577 | // All undef vector. Return an UNDEF. |
| 2578 | return DAG.getNode(ISD::UNDEF, VT); |
| 2579 | else |
| 2580 | // A mix of zero and undef. Return a zero vector. |
| 2581 | return getZeroVector(VT, DAG); |
| 2582 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2583 | |
| 2584 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 2585 | if (Values.size() == 1) |
| 2586 | return SDOperand(); |
| 2587 | |
| 2588 | // Special case for single non-zero element. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2589 | if (NumNonZero == 1) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2590 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
| 2591 | SDOperand Item = Op.getOperand(Idx); |
| 2592 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item); |
| 2593 | if (Idx == 0) |
| 2594 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| 2595 | return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx, |
| 2596 | NumZero > 0, DAG); |
| 2597 | |
| 2598 | if (EVTBits == 32) { |
| 2599 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
| 2600 | Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0, |
| 2601 | DAG); |
| 2602 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2603 | MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2604 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2605 | for (unsigned i = 0; i < NumElems; i++) |
| 2606 | MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2607 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2608 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2609 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item, |
| 2610 | DAG.getNode(ISD::UNDEF, VT), Mask); |
| 2611 | } |
| 2612 | } |
| 2613 | |
Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 2614 | // A vector full of immediates; various special cases are already |
| 2615 | // handled, so this is best done with a single constant-pool load. |
| 2616 | if (NumNonZero == NumNonZeroImms) |
| 2617 | return SDOperand(); |
| 2618 | |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2619 | // Let legalizer expand 2-wide build_vectors. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2620 | if (EVTBits == 64) |
| 2621 | return SDOperand(); |
| 2622 | |
| 2623 | // 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] | 2624 | if (EVTBits == 8 && NumElems == 16) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2625 | SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 2626 | *this); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2627 | if (V.Val) return V; |
| 2628 | } |
| 2629 | |
Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 2630 | if (EVTBits == 16 && NumElems == 8) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2631 | SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 2632 | *this); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2633 | if (V.Val) return V; |
| 2634 | } |
| 2635 | |
| 2636 | // If element VT is == 32 bits, turn it into a number of shuffles. |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2637 | SmallVector<SDOperand, 8> V; |
| 2638 | V.resize(NumElems); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2639 | if (NumElems == 4 && NumZero > 0) { |
| 2640 | for (unsigned i = 0; i < 4; ++i) { |
| 2641 | bool isZero = !(NonZeros & (1 << i)); |
| 2642 | if (isZero) |
| 2643 | V[i] = getZeroVector(VT, DAG); |
| 2644 | else |
| 2645 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2646 | } |
| 2647 | |
| 2648 | for (unsigned i = 0; i < 2; ++i) { |
| 2649 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 2650 | default: break; |
| 2651 | case 0: |
| 2652 | V[i] = V[i*2]; // Must be a zero vector. |
| 2653 | break; |
| 2654 | case 1: |
| 2655 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2], |
| 2656 | getMOVLMask(NumElems, DAG)); |
| 2657 | break; |
| 2658 | case 2: |
| 2659 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2660 | getMOVLMask(NumElems, DAG)); |
| 2661 | break; |
| 2662 | case 3: |
| 2663 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2664 | getUnpacklMask(NumElems, DAG)); |
| 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 2669 | // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2670 | // clears the upper bits. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2671 | // FIXME: we can do the same for v4f32 case when we know both parts of |
| 2672 | // the lower half come from scalar_to_vector (loadf32). We should do |
| 2673 | // that in post legalizer dag combiner with target specific hooks. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2674 | if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0) |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2675 | return V[0]; |
| 2676 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2677 | MVT::ValueType EVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2678 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2679 | bool Reverse = (NonZeros & 0x3) == 2; |
| 2680 | for (unsigned i = 0; i < 2; ++i) |
| 2681 | if (Reverse) |
| 2682 | MaskVec.push_back(DAG.getConstant(1-i, EVT)); |
| 2683 | else |
| 2684 | MaskVec.push_back(DAG.getConstant(i, EVT)); |
| 2685 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 2686 | for (unsigned i = 0; i < 2; ++i) |
| 2687 | if (Reverse) |
| 2688 | MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT)); |
| 2689 | else |
| 2690 | MaskVec.push_back(DAG.getConstant(i+NumElems, EVT)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2691 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2692 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2693 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask); |
| 2694 | } |
| 2695 | |
| 2696 | if (Values.size() > 2) { |
| 2697 | // Expand into a number of unpckl*. |
| 2698 | // e.g. for v4f32 |
| 2699 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 2700 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 2701 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
| 2702 | SDOperand UnpckMask = getUnpacklMask(NumElems, DAG); |
| 2703 | for (unsigned i = 0; i < NumElems; ++i) |
| 2704 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2705 | NumElems >>= 1; |
| 2706 | while (NumElems != 0) { |
| 2707 | for (unsigned i = 0; i < NumElems; ++i) |
| 2708 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems], |
| 2709 | UnpckMask); |
| 2710 | NumElems >>= 1; |
| 2711 | } |
| 2712 | return V[0]; |
| 2713 | } |
| 2714 | |
| 2715 | return SDOperand(); |
| 2716 | } |
| 2717 | |
| 2718 | SDOperand |
| 2719 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { |
| 2720 | SDOperand V1 = Op.getOperand(0); |
| 2721 | SDOperand V2 = Op.getOperand(1); |
| 2722 | SDOperand PermMask = Op.getOperand(2); |
| 2723 | MVT::ValueType VT = Op.getValueType(); |
| 2724 | unsigned NumElems = PermMask.getNumOperands(); |
| 2725 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 2726 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 2727 | bool V1IsSplat = false; |
| 2728 | bool V2IsSplat = false; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2729 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2730 | if (isUndefShuffle(Op.Val)) |
| 2731 | return DAG.getNode(ISD::UNDEF, VT); |
| 2732 | |
Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 2733 | if (isZeroShuffle(Op.Val)) |
| 2734 | return getZeroVector(VT, DAG); |
| 2735 | |
Evan Cheng | 49892af | 2007-06-19 00:02:56 +0000 | [diff] [blame] | 2736 | if (isIdentityMask(PermMask.Val)) |
| 2737 | return V1; |
| 2738 | else if (isIdentityMask(PermMask.Val, true)) |
| 2739 | return V2; |
| 2740 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2741 | if (isSplatMask(PermMask.Val)) { |
| 2742 | if (NumElems <= 4) return Op; |
| 2743 | // Promote it to a v4i32 splat. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2744 | return PromoteSplat(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2747 | if (X86::isMOVLMask(PermMask.Val)) |
| 2748 | return (V1IsUndef) ? V2 : Op; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2749 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2750 | if (X86::isMOVSHDUPMask(PermMask.Val) || |
| 2751 | X86::isMOVSLDUPMask(PermMask.Val) || |
| 2752 | X86::isMOVHLPSMask(PermMask.Val) || |
| 2753 | X86::isMOVHPMask(PermMask.Val) || |
| 2754 | X86::isMOVLPMask(PermMask.Val)) |
| 2755 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2756 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2757 | if (ShouldXformToMOVHLPS(PermMask.Val) || |
| 2758 | ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val)) |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2759 | return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2760 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2761 | bool Commuted = false; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2762 | V1IsSplat = isSplatVector(V1.Val); |
| 2763 | V2IsSplat = isSplatVector(V2.Val); |
| 2764 | if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2765 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2766 | std::swap(V1IsSplat, V2IsSplat); |
| 2767 | std::swap(V1IsUndef, V2IsUndef); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2768 | Commuted = true; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) { |
| 2772 | if (V2IsUndef) return V1; |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2773 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2774 | if (V2IsSplat) { |
| 2775 | // V2 is a splat, so the mask may be malformed. That is, it may point |
| 2776 | // to any V2 element. The instruction selectior won't like this. Get |
| 2777 | // a corrected mask and commute to form a proper MOVS{S|D}. |
| 2778 | SDOperand NewMask = getMOVLMask(NumElems, DAG); |
| 2779 | if (NewMask.Val != PermMask.Val) |
| 2780 | Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2781 | } |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2782 | return Op; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 2783 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2784 | |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 2785 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2786 | X86::isUNPCKH_v_undef_Mask(PermMask.Val) || |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 2787 | X86::isUNPCKLMask(PermMask.Val) || |
| 2788 | X86::isUNPCKHMask(PermMask.Val)) |
| 2789 | return Op; |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 2790 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2791 | if (V2IsSplat) { |
| 2792 | // 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] | 2793 | // 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] | 2794 | // new vector_shuffle with the corrected mask. |
| 2795 | SDOperand NewMask = NormalizeMask(PermMask, DAG); |
| 2796 | if (NewMask.Val != PermMask.Val) { |
| 2797 | if (X86::isUNPCKLMask(PermMask.Val, true)) { |
| 2798 | SDOperand NewMask = getUnpacklMask(NumElems, DAG); |
| 2799 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 2800 | } else if (X86::isUNPCKHMask(PermMask.Val, true)) { |
| 2801 | SDOperand NewMask = getUnpackhMask(NumElems, DAG); |
| 2802 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | // Normalize the node to match x86 shuffle ops if needed |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2808 | if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val)) |
| 2809 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 2810 | |
| 2811 | if (Commuted) { |
| 2812 | // Commute is back and try unpck* again. |
| 2813 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 2814 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 2815 | X86::isUNPCKH_v_undef_Mask(PermMask.Val) || |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2816 | X86::isUNPCKLMask(PermMask.Val) || |
| 2817 | X86::isUNPCKHMask(PermMask.Val)) |
| 2818 | return Op; |
| 2819 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2820 | |
| 2821 | // If VT is integer, try PSHUF* first, then SHUFP*. |
| 2822 | if (MVT::isInteger(VT)) { |
Dan Gohman | 7f55fcb | 2007-08-02 21:17:01 +0000 | [diff] [blame] | 2823 | // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically |
| 2824 | // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented. |
| 2825 | if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) && |
| 2826 | X86::isPSHUFDMask(PermMask.Val)) || |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2827 | X86::isPSHUFHWMask(PermMask.Val) || |
| 2828 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 2829 | if (V2.getOpcode() != ISD::UNDEF) |
| 2830 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 2831 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 2832 | return Op; |
| 2833 | } |
| 2834 | |
Chris Lattner | 07c70cd | 2007-05-17 17:13:13 +0000 | [diff] [blame] | 2835 | if (X86::isSHUFPMask(PermMask.Val) && |
| 2836 | MVT::getSizeInBits(VT) != 64) // Don't do this for MMX. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2837 | return Op; |
| 2838 | |
| 2839 | // Handle v8i16 shuffle high / low shuffle node pair. |
| 2840 | if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) { |
| 2841 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2842 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2843 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2844 | for (unsigned i = 0; i != 4; ++i) |
| 2845 | MaskVec.push_back(PermMask.getOperand(i)); |
| 2846 | for (unsigned i = 4; i != 8; ++i) |
| 2847 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2848 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2849 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2850 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2851 | MaskVec.clear(); |
| 2852 | for (unsigned i = 0; i != 4; ++i) |
| 2853 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2854 | for (unsigned i = 4; i != 8; ++i) |
| 2855 | MaskVec.push_back(PermMask.getOperand(i)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2856 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2857 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2858 | } |
| 2859 | } else { |
| 2860 | // Floating point cases in the other order. |
| 2861 | if (X86::isSHUFPMask(PermMask.Val)) |
| 2862 | return Op; |
| 2863 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 2864 | X86::isPSHUFHWMask(PermMask.Val) || |
| 2865 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 2866 | if (V2.getOpcode() != ISD::UNDEF) |
| 2867 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 2868 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 2869 | return Op; |
| 2870 | } |
| 2871 | } |
| 2872 | |
Chris Lattner | 07c70cd | 2007-05-17 17:13:13 +0000 | [diff] [blame] | 2873 | if (NumElems == 4 && |
| 2874 | // Don't do this for MMX. |
| 2875 | MVT::getSizeInBits(VT) != 64) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2876 | MVT::ValueType MaskVT = PermMask.getValueType(); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2877 | MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2878 | SmallVector<std::pair<int, int>, 8> Locs; |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 2879 | Locs.reserve(NumElems); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2880 | SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2881 | SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 2882 | unsigned NumHi = 0; |
| 2883 | unsigned NumLo = 0; |
| 2884 | // If no more than two elements come from either vector. This can be |
| 2885 | // implemented with two shuffles. First shuffle gather the elements. |
| 2886 | // The second shuffle, which takes the first shuffle as both of its |
| 2887 | // vector operands, put the elements into the right order. |
| 2888 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2889 | SDOperand Elt = PermMask.getOperand(i); |
| 2890 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 2891 | Locs[i] = std::make_pair(-1, -1); |
| 2892 | } else { |
| 2893 | unsigned Val = cast<ConstantSDNode>(Elt)->getValue(); |
| 2894 | if (Val < NumElems) { |
| 2895 | Locs[i] = std::make_pair(0, NumLo); |
| 2896 | Mask1[NumLo] = Elt; |
| 2897 | NumLo++; |
| 2898 | } else { |
| 2899 | Locs[i] = std::make_pair(1, NumHi); |
| 2900 | if (2+NumHi < NumElems) |
| 2901 | Mask1[2+NumHi] = Elt; |
| 2902 | NumHi++; |
| 2903 | } |
| 2904 | } |
| 2905 | } |
| 2906 | if (NumLo <= 2 && NumHi <= 2) { |
| 2907 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2908 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2909 | &Mask1[0], Mask1.size())); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 2910 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2911 | if (Locs[i].first == -1) |
| 2912 | continue; |
| 2913 | else { |
| 2914 | unsigned Idx = (i < NumElems/2) ? 0 : NumElems; |
| 2915 | Idx += Locs[i].first * (NumElems/2) + Locs[i].second; |
| 2916 | Mask2[i] = DAG.getConstant(Idx, MaskEVT); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2921 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2922 | &Mask2[0], Mask2.size())); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 2926 | Locs.clear(); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2927 | SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2928 | SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2929 | SmallVector<SDOperand,8> *MaskPtr = &LoMask; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2930 | unsigned MaskIdx = 0; |
| 2931 | unsigned LoIdx = 0; |
| 2932 | unsigned HiIdx = NumElems/2; |
| 2933 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2934 | if (i == NumElems/2) { |
| 2935 | MaskPtr = &HiMask; |
| 2936 | MaskIdx = 1; |
| 2937 | LoIdx = 0; |
| 2938 | HiIdx = NumElems/2; |
| 2939 | } |
| 2940 | SDOperand Elt = PermMask.getOperand(i); |
| 2941 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 2942 | Locs[i] = std::make_pair(-1, -1); |
| 2943 | } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) { |
| 2944 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
| 2945 | (*MaskPtr)[LoIdx] = Elt; |
| 2946 | LoIdx++; |
| 2947 | } else { |
| 2948 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
| 2949 | (*MaskPtr)[HiIdx] = Elt; |
| 2950 | HiIdx++; |
| 2951 | } |
| 2952 | } |
| 2953 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2954 | SDOperand LoShuffle = |
| 2955 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2956 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2957 | &LoMask[0], LoMask.size())); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2958 | SDOperand HiShuffle = |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2959 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2960 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2961 | &HiMask[0], HiMask.size())); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 2962 | SmallVector<SDOperand, 8> MaskOps; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2963 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2964 | if (Locs[i].first == -1) { |
| 2965 | MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2966 | } else { |
| 2967 | unsigned Idx = Locs[i].first * NumElems + Locs[i].second; |
| 2968 | MaskOps.push_back(DAG.getConstant(Idx, MaskEVT)); |
| 2969 | } |
| 2970 | } |
| 2971 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 2972 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2973 | &MaskOps[0], MaskOps.size())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | return SDOperand(); |
| 2977 | } |
| 2978 | |
| 2979 | SDOperand |
| 2980 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
| 2981 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 2982 | return SDOperand(); |
| 2983 | |
| 2984 | MVT::ValueType VT = Op.getValueType(); |
| 2985 | // TODO: handle v16i8. |
| 2986 | if (MVT::getSizeInBits(VT) == 16) { |
| 2987 | // Transform it so it match pextrw which produces a 32-bit result. |
| 2988 | MVT::ValueType EVT = (MVT::ValueType)(VT+1); |
| 2989 | SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT, |
| 2990 | Op.getOperand(0), Op.getOperand(1)); |
| 2991 | SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract, |
| 2992 | DAG.getValueType(VT)); |
| 2993 | return DAG.getNode(ISD::TRUNCATE, VT, Assert); |
| 2994 | } else if (MVT::getSizeInBits(VT) == 32) { |
| 2995 | SDOperand Vec = Op.getOperand(0); |
| 2996 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 2997 | if (Idx == 0) |
| 2998 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2999 | // SHUFPS the element to the lowest double word, then movss. |
| 3000 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3001 | SmallVector<SDOperand, 8> IdxVec; |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 3002 | IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT))); |
| 3003 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
| 3004 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
| 3005 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3006 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3007 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3008 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 3009 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3010 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3011 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3012 | } else if (MVT::getSizeInBits(VT) == 64) { |
| 3013 | SDOperand Vec = Op.getOperand(0); |
| 3014 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3015 | if (Idx == 0) |
| 3016 | return Op; |
| 3017 | |
| 3018 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 3019 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 3020 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
| 3021 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3022 | SmallVector<SDOperand, 8> IdxVec; |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 3023 | IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT))); |
| 3024 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT))); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3025 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3026 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3027 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 3028 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
| 3029 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3030 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | return SDOperand(); |
| 3034 | } |
| 3035 | |
| 3036 | SDOperand |
| 3037 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 3038 | // Transform it so it match pinsrw which expects a 16-bit value in a GR32 |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3039 | // as its second argument. |
| 3040 | MVT::ValueType VT = Op.getValueType(); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 3041 | MVT::ValueType BaseVT = MVT::getVectorElementType(VT); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3042 | SDOperand N0 = Op.getOperand(0); |
| 3043 | SDOperand N1 = Op.getOperand(1); |
| 3044 | SDOperand N2 = Op.getOperand(2); |
| 3045 | if (MVT::getSizeInBits(BaseVT) == 16) { |
| 3046 | if (N1.getValueType() != MVT::i32) |
| 3047 | N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1); |
| 3048 | if (N2.getValueType() != MVT::i32) |
Evan Cheng | 0db5862 | 2007-06-29 00:01:20 +0000 | [diff] [blame] | 3049 | N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3050 | return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2); |
| 3051 | } else if (MVT::getSizeInBits(BaseVT) == 32) { |
| 3052 | unsigned Idx = cast<ConstantSDNode>(N2)->getValue(); |
| 3053 | if (Idx == 0) { |
| 3054 | // Use a movss. |
| 3055 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1); |
| 3056 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 3057 | MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3058 | SmallVector<SDOperand, 8> MaskVec; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3059 | MaskVec.push_back(DAG.getConstant(4, BaseVT)); |
| 3060 | for (unsigned i = 1; i <= 3; ++i) |
| 3061 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 3062 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3063 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3064 | &MaskVec[0], MaskVec.size())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3065 | } else { |
| 3066 | // Use two pinsrw instructions to insert a 32 bit value. |
| 3067 | Idx <<= 1; |
| 3068 | if (MVT::isFloatingPoint(N1.getValueType())) { |
Evan Cheng | 4ebcc8c | 2007-07-31 06:21:44 +0000 | [diff] [blame] | 3069 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1); |
| 3070 | N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1); |
| 3071 | N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1, |
| 3072 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3073 | } |
| 3074 | N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0); |
| 3075 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3076 | DAG.getConstant(Idx, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3077 | N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8)); |
| 3078 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3079 | DAG.getConstant(Idx+1, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3080 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | return SDOperand(); |
| 3085 | } |
| 3086 | |
| 3087 | SDOperand |
| 3088 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 3089 | SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0)); |
| 3090 | return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt); |
| 3091 | } |
| 3092 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3093 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3094 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 3095 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 3096 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 3097 | // be used to form addressing mode. These wrapped nodes will be selected |
| 3098 | // into MOV32ri. |
| 3099 | SDOperand |
| 3100 | X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { |
| 3101 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3102 | SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(), |
| 3103 | getPointerTy(), |
| 3104 | CP->getAlignment()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3105 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3106 | // With PIC, the address is actually $g + Offset. |
| 3107 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3108 | !Subtarget->isPICStyleRIPRel()) { |
| 3109 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3110 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3111 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | return Result; |
| 3115 | } |
| 3116 | |
| 3117 | SDOperand |
| 3118 | X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) { |
| 3119 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3120 | SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3121 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3122 | // With PIC, the address is actually $g + Offset. |
| 3123 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3124 | !Subtarget->isPICStyleRIPRel()) { |
| 3125 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3126 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3127 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3128 | } |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 3129 | |
| 3130 | // For Darwin & Mingw32, external and weak symbols are indirect, so we want to |
| 3131 | // load the value at address GV, not the value of GV itself. This means that |
| 3132 | // the GlobalAddress must be in the base or index register of the address, not |
| 3133 | // the GV offset field. Platform check is inside GVRequiresExtraLoad() call |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3134 | // The same applies for external symbols during PIC codegen |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 3135 | if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)) |
| 3136 | Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3137 | |
| 3138 | return Result; |
| 3139 | } |
| 3140 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 3141 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model |
| 3142 | static SDOperand |
| 3143 | LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| 3144 | const MVT::ValueType PtrVT) { |
| 3145 | SDOperand InFlag; |
| 3146 | SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX, |
| 3147 | DAG.getNode(X86ISD::GlobalBaseReg, |
| 3148 | PtrVT), InFlag); |
| 3149 | InFlag = Chain.getValue(1); |
| 3150 | |
| 3151 | // emit leal symbol@TLSGD(,%ebx,1), %eax |
| 3152 | SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag); |
| 3153 | SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 3154 | GA->getValueType(0), |
| 3155 | GA->getOffset()); |
| 3156 | SDOperand Ops[] = { Chain, TGA, InFlag }; |
| 3157 | SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3); |
| 3158 | InFlag = Result.getValue(2); |
| 3159 | Chain = Result.getValue(1); |
| 3160 | |
| 3161 | // call ___tls_get_addr. This function receives its argument in |
| 3162 | // the register EAX. |
| 3163 | Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag); |
| 3164 | InFlag = Chain.getValue(1); |
| 3165 | |
| 3166 | NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 3167 | SDOperand Ops1[] = { Chain, |
| 3168 | DAG.getTargetExternalSymbol("___tls_get_addr", |
| 3169 | PtrVT), |
| 3170 | DAG.getRegister(X86::EAX, PtrVT), |
| 3171 | DAG.getRegister(X86::EBX, PtrVT), |
| 3172 | InFlag }; |
| 3173 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5); |
| 3174 | InFlag = Chain.getValue(1); |
| 3175 | |
| 3176 | return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag); |
| 3177 | } |
| 3178 | |
| 3179 | // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or |
| 3180 | // "local exec" model. |
| 3181 | static SDOperand |
| 3182 | LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| 3183 | const MVT::ValueType PtrVT) { |
| 3184 | // Get the Thread Pointer |
| 3185 | SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT); |
| 3186 | // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial |
| 3187 | // exec) |
| 3188 | SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), |
| 3189 | GA->getValueType(0), |
| 3190 | GA->getOffset()); |
| 3191 | SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA); |
Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 3192 | |
| 3193 | if (GA->getGlobal()->isDeclaration()) // initial exec TLS model |
| 3194 | Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0); |
| 3195 | |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 3196 | // The address of the thread local variable is the add of the thread |
| 3197 | // pointer with the offset of the variable. |
| 3198 | return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset); |
| 3199 | } |
| 3200 | |
| 3201 | SDOperand |
| 3202 | X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) { |
| 3203 | // TODO: implement the "local dynamic" model |
Lauro Ramos Venancio | 2c5c111 | 2007-04-21 20:56:26 +0000 | [diff] [blame] | 3204 | // TODO: implement the "initial exec"model for pic executables |
| 3205 | assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() && |
| 3206 | "TLS not implemented for non-ELF and 64-bit targets"); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 3207 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
| 3208 | // If the relocation model is PIC, use the "General Dynamic" TLS Model, |
| 3209 | // otherwise use the "Local Exec"TLS Model |
| 3210 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_) |
| 3211 | return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy()); |
| 3212 | else |
| 3213 | return LowerToTLSExecModel(GA, DAG, getPointerTy()); |
| 3214 | } |
| 3215 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3216 | SDOperand |
| 3217 | X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) { |
| 3218 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3219 | SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3220 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3221 | // With PIC, the address is actually $g + Offset. |
| 3222 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3223 | !Subtarget->isPICStyleRIPRel()) { |
| 3224 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3225 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3226 | Result); |
| 3227 | } |
| 3228 | |
| 3229 | return Result; |
| 3230 | } |
| 3231 | |
| 3232 | SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) { |
| 3233 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 3234 | SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); |
| 3235 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3236 | // With PIC, the address is actually $g + Offset. |
| 3237 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3238 | !Subtarget->isPICStyleRIPRel()) { |
| 3239 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3240 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3241 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | return Result; |
| 3245 | } |
| 3246 | |
| 3247 | SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3248 | assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && |
| 3249 | "Not an i64 shift!"); |
| 3250 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| 3251 | SDOperand ShOpLo = Op.getOperand(0); |
| 3252 | SDOperand ShOpHi = Op.getOperand(1); |
| 3253 | SDOperand ShAmt = Op.getOperand(2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3254 | SDOperand Tmp1 = isSRA ? |
| 3255 | DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) : |
| 3256 | DAG.getConstant(0, MVT::i32); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3257 | |
| 3258 | SDOperand Tmp2, Tmp3; |
| 3259 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3260 | Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); |
| 3261 | Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); |
| 3262 | } else { |
| 3263 | Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); |
Evan Cheng | b7b5706 | 2006-01-19 01:46:14 +0000 | [diff] [blame] | 3264 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3267 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3268 | SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt, |
| 3269 | DAG.getConstant(32, MVT::i8)); |
| 3270 | SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)}; |
| 3271 | SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3272 | |
| 3273 | SDOperand Hi, Lo; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3274 | SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3275 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3276 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag); |
| 3277 | SmallVector<SDOperand, 4> Ops; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3278 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3279 | Ops.push_back(Tmp2); |
| 3280 | Ops.push_back(Tmp3); |
| 3281 | Ops.push_back(CC); |
| 3282 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3283 | Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3284 | InFlag = Hi.getValue(1); |
| 3285 | |
| 3286 | Ops.clear(); |
| 3287 | Ops.push_back(Tmp3); |
| 3288 | Ops.push_back(Tmp1); |
| 3289 | Ops.push_back(CC); |
| 3290 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3291 | Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3292 | } else { |
| 3293 | Ops.push_back(Tmp2); |
| 3294 | Ops.push_back(Tmp3); |
| 3295 | Ops.push_back(CC); |
Evan Cheng | 910cd3c | 2006-01-09 22:29:54 +0000 | [diff] [blame] | 3296 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3297 | Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3298 | InFlag = Lo.getValue(1); |
| 3299 | |
| 3300 | Ops.clear(); |
| 3301 | Ops.push_back(Tmp3); |
| 3302 | Ops.push_back(Tmp1); |
| 3303 | Ops.push_back(CC); |
| 3304 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3305 | Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3308 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3309 | Ops.clear(); |
| 3310 | Ops.push_back(Lo); |
| 3311 | Ops.push_back(Hi); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3312 | return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3313 | } |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3314 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3315 | SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { |
| 3316 | assert(Op.getOperand(0).getValueType() <= MVT::i64 && |
| 3317 | Op.getOperand(0).getValueType() >= MVT::i16 && |
| 3318 | "Unknown SINT_TO_FP to lower!"); |
| 3319 | |
| 3320 | SDOperand Result; |
| 3321 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3322 | unsigned Size = MVT::getSizeInBits(SrcVT)/8; |
| 3323 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3324 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 3325 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3326 | SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3327 | StackSlot, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3328 | |
| 3329 | // Build the FILD |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3330 | SDVTList Tys; |
| 3331 | if (X86ScalarSSE) |
| 3332 | Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag); |
| 3333 | else |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 3334 | Tys = DAG.getVTList(Op.getValueType(), MVT::Other); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3335 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3336 | Ops.push_back(Chain); |
| 3337 | Ops.push_back(StackSlot); |
| 3338 | Ops.push_back(DAG.getValueType(SrcVT)); |
| 3339 | Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3340 | Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3341 | |
| 3342 | if (X86ScalarSSE) { |
| 3343 | Chain = Result.getValue(1); |
| 3344 | SDOperand InFlag = Result.getValue(2); |
| 3345 | |
| 3346 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 3347 | // shouldn't be necessary except that RFP cannot be live across |
| 3348 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3349 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3350 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3351 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3352 | Tys = DAG.getVTList(MVT::Other); |
| 3353 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3354 | Ops.push_back(Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3355 | Ops.push_back(Result); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3356 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3357 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 3358 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3359 | Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3360 | Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3361 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3362 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3363 | return Result; |
| 3364 | } |
| 3365 | |
| 3366 | SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) { |
| 3367 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
| 3368 | "Unknown FP_TO_SINT to lower!"); |
| 3369 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 3370 | // stack slot. |
| 3371 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3372 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3373 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3374 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3375 | |
| 3376 | unsigned Opc; |
| 3377 | switch (Op.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3378 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 3379 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 3380 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 3381 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3382 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3383 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3384 | SDOperand Chain = DAG.getEntryNode(); |
| 3385 | SDOperand Value = Op.getOperand(0); |
| 3386 | if (X86ScalarSSE) { |
| 3387 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3388 | Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0); |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 3389 | SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3390 | SDOperand Ops[] = { |
| 3391 | Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType()) |
| 3392 | }; |
| 3393 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3394 | Chain = Value.getValue(1); |
| 3395 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3396 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3397 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3398 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3399 | // Build the FP_TO_INT*_IN_MEM |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3400 | SDOperand Ops[] = { Chain, Value, StackSlot }; |
| 3401 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3402 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3403 | // Load the result. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3404 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) { |
| 3408 | MVT::ValueType VT = Op.getValueType(); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3409 | MVT::ValueType EltVT = VT; |
| 3410 | if (MVT::isVector(VT)) |
| 3411 | EltVT = MVT::getVectorElementType(VT); |
| 3412 | const Type *OpNTy = MVT::getTypeForValueType(EltVT); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3413 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3414 | if (EltVT == MVT::f64) { |
| 3415 | Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))); |
| 3416 | CV.push_back(C); |
| 3417 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3418 | } else { |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3419 | Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))); |
| 3420 | CV.push_back(C); |
| 3421 | CV.push_back(C); |
| 3422 | CV.push_back(C); |
| 3423 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3424 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 3425 | Constant *C = ConstantVector::get(CV); |
| 3426 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 3427 | SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 3428 | false, 16); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3429 | return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); |
| 3430 | } |
| 3431 | |
| 3432 | SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) { |
| 3433 | MVT::ValueType VT = Op.getValueType(); |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3434 | MVT::ValueType EltVT = VT; |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 3435 | unsigned EltNum = 1; |
| 3436 | if (MVT::isVector(VT)) { |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3437 | EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 3438 | EltNum = MVT::getVectorNumElements(VT); |
| 3439 | } |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3440 | const Type *OpNTy = MVT::getTypeForValueType(EltVT); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3441 | std::vector<Constant*> CV; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3442 | if (EltVT == MVT::f64) { |
| 3443 | Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)); |
| 3444 | CV.push_back(C); |
| 3445 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3446 | } else { |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 3447 | Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31)); |
| 3448 | CV.push_back(C); |
| 3449 | CV.push_back(C); |
| 3450 | CV.push_back(C); |
| 3451 | CV.push_back(C); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3452 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 3453 | Constant *C = ConstantVector::get(CV); |
| 3454 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 3455 | SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 3456 | false, 16); |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 3457 | if (MVT::isVector(VT)) { |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 3458 | return DAG.getNode(ISD::BIT_CONVERT, VT, |
| 3459 | DAG.getNode(ISD::XOR, MVT::v2i64, |
| 3460 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)), |
| 3461 | DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask))); |
| 3462 | } else { |
Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 3463 | return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); |
| 3464 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3467 | SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3468 | SDOperand Op0 = Op.getOperand(0); |
| 3469 | SDOperand Op1 = Op.getOperand(1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3470 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3471 | MVT::ValueType SrcVT = Op1.getValueType(); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3472 | const Type *SrcTy = MVT::getTypeForValueType(SrcVT); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3473 | |
| 3474 | // If second operand is smaller, extend it first. |
| 3475 | if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) { |
| 3476 | Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1); |
| 3477 | SrcVT = VT; |
| 3478 | } |
| 3479 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3480 | // First get the sign bit of second operand. |
| 3481 | std::vector<Constant*> CV; |
| 3482 | if (SrcVT == MVT::f64) { |
| 3483 | CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63))); |
| 3484 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3485 | } else { |
| 3486 | CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31))); |
| 3487 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3488 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3489 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3490 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 3491 | Constant *C = ConstantVector::get(CV); |
| 3492 | SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 3493 | SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 3494 | false, 16); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3495 | SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3496 | |
| 3497 | // Shift sign bit right or left if the two operands have different types. |
| 3498 | if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) { |
| 3499 | // Op0 is MVT::f32, Op1 is MVT::f64. |
| 3500 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit); |
| 3501 | SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit, |
| 3502 | DAG.getConstant(32, MVT::i32)); |
| 3503 | SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit); |
| 3504 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit, |
| 3505 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3508 | // Clear first operand sign bit. |
| 3509 | CV.clear(); |
| 3510 | if (VT == MVT::f64) { |
| 3511 | CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63)))); |
| 3512 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3513 | } else { |
| 3514 | CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31)))); |
| 3515 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3516 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3517 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3518 | } |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 3519 | C = ConstantVector::get(CV); |
| 3520 | CPIdx = DAG.getConstantPool(C, getPointerTy(), 4); |
| 3521 | SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0, |
| 3522 | false, 16); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3523 | SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2); |
| 3524 | |
| 3525 | // Or the value with the sign bit. |
| 3526 | return DAG.getNode(X86ISD::FOR, VT, Val, SignBit); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3529 | SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG, |
| 3530 | SDOperand Chain) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3531 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
| 3532 | SDOperand Cond; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3533 | SDOperand Op0 = Op.getOperand(0); |
| 3534 | SDOperand Op1 = Op.getOperand(1); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3535 | SDOperand CC = Op.getOperand(2); |
| 3536 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3537 | const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3538 | const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3539 | bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3540 | unsigned X86CC; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3541 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3542 | if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 3543 | Op0, Op1, DAG)) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3544 | SDOperand Ops1[] = { Chain, Op0, Op1 }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3545 | Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3546 | SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3547 | return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | assert(isFP && "Illegal integer SetCC!"); |
| 3551 | |
| 3552 | SDOperand COps[] = { Chain, Op0, Op1 }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3553 | Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3554 | |
| 3555 | switch (SetCCOpcode) { |
| 3556 | default: assert(false && "Illegal floating point SetCC!"); |
| 3557 | case ISD::SETOEQ: { // !PF & ZF |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3558 | SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3559 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3560 | SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3561 | Tmp1.getValue(1) }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3562 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3563 | return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); |
| 3564 | } |
| 3565 | case ISD::SETUNE: { // PF | !ZF |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3566 | SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3567 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3568 | SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3569 | Tmp1.getValue(1) }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3570 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3571 | return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); |
| 3572 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 3573 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3574 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3575 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3576 | SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3577 | bool addTest = true; |
| 3578 | SDOperand Chain = DAG.getEntryNode(); |
| 3579 | SDOperand Cond = Op.getOperand(0); |
| 3580 | SDOperand CC; |
| 3581 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3582 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3583 | if (Cond.getOpcode() == ISD::SETCC) |
| 3584 | Cond = LowerSETCC(Cond, DAG, Chain); |
| 3585 | |
| 3586 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 3587 | CC = Cond.getOperand(0); |
| 3588 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3589 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3590 | // (since flag operand cannot be shared). Use it as the condition setting |
| 3591 | // operand in place of the X86ISD::SETCC. |
| 3592 | // If the X86ISD::SETCC has more than one use, then perhaps it's better |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3593 | // to use a test instead of duplicating the X86ISD::CMP (for register |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3594 | // pressure reason)? |
| 3595 | SDOperand Cmp = Cond.getOperand(1); |
| 3596 | unsigned Opc = Cmp.getOpcode(); |
| 3597 | bool IllegalFPCMov = !X86ScalarSSE && |
| 3598 | MVT::isFloatingPoint(Op.getValueType()) && |
| 3599 | !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); |
| 3600 | if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) && |
| 3601 | !IllegalFPCMov) { |
| 3602 | SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; |
| 3603 | Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); |
| 3604 | addTest = false; |
| 3605 | } |
| 3606 | } |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3607 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3608 | if (addTest) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3609 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3610 | SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; |
| 3611 | Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 3612 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3613 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3614 | VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag); |
| 3615 | SmallVector<SDOperand, 4> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3616 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 3617 | // condition is true. |
| 3618 | Ops.push_back(Op.getOperand(2)); |
| 3619 | Ops.push_back(Op.getOperand(1)); |
| 3620 | Ops.push_back(CC); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3621 | Ops.push_back(Cond.getValue(1)); |
| 3622 | return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3623 | } |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3624 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3625 | SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3626 | bool addTest = true; |
| 3627 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3628 | SDOperand Cond = Op.getOperand(1); |
| 3629 | SDOperand Dest = Op.getOperand(2); |
| 3630 | SDOperand CC; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3631 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3632 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3633 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3634 | Cond = LowerSETCC(Cond, DAG, Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3635 | |
| 3636 | if (Cond.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3637 | CC = Cond.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3638 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3639 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 3640 | // (since flag operand cannot be shared). Use it as the condition setting |
| 3641 | // operand in place of the X86ISD::SETCC. |
| 3642 | // If the X86ISD::SETCC has more than one use, then perhaps it's better |
| 3643 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 3644 | // pressure reason)? |
| 3645 | SDOperand Cmp = Cond.getOperand(1); |
| 3646 | unsigned Opc = Cmp.getOpcode(); |
| 3647 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) { |
| 3648 | SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; |
| 3649 | Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); |
| 3650 | addTest = false; |
| 3651 | } |
| 3652 | } |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 3653 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3654 | if (addTest) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3655 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3656 | SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; |
| 3657 | Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 3658 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3659 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3660 | Cond, Op.getOperand(2), CC, Cond.getValue(1)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3661 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 3662 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3663 | SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
| 3664 | unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3665 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3666 | if (Subtarget->is64Bit()) |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 3667 | return LowerX86_64CCCCallTo(Op, DAG, CallingConv); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3668 | else |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3669 | switch (CallingConv) { |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3670 | default: |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3671 | assert(0 && "Unsupported calling convention"); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3672 | case CallingConv::Fast: |
Chris Lattner | 2db39b8 | 2007-02-28 06:05:16 +0000 | [diff] [blame] | 3673 | // TODO: Implement fastcc |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3674 | // Falls through |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3675 | case CallingConv::C: |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3676 | case CallingConv::X86_StdCall: |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 3677 | return LowerCCCCallTo(Op, DAG, CallingConv); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3678 | case CallingConv::X86_FastCall: |
Chris Lattner | 09c75a4 | 2007-02-25 09:06:15 +0000 | [diff] [blame] | 3679 | return LowerFastCCCallTo(Op, DAG, CallingConv); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3680 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 3683 | |
| 3684 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. |
| 3685 | // Calls to _alloca is needed to probe the stack when allocating more than 4k |
| 3686 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure |
| 3687 | // that the guard pages used by the OS virtual memory manager are allocated in |
| 3688 | // correct sequence. |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 3689 | SDOperand |
| 3690 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op, |
| 3691 | SelectionDAG &DAG) { |
Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 3692 | assert(Subtarget->isTargetCygMing() && |
| 3693 | "This should be used only on Cygwin/Mingw targets"); |
| 3694 | |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 3695 | // Get the inputs. |
| 3696 | SDOperand Chain = Op.getOperand(0); |
| 3697 | SDOperand Size = Op.getOperand(1); |
| 3698 | // FIXME: Ensure alignment here |
| 3699 | |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 3700 | SDOperand Flag; |
| 3701 | |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 3702 | MVT::ValueType IntPtr = getPointerTy(); |
| 3703 | MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 3704 | |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 3705 | Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag); |
| 3706 | Flag = Chain.getValue(1); |
| 3707 | |
| 3708 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag); |
| 3709 | SDOperand Ops[] = { Chain, |
| 3710 | DAG.getTargetExternalSymbol("_alloca", IntPtr), |
| 3711 | DAG.getRegister(X86::EAX, IntPtr), |
| 3712 | Flag }; |
| 3713 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4); |
| 3714 | Flag = Chain.getValue(1); |
| 3715 | |
| 3716 | Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 3717 | |
| 3718 | std::vector<MVT::ValueType> Tys; |
| 3719 | Tys.push_back(SPTy); |
| 3720 | Tys.push_back(MVT::Other); |
Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 3721 | SDOperand Ops1[2] = { Chain.getValue(0), Chain }; |
| 3722 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 3725 | SDOperand |
| 3726 | X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 3727 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3728 | const Function* Fn = MF.getFunction(); |
| 3729 | if (Fn->hasExternalLinkage() && |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 3730 | Subtarget->isTargetCygMing() && |
Evan Cheng | b12223e | 2006-06-09 06:24:42 +0000 | [diff] [blame] | 3731 | Fn->getName() == "main") |
Chris Lattner | d15dff2 | 2007-04-17 17:21:52 +0000 | [diff] [blame] | 3732 | MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true); |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 3733 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 3734 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3735 | if (Subtarget->is64Bit()) |
| 3736 | return LowerX86_64CCCArguments(Op, DAG); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 3737 | else |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3738 | switch(CC) { |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3739 | default: |
| 3740 | assert(0 && "Unsupported calling convention"); |
| 3741 | case CallingConv::Fast: |
Chris Lattner | 2db39b8 | 2007-02-28 06:05:16 +0000 | [diff] [blame] | 3742 | // TODO: implement fastcc. |
| 3743 | |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3744 | // Falls through |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3745 | case CallingConv::C: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3746 | return LowerCCCArguments(Op, DAG); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3747 | case CallingConv::X86_StdCall: |
Chris Lattner | d15dff2 | 2007-04-17 17:21:52 +0000 | [diff] [blame] | 3748 | MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3749 | return LowerCCCArguments(Op, DAG, true); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3750 | case CallingConv::X86_FastCall: |
Chris Lattner | d15dff2 | 2007-04-17 17:21:52 +0000 | [diff] [blame] | 3751 | MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall); |
Chris Lattner | 2db39b8 | 2007-02-28 06:05:16 +0000 | [diff] [blame] | 3752 | return LowerFastCCArguments(Op, DAG); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3753 | } |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3756 | SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) { |
| 3757 | SDOperand InFlag(0, 0); |
| 3758 | SDOperand Chain = Op.getOperand(0); |
| 3759 | unsigned Align = |
| 3760 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 3761 | if (Align == 0) Align = 1; |
| 3762 | |
| 3763 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
Rafael Espindola | 6b83b5d | 2007-08-27 10:18:20 +0000 | [diff] [blame] | 3764 | // If not DWORD aligned or size is more than the threshold, call memset. |
Rafael Espindola | 44c8265 | 2007-08-27 17:48:26 +0000 | [diff] [blame] | 3765 | // The libc version is likely to be faster for these cases. It can use the |
| 3766 | // address value and run time information about the CPU. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3767 | if ((Align & 3) != 0 || |
Rafael Espindola | 6b83b5d | 2007-08-27 10:18:20 +0000 | [diff] [blame] | 3768 | (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3769 | MVT::ValueType IntPtr = getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3770 | const Type *IntPtrTy = getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3771 | TargetLowering::ArgListTy Args; |
| 3772 | TargetLowering::ArgListEntry Entry; |
| 3773 | Entry.Node = Op.getOperand(1); |
| 3774 | Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3775 | Args.push_back(Entry); |
Reid Spencer | aff9387 | 2007-01-03 17:24:59 +0000 | [diff] [blame] | 3776 | // Extend the unsigned i8 argument to be an int value for the call. |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3777 | Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); |
| 3778 | Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3779 | Args.push_back(Entry); |
| 3780 | Entry.Node = Op.getOperand(3); |
| 3781 | Args.push_back(Entry); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3782 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3783 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3784 | DAG.getExternalSymbol("memset", IntPtr), Args, DAG); |
| 3785 | return CallResult.second; |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 3786 | } |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 3787 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3788 | MVT::ValueType AVT; |
| 3789 | SDOperand Count; |
| 3790 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 3791 | unsigned BytesLeft = 0; |
| 3792 | bool TwoRepStos = false; |
| 3793 | if (ValC) { |
| 3794 | unsigned ValReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3795 | uint64_t Val = ValC->getValue() & 255; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3796 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3797 | // If the value is a constant, then we can potentially use larger sets. |
| 3798 | switch (Align & 3) { |
| 3799 | case 2: // WORD aligned |
| 3800 | AVT = MVT::i16; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3801 | ValReg = X86::AX; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3802 | Val = (Val << 8) | Val; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3803 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3804 | case 0: // DWORD aligned |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3805 | AVT = MVT::i32; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3806 | ValReg = X86::EAX; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3807 | Val = (Val << 8) | Val; |
| 3808 | Val = (Val << 16) | Val; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3809 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned |
| 3810 | AVT = MVT::i64; |
| 3811 | ValReg = X86::RAX; |
| 3812 | Val = (Val << 32) | Val; |
| 3813 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3814 | break; |
| 3815 | default: // Byte aligned |
| 3816 | AVT = MVT::i8; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3817 | ValReg = X86::AL; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3818 | Count = Op.getOperand(3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3819 | break; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3822 | if (AVT > MVT::i8) { |
| 3823 | if (I) { |
| 3824 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
| 3825 | Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); |
| 3826 | BytesLeft = I->getValue() % UBytes; |
| 3827 | } else { |
| 3828 | assert(AVT >= MVT::i32 && |
| 3829 | "Do not use rep;stos if not at least DWORD aligned"); |
| 3830 | Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), |
| 3831 | Op.getOperand(3), DAG.getConstant(2, MVT::i8)); |
| 3832 | TwoRepStos = true; |
| 3833 | } |
| 3834 | } |
| 3835 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3836 | Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), |
| 3837 | InFlag); |
| 3838 | InFlag = Chain.getValue(1); |
| 3839 | } else { |
| 3840 | AVT = MVT::i8; |
| 3841 | Count = Op.getOperand(3); |
| 3842 | Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); |
| 3843 | InFlag = Chain.getValue(1); |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 3844 | } |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3845 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3846 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 3847 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3848 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3849 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
| 3850 | Op.getOperand(1), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3851 | InFlag = Chain.getValue(1); |
Evan Cheng | a0b3afb | 2006-03-27 07:00:16 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 3853 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3854 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3855 | Ops.push_back(Chain); |
| 3856 | Ops.push_back(DAG.getValueType(AVT)); |
| 3857 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 3858 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3859 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3860 | if (TwoRepStos) { |
| 3861 | InFlag = Chain.getValue(1); |
| 3862 | Count = Op.getOperand(3); |
| 3863 | MVT::ValueType CVT = Count.getValueType(); |
| 3864 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3865 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 3866 | Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, |
| 3867 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3868 | InFlag = Chain.getValue(1); |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 3869 | Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3870 | Ops.clear(); |
| 3871 | Ops.push_back(Chain); |
| 3872 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 3873 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 3874 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3875 | } else if (BytesLeft) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3876 | // Issue stores for the last 1 - 7 bytes. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3877 | SDOperand Value; |
| 3878 | unsigned Val = ValC->getValue() & 255; |
| 3879 | unsigned Offset = I->getValue() - BytesLeft; |
| 3880 | SDOperand DstAddr = Op.getOperand(1); |
| 3881 | MVT::ValueType AddrVT = DstAddr.getValueType(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3882 | if (BytesLeft >= 4) { |
| 3883 | Val = (Val << 8) | Val; |
| 3884 | Val = (Val << 16) | Val; |
| 3885 | Value = DAG.getConstant(Val, MVT::i32); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3886 | Chain = DAG.getStore(Chain, Value, |
| 3887 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 3888 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3889 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3890 | BytesLeft -= 4; |
| 3891 | Offset += 4; |
| 3892 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3893 | if (BytesLeft >= 2) { |
| 3894 | Value = DAG.getConstant((Val << 8) | Val, MVT::i16); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3895 | Chain = DAG.getStore(Chain, Value, |
| 3896 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 3897 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3898 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3899 | BytesLeft -= 2; |
| 3900 | Offset += 2; |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 3901 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3902 | if (BytesLeft == 1) { |
| 3903 | Value = DAG.getConstant(Val, MVT::i8); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3904 | Chain = DAG.getStore(Chain, Value, |
| 3905 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 3906 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3907 | NULL, 0); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 3908 | } |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 3909 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3910 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3911 | return Chain; |
| 3912 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3913 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3914 | SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) { |
| 3915 | SDOperand Chain = Op.getOperand(0); |
| 3916 | unsigned Align = |
| 3917 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 3918 | if (Align == 0) Align = 1; |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3919 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3920 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
Rafael Espindola | 6b83b5d | 2007-08-27 10:18:20 +0000 | [diff] [blame] | 3921 | // If not DWORD aligned or size is more than the threshold, call memcpy. |
Rafael Espindola | 44c8265 | 2007-08-27 17:48:26 +0000 | [diff] [blame] | 3922 | // The libc version is likely to be faster for these cases. It can use the |
| 3923 | // address value and run time information about the CPU. |
| 3924 | // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3925 | if ((Align & 3) != 0 || |
Rafael Espindola | 6b83b5d | 2007-08-27 10:18:20 +0000 | [diff] [blame] | 3926 | (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3927 | MVT::ValueType IntPtr = getPointerTy(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3928 | TargetLowering::ArgListTy Args; |
| 3929 | TargetLowering::ArgListEntry Entry; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3930 | Entry.Ty = getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3931 | Entry.Node = Op.getOperand(1); Args.push_back(Entry); |
| 3932 | Entry.Node = Op.getOperand(2); Args.push_back(Entry); |
| 3933 | Entry.Node = Op.getOperand(3); Args.push_back(Entry); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3934 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3935 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3936 | DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); |
| 3937 | return CallResult.second; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 3938 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3939 | |
| 3940 | MVT::ValueType AVT; |
| 3941 | SDOperand Count; |
| 3942 | unsigned BytesLeft = 0; |
| 3943 | bool TwoRepMovs = false; |
| 3944 | switch (Align & 3) { |
| 3945 | case 2: // WORD aligned |
| 3946 | AVT = MVT::i16; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3947 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3948 | case 0: // DWORD aligned |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3949 | AVT = MVT::i32; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3950 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned |
| 3951 | AVT = MVT::i64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3952 | break; |
| 3953 | default: // Byte aligned |
| 3954 | AVT = MVT::i8; |
| 3955 | Count = Op.getOperand(3); |
| 3956 | break; |
| 3957 | } |
| 3958 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3959 | if (AVT > MVT::i8) { |
| 3960 | if (I) { |
| 3961 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
| 3962 | Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); |
| 3963 | BytesLeft = I->getValue() % UBytes; |
| 3964 | } else { |
| 3965 | assert(AVT >= MVT::i32 && |
| 3966 | "Do not use rep;movs if not at least DWORD aligned"); |
| 3967 | Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), |
| 3968 | Op.getOperand(3), DAG.getConstant(2, MVT::i8)); |
| 3969 | TwoRepMovs = true; |
| 3970 | } |
| 3971 | } |
| 3972 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3973 | SDOperand InFlag(0, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3974 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 3975 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3976 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3977 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
| 3978 | Op.getOperand(1), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3979 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3980 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI, |
| 3981 | Op.getOperand(2), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3982 | InFlag = Chain.getValue(1); |
| 3983 | |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 3984 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3985 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3986 | Ops.push_back(Chain); |
| 3987 | Ops.push_back(DAG.getValueType(AVT)); |
| 3988 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 3989 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3990 | |
| 3991 | if (TwoRepMovs) { |
| 3992 | InFlag = Chain.getValue(1); |
| 3993 | Count = Op.getOperand(3); |
| 3994 | MVT::ValueType CVT = Count.getValueType(); |
| 3995 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3996 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 3997 | Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, |
| 3998 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3999 | InFlag = Chain.getValue(1); |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 4000 | Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4001 | Ops.clear(); |
| 4002 | Ops.push_back(Chain); |
| 4003 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 4004 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4005 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4006 | } else if (BytesLeft) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4007 | // Issue loads and stores for the last 1 - 7 bytes. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4008 | unsigned Offset = I->getValue() - BytesLeft; |
| 4009 | SDOperand DstAddr = Op.getOperand(1); |
| 4010 | MVT::ValueType DstVT = DstAddr.getValueType(); |
| 4011 | SDOperand SrcAddr = Op.getOperand(2); |
| 4012 | MVT::ValueType SrcVT = SrcAddr.getValueType(); |
| 4013 | SDOperand Value; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4014 | if (BytesLeft >= 4) { |
| 4015 | Value = DAG.getLoad(MVT::i32, Chain, |
| 4016 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4017 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4018 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4019 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4020 | Chain = DAG.getStore(Chain, Value, |
| 4021 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4022 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4023 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4024 | BytesLeft -= 4; |
| 4025 | Offset += 4; |
| 4026 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4027 | if (BytesLeft >= 2) { |
| 4028 | Value = DAG.getLoad(MVT::i16, Chain, |
| 4029 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4030 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4031 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4032 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4033 | Chain = DAG.getStore(Chain, Value, |
| 4034 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4035 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4036 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4037 | BytesLeft -= 2; |
| 4038 | Offset += 2; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4041 | if (BytesLeft == 1) { |
| 4042 | Value = DAG.getLoad(MVT::i8, Chain, |
| 4043 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4044 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4045 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4046 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4047 | Chain = DAG.getStore(Chain, Value, |
| 4048 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4049 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4050 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4051 | } |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4052 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4053 | |
| 4054 | return Chain; |
| 4055 | } |
| 4056 | |
| 4057 | SDOperand |
| 4058 | X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 4059 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4060 | SDOperand TheOp = Op.getOperand(0); |
| 4061 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1); |
Evan Cheng | 3fa9dff | 2006-11-29 08:28:13 +0000 | [diff] [blame] | 4062 | if (Subtarget->is64Bit()) { |
| 4063 | SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1)); |
| 4064 | SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX, |
| 4065 | MVT::i64, Copy1.getValue(2)); |
| 4066 | SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2, |
| 4067 | DAG.getConstant(32, MVT::i8)); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4068 | SDOperand Ops[] = { |
| 4069 | DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1) |
| 4070 | }; |
Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 4071 | |
| 4072 | Tys = DAG.getVTList(MVT::i64, MVT::Other); |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4073 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2); |
Evan Cheng | 3fa9dff | 2006-11-29 08:28:13 +0000 | [diff] [blame] | 4074 | } |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4075 | |
| 4076 | SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)); |
| 4077 | SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX, |
| 4078 | MVT::i32, Copy1.getValue(2)); |
| 4079 | SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) }; |
| 4080 | Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); |
| 4081 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4085 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2)); |
| 4086 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4087 | if (!Subtarget->is64Bit()) { |
| 4088 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 4089 | // memory location argument. |
| 4090 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4091 | return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(), |
| 4092 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | // __va_list_tag: |
| 4096 | // gp_offset (0 - 6 * 8) |
| 4097 | // fp_offset (48 - 48 + 8 * 16) |
| 4098 | // overflow_arg_area (point to parameters coming in memory). |
| 4099 | // reg_save_area |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4100 | SmallVector<SDOperand, 8> MemOps; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4101 | SDOperand FIN = Op.getOperand(1); |
| 4102 | // Store gp_offset |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4103 | SDOperand Store = DAG.getStore(Op.getOperand(0), |
| 4104 | DAG.getConstant(VarArgsGPOffset, MVT::i32), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4105 | FIN, SV->getValue(), SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4106 | MemOps.push_back(Store); |
| 4107 | |
| 4108 | // Store fp_offset |
| 4109 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4110 | DAG.getConstant(4, getPointerTy())); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4111 | Store = DAG.getStore(Op.getOperand(0), |
| 4112 | DAG.getConstant(VarArgsFPOffset, MVT::i32), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4113 | FIN, SV->getValue(), SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4114 | MemOps.push_back(Store); |
| 4115 | |
| 4116 | // Store ptr to overflow_arg_area |
| 4117 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4118 | DAG.getConstant(4, getPointerTy())); |
| 4119 | SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4120 | Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(), |
| 4121 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4122 | MemOps.push_back(Store); |
| 4123 | |
| 4124 | // Store ptr to reg_save_area. |
| 4125 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4126 | DAG.getConstant(8, getPointerTy())); |
| 4127 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4128 | Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(), |
| 4129 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4130 | MemOps.push_back(Store); |
| 4131 | return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 4134 | SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) { |
| 4135 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
| 4136 | SDOperand Chain = Op.getOperand(0); |
| 4137 | SDOperand DstPtr = Op.getOperand(1); |
| 4138 | SDOperand SrcPtr = Op.getOperand(2); |
| 4139 | SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3)); |
| 4140 | SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4)); |
| 4141 | |
| 4142 | SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, |
| 4143 | SrcSV->getValue(), SrcSV->getOffset()); |
| 4144 | Chain = SrcPtr.getValue(1); |
| 4145 | for (unsigned i = 0; i < 3; ++i) { |
| 4146 | SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, |
| 4147 | SrcSV->getValue(), SrcSV->getOffset()); |
| 4148 | Chain = Val.getValue(1); |
| 4149 | Chain = DAG.getStore(Chain, Val, DstPtr, |
| 4150 | DstSV->getValue(), DstSV->getOffset()); |
| 4151 | if (i == 2) |
| 4152 | break; |
| 4153 | SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr, |
| 4154 | DAG.getConstant(8, getPointerTy())); |
| 4155 | DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr, |
| 4156 | DAG.getConstant(8, getPointerTy())); |
| 4157 | } |
| 4158 | return Chain; |
| 4159 | } |
| 4160 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4161 | SDOperand |
| 4162 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) { |
| 4163 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue(); |
| 4164 | switch (IntNo) { |
| 4165 | default: return SDOperand(); // Don't custom lower most intrinsics. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4166 | // Comparison intrinsics. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4167 | case Intrinsic::x86_sse_comieq_ss: |
| 4168 | case Intrinsic::x86_sse_comilt_ss: |
| 4169 | case Intrinsic::x86_sse_comile_ss: |
| 4170 | case Intrinsic::x86_sse_comigt_ss: |
| 4171 | case Intrinsic::x86_sse_comige_ss: |
| 4172 | case Intrinsic::x86_sse_comineq_ss: |
| 4173 | case Intrinsic::x86_sse_ucomieq_ss: |
| 4174 | case Intrinsic::x86_sse_ucomilt_ss: |
| 4175 | case Intrinsic::x86_sse_ucomile_ss: |
| 4176 | case Intrinsic::x86_sse_ucomigt_ss: |
| 4177 | case Intrinsic::x86_sse_ucomige_ss: |
| 4178 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4179 | case Intrinsic::x86_sse2_comieq_sd: |
| 4180 | case Intrinsic::x86_sse2_comilt_sd: |
| 4181 | case Intrinsic::x86_sse2_comile_sd: |
| 4182 | case Intrinsic::x86_sse2_comigt_sd: |
| 4183 | case Intrinsic::x86_sse2_comige_sd: |
| 4184 | case Intrinsic::x86_sse2_comineq_sd: |
| 4185 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 4186 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 4187 | case Intrinsic::x86_sse2_ucomile_sd: |
| 4188 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 4189 | case Intrinsic::x86_sse2_ucomige_sd: |
| 4190 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 4191 | unsigned Opc = 0; |
| 4192 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 4193 | switch (IntNo) { |
| 4194 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4195 | case Intrinsic::x86_sse_comieq_ss: |
| 4196 | case Intrinsic::x86_sse2_comieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4197 | Opc = X86ISD::COMI; |
| 4198 | CC = ISD::SETEQ; |
| 4199 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4200 | case Intrinsic::x86_sse_comilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4201 | case Intrinsic::x86_sse2_comilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4202 | Opc = X86ISD::COMI; |
| 4203 | CC = ISD::SETLT; |
| 4204 | break; |
| 4205 | case Intrinsic::x86_sse_comile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4206 | case Intrinsic::x86_sse2_comile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4207 | Opc = X86ISD::COMI; |
| 4208 | CC = ISD::SETLE; |
| 4209 | break; |
| 4210 | case Intrinsic::x86_sse_comigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4211 | case Intrinsic::x86_sse2_comigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4212 | Opc = X86ISD::COMI; |
| 4213 | CC = ISD::SETGT; |
| 4214 | break; |
| 4215 | case Intrinsic::x86_sse_comige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4216 | case Intrinsic::x86_sse2_comige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4217 | Opc = X86ISD::COMI; |
| 4218 | CC = ISD::SETGE; |
| 4219 | break; |
| 4220 | case Intrinsic::x86_sse_comineq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4221 | case Intrinsic::x86_sse2_comineq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4222 | Opc = X86ISD::COMI; |
| 4223 | CC = ISD::SETNE; |
| 4224 | break; |
| 4225 | case Intrinsic::x86_sse_ucomieq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4226 | case Intrinsic::x86_sse2_ucomieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4227 | Opc = X86ISD::UCOMI; |
| 4228 | CC = ISD::SETEQ; |
| 4229 | break; |
| 4230 | case Intrinsic::x86_sse_ucomilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4231 | case Intrinsic::x86_sse2_ucomilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4232 | Opc = X86ISD::UCOMI; |
| 4233 | CC = ISD::SETLT; |
| 4234 | break; |
| 4235 | case Intrinsic::x86_sse_ucomile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4236 | case Intrinsic::x86_sse2_ucomile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4237 | Opc = X86ISD::UCOMI; |
| 4238 | CC = ISD::SETLE; |
| 4239 | break; |
| 4240 | case Intrinsic::x86_sse_ucomigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4241 | case Intrinsic::x86_sse2_ucomigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4242 | Opc = X86ISD::UCOMI; |
| 4243 | CC = ISD::SETGT; |
| 4244 | break; |
| 4245 | case Intrinsic::x86_sse_ucomige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4246 | case Intrinsic::x86_sse2_ucomige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4247 | Opc = X86ISD::UCOMI; |
| 4248 | CC = ISD::SETGE; |
| 4249 | break; |
| 4250 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4251 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 4252 | Opc = X86ISD::UCOMI; |
| 4253 | CC = ISD::SETNE; |
| 4254 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4255 | } |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4256 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4257 | unsigned X86CC; |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 4258 | SDOperand LHS = Op.getOperand(1); |
| 4259 | SDOperand RHS = Op.getOperand(2); |
| 4260 | translateX86CC(CC, true, X86CC, LHS, RHS, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4261 | |
| 4262 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 4263 | SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS }; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4264 | SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3); |
| 4265 | VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); |
| 4266 | SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; |
| 4267 | SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4268 | return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC); |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4269 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 4270 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4271 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4272 | |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 4273 | SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4274 | // Depths > 0 not supported yet! |
| 4275 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4276 | return SDOperand(); |
| 4277 | |
| 4278 | // Just load the return address |
| 4279 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4280 | return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0); |
| 4281 | } |
| 4282 | |
| 4283 | SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4284 | // Depths > 0 not supported yet! |
| 4285 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4286 | return SDOperand(); |
| 4287 | |
| 4288 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4289 | return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI, |
| 4290 | DAG.getConstant(4, getPointerTy())); |
| 4291 | } |
| 4292 | |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 4293 | SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op, |
| 4294 | SelectionDAG &DAG) { |
| 4295 | // Is not yet supported on x86-64 |
| 4296 | if (Subtarget->is64Bit()) |
| 4297 | return SDOperand(); |
| 4298 | |
| 4299 | return DAG.getConstant(8, getPointerTy()); |
| 4300 | } |
| 4301 | |
| 4302 | SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG) |
| 4303 | { |
| 4304 | assert(!Subtarget->is64Bit() && |
| 4305 | "Lowering of eh_return builtin is not supported yet on x86-64"); |
| 4306 | |
| 4307 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4308 | SDOperand Chain = Op.getOperand(0); |
| 4309 | SDOperand Offset = Op.getOperand(1); |
| 4310 | SDOperand Handler = Op.getOperand(2); |
| 4311 | |
| 4312 | SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF), |
| 4313 | getPointerTy()); |
| 4314 | |
| 4315 | SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame, |
| 4316 | DAG.getConstant(-4UL, getPointerTy())); |
| 4317 | StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset); |
| 4318 | Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0); |
| 4319 | Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr); |
| 4320 | MF.addLiveOut(X86::ECX); |
| 4321 | |
| 4322 | return DAG.getNode(X86ISD::EH_RETURN, MVT::Other, |
| 4323 | Chain, DAG.getRegister(X86::ECX, getPointerTy())); |
| 4324 | } |
| 4325 | |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4326 | SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op, |
| 4327 | SelectionDAG &DAG) { |
| 4328 | SDOperand Root = Op.getOperand(0); |
| 4329 | SDOperand Trmp = Op.getOperand(1); // trampoline |
| 4330 | SDOperand FPtr = Op.getOperand(2); // nested function |
| 4331 | SDOperand Nest = Op.getOperand(3); // 'nest' parameter value |
| 4332 | |
| 4333 | SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4)); |
| 4334 | |
| 4335 | if (Subtarget->is64Bit()) { |
| 4336 | return SDOperand(); // not yet supported |
| 4337 | } else { |
| 4338 | Function *Func = (Function *) |
| 4339 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); |
| 4340 | unsigned CC = Func->getCallingConv(); |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4341 | unsigned NestReg; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4342 | |
| 4343 | switch (CC) { |
| 4344 | default: |
| 4345 | assert(0 && "Unsupported calling convention"); |
| 4346 | case CallingConv::C: |
| 4347 | case CallingConv::Fast: |
| 4348 | case CallingConv::X86_StdCall: { |
| 4349 | // Pass 'nest' parameter in ECX. |
| 4350 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4351 | NestReg = X86::ECX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4352 | |
| 4353 | // Check that ECX wasn't needed by an 'inreg' parameter. |
| 4354 | const FunctionType *FTy = Func->getFunctionType(); |
| 4355 | const ParamAttrsList *Attrs = FTy->getParamAttrs(); |
| 4356 | |
| 4357 | if (Attrs && !Func->isVarArg()) { |
| 4358 | unsigned InRegCount = 0; |
| 4359 | unsigned Idx = 1; |
| 4360 | |
| 4361 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 4362 | E = FTy->param_end(); I != E; ++I, ++Idx) |
| 4363 | if (Attrs->paramHasAttr(Idx, ParamAttr::InReg)) |
| 4364 | // FIXME: should only count parameters that are lowered to integers. |
| 4365 | InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32; |
| 4366 | |
| 4367 | if (InRegCount > 2) { |
| 4368 | cerr << "Nest register in use - reduce number of inreg parameters!\n"; |
| 4369 | abort(); |
| 4370 | } |
| 4371 | } |
| 4372 | break; |
| 4373 | } |
| 4374 | case CallingConv::X86_FastCall: |
| 4375 | // Pass 'nest' parameter in EAX. |
| 4376 | // Must be kept in sync with X86CallingConv.td |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4377 | NestReg = X86::EAX; |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4378 | break; |
| 4379 | } |
| 4380 | |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4381 | const X86InstrInfo *TII = |
| 4382 | ((X86TargetMachine&)getTargetMachine()).getInstrInfo(); |
| 4383 | |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4384 | SDOperand OutChains[4]; |
| 4385 | SDOperand Addr, Disp; |
| 4386 | |
| 4387 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32)); |
| 4388 | Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr); |
| 4389 | |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4390 | unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri); |
| 4391 | unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg); |
| 4392 | OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8), |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4393 | Trmp, TrmpSV->getValue(), TrmpSV->getOffset()); |
| 4394 | |
| 4395 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32)); |
| 4396 | OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(), |
| 4397 | TrmpSV->getOffset() + 1, false, 1); |
| 4398 | |
Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 4399 | unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4400 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32)); |
| 4401 | OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr, |
| 4402 | TrmpSV->getValue() + 5, TrmpSV->getOffset()); |
| 4403 | |
| 4404 | Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32)); |
| 4405 | OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(), |
| 4406 | TrmpSV->getOffset() + 6, false, 1); |
| 4407 | |
| 4408 | return DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4); |
| 4409 | } |
| 4410 | } |
| 4411 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4412 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 4413 | /// |
| 4414 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 4415 | switch (Op.getOpcode()) { |
| 4416 | default: assert(0 && "Should not custom lower this!"); |
| 4417 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 4418 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 4419 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 4420 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 4421 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 4422 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 4423 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4424 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4425 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
| 4426 | case ISD::SHL_PARTS: |
| 4427 | case ISD::SRA_PARTS: |
| 4428 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 4429 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
| 4430 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| 4431 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 4432 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4433 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4434 | case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4435 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 4436 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
| 4437 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 4438 | case ISD::CALL: return LowerCALL(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4439 | case ISD::RET: return LowerRET(Op, DAG); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 4440 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4441 | case ISD::MEMSET: return LowerMEMSET(Op, DAG); |
| 4442 | case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); |
| 4443 | case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG); |
| 4444 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 4445 | case ISD::VACOPY: return LowerVACOPY(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4446 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 4447 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 4448 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 4449 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 4450 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); |
Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 4451 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 4452 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); |
Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 4453 | case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4454 | } |
Jim Laskey | 62819f3 | 2007-02-21 22:54:50 +0000 | [diff] [blame] | 4455 | return SDOperand(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4458 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 4459 | switch (Opcode) { |
| 4460 | default: return NULL; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4461 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 4462 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 4463 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4464 | case X86ISD::FOR: return "X86ISD::FOR"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 4465 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4466 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4467 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 4468 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4469 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 4470 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 4471 | 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] | 4472 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 4473 | case X86ISD::FST: return "X86ISD::FST"; |
| 4474 | case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 4475 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4476 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 4477 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 4478 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 4479 | case X86ISD::CMP: return "X86ISD::CMP"; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4480 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 4481 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 4482 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4483 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 4484 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 4485 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 4486 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 4487 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 4488 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 4489 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Evan Cheng | bc4832b | 2006-03-24 23:15:12 +0000 | [diff] [blame] | 4490 | case X86ISD::S2VEC: return "X86ISD::S2VEC"; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4491 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 4492 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4493 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 4494 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 4495 | case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; |
| 4496 | case X86ISD::FRCP: return "X86ISD::FRCP"; |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 4497 | case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; |
| 4498 | case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER"; |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 4499 | case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4500 | } |
| 4501 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4502 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 4503 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 4504 | // by AM is legal for this target, for a load/store of the specified type. |
| 4505 | bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 4506 | const Type *Ty) const { |
| 4507 | // X86 supports extremely general addressing modes. |
| 4508 | |
| 4509 | // X86 allows a sign-extended 32-bit immediate field as a displacement. |
| 4510 | if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1) |
| 4511 | return false; |
| 4512 | |
| 4513 | if (AM.BaseGV) { |
Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 4514 | // We can only fold this if we don't need an extra load. |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 4515 | if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false)) |
| 4516 | return false; |
Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 4517 | |
| 4518 | // X86-64 only supports addr of globals in small code model. |
| 4519 | if (Subtarget->is64Bit()) { |
| 4520 | if (getTargetMachine().getCodeModel() != CodeModel::Small) |
| 4521 | return false; |
| 4522 | // If lower 4G is not available, then we must use rip-relative addressing. |
| 4523 | if (AM.BaseOffs || AM.Scale > 1) |
| 4524 | return false; |
| 4525 | } |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | switch (AM.Scale) { |
| 4529 | case 0: |
| 4530 | case 1: |
| 4531 | case 2: |
| 4532 | case 4: |
| 4533 | case 8: |
| 4534 | // These scales always work. |
| 4535 | break; |
| 4536 | case 3: |
| 4537 | case 5: |
| 4538 | case 9: |
| 4539 | // These scales are formed with basereg+scalereg. Only accept if there is |
| 4540 | // no basereg yet. |
| 4541 | if (AM.HasBaseReg) |
| 4542 | return false; |
| 4543 | break; |
| 4544 | default: // Other stuff never works. |
| 4545 | return false; |
| 4546 | } |
| 4547 | |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
| 4551 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4552 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 4553 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 4554 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 4555 | /// are assumed to be legal. |
| 4556 | bool |
| 4557 | X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const { |
| 4558 | // Only do shuffles on 128-bit vector types for now. |
| 4559 | if (MVT::getSizeInBits(VT) == 64) return false; |
| 4560 | return (Mask.Val->getNumOperands() <= 4 || |
Evan Cheng | 49892af | 2007-06-19 00:02:56 +0000 | [diff] [blame] | 4561 | isIdentityMask(Mask.Val) || |
| 4562 | isIdentityMask(Mask.Val, true) || |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4563 | isSplatMask(Mask.Val) || |
| 4564 | isPSHUFHW_PSHUFLWMask(Mask.Val) || |
| 4565 | X86::isUNPCKLMask(Mask.Val) || |
Evan Cheng | 49892af | 2007-06-19 00:02:56 +0000 | [diff] [blame] | 4566 | X86::isUNPCKHMask(Mask.Val) || |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4567 | X86::isUNPCKL_v_undef_Mask(Mask.Val) || |
Evan Cheng | 49892af | 2007-06-19 00:02:56 +0000 | [diff] [blame] | 4568 | X86::isUNPCKH_v_undef_Mask(Mask.Val)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps, |
| 4572 | MVT::ValueType EVT, |
| 4573 | SelectionDAG &DAG) const { |
| 4574 | unsigned NumElts = BVOps.size(); |
| 4575 | // Only do shuffles on 128-bit vector types for now. |
| 4576 | if (MVT::getSizeInBits(EVT) * NumElts == 64) return false; |
| 4577 | if (NumElts == 2) return true; |
| 4578 | if (NumElts == 4) { |
Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 4579 | return (isMOVLMask(&BVOps[0], 4) || |
| 4580 | isCommutedMOVL(&BVOps[0], 4, true) || |
| 4581 | isSHUFPMask(&BVOps[0], 4) || |
| 4582 | isCommutedSHUFP(&BVOps[0], 4)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4583 | } |
| 4584 | return false; |
| 4585 | } |
| 4586 | |
| 4587 | //===----------------------------------------------------------------------===// |
| 4588 | // X86 Scheduler Hooks |
| 4589 | //===----------------------------------------------------------------------===// |
| 4590 | |
| 4591 | MachineBasicBlock * |
| 4592 | X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 4593 | MachineBasicBlock *BB) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4594 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4595 | switch (MI->getOpcode()) { |
| 4596 | default: assert(false && "Unexpected instr type to insert"); |
| 4597 | case X86::CMOV_FR32: |
| 4598 | case X86::CMOV_FR64: |
| 4599 | case X86::CMOV_V4F32: |
| 4600 | case X86::CMOV_V2F64: |
| 4601 | case X86::CMOV_V2I64: { |
| 4602 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 4603 | // diamond control-flow pattern. The incoming instruction knows the |
| 4604 | // destination vreg to set, the condition code register to branch on, the |
| 4605 | // true/false values to select between, and a branch opcode to use. |
| 4606 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 4607 | ilist<MachineBasicBlock>::iterator It = BB; |
| 4608 | ++It; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4609 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4610 | // thisMBB: |
| 4611 | // ... |
| 4612 | // TrueVal = ... |
| 4613 | // cmpTY ccX, r1, r2 |
| 4614 | // bCC copy1MBB |
| 4615 | // fallthrough --> copy0MBB |
| 4616 | MachineBasicBlock *thisMBB = BB; |
| 4617 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 4618 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4619 | unsigned Opc = |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4620 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4621 | BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4622 | MachineFunction *F = BB->getParent(); |
| 4623 | F->getBasicBlockList().insert(It, copy0MBB); |
| 4624 | F->getBasicBlockList().insert(It, sinkMBB); |
| 4625 | // Update machine-CFG edges by first adding all successors of the current |
| 4626 | // block to the new block which will contain the Phi node for the select. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4627 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4628 | e = BB->succ_end(); i != e; ++i) |
| 4629 | sinkMBB->addSuccessor(*i); |
| 4630 | // Next, remove all successors of the current block, and add the true |
| 4631 | // and fallthrough blocks as its successors. |
| 4632 | while(!BB->succ_empty()) |
| 4633 | BB->removeSuccessor(BB->succ_begin()); |
| 4634 | BB->addSuccessor(copy0MBB); |
| 4635 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4636 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4637 | // copy0MBB: |
| 4638 | // %FalseValue = ... |
| 4639 | // # fallthrough to sinkMBB |
| 4640 | BB = copy0MBB; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4641 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4642 | // Update machine-CFG edges |
| 4643 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4644 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4645 | // sinkMBB: |
| 4646 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 4647 | // ... |
| 4648 | BB = sinkMBB; |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4649 | BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4650 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 4651 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 4652 | |
| 4653 | delete MI; // The pseudo instruction is gone now. |
| 4654 | return BB; |
| 4655 | } |
| 4656 | |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 4657 | case X86::FP32_TO_INT16_IN_MEM: |
| 4658 | case X86::FP32_TO_INT32_IN_MEM: |
| 4659 | case X86::FP32_TO_INT64_IN_MEM: |
| 4660 | case X86::FP64_TO_INT16_IN_MEM: |
| 4661 | case X86::FP64_TO_INT32_IN_MEM: |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 4662 | case X86::FP64_TO_INT64_IN_MEM: |
| 4663 | case X86::FP80_TO_INT16_IN_MEM: |
| 4664 | case X86::FP80_TO_INT32_IN_MEM: |
| 4665 | case X86::FP80_TO_INT64_IN_MEM: { |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4666 | // Change the floating point control register to use "round towards zero" |
| 4667 | // mode when truncating to an integer value. |
| 4668 | MachineFunction *F = BB->getParent(); |
| 4669 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4670 | addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4671 | |
| 4672 | // Load the old value of the high byte of the control word... |
| 4673 | unsigned OldCW = |
| 4674 | F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4675 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4676 | |
| 4677 | // Set the high part to be round to zero... |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4678 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx) |
| 4679 | .addImm(0xC7F); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4680 | |
| 4681 | // Reload the modified control word now... |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4682 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4683 | |
| 4684 | // Restore the memory image of control word to original value |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4685 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx) |
| 4686 | .addReg(OldCW); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4687 | |
| 4688 | // Get the X86 opcode to use. |
| 4689 | unsigned Opc; |
| 4690 | switch (MI->getOpcode()) { |
| 4691 | default: assert(0 && "illegal opcode!"); |
Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 4692 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; |
| 4693 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; |
| 4694 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; |
| 4695 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; |
| 4696 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; |
| 4697 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; |
Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 4698 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; |
| 4699 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; |
| 4700 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | X86AddressMode AM; |
| 4704 | MachineOperand &Op = MI->getOperand(0); |
| 4705 | if (Op.isRegister()) { |
| 4706 | AM.BaseType = X86AddressMode::RegBase; |
| 4707 | AM.Base.Reg = Op.getReg(); |
| 4708 | } else { |
| 4709 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 4710 | AM.Base.FrameIndex = Op.getFrameIndex(); |
| 4711 | } |
| 4712 | Op = MI->getOperand(1); |
| 4713 | if (Op.isImmediate()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4714 | AM.Scale = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4715 | Op = MI->getOperand(2); |
| 4716 | if (Op.isImmediate()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4717 | AM.IndexReg = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4718 | Op = MI->getOperand(3); |
| 4719 | if (Op.isGlobalAddress()) { |
| 4720 | AM.GV = Op.getGlobal(); |
| 4721 | } else { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4722 | AM.Disp = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4723 | } |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4724 | addFullAddress(BuildMI(BB, TII->get(Opc)), AM) |
| 4725 | .addReg(MI->getOperand(4).getReg()); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4726 | |
| 4727 | // Reload the original control word now. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4728 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4729 | |
| 4730 | delete MI; // The pseudo instruction is gone now. |
| 4731 | return BB; |
| 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | //===----------------------------------------------------------------------===// |
| 4737 | // X86 Optimization Hooks |
| 4738 | //===----------------------------------------------------------------------===// |
| 4739 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4740 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 4741 | uint64_t Mask, |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4742 | uint64_t &KnownZero, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4743 | uint64_t &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4744 | const SelectionDAG &DAG, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4745 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4746 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4747 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 4748 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 4749 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 4750 | Opc == ISD::INTRINSIC_VOID) && |
| 4751 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 4752 | " is a target node!"); |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4753 | |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4754 | KnownZero = KnownOne = 0; // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4755 | switch (Opc) { |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4756 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4757 | case X86ISD::SETCC: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4758 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 4759 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4760 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4761 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 4762 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4763 | /// getShuffleScalarElt - Returns the scalar element that will make up the ith |
| 4764 | /// element of the result of the vector shuffle. |
| 4765 | static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) { |
| 4766 | MVT::ValueType VT = N->getValueType(0); |
| 4767 | SDOperand PermMask = N->getOperand(2); |
| 4768 | unsigned NumElems = PermMask.getNumOperands(); |
| 4769 | SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1); |
| 4770 | i %= NumElems; |
| 4771 | if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 4772 | return (i == 0) |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4773 | ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT)); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4774 | } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) { |
| 4775 | SDOperand Idx = PermMask.getOperand(i); |
| 4776 | if (Idx.getOpcode() == ISD::UNDEF) |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4777 | return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT)); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4778 | return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG); |
| 4779 | } |
| 4780 | return SDOperand(); |
| 4781 | } |
| 4782 | |
| 4783 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
| 4784 | /// node is a GlobalAddress + an offset. |
| 4785 | static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) { |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 4786 | unsigned Opc = N->getOpcode(); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 4787 | if (Opc == X86ISD::Wrapper) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4788 | if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) { |
| 4789 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
| 4790 | return true; |
| 4791 | } |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 4792 | } else if (Opc == ISD::ADD) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4793 | SDOperand N1 = N->getOperand(0); |
| 4794 | SDOperand N2 = N->getOperand(1); |
| 4795 | if (isGAPlusOffset(N1.Val, GA, Offset)) { |
| 4796 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2); |
| 4797 | if (V) { |
| 4798 | Offset += V->getSignExtended(); |
| 4799 | return true; |
| 4800 | } |
| 4801 | } else if (isGAPlusOffset(N2.Val, GA, Offset)) { |
| 4802 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1); |
| 4803 | if (V) { |
| 4804 | Offset += V->getSignExtended(); |
| 4805 | return true; |
| 4806 | } |
| 4807 | } |
| 4808 | } |
| 4809 | return false; |
| 4810 | } |
| 4811 | |
| 4812 | /// isConsecutiveLoad - Returns true if N is loading from an address of Base |
| 4813 | /// + Dist * Size. |
| 4814 | static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size, |
| 4815 | MachineFrameInfo *MFI) { |
| 4816 | if (N->getOperand(0).Val != Base->getOperand(0).Val) |
| 4817 | return false; |
| 4818 | |
| 4819 | SDOperand Loc = N->getOperand(1); |
| 4820 | SDOperand BaseLoc = Base->getOperand(1); |
| 4821 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 4822 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 4823 | return false; |
Dan Gohman | 275769a | 2007-07-23 20:24:29 +0000 | [diff] [blame] | 4824 | int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 4825 | int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4826 | int FS = MFI->getObjectSize(FI); |
| 4827 | int BFS = MFI->getObjectSize(BFI); |
| 4828 | if (FS != BFS || FS != Size) return false; |
| 4829 | return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size); |
| 4830 | } else { |
| 4831 | GlobalValue *GV1 = NULL; |
| 4832 | GlobalValue *GV2 = NULL; |
| 4833 | int64_t Offset1 = 0; |
| 4834 | int64_t Offset2 = 0; |
| 4835 | bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1); |
| 4836 | bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2); |
| 4837 | if (isGA1 && isGA2 && GV1 == GV2) |
| 4838 | return Offset1 == (Offset2 + Dist*Size); |
| 4839 | } |
| 4840 | |
| 4841 | return false; |
| 4842 | } |
| 4843 | |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 4844 | static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI, |
| 4845 | const X86Subtarget *Subtarget) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4846 | GlobalValue *GV; |
| 4847 | int64_t Offset; |
| 4848 | if (isGAPlusOffset(Base, GV, Offset)) |
| 4849 | return (GV->getAlignment() >= 16 && (Offset % 16) == 0); |
| 4850 | else { |
| 4851 | assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!"); |
Dan Gohman | 275769a | 2007-07-23 20:24:29 +0000 | [diff] [blame] | 4852 | int BFI = cast<FrameIndexSDNode>(Base)->getIndex(); |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 4853 | if (BFI < 0) |
| 4854 | // Fixed objects do not specify alignment, however the offsets are known. |
| 4855 | return ((Subtarget->getStackAlignment() % 16) == 0 && |
| 4856 | (MFI->getObjectOffset(BFI) % 16) == 0); |
| 4857 | else |
| 4858 | return MFI->getObjectAlignment(BFI) >= 16; |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4859 | } |
| 4860 | return false; |
| 4861 | } |
| 4862 | |
| 4863 | |
| 4864 | /// PerformShuffleCombine - Combine a vector_shuffle that is equal to |
| 4865 | /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load |
| 4866 | /// if the load addresses are consecutive, non-overlapping, and in the right |
| 4867 | /// order. |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 4868 | static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
| 4869 | const X86Subtarget *Subtarget) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4870 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4871 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 4872 | MVT::ValueType VT = N->getValueType(0); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4873 | MVT::ValueType EVT = MVT::getVectorElementType(VT); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4874 | SDOperand PermMask = N->getOperand(2); |
| 4875 | int NumElems = (int)PermMask.getNumOperands(); |
| 4876 | SDNode *Base = NULL; |
| 4877 | for (int i = 0; i < NumElems; ++i) { |
| 4878 | SDOperand Idx = PermMask.getOperand(i); |
| 4879 | if (Idx.getOpcode() == ISD::UNDEF) { |
| 4880 | if (!Base) return SDOperand(); |
| 4881 | } else { |
| 4882 | SDOperand Arg = |
| 4883 | getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4884 | if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val)) |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4885 | return SDOperand(); |
| 4886 | if (!Base) |
| 4887 | Base = Arg.Val; |
| 4888 | else if (!isConsecutiveLoad(Arg.Val, Base, |
| 4889 | i, MVT::getSizeInBits(EVT)/8,MFI)) |
| 4890 | return SDOperand(); |
| 4891 | } |
| 4892 | } |
| 4893 | |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 4894 | bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget); |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4895 | LoadSDNode *LD = cast<LoadSDNode>(Base); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4896 | if (isAlign16) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4897 | return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4898 | LD->getSrcValueOffset(), LD->isVolatile()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4899 | } else { |
Dan Gohman | d300622 | 2007-07-27 17:16:43 +0000 | [diff] [blame] | 4900 | return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), |
| 4901 | LD->getSrcValueOffset(), LD->isVolatile(), |
| 4902 | LD->getAlignment()); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4903 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4906 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. |
| 4907 | static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
| 4908 | const X86Subtarget *Subtarget) { |
| 4909 | SDOperand Cond = N->getOperand(0); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4910 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4911 | // If we have SSE[12] support, try to form min/max nodes. |
| 4912 | if (Subtarget->hasSSE2() && |
| 4913 | (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) { |
| 4914 | if (Cond.getOpcode() == ISD::SETCC) { |
| 4915 | // Get the LHS/RHS of the select. |
| 4916 | SDOperand LHS = N->getOperand(1); |
| 4917 | SDOperand RHS = N->getOperand(2); |
| 4918 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4919 | |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4920 | unsigned Opcode = 0; |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4921 | if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4922 | switch (CC) { |
| 4923 | default: break; |
| 4924 | case ISD::SETOLE: // (X <= Y) ? X : Y -> min |
| 4925 | case ISD::SETULE: |
| 4926 | case ISD::SETLE: |
| 4927 | if (!UnsafeFPMath) break; |
| 4928 | // FALL THROUGH. |
| 4929 | case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min |
| 4930 | case ISD::SETLT: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4931 | Opcode = X86ISD::FMIN; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4932 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4933 | |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4934 | case ISD::SETOGT: // (X > Y) ? X : Y -> max |
| 4935 | case ISD::SETUGT: |
| 4936 | case ISD::SETGT: |
| 4937 | if (!UnsafeFPMath) break; |
| 4938 | // FALL THROUGH. |
| 4939 | case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max |
| 4940 | case ISD::SETGE: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4941 | Opcode = X86ISD::FMAX; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4942 | break; |
| 4943 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4944 | } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4945 | switch (CC) { |
| 4946 | default: break; |
| 4947 | case ISD::SETOGT: // (X > Y) ? Y : X -> min |
| 4948 | case ISD::SETUGT: |
| 4949 | case ISD::SETGT: |
| 4950 | if (!UnsafeFPMath) break; |
| 4951 | // FALL THROUGH. |
| 4952 | case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min |
| 4953 | case ISD::SETGE: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4954 | Opcode = X86ISD::FMIN; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4955 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4956 | |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4957 | case ISD::SETOLE: // (X <= Y) ? Y : X -> max |
| 4958 | case ISD::SETULE: |
| 4959 | case ISD::SETLE: |
| 4960 | if (!UnsafeFPMath) break; |
| 4961 | // FALL THROUGH. |
| 4962 | case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max |
| 4963 | case ISD::SETLT: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4964 | Opcode = X86ISD::FMAX; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 4965 | break; |
| 4966 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4967 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4968 | |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4969 | if (Opcode) |
| 4970 | return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4971 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4972 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | return SDOperand(); |
| 4976 | } |
| 4977 | |
| 4978 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4979 | SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N, |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4980 | DAGCombinerInfo &DCI) const { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4981 | SelectionDAG &DAG = DCI.DAG; |
| 4982 | switch (N->getOpcode()) { |
| 4983 | default: break; |
| 4984 | case ISD::VECTOR_SHUFFLE: |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 4985 | return PerformShuffleCombine(N, DAG, Subtarget); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 4986 | case ISD::SELECT: |
| 4987 | return PerformSELECTCombine(N, DAG, Subtarget); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | return SDOperand(); |
| 4991 | } |
| 4992 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4993 | //===----------------------------------------------------------------------===// |
| 4994 | // X86 Inline Assembly Support |
| 4995 | //===----------------------------------------------------------------------===// |
| 4996 | |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 4997 | /// getConstraintType - Given a constraint letter, return the type of |
| 4998 | /// constraint it is for this target. |
| 4999 | X86TargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 5000 | X86TargetLowering::getConstraintType(const std::string &Constraint) const { |
| 5001 | if (Constraint.size() == 1) { |
| 5002 | switch (Constraint[0]) { |
| 5003 | case 'A': |
| 5004 | case 'r': |
| 5005 | case 'R': |
| 5006 | case 'l': |
| 5007 | case 'q': |
| 5008 | case 'Q': |
| 5009 | case 'x': |
| 5010 | case 'Y': |
| 5011 | return C_RegisterClass; |
| 5012 | default: |
| 5013 | break; |
| 5014 | } |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5015 | } |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 5016 | return TargetLowering::getConstraintType(Constraint); |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5019 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 5020 | /// vector. If it is invalid, don't add anything to Ops. |
| 5021 | void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op, |
| 5022 | char Constraint, |
| 5023 | std::vector<SDOperand>&Ops, |
| 5024 | SelectionDAG &DAG) { |
| 5025 | SDOperand Result(0, 0); |
| 5026 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5027 | switch (Constraint) { |
| 5028 | default: break; |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 5029 | case 'I': |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 5030 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5031 | if (C->getValue() <= 31) { |
| 5032 | Result = DAG.getTargetConstant(C->getValue(), Op.getValueType()); |
| 5033 | break; |
| 5034 | } |
Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 5035 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5036 | return; |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 5037 | case 'N': |
| 5038 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5039 | if (C->getValue() <= 255) { |
| 5040 | Result = DAG.getTargetConstant(C->getValue(), Op.getValueType()); |
| 5041 | break; |
| 5042 | } |
Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 5043 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5044 | return; |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 5045 | case 'i': { |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5046 | // Literal immediates are always ok. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5047 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { |
| 5048 | Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType()); |
| 5049 | break; |
| 5050 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5051 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 5052 | // If we are in non-pic codegen mode, we allow the address of a global (with |
| 5053 | // an optional displacement) to be used with 'i'. |
| 5054 | GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op); |
| 5055 | int64_t Offset = 0; |
| 5056 | |
| 5057 | // Match either (GA) or (GA+C) |
| 5058 | if (GA) { |
| 5059 | Offset = GA->getOffset(); |
| 5060 | } else if (Op.getOpcode() == ISD::ADD) { |
| 5061 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5062 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 5063 | if (C && GA) { |
| 5064 | Offset = GA->getOffset()+C->getValue(); |
| 5065 | } else { |
| 5066 | C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 5067 | GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0)); |
| 5068 | if (C && GA) |
| 5069 | Offset = GA->getOffset()+C->getValue(); |
| 5070 | else |
| 5071 | C = 0, GA = 0; |
| 5072 | } |
| 5073 | } |
| 5074 | |
| 5075 | if (GA) { |
| 5076 | // If addressing this global requires a load (e.g. in PIC mode), we can't |
| 5077 | // match. |
| 5078 | if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(), |
| 5079 | false)) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5080 | return; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5081 | |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 5082 | Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), |
| 5083 | Offset); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5084 | Result = Op; |
| 5085 | break; |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5086 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5087 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5088 | // Otherwise, not valid for this mode. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5089 | return; |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5090 | } |
Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 5091 | } |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5092 | |
| 5093 | if (Result.Val) { |
| 5094 | Ops.push_back(Result); |
| 5095 | return; |
| 5096 | } |
| 5097 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5100 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 5101 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 5102 | MVT::ValueType VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5103 | if (Constraint.size() == 1) { |
| 5104 | // FIXME: not handling fp-stack yet! |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5105 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5106 | default: break; // Unknown constraint letter |
| 5107 | case 'A': // EAX/EDX |
| 5108 | if (VT == MVT::i32 || VT == MVT::i64) |
| 5109 | return make_vector<unsigned>(X86::EAX, X86::EDX, 0); |
| 5110 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5111 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 5112 | case 'Q': // Q_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5113 | if (VT == MVT::i32) |
| 5114 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 5115 | else if (VT == MVT::i16) |
| 5116 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 5117 | else if (VT == MVT::i8) |
Evan Cheng | 1291438 | 2007-08-13 23:27:11 +0000 | [diff] [blame] | 5118 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5119 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5120 | } |
| 5121 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5122 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 5123 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5124 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5125 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5126 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5127 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
| 5128 | MVT::ValueType VT) const { |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 5129 | // First, see if this is a constraint that directly corresponds to an LLVM |
| 5130 | // register class. |
| 5131 | if (Constraint.size() == 1) { |
| 5132 | // GCC Constraint Letters |
| 5133 | switch (Constraint[0]) { |
| 5134 | default: break; |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 5135 | case 'r': // GENERAL_REGS |
| 5136 | case 'R': // LEGACY_REGS |
| 5137 | case 'l': // INDEX_REGS |
| 5138 | if (VT == MVT::i64 && Subtarget->is64Bit()) |
| 5139 | return std::make_pair(0U, X86::GR64RegisterClass); |
| 5140 | if (VT == MVT::i32) |
| 5141 | return std::make_pair(0U, X86::GR32RegisterClass); |
| 5142 | else if (VT == MVT::i16) |
| 5143 | return std::make_pair(0U, X86::GR16RegisterClass); |
| 5144 | else if (VT == MVT::i8) |
| 5145 | return std::make_pair(0U, X86::GR8RegisterClass); |
| 5146 | break; |
Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 5147 | case 'y': // MMX_REGS if MMX allowed. |
| 5148 | if (!Subtarget->hasMMX()) break; |
| 5149 | return std::make_pair(0U, X86::VR64RegisterClass); |
| 5150 | break; |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 5151 | case 'Y': // SSE_REGS if SSE2 allowed |
| 5152 | if (!Subtarget->hasSSE2()) break; |
| 5153 | // FALL THROUGH. |
| 5154 | case 'x': // SSE_REGS if SSE1 allowed |
| 5155 | if (!Subtarget->hasSSE1()) break; |
| 5156 | |
| 5157 | switch (VT) { |
| 5158 | default: break; |
| 5159 | // Scalar SSE types. |
| 5160 | case MVT::f32: |
| 5161 | case MVT::i32: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 5162 | return std::make_pair(0U, X86::FR32RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 5163 | case MVT::f64: |
| 5164 | case MVT::i64: |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 5165 | return std::make_pair(0U, X86::FR64RegisterClass); |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 5166 | // Vector types. |
Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 5167 | case MVT::v16i8: |
| 5168 | case MVT::v8i16: |
| 5169 | case MVT::v4i32: |
| 5170 | case MVT::v2i64: |
| 5171 | case MVT::v4f32: |
| 5172 | case MVT::v2f64: |
| 5173 | return std::make_pair(0U, X86::VR128RegisterClass); |
| 5174 | } |
Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 5175 | break; |
| 5176 | } |
| 5177 | } |
| 5178 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5179 | // Use the default implementation in TargetLowering to convert the register |
| 5180 | // constraint into a member of a register class. |
| 5181 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 5182 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 5183 | |
| 5184 | // Not found as a standard register? |
| 5185 | if (Res.second == 0) { |
| 5186 | // GCC calls "st(0)" just plain "st". |
| 5187 | if (StringsEqualNoCase("{st}", Constraint)) { |
| 5188 | Res.first = X86::ST0; |
| 5189 | Res.second = X86::RSTRegisterClass; |
| 5190 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5191 | |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 5192 | return Res; |
| 5193 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5194 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5195 | // Otherwise, check to see if this is a register class of the wrong value |
| 5196 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 5197 | // turn into {ax},{dx}. |
| 5198 | if (Res.second->hasType(VT)) |
| 5199 | return Res; // Correct type already, nothing to do. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5200 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5201 | // All of the single-register GCC register classes map their values onto |
| 5202 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 5203 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 5204 | // class and return the appropriate register. |
| 5205 | if (Res.second != X86::GR16RegisterClass) |
| 5206 | return Res; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5207 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5208 | if (VT == MVT::i8) { |
| 5209 | unsigned DestReg = 0; |
| 5210 | switch (Res.first) { |
| 5211 | default: break; |
| 5212 | case X86::AX: DestReg = X86::AL; break; |
| 5213 | case X86::DX: DestReg = X86::DL; break; |
| 5214 | case X86::CX: DestReg = X86::CL; break; |
| 5215 | case X86::BX: DestReg = X86::BL; break; |
| 5216 | } |
| 5217 | if (DestReg) { |
| 5218 | Res.first = DestReg; |
| 5219 | Res.second = Res.second = X86::GR8RegisterClass; |
| 5220 | } |
| 5221 | } else if (VT == MVT::i32) { |
| 5222 | unsigned DestReg = 0; |
| 5223 | switch (Res.first) { |
| 5224 | default: break; |
| 5225 | case X86::AX: DestReg = X86::EAX; break; |
| 5226 | case X86::DX: DestReg = X86::EDX; break; |
| 5227 | case X86::CX: DestReg = X86::ECX; break; |
| 5228 | case X86::BX: DestReg = X86::EBX; break; |
| 5229 | case X86::SI: DestReg = X86::ESI; break; |
| 5230 | case X86::DI: DestReg = X86::EDI; break; |
| 5231 | case X86::BP: DestReg = X86::EBP; break; |
| 5232 | case X86::SP: DestReg = X86::ESP; break; |
| 5233 | } |
| 5234 | if (DestReg) { |
| 5235 | Res.first = DestReg; |
| 5236 | Res.second = Res.second = X86::GR32RegisterClass; |
| 5237 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5238 | } else if (VT == MVT::i64) { |
| 5239 | unsigned DestReg = 0; |
| 5240 | switch (Res.first) { |
| 5241 | default: break; |
| 5242 | case X86::AX: DestReg = X86::RAX; break; |
| 5243 | case X86::DX: DestReg = X86::RDX; break; |
| 5244 | case X86::CX: DestReg = X86::RCX; break; |
| 5245 | case X86::BX: DestReg = X86::RBX; break; |
| 5246 | case X86::SI: DestReg = X86::RSI; break; |
| 5247 | case X86::DI: DestReg = X86::RDI; break; |
| 5248 | case X86::BP: DestReg = X86::RBP; break; |
| 5249 | case X86::SP: DestReg = X86::RSP; break; |
| 5250 | } |
| 5251 | if (DestReg) { |
| 5252 | Res.first = DestReg; |
| 5253 | Res.second = Res.second = X86::GR64RegisterClass; |
| 5254 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5255 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5256 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5257 | return Res; |
| 5258 | } |