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" |
| 18 | #include "X86TargetMachine.h" |
| 19 | #include "llvm/CallingConv.h" |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/VectorExtras.h" |
| 25 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/SelectionDAG.h" |
| 30 | #include "llvm/CodeGen/SSARegMap.h" |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
| 33 | using namespace llvm; |
| 34 | |
| 35 | // FIXME: temporary. |
| 36 | #include "llvm/Support/CommandLine.h" |
| 37 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 38 | cl::desc("Enable fastcc on X86")); |
| 39 | |
| 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(); |
| 44 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 45 | // Set up the TargetLowering object. |
| 46 | |
| 47 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 48 | setShiftAmountType(MVT::i8); |
| 49 | setSetCCResultType(MVT::i8); |
| 50 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 51 | setSchedulingPreference(SchedulingForRegPressure); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 52 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Chris Lattner | 9edba76 | 2006-01-13 18:00:54 +0000 | [diff] [blame] | 53 | setStackPointerRegisterToSaveRestore(X86::ESP); |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 54 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 55 | if (!Subtarget->isTargetDarwin()) |
Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 56 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
| 57 | setUseUnderscoreSetJmpLongJmp(true); |
| 58 | |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 59 | // Add legal addressing mode scale values. |
| 60 | addLegalAddressScale(8); |
| 61 | addLegalAddressScale(4); |
| 62 | addLegalAddressScale(2); |
| 63 | // Enter the ones which require both scale + index last. These are more |
| 64 | // expensive. |
| 65 | addLegalAddressScale(9); |
| 66 | addLegalAddressScale(5); |
| 67 | addLegalAddressScale(3); |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 68 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 69 | // Set up the register classes. |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 70 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 71 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 72 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 73 | |
| 74 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 75 | // operation. |
| 76 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 77 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 78 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 79 | |
| 80 | if (X86ScalarSSE) |
| 81 | // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead. |
| 82 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); |
| 83 | else |
| 84 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 85 | |
| 86 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 87 | // this operation. |
| 88 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 89 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 90 | // SSE has no i16 to fp conversion, only i32 |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 91 | if (X86ScalarSSE) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 92 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 93 | else { |
| 94 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 95 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 96 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 97 | |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 98 | // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64 |
| 99 | // isn't legal. |
| 100 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 101 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 102 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 103 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 104 | // this operation. |
| 105 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 106 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 107 | |
| 108 | if (X86ScalarSSE) { |
| 109 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| 110 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 111 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 116 | // conversion. |
| 117 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 118 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 119 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 120 | |
Evan Cheng | 45af8fd | 2006-02-18 07:26:17 +0000 | [diff] [blame] | 121 | if (X86ScalarSSE && !Subtarget->hasSSE3()) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 122 | // Expand FP_TO_UINT into a select. |
| 123 | // FIXME: We would like to use a Custom expander here eventually to do |
| 124 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 125 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 126 | else |
Evan Cheng | 45af8fd | 2006-02-18 07:26:17 +0000 | [diff] [blame] | 127 | // With SSE3 we can use fisttpll to convert to a signed i64. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 128 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 129 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 130 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 131 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 132 | |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 133 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 134 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 135 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 136 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 137 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | e80242a | 2005-12-07 17:59:14 +0000 | [diff] [blame] | 138 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 139 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 140 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 141 | setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand); |
| 142 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
| 143 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 144 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 145 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 146 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 147 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 148 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
| 149 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 150 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 151 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 152 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 153 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 154 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 155 | // These should be promoted to a larger select which is supported. |
| 156 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 157 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 158 | |
| 159 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 160 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 161 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 162 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 163 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
| 164 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 165 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 166 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 167 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 168 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 169 | // X86 ret instruction may pop stack. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 171 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 172 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 174 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 175 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 176 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 177 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 178 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 179 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 180 | // X86 wants to expand memset / memcpy itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 181 | setOperationAction(ISD::MEMSET , MVT::Other, Custom); |
| 182 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 183 | |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 184 | // We don't have line number support yet. |
| 185 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 186 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 187 | // FIXME - use subtarget debug flags |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 188 | if (!Subtarget->isTargetDarwin()) |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 189 | setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand); |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 190 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 191 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 192 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| 193 | |
| 194 | // Use the default implementation. |
| 195 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 196 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 197 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 198 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 199 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 200 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 9601a86 | 2006-03-05 05:08:37 +0000 | [diff] [blame] | 202 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 203 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 204 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 205 | if (X86ScalarSSE) { |
| 206 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 207 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 208 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 209 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 210 | // Use ANDPD to simulate FABS. |
| 211 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 212 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 213 | |
| 214 | // Use XORP to simulate FNEG. |
| 215 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 216 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 217 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 218 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 219 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 220 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 221 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 222 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 223 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 224 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 225 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 226 | // Expand FP immediates into loads from the stack, except for the special |
| 227 | // cases we handle. |
| 228 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 229 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 230 | addLegalFPImmediate(+0.0); // xorps / xorpd |
| 231 | } else { |
| 232 | // Set up the FP register classes. |
| 233 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
Chris Lattner | 44d9b9b | 2006-01-29 06:44:22 +0000 | [diff] [blame] | 234 | |
| 235 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 236 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 237 | if (!UnsafeFPMath) { |
| 238 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 239 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 240 | } |
| 241 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 242 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 243 | addLegalFPImmediate(+0.0); // FLD0 |
| 244 | addLegalFPImmediate(+1.0); // FLD1 |
| 245 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 246 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 247 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 248 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 249 | // First set operation action for all vector types to expand. Then we |
| 250 | // will selectively turn on ones that can be effectively codegen'd. |
| 251 | for (unsigned VT = (unsigned)MVT::Vector + 1; |
| 252 | VT != (unsigned)MVT::LAST_VALUETYPE; VT++) { |
| 253 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 254 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
| 255 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
| 256 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 257 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); |
Chris Lattner | 9b3bd46 | 2006-03-21 20:51:05 +0000 | [diff] [blame] | 258 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 259 | setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 262 | if (Subtarget->hasMMX()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 263 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 264 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 265 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
| 266 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 267 | // FIXME: add MMX packed arithmetics |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 268 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand); |
| 269 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand); |
| 270 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 273 | if (Subtarget->hasSSE1()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 274 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 275 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 276 | setOperationAction(ISD::AND, MVT::v4f32, Legal); |
| 277 | setOperationAction(ISD::OR, MVT::v4f32, Legal); |
| 278 | setOperationAction(ISD::XOR, MVT::v4f32, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 279 | setOperationAction(ISD::ADD, MVT::v4f32, Legal); |
| 280 | setOperationAction(ISD::SUB, MVT::v4f32, Legal); |
| 281 | setOperationAction(ISD::MUL, MVT::v4f32, Legal); |
| 282 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 283 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 284 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 285 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 286 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 289 | if (Subtarget->hasSSE2()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 290 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
| 291 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 292 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 293 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 294 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 295 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 296 | setOperationAction(ISD::ADD, MVT::v2f64, Legal); |
| 297 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 298 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 299 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
| 300 | setOperationAction(ISD::SUB, MVT::v2f64, Legal); |
| 301 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 302 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 303 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
Evan Cheng | f998984 | 2006-04-13 05:10:25 +0000 | [diff] [blame] | 304 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 305 | setOperationAction(ISD::MUL, MVT::v2f64, Legal); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 306 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 307 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 308 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 309 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 310 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 311 | // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones. |
| 312 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 313 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 314 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| 315 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 316 | setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom); |
| 317 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom); |
| 318 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom); |
| 319 | } |
| 320 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 321 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 322 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 323 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 324 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| 325 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
| 326 | |
| 327 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
| 328 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 329 | setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote); |
| 330 | AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64); |
| 331 | setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote); |
| 332 | AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64); |
| 333 | setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote); |
| 334 | AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 335 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote); |
| 336 | AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 337 | setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote); |
| 338 | AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 339 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 340 | |
| 341 | // Custom lower v2i64 and v2f64 selects. |
| 342 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 343 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 344 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 345 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 348 | // We want to custom lower some of our intrinsics. |
| 349 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 350 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 351 | computeRegisterProperties(); |
| 352 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 353 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 354 | // be smaller when we are in optimizing for size mode. |
Evan Cheng | a03a5dc | 2006-02-14 08:38:30 +0000 | [diff] [blame] | 355 | maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores |
| 356 | maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores |
| 357 | maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 358 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 359 | } |
| 360 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
| 362 | // C Calling Convention implementation |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 365 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 366 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 367 | /// register for it. |
| 368 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 369 | TargetRegisterClass *RC) { |
| 370 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 371 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 372 | MF.addLiveIn(PReg, VReg); |
| 373 | return VReg; |
| 374 | } |
| 375 | |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 376 | /// HowToPassCCCArgument - Returns how an formal argument of the specified type |
| 377 | /// should be passed. If it is through stack, returns the size of the stack |
Evan Cheng | f9ff7c5 | 2006-05-26 18:25:43 +0000 | [diff] [blame] | 378 | /// slot; if it is through XMM register, returns the number of XMM registers |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 379 | /// are needed. |
| 380 | static void |
| 381 | HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs, |
| 382 | unsigned &ObjSize, unsigned &ObjXMMRegs) { |
Evan Cheng | cc1fc22 | 2006-05-25 23:31:23 +0000 | [diff] [blame] | 383 | NumXMMRegs = 0; |
| 384 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 385 | switch (ObjectVT) { |
| 386 | default: assert(0 && "Unhandled argument type!"); |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 387 | case MVT::i8: ObjSize = 1; break; |
| 388 | case MVT::i16: ObjSize = 2; break; |
| 389 | case MVT::i32: ObjSize = 4; break; |
| 390 | case MVT::i64: ObjSize = 8; break; |
| 391 | case MVT::f32: ObjSize = 4; break; |
| 392 | case MVT::f64: ObjSize = 8; break; |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 393 | case MVT::v16i8: |
| 394 | case MVT::v8i16: |
| 395 | case MVT::v4i32: |
| 396 | case MVT::v2i64: |
| 397 | case MVT::v4f32: |
| 398 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 399 | if (NumXMMRegs < 4) |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 400 | ObjXMMRegs = 1; |
| 401 | else |
| 402 | ObjSize = 16; |
| 403 | break; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 404 | } |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 407 | SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) { |
| 408 | unsigned NumArgs = Op.Val->getNumValues() - 1; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 409 | MachineFunction &MF = DAG.getMachineFunction(); |
| 410 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 411 | SDOperand Root = Op.getOperand(0); |
| 412 | std::vector<SDOperand> ArgValues; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 413 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 414 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 415 | // the stack frame looks like this: |
| 416 | // |
| 417 | // [ESP] -- return address |
| 418 | // [ESP + 4] -- first argument (leftmost lexically) |
Evan Cheng | f9d62dc | 2006-05-26 18:37:16 +0000 | [diff] [blame] | 419 | // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 420 | // ... |
| 421 | // |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 422 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 423 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 424 | static const unsigned XMMArgRegs[] = { |
| 425 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
| 426 | }; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 427 | for (unsigned i = 0; i < NumArgs; ++i) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 428 | MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); |
| 429 | unsigned ArgIncrement = 4; |
| 430 | unsigned ObjSize = 0; |
| 431 | unsigned ObjXMMRegs = 0; |
| 432 | HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs); |
Evan Cheng | 052fb51 | 2006-05-26 18:39:59 +0000 | [diff] [blame] | 433 | if (ObjSize > 4) |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 434 | ArgIncrement = ObjSize; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 435 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 436 | SDOperand ArgValue; |
| 437 | if (ObjXMMRegs) { |
| 438 | // Passed in a XMM register. |
| 439 | unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 440 | X86::VR128RegisterClass); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 441 | ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 442 | ArgValues.push_back(ArgValue); |
| 443 | NumXMMRegs += ObjXMMRegs; |
| 444 | } else { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 445 | // XMM arguments have to be aligned on 16-byte boundary. |
| 446 | if (ObjSize == 16) |
| 447 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 448 | // Create the frame index object for this incoming parameter... |
| 449 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 450 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 451 | ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, |
| 452 | DAG.getSrcValue(NULL)); |
| 453 | ArgValues.push_back(ArgValue); |
| 454 | ArgOffset += ArgIncrement; // Move on to the next argument... |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 455 | } |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 458 | ArgValues.push_back(Root); |
| 459 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 460 | // If the function takes variable number of arguments, make a frame index for |
| 461 | // 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] | 462 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 463 | if (isVarArg) |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 464 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
| 465 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 466 | BytesToPopOnReturn = 0; // Callee pops nothing. |
| 467 | BytesCallerReserves = ArgOffset; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 469 | // If this is a struct return on Darwin/X86, the callee pops the hidden struct |
| 470 | // pointer. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 471 | if (MF.getFunction()->getCallingConv() == CallingConv::CSRet && |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 472 | Subtarget->isTargetDarwin()) |
| 473 | BytesToPopOnReturn = 4; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 474 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 475 | // Return the new list of results. |
| 476 | std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(), |
| 477 | Op.Val->value_end()); |
| 478 | return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 481 | |
| 482 | SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) { |
| 483 | SDOperand Chain = Op.getOperand(0); |
| 484 | unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 485 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 486 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 487 | SDOperand Callee = Op.getOperand(4); |
| 488 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 489 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 490 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 491 | // Keep track of the number of XMM regs passed so far. |
| 492 | unsigned NumXMMRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 493 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 494 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 495 | }; |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 496 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 497 | // Count how many bytes are to be pushed on the stack. |
| 498 | unsigned NumBytes = 0; |
| 499 | for (unsigned i = 0; i != NumOps; ++i) { |
| 500 | SDOperand Arg = Op.getOperand(5+2*i); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 501 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 502 | switch (Arg.getValueType()) { |
| 503 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 504 | case MVT::i8: |
| 505 | case MVT::i16: |
| 506 | case MVT::i32: |
| 507 | case MVT::f32: |
| 508 | NumBytes += 4; |
| 509 | break; |
| 510 | case MVT::i64: |
| 511 | case MVT::f64: |
| 512 | NumBytes += 8; |
| 513 | break; |
| 514 | case MVT::v16i8: |
| 515 | case MVT::v8i16: |
| 516 | case MVT::v4i32: |
| 517 | case MVT::v2i64: |
| 518 | case MVT::v4f32: |
Evan Cheng | 25e71d1 | 2006-05-25 22:38:31 +0000 | [diff] [blame] | 519 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 520 | if (NumXMMRegs < 4) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 521 | ++NumXMMRegs; |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 522 | else { |
| 523 | // XMM arguments have to be aligned on 16-byte boundary. |
| 524 | NumBytes = ((NumBytes + 15) / 16) * 16; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 525 | NumBytes += 16; |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 526 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 527 | break; |
| 528 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 529 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 530 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 531 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 532 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 533 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 534 | unsigned ArgOffset = 0; |
| 535 | NumXMMRegs = 0; |
| 536 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 537 | std::vector<SDOperand> MemOpChains; |
| 538 | SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy()); |
| 539 | for (unsigned i = 0; i != NumOps; ++i) { |
| 540 | SDOperand Arg = Op.getOperand(5+2*i); |
| 541 | |
| 542 | switch (Arg.getValueType()) { |
| 543 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 544 | case MVT::i8: |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 545 | case MVT::i16: { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 546 | // Promote the integer to 32 bits. If the input type is signed use a |
| 547 | // sign extend, otherwise use a zero extend. |
| 548 | unsigned ExtOp = |
| 549 | dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ? |
| 550 | ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
| 551 | Arg = DAG.getNode(ExtOp, MVT::i32, Arg); |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 552 | } |
| 553 | // Fallthrough |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 554 | |
| 555 | case MVT::i32: |
| 556 | case MVT::f32: { |
| 557 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 558 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 559 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 560 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
| 561 | ArgOffset += 4; |
| 562 | break; |
| 563 | } |
| 564 | case MVT::i64: |
| 565 | case MVT::f64: { |
| 566 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 567 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 568 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 569 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
| 570 | ArgOffset += 8; |
| 571 | break; |
| 572 | } |
| 573 | case MVT::v16i8: |
| 574 | case MVT::v8i16: |
| 575 | case MVT::v4i32: |
| 576 | case MVT::v2i64: |
| 577 | case MVT::v4f32: |
Evan Cheng | 25e71d1 | 2006-05-25 22:38:31 +0000 | [diff] [blame] | 578 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 579 | if (NumXMMRegs < 4) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 580 | RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); |
| 581 | NumXMMRegs++; |
| 582 | } else { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 583 | // XMM arguments have to be aligned on 16-byte boundary. |
| 584 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 585 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 586 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 587 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 588 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
| 589 | ArgOffset += 16; |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 594 | if (!MemOpChains.empty()) |
| 595 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 596 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 597 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 598 | // and flag operands which copy the outgoing args into registers. |
| 599 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 600 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 601 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 602 | InFlag); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 603 | InFlag = Chain.getValue(1); |
| 604 | } |
| 605 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 606 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 607 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 608 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 609 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 610 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 611 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 612 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 613 | std::vector<MVT::ValueType> NodeTys; |
| 614 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 615 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 616 | std::vector<SDOperand> Ops; |
| 617 | Ops.push_back(Chain); |
| 618 | Ops.push_back(Callee); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 619 | if (InFlag.Val) |
| 620 | Ops.push_back(InFlag); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 621 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 622 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 623 | NodeTys, Ops); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 624 | InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 625 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 626 | // Create the CALLSEQ_END node. |
| 627 | unsigned NumBytesForCalleeToPush = 0; |
| 628 | |
| 629 | // If this is is a call to a struct-return function on Darwin/X86, the callee |
| 630 | // pops the hidden struct pointer, so we have to push it back. |
| 631 | if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin()) |
| 632 | NumBytesForCalleeToPush = 4; |
| 633 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 634 | NodeTys.clear(); |
| 635 | NodeTys.push_back(MVT::Other); // Returns a chain |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 636 | if (RetVT != MVT::Other) |
| 637 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 638 | Ops.clear(); |
| 639 | Ops.push_back(Chain); |
| 640 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 641 | Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 642 | Ops.push_back(InFlag); |
| 643 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 644 | if (RetVT != MVT::Other) |
| 645 | InFlag = Chain.getValue(1); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 646 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 647 | std::vector<SDOperand> ResultVals; |
| 648 | NodeTys.clear(); |
| 649 | switch (RetVT) { |
| 650 | default: assert(0 && "Unknown value type to return!"); |
| 651 | case MVT::Other: break; |
| 652 | case MVT::i8: |
| 653 | Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1); |
| 654 | ResultVals.push_back(Chain.getValue(0)); |
| 655 | NodeTys.push_back(MVT::i8); |
| 656 | break; |
| 657 | case MVT::i16: |
| 658 | Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1); |
| 659 | ResultVals.push_back(Chain.getValue(0)); |
| 660 | NodeTys.push_back(MVT::i16); |
| 661 | break; |
| 662 | case MVT::i32: |
| 663 | if (Op.Val->getValueType(1) == MVT::i32) { |
| 664 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 665 | ResultVals.push_back(Chain.getValue(0)); |
| 666 | Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32, |
| 667 | Chain.getValue(2)).getValue(1); |
| 668 | ResultVals.push_back(Chain.getValue(0)); |
| 669 | NodeTys.push_back(MVT::i32); |
| 670 | } else { |
| 671 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 672 | ResultVals.push_back(Chain.getValue(0)); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 673 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 674 | NodeTys.push_back(MVT::i32); |
| 675 | break; |
| 676 | case MVT::v16i8: |
| 677 | case MVT::v8i16: |
| 678 | case MVT::v4i32: |
| 679 | case MVT::v2i64: |
| 680 | case MVT::v4f32: |
| 681 | case MVT::v2f64: |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 682 | Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1); |
| 683 | ResultVals.push_back(Chain.getValue(0)); |
| 684 | NodeTys.push_back(RetVT); |
| 685 | break; |
| 686 | case MVT::f32: |
| 687 | case MVT::f64: { |
| 688 | std::vector<MVT::ValueType> Tys; |
| 689 | Tys.push_back(MVT::f64); |
| 690 | Tys.push_back(MVT::Other); |
| 691 | Tys.push_back(MVT::Flag); |
| 692 | std::vector<SDOperand> Ops; |
| 693 | Ops.push_back(Chain); |
| 694 | Ops.push_back(InFlag); |
| 695 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops); |
| 696 | Chain = RetVal.getValue(1); |
| 697 | InFlag = RetVal.getValue(2); |
| 698 | if (X86ScalarSSE) { |
| 699 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 700 | // shouldn't be necessary except that RFP cannot be live across |
| 701 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 702 | MachineFunction &MF = DAG.getMachineFunction(); |
| 703 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 704 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 705 | Tys.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 706 | Tys.push_back(MVT::Other); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 707 | Ops.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 708 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 709 | Ops.push_back(RetVal); |
| 710 | Ops.push_back(StackSlot); |
| 711 | Ops.push_back(DAG.getValueType(RetVT)); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 712 | Ops.push_back(InFlag); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 713 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 714 | RetVal = DAG.getLoad(RetVT, Chain, StackSlot, |
| 715 | DAG.getSrcValue(NULL)); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 716 | Chain = RetVal.getValue(1); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 717 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 718 | |
| 719 | if (RetVT == MVT::f32 && !X86ScalarSSE) |
| 720 | // FIXME: we would really like to remember that this FP_ROUND |
| 721 | // operation is okay to eliminate if we allow excess FP precision. |
| 722 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 723 | ResultVals.push_back(RetVal); |
| 724 | NodeTys.push_back(RetVT); |
| 725 | break; |
| 726 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 727 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 728 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 729 | // If the function returns void, just return the chain. |
| 730 | if (ResultVals.empty()) |
| 731 | return Chain; |
| 732 | |
| 733 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 734 | NodeTys.push_back(MVT::Other); |
| 735 | ResultVals.push_back(Chain); |
| 736 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals); |
| 737 | return Res.getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 740 | //===----------------------------------------------------------------------===// |
| 741 | // Fast Calling Convention implementation |
| 742 | //===----------------------------------------------------------------------===// |
| 743 | // |
| 744 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 745 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 746 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 747 | // tail calls), and has the same return value conventions as C calling convs. |
| 748 | // |
| 749 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 750 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 751 | // reasons. |
| 752 | // |
| 753 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 754 | // (when we have a global fp allocator) and do other tricks. |
| 755 | // |
| 756 | |
Chris Lattner | 89fad2c | 2006-03-17 17:27:47 +0000 | [diff] [blame] | 757 | // FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments |
| 758 | // to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and |
| 759 | // EDX". Anything more is illegal. |
| 760 | // |
| 761 | // FIXME: The linscan register allocator currently has problem with |
Chris Lattner | 9d5da1d | 2006-03-24 07:12:19 +0000 | [diff] [blame] | 762 | // coalescing. At the time of this writing, whenever it decides to coalesce |
Chris Lattner | 89fad2c | 2006-03-17 17:27:47 +0000 | [diff] [blame] | 763 | // a physreg with a virtreg, this increases the size of the physreg's live |
| 764 | // range, and the live range cannot ever be reduced. This causes problems if |
Chris Lattner | 9d5da1d | 2006-03-24 07:12:19 +0000 | [diff] [blame] | 765 | // too many physregs are coaleced with virtregs, which can cause the register |
Chris Lattner | 89fad2c | 2006-03-17 17:27:47 +0000 | [diff] [blame] | 766 | // allocator to wedge itself. |
| 767 | // |
| 768 | // This code triggers this problem more often if we pass args in registers, |
| 769 | // so disable it until this is fixed. |
| 770 | // |
| 771 | // NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings |
| 772 | // about code being dead. |
| 773 | // |
| 774 | static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0; |
Chris Lattner | 1c636e9 | 2006-03-17 05:10:20 +0000 | [diff] [blame] | 775 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 776 | |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 777 | /// HowToPassFastCCArgument - Returns how an formal argument of the specified |
| 778 | /// type should be passed. If it is through stack, returns the size of the stack |
Evan Cheng | f9ff7c5 | 2006-05-26 18:25:43 +0000 | [diff] [blame] | 779 | /// slot; if it is through integer or XMM register, returns the number of |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 780 | /// integer or XMM registers are needed. |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 781 | static void |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 782 | HowToPassFastCCArgument(MVT::ValueType ObjectVT, |
| 783 | unsigned NumIntRegs, unsigned NumXMMRegs, |
| 784 | unsigned &ObjSize, unsigned &ObjIntRegs, |
| 785 | unsigned &ObjXMMRegs) { |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 786 | ObjSize = 0; |
| 787 | NumIntRegs = 0; |
Evan Cheng | cc1fc22 | 2006-05-25 23:31:23 +0000 | [diff] [blame] | 788 | NumXMMRegs = 0; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 789 | |
| 790 | switch (ObjectVT) { |
| 791 | default: assert(0 && "Unhandled argument type!"); |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 792 | case MVT::i8: |
| 793 | if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 794 | ObjIntRegs = 1; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 795 | else |
| 796 | ObjSize = 1; |
| 797 | break; |
| 798 | case MVT::i16: |
| 799 | if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 800 | ObjIntRegs = 1; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 801 | else |
| 802 | ObjSize = 2; |
| 803 | break; |
| 804 | case MVT::i32: |
| 805 | if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 806 | ObjIntRegs = 1; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 807 | else |
| 808 | ObjSize = 4; |
| 809 | break; |
| 810 | case MVT::i64: |
| 811 | if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) { |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 812 | ObjIntRegs = 2; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 813 | } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) { |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 814 | ObjIntRegs = 1; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 815 | ObjSize = 4; |
| 816 | } else |
| 817 | ObjSize = 8; |
| 818 | case MVT::f32: |
| 819 | ObjSize = 4; |
| 820 | break; |
| 821 | case MVT::f64: |
| 822 | ObjSize = 8; |
| 823 | break; |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 824 | case MVT::v16i8: |
| 825 | case MVT::v8i16: |
| 826 | case MVT::v4i32: |
| 827 | case MVT::v2i64: |
| 828 | case MVT::v4f32: |
| 829 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 830 | if (NumXMMRegs < 4) |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 831 | ObjXMMRegs = 1; |
| 832 | else |
| 833 | ObjSize = 16; |
| 834 | break; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 838 | SDOperand |
| 839 | X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) { |
| 840 | unsigned NumArgs = Op.Val->getNumValues()-1; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 841 | MachineFunction &MF = DAG.getMachineFunction(); |
| 842 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 843 | SDOperand Root = Op.getOperand(0); |
| 844 | std::vector<SDOperand> ArgValues; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 845 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 846 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 847 | // frame looks like this: |
| 848 | // |
| 849 | // [ESP] -- return address |
| 850 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
Evan Cheng | f9d62dc | 2006-05-26 18:37:16 +0000 | [diff] [blame] | 851 | // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 852 | // ... |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 853 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 854 | |
| 855 | // Keep track of the number of integer regs passed so far. This can be either |
| 856 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 857 | // used). |
| 858 | unsigned NumIntRegs = 0; |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 859 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 860 | |
| 861 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 862 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 863 | }; |
Chris Lattner | 1c636e9 | 2006-03-17 05:10:20 +0000 | [diff] [blame] | 864 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 865 | for (unsigned i = 0; i < NumArgs; ++i) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 866 | MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); |
| 867 | unsigned ArgIncrement = 4; |
| 868 | unsigned ObjSize = 0; |
| 869 | unsigned ObjIntRegs = 0; |
| 870 | unsigned ObjXMMRegs = 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 871 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 872 | HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs, |
| 873 | ObjSize, ObjIntRegs, ObjXMMRegs); |
Evan Cheng | 052fb51 | 2006-05-26 18:39:59 +0000 | [diff] [blame] | 874 | if (ObjSize > 4) |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 875 | ArgIncrement = ObjSize; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 876 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 877 | unsigned Reg; |
| 878 | SDOperand ArgValue; |
| 879 | if (ObjIntRegs || ObjXMMRegs) { |
| 880 | switch (ObjectVT) { |
| 881 | default: assert(0 && "Unhandled argument type!"); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 882 | case MVT::i8: |
| 883 | Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL, |
| 884 | X86::GR8RegisterClass); |
| 885 | ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8); |
| 886 | break; |
| 887 | case MVT::i16: |
| 888 | Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX, |
| 889 | X86::GR16RegisterClass); |
| 890 | ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16); |
| 891 | break; |
| 892 | case MVT::i32: |
| 893 | Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX, |
| 894 | X86::GR32RegisterClass); |
| 895 | ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32); |
| 896 | break; |
| 897 | case MVT::i64: |
| 898 | Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX, |
| 899 | X86::GR32RegisterClass); |
| 900 | ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32); |
| 901 | if (ObjIntRegs == 2) { |
| 902 | Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass); |
| 903 | SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32); |
| 904 | ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2); |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 905 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 906 | break; |
| 907 | case MVT::v16i8: |
| 908 | case MVT::v8i16: |
| 909 | case MVT::v4i32: |
| 910 | case MVT::v2i64: |
| 911 | case MVT::v4f32: |
| 912 | case MVT::v2f64: |
| 913 | Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass); |
| 914 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 915 | break; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 916 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 917 | NumIntRegs += ObjIntRegs; |
| 918 | NumXMMRegs += ObjXMMRegs; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 919 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 920 | |
| 921 | if (ObjSize) { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 922 | // XMM arguments have to be aligned on 16-byte boundary. |
| 923 | if (ObjSize == 16) |
| 924 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 925 | // Create the SelectionDAG nodes corresponding to a load from this |
| 926 | // parameter. |
| 927 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 928 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 929 | if (ObjectVT == MVT::i64 && ObjIntRegs) { |
| 930 | SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, |
| 931 | DAG.getSrcValue(NULL)); |
| 932 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2); |
| 933 | } else |
| 934 | ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, |
| 935 | DAG.getSrcValue(NULL)); |
| 936 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 937 | } |
| 938 | |
| 939 | ArgValues.push_back(ArgValue); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 942 | ArgValues.push_back(Root); |
| 943 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 944 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 945 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 946 | if ((ArgOffset & 7) == 0) |
| 947 | ArgOffset += 4; |
| 948 | |
| 949 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 950 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 951 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
| 952 | BytesCallerReserves = 0; |
| 953 | |
| 954 | // Finally, inform the code generator which regs we return values in. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 955 | switch (getValueType(MF.getFunction()->getReturnType())) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 956 | default: assert(0 && "Unknown type!"); |
| 957 | case MVT::isVoid: break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 958 | case MVT::i8: |
| 959 | case MVT::i16: |
| 960 | case MVT::i32: |
| 961 | MF.addLiveOut(X86::EAX); |
| 962 | break; |
| 963 | case MVT::i64: |
| 964 | MF.addLiveOut(X86::EAX); |
| 965 | MF.addLiveOut(X86::EDX); |
| 966 | break; |
| 967 | case MVT::f32: |
| 968 | case MVT::f64: |
| 969 | MF.addLiveOut(X86::ST0); |
| 970 | break; |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 971 | case MVT::v16i8: |
| 972 | case MVT::v8i16: |
| 973 | case MVT::v4i32: |
| 974 | case MVT::v2i64: |
| 975 | case MVT::v4f32: |
| 976 | case MVT::v2f64: |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 977 | MF.addLiveOut(X86::XMM0); |
| 978 | break; |
| 979 | } |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 980 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 981 | // Return the new list of results. |
| 982 | std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(), |
| 983 | Op.Val->value_end()); |
| 984 | return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 987 | SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) { |
| 988 | SDOperand Chain = Op.getOperand(0); |
| 989 | unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 990 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 991 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 992 | SDOperand Callee = Op.getOperand(4); |
| 993 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 994 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 995 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 996 | // Count how many bytes are to be pushed on the stack. |
| 997 | unsigned NumBytes = 0; |
| 998 | |
| 999 | // Keep track of the number of integer regs passed so far. This can be either |
| 1000 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 1001 | // used). |
| 1002 | unsigned NumIntRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1003 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1004 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1005 | static const unsigned GPRArgRegs[][2] = { |
| 1006 | { X86::AL, X86::DL }, |
| 1007 | { X86::AX, X86::DX }, |
| 1008 | { X86::EAX, X86::EDX } |
| 1009 | }; |
| 1010 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 1011 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1012 | }; |
| 1013 | |
| 1014 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1015 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1016 | |
| 1017 | switch (Arg.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1018 | default: assert(0 && "Unknown value type!"); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1019 | case MVT::i8: |
| 1020 | case MVT::i16: |
| 1021 | case MVT::i32: |
Chris Lattner | 1c636e9 | 2006-03-17 05:10:20 +0000 | [diff] [blame] | 1022 | if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1023 | ++NumIntRegs; |
| 1024 | break; |
| 1025 | } |
Evan Cheng | 25e71d1 | 2006-05-25 22:38:31 +0000 | [diff] [blame] | 1026 | // Fall through |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1027 | case MVT::f32: |
| 1028 | NumBytes += 4; |
| 1029 | break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1030 | case MVT::f64: |
| 1031 | NumBytes += 8; |
| 1032 | break; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1033 | case MVT::v16i8: |
| 1034 | case MVT::v8i16: |
| 1035 | case MVT::v4i32: |
| 1036 | case MVT::v2i64: |
| 1037 | case MVT::v4f32: |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1038 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 1039 | if (NumXMMRegs < 4) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1040 | NumXMMRegs++; |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 1041 | else { |
| 1042 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1043 | NumBytes = ((NumBytes + 15) / 16) * 16; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1044 | NumBytes += 16; |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 1045 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1046 | break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1047 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1048 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1051 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1052 | if ((NumBytes & 7) == 0) |
| 1053 | NumBytes += 4; |
| 1054 | |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 1055 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 1058 | unsigned ArgOffset = 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1059 | NumIntRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1060 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 1061 | std::vector<SDOperand> MemOpChains; |
| 1062 | SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy()); |
| 1063 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1064 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1065 | |
| 1066 | switch (Arg.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1067 | default: assert(0 && "Unexpected ValueType for argument!"); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1068 | case MVT::i8: |
| 1069 | case MVT::i16: |
| 1070 | case MVT::i32: |
Chris Lattner | 1c636e9 | 2006-03-17 05:10:20 +0000 | [diff] [blame] | 1071 | if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1072 | RegsToPass.push_back( |
| 1073 | std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs], |
| 1074 | Arg)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1075 | ++NumIntRegs; |
| 1076 | break; |
| 1077 | } |
| 1078 | // Fall through |
| 1079 | case MVT::f32: { |
| 1080 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1081 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1082 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1083 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1084 | ArgOffset += 4; |
| 1085 | break; |
| 1086 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1087 | case MVT::f64: { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1088 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1089 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1090 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1091 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1092 | ArgOffset += 8; |
| 1093 | break; |
| 1094 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1095 | case MVT::v16i8: |
| 1096 | case MVT::v8i16: |
| 1097 | case MVT::v4i32: |
| 1098 | case MVT::v2i64: |
| 1099 | case MVT::v4f32: |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1100 | case MVT::v2f64: |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 1101 | if (NumXMMRegs < 4) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1102 | RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); |
| 1103 | NumXMMRegs++; |
| 1104 | } else { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame^] | 1105 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1106 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1107 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 1108 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1109 | MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1110 | Arg, PtrOff, DAG.getSrcValue(NULL))); |
| 1111 | ArgOffset += 16; |
| 1112 | } |
| 1113 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1114 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1115 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1116 | if (!MemOpChains.empty()) |
| 1117 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1118 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1119 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1120 | // and flag operands which copy the outgoing args into registers. |
| 1121 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1122 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1123 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1124 | InFlag); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1125 | InFlag = Chain.getValue(1); |
| 1126 | } |
| 1127 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1128 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1129 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 1130 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 1131 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 1132 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 1133 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 1134 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1135 | std::vector<MVT::ValueType> NodeTys; |
| 1136 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 1137 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 1138 | std::vector<SDOperand> Ops; |
| 1139 | Ops.push_back(Chain); |
| 1140 | Ops.push_back(Callee); |
| 1141 | if (InFlag.Val) |
| 1142 | Ops.push_back(InFlag); |
| 1143 | |
| 1144 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 1145 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 1146 | NodeTys, Ops); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1147 | InFlag = Chain.getValue(1); |
| 1148 | |
| 1149 | NodeTys.clear(); |
| 1150 | NodeTys.push_back(MVT::Other); // Returns a chain |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1151 | if (RetVT != MVT::Other) |
| 1152 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1153 | Ops.clear(); |
| 1154 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1155 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 1156 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1157 | Ops.push_back(InFlag); |
| 1158 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1159 | if (RetVT != MVT::Other) |
| 1160 | InFlag = Chain.getValue(1); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1161 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1162 | std::vector<SDOperand> ResultVals; |
| 1163 | NodeTys.clear(); |
| 1164 | switch (RetVT) { |
| 1165 | default: assert(0 && "Unknown value type to return!"); |
| 1166 | case MVT::Other: break; |
| 1167 | case MVT::i8: |
| 1168 | Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1); |
| 1169 | ResultVals.push_back(Chain.getValue(0)); |
| 1170 | NodeTys.push_back(MVT::i8); |
| 1171 | break; |
| 1172 | case MVT::i16: |
| 1173 | Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1); |
| 1174 | ResultVals.push_back(Chain.getValue(0)); |
| 1175 | NodeTys.push_back(MVT::i16); |
| 1176 | break; |
| 1177 | case MVT::i32: |
| 1178 | if (Op.Val->getValueType(1) == MVT::i32) { |
| 1179 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 1180 | ResultVals.push_back(Chain.getValue(0)); |
| 1181 | Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32, |
| 1182 | Chain.getValue(2)).getValue(1); |
| 1183 | ResultVals.push_back(Chain.getValue(0)); |
| 1184 | NodeTys.push_back(MVT::i32); |
| 1185 | } else { |
| 1186 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 1187 | ResultVals.push_back(Chain.getValue(0)); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1188 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1189 | NodeTys.push_back(MVT::i32); |
| 1190 | break; |
| 1191 | case MVT::v16i8: |
| 1192 | case MVT::v8i16: |
| 1193 | case MVT::v4i32: |
| 1194 | case MVT::v2i64: |
| 1195 | case MVT::v4f32: |
| 1196 | case MVT::v2f64: |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1197 | Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1); |
| 1198 | ResultVals.push_back(Chain.getValue(0)); |
| 1199 | NodeTys.push_back(RetVT); |
| 1200 | break; |
| 1201 | case MVT::f32: |
| 1202 | case MVT::f64: { |
| 1203 | std::vector<MVT::ValueType> Tys; |
| 1204 | Tys.push_back(MVT::f64); |
| 1205 | Tys.push_back(MVT::Other); |
| 1206 | Tys.push_back(MVT::Flag); |
| 1207 | std::vector<SDOperand> Ops; |
| 1208 | Ops.push_back(Chain); |
| 1209 | Ops.push_back(InFlag); |
| 1210 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops); |
| 1211 | Chain = RetVal.getValue(1); |
| 1212 | InFlag = RetVal.getValue(2); |
| 1213 | if (X86ScalarSSE) { |
| 1214 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 1215 | // shouldn't be necessary except that RFP cannot be live across |
| 1216 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 1217 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1218 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 1219 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1220 | Tys.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1221 | Tys.push_back(MVT::Other); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1222 | Ops.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1223 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1224 | Ops.push_back(RetVal); |
| 1225 | Ops.push_back(StackSlot); |
| 1226 | Ops.push_back(DAG.getValueType(RetVT)); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1227 | Ops.push_back(InFlag); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1228 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 1229 | RetVal = DAG.getLoad(RetVT, Chain, StackSlot, |
| 1230 | DAG.getSrcValue(NULL)); |
| 1231 | Chain = RetVal.getValue(1); |
| 1232 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1233 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1234 | if (RetVT == MVT::f32 && !X86ScalarSSE) |
| 1235 | // FIXME: we would really like to remember that this FP_ROUND |
| 1236 | // operation is okay to eliminate if we allow excess FP precision. |
| 1237 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 1238 | ResultVals.push_back(RetVal); |
| 1239 | NodeTys.push_back(RetVT); |
| 1240 | break; |
| 1241 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1242 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1243 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1244 | |
| 1245 | // If the function returns void, just return the chain. |
| 1246 | if (ResultVals.empty()) |
| 1247 | return Chain; |
| 1248 | |
| 1249 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 1250 | NodeTys.push_back(MVT::Other); |
| 1251 | ResultVals.push_back(Chain); |
| 1252 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals); |
| 1253 | return Res.getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
| 1257 | if (ReturnAddrIndex == 0) { |
| 1258 | // Set up a frame object for the return address. |
| 1259 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1260 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
| 1261 | } |
| 1262 | |
| 1263 | return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32); |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | std::pair<SDOperand, SDOperand> X86TargetLowering:: |
| 1269 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 1270 | SelectionDAG &DAG) { |
| 1271 | SDOperand Result; |
| 1272 | if (Depth) // Depths > 0 not supported yet! |
| 1273 | Result = DAG.getConstant(0, getPointerTy()); |
| 1274 | else { |
| 1275 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 1276 | if (!isFrameAddress) |
| 1277 | // Just load the return address |
| 1278 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, |
| 1279 | DAG.getSrcValue(NULL)); |
| 1280 | else |
| 1281 | Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI, |
| 1282 | DAG.getConstant(4, MVT::i32)); |
| 1283 | } |
| 1284 | return std::make_pair(Result, Chain); |
| 1285 | } |
| 1286 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1287 | /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode |
| 1288 | /// which corresponds to the condition code. |
| 1289 | static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) { |
| 1290 | switch (X86CC) { |
| 1291 | default: assert(0 && "Unknown X86 conditional code!"); |
| 1292 | case X86ISD::COND_A: return X86::JA; |
| 1293 | case X86ISD::COND_AE: return X86::JAE; |
| 1294 | case X86ISD::COND_B: return X86::JB; |
| 1295 | case X86ISD::COND_BE: return X86::JBE; |
| 1296 | case X86ISD::COND_E: return X86::JE; |
| 1297 | case X86ISD::COND_G: return X86::JG; |
| 1298 | case X86ISD::COND_GE: return X86::JGE; |
| 1299 | case X86ISD::COND_L: return X86::JL; |
| 1300 | case X86ISD::COND_LE: return X86::JLE; |
| 1301 | case X86ISD::COND_NE: return X86::JNE; |
| 1302 | case X86ISD::COND_NO: return X86::JNO; |
| 1303 | case X86ISD::COND_NP: return X86::JNP; |
| 1304 | case X86ISD::COND_NS: return X86::JNS; |
| 1305 | case X86ISD::COND_O: return X86::JO; |
| 1306 | case X86ISD::COND_P: return X86::JP; |
| 1307 | case X86ISD::COND_S: return X86::JS; |
| 1308 | } |
| 1309 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1310 | |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1311 | /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 1312 | /// specific condition code. It returns a false if it cannot do a direct |
| 1313 | /// translation. X86CC is the translated CondCode. Flip is set to true if the |
| 1314 | /// the order of comparison operands should be flipped. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1315 | static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
| 1316 | unsigned &X86CC, bool &Flip) { |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1317 | Flip = false; |
| 1318 | X86CC = X86ISD::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1319 | if (!isFP) { |
| 1320 | switch (SetCCOpcode) { |
| 1321 | default: break; |
| 1322 | case ISD::SETEQ: X86CC = X86ISD::COND_E; break; |
| 1323 | case ISD::SETGT: X86CC = X86ISD::COND_G; break; |
| 1324 | case ISD::SETGE: X86CC = X86ISD::COND_GE; break; |
| 1325 | case ISD::SETLT: X86CC = X86ISD::COND_L; break; |
| 1326 | case ISD::SETLE: X86CC = X86ISD::COND_LE; break; |
| 1327 | case ISD::SETNE: X86CC = X86ISD::COND_NE; break; |
| 1328 | case ISD::SETULT: X86CC = X86ISD::COND_B; break; |
| 1329 | case ISD::SETUGT: X86CC = X86ISD::COND_A; break; |
| 1330 | case ISD::SETULE: X86CC = X86ISD::COND_BE; break; |
| 1331 | case ISD::SETUGE: X86CC = X86ISD::COND_AE; break; |
| 1332 | } |
| 1333 | } else { |
| 1334 | // On a floating point condition, the flags are set as follows: |
| 1335 | // ZF PF CF op |
| 1336 | // 0 | 0 | 0 | X > Y |
| 1337 | // 0 | 0 | 1 | X < Y |
| 1338 | // 1 | 0 | 0 | X == Y |
| 1339 | // 1 | 1 | 1 | unordered |
| 1340 | switch (SetCCOpcode) { |
| 1341 | default: break; |
| 1342 | case ISD::SETUEQ: |
| 1343 | case ISD::SETEQ: X86CC = X86ISD::COND_E; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1344 | case ISD::SETOLT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1345 | case ISD::SETOGT: |
| 1346 | case ISD::SETGT: X86CC = X86ISD::COND_A; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1347 | case ISD::SETOLE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1348 | case ISD::SETOGE: |
| 1349 | case ISD::SETGE: X86CC = X86ISD::COND_AE; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1350 | case ISD::SETUGT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1351 | case ISD::SETULT: |
| 1352 | case ISD::SETLT: X86CC = X86ISD::COND_B; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 1353 | case ISD::SETUGE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1354 | case ISD::SETULE: |
| 1355 | case ISD::SETLE: X86CC = X86ISD::COND_BE; break; |
| 1356 | case ISD::SETONE: |
| 1357 | case ISD::SETNE: X86CC = X86ISD::COND_NE; break; |
| 1358 | case ISD::SETUO: X86CC = X86ISD::COND_P; break; |
| 1359 | case ISD::SETO: X86CC = X86ISD::COND_NP; break; |
| 1360 | } |
| 1361 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1362 | |
| 1363 | return X86CC != X86ISD::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1366 | static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC, |
| 1367 | bool &Flip) { |
| 1368 | return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip); |
| 1369 | } |
| 1370 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1371 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 1372 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1373 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1374 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1375 | switch (X86CC) { |
| 1376 | default: |
| 1377 | return false; |
| 1378 | case X86ISD::COND_B: |
| 1379 | case X86ISD::COND_BE: |
| 1380 | case X86ISD::COND_E: |
| 1381 | case X86ISD::COND_P: |
| 1382 | case X86ISD::COND_A: |
| 1383 | case X86ISD::COND_AE: |
| 1384 | case X86ISD::COND_NE: |
| 1385 | case X86ISD::COND_NP: |
| 1386 | return true; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1390 | MachineBasicBlock * |
| 1391 | X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1392 | MachineBasicBlock *BB) { |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1393 | switch (MI->getOpcode()) { |
| 1394 | default: assert(false && "Unexpected instr type to insert"); |
| 1395 | case X86::CMOV_FR32: |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 1396 | case X86::CMOV_FR64: |
| 1397 | case X86::CMOV_V4F32: |
| 1398 | case X86::CMOV_V2F64: |
| 1399 | case X86::CMOV_V2I64: { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 1400 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 1401 | // diamond control-flow pattern. The incoming instruction knows the |
| 1402 | // destination vreg to set, the condition code register to branch on, the |
| 1403 | // true/false values to select between, and a branch opcode to use. |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1404 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1405 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1406 | ++It; |
| 1407 | |
| 1408 | // thisMBB: |
| 1409 | // ... |
| 1410 | // TrueVal = ... |
| 1411 | // cmpTY ccX, r1, r2 |
| 1412 | // bCC copy1MBB |
| 1413 | // fallthrough --> copy0MBB |
| 1414 | MachineBasicBlock *thisMBB = BB; |
| 1415 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1416 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1417 | unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue()); |
| 1418 | BuildMI(BB, Opc, 1).addMBB(sinkMBB); |
| 1419 | MachineFunction *F = BB->getParent(); |
| 1420 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1421 | F->getBasicBlockList().insert(It, sinkMBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1422 | // Update machine-CFG edges by first adding all successors of the current |
| 1423 | // block to the new block which will contain the Phi node for the select. |
| 1424 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
| 1425 | e = BB->succ_end(); i != e; ++i) |
| 1426 | sinkMBB->addSuccessor(*i); |
| 1427 | // Next, remove all successors of the current block, and add the true |
| 1428 | // and fallthrough blocks as its successors. |
| 1429 | while(!BB->succ_empty()) |
| 1430 | BB->removeSuccessor(BB->succ_begin()); |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1431 | BB->addSuccessor(copy0MBB); |
| 1432 | BB->addSuccessor(sinkMBB); |
| 1433 | |
| 1434 | // copy0MBB: |
| 1435 | // %FalseValue = ... |
| 1436 | // # fallthrough to sinkMBB |
| 1437 | BB = copy0MBB; |
| 1438 | |
| 1439 | // Update machine-CFG edges |
| 1440 | BB->addSuccessor(sinkMBB); |
| 1441 | |
| 1442 | // sinkMBB: |
| 1443 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1444 | // ... |
| 1445 | BB = sinkMBB; |
| 1446 | BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg()) |
| 1447 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 1448 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1449 | |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1450 | delete MI; // The pseudo instruction is gone now. |
| 1451 | return BB; |
| 1452 | } |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1453 | |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1454 | case X86::FP_TO_INT16_IN_MEM: |
| 1455 | case X86::FP_TO_INT32_IN_MEM: |
| 1456 | case X86::FP_TO_INT64_IN_MEM: { |
| 1457 | // Change the floating point control register to use "round towards zero" |
| 1458 | // mode when truncating to an integer value. |
| 1459 | MachineFunction *F = BB->getParent(); |
| 1460 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 1461 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 1462 | |
| 1463 | // Load the old value of the high byte of the control word... |
| 1464 | unsigned OldCW = |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 1465 | F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass); |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1466 | addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx); |
| 1467 | |
| 1468 | // Set the high part to be round to zero... |
| 1469 | addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F); |
| 1470 | |
| 1471 | // Reload the modified control word now... |
| 1472 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1473 | |
| 1474 | // Restore the memory image of control word to original value |
| 1475 | addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW); |
| 1476 | |
| 1477 | // Get the X86 opcode to use. |
| 1478 | unsigned Opc; |
| 1479 | switch (MI->getOpcode()) { |
Chris Lattner | 6b2469c | 2006-01-28 10:34:47 +0000 | [diff] [blame] | 1480 | default: assert(0 && "illegal opcode!"); |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1481 | case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break; |
| 1482 | case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break; |
| 1483 | case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break; |
| 1484 | } |
| 1485 | |
| 1486 | X86AddressMode AM; |
| 1487 | MachineOperand &Op = MI->getOperand(0); |
| 1488 | if (Op.isRegister()) { |
| 1489 | AM.BaseType = X86AddressMode::RegBase; |
| 1490 | AM.Base.Reg = Op.getReg(); |
| 1491 | } else { |
| 1492 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 1493 | AM.Base.FrameIndex = Op.getFrameIndex(); |
| 1494 | } |
| 1495 | Op = MI->getOperand(1); |
| 1496 | if (Op.isImmediate()) |
| 1497 | AM.Scale = Op.getImmedValue(); |
| 1498 | Op = MI->getOperand(2); |
| 1499 | if (Op.isImmediate()) |
| 1500 | AM.IndexReg = Op.getImmedValue(); |
| 1501 | Op = MI->getOperand(3); |
| 1502 | if (Op.isGlobalAddress()) { |
| 1503 | AM.GV = Op.getGlobal(); |
| 1504 | } else { |
| 1505 | AM.Disp = Op.getImmedValue(); |
| 1506 | } |
| 1507 | addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg()); |
| 1508 | |
| 1509 | // Reload the original control word now. |
| 1510 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1511 | |
| 1512 | delete MI; // The pseudo instruction is gone now. |
| 1513 | return BB; |
| 1514 | } |
| 1515 | } |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | //===----------------------------------------------------------------------===// |
| 1520 | // X86 Custom Lowering Hooks |
| 1521 | //===----------------------------------------------------------------------===// |
| 1522 | |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 1523 | /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra |
| 1524 | /// load. For Darwin, external and weak symbols are indirect, loading the value |
| 1525 | /// at address GV rather then the value of GV itself. This means that the |
| 1526 | /// GlobalAddress must be in the base or index register of the address, not the |
| 1527 | /// GV offset field. |
| 1528 | static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) { |
| 1529 | return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
| 1530 | (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())); |
| 1531 | } |
| 1532 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1533 | /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1534 | /// 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] | 1535 | static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) { |
| 1536 | if (Op.getOpcode() == ISD::UNDEF) |
| 1537 | return true; |
| 1538 | |
| 1539 | unsigned Val = cast<ConstantSDNode>(Op)->getValue(); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1540 | return (Val >= Low && Val < Hi); |
| 1541 | } |
| 1542 | |
| 1543 | /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return |
| 1544 | /// true if Op is undef or if its value equal to the specified value. |
| 1545 | static bool isUndefOrEqual(SDOperand Op, unsigned Val) { |
| 1546 | if (Op.getOpcode() == ISD::UNDEF) |
| 1547 | return true; |
| 1548 | return cast<ConstantSDNode>(Op)->getValue() == Val; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 1551 | /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1552 | /// specifies a shuffle of elements that is suitable for input to PSHUFD. |
| 1553 | bool X86::isPSHUFDMask(SDNode *N) { |
| 1554 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1555 | |
| 1556 | if (N->getNumOperands() != 4) |
| 1557 | return false; |
| 1558 | |
| 1559 | // Check if the value doesn't reference the second vector. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1560 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1561 | SDOperand Arg = N->getOperand(i); |
| 1562 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1563 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1564 | if (cast<ConstantSDNode>(Arg)->getValue() >= 4) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1565 | return false; |
| 1566 | } |
| 1567 | |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
| 1571 | /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 1572 | /// specifies a shuffle of elements that is suitable for input to PSHUFHW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1573 | bool X86::isPSHUFHWMask(SDNode *N) { |
| 1574 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1575 | |
| 1576 | if (N->getNumOperands() != 8) |
| 1577 | return false; |
| 1578 | |
| 1579 | // Lower quadword copied in order. |
| 1580 | for (unsigned i = 0; i != 4; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1581 | SDOperand Arg = N->getOperand(i); |
| 1582 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1583 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1584 | if (cast<ConstantSDNode>(Arg)->getValue() != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1585 | return false; |
| 1586 | } |
| 1587 | |
| 1588 | // Upper quadword shuffled. |
| 1589 | for (unsigned i = 4; i != 8; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1590 | SDOperand Arg = N->getOperand(i); |
| 1591 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1592 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1593 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1594 | if (Val < 4 || Val > 7) |
| 1595 | return false; |
| 1596 | } |
| 1597 | |
| 1598 | return true; |
| 1599 | } |
| 1600 | |
| 1601 | /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 1602 | /// specifies a shuffle of elements that is suitable for input to PSHUFLW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1603 | bool X86::isPSHUFLWMask(SDNode *N) { |
| 1604 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1605 | |
| 1606 | if (N->getNumOperands() != 8) |
| 1607 | return false; |
| 1608 | |
| 1609 | // Upper quadword copied in order. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1610 | for (unsigned i = 4; i != 8; ++i) |
| 1611 | if (!isUndefOrEqual(N->getOperand(i), i)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1612 | return false; |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1613 | |
| 1614 | // Lower quadword shuffled. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1615 | for (unsigned i = 0; i != 4; ++i) |
| 1616 | if (!isUndefOrInRange(N->getOperand(i), 0, 4)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1617 | return false; |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 1618 | |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1622 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1623 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1624 | static bool isSHUFPMask(std::vector<SDOperand> &N) { |
| 1625 | unsigned NumElems = N.size(); |
| 1626 | if (NumElems != 2 && NumElems != 4) return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1627 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1628 | unsigned Half = NumElems / 2; |
| 1629 | for (unsigned i = 0; i < Half; ++i) |
| 1630 | if (!isUndefOrInRange(N[i], 0, NumElems)) |
| 1631 | return false; |
| 1632 | for (unsigned i = Half; i < NumElems; ++i) |
| 1633 | if (!isUndefOrInRange(N[i], NumElems, NumElems*2)) |
| 1634 | return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1635 | |
| 1636 | return true; |
| 1637 | } |
| 1638 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1639 | bool X86::isSHUFPMask(SDNode *N) { |
| 1640 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1641 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 1642 | return ::isSHUFPMask(Ops); |
| 1643 | } |
| 1644 | |
| 1645 | /// isCommutedSHUFP - Returns true if the shuffle mask is except |
| 1646 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 1647 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 1648 | /// the upper half to come from vector 2. |
| 1649 | static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) { |
| 1650 | unsigned NumElems = Ops.size(); |
| 1651 | if (NumElems != 2 && NumElems != 4) return false; |
| 1652 | |
| 1653 | unsigned Half = NumElems / 2; |
| 1654 | for (unsigned i = 0; i < Half; ++i) |
| 1655 | if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2)) |
| 1656 | return false; |
| 1657 | for (unsigned i = Half; i < NumElems; ++i) |
| 1658 | if (!isUndefOrInRange(Ops[i], 0, NumElems)) |
| 1659 | return false; |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | static bool isCommutedSHUFP(SDNode *N) { |
| 1664 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1665 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 1666 | return isCommutedSHUFP(Ops); |
| 1667 | } |
| 1668 | |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 1669 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1670 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
| 1671 | bool X86::isMOVHLPSMask(SDNode *N) { |
| 1672 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1673 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1674 | if (N->getNumOperands() != 4) |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 1675 | return false; |
| 1676 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1677 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1678 | return isUndefOrEqual(N->getOperand(0), 6) && |
| 1679 | isUndefOrEqual(N->getOperand(1), 7) && |
| 1680 | isUndefOrEqual(N->getOperand(2), 2) && |
| 1681 | isUndefOrEqual(N->getOperand(3), 3); |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1684 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1685 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
| 1686 | bool X86::isMOVLPMask(SDNode *N) { |
| 1687 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1688 | |
| 1689 | unsigned NumElems = N->getNumOperands(); |
| 1690 | if (NumElems != 2 && NumElems != 4) |
| 1691 | return false; |
| 1692 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1693 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 1694 | if (!isUndefOrEqual(N->getOperand(i), i + NumElems)) |
| 1695 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1696 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1697 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
| 1698 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 1699 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1700 | |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
| 1704 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 1705 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 1706 | /// and MOVLHPS. |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1707 | bool X86::isMOVHPMask(SDNode *N) { |
| 1708 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1709 | |
| 1710 | unsigned NumElems = N->getNumOperands(); |
| 1711 | if (NumElems != 2 && NumElems != 4) |
| 1712 | return false; |
| 1713 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1714 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 1715 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 1716 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1717 | |
| 1718 | for (unsigned i = 0; i < NumElems/2; ++i) { |
| 1719 | SDOperand Arg = N->getOperand(i + NumElems/2); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1720 | if (!isUndefOrEqual(Arg, i + NumElems)) |
| 1721 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1727 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1728 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1729 | bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) { |
| 1730 | unsigned NumElems = N.size(); |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1731 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 1732 | return false; |
| 1733 | |
| 1734 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1735 | SDOperand BitI = N[i]; |
| 1736 | SDOperand BitI1 = N[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1737 | if (!isUndefOrEqual(BitI, j)) |
| 1738 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1739 | if (V2IsSplat) { |
| 1740 | if (isUndefOrEqual(BitI1, NumElems)) |
| 1741 | return false; |
| 1742 | } else { |
| 1743 | if (!isUndefOrEqual(BitI1, j + NumElems)) |
| 1744 | return false; |
| 1745 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | return true; |
| 1749 | } |
| 1750 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1751 | bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) { |
| 1752 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1753 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 1754 | return ::isUNPCKLMask(Ops, V2IsSplat); |
| 1755 | } |
| 1756 | |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1757 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1758 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1759 | bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) { |
| 1760 | unsigned NumElems = N.size(); |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1761 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 1762 | return false; |
| 1763 | |
| 1764 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1765 | SDOperand BitI = N[i]; |
| 1766 | SDOperand BitI1 = N[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1767 | if (!isUndefOrEqual(BitI, j + NumElems/2)) |
| 1768 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1769 | if (V2IsSplat) { |
| 1770 | if (isUndefOrEqual(BitI1, NumElems)) |
| 1771 | return false; |
| 1772 | } else { |
| 1773 | if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems)) |
| 1774 | return false; |
| 1775 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1781 | bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) { |
| 1782 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1783 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 1784 | return ::isUNPCKHMask(Ops, V2IsSplat); |
| 1785 | } |
| 1786 | |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1787 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 1788 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 1789 | /// <0, 0, 1, 1> |
| 1790 | bool X86::isUNPCKL_v_undef_Mask(SDNode *N) { |
| 1791 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1792 | |
| 1793 | unsigned NumElems = N->getNumOperands(); |
| 1794 | if (NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 1795 | return false; |
| 1796 | |
| 1797 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 1798 | SDOperand BitI = N->getOperand(i); |
| 1799 | SDOperand BitI1 = N->getOperand(i+1); |
| 1800 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 1801 | if (!isUndefOrEqual(BitI, j)) |
| 1802 | return false; |
| 1803 | if (!isUndefOrEqual(BitI1, j)) |
| 1804 | return false; |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | return true; |
| 1808 | } |
| 1809 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1810 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1811 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 1812 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
| 1813 | static bool isMOVLMask(std::vector<SDOperand> &N) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1814 | unsigned NumElems = N.size(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1815 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1816 | return false; |
| 1817 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1818 | if (!isUndefOrEqual(N[0], NumElems)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1819 | return false; |
| 1820 | |
| 1821 | for (unsigned i = 1; i < NumElems; ++i) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1822 | SDOperand Arg = N[i]; |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 1823 | if (!isUndefOrEqual(Arg, i)) |
| 1824 | return false; |
| 1825 | } |
| 1826 | |
| 1827 | return true; |
| 1828 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 1829 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1830 | bool X86::isMOVLMask(SDNode *N) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1831 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1832 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1833 | return ::isMOVLMask(Ops); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1836 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 1837 | /// 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] | 1838 | /// element of vector 2 and the other elements to come from vector 1 in order. |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1839 | static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1840 | unsigned NumElems = Ops.size(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1841 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1842 | return false; |
| 1843 | |
| 1844 | if (!isUndefOrEqual(Ops[0], 0)) |
| 1845 | return false; |
| 1846 | |
| 1847 | for (unsigned i = 1; i < NumElems; ++i) { |
| 1848 | SDOperand Arg = Ops[i]; |
| 1849 | if (V2IsSplat) { |
| 1850 | if (!isUndefOrEqual(Arg, NumElems)) |
| 1851 | return false; |
| 1852 | } else { |
| 1853 | if (!isUndefOrEqual(Arg, i+NumElems)) |
| 1854 | return false; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | return true; |
| 1859 | } |
| 1860 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1861 | static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1862 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1863 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 1864 | return isCommutedMOVL(Ops, V2IsSplat); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1867 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1868 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
| 1869 | bool X86::isMOVSHDUPMask(SDNode *N) { |
| 1870 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1871 | |
| 1872 | if (N->getNumOperands() != 4) |
| 1873 | return false; |
| 1874 | |
| 1875 | // Expect 1, 1, 3, 3 |
| 1876 | for (unsigned i = 0; i < 2; ++i) { |
| 1877 | SDOperand Arg = N->getOperand(i); |
| 1878 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1879 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1880 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1881 | if (Val != 1) return false; |
| 1882 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1883 | |
| 1884 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1885 | for (unsigned i = 2; i < 4; ++i) { |
| 1886 | SDOperand Arg = N->getOperand(i); |
| 1887 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1888 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1889 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1890 | if (Val != 3) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1891 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1892 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 1893 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1894 | // Don't use movshdup if it can be done with a shufps. |
| 1895 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 1899 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
| 1900 | bool X86::isMOVSLDUPMask(SDNode *N) { |
| 1901 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1902 | |
| 1903 | if (N->getNumOperands() != 4) |
| 1904 | return false; |
| 1905 | |
| 1906 | // Expect 0, 0, 2, 2 |
| 1907 | for (unsigned i = 0; i < 2; ++i) { |
| 1908 | SDOperand Arg = N->getOperand(i); |
| 1909 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1910 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1911 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1912 | if (Val != 0) return false; |
| 1913 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1914 | |
| 1915 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1916 | for (unsigned i = 2; i < 4; ++i) { |
| 1917 | SDOperand Arg = N->getOperand(i); |
| 1918 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1919 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1920 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 1921 | if (Val != 2) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1922 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1923 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 1924 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 1925 | // Don't use movshdup if it can be done with a shufps. |
| 1926 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1929 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 1930 | /// a splat of a single element. |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 1931 | static bool isSplatMask(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1932 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1933 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1934 | // This is a splat operation if each element of the permute is the same, and |
| 1935 | // if the value doesn't reference the second vector. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 1936 | unsigned NumElems = N->getNumOperands(); |
| 1937 | SDOperand ElementBase; |
| 1938 | unsigned i = 0; |
| 1939 | for (; i != NumElems; ++i) { |
| 1940 | SDOperand Elt = N->getOperand(i); |
| 1941 | if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) { |
| 1942 | ElementBase = Elt; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | if (!ElementBase.Val) |
| 1948 | return false; |
| 1949 | |
| 1950 | for (; i != NumElems; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1951 | SDOperand Arg = N->getOperand(i); |
| 1952 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 1953 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 1954 | if (Arg != ElementBase) return false; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // Make sure it is a splat of the first vector operand. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 1958 | return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 1961 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 1962 | /// a splat of a single element and it's a 2 or 4 element mask. |
| 1963 | bool X86::isSplatMask(SDNode *N) { |
| 1964 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 1965 | |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 1966 | // 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] | 1967 | if (N->getNumOperands() != 4 && N->getNumOperands() != 2) |
| 1968 | return false; |
| 1969 | return ::isSplatMask(N); |
| 1970 | } |
| 1971 | |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 1972 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 1973 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 1974 | /// instructions. |
| 1975 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 1976 | unsigned NumOperands = N->getNumOperands(); |
| 1977 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 1978 | unsigned Mask = 0; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 1979 | for (unsigned i = 0; i < NumOperands; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 1980 | unsigned Val = 0; |
| 1981 | SDOperand Arg = N->getOperand(NumOperands-i-1); |
| 1982 | if (Arg.getOpcode() != ISD::UNDEF) |
| 1983 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 1984 | if (Val >= NumOperands) Val -= NumOperands; |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 1985 | Mask |= Val; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 1986 | if (i != NumOperands - 1) |
| 1987 | Mask <<= Shift; |
| 1988 | } |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 1989 | |
| 1990 | return Mask; |
| 1991 | } |
| 1992 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 1993 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 1994 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 1995 | /// instructions. |
| 1996 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
| 1997 | unsigned Mask = 0; |
| 1998 | // 8 nodes, but we only care about the last 4. |
| 1999 | for (unsigned i = 7; i >= 4; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2000 | unsigned Val = 0; |
| 2001 | SDOperand Arg = N->getOperand(i); |
| 2002 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2003 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2004 | Mask |= (Val - 4); |
| 2005 | if (i != 4) |
| 2006 | Mask <<= 2; |
| 2007 | } |
| 2008 | |
| 2009 | return Mask; |
| 2010 | } |
| 2011 | |
| 2012 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2013 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2014 | /// instructions. |
| 2015 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
| 2016 | unsigned Mask = 0; |
| 2017 | // 8 nodes, but we only care about the first 4. |
| 2018 | for (int i = 3; i >= 0; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2019 | unsigned Val = 0; |
| 2020 | SDOperand Arg = N->getOperand(i); |
| 2021 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2022 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2023 | Mask |= Val; |
| 2024 | if (i != 0) |
| 2025 | Mask <<= 2; |
| 2026 | } |
| 2027 | |
| 2028 | return Mask; |
| 2029 | } |
| 2030 | |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 2031 | /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand |
| 2032 | /// specifies a 8 element shuffle that can be broken into a pair of |
| 2033 | /// PSHUFHW and PSHUFLW. |
| 2034 | static bool isPSHUFHW_PSHUFLWMask(SDNode *N) { |
| 2035 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2036 | |
| 2037 | if (N->getNumOperands() != 8) |
| 2038 | return false; |
| 2039 | |
| 2040 | // Lower quadword shuffled. |
| 2041 | for (unsigned i = 0; i != 4; ++i) { |
| 2042 | SDOperand Arg = N->getOperand(i); |
| 2043 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2044 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2045 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2046 | if (Val > 4) |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
| 2050 | // Upper quadword shuffled. |
| 2051 | for (unsigned i = 4; i != 8; ++i) { |
| 2052 | SDOperand Arg = N->getOperand(i); |
| 2053 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2054 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2055 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2056 | if (Val < 4 || Val > 7) |
| 2057 | return false; |
| 2058 | } |
| 2059 | |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2063 | /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as |
| 2064 | /// values in ther permute mask. |
| 2065 | static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) { |
| 2066 | SDOperand V1 = Op.getOperand(0); |
| 2067 | SDOperand V2 = Op.getOperand(1); |
| 2068 | SDOperand Mask = Op.getOperand(2); |
| 2069 | MVT::ValueType VT = Op.getValueType(); |
| 2070 | MVT::ValueType MaskVT = Mask.getValueType(); |
| 2071 | MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT); |
| 2072 | unsigned NumElems = Mask.getNumOperands(); |
| 2073 | std::vector<SDOperand> MaskVec; |
| 2074 | |
| 2075 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2076 | SDOperand Arg = Mask.getOperand(i); |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 2077 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2078 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 2079 | continue; |
| 2080 | } |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2081 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2082 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2083 | if (Val < NumElems) |
| 2084 | MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); |
| 2085 | else |
| 2086 | MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); |
| 2087 | } |
| 2088 | |
| 2089 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2090 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask); |
| 2091 | } |
| 2092 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2093 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2094 | /// match movhlps. The lower half elements should come from upper half of |
| 2095 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2096 | /// half of V2 (and in order). |
| 2097 | static bool ShouldXformToMOVHLPS(SDNode *Mask) { |
| 2098 | unsigned NumElems = Mask->getNumOperands(); |
| 2099 | if (NumElems != 4) |
| 2100 | return false; |
| 2101 | for (unsigned i = 0, e = 2; i != e; ++i) |
| 2102 | if (!isUndefOrEqual(Mask->getOperand(i), i+2)) |
| 2103 | return false; |
| 2104 | for (unsigned i = 2; i != 4; ++i) |
| 2105 | if (!isUndefOrEqual(Mask->getOperand(i), i+4)) |
| 2106 | return false; |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2110 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
| 2111 | /// is promoted to a vector. |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2112 | static inline bool isScalarLoadToVector(SDNode *N) { |
| 2113 | if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 2114 | N = N->getOperand(0).Val; |
| 2115 | return (N->getOpcode() == ISD::LOAD); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2116 | } |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2120 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2121 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2122 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2123 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2124 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
| 2125 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) { |
| 2126 | if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1)) |
| 2127 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2128 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2129 | unsigned NumElems = Mask->getNumOperands(); |
| 2130 | if (NumElems != 2 && NumElems != 4) |
| 2131 | return false; |
| 2132 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| 2133 | if (!isUndefOrEqual(Mask->getOperand(i), i)) |
| 2134 | return false; |
| 2135 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
| 2136 | if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems)) |
| 2137 | return false; |
| 2138 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2141 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2142 | /// all the same. |
| 2143 | static bool isSplatVector(SDNode *N) { |
| 2144 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2145 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2146 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2147 | SDOperand SplatValue = N->getOperand(0); |
| 2148 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2149 | if (N->getOperand(i) != SplatValue) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2150 | return false; |
| 2151 | return true; |
| 2152 | } |
| 2153 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2154 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2155 | /// that point to V2 points to its first element. |
| 2156 | static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) { |
| 2157 | assert(Mask.getOpcode() == ISD::BUILD_VECTOR); |
| 2158 | |
| 2159 | bool Changed = false; |
| 2160 | std::vector<SDOperand> MaskVec; |
| 2161 | unsigned NumElems = Mask.getNumOperands(); |
| 2162 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2163 | SDOperand Arg = Mask.getOperand(i); |
| 2164 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2165 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2166 | if (Val > NumElems) { |
| 2167 | Arg = DAG.getConstant(NumElems, Arg.getValueType()); |
| 2168 | Changed = true; |
| 2169 | } |
| 2170 | } |
| 2171 | MaskVec.push_back(Arg); |
| 2172 | } |
| 2173 | |
| 2174 | if (Changed) |
| 2175 | Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec); |
| 2176 | return Mask; |
| 2177 | } |
| 2178 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2179 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2180 | /// operation of specified width. |
| 2181 | static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2182 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2183 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2184 | |
| 2185 | std::vector<SDOperand> MaskVec; |
| 2186 | MaskVec.push_back(DAG.getConstant(NumElems, BaseVT)); |
| 2187 | for (unsigned i = 1; i != NumElems; ++i) |
| 2188 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2189 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2190 | } |
| 2191 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2192 | /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation |
| 2193 | /// of specified width. |
| 2194 | static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2195 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2196 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2197 | std::vector<SDOperand> MaskVec; |
| 2198 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
| 2199 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2200 | MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT)); |
| 2201 | } |
| 2202 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2203 | } |
| 2204 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2205 | /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation |
| 2206 | /// of specified width. |
| 2207 | static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2208 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2209 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2210 | unsigned Half = NumElems/2; |
| 2211 | std::vector<SDOperand> MaskVec; |
| 2212 | for (unsigned i = 0; i != Half; ++i) { |
| 2213 | MaskVec.push_back(DAG.getConstant(i + Half, BaseVT)); |
| 2214 | MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT)); |
| 2215 | } |
| 2216 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2217 | } |
| 2218 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2219 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2220 | /// |
| 2221 | static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) { |
| 2222 | assert(MVT::isVector(VT) && "Expected a vector type"); |
| 2223 | unsigned NumElems = getVectorNumElements(VT); |
| 2224 | MVT::ValueType EVT = MVT::getVectorBaseType(VT); |
| 2225 | bool isFP = MVT::isFloatingPoint(EVT); |
| 2226 | SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT); |
| 2227 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
| 2228 | return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec); |
| 2229 | } |
| 2230 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2231 | /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32. |
| 2232 | /// |
| 2233 | static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) { |
| 2234 | SDOperand V1 = Op.getOperand(0); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2235 | SDOperand Mask = Op.getOperand(2); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2236 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2237 | unsigned NumElems = Mask.getNumOperands(); |
| 2238 | Mask = getUnpacklMask(NumElems, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2239 | while (NumElems != 4) { |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2240 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2241 | NumElems >>= 1; |
| 2242 | } |
| 2243 | V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1); |
| 2244 | |
| 2245 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2246 | Mask = getZeroVector(MaskVT, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2247 | SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2248 | DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2249 | return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle); |
| 2250 | } |
| 2251 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2252 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2253 | /// constant +0.0. |
| 2254 | static inline bool isZeroNode(SDOperand Elt) { |
| 2255 | return ((isa<ConstantSDNode>(Elt) && |
| 2256 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 2257 | (isa<ConstantFPSDNode>(Elt) && |
| 2258 | cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0))); |
| 2259 | } |
| 2260 | |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2261 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
| 2262 | /// vector and zero or undef vector. |
| 2263 | static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2264 | unsigned NumElems, unsigned Idx, |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2265 | bool isZero, SelectionDAG &DAG) { |
| 2266 | SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2267 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2268 | MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); |
| 2269 | SDOperand Zero = DAG.getConstant(0, EVT); |
| 2270 | std::vector<SDOperand> MaskVec(NumElems, Zero); |
| 2271 | MaskVec[Idx] = DAG.getConstant(NumElems, EVT); |
| 2272 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2273 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2276 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 2277 | /// |
| 2278 | static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros, |
| 2279 | unsigned NumNonZero, unsigned NumZero, |
| 2280 | SelectionDAG &DAG) { |
| 2281 | if (NumNonZero > 8) |
| 2282 | return SDOperand(); |
| 2283 | |
| 2284 | SDOperand V(0, 0); |
| 2285 | bool First = true; |
| 2286 | for (unsigned i = 0; i < 16; ++i) { |
| 2287 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 2288 | if (ThisIsNonZero && First) { |
| 2289 | if (NumZero) |
| 2290 | V = getZeroVector(MVT::v8i16, DAG); |
| 2291 | else |
| 2292 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2293 | First = false; |
| 2294 | } |
| 2295 | |
| 2296 | if ((i & 1) != 0) { |
| 2297 | SDOperand ThisElt(0, 0), LastElt(0, 0); |
| 2298 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 2299 | if (LastIsNonZero) { |
| 2300 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1)); |
| 2301 | } |
| 2302 | if (ThisIsNonZero) { |
| 2303 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i)); |
| 2304 | ThisElt = DAG.getNode(ISD::SHL, MVT::i16, |
| 2305 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 2306 | if (LastIsNonZero) |
| 2307 | ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt); |
| 2308 | } else |
| 2309 | ThisElt = LastElt; |
| 2310 | |
| 2311 | if (ThisElt.Val) |
| 2312 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt, |
| 2313 | DAG.getConstant(i/2, MVT::i32)); |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V); |
| 2318 | } |
| 2319 | |
| 2320 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16. |
| 2321 | /// |
| 2322 | static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros, |
| 2323 | unsigned NumNonZero, unsigned NumZero, |
| 2324 | SelectionDAG &DAG) { |
| 2325 | if (NumNonZero > 4) |
| 2326 | return SDOperand(); |
| 2327 | |
| 2328 | SDOperand V(0, 0); |
| 2329 | bool First = true; |
| 2330 | for (unsigned i = 0; i < 8; ++i) { |
| 2331 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 2332 | if (isNonZero) { |
| 2333 | if (First) { |
| 2334 | if (NumZero) |
| 2335 | V = getZeroVector(MVT::v8i16, DAG); |
| 2336 | else |
| 2337 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2338 | First = false; |
| 2339 | } |
| 2340 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i), |
| 2341 | DAG.getConstant(i, MVT::i32)); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | return V; |
| 2346 | } |
| 2347 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2348 | SDOperand |
| 2349 | X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 2350 | // All zero's are handled with pxor. |
| 2351 | if (ISD::isBuildVectorAllZeros(Op.Val)) |
| 2352 | return Op; |
| 2353 | |
| 2354 | // All one's are handled with pcmpeqd. |
| 2355 | if (ISD::isBuildVectorAllOnes(Op.Val)) |
| 2356 | return Op; |
| 2357 | |
| 2358 | MVT::ValueType VT = Op.getValueType(); |
| 2359 | MVT::ValueType EVT = MVT::getVectorBaseType(VT); |
| 2360 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
| 2361 | |
| 2362 | unsigned NumElems = Op.getNumOperands(); |
| 2363 | unsigned NumZero = 0; |
| 2364 | unsigned NumNonZero = 0; |
| 2365 | unsigned NonZeros = 0; |
| 2366 | std::set<SDOperand> Values; |
| 2367 | for (unsigned i = 0; i < NumElems; ++i) { |
| 2368 | SDOperand Elt = Op.getOperand(i); |
| 2369 | if (Elt.getOpcode() != ISD::UNDEF) { |
| 2370 | Values.insert(Elt); |
| 2371 | if (isZeroNode(Elt)) |
| 2372 | NumZero++; |
| 2373 | else { |
| 2374 | NonZeros |= (1 << i); |
| 2375 | NumNonZero++; |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | if (NumNonZero == 0) |
| 2381 | // Must be a mix of zero and undef. Return a zero vector. |
| 2382 | return getZeroVector(VT, DAG); |
| 2383 | |
| 2384 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 2385 | if (Values.size() == 1) |
| 2386 | return SDOperand(); |
| 2387 | |
| 2388 | // Special case for single non-zero element. |
| 2389 | if (NumNonZero == 1) { |
| 2390 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
| 2391 | SDOperand Item = Op.getOperand(Idx); |
| 2392 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item); |
| 2393 | if (Idx == 0) |
| 2394 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| 2395 | return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx, |
| 2396 | NumZero > 0, DAG); |
| 2397 | |
| 2398 | if (EVTBits == 32) { |
| 2399 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
| 2400 | Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0, |
| 2401 | DAG); |
| 2402 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2403 | MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); |
| 2404 | std::vector<SDOperand> MaskVec; |
| 2405 | for (unsigned i = 0; i < NumElems; i++) |
| 2406 | MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT)); |
| 2407 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2408 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item, |
| 2409 | DAG.getNode(ISD::UNDEF, VT), Mask); |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | // Let legalizer expand 2-widde build_vector's. |
| 2414 | if (EVTBits == 64) |
| 2415 | return SDOperand(); |
| 2416 | |
| 2417 | // If element VT is < 32 bits, convert it to inserts into a zero vector. |
| 2418 | if (EVTBits == 8) { |
| 2419 | SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG); |
| 2420 | if (V.Val) return V; |
| 2421 | } |
| 2422 | |
| 2423 | if (EVTBits == 16) { |
| 2424 | SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG); |
| 2425 | if (V.Val) return V; |
| 2426 | } |
| 2427 | |
| 2428 | // If element VT is == 32 bits, turn it into a number of shuffles. |
| 2429 | std::vector<SDOperand> V(NumElems); |
| 2430 | if (NumElems == 4 && NumZero > 0) { |
| 2431 | for (unsigned i = 0; i < 4; ++i) { |
| 2432 | bool isZero = !(NonZeros & (1 << i)); |
| 2433 | if (isZero) |
| 2434 | V[i] = getZeroVector(VT, DAG); |
| 2435 | else |
| 2436 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2437 | } |
| 2438 | |
| 2439 | for (unsigned i = 0; i < 2; ++i) { |
| 2440 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 2441 | default: break; |
| 2442 | case 0: |
| 2443 | V[i] = V[i*2]; // Must be a zero vector. |
| 2444 | break; |
| 2445 | case 1: |
| 2446 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2], |
| 2447 | getMOVLMask(NumElems, DAG)); |
| 2448 | break; |
| 2449 | case 2: |
| 2450 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2451 | getMOVLMask(NumElems, DAG)); |
| 2452 | break; |
| 2453 | case 3: |
| 2454 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 2455 | getUnpacklMask(NumElems, DAG)); |
| 2456 | break; |
| 2457 | } |
| 2458 | } |
| 2459 | |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 2460 | // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd) |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2461 | // clears the upper bits. |
| 2462 | // FIXME: we can do the same for v4f32 case when we know both parts of |
| 2463 | // the lower half come from scalar_to_vector (loadf32). We should do |
| 2464 | // that in post legalizer dag combiner with target specific hooks. |
| 2465 | if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0) |
| 2466 | return V[0]; |
| 2467 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2468 | MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); |
| 2469 | std::vector<SDOperand> MaskVec; |
| 2470 | bool Reverse = (NonZeros & 0x3) == 2; |
| 2471 | for (unsigned i = 0; i < 2; ++i) |
| 2472 | if (Reverse) |
| 2473 | MaskVec.push_back(DAG.getConstant(1-i, EVT)); |
| 2474 | else |
| 2475 | MaskVec.push_back(DAG.getConstant(i, EVT)); |
| 2476 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 2477 | for (unsigned i = 0; i < 2; ++i) |
| 2478 | if (Reverse) |
| 2479 | MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT)); |
| 2480 | else |
| 2481 | MaskVec.push_back(DAG.getConstant(i+NumElems, EVT)); |
| 2482 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2483 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask); |
| 2484 | } |
| 2485 | |
| 2486 | if (Values.size() > 2) { |
| 2487 | // Expand into a number of unpckl*. |
| 2488 | // e.g. for v4f32 |
| 2489 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 2490 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 2491 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
| 2492 | SDOperand UnpckMask = getUnpacklMask(NumElems, DAG); |
| 2493 | for (unsigned i = 0; i < NumElems; ++i) |
| 2494 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 2495 | NumElems >>= 1; |
| 2496 | while (NumElems != 0) { |
| 2497 | for (unsigned i = 0; i < NumElems; ++i) |
| 2498 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems], |
| 2499 | UnpckMask); |
| 2500 | NumElems >>= 1; |
| 2501 | } |
| 2502 | return V[0]; |
| 2503 | } |
| 2504 | |
| 2505 | return SDOperand(); |
| 2506 | } |
| 2507 | |
| 2508 | SDOperand |
| 2509 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { |
| 2510 | SDOperand V1 = Op.getOperand(0); |
| 2511 | SDOperand V2 = Op.getOperand(1); |
| 2512 | SDOperand PermMask = Op.getOperand(2); |
| 2513 | MVT::ValueType VT = Op.getValueType(); |
| 2514 | unsigned NumElems = PermMask.getNumOperands(); |
| 2515 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 2516 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
| 2517 | |
| 2518 | if (isSplatMask(PermMask.Val)) { |
| 2519 | if (NumElems <= 4) return Op; |
| 2520 | // Promote it to a v4i32 splat. |
| 2521 | return PromoteSplat(Op, DAG); |
| 2522 | } |
| 2523 | |
| 2524 | if (X86::isMOVLMask(PermMask.Val)) |
| 2525 | return (V1IsUndef) ? V2 : Op; |
| 2526 | |
| 2527 | if (X86::isMOVSHDUPMask(PermMask.Val) || |
| 2528 | X86::isMOVSLDUPMask(PermMask.Val) || |
| 2529 | X86::isMOVHLPSMask(PermMask.Val) || |
| 2530 | X86::isMOVHPMask(PermMask.Val) || |
| 2531 | X86::isMOVLPMask(PermMask.Val)) |
| 2532 | return Op; |
| 2533 | |
| 2534 | if (ShouldXformToMOVHLPS(PermMask.Val) || |
| 2535 | ShouldXformToMOVLP(V1.Val, PermMask.Val)) |
| 2536 | return CommuteVectorShuffle(Op, DAG); |
| 2537 | |
| 2538 | bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF; |
| 2539 | bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF; |
| 2540 | if (V1IsSplat && !V2IsSplat) { |
| 2541 | Op = CommuteVectorShuffle(Op, DAG); |
| 2542 | V1 = Op.getOperand(0); |
| 2543 | V2 = Op.getOperand(1); |
| 2544 | PermMask = Op.getOperand(2); |
| 2545 | V2IsSplat = true; |
| 2546 | } |
| 2547 | |
| 2548 | if (isCommutedMOVL(PermMask.Val, V2IsSplat)) { |
| 2549 | if (V2IsUndef) return V1; |
| 2550 | Op = CommuteVectorShuffle(Op, DAG); |
| 2551 | V1 = Op.getOperand(0); |
| 2552 | V2 = Op.getOperand(1); |
| 2553 | PermMask = Op.getOperand(2); |
| 2554 | if (V2IsSplat) { |
| 2555 | // V2 is a splat, so the mask may be malformed. That is, it may point |
| 2556 | // to any V2 element. The instruction selectior won't like this. Get |
| 2557 | // a corrected mask and commute to form a proper MOVS{S|D}. |
| 2558 | SDOperand NewMask = getMOVLMask(NumElems, DAG); |
| 2559 | if (NewMask.Val != PermMask.Val) |
| 2560 | Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 2561 | } |
| 2562 | return Op; |
| 2563 | } |
| 2564 | |
| 2565 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
| 2566 | X86::isUNPCKLMask(PermMask.Val) || |
| 2567 | X86::isUNPCKHMask(PermMask.Val)) |
| 2568 | return Op; |
| 2569 | |
| 2570 | if (V2IsSplat) { |
| 2571 | // Normalize mask so all entries that point to V2 points to its first |
| 2572 | // element then try to match unpck{h|l} again. If match, return a |
| 2573 | // new vector_shuffle with the corrected mask. |
| 2574 | SDOperand NewMask = NormalizeMask(PermMask, DAG); |
| 2575 | if (NewMask.Val != PermMask.Val) { |
| 2576 | if (X86::isUNPCKLMask(PermMask.Val, true)) { |
| 2577 | SDOperand NewMask = getUnpacklMask(NumElems, DAG); |
| 2578 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 2579 | } else if (X86::isUNPCKHMask(PermMask.Val, true)) { |
| 2580 | SDOperand NewMask = getUnpackhMask(NumElems, DAG); |
| 2581 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | // Normalize the node to match x86 shuffle ops if needed |
| 2587 | if (V2.getOpcode() != ISD::UNDEF) |
| 2588 | if (isCommutedSHUFP(PermMask.Val)) { |
| 2589 | Op = CommuteVectorShuffle(Op, DAG); |
| 2590 | V1 = Op.getOperand(0); |
| 2591 | V2 = Op.getOperand(1); |
| 2592 | PermMask = Op.getOperand(2); |
| 2593 | } |
| 2594 | |
| 2595 | // If VT is integer, try PSHUF* first, then SHUFP*. |
| 2596 | if (MVT::isInteger(VT)) { |
| 2597 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 2598 | X86::isPSHUFHWMask(PermMask.Val) || |
| 2599 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 2600 | if (V2.getOpcode() != ISD::UNDEF) |
| 2601 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 2602 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 2603 | return Op; |
| 2604 | } |
| 2605 | |
| 2606 | if (X86::isSHUFPMask(PermMask.Val)) |
| 2607 | return Op; |
| 2608 | |
| 2609 | // Handle v8i16 shuffle high / low shuffle node pair. |
| 2610 | if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) { |
| 2611 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2612 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2613 | std::vector<SDOperand> MaskVec; |
| 2614 | for (unsigned i = 0; i != 4; ++i) |
| 2615 | MaskVec.push_back(PermMask.getOperand(i)); |
| 2616 | for (unsigned i = 4; i != 8; ++i) |
| 2617 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2618 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2619 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2620 | MaskVec.clear(); |
| 2621 | for (unsigned i = 0; i != 4; ++i) |
| 2622 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2623 | for (unsigned i = 4; i != 8; ++i) |
| 2624 | MaskVec.push_back(PermMask.getOperand(i)); |
| 2625 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 2626 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 2627 | } |
| 2628 | } else { |
| 2629 | // Floating point cases in the other order. |
| 2630 | if (X86::isSHUFPMask(PermMask.Val)) |
| 2631 | return Op; |
| 2632 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 2633 | X86::isPSHUFHWMask(PermMask.Val) || |
| 2634 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 2635 | if (V2.getOpcode() != ISD::UNDEF) |
| 2636 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 2637 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 2638 | return Op; |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | if (NumElems == 4) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2643 | MVT::ValueType MaskVT = PermMask.getValueType(); |
| 2644 | MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 2645 | std::vector<std::pair<int, int> > Locs; |
| 2646 | Locs.reserve(NumElems); |
| 2647 | std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2648 | std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2649 | unsigned NumHi = 0; |
| 2650 | unsigned NumLo = 0; |
| 2651 | // If no more than two elements come from either vector. This can be |
| 2652 | // implemented with two shuffles. First shuffle gather the elements. |
| 2653 | // The second shuffle, which takes the first shuffle as both of its |
| 2654 | // vector operands, put the elements into the right order. |
| 2655 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2656 | SDOperand Elt = PermMask.getOperand(i); |
| 2657 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 2658 | Locs[i] = std::make_pair(-1, -1); |
| 2659 | } else { |
| 2660 | unsigned Val = cast<ConstantSDNode>(Elt)->getValue(); |
| 2661 | if (Val < NumElems) { |
| 2662 | Locs[i] = std::make_pair(0, NumLo); |
| 2663 | Mask1[NumLo] = Elt; |
| 2664 | NumLo++; |
| 2665 | } else { |
| 2666 | Locs[i] = std::make_pair(1, NumHi); |
| 2667 | if (2+NumHi < NumElems) |
| 2668 | Mask1[2+NumHi] = Elt; |
| 2669 | NumHi++; |
| 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | if (NumLo <= 2 && NumHi <= 2) { |
| 2674 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 2675 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1)); |
| 2676 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2677 | if (Locs[i].first == -1) |
| 2678 | continue; |
| 2679 | else { |
| 2680 | unsigned Idx = (i < NumElems/2) ? 0 : NumElems; |
| 2681 | Idx += Locs[i].first * (NumElems/2) + Locs[i].second; |
| 2682 | Mask2[i] = DAG.getConstant(Idx, MaskEVT); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, |
| 2687 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2)); |
| 2688 | } |
| 2689 | |
| 2690 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 2691 | Locs.clear(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2692 | std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2693 | std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2694 | std::vector<SDOperand> *MaskPtr = &LoMask; |
| 2695 | unsigned MaskIdx = 0; |
| 2696 | unsigned LoIdx = 0; |
| 2697 | unsigned HiIdx = NumElems/2; |
| 2698 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2699 | if (i == NumElems/2) { |
| 2700 | MaskPtr = &HiMask; |
| 2701 | MaskIdx = 1; |
| 2702 | LoIdx = 0; |
| 2703 | HiIdx = NumElems/2; |
| 2704 | } |
| 2705 | SDOperand Elt = PermMask.getOperand(i); |
| 2706 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 2707 | Locs[i] = std::make_pair(-1, -1); |
| 2708 | } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) { |
| 2709 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
| 2710 | (*MaskPtr)[LoIdx] = Elt; |
| 2711 | LoIdx++; |
| 2712 | } else { |
| 2713 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
| 2714 | (*MaskPtr)[HiIdx] = Elt; |
| 2715 | HiIdx++; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2719 | SDOperand LoShuffle = |
| 2720 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 2721 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask)); |
| 2722 | SDOperand HiShuffle = |
| 2723 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
| 2724 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2725 | std::vector<SDOperand> MaskOps; |
| 2726 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2727 | if (Locs[i].first == -1) { |
| 2728 | MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 2729 | } else { |
| 2730 | unsigned Idx = Locs[i].first * NumElems + Locs[i].second; |
| 2731 | MaskOps.push_back(DAG.getConstant(Idx, MaskEVT)); |
| 2732 | } |
| 2733 | } |
| 2734 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle, |
| 2735 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps)); |
| 2736 | } |
| 2737 | |
| 2738 | return SDOperand(); |
| 2739 | } |
| 2740 | |
| 2741 | SDOperand |
| 2742 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
| 2743 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 2744 | return SDOperand(); |
| 2745 | |
| 2746 | MVT::ValueType VT = Op.getValueType(); |
| 2747 | // TODO: handle v16i8. |
| 2748 | if (MVT::getSizeInBits(VT) == 16) { |
| 2749 | // Transform it so it match pextrw which produces a 32-bit result. |
| 2750 | MVT::ValueType EVT = (MVT::ValueType)(VT+1); |
| 2751 | SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT, |
| 2752 | Op.getOperand(0), Op.getOperand(1)); |
| 2753 | SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract, |
| 2754 | DAG.getValueType(VT)); |
| 2755 | return DAG.getNode(ISD::TRUNCATE, VT, Assert); |
| 2756 | } else if (MVT::getSizeInBits(VT) == 32) { |
| 2757 | SDOperand Vec = Op.getOperand(0); |
| 2758 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 2759 | if (Idx == 0) |
| 2760 | return Op; |
| 2761 | |
| 2762 | // SHUFPS the element to the lowest double word, then movss. |
| 2763 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 2764 | SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4, |
| 2765 | MVT::getVectorBaseType(MaskVT)); |
| 2766 | std::vector<SDOperand> IdxVec; |
| 2767 | IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT))); |
| 2768 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 2769 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 2770 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 2771 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec); |
| 2772 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 2773 | Vec, Vec, Mask); |
| 2774 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
| 2775 | DAG.getConstant(0, MVT::i32)); |
| 2776 | } else if (MVT::getSizeInBits(VT) == 64) { |
| 2777 | SDOperand Vec = Op.getOperand(0); |
| 2778 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 2779 | if (Idx == 0) |
| 2780 | return Op; |
| 2781 | |
| 2782 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 2783 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 2784 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
| 2785 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 2786 | std::vector<SDOperand> IdxVec; |
| 2787 | IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT))); |
| 2788 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 2789 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec); |
| 2790 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 2791 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
| 2792 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
| 2793 | DAG.getConstant(0, MVT::i32)); |
| 2794 | } |
| 2795 | |
| 2796 | return SDOperand(); |
| 2797 | } |
| 2798 | |
| 2799 | SDOperand |
| 2800 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 2801 | // 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] | 2802 | // as its second argument. |
| 2803 | MVT::ValueType VT = Op.getValueType(); |
| 2804 | MVT::ValueType BaseVT = MVT::getVectorBaseType(VT); |
| 2805 | SDOperand N0 = Op.getOperand(0); |
| 2806 | SDOperand N1 = Op.getOperand(1); |
| 2807 | SDOperand N2 = Op.getOperand(2); |
| 2808 | if (MVT::getSizeInBits(BaseVT) == 16) { |
| 2809 | if (N1.getValueType() != MVT::i32) |
| 2810 | N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1); |
| 2811 | if (N2.getValueType() != MVT::i32) |
| 2812 | N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32); |
| 2813 | return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2); |
| 2814 | } else if (MVT::getSizeInBits(BaseVT) == 32) { |
| 2815 | unsigned Idx = cast<ConstantSDNode>(N2)->getValue(); |
| 2816 | if (Idx == 0) { |
| 2817 | // Use a movss. |
| 2818 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1); |
| 2819 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 2820 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2821 | std::vector<SDOperand> MaskVec; |
| 2822 | MaskVec.push_back(DAG.getConstant(4, BaseVT)); |
| 2823 | for (unsigned i = 1; i <= 3; ++i) |
| 2824 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2825 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1, |
| 2826 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec)); |
| 2827 | } else { |
| 2828 | // Use two pinsrw instructions to insert a 32 bit value. |
| 2829 | Idx <<= 1; |
| 2830 | if (MVT::isFloatingPoint(N1.getValueType())) { |
| 2831 | if (N1.getOpcode() == ISD::LOAD) { |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 2832 | // Just load directly from f32mem to GR32. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2833 | N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1), |
| 2834 | N1.getOperand(2)); |
| 2835 | } else { |
| 2836 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1); |
| 2837 | N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1); |
| 2838 | N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1, |
| 2839 | DAG.getConstant(0, MVT::i32)); |
| 2840 | } |
| 2841 | } |
| 2842 | N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0); |
| 2843 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
| 2844 | DAG.getConstant(Idx, MVT::i32)); |
| 2845 | N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8)); |
| 2846 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
| 2847 | DAG.getConstant(Idx+1, MVT::i32)); |
| 2848 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | return SDOperand(); |
| 2853 | } |
| 2854 | |
| 2855 | SDOperand |
| 2856 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 2857 | SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0)); |
| 2858 | return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt); |
| 2859 | } |
| 2860 | |
| 2861 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 2862 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 2863 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 2864 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 2865 | // be used to form addressing mode. These wrapped nodes will be selected |
| 2866 | // into MOV32ri. |
| 2867 | SDOperand |
| 2868 | X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { |
| 2869 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| 2870 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
| 2871 | DAG.getTargetConstantPool(CP->get(), getPointerTy(), |
| 2872 | CP->getAlignment())); |
| 2873 | if (Subtarget->isTargetDarwin()) { |
| 2874 | // With PIC, the address is actually $g + Offset. |
| 2875 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
| 2876 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 2877 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result); |
| 2878 | } |
| 2879 | |
| 2880 | return Result; |
| 2881 | } |
| 2882 | |
| 2883 | SDOperand |
| 2884 | X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) { |
| 2885 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 2886 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2887 | DAG.getTargetGlobalAddress(GV, |
| 2888 | getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2889 | if (Subtarget->isTargetDarwin()) { |
| 2890 | // With PIC, the address is actually $g + Offset. |
| 2891 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
| 2892 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2893 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 2894 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2895 | |
| 2896 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 2897 | // the value at address GV, not the value of GV itself. This means that |
| 2898 | // the GlobalAddress must be in the base or index register of the address, |
| 2899 | // not the GV offset field. |
| 2900 | if (getTargetMachine().getRelocationModel() != Reloc::Static && |
| 2901 | DarwinGVRequiresExtraLoad(GV)) |
| 2902 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), |
| 2903 | Result, DAG.getSrcValue(NULL)); |
| 2904 | } |
| 2905 | |
| 2906 | return Result; |
| 2907 | } |
| 2908 | |
| 2909 | SDOperand |
| 2910 | X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) { |
| 2911 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 2912 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2913 | DAG.getTargetExternalSymbol(Sym, |
| 2914 | getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2915 | if (Subtarget->isTargetDarwin()) { |
| 2916 | // With PIC, the address is actually $g + Offset. |
| 2917 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
| 2918 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2919 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 2920 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | return Result; |
| 2924 | } |
| 2925 | |
| 2926 | SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2927 | assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && |
| 2928 | "Not an i64 shift!"); |
| 2929 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| 2930 | SDOperand ShOpLo = Op.getOperand(0); |
| 2931 | SDOperand ShOpHi = Op.getOperand(1); |
| 2932 | SDOperand ShAmt = Op.getOperand(2); |
| 2933 | SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, |
Evan Cheng | 99fa0a1 | 2006-01-18 09:26:46 +0000 | [diff] [blame] | 2934 | DAG.getConstant(31, MVT::i8)) |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2935 | : DAG.getConstant(0, MVT::i32); |
| 2936 | |
| 2937 | SDOperand Tmp2, Tmp3; |
| 2938 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 2939 | Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); |
| 2940 | Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); |
| 2941 | } else { |
| 2942 | Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); |
Evan Cheng | b7b5706 | 2006-01-19 01:46:14 +0000 | [diff] [blame] | 2943 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag, |
| 2947 | ShAmt, DAG.getConstant(32, MVT::i8)); |
| 2948 | |
| 2949 | SDOperand Hi, Lo; |
Evan Cheng | 82a24b9 | 2006-01-09 20:49:21 +0000 | [diff] [blame] | 2950 | SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2951 | |
| 2952 | std::vector<MVT::ValueType> Tys; |
| 2953 | Tys.push_back(MVT::i32); |
| 2954 | Tys.push_back(MVT::Flag); |
| 2955 | std::vector<SDOperand> Ops; |
| 2956 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 2957 | Ops.push_back(Tmp2); |
| 2958 | Ops.push_back(Tmp3); |
| 2959 | Ops.push_back(CC); |
| 2960 | Ops.push_back(InFlag); |
| 2961 | Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 2962 | InFlag = Hi.getValue(1); |
| 2963 | |
| 2964 | Ops.clear(); |
| 2965 | Ops.push_back(Tmp3); |
| 2966 | Ops.push_back(Tmp1); |
| 2967 | Ops.push_back(CC); |
| 2968 | Ops.push_back(InFlag); |
| 2969 | Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 2970 | } else { |
| 2971 | Ops.push_back(Tmp2); |
| 2972 | Ops.push_back(Tmp3); |
| 2973 | Ops.push_back(CC); |
Evan Cheng | 910cd3c | 2006-01-09 22:29:54 +0000 | [diff] [blame] | 2974 | Ops.push_back(InFlag); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2975 | Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 2976 | InFlag = Lo.getValue(1); |
| 2977 | |
| 2978 | Ops.clear(); |
| 2979 | Ops.push_back(Tmp3); |
| 2980 | Ops.push_back(Tmp1); |
| 2981 | Ops.push_back(CC); |
| 2982 | Ops.push_back(InFlag); |
| 2983 | Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 2984 | } |
| 2985 | |
| 2986 | Tys.clear(); |
| 2987 | Tys.push_back(MVT::i32); |
| 2988 | Tys.push_back(MVT::i32); |
| 2989 | Ops.clear(); |
| 2990 | Ops.push_back(Lo); |
| 2991 | Ops.push_back(Hi); |
| 2992 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2993 | } |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 2994 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2995 | SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { |
| 2996 | assert(Op.getOperand(0).getValueType() <= MVT::i64 && |
| 2997 | Op.getOperand(0).getValueType() >= MVT::i16 && |
| 2998 | "Unknown SINT_TO_FP to lower!"); |
| 2999 | |
| 3000 | SDOperand Result; |
| 3001 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3002 | unsigned Size = MVT::getSizeInBits(SrcVT)/8; |
| 3003 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3004 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 3005 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3006 | SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, |
| 3007 | DAG.getEntryNode(), Op.getOperand(0), |
| 3008 | StackSlot, DAG.getSrcValue(NULL)); |
| 3009 | |
| 3010 | // Build the FILD |
| 3011 | std::vector<MVT::ValueType> Tys; |
| 3012 | Tys.push_back(MVT::f64); |
| 3013 | Tys.push_back(MVT::Other); |
| 3014 | if (X86ScalarSSE) Tys.push_back(MVT::Flag); |
| 3015 | std::vector<SDOperand> Ops; |
| 3016 | Ops.push_back(Chain); |
| 3017 | Ops.push_back(StackSlot); |
| 3018 | Ops.push_back(DAG.getValueType(SrcVT)); |
| 3019 | Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, |
| 3020 | Tys, Ops); |
| 3021 | |
| 3022 | if (X86ScalarSSE) { |
| 3023 | Chain = Result.getValue(1); |
| 3024 | SDOperand InFlag = Result.getValue(2); |
| 3025 | |
| 3026 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 3027 | // shouldn't be necessary except that RFP cannot be live across |
| 3028 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3029 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3030 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3031 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3032 | std::vector<MVT::ValueType> Tys; |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 3033 | Tys.push_back(MVT::Other); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3034 | std::vector<SDOperand> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3035 | Ops.push_back(Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3036 | Ops.push_back(Result); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3037 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3038 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 3039 | Ops.push_back(InFlag); |
| 3040 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 3041 | Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, |
| 3042 | DAG.getSrcValue(NULL)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3043 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3044 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3045 | return Result; |
| 3046 | } |
| 3047 | |
| 3048 | SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) { |
| 3049 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
| 3050 | "Unknown FP_TO_SINT to lower!"); |
| 3051 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 3052 | // stack slot. |
| 3053 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3054 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3055 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3056 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3057 | |
| 3058 | unsigned Opc; |
| 3059 | switch (Op.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3060 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 3061 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 3062 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 3063 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3064 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3065 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3066 | SDOperand Chain = DAG.getEntryNode(); |
| 3067 | SDOperand Value = Op.getOperand(0); |
| 3068 | if (X86ScalarSSE) { |
| 3069 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
| 3070 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, |
| 3071 | DAG.getSrcValue(0)); |
| 3072 | std::vector<MVT::ValueType> Tys; |
| 3073 | Tys.push_back(MVT::f64); |
| 3074 | Tys.push_back(MVT::Other); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3075 | std::vector<SDOperand> Ops; |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 3076 | Ops.push_back(Chain); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3077 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3078 | Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType())); |
| 3079 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops); |
| 3080 | Chain = Value.getValue(1); |
| 3081 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3082 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3083 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3084 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3085 | // Build the FP_TO_INT*_IN_MEM |
| 3086 | std::vector<SDOperand> Ops; |
| 3087 | Ops.push_back(Chain); |
| 3088 | Ops.push_back(Value); |
| 3089 | Ops.push_back(StackSlot); |
| 3090 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3091 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3092 | // Load the result. |
| 3093 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, |
| 3094 | DAG.getSrcValue(NULL)); |
| 3095 | } |
| 3096 | |
| 3097 | SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) { |
| 3098 | MVT::ValueType VT = Op.getValueType(); |
| 3099 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 3100 | std::vector<Constant*> CV; |
| 3101 | if (VT == MVT::f64) { |
| 3102 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)))); |
| 3103 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3104 | } else { |
| 3105 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)))); |
| 3106 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3107 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3108 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3109 | } |
| 3110 | Constant *CS = ConstantStruct::get(CV); |
| 3111 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 3112 | SDOperand Mask |
| 3113 | = DAG.getNode(X86ISD::LOAD_PACK, |
| 3114 | VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
| 3115 | return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); |
| 3116 | } |
| 3117 | |
| 3118 | SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) { |
| 3119 | MVT::ValueType VT = Op.getValueType(); |
| 3120 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 3121 | std::vector<Constant*> CV; |
| 3122 | if (VT == MVT::f64) { |
| 3123 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63))); |
| 3124 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3125 | } else { |
| 3126 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31))); |
| 3127 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3128 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3129 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3130 | } |
| 3131 | Constant *CS = ConstantStruct::get(CV); |
| 3132 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 3133 | SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, |
| 3134 | VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
| 3135 | return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); |
| 3136 | } |
| 3137 | |
| 3138 | SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) { |
| 3139 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
| 3140 | SDOperand Cond; |
| 3141 | SDOperand CC = Op.getOperand(2); |
| 3142 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| 3143 | bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); |
| 3144 | bool Flip; |
| 3145 | unsigned X86CC; |
| 3146 | if (translateX86CC(CC, isFP, X86CC, Flip)) { |
| 3147 | if (Flip) |
| 3148 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 3149 | Op.getOperand(1), Op.getOperand(0)); |
| 3150 | else |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3151 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 3152 | Op.getOperand(0), Op.getOperand(1)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3153 | return DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 3154 | DAG.getConstant(X86CC, MVT::i8), Cond); |
| 3155 | } else { |
| 3156 | assert(isFP && "Illegal integer SetCC!"); |
| 3157 | |
| 3158 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 3159 | Op.getOperand(0), Op.getOperand(1)); |
| 3160 | std::vector<MVT::ValueType> Tys; |
| 3161 | std::vector<SDOperand> Ops; |
| 3162 | switch (SetCCOpcode) { |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3163 | default: assert(false && "Illegal floating point SetCC!"); |
| 3164 | case ISD::SETOEQ: { // !PF & ZF |
| 3165 | Tys.push_back(MVT::i8); |
| 3166 | Tys.push_back(MVT::Flag); |
| 3167 | Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8)); |
| 3168 | Ops.push_back(Cond); |
| 3169 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 3170 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 3171 | DAG.getConstant(X86ISD::COND_E, MVT::i8), |
| 3172 | Tmp1.getValue(1)); |
| 3173 | return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); |
| 3174 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3175 | case ISD::SETUNE: { // PF | !ZF |
| 3176 | Tys.push_back(MVT::i8); |
| 3177 | Tys.push_back(MVT::Flag); |
| 3178 | Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8)); |
| 3179 | Ops.push_back(Cond); |
| 3180 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 3181 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 3182 | DAG.getConstant(X86ISD::COND_NE, MVT::i8), |
| 3183 | Tmp1.getValue(1)); |
| 3184 | return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); |
| 3185 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3186 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 3187 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3188 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3189 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3190 | SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) { |
| 3191 | MVT::ValueType VT = Op.getValueType(); |
| 3192 | bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE; |
| 3193 | bool addTest = false; |
| 3194 | SDOperand Op0 = Op.getOperand(0); |
| 3195 | SDOperand Cond, CC; |
| 3196 | if (Op0.getOpcode() == ISD::SETCC) |
| 3197 | Op0 = LowerOperation(Op0, DAG); |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3198 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3199 | if (Op0.getOpcode() == X86ISD::SETCC) { |
| 3200 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 3201 | // (since flag operand cannot be shared). If the X86ISD::SETCC does not |
| 3202 | // have another use it will be eliminated. |
| 3203 | // If the X86ISD::SETCC has more than one use, then it's probably better |
| 3204 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 3205 | // pressure reason). |
| 3206 | unsigned CmpOpc = Op0.getOperand(1).getOpcode(); |
| 3207 | if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI || |
| 3208 | CmpOpc == X86ISD::UCOMI) { |
| 3209 | if (!Op0.hasOneUse()) { |
| 3210 | std::vector<MVT::ValueType> Tys; |
| 3211 | for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i) |
| 3212 | Tys.push_back(Op0.Val->getValueType(i)); |
| 3213 | std::vector<SDOperand> Ops; |
| 3214 | for (unsigned i = 0; i < Op0.getNumOperands(); ++i) |
| 3215 | Ops.push_back(Op0.getOperand(i)); |
| 3216 | Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 3217 | } |
| 3218 | |
| 3219 | CC = Op0.getOperand(0); |
| 3220 | Cond = Op0.getOperand(1); |
| 3221 | // Make a copy as flag result cannot be used by more than one. |
| 3222 | Cond = DAG.getNode(CmpOpc, MVT::Flag, |
| 3223 | Cond.getOperand(0), Cond.getOperand(1)); |
| 3224 | addTest = |
| 3225 | isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 3226 | } else |
| 3227 | addTest = true; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3228 | } else |
| 3229 | addTest = true; |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3230 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3231 | if (addTest) { |
| 3232 | CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
| 3233 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 3234 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3235 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3236 | std::vector<MVT::ValueType> Tys; |
| 3237 | Tys.push_back(Op.getValueType()); |
| 3238 | Tys.push_back(MVT::Flag); |
| 3239 | std::vector<SDOperand> Ops; |
| 3240 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 3241 | // condition is true. |
| 3242 | Ops.push_back(Op.getOperand(2)); |
| 3243 | Ops.push_back(Op.getOperand(1)); |
| 3244 | Ops.push_back(CC); |
| 3245 | Ops.push_back(Cond); |
| 3246 | return DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 3247 | } |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3248 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3249 | SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) { |
| 3250 | bool addTest = false; |
| 3251 | SDOperand Cond = Op.getOperand(1); |
| 3252 | SDOperand Dest = Op.getOperand(2); |
| 3253 | SDOperand CC; |
| 3254 | if (Cond.getOpcode() == ISD::SETCC) |
| 3255 | Cond = LowerOperation(Cond, DAG); |
| 3256 | |
| 3257 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 3258 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 3259 | // (since flag operand cannot be shared). If the X86ISD::SETCC does not |
| 3260 | // have another use it will be eliminated. |
| 3261 | // If the X86ISD::SETCC has more than one use, then it's probably better |
| 3262 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 3263 | // pressure reason). |
| 3264 | unsigned CmpOpc = Cond.getOperand(1).getOpcode(); |
| 3265 | if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI || |
| 3266 | CmpOpc == X86ISD::UCOMI) { |
| 3267 | if (!Cond.hasOneUse()) { |
| 3268 | std::vector<MVT::ValueType> Tys; |
| 3269 | for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i) |
| 3270 | Tys.push_back(Cond.Val->getValueType(i)); |
| 3271 | std::vector<SDOperand> Ops; |
| 3272 | for (unsigned i = 0; i < Cond.getNumOperands(); ++i) |
| 3273 | Ops.push_back(Cond.getOperand(i)); |
| 3274 | Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 3275 | } |
| 3276 | |
| 3277 | CC = Cond.getOperand(0); |
| 3278 | Cond = Cond.getOperand(1); |
| 3279 | // Make a copy as flag result cannot be used by more than one. |
| 3280 | Cond = DAG.getNode(CmpOpc, MVT::Flag, |
| 3281 | Cond.getOperand(0), Cond.getOperand(1)); |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 3282 | } else |
| 3283 | addTest = true; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3284 | } else |
| 3285 | addTest = true; |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 3286 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3287 | if (addTest) { |
| 3288 | CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
| 3289 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond); |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 3290 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3291 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
| 3292 | Op.getOperand(0), Op.getOperand(2), CC, Cond); |
| 3293 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 3294 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3295 | SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) { |
| 3296 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 3297 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
| 3298 | DAG.getTargetJumpTable(JT->getIndex(), |
| 3299 | getPointerTy())); |
| 3300 | if (Subtarget->isTargetDarwin()) { |
| 3301 | // With PIC, the address is actually $g + Offset. |
| 3302 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
| 3303 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3304 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3305 | Result); |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 3306 | } |
Evan Cheng | bbbb2fb | 2006-02-25 09:55:19 +0000 | [diff] [blame] | 3307 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3308 | return Result; |
| 3309 | } |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 3310 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3311 | SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
| 3312 | unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3313 | if (CallingConv == CallingConv::Fast && EnableFastCC) |
| 3314 | return LowerFastCCCallTo(Op, DAG); |
| 3315 | else |
| 3316 | return LowerCCCCallTo(Op, DAG); |
| 3317 | } |
| 3318 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3319 | SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 3320 | SDOperand Copy; |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3321 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3322 | switch(Op.getNumOperands()) { |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3323 | default: |
| 3324 | assert(0 && "Do not know how to return this many arguments!"); |
| 3325 | abort(); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3326 | case 1: // ret void. |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3327 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0), |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3328 | DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3329 | case 2: { |
| 3330 | MVT::ValueType ArgVT = Op.getOperand(1).getValueType(); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3331 | |
| 3332 | if (MVT::isVector(ArgVT)) { |
| 3333 | // Integer or FP vector result -> XMM0. |
| 3334 | if (DAG.getMachineFunction().liveout_empty()) |
| 3335 | DAG.getMachineFunction().addLiveOut(X86::XMM0); |
| 3336 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1), |
| 3337 | SDOperand()); |
| 3338 | } else if (MVT::isInteger(ArgVT)) { |
| 3339 | // Integer result -> EAX |
| 3340 | if (DAG.getMachineFunction().liveout_empty()) |
| 3341 | DAG.getMachineFunction().addLiveOut(X86::EAX); |
| 3342 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3343 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1), |
| 3344 | SDOperand()); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3345 | } else if (!X86ScalarSSE) { |
| 3346 | // FP return with fp-stack value. |
| 3347 | if (DAG.getMachineFunction().liveout_empty()) |
| 3348 | DAG.getMachineFunction().addLiveOut(X86::ST0); |
| 3349 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3350 | std::vector<MVT::ValueType> Tys; |
| 3351 | Tys.push_back(MVT::Other); |
| 3352 | Tys.push_back(MVT::Flag); |
| 3353 | std::vector<SDOperand> Ops; |
| 3354 | Ops.push_back(Op.getOperand(0)); |
| 3355 | Ops.push_back(Op.getOperand(1)); |
| 3356 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 3357 | } else { |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3358 | // FP return with ScalarSSE (return on fp-stack). |
| 3359 | if (DAG.getMachineFunction().liveout_empty()) |
| 3360 | DAG.getMachineFunction().addLiveOut(X86::ST0); |
| 3361 | |
Evan Cheng | 0d084c9 | 2006-02-01 00:20:21 +0000 | [diff] [blame] | 3362 | SDOperand MemLoc; |
| 3363 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 3364 | SDOperand Value = Op.getOperand(1); |
| 3365 | |
Evan Cheng | 760df29 | 2006-02-01 01:19:32 +0000 | [diff] [blame] | 3366 | if (Value.getOpcode() == ISD::LOAD && |
| 3367 | (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) { |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 3368 | Chain = Value.getOperand(0); |
| 3369 | MemLoc = Value.getOperand(1); |
| 3370 | } else { |
| 3371 | // Spill the value to memory and reload it into top of stack. |
| 3372 | unsigned Size = MVT::getSizeInBits(ArgVT)/8; |
| 3373 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3374 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 3375 | MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3376 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), |
| 3377 | Value, MemLoc, DAG.getSrcValue(0)); |
| 3378 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3379 | std::vector<MVT::ValueType> Tys; |
| 3380 | Tys.push_back(MVT::f64); |
| 3381 | Tys.push_back(MVT::Other); |
| 3382 | std::vector<SDOperand> Ops; |
| 3383 | Ops.push_back(Chain); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 3384 | Ops.push_back(MemLoc); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3385 | Ops.push_back(DAG.getValueType(ArgVT)); |
| 3386 | Copy = DAG.getNode(X86ISD::FLD, Tys, Ops); |
| 3387 | Tys.clear(); |
| 3388 | Tys.push_back(MVT::Other); |
| 3389 | Tys.push_back(MVT::Flag); |
| 3390 | Ops.clear(); |
| 3391 | Ops.push_back(Copy.getValue(1)); |
| 3392 | Ops.push_back(Copy); |
| 3393 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 3394 | } |
| 3395 | break; |
| 3396 | } |
| 3397 | case 3: |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3398 | if (DAG.getMachineFunction().liveout_empty()) { |
| 3399 | DAG.getMachineFunction().addLiveOut(X86::EAX); |
| 3400 | DAG.getMachineFunction().addLiveOut(X86::EDX); |
| 3401 | } |
| 3402 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3403 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), |
| 3404 | SDOperand()); |
| 3405 | Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1)); |
| 3406 | break; |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3407 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3408 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, |
| 3409 | Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16), |
| 3410 | Copy.getValue(1)); |
| 3411 | } |
| 3412 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 3413 | SDOperand |
| 3414 | X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 3415 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3416 | if (CC == CallingConv::Fast && EnableFastCC) |
| 3417 | return LowerFastCCArguments(Op, DAG); |
| 3418 | else |
| 3419 | return LowerCCCArguments(Op, DAG); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3422 | SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) { |
| 3423 | SDOperand InFlag(0, 0); |
| 3424 | SDOperand Chain = Op.getOperand(0); |
| 3425 | unsigned Align = |
| 3426 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 3427 | if (Align == 0) Align = 1; |
| 3428 | |
| 3429 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 3430 | // If not DWORD aligned, call memset if size is less than the threshold. |
| 3431 | // It knows how to align to the right boundary first. |
| 3432 | if ((Align & 3) != 0 || |
| 3433 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
| 3434 | MVT::ValueType IntPtr = getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3435 | const Type *IntPtrTy = getTargetData()->getIntPtrType(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3436 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 3437 | Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy)); |
| 3438 | // Extend the ubyte argument to be an int value for the call. |
| 3439 | SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); |
| 3440 | Args.push_back(std::make_pair(Val, IntPtrTy)); |
| 3441 | Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy)); |
| 3442 | std::pair<SDOperand,SDOperand> CallResult = |
| 3443 | LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false, |
| 3444 | DAG.getExternalSymbol("memset", IntPtr), Args, DAG); |
| 3445 | return CallResult.second; |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 3446 | } |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 3447 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3448 | MVT::ValueType AVT; |
| 3449 | SDOperand Count; |
| 3450 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 3451 | unsigned BytesLeft = 0; |
| 3452 | bool TwoRepStos = false; |
| 3453 | if (ValC) { |
| 3454 | unsigned ValReg; |
| 3455 | unsigned Val = ValC->getValue() & 255; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3456 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3457 | // If the value is a constant, then we can potentially use larger sets. |
| 3458 | switch (Align & 3) { |
| 3459 | case 2: // WORD aligned |
| 3460 | AVT = MVT::i16; |
| 3461 | Count = DAG.getConstant(I->getValue() / 2, MVT::i32); |
| 3462 | BytesLeft = I->getValue() % 2; |
| 3463 | Val = (Val << 8) | Val; |
| 3464 | ValReg = X86::AX; |
| 3465 | break; |
| 3466 | case 0: // DWORD aligned |
| 3467 | AVT = MVT::i32; |
| 3468 | if (I) { |
| 3469 | Count = DAG.getConstant(I->getValue() / 4, MVT::i32); |
| 3470 | BytesLeft = I->getValue() % 4; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 3471 | } else { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3472 | Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3), |
| 3473 | DAG.getConstant(2, MVT::i8)); |
| 3474 | TwoRepStos = true; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 3475 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3476 | Val = (Val << 8) | Val; |
| 3477 | Val = (Val << 16) | Val; |
| 3478 | ValReg = X86::EAX; |
| 3479 | break; |
| 3480 | default: // Byte aligned |
| 3481 | AVT = MVT::i8; |
| 3482 | Count = Op.getOperand(3); |
| 3483 | ValReg = X86::AL; |
| 3484 | break; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3487 | Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), |
| 3488 | InFlag); |
| 3489 | InFlag = Chain.getValue(1); |
| 3490 | } else { |
| 3491 | AVT = MVT::i8; |
| 3492 | Count = Op.getOperand(3); |
| 3493 | Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); |
| 3494 | InFlag = Chain.getValue(1); |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 3495 | } |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3496 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3497 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag); |
| 3498 | InFlag = Chain.getValue(1); |
| 3499 | Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag); |
| 3500 | InFlag = Chain.getValue(1); |
Evan Cheng | a0b3afb | 2006-03-27 07:00:16 +0000 | [diff] [blame] | 3501 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3502 | std::vector<MVT::ValueType> Tys; |
| 3503 | Tys.push_back(MVT::Other); |
| 3504 | Tys.push_back(MVT::Flag); |
| 3505 | std::vector<SDOperand> Ops; |
| 3506 | Ops.push_back(Chain); |
| 3507 | Ops.push_back(DAG.getValueType(AVT)); |
| 3508 | Ops.push_back(InFlag); |
| 3509 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 3510 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3511 | if (TwoRepStos) { |
| 3512 | InFlag = Chain.getValue(1); |
| 3513 | Count = Op.getOperand(3); |
| 3514 | MVT::ValueType CVT = Count.getValueType(); |
| 3515 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
| 3516 | DAG.getConstant(3, CVT)); |
| 3517 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag); |
| 3518 | InFlag = Chain.getValue(1); |
| 3519 | Tys.clear(); |
| 3520 | Tys.push_back(MVT::Other); |
| 3521 | Tys.push_back(MVT::Flag); |
| 3522 | Ops.clear(); |
| 3523 | Ops.push_back(Chain); |
| 3524 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 3525 | Ops.push_back(InFlag); |
| 3526 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops); |
| 3527 | } else if (BytesLeft) { |
| 3528 | // Issue stores for the last 1 - 3 bytes. |
| 3529 | SDOperand Value; |
| 3530 | unsigned Val = ValC->getValue() & 255; |
| 3531 | unsigned Offset = I->getValue() - BytesLeft; |
| 3532 | SDOperand DstAddr = Op.getOperand(1); |
| 3533 | MVT::ValueType AddrVT = DstAddr.getValueType(); |
| 3534 | if (BytesLeft >= 2) { |
| 3535 | Value = DAG.getConstant((Val << 8) | Val, MVT::i16); |
| 3536 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 3537 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 3538 | DAG.getConstant(Offset, AddrVT)), |
| 3539 | DAG.getSrcValue(NULL)); |
| 3540 | BytesLeft -= 2; |
| 3541 | Offset += 2; |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3544 | if (BytesLeft == 1) { |
| 3545 | Value = DAG.getConstant(Val, MVT::i8); |
| 3546 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 3547 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 3548 | DAG.getConstant(Offset, AddrVT)), |
| 3549 | DAG.getSrcValue(NULL)); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 3550 | } |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 3551 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3552 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3553 | return Chain; |
| 3554 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3555 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3556 | SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) { |
| 3557 | SDOperand Chain = Op.getOperand(0); |
| 3558 | unsigned Align = |
| 3559 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 3560 | if (Align == 0) Align = 1; |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 3561 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3562 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 3563 | // If not DWORD aligned, call memcpy if size is less than the threshold. |
| 3564 | // It knows how to align to the right boundary first. |
| 3565 | if ((Align & 3) != 0 || |
| 3566 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
| 3567 | MVT::ValueType IntPtr = getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3568 | const Type *IntPtrTy = getTargetData()->getIntPtrType(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3569 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 3570 | Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy)); |
| 3571 | Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy)); |
| 3572 | Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy)); |
| 3573 | std::pair<SDOperand,SDOperand> CallResult = |
| 3574 | LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false, |
| 3575 | DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); |
| 3576 | return CallResult.second; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 3577 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3578 | |
| 3579 | MVT::ValueType AVT; |
| 3580 | SDOperand Count; |
| 3581 | unsigned BytesLeft = 0; |
| 3582 | bool TwoRepMovs = false; |
| 3583 | switch (Align & 3) { |
| 3584 | case 2: // WORD aligned |
| 3585 | AVT = MVT::i16; |
| 3586 | Count = DAG.getConstant(I->getValue() / 2, MVT::i32); |
| 3587 | BytesLeft = I->getValue() % 2; |
| 3588 | break; |
| 3589 | case 0: // DWORD aligned |
| 3590 | AVT = MVT::i32; |
| 3591 | if (I) { |
| 3592 | Count = DAG.getConstant(I->getValue() / 4, MVT::i32); |
| 3593 | BytesLeft = I->getValue() % 4; |
Evan Cheng | cdfc3c8 | 2006-04-17 22:45:49 +0000 | [diff] [blame] | 3594 | } else { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3595 | Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3), |
| 3596 | DAG.getConstant(2, MVT::i8)); |
| 3597 | TwoRepMovs = true; |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 3598 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3599 | break; |
| 3600 | default: // Byte aligned |
| 3601 | AVT = MVT::i8; |
| 3602 | Count = Op.getOperand(3); |
| 3603 | break; |
| 3604 | } |
| 3605 | |
| 3606 | SDOperand InFlag(0, 0); |
| 3607 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag); |
| 3608 | InFlag = Chain.getValue(1); |
| 3609 | Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag); |
| 3610 | InFlag = Chain.getValue(1); |
| 3611 | Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag); |
| 3612 | InFlag = Chain.getValue(1); |
| 3613 | |
| 3614 | std::vector<MVT::ValueType> Tys; |
| 3615 | Tys.push_back(MVT::Other); |
| 3616 | Tys.push_back(MVT::Flag); |
| 3617 | std::vector<SDOperand> Ops; |
| 3618 | Ops.push_back(Chain); |
| 3619 | Ops.push_back(DAG.getValueType(AVT)); |
| 3620 | Ops.push_back(InFlag); |
| 3621 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops); |
| 3622 | |
| 3623 | if (TwoRepMovs) { |
| 3624 | InFlag = Chain.getValue(1); |
| 3625 | Count = Op.getOperand(3); |
| 3626 | MVT::ValueType CVT = Count.getValueType(); |
| 3627 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
| 3628 | DAG.getConstant(3, CVT)); |
| 3629 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag); |
| 3630 | InFlag = Chain.getValue(1); |
| 3631 | Tys.clear(); |
| 3632 | Tys.push_back(MVT::Other); |
| 3633 | Tys.push_back(MVT::Flag); |
| 3634 | Ops.clear(); |
| 3635 | Ops.push_back(Chain); |
| 3636 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 3637 | Ops.push_back(InFlag); |
| 3638 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops); |
| 3639 | } else if (BytesLeft) { |
| 3640 | // Issue loads and stores for the last 1 - 3 bytes. |
| 3641 | unsigned Offset = I->getValue() - BytesLeft; |
| 3642 | SDOperand DstAddr = Op.getOperand(1); |
| 3643 | MVT::ValueType DstVT = DstAddr.getValueType(); |
| 3644 | SDOperand SrcAddr = Op.getOperand(2); |
| 3645 | MVT::ValueType SrcVT = SrcAddr.getValueType(); |
| 3646 | SDOperand Value; |
| 3647 | if (BytesLeft >= 2) { |
| 3648 | Value = DAG.getLoad(MVT::i16, Chain, |
| 3649 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 3650 | DAG.getConstant(Offset, SrcVT)), |
| 3651 | DAG.getSrcValue(NULL)); |
| 3652 | Chain = Value.getValue(1); |
| 3653 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 3654 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 3655 | DAG.getConstant(Offset, DstVT)), |
| 3656 | DAG.getSrcValue(NULL)); |
| 3657 | BytesLeft -= 2; |
| 3658 | Offset += 2; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3661 | if (BytesLeft == 1) { |
| 3662 | Value = DAG.getLoad(MVT::i8, Chain, |
| 3663 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 3664 | DAG.getConstant(Offset, SrcVT)), |
| 3665 | DAG.getSrcValue(NULL)); |
| 3666 | Chain = Value.getValue(1); |
| 3667 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 3668 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 3669 | DAG.getConstant(Offset, DstVT)), |
| 3670 | DAG.getSrcValue(NULL)); |
| 3671 | } |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 3672 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3673 | |
| 3674 | return Chain; |
| 3675 | } |
| 3676 | |
| 3677 | SDOperand |
| 3678 | X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) { |
| 3679 | std::vector<MVT::ValueType> Tys; |
| 3680 | Tys.push_back(MVT::Other); |
| 3681 | Tys.push_back(MVT::Flag); |
| 3682 | std::vector<SDOperand> Ops; |
| 3683 | Ops.push_back(Op.getOperand(0)); |
| 3684 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops); |
| 3685 | Ops.clear(); |
| 3686 | Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1))); |
| 3687 | Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, |
| 3688 | MVT::i32, Ops[0].getValue(2))); |
| 3689 | Ops.push_back(Ops[1].getValue(1)); |
| 3690 | Tys[0] = Tys[1] = MVT::i32; |
| 3691 | Tys.push_back(MVT::Other); |
| 3692 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops); |
| 3693 | } |
| 3694 | |
| 3695 | SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) { |
| 3696 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 3697 | // memory location argument. |
| 3698 | // FIXME: Replace MVT::i32 with PointerTy |
| 3699 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 3700 | return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, |
| 3701 | Op.getOperand(1), Op.getOperand(2)); |
| 3702 | } |
| 3703 | |
| 3704 | SDOperand |
| 3705 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) { |
| 3706 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue(); |
| 3707 | switch (IntNo) { |
| 3708 | default: return SDOperand(); // Don't custom lower most intrinsics. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3709 | // Comparison intrinsics. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3710 | case Intrinsic::x86_sse_comieq_ss: |
| 3711 | case Intrinsic::x86_sse_comilt_ss: |
| 3712 | case Intrinsic::x86_sse_comile_ss: |
| 3713 | case Intrinsic::x86_sse_comigt_ss: |
| 3714 | case Intrinsic::x86_sse_comige_ss: |
| 3715 | case Intrinsic::x86_sse_comineq_ss: |
| 3716 | case Intrinsic::x86_sse_ucomieq_ss: |
| 3717 | case Intrinsic::x86_sse_ucomilt_ss: |
| 3718 | case Intrinsic::x86_sse_ucomile_ss: |
| 3719 | case Intrinsic::x86_sse_ucomigt_ss: |
| 3720 | case Intrinsic::x86_sse_ucomige_ss: |
| 3721 | case Intrinsic::x86_sse_ucomineq_ss: |
| 3722 | case Intrinsic::x86_sse2_comieq_sd: |
| 3723 | case Intrinsic::x86_sse2_comilt_sd: |
| 3724 | case Intrinsic::x86_sse2_comile_sd: |
| 3725 | case Intrinsic::x86_sse2_comigt_sd: |
| 3726 | case Intrinsic::x86_sse2_comige_sd: |
| 3727 | case Intrinsic::x86_sse2_comineq_sd: |
| 3728 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 3729 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 3730 | case Intrinsic::x86_sse2_ucomile_sd: |
| 3731 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 3732 | case Intrinsic::x86_sse2_ucomige_sd: |
| 3733 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 3734 | unsigned Opc = 0; |
| 3735 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 3736 | switch (IntNo) { |
| 3737 | default: break; |
| 3738 | case Intrinsic::x86_sse_comieq_ss: |
| 3739 | case Intrinsic::x86_sse2_comieq_sd: |
| 3740 | Opc = X86ISD::COMI; |
| 3741 | CC = ISD::SETEQ; |
| 3742 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3743 | case Intrinsic::x86_sse_comilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3744 | case Intrinsic::x86_sse2_comilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3745 | Opc = X86ISD::COMI; |
| 3746 | CC = ISD::SETLT; |
| 3747 | break; |
| 3748 | case Intrinsic::x86_sse_comile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3749 | case Intrinsic::x86_sse2_comile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3750 | Opc = X86ISD::COMI; |
| 3751 | CC = ISD::SETLE; |
| 3752 | break; |
| 3753 | case Intrinsic::x86_sse_comigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3754 | case Intrinsic::x86_sse2_comigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3755 | Opc = X86ISD::COMI; |
| 3756 | CC = ISD::SETGT; |
| 3757 | break; |
| 3758 | case Intrinsic::x86_sse_comige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3759 | case Intrinsic::x86_sse2_comige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3760 | Opc = X86ISD::COMI; |
| 3761 | CC = ISD::SETGE; |
| 3762 | break; |
| 3763 | case Intrinsic::x86_sse_comineq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3764 | case Intrinsic::x86_sse2_comineq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3765 | Opc = X86ISD::COMI; |
| 3766 | CC = ISD::SETNE; |
| 3767 | break; |
| 3768 | case Intrinsic::x86_sse_ucomieq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3769 | case Intrinsic::x86_sse2_ucomieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3770 | Opc = X86ISD::UCOMI; |
| 3771 | CC = ISD::SETEQ; |
| 3772 | break; |
| 3773 | case Intrinsic::x86_sse_ucomilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3774 | case Intrinsic::x86_sse2_ucomilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3775 | Opc = X86ISD::UCOMI; |
| 3776 | CC = ISD::SETLT; |
| 3777 | break; |
| 3778 | case Intrinsic::x86_sse_ucomile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3779 | case Intrinsic::x86_sse2_ucomile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3780 | Opc = X86ISD::UCOMI; |
| 3781 | CC = ISD::SETLE; |
| 3782 | break; |
| 3783 | case Intrinsic::x86_sse_ucomigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3784 | case Intrinsic::x86_sse2_ucomigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3785 | Opc = X86ISD::UCOMI; |
| 3786 | CC = ISD::SETGT; |
| 3787 | break; |
| 3788 | case Intrinsic::x86_sse_ucomige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3789 | case Intrinsic::x86_sse2_ucomige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3790 | Opc = X86ISD::UCOMI; |
| 3791 | CC = ISD::SETGE; |
| 3792 | break; |
| 3793 | case Intrinsic::x86_sse_ucomineq_ss: |
| 3794 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 3795 | Opc = X86ISD::UCOMI; |
| 3796 | CC = ISD::SETNE; |
| 3797 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3798 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3799 | bool Flip; |
| 3800 | unsigned X86CC; |
| 3801 | translateX86CC(CC, true, X86CC, Flip); |
| 3802 | SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1), |
| 3803 | Op.getOperand(Flip?1:2)); |
| 3804 | SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 3805 | DAG.getConstant(X86CC, MVT::i8), Cond); |
| 3806 | return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC); |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3807 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 3808 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3809 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3810 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3811 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 3812 | /// |
| 3813 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 3814 | switch (Op.getOpcode()) { |
| 3815 | default: assert(0 && "Should not custom lower this!"); |
| 3816 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 3817 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 3818 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 3819 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 3820 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 3821 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 3822 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 3823 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
| 3824 | case ISD::SHL_PARTS: |
| 3825 | case ISD::SRA_PARTS: |
| 3826 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 3827 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
| 3828 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| 3829 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 3830 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
| 3831 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
| 3832 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 3833 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
| 3834 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3835 | case ISD::CALL: return LowerCALL(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3836 | case ISD::RET: return LowerRET(Op, DAG); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 3837 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3838 | case ISD::MEMSET: return LowerMEMSET(Op, DAG); |
| 3839 | case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); |
| 3840 | case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG); |
| 3841 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
| 3842 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 3843 | } |
| 3844 | } |
| 3845 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3846 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 3847 | switch (Opcode) { |
| 3848 | default: return NULL; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3849 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 3850 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 3851 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 3852 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3853 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 3854 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3855 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 3856 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 3857 | 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] | 3858 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 3859 | case X86ISD::FST: return "X86ISD::FST"; |
| 3860 | case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 3861 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3862 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 3863 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 3864 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 3865 | case X86ISD::CMP: return "X86ISD::CMP"; |
| 3866 | case X86ISD::TEST: return "X86ISD::TEST"; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 3867 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 3868 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 3869 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3870 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 3871 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 3872 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 3873 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 3874 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 3875 | case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 3876 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 3877 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Evan Cheng | bc4832b | 2006-03-24 23:15:12 +0000 | [diff] [blame] | 3878 | case X86ISD::S2VEC: return "X86ISD::S2VEC"; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 3879 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 3880 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3883 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 3884 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 3885 | uint64_t Mask, |
| 3886 | uint64_t &KnownZero, |
| 3887 | uint64_t &KnownOne, |
| 3888 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3889 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 3890 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 3891 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 3892 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 3893 | Opc == ISD::INTRINSIC_VOID) && |
| 3894 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 3895 | " is a target node!"); |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3896 | |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 3897 | KnownZero = KnownOne = 0; // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3898 | switch (Opc) { |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 3899 | default: break; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 3900 | case X86ISD::SETCC: |
| 3901 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 3902 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3903 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 3904 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3905 | |
| 3906 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3907 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 3908 | MVT::ValueType VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3909 | if (Constraint.size() == 1) { |
| 3910 | // FIXME: not handling fp-stack yet! |
| 3911 | // FIXME: not handling MMX registers yet ('y' constraint). |
| 3912 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
| 3913 | default: break; // Unknown constriant letter |
| 3914 | case 'r': // GENERAL_REGS |
| 3915 | case 'R': // LEGACY_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 3916 | if (VT == MVT::i32) |
| 3917 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, |
| 3918 | X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0); |
| 3919 | else if (VT == MVT::i16) |
| 3920 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, |
| 3921 | X86::SI, X86::DI, X86::BP, X86::SP, 0); |
| 3922 | else if (VT == MVT::i8) |
| 3923 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); |
| 3924 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3925 | case 'l': // INDEX_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 3926 | if (VT == MVT::i32) |
| 3927 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, |
| 3928 | X86::ESI, X86::EDI, X86::EBP, 0); |
| 3929 | else if (VT == MVT::i16) |
| 3930 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, |
| 3931 | X86::SI, X86::DI, X86::BP, 0); |
| 3932 | else if (VT == MVT::i8) |
| 3933 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); |
| 3934 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3935 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 3936 | case 'Q': // Q_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 3937 | if (VT == MVT::i32) |
| 3938 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 3939 | else if (VT == MVT::i16) |
| 3940 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 3941 | else if (VT == MVT::i8) |
| 3942 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); |
| 3943 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3944 | case 'x': // SSE_REGS if SSE1 allowed |
| 3945 | if (Subtarget->hasSSE1()) |
| 3946 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 3947 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 3948 | 0); |
| 3949 | return std::vector<unsigned>(); |
| 3950 | case 'Y': // SSE_REGS if SSE2 allowed |
| 3951 | if (Subtarget->hasSSE2()) |
| 3952 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 3953 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 3954 | 0); |
| 3955 | return std::vector<unsigned>(); |
| 3956 | } |
| 3957 | } |
| 3958 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3959 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 3960 | } |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 3961 | |
| 3962 | /// isLegalAddressImmediate - Return true if the integer value or |
| 3963 | /// GlobalValue can be used as the offset of the target addressing mode. |
| 3964 | bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const { |
| 3965 | // X86 allows a sign-extended 32-bit immediate field. |
| 3966 | return (V > -(1LL << 32) && V < (1LL << 32)-1); |
| 3967 | } |
| 3968 | |
| 3969 | bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const { |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 3970 | if (Subtarget->isTargetDarwin()) { |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 3971 | Reloc::Model RModel = getTargetMachine().getRelocationModel(); |
| 3972 | if (RModel == Reloc::Static) |
| 3973 | return true; |
| 3974 | else if (RModel == Reloc::DynamicNoPIC) |
Evan Cheng | 2221de9 | 2006-03-16 22:02:48 +0000 | [diff] [blame] | 3975 | return !DarwinGVRequiresExtraLoad(GV); |
Evan Cheng | 30b37b5 | 2006-03-13 23:18:16 +0000 | [diff] [blame] | 3976 | else |
| 3977 | return false; |
| 3978 | } else |
| 3979 | return true; |
| 3980 | } |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 3981 | |
| 3982 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 3983 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 3984 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 3985 | /// are assumed to be legal. |
Evan Cheng | ca6e8ea | 2006-03-22 22:07:06 +0000 | [diff] [blame] | 3986 | bool |
| 3987 | X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const { |
| 3988 | // Only do shuffles on 128-bit vector types for now. |
| 3989 | if (MVT::getSizeInBits(VT) == 64) return false; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 3990 | return (Mask.Val->getNumOperands() <= 4 || |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 3991 | isSplatMask(Mask.Val) || |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 3992 | isPSHUFHW_PSHUFLWMask(Mask.Val) || |
Evan Cheng | ed4ca7f | 2006-03-28 08:27:15 +0000 | [diff] [blame] | 3993 | X86::isUNPCKLMask(Mask.Val) || |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 3994 | X86::isUNPCKL_v_undef_Mask(Mask.Val) || |
Jim Laskey | 2d2a613 | 2006-03-28 10:17:11 +0000 | [diff] [blame] | 3995 | X86::isUNPCKHMask(Mask.Val)); |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 3996 | } |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3997 | |
| 3998 | bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps, |
| 3999 | MVT::ValueType EVT, |
| 4000 | SelectionDAG &DAG) const { |
| 4001 | unsigned NumElts = BVOps.size(); |
| 4002 | // Only do shuffles on 128-bit vector types for now. |
| 4003 | if (MVT::getSizeInBits(EVT) * NumElts == 64) return false; |
| 4004 | if (NumElts == 2) return true; |
| 4005 | if (NumElts == 4) { |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 4006 | return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) || |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4007 | isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps)); |
| 4008 | } |
| 4009 | return false; |
| 4010 | } |