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