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