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" |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
| 38 | // FIXME: temporary. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 39 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 40 | cl::desc("Enable fastcc on X86")); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 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(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 45 | X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 46 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 47 | // Set up the TargetLowering object. |
| 48 | |
| 49 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 50 | setShiftAmountType(MVT::i8); |
| 51 | setSetCCResultType(MVT::i8); |
| 52 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 53 | setSchedulingPreference(SchedulingForRegPressure); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 54 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 55 | setStackPointerRegisterToSaveRestore(X86StackPtr); |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 56 | |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 57 | if (Subtarget->isTargetDarwin()) { |
Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 58 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 59 | setUseUnderscoreSetJmp(false); |
| 60 | setUseUnderscoreLongJmp(false); |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 61 | } else if (Subtarget->isTargetMingw()) { |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 62 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 63 | setUseUnderscoreSetJmp(true); |
| 64 | setUseUnderscoreLongJmp(false); |
| 65 | } else { |
| 66 | setUseUnderscoreSetJmp(true); |
| 67 | setUseUnderscoreLongJmp(true); |
| 68 | } |
| 69 | |
Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 70 | // Add legal addressing mode scale values. |
| 71 | addLegalAddressScale(8); |
| 72 | addLegalAddressScale(4); |
| 73 | addLegalAddressScale(2); |
| 74 | // Enter the ones which require both scale + index last. These are more |
| 75 | // expensive. |
| 76 | addLegalAddressScale(9); |
| 77 | addLegalAddressScale(5); |
| 78 | addLegalAddressScale(3); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 79 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 80 | // Set up the register classes. |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 81 | addRegisterClass(MVT::i8, X86::GR8RegisterClass); |
| 82 | addRegisterClass(MVT::i16, X86::GR16RegisterClass); |
| 83 | addRegisterClass(MVT::i32, X86::GR32RegisterClass); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 84 | if (Subtarget->is64Bit()) |
| 85 | addRegisterClass(MVT::i64, X86::GR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 86 | |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 87 | setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 88 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 89 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 90 | // operation. |
| 91 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 92 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 93 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 94 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 95 | if (Subtarget->is64Bit()) { |
| 96 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 97 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 98 | } else { |
| 99 | if (X86ScalarSSE) |
| 100 | // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP. |
| 101 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); |
| 102 | else |
| 103 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| 104 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 105 | |
| 106 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 107 | // this operation. |
| 108 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 109 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 110 | // SSE has no i16 to fp conversion, only i32 |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 111 | if (X86ScalarSSE) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 113 | else { |
| 114 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 115 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 116 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 117 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 118 | if (!Subtarget->is64Bit()) { |
| 119 | // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode. |
| 120 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 121 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 122 | } |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 123 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 124 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 125 | // this operation. |
| 126 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 127 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 128 | |
| 129 | if (X86ScalarSSE) { |
| 130 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| 131 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 132 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 133 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 137 | // conversion. |
| 138 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 139 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 140 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 141 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 142 | if (Subtarget->is64Bit()) { |
| 143 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 144 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 145 | } else { |
| 146 | if (X86ScalarSSE && !Subtarget->hasSSE3()) |
| 147 | // Expand FP_TO_UINT into a select. |
| 148 | // FIXME: We would like to use a Custom expander here eventually to do |
| 149 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 150 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 151 | else |
| 152 | // With SSE3 we can use fisttpll to convert to a signed i64. |
| 153 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 154 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 399610a | 2006-12-05 18:22:22 +0000 | [diff] [blame] | 156 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. |
Chris Lattner | f3597a1 | 2006-12-05 18:45:06 +0000 | [diff] [blame] | 157 | if (!X86ScalarSSE) { |
| 158 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 159 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
| 160 | } |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 161 | |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 162 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 163 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 164 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 165 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 166 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 167 | if (Subtarget->is64Bit()) |
| 168 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 169 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | e80242a | 2005-12-07 17:59:14 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 171 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 172 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 174 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 175 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 176 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 177 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 178 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 179 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 180 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
| 181 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 182 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 183 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 184 | if (Subtarget->is64Bit()) { |
| 185 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
| 186 | setOperationAction(ISD::CTTZ , MVT::i64 , Expand); |
| 187 | setOperationAction(ISD::CTLZ , MVT::i64 , Expand); |
| 188 | } |
| 189 | |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 190 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 192 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 193 | // These should be promoted to a larger select which is supported. |
| 194 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 195 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 196 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 197 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 198 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 199 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 200 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
| 201 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 202 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 203 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 204 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 205 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 206 | if (Subtarget->is64Bit()) { |
| 207 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| 208 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| 209 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 210 | // X86 ret instruction may pop stack. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 211 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 212 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 213 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 214 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 215 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 216 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 217 | if (Subtarget->is64Bit()) { |
| 218 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 219 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 220 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
| 221 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
| 222 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 223 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 224 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 225 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 226 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 227 | // X86 wants to expand memset / memcpy itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 228 | setOperationAction(ISD::MEMSET , MVT::Other, Custom); |
| 229 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 230 | |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 231 | // We don't have line number support yet. |
| 232 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 233 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 234 | // FIXME - use subtarget debug flags |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 235 | if (!Subtarget->isTargetDarwin() && |
| 236 | !Subtarget->isTargetELF() && |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 237 | !Subtarget->isTargetCygMing()) |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 238 | setOperationAction(ISD::LABEL, MVT::Other, Expand); |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 239 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 240 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 241 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 242 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 243 | // Use the default implementation. |
| 244 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 245 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 246 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 247 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 248 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 249 | if (Subtarget->is64Bit()) |
| 250 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 251 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 252 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 253 | if (X86ScalarSSE) { |
| 254 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 255 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 256 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 257 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 258 | // Use ANDPD to simulate FABS. |
| 259 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 260 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 261 | |
| 262 | // Use XORP to simulate FNEG. |
| 263 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 264 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 265 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 266 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| 267 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 268 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| 269 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 270 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 271 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 272 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 273 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 274 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 275 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 276 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 277 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 278 | // Expand FP immediates into loads from the stack, except for the special |
| 279 | // cases we handle. |
| 280 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 281 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 282 | addLegalFPImmediate(+0.0); // xorps / xorpd |
| 283 | } else { |
| 284 | // Set up the FP register classes. |
| 285 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 286 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 287 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 288 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 289 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 290 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 291 | if (!UnsafeFPMath) { |
| 292 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 293 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 294 | } |
| 295 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 296 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 297 | addLegalFPImmediate(+0.0); // FLD0 |
| 298 | addLegalFPImmediate(+1.0); // FLD1 |
| 299 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 300 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 301 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 302 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 303 | // First set operation action for all vector types to expand. Then we |
| 304 | // will selectively turn on ones that can be effectively codegen'd. |
| 305 | for (unsigned VT = (unsigned)MVT::Vector + 1; |
| 306 | VT != (unsigned)MVT::LAST_VALUETYPE; VT++) { |
| 307 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 308 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 309 | setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand); |
| 310 | setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 311 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 312 | setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand); |
| 313 | setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand); |
| 314 | setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand); |
| 315 | setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand); |
| 316 | setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand); |
| 317 | setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 318 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 319 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); |
Chris Lattner | 9b3bd46 | 2006-03-21 20:51:05 +0000 | [diff] [blame] | 320 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 321 | setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 324 | if (Subtarget->hasMMX()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 325 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 326 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 327 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
| 328 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 329 | // FIXME: add MMX packed arithmetics |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 330 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand); |
| 331 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand); |
| 332 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 335 | if (Subtarget->hasSSE1()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 336 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 337 | |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 338 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 339 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 340 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 341 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 342 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 343 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 344 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 345 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 346 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Evan Cheng | a88973f | 2006-03-22 19:22:18 +0000 | [diff] [blame] | 349 | if (Subtarget->hasSSE2()) { |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 350 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
| 351 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 352 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 353 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 354 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 355 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 356 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 357 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 358 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 359 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 360 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 361 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
Evan Cheng | f998984 | 2006-04-13 05:10:25 +0000 | [diff] [blame] | 362 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
Evan Cheng | 6bdb3f6 | 2006-10-27 18:49:08 +0000 | [diff] [blame] | 363 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 364 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 365 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 366 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 367 | |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 368 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 369 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 370 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
Evan Cheng | 5edb8d2 | 2006-04-17 22:04:06 +0000 | [diff] [blame] | 371 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 372 | // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones. |
| 373 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 374 | |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 375 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| 376 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 377 | setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom); |
| 378 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom); |
| 379 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom); |
| 380 | } |
| 381 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 382 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 383 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 384 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 385 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| 386 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
| 387 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 388 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 389 | for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) { |
| 390 | setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote); |
| 391 | AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64); |
| 392 | setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote); |
| 393 | AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64); |
| 394 | setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote); |
| 395 | AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 396 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote); |
| 397 | AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 398 | setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote); |
| 399 | AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 400 | } |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 401 | |
| 402 | // Custom lower v2i64 and v2f64 selects. |
| 403 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
Evan Cheng | 91b740d | 2006-04-12 17:12:36 +0000 | [diff] [blame] | 404 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 405 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 406 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 409 | // We want to custom lower some of our intrinsics. |
| 410 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 411 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 412 | // We have target-specific dag combine patterns for the following nodes: |
| 413 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 414 | setTargetDAGCombine(ISD::SELECT); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 415 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 416 | computeRegisterProperties(); |
| 417 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 418 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 419 | // be smaller when we are in optimizing for size mode. |
Evan Cheng | a03a5dc | 2006-02-14 08:38:30 +0000 | [diff] [blame] | 420 | maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores |
| 421 | maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores |
| 422 | maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 423 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 424 | } |
| 425 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 426 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 427 | // C & StdCall Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 429 | // StdCall calling convention seems to be standard for many Windows' API |
| 430 | // routines and around. It differs from C calling convention just a little: |
| 431 | // callee should clean up the stack, not caller. Symbols should be also |
| 432 | // decorated in some fancy way :) It doesn't support any vector arguments. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 433 | |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 434 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 435 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 436 | /// register for it. |
| 437 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 438 | const TargetRegisterClass *RC) { |
Evan Cheng | 85e3800 | 2006-04-27 05:35:28 +0000 | [diff] [blame] | 439 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 440 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 441 | MF.addLiveIn(PReg, VReg); |
| 442 | return VReg; |
| 443 | } |
| 444 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 445 | /// HowToPassArgument - Returns how an formal argument of the specified type |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 446 | /// should be passed. If it is through stack, returns the size of the stack |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 447 | /// slot; if it is through integer or XMM register, returns the number of |
| 448 | /// integer or XMM registers are needed. |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 449 | static void |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 450 | HowToPassCallArgument(MVT::ValueType ObjectVT, |
| 451 | bool ArgInReg, |
| 452 | unsigned NumIntRegs, unsigned NumXMMRegs, |
| 453 | unsigned MaxNumIntRegs, |
| 454 | unsigned &ObjSize, unsigned &ObjIntRegs, |
| 455 | unsigned &ObjXMMRegs, |
| 456 | bool AllowVectors = true) { |
| 457 | ObjSize = 0; |
| 458 | ObjIntRegs = 0; |
Evan Cheng | 2675534 | 2006-06-01 05:53:27 +0000 | [diff] [blame] | 459 | ObjXMMRegs = 0; |
Evan Cheng | cc1fc22 | 2006-05-25 23:31:23 +0000 | [diff] [blame] | 460 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 461 | if (MaxNumIntRegs>3) { |
| 462 | // We don't have too much registers on ia32! :) |
| 463 | MaxNumIntRegs = 3; |
| 464 | } |
| 465 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 466 | switch (ObjectVT) { |
| 467 | default: assert(0 && "Unhandled argument type!"); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 468 | case MVT::i8: |
| 469 | if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) |
| 470 | ObjIntRegs = 1; |
| 471 | else |
| 472 | ObjSize = 1; |
| 473 | break; |
| 474 | case MVT::i16: |
| 475 | if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) |
| 476 | ObjIntRegs = 1; |
| 477 | else |
| 478 | ObjSize = 2; |
| 479 | break; |
| 480 | case MVT::i32: |
| 481 | if (ArgInReg && (NumIntRegs < MaxNumIntRegs)) |
| 482 | ObjIntRegs = 1; |
| 483 | else |
| 484 | ObjSize = 4; |
| 485 | break; |
| 486 | case MVT::i64: |
| 487 | if (ArgInReg && (NumIntRegs+2 <= MaxNumIntRegs)) { |
| 488 | ObjIntRegs = 2; |
| 489 | } else if (ArgInReg && (NumIntRegs+1 <= MaxNumIntRegs)) { |
| 490 | ObjIntRegs = 1; |
| 491 | ObjSize = 4; |
| 492 | } else |
| 493 | ObjSize = 8; |
| 494 | case MVT::f32: |
| 495 | ObjSize = 4; |
| 496 | break; |
| 497 | case MVT::f64: |
| 498 | ObjSize = 8; |
| 499 | break; |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 500 | case MVT::v16i8: |
| 501 | case MVT::v8i16: |
| 502 | case MVT::v4i32: |
| 503 | case MVT::v2i64: |
| 504 | case MVT::v4f32: |
| 505 | case MVT::v2f64: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 506 | if (AllowVectors) { |
| 507 | if (NumXMMRegs < 4) |
| 508 | ObjXMMRegs = 1; |
| 509 | else |
| 510 | ObjSize = 16; |
| 511 | break; |
| 512 | } else |
| 513 | assert(0 && "Unhandled argument type [vector]!"); |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 514 | } |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 517 | SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG, |
| 518 | bool isStdCall) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 519 | unsigned NumArgs = Op.Val->getNumValues() - 1; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 520 | MachineFunction &MF = DAG.getMachineFunction(); |
| 521 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 522 | SDOperand Root = Op.getOperand(0); |
| 523 | std::vector<SDOperand> ArgValues; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 524 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 525 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 526 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 527 | // the stack frame looks like this: |
| 528 | // |
| 529 | // [ESP] -- return address |
| 530 | // [ESP + 4] -- first argument (leftmost lexically) |
Evan Cheng | f9d62dc | 2006-05-26 18:37:16 +0000 | [diff] [blame] | 531 | // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 532 | // ... |
| 533 | // |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 534 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 535 | unsigned NumSRetBytes= 0; // How much bytes on stack used for struct return |
| 536 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
| 537 | unsigned NumIntRegs = 0; // Integer regs used for parameter passing |
| 538 | |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 539 | static const unsigned XMMArgRegs[] = { |
| 540 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
| 541 | }; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 542 | static const unsigned GPRArgRegs[][3] = { |
| 543 | { X86::AL, X86::DL, X86::CL }, |
| 544 | { X86::AX, X86::DX, X86::CX }, |
| 545 | { X86::EAX, X86::EDX, X86::ECX } |
| 546 | }; |
| 547 | static const TargetRegisterClass* GPRClasses[3] = { |
| 548 | X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass |
| 549 | }; |
| 550 | |
| 551 | // Handle regparm attribute |
| 552 | std::vector<bool> ArgInRegs(NumArgs, false); |
| 553 | std::vector<bool> SRetArgs(NumArgs, false); |
| 554 | if (!isVarArg) { |
| 555 | for (unsigned i = 0; i<NumArgs; ++i) { |
| 556 | unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue(); |
| 557 | ArgInRegs[i] = (Flags >> 1) & 1; |
| 558 | SRetArgs[i] = (Flags >> 2) & 1; |
| 559 | } |
| 560 | } |
| 561 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 562 | for (unsigned i = 0; i < NumArgs; ++i) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 563 | MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); |
| 564 | unsigned ArgIncrement = 4; |
| 565 | unsigned ObjSize = 0; |
| 566 | unsigned ObjXMMRegs = 0; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 567 | unsigned ObjIntRegs = 0; |
| 568 | unsigned Reg = 0; |
| 569 | SDOperand ArgValue; |
| 570 | |
| 571 | HowToPassCallArgument(ObjectVT, |
| 572 | ArgInRegs[i], |
| 573 | NumIntRegs, NumXMMRegs, 3, |
| 574 | ObjSize, ObjIntRegs, ObjXMMRegs, |
| 575 | !isStdCall); |
| 576 | |
Evan Cheng | 052fb51 | 2006-05-26 18:39:59 +0000 | [diff] [blame] | 577 | if (ObjSize > 4) |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 578 | ArgIncrement = ObjSize; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 579 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 580 | if (ObjIntRegs || ObjXMMRegs) { |
| 581 | switch (ObjectVT) { |
| 582 | default: assert(0 && "Unhandled argument type!"); |
| 583 | case MVT::i8: |
| 584 | case MVT::i16: |
| 585 | case MVT::i32: { |
| 586 | unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][NumIntRegs]; |
| 587 | Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]); |
| 588 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 589 | break; |
| 590 | } |
| 591 | case MVT::v16i8: |
| 592 | case MVT::v8i16: |
| 593 | case MVT::v4i32: |
| 594 | case MVT::v2i64: |
| 595 | case MVT::v4f32: |
| 596 | case MVT::v2f64: |
| 597 | assert(!isStdCall && "Unhandled argument type!"); |
| 598 | Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass); |
| 599 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 600 | break; |
| 601 | } |
| 602 | NumIntRegs += ObjIntRegs; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 603 | NumXMMRegs += ObjXMMRegs; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 604 | } |
| 605 | if (ObjSize) { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame] | 606 | // XMM arguments have to be aligned on 16-byte boundary. |
| 607 | if (ObjSize == 16) |
| 608 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 609 | // Create the SelectionDAG nodes corresponding to a load from this |
| 610 | // parameter. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 611 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 612 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 613 | ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 614 | |
| 615 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 616 | if (SRetArgs[i]) |
| 617 | NumSRetBytes += ArgIncrement; |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 618 | } |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 619 | |
| 620 | ArgValues.push_back(ArgValue); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 623 | ArgValues.push_back(Root); |
| 624 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 625 | // If the function takes variable number of arguments, make a frame index for |
| 626 | // 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] | 627 | if (isVarArg) |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 628 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 629 | |
| 630 | if (isStdCall && !isVarArg) { |
| 631 | BytesToPopOnReturn = ArgOffset; // Callee pops everything.. |
| 632 | BytesCallerReserves = 0; |
| 633 | } else { |
| 634 | BytesToPopOnReturn = NumSRetBytes; // Callee pops hidden struct pointer. |
| 635 | BytesCallerReserves = ArgOffset; |
| 636 | } |
| 637 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 638 | RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. |
| 639 | ReturnAddrIndex = 0; // No return address slot generated yet. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 640 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 641 | |
| 642 | MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 643 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 644 | // Return the new list of results. |
| 645 | std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(), |
| 646 | Op.Val->value_end()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 647 | return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 650 | SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, |
| 651 | bool isStdCall) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 652 | SDOperand Chain = Op.getOperand(0); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 653 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 654 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 655 | SDOperand Callee = Op.getOperand(4); |
| 656 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 657 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 658 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 659 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 660 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 661 | }; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 662 | static const unsigned GPR32ArgRegs[] = { |
| 663 | X86::EAX, X86::EDX, X86::ECX |
| 664 | }; |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 665 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 666 | // Count how many bytes are to be pushed on the stack. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 667 | unsigned NumBytes = 0; |
| 668 | // Keep track of the number of integer regs passed so far. |
| 669 | unsigned NumIntRegs = 0; |
| 670 | // Keep track of the number of XMM regs passed so far. |
| 671 | unsigned NumXMMRegs = 0; |
| 672 | // How much bytes on stack used for struct return |
| 673 | unsigned NumSRetBytes= 0; |
| 674 | |
| 675 | // Handle regparm attribute |
| 676 | std::vector<bool> ArgInRegs(NumOps, false); |
| 677 | std::vector<bool> SRetArgs(NumOps, false); |
| 678 | for (unsigned i = 0; i<NumOps; ++i) { |
| 679 | unsigned Flags = |
| 680 | dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue(); |
| 681 | ArgInRegs[i] = (Flags >> 1) & 1; |
| 682 | SRetArgs[i] = (Flags >> 2) & 1; |
| 683 | } |
| 684 | |
| 685 | // Calculate stack frame size |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 686 | for (unsigned i = 0; i != NumOps; ++i) { |
| 687 | SDOperand Arg = Op.getOperand(5+2*i); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 688 | unsigned ArgIncrement = 4; |
| 689 | unsigned ObjSize = 0; |
| 690 | unsigned ObjIntRegs = 0; |
| 691 | unsigned ObjXMMRegs = 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 692 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 693 | HowToPassCallArgument(Arg.getValueType(), |
| 694 | ArgInRegs[i], |
| 695 | NumIntRegs, NumXMMRegs, 3, |
| 696 | ObjSize, ObjIntRegs, ObjXMMRegs, |
| 697 | !isStdCall); |
| 698 | if (ObjSize > 4) |
| 699 | ArgIncrement = ObjSize; |
| 700 | |
| 701 | NumIntRegs += ObjIntRegs; |
| 702 | NumXMMRegs += ObjXMMRegs; |
| 703 | if (ObjSize) { |
| 704 | // XMM arguments have to be aligned on 16-byte boundary. |
| 705 | if (ObjSize == 16) |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame] | 706 | NumBytes = ((NumBytes + 15) / 16) * 16; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 707 | NumBytes += ArgIncrement; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 708 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 709 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 710 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 711 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 712 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 713 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 714 | unsigned ArgOffset = 0; |
| 715 | NumXMMRegs = 0; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 716 | NumIntRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 717 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 718 | std::vector<SDOperand> MemOpChains; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 719 | SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 720 | for (unsigned i = 0; i != NumOps; ++i) { |
| 721 | SDOperand Arg = Op.getOperand(5+2*i); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 722 | unsigned ArgIncrement = 4; |
| 723 | unsigned ObjSize = 0; |
| 724 | unsigned ObjIntRegs = 0; |
| 725 | unsigned ObjXMMRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 726 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 727 | HowToPassCallArgument(Arg.getValueType(), |
| 728 | ArgInRegs[i], |
| 729 | NumIntRegs, NumXMMRegs, 3, |
| 730 | ObjSize, ObjIntRegs, ObjXMMRegs, |
| 731 | !isStdCall); |
| 732 | |
| 733 | if (ObjSize > 4) |
| 734 | ArgIncrement = ObjSize; |
| 735 | |
| 736 | if (Arg.getValueType() == MVT::i8 || Arg.getValueType() == MVT::i16) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 737 | // Promote the integer to 32 bits. If the input type is signed use a |
| 738 | // sign extend, otherwise use a zero extend. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 739 | unsigned Flags = cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue(); |
| 740 | |
| 741 | unsigned ExtOp = (Flags & 1) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 742 | Arg = DAG.getNode(ExtOp, MVT::i32, Arg); |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 743 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 744 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 745 | if (ObjIntRegs || ObjXMMRegs) { |
| 746 | switch (Arg.getValueType()) { |
| 747 | default: assert(0 && "Unhandled argument type!"); |
| 748 | case MVT::i32: |
| 749 | RegsToPass.push_back(std::make_pair(GPR32ArgRegs[NumIntRegs], Arg)); |
| 750 | break; |
| 751 | case MVT::v16i8: |
| 752 | case MVT::v8i16: |
| 753 | case MVT::v4i32: |
| 754 | case MVT::v2i64: |
| 755 | case MVT::v4f32: |
| 756 | case MVT::v2f64: |
| 757 | assert(!isStdCall && "Unhandled argument type!"); |
| 758 | RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); |
| 759 | break; |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 760 | } |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 761 | |
| 762 | NumIntRegs += ObjIntRegs; |
| 763 | NumXMMRegs += ObjXMMRegs; |
| 764 | } |
| 765 | if (ObjSize) { |
| 766 | // XMM arguments have to be aligned on 16-byte boundary. |
| 767 | if (ObjSize == 16) |
| 768 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
| 769 | |
| 770 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 771 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 772 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
| 773 | |
| 774 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 775 | if (SRetArgs[i]) |
| 776 | NumSRetBytes += ArgIncrement; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 777 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame] | 780 | // Sanity check: we haven't seen NumSRetBytes > 4 |
| 781 | assert((NumSRetBytes<=4) && |
| 782 | "Too much space for struct-return pointer requested"); |
| 783 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 784 | if (!MemOpChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 785 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 786 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 787 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 788 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 789 | // and flag operands which copy the outgoing args into registers. |
| 790 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 791 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 792 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 793 | InFlag); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 794 | InFlag = Chain.getValue(1); |
| 795 | } |
| 796 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 797 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 798 | // GOT pointer. |
Evan Cheng | 706535d | 2007-01-22 21:34:25 +0000 | [diff] [blame] | 799 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 800 | Subtarget->isPICStyleGOT()) { |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 801 | Chain = DAG.getCopyToReg(Chain, X86::EBX, |
| 802 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 803 | InFlag); |
| 804 | InFlag = Chain.getValue(1); |
| 805 | } |
| 806 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 807 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 808 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 809 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 810 | // We should use extra load for direct calls to dllimported functions in |
| 811 | // non-JIT mode. |
| 812 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 813 | getTargetMachine(), true)) |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 814 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 815 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 816 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 817 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 818 | std::vector<MVT::ValueType> NodeTys; |
| 819 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 820 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 821 | std::vector<SDOperand> Ops; |
| 822 | Ops.push_back(Chain); |
| 823 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 824 | |
| 825 | // Add argument registers to the end of the list so that they are known live |
| 826 | // into the call. |
| 827 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 828 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 829 | RegsToPass[i].second.getValueType())); |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 830 | |
| 831 | // Add an implicit use GOT pointer in EBX. |
| 832 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 833 | Subtarget->isPICStyleGOT()) |
| 834 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 835 | |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 836 | if (InFlag.Val) |
| 837 | Ops.push_back(InFlag); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 838 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 839 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 840 | NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 841 | InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 843 | // Create the CALLSEQ_END node. |
| 844 | unsigned NumBytesForCalleeToPush = 0; |
| 845 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 846 | if (isStdCall) { |
| 847 | if (isVarArg) { |
| 848 | NumBytesForCalleeToPush = NumSRetBytes; |
| 849 | } else { |
| 850 | NumBytesForCalleeToPush = NumBytes; |
| 851 | } |
| 852 | } else { |
| 853 | // If this is is a call to a struct-return function, the callee |
| 854 | // pops the hidden struct pointer, so we have to push it back. |
| 855 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
| 856 | NumBytesForCalleeToPush = NumSRetBytes; |
| 857 | } |
| 858 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 859 | NodeTys.clear(); |
| 860 | NodeTys.push_back(MVT::Other); // Returns a chain |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 861 | if (RetVT != MVT::Other) |
| 862 | 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] | 863 | Ops.clear(); |
| 864 | Ops.push_back(Chain); |
| 865 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 866 | Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 867 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 868 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 869 | if (RetVT != MVT::Other) |
| 870 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 871 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 872 | std::vector<SDOperand> ResultVals; |
| 873 | NodeTys.clear(); |
| 874 | switch (RetVT) { |
| 875 | default: assert(0 && "Unknown value type to return!"); |
| 876 | case MVT::Other: break; |
| 877 | case MVT::i8: |
| 878 | Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1); |
| 879 | ResultVals.push_back(Chain.getValue(0)); |
| 880 | NodeTys.push_back(MVT::i8); |
| 881 | break; |
| 882 | case MVT::i16: |
| 883 | Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1); |
| 884 | ResultVals.push_back(Chain.getValue(0)); |
| 885 | NodeTys.push_back(MVT::i16); |
| 886 | break; |
| 887 | case MVT::i32: |
| 888 | if (Op.Val->getValueType(1) == MVT::i32) { |
| 889 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 890 | ResultVals.push_back(Chain.getValue(0)); |
| 891 | Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32, |
| 892 | Chain.getValue(2)).getValue(1); |
| 893 | ResultVals.push_back(Chain.getValue(0)); |
| 894 | NodeTys.push_back(MVT::i32); |
| 895 | } else { |
| 896 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 897 | ResultVals.push_back(Chain.getValue(0)); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 898 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 899 | NodeTys.push_back(MVT::i32); |
| 900 | break; |
| 901 | case MVT::v16i8: |
| 902 | case MVT::v8i16: |
| 903 | case MVT::v4i32: |
| 904 | case MVT::v2i64: |
| 905 | case MVT::v4f32: |
| 906 | case MVT::v2f64: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 907 | assert(!isStdCall && "Unknown value type to return!"); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 908 | Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1); |
| 909 | ResultVals.push_back(Chain.getValue(0)); |
| 910 | NodeTys.push_back(RetVT); |
| 911 | break; |
| 912 | case MVT::f32: |
| 913 | case MVT::f64: { |
| 914 | std::vector<MVT::ValueType> Tys; |
| 915 | Tys.push_back(MVT::f64); |
| 916 | Tys.push_back(MVT::Other); |
| 917 | Tys.push_back(MVT::Flag); |
| 918 | std::vector<SDOperand> Ops; |
| 919 | Ops.push_back(Chain); |
| 920 | Ops.push_back(InFlag); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 921 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 922 | &Ops[0], Ops.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 923 | Chain = RetVal.getValue(1); |
| 924 | InFlag = RetVal.getValue(2); |
| 925 | if (X86ScalarSSE) { |
| 926 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 927 | // shouldn't be necessary except that RFP cannot be live across |
| 928 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 929 | MachineFunction &MF = DAG.getMachineFunction(); |
| 930 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 931 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 932 | Tys.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 933 | Tys.push_back(MVT::Other); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 934 | Ops.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 935 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 936 | Ops.push_back(RetVal); |
| 937 | Ops.push_back(StackSlot); |
| 938 | Ops.push_back(DAG.getValueType(RetVT)); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 939 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 940 | Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 941 | RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 942 | Chain = RetVal.getValue(1); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 943 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 944 | |
| 945 | if (RetVT == MVT::f32 && !X86ScalarSSE) |
| 946 | // FIXME: we would really like to remember that this FP_ROUND |
| 947 | // operation is okay to eliminate if we allow excess FP precision. |
| 948 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 949 | ResultVals.push_back(RetVal); |
| 950 | NodeTys.push_back(RetVT); |
| 951 | break; |
| 952 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 953 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 954 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 955 | // If the function returns void, just return the chain. |
| 956 | if (ResultVals.empty()) |
| 957 | return Chain; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 958 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 959 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 960 | NodeTys.push_back(MVT::Other); |
| 961 | ResultVals.push_back(Chain); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 962 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, |
| 963 | &ResultVals[0], ResultVals.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 964 | return Res.getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 967 | |
| 968 | //===----------------------------------------------------------------------===// |
| 969 | // X86-64 C Calling Convention implementation |
| 970 | //===----------------------------------------------------------------------===// |
| 971 | |
| 972 | /// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified |
| 973 | /// type should be passed. If it is through stack, returns the size of the stack |
| 974 | /// slot; if it is through integer or XMM register, returns the number of |
| 975 | /// integer or XMM registers are needed. |
| 976 | static void |
| 977 | HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT, |
| 978 | unsigned NumIntRegs, unsigned NumXMMRegs, |
| 979 | unsigned &ObjSize, unsigned &ObjIntRegs, |
| 980 | unsigned &ObjXMMRegs) { |
| 981 | ObjSize = 0; |
| 982 | ObjIntRegs = 0; |
| 983 | ObjXMMRegs = 0; |
| 984 | |
| 985 | switch (ObjectVT) { |
| 986 | default: assert(0 && "Unhandled argument type!"); |
| 987 | case MVT::i8: |
| 988 | case MVT::i16: |
| 989 | case MVT::i32: |
| 990 | case MVT::i64: |
| 991 | if (NumIntRegs < 6) |
| 992 | ObjIntRegs = 1; |
| 993 | else { |
| 994 | switch (ObjectVT) { |
| 995 | default: break; |
| 996 | case MVT::i8: ObjSize = 1; break; |
| 997 | case MVT::i16: ObjSize = 2; break; |
| 998 | case MVT::i32: ObjSize = 4; break; |
| 999 | case MVT::i64: ObjSize = 8; break; |
| 1000 | } |
| 1001 | } |
| 1002 | break; |
| 1003 | case MVT::f32: |
| 1004 | case MVT::f64: |
| 1005 | case MVT::v16i8: |
| 1006 | case MVT::v8i16: |
| 1007 | case MVT::v4i32: |
| 1008 | case MVT::v2i64: |
| 1009 | case MVT::v4f32: |
| 1010 | case MVT::v2f64: |
| 1011 | if (NumXMMRegs < 8) |
| 1012 | ObjXMMRegs = 1; |
| 1013 | else { |
| 1014 | switch (ObjectVT) { |
| 1015 | default: break; |
| 1016 | case MVT::f32: ObjSize = 4; break; |
| 1017 | case MVT::f64: ObjSize = 8; break; |
| 1018 | case MVT::v16i8: |
| 1019 | case MVT::v8i16: |
| 1020 | case MVT::v4i32: |
| 1021 | case MVT::v2i64: |
| 1022 | case MVT::v4f32: |
| 1023 | case MVT::v2f64: ObjSize = 16; break; |
| 1024 | } |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | SDOperand |
| 1031 | X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) { |
| 1032 | unsigned NumArgs = Op.Val->getNumValues() - 1; |
| 1033 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1034 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1035 | SDOperand Root = Op.getOperand(0); |
| 1036 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 1037 | std::vector<SDOperand> ArgValues; |
| 1038 | |
| 1039 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 1040 | // the stack frame looks like this: |
| 1041 | // |
| 1042 | // [RSP] -- return address |
| 1043 | // [RSP + 8] -- first nonreg argument (leftmost lexically) |
| 1044 | // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size |
| 1045 | // ... |
| 1046 | // |
| 1047 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 1048 | unsigned NumIntRegs = 0; // Int regs used for parameter passing. |
| 1049 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
| 1050 | |
| 1051 | static const unsigned GPR8ArgRegs[] = { |
| 1052 | X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B |
| 1053 | }; |
| 1054 | static const unsigned GPR16ArgRegs[] = { |
| 1055 | X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W |
| 1056 | }; |
| 1057 | static const unsigned GPR32ArgRegs[] = { |
| 1058 | X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D |
| 1059 | }; |
| 1060 | static const unsigned GPR64ArgRegs[] = { |
| 1061 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1062 | }; |
| 1063 | static const unsigned XMMArgRegs[] = { |
| 1064 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1065 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1066 | }; |
| 1067 | |
| 1068 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1069 | MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); |
| 1070 | unsigned ArgIncrement = 8; |
| 1071 | unsigned ObjSize = 0; |
| 1072 | unsigned ObjIntRegs = 0; |
| 1073 | unsigned ObjXMMRegs = 0; |
| 1074 | |
| 1075 | // FIXME: __int128 and long double support? |
| 1076 | HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs, |
| 1077 | ObjSize, ObjIntRegs, ObjXMMRegs); |
| 1078 | if (ObjSize > 8) |
| 1079 | ArgIncrement = ObjSize; |
| 1080 | |
| 1081 | unsigned Reg = 0; |
| 1082 | SDOperand ArgValue; |
| 1083 | if (ObjIntRegs || ObjXMMRegs) { |
| 1084 | switch (ObjectVT) { |
| 1085 | default: assert(0 && "Unhandled argument type!"); |
| 1086 | case MVT::i8: |
| 1087 | case MVT::i16: |
| 1088 | case MVT::i32: |
| 1089 | case MVT::i64: { |
| 1090 | TargetRegisterClass *RC = NULL; |
| 1091 | switch (ObjectVT) { |
| 1092 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1093 | case MVT::i8: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1094 | RC = X86::GR8RegisterClass; |
| 1095 | Reg = GPR8ArgRegs[NumIntRegs]; |
| 1096 | break; |
| 1097 | case MVT::i16: |
| 1098 | RC = X86::GR16RegisterClass; |
| 1099 | Reg = GPR16ArgRegs[NumIntRegs]; |
| 1100 | break; |
| 1101 | case MVT::i32: |
| 1102 | RC = X86::GR32RegisterClass; |
| 1103 | Reg = GPR32ArgRegs[NumIntRegs]; |
| 1104 | break; |
| 1105 | case MVT::i64: |
| 1106 | RC = X86::GR64RegisterClass; |
| 1107 | Reg = GPR64ArgRegs[NumIntRegs]; |
| 1108 | break; |
| 1109 | } |
| 1110 | Reg = AddLiveIn(MF, Reg, RC); |
| 1111 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 1112 | break; |
| 1113 | } |
| 1114 | case MVT::f32: |
| 1115 | case MVT::f64: |
| 1116 | case MVT::v16i8: |
| 1117 | case MVT::v8i16: |
| 1118 | case MVT::v4i32: |
| 1119 | case MVT::v2i64: |
| 1120 | case MVT::v4f32: |
| 1121 | case MVT::v2f64: { |
| 1122 | TargetRegisterClass *RC= (ObjectVT == MVT::f32) ? |
| 1123 | X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ? |
| 1124 | X86::FR64RegisterClass : X86::VR128RegisterClass); |
| 1125 | Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC); |
| 1126 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
| 1130 | NumIntRegs += ObjIntRegs; |
| 1131 | NumXMMRegs += ObjXMMRegs; |
| 1132 | } else if (ObjSize) { |
| 1133 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1134 | if (ObjSize == 16) |
| 1135 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
| 1136 | // Create the SelectionDAG nodes corresponding to a load from this |
| 1137 | // parameter. |
| 1138 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 1139 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1140 | ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1141 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 1142 | } |
| 1143 | |
| 1144 | ArgValues.push_back(ArgValue); |
| 1145 | } |
| 1146 | |
| 1147 | // If the function takes variable number of arguments, make a frame index for |
| 1148 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 1149 | if (isVarArg) { |
| 1150 | // For X86-64, if there are vararg parameters that are passed via |
| 1151 | // registers, then we must store them to their spots on the stack so they |
| 1152 | // may be loaded by deferencing the result of va_next. |
| 1153 | VarArgsGPOffset = NumIntRegs * 8; |
| 1154 | VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16; |
| 1155 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
| 1156 | RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16); |
| 1157 | |
| 1158 | // Store the integer parameter registers. |
| 1159 | std::vector<SDOperand> MemOps; |
| 1160 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
| 1161 | SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
| 1162 | DAG.getConstant(VarArgsGPOffset, getPointerTy())); |
| 1163 | for (; NumIntRegs != 6; ++NumIntRegs) { |
| 1164 | unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs], |
| 1165 | X86::GR64RegisterClass); |
| 1166 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1167 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1168 | MemOps.push_back(Store); |
| 1169 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 1170 | DAG.getConstant(8, getPointerTy())); |
| 1171 | } |
| 1172 | |
| 1173 | // Now store the XMM (fp + vector) parameter registers. |
| 1174 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN, |
| 1175 | DAG.getConstant(VarArgsFPOffset, getPointerTy())); |
| 1176 | for (; NumXMMRegs != 8; ++NumXMMRegs) { |
| 1177 | unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], |
| 1178 | X86::VR128RegisterClass); |
| 1179 | SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1180 | SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1181 | MemOps.push_back(Store); |
| 1182 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 1183 | DAG.getConstant(16, getPointerTy())); |
| 1184 | } |
| 1185 | if (!MemOps.empty()) |
| 1186 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1187 | &MemOps[0], MemOps.size()); |
| 1188 | } |
| 1189 | |
| 1190 | ArgValues.push_back(Root); |
| 1191 | |
| 1192 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 1193 | BytesToPopOnReturn = 0; // Callee pops nothing. |
| 1194 | BytesCallerReserves = ArgOffset; |
| 1195 | |
| 1196 | // Return the new list of results. |
| 1197 | std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(), |
| 1198 | Op.Val->value_end()); |
| 1199 | return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size()); |
| 1200 | } |
| 1201 | |
| 1202 | SDOperand |
| 1203 | X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) { |
| 1204 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1205 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0; |
| 1206 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 1207 | SDOperand Callee = Op.getOperand(4); |
| 1208 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 1209 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 1210 | |
| 1211 | // Count how many bytes are to be pushed on the stack. |
| 1212 | unsigned NumBytes = 0; |
| 1213 | unsigned NumIntRegs = 0; // Int regs used for parameter passing. |
| 1214 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
| 1215 | |
| 1216 | static const unsigned GPR8ArgRegs[] = { |
| 1217 | X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B |
| 1218 | }; |
| 1219 | static const unsigned GPR16ArgRegs[] = { |
| 1220 | X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W |
| 1221 | }; |
| 1222 | static const unsigned GPR32ArgRegs[] = { |
| 1223 | X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D |
| 1224 | }; |
| 1225 | static const unsigned GPR64ArgRegs[] = { |
| 1226 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 1227 | }; |
| 1228 | static const unsigned XMMArgRegs[] = { |
| 1229 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 1230 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 1231 | }; |
| 1232 | |
| 1233 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1234 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1235 | MVT::ValueType ArgVT = Arg.getValueType(); |
| 1236 | |
| 1237 | switch (ArgVT) { |
| 1238 | default: assert(0 && "Unknown value type!"); |
| 1239 | case MVT::i8: |
| 1240 | case MVT::i16: |
| 1241 | case MVT::i32: |
| 1242 | case MVT::i64: |
| 1243 | if (NumIntRegs < 6) |
| 1244 | ++NumIntRegs; |
| 1245 | else |
| 1246 | NumBytes += 8; |
| 1247 | break; |
| 1248 | case MVT::f32: |
| 1249 | case MVT::f64: |
| 1250 | case MVT::v16i8: |
| 1251 | case MVT::v8i16: |
| 1252 | case MVT::v4i32: |
| 1253 | case MVT::v2i64: |
| 1254 | case MVT::v4f32: |
| 1255 | case MVT::v2f64: |
| 1256 | if (NumXMMRegs < 8) |
| 1257 | NumXMMRegs++; |
| 1258 | else if (ArgVT == MVT::f32 || ArgVT == MVT::f64) |
| 1259 | NumBytes += 8; |
| 1260 | else { |
| 1261 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1262 | NumBytes = ((NumBytes + 15) / 16) * 16; |
| 1263 | NumBytes += 16; |
| 1264 | } |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
| 1270 | |
| 1271 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 1272 | unsigned ArgOffset = 0; |
| 1273 | NumIntRegs = 0; |
| 1274 | NumXMMRegs = 0; |
| 1275 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 1276 | std::vector<SDOperand> MemOpChains; |
| 1277 | SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy()); |
| 1278 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1279 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1280 | MVT::ValueType ArgVT = Arg.getValueType(); |
| 1281 | |
| 1282 | switch (ArgVT) { |
| 1283 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 1284 | case MVT::i8: |
| 1285 | case MVT::i16: |
| 1286 | case MVT::i32: |
| 1287 | case MVT::i64: |
| 1288 | if (NumIntRegs < 6) { |
| 1289 | unsigned Reg = 0; |
| 1290 | switch (ArgVT) { |
| 1291 | default: break; |
| 1292 | case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break; |
| 1293 | case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break; |
| 1294 | case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break; |
| 1295 | case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break; |
| 1296 | } |
| 1297 | RegsToPass.push_back(std::make_pair(Reg, Arg)); |
| 1298 | ++NumIntRegs; |
| 1299 | } else { |
| 1300 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 1301 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1302 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1303 | ArgOffset += 8; |
| 1304 | } |
| 1305 | break; |
| 1306 | case MVT::f32: |
| 1307 | case MVT::f64: |
| 1308 | case MVT::v16i8: |
| 1309 | case MVT::v8i16: |
| 1310 | case MVT::v4i32: |
| 1311 | case MVT::v2i64: |
| 1312 | case MVT::v4f32: |
| 1313 | case MVT::v2f64: |
| 1314 | if (NumXMMRegs < 8) { |
| 1315 | RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); |
| 1316 | NumXMMRegs++; |
| 1317 | } else { |
| 1318 | if (ArgVT != MVT::f32 && ArgVT != MVT::f64) { |
| 1319 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1320 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
| 1321 | } |
| 1322 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 1323 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1324 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1325 | if (ArgVT == MVT::f32 || ArgVT == MVT::f64) |
| 1326 | ArgOffset += 8; |
| 1327 | else |
| 1328 | ArgOffset += 16; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | if (!MemOpChains.empty()) |
| 1334 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1335 | &MemOpChains[0], MemOpChains.size()); |
| 1336 | |
| 1337 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1338 | // and flag operands which copy the outgoing args into registers. |
| 1339 | SDOperand InFlag; |
| 1340 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1341 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1342 | InFlag); |
| 1343 | InFlag = Chain.getValue(1); |
| 1344 | } |
| 1345 | |
| 1346 | if (isVarArg) { |
| 1347 | // From AMD64 ABI document: |
| 1348 | // For calls that may call functions that use varargs or stdargs |
| 1349 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 1350 | // the declaration) %al is used as hidden argument to specify the number |
| 1351 | // of SSE registers used. The contents of %al do not need to match exactly |
| 1352 | // the number of registers, but must be an ubound on the number of SSE |
| 1353 | // registers used and is in the range 0 - 8 inclusive. |
| 1354 | Chain = DAG.getCopyToReg(Chain, X86::AL, |
| 1355 | DAG.getConstant(NumXMMRegs, MVT::i8), InFlag); |
| 1356 | InFlag = Chain.getValue(1); |
| 1357 | } |
| 1358 | |
| 1359 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1360 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1361 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 1362 | // We should use extra load for direct calls to dllimported functions in |
| 1363 | // non-JIT mode. |
| 1364 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1365 | getTargetMachine(), true)) |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1366 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 1367 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1368 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 1369 | |
| 1370 | std::vector<MVT::ValueType> NodeTys; |
| 1371 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 1372 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 1373 | std::vector<SDOperand> Ops; |
| 1374 | Ops.push_back(Chain); |
| 1375 | Ops.push_back(Callee); |
| 1376 | |
| 1377 | // Add argument registers to the end of the list so that they are known live |
| 1378 | // into the call. |
| 1379 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1380 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1381 | RegsToPass[i].second.getValueType())); |
| 1382 | |
| 1383 | if (InFlag.Val) |
| 1384 | Ops.push_back(InFlag); |
| 1385 | |
| 1386 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
| 1387 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 1388 | NodeTys, &Ops[0], Ops.size()); |
| 1389 | InFlag = Chain.getValue(1); |
| 1390 | |
| 1391 | NodeTys.clear(); |
| 1392 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 1393 | if (RetVT != MVT::Other) |
| 1394 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 1395 | Ops.clear(); |
| 1396 | Ops.push_back(Chain); |
| 1397 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 1398 | Ops.push_back(DAG.getConstant(0, getPointerTy())); |
| 1399 | Ops.push_back(InFlag); |
| 1400 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
| 1401 | if (RetVT != MVT::Other) |
| 1402 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1403 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1404 | std::vector<SDOperand> ResultVals; |
| 1405 | NodeTys.clear(); |
| 1406 | switch (RetVT) { |
| 1407 | default: assert(0 && "Unknown value type to return!"); |
| 1408 | case MVT::Other: break; |
| 1409 | case MVT::i8: |
| 1410 | Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1); |
| 1411 | ResultVals.push_back(Chain.getValue(0)); |
| 1412 | NodeTys.push_back(MVT::i8); |
| 1413 | break; |
| 1414 | case MVT::i16: |
| 1415 | Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1); |
| 1416 | ResultVals.push_back(Chain.getValue(0)); |
| 1417 | NodeTys.push_back(MVT::i16); |
| 1418 | break; |
| 1419 | case MVT::i32: |
| 1420 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 1421 | ResultVals.push_back(Chain.getValue(0)); |
| 1422 | NodeTys.push_back(MVT::i32); |
| 1423 | break; |
| 1424 | case MVT::i64: |
| 1425 | if (Op.Val->getValueType(1) == MVT::i64) { |
| 1426 | // FIXME: __int128 support? |
| 1427 | Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1); |
| 1428 | ResultVals.push_back(Chain.getValue(0)); |
| 1429 | Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64, |
| 1430 | Chain.getValue(2)).getValue(1); |
| 1431 | ResultVals.push_back(Chain.getValue(0)); |
| 1432 | NodeTys.push_back(MVT::i64); |
| 1433 | } else { |
| 1434 | Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1); |
| 1435 | ResultVals.push_back(Chain.getValue(0)); |
| 1436 | } |
| 1437 | NodeTys.push_back(MVT::i64); |
| 1438 | break; |
| 1439 | case MVT::f32: |
| 1440 | case MVT::f64: |
| 1441 | case MVT::v16i8: |
| 1442 | case MVT::v8i16: |
| 1443 | case MVT::v4i32: |
| 1444 | case MVT::v2i64: |
| 1445 | case MVT::v4f32: |
| 1446 | case MVT::v2f64: |
| 1447 | // FIXME: long double support? |
| 1448 | Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1); |
| 1449 | ResultVals.push_back(Chain.getValue(0)); |
| 1450 | NodeTys.push_back(RetVT); |
| 1451 | break; |
| 1452 | } |
| 1453 | |
| 1454 | // If the function returns void, just return the chain. |
| 1455 | if (ResultVals.empty()) |
| 1456 | return Chain; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1457 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1458 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 1459 | NodeTys.push_back(MVT::Other); |
| 1460 | ResultVals.push_back(Chain); |
| 1461 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, |
| 1462 | &ResultVals[0], ResultVals.size()); |
| 1463 | return Res.getValue(Op.ResNo); |
| 1464 | } |
| 1465 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1466 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1467 | // Fast & FastCall Calling Convention implementation |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1468 | //===----------------------------------------------------------------------===// |
| 1469 | // |
| 1470 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 1471 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 1472 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 1473 | // tail calls), and has the same return value conventions as C calling convs. |
| 1474 | // |
| 1475 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 1476 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 1477 | // reasons. |
| 1478 | // |
| 1479 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 1480 | // (when we have a global fp allocator) and do other tricks. |
| 1481 | // |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1482 | //===----------------------------------------------------------------------===// |
| 1483 | // The X86 'fastcall' calling convention passes up to two integer arguments in |
| 1484 | // registers (an appropriate portion of ECX/EDX), passes arguments in C order, |
| 1485 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 1486 | // tail calls), and has the same return value conventions as C calling convs. |
| 1487 | // |
| 1488 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 1489 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 1490 | // reasons. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1491 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 1492 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1493 | SDOperand |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1494 | X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG, |
| 1495 | bool isFastCall) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1496 | unsigned NumArgs = Op.Val->getNumValues()-1; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1497 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1498 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1499 | SDOperand Root = Op.getOperand(0); |
| 1500 | std::vector<SDOperand> ArgValues; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1501 | |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 1502 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 1503 | // frame looks like this: |
| 1504 | // |
| 1505 | // [ESP] -- return address |
| 1506 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
Evan Cheng | f9d62dc | 2006-05-26 18:37:16 +0000 | [diff] [blame] | 1507 | // [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] | 1508 | // ... |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1509 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 1510 | |
| 1511 | // Keep track of the number of integer regs passed so far. This can be either |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1512 | // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX |
| 1513 | // are both used). |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1514 | unsigned NumIntRegs = 0; |
Evan Cheng | 2fdd95e | 2006-04-27 08:31:10 +0000 | [diff] [blame] | 1515 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1516 | |
| 1517 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 1518 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1519 | }; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1520 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1521 | static const unsigned GPRArgRegs[][2][2] = { |
| 1522 | {{ X86::AL, X86::DL }, { X86::CL, X86::DL }}, |
| 1523 | {{ X86::AX, X86::DX }, { X86::CX, X86::DX }}, |
| 1524 | {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }} |
| 1525 | }; |
| 1526 | |
| 1527 | static const TargetRegisterClass* GPRClasses[3] = { |
| 1528 | X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass |
| 1529 | }; |
| 1530 | |
| 1531 | unsigned GPRInd = (isFastCall ? 1 : 0); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1532 | for (unsigned i = 0; i < NumArgs; ++i) { |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1533 | MVT::ValueType ObjectVT = Op.getValue(i).getValueType(); |
| 1534 | unsigned ArgIncrement = 4; |
| 1535 | unsigned ObjSize = 0; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1536 | unsigned ObjXMMRegs = 0; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1537 | unsigned ObjIntRegs = 0; |
| 1538 | unsigned Reg = 0; |
| 1539 | SDOperand ArgValue; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1540 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1541 | HowToPassCallArgument(ObjectVT, |
| 1542 | true, // Use as much registers as possible |
| 1543 | NumIntRegs, NumXMMRegs, |
| 1544 | (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS), |
| 1545 | ObjSize, ObjIntRegs, ObjXMMRegs, |
| 1546 | !isFastCall); |
| 1547 | |
Evan Cheng | 052fb51 | 2006-05-26 18:39:59 +0000 | [diff] [blame] | 1548 | if (ObjSize > 4) |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1549 | ArgIncrement = ObjSize; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 1550 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1551 | if (ObjIntRegs || ObjXMMRegs) { |
| 1552 | switch (ObjectVT) { |
| 1553 | default: assert(0 && "Unhandled argument type!"); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1554 | case MVT::i8: |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1555 | case MVT::i16: |
Nick Lewycky | 5310897 | 2007-01-28 15:39:16 +0000 | [diff] [blame] | 1556 | case MVT::i32: { |
| 1557 | unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][GPRInd][NumIntRegs]; |
| 1558 | Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]); |
| 1559 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 1560 | break; |
| 1561 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1562 | case MVT::v16i8: |
| 1563 | case MVT::v8i16: |
| 1564 | case MVT::v4i32: |
| 1565 | case MVT::v2i64: |
| 1566 | case MVT::v4f32: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1567 | case MVT::v2f64: { |
| 1568 | assert(!isFastCall && "Unhandled argument type!"); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1569 | Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass); |
| 1570 | ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT); |
| 1571 | break; |
Evan Cheng | eda65fa | 2006-04-27 01:32:22 +0000 | [diff] [blame] | 1572 | } |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1573 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1574 | NumIntRegs += ObjIntRegs; |
| 1575 | NumXMMRegs += ObjXMMRegs; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1576 | } |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1577 | if (ObjSize) { |
Evan Cheng | 3fddf24 | 2006-05-26 20:37:47 +0000 | [diff] [blame] | 1578 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1579 | if (ObjSize == 16) |
| 1580 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1581 | // Create the SelectionDAG nodes corresponding to a load from this |
| 1582 | // parameter. |
| 1583 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 1584 | SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy()); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1585 | ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0); |
| 1586 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1587 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 1588 | } |
| 1589 | |
| 1590 | ArgValues.push_back(ArgValue); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1593 | ArgValues.push_back(Root); |
| 1594 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1595 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1596 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1597 | if ((ArgOffset & 7) == 0) |
| 1598 | ArgOffset += 4; |
| 1599 | |
| 1600 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1601 | RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1602 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 1603 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
| 1604 | BytesCallerReserves = 0; |
| 1605 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1606 | MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn); |
| 1607 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1608 | // Finally, inform the code generator which regs we return values in. |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1609 | switch (getValueType(MF.getFunction()->getReturnType())) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1610 | default: assert(0 && "Unknown type!"); |
| 1611 | case MVT::isVoid: break; |
Chris Lattner | 13bf6c1 | 2006-10-03 17:18:42 +0000 | [diff] [blame] | 1612 | case MVT::i1: |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1613 | case MVT::i8: |
| 1614 | case MVT::i16: |
| 1615 | case MVT::i32: |
| 1616 | MF.addLiveOut(X86::EAX); |
| 1617 | break; |
| 1618 | case MVT::i64: |
| 1619 | MF.addLiveOut(X86::EAX); |
| 1620 | MF.addLiveOut(X86::EDX); |
| 1621 | break; |
| 1622 | case MVT::f32: |
| 1623 | case MVT::f64: |
| 1624 | MF.addLiveOut(X86::ST0); |
| 1625 | break; |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1626 | case MVT::v16i8: |
| 1627 | case MVT::v8i16: |
| 1628 | case MVT::v4i32: |
| 1629 | case MVT::v2i64: |
| 1630 | case MVT::v4f32: |
| 1631 | case MVT::v2f64: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1632 | assert(!isFastCall && "Unknown result type"); |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1633 | MF.addLiveOut(X86::XMM0); |
| 1634 | break; |
| 1635 | } |
Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 1636 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 1637 | // Return the new list of results. |
| 1638 | std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(), |
| 1639 | Op.Val->value_end()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1640 | return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 1643 | SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG, |
| 1644 | bool isFastCall) { |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1645 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1646 | bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0; |
| 1647 | SDOperand Callee = Op.getOperand(4); |
| 1648 | MVT::ValueType RetVT= Op.Val->getValueType(0); |
| 1649 | unsigned NumOps = (Op.getNumOperands() - 5) / 2; |
| 1650 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1651 | // Count how many bytes are to be pushed on the stack. |
| 1652 | unsigned NumBytes = 0; |
| 1653 | |
| 1654 | // Keep track of the number of integer regs passed so far. This can be either |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1655 | // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX |
| 1656 | // are both used). |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1657 | unsigned NumIntRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1658 | unsigned NumXMMRegs = 0; // XMM regs used for parameter passing. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1659 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1660 | static const unsigned GPRArgRegs[][2][2] = { |
| 1661 | {{ X86::AL, X86::DL }, { X86::CL, X86::DL }}, |
| 1662 | {{ X86::AX, X86::DX }, { X86::CX, X86::DX }}, |
| 1663 | {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }} |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1664 | }; |
| 1665 | static const unsigned XMMArgRegs[] = { |
Evan Cheng | 1d6a9b3 | 2006-05-26 19:22:06 +0000 | [diff] [blame] | 1666 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3 |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1667 | }; |
| 1668 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1669 | unsigned GPRInd = (isFastCall ? 1 : 0); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1670 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1671 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1672 | |
| 1673 | switch (Arg.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1674 | default: assert(0 && "Unknown value type!"); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1675 | case MVT::i8: |
| 1676 | case MVT::i16: |
Nick Lewycky | 70084fd | 2006-09-21 02:08:31 +0000 | [diff] [blame] | 1677 | case MVT::i32: { |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1678 | unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS); |
| 1679 | if (NumIntRegs < MaxNumIntRegs) { |
| 1680 | ++NumIntRegs; |
| 1681 | break; |
| 1682 | } |
Nick Lewycky | 70084fd | 2006-09-21 02:08:31 +0000 | [diff] [blame] | 1683 | } // Fall through |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1684 | case MVT::f32: |
| 1685 | NumBytes += 4; |
| 1686 | break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1687 | case MVT::f64: |
| 1688 | NumBytes += 8; |
| 1689 | break; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1690 | case MVT::v16i8: |
| 1691 | case MVT::v8i16: |
| 1692 | case MVT::v4i32: |
| 1693 | case MVT::v2i64: |
| 1694 | case MVT::v4f32: |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1695 | case MVT::v2f64: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1696 | assert(!isFastCall && "Unknown value type!"); |
| 1697 | if (NumXMMRegs < 4) |
| 1698 | NumXMMRegs++; |
| 1699 | else { |
| 1700 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1701 | NumBytes = ((NumBytes + 15) / 16) * 16; |
| 1702 | NumBytes += 16; |
| 1703 | } |
| 1704 | break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1705 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1706 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1707 | |
| 1708 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 1709 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 1710 | if ((NumBytes & 7) == 0) |
| 1711 | NumBytes += 4; |
| 1712 | |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 1713 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1714 | |
| 1715 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 1716 | unsigned ArgOffset = 0; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1717 | NumIntRegs = 0; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1718 | std::vector<std::pair<unsigned, SDOperand> > RegsToPass; |
| 1719 | std::vector<SDOperand> MemOpChains; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1720 | SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1721 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1722 | SDOperand Arg = Op.getOperand(5+2*i); |
| 1723 | |
| 1724 | switch (Arg.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1725 | default: assert(0 && "Unexpected ValueType for argument!"); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1726 | case MVT::i8: |
| 1727 | case MVT::i16: |
Nick Lewycky | 70084fd | 2006-09-21 02:08:31 +0000 | [diff] [blame] | 1728 | case MVT::i32: { |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1729 | unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS); |
| 1730 | if (NumIntRegs < MaxNumIntRegs) { |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1731 | unsigned RegToUse = |
| 1732 | GPRArgRegs[Arg.getValueType()-MVT::i8][GPRInd][NumIntRegs]; |
| 1733 | RegsToPass.push_back(std::make_pair(RegToUse, Arg)); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1734 | ++NumIntRegs; |
| 1735 | break; |
| 1736 | } |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1737 | } // Fall through |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1738 | case MVT::f32: { |
| 1739 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1740 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1741 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1742 | ArgOffset += 4; |
| 1743 | break; |
| 1744 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1745 | case MVT::f64: { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1746 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1747 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1748 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1749 | ArgOffset += 8; |
| 1750 | break; |
| 1751 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1752 | case MVT::v16i8: |
| 1753 | case MVT::v8i16: |
| 1754 | case MVT::v4i32: |
| 1755 | case MVT::v2i64: |
| 1756 | case MVT::v4f32: |
Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 1757 | case MVT::v2f64: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1758 | assert(!isFastCall && "Unexpected ValueType for argument!"); |
| 1759 | if (NumXMMRegs < 4) { |
| 1760 | RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg)); |
| 1761 | NumXMMRegs++; |
| 1762 | } else { |
| 1763 | // XMM arguments have to be aligned on 16-byte boundary. |
| 1764 | ArgOffset = ((ArgOffset + 15) / 16) * 16; |
| 1765 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 1766 | PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff); |
| 1767 | MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0)); |
| 1768 | ArgOffset += 16; |
| 1769 | } |
| 1770 | break; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1771 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1772 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1773 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1774 | if (!MemOpChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1775 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 1776 | &MemOpChains[0], MemOpChains.size()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1777 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1778 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 1779 | // and flag operands which copy the outgoing args into registers. |
| 1780 | SDOperand InFlag; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1781 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 1782 | Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second, |
| 1783 | InFlag); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1784 | InFlag = Chain.getValue(1); |
| 1785 | } |
| 1786 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1787 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1788 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1789 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 1790 | // We should use extra load for direct calls to dllimported functions in |
| 1791 | // non-JIT mode. |
| 1792 | if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(), |
| 1793 | getTargetMachine(), true)) |
Anton Korobeynikov | a598685 | 2006-11-20 10:46:14 +0000 | [diff] [blame] | 1794 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
| 1795 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1796 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
| 1797 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1798 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 1799 | // GOT pointer. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1800 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1801 | Subtarget->isPICStyleGOT()) { |
| 1802 | Chain = DAG.getCopyToReg(Chain, X86::EBX, |
| 1803 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 1804 | InFlag); |
| 1805 | InFlag = Chain.getValue(1); |
| 1806 | } |
| 1807 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1808 | std::vector<MVT::ValueType> NodeTys; |
| 1809 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 1810 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 1811 | std::vector<SDOperand> Ops; |
| 1812 | Ops.push_back(Chain); |
| 1813 | Ops.push_back(Callee); |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Add argument registers to the end of the list so that they are known live |
| 1816 | // into the call. |
| 1817 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1818 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 1819 | RegsToPass[i].second.getValueType())); |
| 1820 | |
Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 1821 | // Add an implicit use GOT pointer in EBX. |
| 1822 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1823 | Subtarget->isPICStyleGOT()) |
| 1824 | Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy())); |
| 1825 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1826 | if (InFlag.Val) |
| 1827 | Ops.push_back(InFlag); |
| 1828 | |
| 1829 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 1830 | Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1831 | NodeTys, &Ops[0], Ops.size()); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1832 | InFlag = Chain.getValue(1); |
| 1833 | |
| 1834 | NodeTys.clear(); |
| 1835 | NodeTys.push_back(MVT::Other); // Returns a chain |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1836 | if (RetVT != MVT::Other) |
| 1837 | 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] | 1838 | Ops.clear(); |
| 1839 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1840 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 1841 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1842 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1843 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1844 | if (RetVT != MVT::Other) |
| 1845 | InFlag = Chain.getValue(1); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1846 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1847 | std::vector<SDOperand> ResultVals; |
| 1848 | NodeTys.clear(); |
| 1849 | switch (RetVT) { |
| 1850 | default: assert(0 && "Unknown value type to return!"); |
| 1851 | case MVT::Other: break; |
| 1852 | case MVT::i8: |
| 1853 | Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1); |
| 1854 | ResultVals.push_back(Chain.getValue(0)); |
| 1855 | NodeTys.push_back(MVT::i8); |
| 1856 | break; |
| 1857 | case MVT::i16: |
| 1858 | Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1); |
| 1859 | ResultVals.push_back(Chain.getValue(0)); |
| 1860 | NodeTys.push_back(MVT::i16); |
| 1861 | break; |
| 1862 | case MVT::i32: |
| 1863 | if (Op.Val->getValueType(1) == MVT::i32) { |
| 1864 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 1865 | ResultVals.push_back(Chain.getValue(0)); |
| 1866 | Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32, |
| 1867 | Chain.getValue(2)).getValue(1); |
| 1868 | ResultVals.push_back(Chain.getValue(0)); |
| 1869 | NodeTys.push_back(MVT::i32); |
| 1870 | } else { |
| 1871 | Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1); |
| 1872 | ResultVals.push_back(Chain.getValue(0)); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1873 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1874 | NodeTys.push_back(MVT::i32); |
| 1875 | break; |
| 1876 | case MVT::v16i8: |
| 1877 | case MVT::v8i16: |
| 1878 | case MVT::v4i32: |
| 1879 | case MVT::v2i64: |
| 1880 | case MVT::v4f32: |
| 1881 | case MVT::v2f64: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1882 | if (isFastCall) { |
| 1883 | assert(0 && "Unknown value type to return!"); |
| 1884 | } else { |
| 1885 | Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1); |
| 1886 | ResultVals.push_back(Chain.getValue(0)); |
| 1887 | NodeTys.push_back(RetVT); |
| 1888 | } |
| 1889 | break; |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1890 | case MVT::f32: |
| 1891 | case MVT::f64: { |
| 1892 | std::vector<MVT::ValueType> Tys; |
| 1893 | Tys.push_back(MVT::f64); |
| 1894 | Tys.push_back(MVT::Other); |
| 1895 | Tys.push_back(MVT::Flag); |
| 1896 | std::vector<SDOperand> Ops; |
| 1897 | Ops.push_back(Chain); |
| 1898 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1899 | SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, |
| 1900 | &Ops[0], Ops.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1901 | Chain = RetVal.getValue(1); |
| 1902 | InFlag = RetVal.getValue(2); |
| 1903 | if (X86ScalarSSE) { |
| 1904 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 1905 | // shouldn't be necessary except that RFP cannot be live across |
| 1906 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 1907 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1908 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 1909 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1910 | Tys.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1911 | Tys.push_back(MVT::Other); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1912 | Ops.clear(); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1913 | Ops.push_back(Chain); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1914 | Ops.push_back(RetVal); |
| 1915 | Ops.push_back(StackSlot); |
| 1916 | Ops.push_back(DAG.getValueType(RetVT)); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1917 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1918 | Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1919 | RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1920 | Chain = RetVal.getValue(1); |
| 1921 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1922 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1923 | if (RetVT == MVT::f32 && !X86ScalarSSE) |
| 1924 | // FIXME: we would really like to remember that this FP_ROUND |
| 1925 | // operation is okay to eliminate if we allow excess FP precision. |
| 1926 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 1927 | ResultVals.push_back(RetVal); |
| 1928 | NodeTys.push_back(RetVT); |
| 1929 | break; |
| 1930 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1931 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1932 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1933 | |
| 1934 | // If the function returns void, just return the chain. |
| 1935 | if (ResultVals.empty()) |
| 1936 | return Chain; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1937 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1938 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 1939 | NodeTys.push_back(MVT::Other); |
| 1940 | ResultVals.push_back(Chain); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1941 | SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, |
| 1942 | &ResultVals[0], ResultVals.size()); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 1943 | return Res.getValue(Op.ResNo); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
| 1947 | if (ReturnAddrIndex == 0) { |
| 1948 | // Set up a frame object for the return address. |
| 1949 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1950 | if (Subtarget->is64Bit()) |
| 1951 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8); |
| 1952 | else |
| 1953 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1956 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | |
| 1960 | |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1961 | /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 1962 | /// specific condition code. It returns a false if it cannot do a direct |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1963 | /// translation. X86CC is the translated CondCode. LHS/RHS are modified as |
| 1964 | /// needed. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1965 | static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1966 | unsigned &X86CC, SDOperand &LHS, SDOperand &RHS, |
| 1967 | SelectionDAG &DAG) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1968 | X86CC = X86::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1969 | if (!isFP) { |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1970 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 1971 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 1972 | // X > -1 -> X == 0, jump !sign. |
| 1973 | RHS = DAG.getConstant(0, RHS.getValueType()); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1974 | X86CC = X86::COND_NS; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1975 | return true; |
| 1976 | } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| 1977 | // X < 0 -> X == 0, jump on sign. |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1978 | X86CC = X86::COND_S; |
Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 1979 | return true; |
| 1980 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 1981 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 1982 | |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1983 | switch (SetCCOpcode) { |
| 1984 | default: break; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 1985 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
| 1986 | case ISD::SETGT: X86CC = X86::COND_G; break; |
| 1987 | case ISD::SETGE: X86CC = X86::COND_GE; break; |
| 1988 | case ISD::SETLT: X86CC = X86::COND_L; break; |
| 1989 | case ISD::SETLE: X86CC = X86::COND_LE; break; |
| 1990 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 1991 | case ISD::SETULT: X86CC = X86::COND_B; break; |
| 1992 | case ISD::SETUGT: X86CC = X86::COND_A; break; |
| 1993 | case ISD::SETULE: X86CC = X86::COND_BE; break; |
| 1994 | case ISD::SETUGE: X86CC = X86::COND_AE; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } else { |
| 1997 | // On a floating point condition, the flags are set as follows: |
| 1998 | // ZF PF CF op |
| 1999 | // 0 | 0 | 0 | X > Y |
| 2000 | // 0 | 0 | 1 | X < Y |
| 2001 | // 1 | 0 | 0 | X == Y |
| 2002 | // 1 | 1 | 1 | unordered |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 2003 | bool Flip = false; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2004 | switch (SetCCOpcode) { |
| 2005 | default: break; |
| 2006 | case ISD::SETUEQ: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2007 | case ISD::SETEQ: X86CC = X86::COND_E; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 2008 | case ISD::SETOLT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2009 | case ISD::SETOGT: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2010 | case ISD::SETGT: X86CC = X86::COND_A; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 2011 | case ISD::SETOLE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2012 | case ISD::SETOGE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2013 | case ISD::SETGE: X86CC = X86::COND_AE; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 2014 | case ISD::SETUGT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2015 | case ISD::SETULT: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2016 | case ISD::SETLT: X86CC = X86::COND_B; break; |
Evan Cheng | 5001ea1 | 2006-04-17 07:24:10 +0000 | [diff] [blame] | 2017 | case ISD::SETUGE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2018 | case ISD::SETULE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2019 | case ISD::SETLE: X86CC = X86::COND_BE; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2020 | case ISD::SETONE: |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2021 | case ISD::SETNE: X86CC = X86::COND_NE; break; |
| 2022 | case ISD::SETUO: X86CC = X86::COND_P; break; |
| 2023 | case ISD::SETO: X86CC = X86::COND_NP; break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2024 | } |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 2025 | if (Flip) |
| 2026 | std::swap(LHS, RHS); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2027 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 2028 | |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2029 | return X86CC != X86::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2032 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 2033 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2034 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 2035 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2036 | switch (X86CC) { |
| 2037 | default: |
| 2038 | return false; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 2039 | case X86::COND_B: |
| 2040 | case X86::COND_BE: |
| 2041 | case X86::COND_E: |
| 2042 | case X86::COND_P: |
| 2043 | case X86::COND_A: |
| 2044 | case X86::COND_AE: |
| 2045 | case X86::COND_NE: |
| 2046 | case X86::COND_NP: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 2047 | return true; |
| 2048 | } |
| 2049 | } |
| 2050 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2051 | /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2052 | /// 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] | 2053 | static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) { |
| 2054 | if (Op.getOpcode() == ISD::UNDEF) |
| 2055 | return true; |
| 2056 | |
| 2057 | unsigned Val = cast<ConstantSDNode>(Op)->getValue(); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2058 | return (Val >= Low && Val < Hi); |
| 2059 | } |
| 2060 | |
| 2061 | /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return |
| 2062 | /// true if Op is undef or if its value equal to the specified value. |
| 2063 | static bool isUndefOrEqual(SDOperand Op, unsigned Val) { |
| 2064 | if (Op.getOpcode() == ISD::UNDEF) |
| 2065 | return true; |
| 2066 | return cast<ConstantSDNode>(Op)->getValue() == Val; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2069 | /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2070 | /// specifies a shuffle of elements that is suitable for input to PSHUFD. |
| 2071 | bool X86::isPSHUFDMask(SDNode *N) { |
| 2072 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2073 | |
| 2074 | if (N->getNumOperands() != 4) |
| 2075 | return false; |
| 2076 | |
| 2077 | // Check if the value doesn't reference the second vector. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2078 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2079 | SDOperand Arg = N->getOperand(i); |
| 2080 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2081 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2082 | if (cast<ConstantSDNode>(Arg)->getValue() >= 4) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | return true; |
| 2087 | } |
| 2088 | |
| 2089 | /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 2090 | /// specifies a shuffle of elements that is suitable for input to PSHUFHW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2091 | bool X86::isPSHUFHWMask(SDNode *N) { |
| 2092 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2093 | |
| 2094 | if (N->getNumOperands() != 8) |
| 2095 | return false; |
| 2096 | |
| 2097 | // Lower quadword copied in order. |
| 2098 | for (unsigned i = 0; i != 4; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2099 | SDOperand Arg = N->getOperand(i); |
| 2100 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2101 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2102 | if (cast<ConstantSDNode>(Arg)->getValue() != i) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2103 | return false; |
| 2104 | } |
| 2105 | |
| 2106 | // Upper quadword shuffled. |
| 2107 | for (unsigned i = 4; i != 8; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2108 | SDOperand Arg = N->getOperand(i); |
| 2109 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2110 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2111 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2112 | if (Val < 4 || Val > 7) |
| 2113 | return false; |
| 2114 | } |
| 2115 | |
| 2116 | return true; |
| 2117 | } |
| 2118 | |
| 2119 | /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 2120 | /// specifies a shuffle of elements that is suitable for input to PSHUFLW. |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2121 | bool X86::isPSHUFLWMask(SDNode *N) { |
| 2122 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2123 | |
| 2124 | if (N->getNumOperands() != 8) |
| 2125 | return false; |
| 2126 | |
| 2127 | // Upper quadword copied in order. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2128 | for (unsigned i = 4; i != 8; ++i) |
| 2129 | if (!isUndefOrEqual(N->getOperand(i), i)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2130 | return false; |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2131 | |
| 2132 | // Lower quadword shuffled. |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2133 | for (unsigned i = 0; i != 4; ++i) |
| 2134 | if (!isUndefOrInRange(N->getOperand(i), 0, 4)) |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2135 | return false; |
Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 2136 | |
| 2137 | return true; |
| 2138 | } |
| 2139 | |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2140 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2141 | /// specifies a shuffle of elements that is suitable for input to SHUFP*. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2142 | static bool isSHUFPMask(std::vector<SDOperand> &N) { |
| 2143 | unsigned NumElems = N.size(); |
| 2144 | if (NumElems != 2 && NumElems != 4) return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2145 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2146 | unsigned Half = NumElems / 2; |
| 2147 | for (unsigned i = 0; i < Half; ++i) |
| 2148 | if (!isUndefOrInRange(N[i], 0, NumElems)) |
| 2149 | return false; |
| 2150 | for (unsigned i = Half; i < NumElems; ++i) |
| 2151 | if (!isUndefOrInRange(N[i], NumElems, NumElems*2)) |
| 2152 | return false; |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2153 | |
| 2154 | return true; |
| 2155 | } |
| 2156 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2157 | bool X86::isSHUFPMask(SDNode *N) { |
| 2158 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2159 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 2160 | return ::isSHUFPMask(Ops); |
| 2161 | } |
| 2162 | |
| 2163 | /// isCommutedSHUFP - Returns true if the shuffle mask is except |
| 2164 | /// the reverse of what x86 shuffles want. x86 shuffles requires the lower |
| 2165 | /// half elements to come from vector 1 (which would equal the dest.) and |
| 2166 | /// the upper half to come from vector 2. |
| 2167 | static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) { |
| 2168 | unsigned NumElems = Ops.size(); |
| 2169 | if (NumElems != 2 && NumElems != 4) return false; |
| 2170 | |
| 2171 | unsigned Half = NumElems / 2; |
| 2172 | for (unsigned i = 0; i < Half; ++i) |
| 2173 | if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2)) |
| 2174 | return false; |
| 2175 | for (unsigned i = Half; i < NumElems; ++i) |
| 2176 | if (!isUndefOrInRange(Ops[i], 0, NumElems)) |
| 2177 | return false; |
| 2178 | return true; |
| 2179 | } |
| 2180 | |
| 2181 | static bool isCommutedSHUFP(SDNode *N) { |
| 2182 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2183 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 2184 | return isCommutedSHUFP(Ops); |
| 2185 | } |
| 2186 | |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2187 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2188 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
| 2189 | bool X86::isMOVHLPSMask(SDNode *N) { |
| 2190 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2191 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 2192 | if (N->getNumOperands() != 4) |
Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 2193 | return false; |
| 2194 | |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 2195 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2196 | return isUndefOrEqual(N->getOperand(0), 6) && |
| 2197 | isUndefOrEqual(N->getOperand(1), 7) && |
| 2198 | isUndefOrEqual(N->getOperand(2), 2) && |
| 2199 | isUndefOrEqual(N->getOperand(3), 3); |
Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 2202 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 2203 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 2204 | /// <2, 3, 2, 3> |
| 2205 | bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) { |
| 2206 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2207 | |
| 2208 | if (N->getNumOperands() != 4) |
| 2209 | return false; |
| 2210 | |
| 2211 | // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3 |
| 2212 | return isUndefOrEqual(N->getOperand(0), 2) && |
| 2213 | isUndefOrEqual(N->getOperand(1), 3) && |
| 2214 | isUndefOrEqual(N->getOperand(2), 2) && |
| 2215 | isUndefOrEqual(N->getOperand(3), 3); |
| 2216 | } |
| 2217 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2218 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2219 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
| 2220 | bool X86::isMOVLPMask(SDNode *N) { |
| 2221 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2222 | |
| 2223 | unsigned NumElems = N->getNumOperands(); |
| 2224 | if (NumElems != 2 && NumElems != 4) |
| 2225 | return false; |
| 2226 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2227 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2228 | if (!isUndefOrEqual(N->getOperand(i), i + NumElems)) |
| 2229 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2230 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2231 | for (unsigned i = NumElems/2; i < NumElems; ++i) |
| 2232 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 2233 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2234 | |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
| 2238 | /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2239 | /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D} |
| 2240 | /// and MOVLHPS. |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2241 | bool X86::isMOVHPMask(SDNode *N) { |
| 2242 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2243 | |
| 2244 | unsigned NumElems = N->getNumOperands(); |
| 2245 | if (NumElems != 2 && NumElems != 4) |
| 2246 | return false; |
| 2247 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2248 | for (unsigned i = 0; i < NumElems/2; ++i) |
| 2249 | if (!isUndefOrEqual(N->getOperand(i), i)) |
| 2250 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2251 | |
| 2252 | for (unsigned i = 0; i < NumElems/2; ++i) { |
| 2253 | SDOperand Arg = N->getOperand(i + NumElems/2); |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2254 | if (!isUndefOrEqual(Arg, i + NumElems)) |
| 2255 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | return true; |
| 2259 | } |
| 2260 | |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2261 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2262 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2263 | bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) { |
| 2264 | unsigned NumElems = N.size(); |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2265 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2266 | return false; |
| 2267 | |
| 2268 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2269 | SDOperand BitI = N[i]; |
| 2270 | SDOperand BitI1 = N[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2271 | if (!isUndefOrEqual(BitI, j)) |
| 2272 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2273 | if (V2IsSplat) { |
| 2274 | if (isUndefOrEqual(BitI1, NumElems)) |
| 2275 | return false; |
| 2276 | } else { |
| 2277 | if (!isUndefOrEqual(BitI1, j + NumElems)) |
| 2278 | return false; |
| 2279 | } |
Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | return true; |
| 2283 | } |
| 2284 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2285 | bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) { |
| 2286 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2287 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 2288 | return ::isUNPCKLMask(Ops, V2IsSplat); |
| 2289 | } |
| 2290 | |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2291 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2292 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2293 | bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) { |
| 2294 | unsigned NumElems = N.size(); |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2295 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2296 | return false; |
| 2297 | |
| 2298 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2299 | SDOperand BitI = N[i]; |
| 2300 | SDOperand BitI1 = N[i+1]; |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2301 | if (!isUndefOrEqual(BitI, j + NumElems/2)) |
| 2302 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2303 | if (V2IsSplat) { |
| 2304 | if (isUndefOrEqual(BitI1, NumElems)) |
| 2305 | return false; |
| 2306 | } else { |
| 2307 | if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems)) |
| 2308 | return false; |
| 2309 | } |
Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | return true; |
| 2313 | } |
| 2314 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2315 | bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) { |
| 2316 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2317 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 2318 | return ::isUNPCKHMask(Ops, V2IsSplat); |
| 2319 | } |
| 2320 | |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2321 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 2322 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 2323 | /// <0, 0, 1, 1> |
| 2324 | bool X86::isUNPCKL_v_undef_Mask(SDNode *N) { |
| 2325 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2326 | |
| 2327 | unsigned NumElems = N->getNumOperands(); |
| 2328 | if (NumElems != 4 && NumElems != 8 && NumElems != 16) |
| 2329 | return false; |
| 2330 | |
| 2331 | for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) { |
| 2332 | SDOperand BitI = N->getOperand(i); |
| 2333 | SDOperand BitI1 = N->getOperand(i+1); |
| 2334 | |
Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 2335 | if (!isUndefOrEqual(BitI, j)) |
| 2336 | return false; |
| 2337 | if (!isUndefOrEqual(BitI1, j)) |
| 2338 | return false; |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | return true; |
| 2342 | } |
| 2343 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2344 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2345 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 2346 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
| 2347 | static bool isMOVLMask(std::vector<SDOperand> &N) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2348 | unsigned NumElems = N.size(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2349 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2350 | return false; |
| 2351 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2352 | if (!isUndefOrEqual(N[0], NumElems)) |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2353 | return false; |
| 2354 | |
| 2355 | for (unsigned i = 1; i < NumElems; ++i) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2356 | SDOperand Arg = N[i]; |
Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 2357 | if (!isUndefOrEqual(Arg, i)) |
| 2358 | return false; |
| 2359 | } |
| 2360 | |
| 2361 | return true; |
| 2362 | } |
Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 2363 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2364 | bool X86::isMOVLMask(SDNode *N) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2365 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2366 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2367 | return ::isMOVLMask(Ops); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2370 | /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse |
| 2371 | /// 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] | 2372 | /// element of vector 2 and the other elements to come from vector 1 in order. |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2373 | static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false, |
| 2374 | bool V2IsUndef = false) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2375 | unsigned NumElems = Ops.size(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2376 | if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16) |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2377 | return false; |
| 2378 | |
| 2379 | if (!isUndefOrEqual(Ops[0], 0)) |
| 2380 | return false; |
| 2381 | |
| 2382 | for (unsigned i = 1; i < NumElems; ++i) { |
| 2383 | SDOperand Arg = Ops[i]; |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2384 | if (!(isUndefOrEqual(Arg, i+NumElems) || |
| 2385 | (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) || |
| 2386 | (V2IsSplat && isUndefOrEqual(Arg, NumElems)))) |
| 2387 | return false; |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | return true; |
| 2391 | } |
| 2392 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2393 | static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false, |
| 2394 | bool V2IsUndef = false) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2395 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2396 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2397 | return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2400 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2401 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
| 2402 | bool X86::isMOVSHDUPMask(SDNode *N) { |
| 2403 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2404 | |
| 2405 | if (N->getNumOperands() != 4) |
| 2406 | return false; |
| 2407 | |
| 2408 | // Expect 1, 1, 3, 3 |
| 2409 | for (unsigned i = 0; i < 2; ++i) { |
| 2410 | SDOperand Arg = N->getOperand(i); |
| 2411 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2412 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2413 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2414 | if (Val != 1) return false; |
| 2415 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2416 | |
| 2417 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2418 | for (unsigned i = 2; i < 4; ++i) { |
| 2419 | SDOperand Arg = N->getOperand(i); |
| 2420 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2421 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2422 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2423 | if (Val != 3) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2424 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2425 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 2426 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2427 | // Don't use movshdup if it can be done with a shufps. |
| 2428 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2432 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
| 2433 | bool X86::isMOVSLDUPMask(SDNode *N) { |
| 2434 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2435 | |
| 2436 | if (N->getNumOperands() != 4) |
| 2437 | return false; |
| 2438 | |
| 2439 | // Expect 0, 0, 2, 2 |
| 2440 | for (unsigned i = 0; i < 2; ++i) { |
| 2441 | SDOperand Arg = N->getOperand(i); |
| 2442 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2443 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2444 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2445 | if (Val != 0) return false; |
| 2446 | } |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2447 | |
| 2448 | bool HasHi = false; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2449 | for (unsigned i = 2; i < 4; ++i) { |
| 2450 | SDOperand Arg = N->getOperand(i); |
| 2451 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2452 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2453 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2454 | if (Val != 2) return false; |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2455 | HasHi = true; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2456 | } |
Evan Cheng | 39fc145 | 2006-04-15 03:13:24 +0000 | [diff] [blame] | 2457 | |
Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 2458 | // Don't use movshdup if it can be done with a shufps. |
| 2459 | return HasHi; |
Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2462 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2463 | /// a splat of a single element. |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2464 | static bool isSplatMask(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2465 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2466 | |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2467 | // This is a splat operation if each element of the permute is the same, and |
| 2468 | // if the value doesn't reference the second vector. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2469 | unsigned NumElems = N->getNumOperands(); |
| 2470 | SDOperand ElementBase; |
| 2471 | unsigned i = 0; |
| 2472 | for (; i != NumElems; ++i) { |
| 2473 | SDOperand Elt = N->getOperand(i); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 2474 | if (isa<ConstantSDNode>(Elt)) { |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2475 | ElementBase = Elt; |
| 2476 | break; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | if (!ElementBase.Val) |
| 2481 | return false; |
| 2482 | |
| 2483 | for (; i != NumElems; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2484 | SDOperand Arg = N->getOperand(i); |
| 2485 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2486 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2487 | if (Arg != ElementBase) return false; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | // Make sure it is a splat of the first vector operand. |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2491 | return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems; |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2494 | /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies |
| 2495 | /// a splat of a single element and it's a 2 or 4 element mask. |
| 2496 | bool X86::isSplatMask(SDNode *N) { |
| 2497 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2498 | |
Evan Cheng | 94fe5eb | 2006-04-19 23:28:59 +0000 | [diff] [blame] | 2499 | // 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] | 2500 | if (N->getNumOperands() != 4 && N->getNumOperands() != 2) |
| 2501 | return false; |
| 2502 | return ::isSplatMask(N); |
| 2503 | } |
| 2504 | |
Evan Cheng | f686d9b | 2006-10-27 21:08:32 +0000 | [diff] [blame] | 2505 | /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand |
| 2506 | /// specifies a splat of zero element. |
| 2507 | bool X86::isSplatLoMask(SDNode *N) { |
| 2508 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2509 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2510 | for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) |
Evan Cheng | f686d9b | 2006-10-27 21:08:32 +0000 | [diff] [blame] | 2511 | if (!isUndefOrEqual(N->getOperand(i), 0)) |
| 2512 | return false; |
| 2513 | return true; |
| 2514 | } |
| 2515 | |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2516 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| 2517 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP* |
| 2518 | /// instructions. |
| 2519 | unsigned X86::getShuffleSHUFImmediate(SDNode *N) { |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 2520 | unsigned NumOperands = N->getNumOperands(); |
| 2521 | unsigned Shift = (NumOperands == 4) ? 2 : 1; |
| 2522 | unsigned Mask = 0; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2523 | for (unsigned i = 0; i < NumOperands; ++i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2524 | unsigned Val = 0; |
| 2525 | SDOperand Arg = N->getOperand(NumOperands-i-1); |
| 2526 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2527 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 14aed5e | 2006-03-24 01:18:28 +0000 | [diff] [blame] | 2528 | if (Val >= NumOperands) Val -= NumOperands; |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2529 | Mask |= Val; |
Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 2530 | if (i != NumOperands - 1) |
| 2531 | Mask <<= Shift; |
| 2532 | } |
Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 2533 | |
| 2534 | return Mask; |
| 2535 | } |
| 2536 | |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2537 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| 2538 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW |
| 2539 | /// instructions. |
| 2540 | unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) { |
| 2541 | unsigned Mask = 0; |
| 2542 | // 8 nodes, but we only care about the last 4. |
| 2543 | for (unsigned i = 7; i >= 4; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2544 | unsigned Val = 0; |
| 2545 | SDOperand Arg = N->getOperand(i); |
| 2546 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2547 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2548 | Mask |= (Val - 4); |
| 2549 | if (i != 4) |
| 2550 | Mask <<= 2; |
| 2551 | } |
| 2552 | |
| 2553 | return Mask; |
| 2554 | } |
| 2555 | |
| 2556 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| 2557 | /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW |
| 2558 | /// instructions. |
| 2559 | unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) { |
| 2560 | unsigned Mask = 0; |
| 2561 | // 8 nodes, but we only care about the first 4. |
| 2562 | for (int i = 3; i >= 0; --i) { |
Evan Cheng | ef698ca | 2006-03-31 00:30:29 +0000 | [diff] [blame] | 2563 | unsigned Val = 0; |
| 2564 | SDOperand Arg = N->getOperand(i); |
| 2565 | if (Arg.getOpcode() != ISD::UNDEF) |
| 2566 | Val = cast<ConstantSDNode>(Arg)->getValue(); |
Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 2567 | Mask |= Val; |
| 2568 | if (i != 0) |
| 2569 | Mask <<= 2; |
| 2570 | } |
| 2571 | |
| 2572 | return Mask; |
| 2573 | } |
| 2574 | |
Evan Cheng | c21a053 | 2006-04-05 01:47:37 +0000 | [diff] [blame] | 2575 | /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand |
| 2576 | /// specifies a 8 element shuffle that can be broken into a pair of |
| 2577 | /// PSHUFHW and PSHUFLW. |
| 2578 | static bool isPSHUFHW_PSHUFLWMask(SDNode *N) { |
| 2579 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
| 2580 | |
| 2581 | if (N->getNumOperands() != 8) |
| 2582 | return false; |
| 2583 | |
| 2584 | // Lower quadword shuffled. |
| 2585 | for (unsigned i = 0; i != 4; ++i) { |
| 2586 | SDOperand Arg = N->getOperand(i); |
| 2587 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2588 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2589 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2590 | if (Val > 4) |
| 2591 | return false; |
| 2592 | } |
| 2593 | |
| 2594 | // Upper quadword shuffled. |
| 2595 | for (unsigned i = 4; i != 8; ++i) { |
| 2596 | SDOperand Arg = N->getOperand(i); |
| 2597 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 2598 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2599 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2600 | if (Val < 4 || Val > 7) |
| 2601 | return false; |
| 2602 | } |
| 2603 | |
| 2604 | return true; |
| 2605 | } |
| 2606 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2607 | /// CommuteVectorShuffle - Swap vector_shuffle operandsas well as |
| 2608 | /// values in ther permute mask. |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2609 | static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1, |
| 2610 | SDOperand &V2, SDOperand &Mask, |
| 2611 | SelectionDAG &DAG) { |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2612 | MVT::ValueType VT = Op.getValueType(); |
| 2613 | MVT::ValueType MaskVT = Mask.getValueType(); |
| 2614 | MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT); |
| 2615 | unsigned NumElems = Mask.getNumOperands(); |
| 2616 | std::vector<SDOperand> MaskVec; |
| 2617 | |
| 2618 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2619 | SDOperand Arg = Mask.getOperand(i); |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 2620 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 2621 | MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 2622 | continue; |
| 2623 | } |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2624 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 2625 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2626 | if (Val < NumElems) |
| 2627 | MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT)); |
| 2628 | else |
| 2629 | MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT)); |
| 2630 | } |
| 2631 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2632 | std::swap(V1, V2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2633 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 2634 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2637 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 2638 | /// match movhlps. The lower half elements should come from upper half of |
| 2639 | /// V1 (and in order), and the upper half elements should come from the upper |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2640 | /// half of V2 (and in order). |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2641 | static bool ShouldXformToMOVHLPS(SDNode *Mask) { |
| 2642 | unsigned NumElems = Mask->getNumOperands(); |
| 2643 | if (NumElems != 4) |
| 2644 | return false; |
| 2645 | for (unsigned i = 0, e = 2; i != e; ++i) |
| 2646 | if (!isUndefOrEqual(Mask->getOperand(i), i+2)) |
| 2647 | return false; |
| 2648 | for (unsigned i = 2; i != 4; ++i) |
| 2649 | if (!isUndefOrEqual(Mask->getOperand(i), i+4)) |
| 2650 | return false; |
| 2651 | return true; |
| 2652 | } |
| 2653 | |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2654 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
| 2655 | /// is promoted to a vector. |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2656 | static inline bool isScalarLoadToVector(SDNode *N) { |
| 2657 | if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 2658 | N = N->getOperand(0).Val; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2659 | return ISD::isNON_EXTLoad(N); |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2660 | } |
| 2661 | return false; |
| 2662 | } |
| 2663 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2664 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 2665 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 2666 | /// V1 (and in order), and the upper half elements should come from the upper |
| 2667 | /// half of V2 (and in order). And since V1 will become the source of the |
| 2668 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2669 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2670 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2671 | return false; |
Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 2672 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 2673 | // load folding shufps op. |
| 2674 | if (ISD::isNON_EXTLoad(V2)) |
| 2675 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2676 | |
Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 2677 | unsigned NumElems = Mask->getNumOperands(); |
| 2678 | if (NumElems != 2 && NumElems != 4) |
| 2679 | return false; |
| 2680 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| 2681 | if (!isUndefOrEqual(Mask->getOperand(i), i)) |
| 2682 | return false; |
| 2683 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
| 2684 | if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems)) |
| 2685 | return false; |
| 2686 | return true; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2689 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 2690 | /// all the same. |
| 2691 | static bool isSplatVector(SDNode *N) { |
| 2692 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2693 | return false; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2694 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2695 | SDOperand SplatValue = N->getOperand(0); |
| 2696 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 2697 | if (N->getOperand(i) != SplatValue) |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 2698 | return false; |
| 2699 | return true; |
| 2700 | } |
| 2701 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 2702 | /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| 2703 | /// to an undef. |
| 2704 | static bool isUndefShuffle(SDNode *N) { |
| 2705 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 2706 | return false; |
| 2707 | |
| 2708 | SDOperand V1 = N->getOperand(0); |
| 2709 | SDOperand V2 = N->getOperand(1); |
| 2710 | SDOperand Mask = N->getOperand(2); |
| 2711 | unsigned NumElems = Mask.getNumOperands(); |
| 2712 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2713 | SDOperand Arg = Mask.getOperand(i); |
| 2714 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2715 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2716 | if (Val < NumElems && V1.getOpcode() != ISD::UNDEF) |
| 2717 | return false; |
| 2718 | else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF) |
| 2719 | return false; |
| 2720 | } |
| 2721 | } |
| 2722 | return true; |
| 2723 | } |
| 2724 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2725 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 2726 | /// that point to V2 points to its first element. |
| 2727 | static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) { |
| 2728 | assert(Mask.getOpcode() == ISD::BUILD_VECTOR); |
| 2729 | |
| 2730 | bool Changed = false; |
| 2731 | std::vector<SDOperand> MaskVec; |
| 2732 | unsigned NumElems = Mask.getNumOperands(); |
| 2733 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2734 | SDOperand Arg = Mask.getOperand(i); |
| 2735 | if (Arg.getOpcode() != ISD::UNDEF) { |
| 2736 | unsigned Val = cast<ConstantSDNode>(Arg)->getValue(); |
| 2737 | if (Val > NumElems) { |
| 2738 | Arg = DAG.getConstant(NumElems, Arg.getValueType()); |
| 2739 | Changed = true; |
| 2740 | } |
| 2741 | } |
| 2742 | MaskVec.push_back(Arg); |
| 2743 | } |
| 2744 | |
| 2745 | if (Changed) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2746 | Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), |
| 2747 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2748 | return Mask; |
| 2749 | } |
| 2750 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2751 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 2752 | /// operation of specified width. |
| 2753 | static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) { |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2754 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2755 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2756 | |
| 2757 | std::vector<SDOperand> MaskVec; |
| 2758 | MaskVec.push_back(DAG.getConstant(NumElems, BaseVT)); |
| 2759 | for (unsigned i = 1; i != NumElems; ++i) |
| 2760 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2761 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2764 | /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation |
| 2765 | /// of specified width. |
| 2766 | static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2767 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2768 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2769 | std::vector<SDOperand> MaskVec; |
| 2770 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
| 2771 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 2772 | MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT)); |
| 2773 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2774 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2777 | /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation |
| 2778 | /// of specified width. |
| 2779 | static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) { |
| 2780 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2781 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 2782 | unsigned Half = NumElems/2; |
| 2783 | std::vector<SDOperand> MaskVec; |
| 2784 | for (unsigned i = 0; i != Half; ++i) { |
| 2785 | MaskVec.push_back(DAG.getConstant(i + Half, BaseVT)); |
| 2786 | MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT)); |
| 2787 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2788 | return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size()); |
Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2791 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 2792 | /// |
| 2793 | static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) { |
| 2794 | assert(MVT::isVector(VT) && "Expected a vector type"); |
| 2795 | unsigned NumElems = getVectorNumElements(VT); |
| 2796 | MVT::ValueType EVT = MVT::getVectorBaseType(VT); |
| 2797 | bool isFP = MVT::isFloatingPoint(EVT); |
| 2798 | SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT); |
| 2799 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2800 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size()); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2803 | /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32. |
| 2804 | /// |
| 2805 | static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) { |
| 2806 | SDOperand V1 = Op.getOperand(0); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2807 | SDOperand Mask = Op.getOperand(2); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2808 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2809 | unsigned NumElems = Mask.getNumOperands(); |
| 2810 | Mask = getUnpacklMask(NumElems, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2811 | while (NumElems != 4) { |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2812 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2813 | NumElems >>= 1; |
| 2814 | } |
| 2815 | V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1); |
| 2816 | |
| 2817 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2818 | Mask = getZeroVector(MaskVT, DAG); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2819 | SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2820 | DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask); |
Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 2821 | return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle); |
| 2822 | } |
| 2823 | |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2824 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 2825 | /// constant +0.0. |
| 2826 | static inline bool isZeroNode(SDOperand Elt) { |
| 2827 | return ((isa<ConstantSDNode>(Elt) && |
| 2828 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 2829 | (isa<ConstantFPSDNode>(Elt) && |
| 2830 | cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0))); |
| 2831 | } |
| 2832 | |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2833 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
| 2834 | /// vector and zero or undef vector. |
| 2835 | static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT, |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2836 | unsigned NumElems, unsigned Idx, |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2837 | bool isZero, SelectionDAG &DAG) { |
| 2838 | SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2839 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2840 | MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); |
| 2841 | SDOperand Zero = DAG.getConstant(0, EVT); |
| 2842 | std::vector<SDOperand> MaskVec(NumElems, Zero); |
| 2843 | MaskVec[Idx] = DAG.getConstant(NumElems, EVT); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2844 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2845 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 2846 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2849 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 2850 | /// |
| 2851 | static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros, |
| 2852 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2853 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2854 | if (NumNonZero > 8) |
| 2855 | return SDOperand(); |
| 2856 | |
| 2857 | SDOperand V(0, 0); |
| 2858 | bool First = true; |
| 2859 | for (unsigned i = 0; i < 16; ++i) { |
| 2860 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 2861 | if (ThisIsNonZero && First) { |
| 2862 | if (NumZero) |
| 2863 | V = getZeroVector(MVT::v8i16, DAG); |
| 2864 | else |
| 2865 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2866 | First = false; |
| 2867 | } |
| 2868 | |
| 2869 | if ((i & 1) != 0) { |
| 2870 | SDOperand ThisElt(0, 0), LastElt(0, 0); |
| 2871 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 2872 | if (LastIsNonZero) { |
| 2873 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1)); |
| 2874 | } |
| 2875 | if (ThisIsNonZero) { |
| 2876 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i)); |
| 2877 | ThisElt = DAG.getNode(ISD::SHL, MVT::i16, |
| 2878 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| 2879 | if (LastIsNonZero) |
| 2880 | ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt); |
| 2881 | } else |
| 2882 | ThisElt = LastElt; |
| 2883 | |
| 2884 | if (ThisElt.Val) |
| 2885 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2886 | DAG.getConstant(i/2, TLI.getPointerTy())); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V); |
| 2891 | } |
| 2892 | |
| 2893 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16. |
| 2894 | /// |
| 2895 | static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros, |
| 2896 | unsigned NumNonZero, unsigned NumZero, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2897 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2898 | if (NumNonZero > 4) |
| 2899 | return SDOperand(); |
| 2900 | |
| 2901 | SDOperand V(0, 0); |
| 2902 | bool First = true; |
| 2903 | for (unsigned i = 0; i < 8; ++i) { |
| 2904 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 2905 | if (isNonZero) { |
| 2906 | if (First) { |
| 2907 | if (NumZero) |
| 2908 | V = getZeroVector(MVT::v8i16, DAG); |
| 2909 | else |
| 2910 | V = DAG.getNode(ISD::UNDEF, MVT::v8i16); |
| 2911 | First = false; |
| 2912 | } |
| 2913 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i), |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2914 | DAG.getConstant(i, TLI.getPointerTy())); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | return V; |
| 2919 | } |
| 2920 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2921 | SDOperand |
| 2922 | X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 2923 | // All zero's are handled with pxor. |
| 2924 | if (ISD::isBuildVectorAllZeros(Op.Val)) |
| 2925 | return Op; |
| 2926 | |
| 2927 | // All one's are handled with pcmpeqd. |
| 2928 | if (ISD::isBuildVectorAllOnes(Op.Val)) |
| 2929 | return Op; |
| 2930 | |
| 2931 | MVT::ValueType VT = Op.getValueType(); |
| 2932 | MVT::ValueType EVT = MVT::getVectorBaseType(VT); |
| 2933 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
| 2934 | |
| 2935 | unsigned NumElems = Op.getNumOperands(); |
| 2936 | unsigned NumZero = 0; |
| 2937 | unsigned NumNonZero = 0; |
| 2938 | unsigned NonZeros = 0; |
| 2939 | std::set<SDOperand> Values; |
| 2940 | for (unsigned i = 0; i < NumElems; ++i) { |
| 2941 | SDOperand Elt = Op.getOperand(i); |
| 2942 | if (Elt.getOpcode() != ISD::UNDEF) { |
| 2943 | Values.insert(Elt); |
| 2944 | if (isZeroNode(Elt)) |
| 2945 | NumZero++; |
| 2946 | else { |
| 2947 | NonZeros |= (1 << i); |
| 2948 | NumNonZero++; |
| 2949 | } |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | if (NumNonZero == 0) |
| 2954 | // Must be a mix of zero and undef. Return a zero vector. |
| 2955 | return getZeroVector(VT, DAG); |
| 2956 | |
| 2957 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| 2958 | if (Values.size() == 1) |
| 2959 | return SDOperand(); |
| 2960 | |
| 2961 | // Special case for single non-zero element. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 2962 | if (NumNonZero == 1) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2963 | unsigned Idx = CountTrailingZeros_32(NonZeros); |
| 2964 | SDOperand Item = Op.getOperand(Idx); |
| 2965 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item); |
| 2966 | if (Idx == 0) |
| 2967 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| 2968 | return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx, |
| 2969 | NumZero > 0, DAG); |
| 2970 | |
| 2971 | if (EVTBits == 32) { |
| 2972 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
| 2973 | Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0, |
| 2974 | DAG); |
| 2975 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 2976 | MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); |
| 2977 | std::vector<SDOperand> MaskVec; |
| 2978 | for (unsigned i = 0; i < NumElems; i++) |
| 2979 | MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2980 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 2981 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2982 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item, |
| 2983 | DAG.getNode(ISD::UNDEF, VT), Mask); |
| 2984 | } |
| 2985 | } |
| 2986 | |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 2987 | // Let legalizer expand 2-wide build_vector's. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2988 | if (EVTBits == 64) |
| 2989 | return SDOperand(); |
| 2990 | |
| 2991 | // If element VT is < 32 bits, convert it to inserts into a zero vector. |
| 2992 | if (EVTBits == 8) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2993 | SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 2994 | *this); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 2995 | if (V.Val) return V; |
| 2996 | } |
| 2997 | |
| 2998 | if (EVTBits == 16) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2999 | SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
| 3000 | *this); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3001 | if (V.Val) return V; |
| 3002 | } |
| 3003 | |
| 3004 | // If element VT is == 32 bits, turn it into a number of shuffles. |
| 3005 | std::vector<SDOperand> V(NumElems); |
| 3006 | if (NumElems == 4 && NumZero > 0) { |
| 3007 | for (unsigned i = 0; i < 4; ++i) { |
| 3008 | bool isZero = !(NonZeros & (1 << i)); |
| 3009 | if (isZero) |
| 3010 | V[i] = getZeroVector(VT, DAG); |
| 3011 | else |
| 3012 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 3013 | } |
| 3014 | |
| 3015 | for (unsigned i = 0; i < 2; ++i) { |
| 3016 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 3017 | default: break; |
| 3018 | case 0: |
| 3019 | V[i] = V[i*2]; // Must be a zero vector. |
| 3020 | break; |
| 3021 | case 1: |
| 3022 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2], |
| 3023 | getMOVLMask(NumElems, DAG)); |
| 3024 | break; |
| 3025 | case 2: |
| 3026 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 3027 | getMOVLMask(NumElems, DAG)); |
| 3028 | break; |
| 3029 | case 3: |
| 3030 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1], |
| 3031 | getUnpacklMask(NumElems, DAG)); |
| 3032 | break; |
| 3033 | } |
| 3034 | } |
| 3035 | |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 3036 | // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3037 | // clears the upper bits. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3038 | // FIXME: we can do the same for v4f32 case when we know both parts of |
| 3039 | // the lower half come from scalar_to_vector (loadf32). We should do |
| 3040 | // that in post legalizer dag combiner with target specific hooks. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3041 | if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0) |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3042 | return V[0]; |
| 3043 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 3044 | MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT); |
| 3045 | std::vector<SDOperand> MaskVec; |
| 3046 | bool Reverse = (NonZeros & 0x3) == 2; |
| 3047 | for (unsigned i = 0; i < 2; ++i) |
| 3048 | if (Reverse) |
| 3049 | MaskVec.push_back(DAG.getConstant(1-i, EVT)); |
| 3050 | else |
| 3051 | MaskVec.push_back(DAG.getConstant(i, EVT)); |
| 3052 | Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 3053 | for (unsigned i = 0; i < 2; ++i) |
| 3054 | if (Reverse) |
| 3055 | MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT)); |
| 3056 | else |
| 3057 | MaskVec.push_back(DAG.getConstant(i+NumElems, EVT)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3058 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3059 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3060 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask); |
| 3061 | } |
| 3062 | |
| 3063 | if (Values.size() > 2) { |
| 3064 | // Expand into a number of unpckl*. |
| 3065 | // e.g. for v4f32 |
| 3066 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 3067 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 3068 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
| 3069 | SDOperand UnpckMask = getUnpacklMask(NumElems, DAG); |
| 3070 | for (unsigned i = 0; i < NumElems; ++i) |
| 3071 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i)); |
| 3072 | NumElems >>= 1; |
| 3073 | while (NumElems != 0) { |
| 3074 | for (unsigned i = 0; i < NumElems; ++i) |
| 3075 | V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems], |
| 3076 | UnpckMask); |
| 3077 | NumElems >>= 1; |
| 3078 | } |
| 3079 | return V[0]; |
| 3080 | } |
| 3081 | |
| 3082 | return SDOperand(); |
| 3083 | } |
| 3084 | |
| 3085 | SDOperand |
| 3086 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { |
| 3087 | SDOperand V1 = Op.getOperand(0); |
| 3088 | SDOperand V2 = Op.getOperand(1); |
| 3089 | SDOperand PermMask = Op.getOperand(2); |
| 3090 | MVT::ValueType VT = Op.getValueType(); |
| 3091 | unsigned NumElems = PermMask.getNumOperands(); |
| 3092 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| 3093 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 3094 | bool V1IsSplat = false; |
| 3095 | bool V2IsSplat = false; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3096 | |
Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 3097 | if (isUndefShuffle(Op.Val)) |
| 3098 | return DAG.getNode(ISD::UNDEF, VT); |
| 3099 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3100 | if (isSplatMask(PermMask.Val)) { |
| 3101 | if (NumElems <= 4) return Op; |
| 3102 | // Promote it to a v4i32 splat. |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3103 | return PromoteSplat(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3106 | if (X86::isMOVLMask(PermMask.Val)) |
| 3107 | return (V1IsUndef) ? V2 : Op; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3108 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3109 | if (X86::isMOVSHDUPMask(PermMask.Val) || |
| 3110 | X86::isMOVSLDUPMask(PermMask.Val) || |
| 3111 | X86::isMOVHLPSMask(PermMask.Val) || |
| 3112 | X86::isMOVHPMask(PermMask.Val) || |
| 3113 | X86::isMOVLPMask(PermMask.Val)) |
| 3114 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3115 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3116 | if (ShouldXformToMOVHLPS(PermMask.Val) || |
| 3117 | ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val)) |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3118 | return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3119 | |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3120 | bool Commuted = false; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3121 | V1IsSplat = isSplatVector(V1.Val); |
| 3122 | V2IsSplat = isSplatVector(V2.Val); |
| 3123 | if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) { |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3124 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3125 | std::swap(V1IsSplat, V2IsSplat); |
| 3126 | std::swap(V1IsUndef, V2IsUndef); |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3127 | Commuted = true; |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) { |
| 3131 | if (V2IsUndef) return V1; |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3132 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3133 | if (V2IsSplat) { |
| 3134 | // V2 is a splat, so the mask may be malformed. That is, it may point |
| 3135 | // to any V2 element. The instruction selectior won't like this. Get |
| 3136 | // a corrected mask and commute to form a proper MOVS{S|D}. |
| 3137 | SDOperand NewMask = getMOVLMask(NumElems, DAG); |
| 3138 | if (NewMask.Val != PermMask.Val) |
| 3139 | Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3140 | } |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3141 | return Op; |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 3142 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3143 | |
Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 3144 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
| 3145 | X86::isUNPCKLMask(PermMask.Val) || |
| 3146 | X86::isUNPCKHMask(PermMask.Val)) |
| 3147 | return Op; |
Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 3148 | |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3149 | if (V2IsSplat) { |
| 3150 | // Normalize mask so all entries that point to V2 points to its first |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3151 | // element then try to match unpck{h|l} again. If match, return a |
Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 3152 | // new vector_shuffle with the corrected mask. |
| 3153 | SDOperand NewMask = NormalizeMask(PermMask, DAG); |
| 3154 | if (NewMask.Val != PermMask.Val) { |
| 3155 | if (X86::isUNPCKLMask(PermMask.Val, true)) { |
| 3156 | SDOperand NewMask = getUnpacklMask(NumElems, DAG); |
| 3157 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
| 3158 | } else if (X86::isUNPCKHMask(PermMask.Val, true)) { |
| 3159 | SDOperand NewMask = getUnpackhMask(NumElems, DAG); |
| 3160 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | // Normalize the node to match x86 shuffle ops if needed |
Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 3166 | if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val)) |
| 3167 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3168 | |
| 3169 | if (Commuted) { |
| 3170 | // Commute is back and try unpck* again. |
| 3171 | Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG); |
| 3172 | if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) || |
| 3173 | X86::isUNPCKLMask(PermMask.Val) || |
| 3174 | X86::isUNPCKHMask(PermMask.Val)) |
| 3175 | return Op; |
| 3176 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3177 | |
| 3178 | // If VT is integer, try PSHUF* first, then SHUFP*. |
| 3179 | if (MVT::isInteger(VT)) { |
| 3180 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 3181 | X86::isPSHUFHWMask(PermMask.Val) || |
| 3182 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 3183 | if (V2.getOpcode() != ISD::UNDEF) |
| 3184 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 3185 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 3186 | return Op; |
| 3187 | } |
| 3188 | |
| 3189 | if (X86::isSHUFPMask(PermMask.Val)) |
| 3190 | return Op; |
| 3191 | |
| 3192 | // Handle v8i16 shuffle high / low shuffle node pair. |
| 3193 | if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) { |
| 3194 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 3195 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 3196 | std::vector<SDOperand> MaskVec; |
| 3197 | for (unsigned i = 0; i != 4; ++i) |
| 3198 | MaskVec.push_back(PermMask.getOperand(i)); |
| 3199 | for (unsigned i = 4; i != 8; ++i) |
| 3200 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3201 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3202 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3203 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 3204 | MaskVec.clear(); |
| 3205 | for (unsigned i = 0; i != 4; ++i) |
| 3206 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 3207 | for (unsigned i = 4; i != 8; ++i) |
| 3208 | MaskVec.push_back(PermMask.getOperand(i)); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3209 | Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3210 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask); |
| 3211 | } |
| 3212 | } else { |
| 3213 | // Floating point cases in the other order. |
| 3214 | if (X86::isSHUFPMask(PermMask.Val)) |
| 3215 | return Op; |
| 3216 | if (X86::isPSHUFDMask(PermMask.Val) || |
| 3217 | X86::isPSHUFHWMask(PermMask.Val) || |
| 3218 | X86::isPSHUFLWMask(PermMask.Val)) { |
| 3219 | if (V2.getOpcode() != ISD::UNDEF) |
| 3220 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, |
| 3221 | DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask); |
| 3222 | return Op; |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | if (NumElems == 4) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3227 | MVT::ValueType MaskVT = PermMask.getValueType(); |
| 3228 | MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 3229 | std::vector<std::pair<int, int> > Locs; |
| 3230 | Locs.reserve(NumElems); |
| 3231 | std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3232 | std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3233 | unsigned NumHi = 0; |
| 3234 | unsigned NumLo = 0; |
| 3235 | // If no more than two elements come from either vector. This can be |
| 3236 | // implemented with two shuffles. First shuffle gather the elements. |
| 3237 | // The second shuffle, which takes the first shuffle as both of its |
| 3238 | // vector operands, put the elements into the right order. |
| 3239 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3240 | SDOperand Elt = PermMask.getOperand(i); |
| 3241 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3242 | Locs[i] = std::make_pair(-1, -1); |
| 3243 | } else { |
| 3244 | unsigned Val = cast<ConstantSDNode>(Elt)->getValue(); |
| 3245 | if (Val < NumElems) { |
| 3246 | Locs[i] = std::make_pair(0, NumLo); |
| 3247 | Mask1[NumLo] = Elt; |
| 3248 | NumLo++; |
| 3249 | } else { |
| 3250 | Locs[i] = std::make_pair(1, NumHi); |
| 3251 | if (2+NumHi < NumElems) |
| 3252 | Mask1[2+NumHi] = Elt; |
| 3253 | NumHi++; |
| 3254 | } |
| 3255 | } |
| 3256 | } |
| 3257 | if (NumLo <= 2 && NumHi <= 2) { |
| 3258 | V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3259 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3260 | &Mask1[0], Mask1.size())); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 3261 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3262 | if (Locs[i].first == -1) |
| 3263 | continue; |
| 3264 | else { |
| 3265 | unsigned Idx = (i < NumElems/2) ? 0 : NumElems; |
| 3266 | Idx += Locs[i].first * (NumElems/2) + Locs[i].second; |
| 3267 | Mask2[i] = DAG.getConstant(Idx, MaskEVT); |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3272 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3273 | &Mask2[0], Mask2.size())); |
Evan Cheng | 43f3bd3 | 2006-04-28 07:03:38 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| 3277 | Locs.clear(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3278 | std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3279 | std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3280 | std::vector<SDOperand> *MaskPtr = &LoMask; |
| 3281 | unsigned MaskIdx = 0; |
| 3282 | unsigned LoIdx = 0; |
| 3283 | unsigned HiIdx = NumElems/2; |
| 3284 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3285 | if (i == NumElems/2) { |
| 3286 | MaskPtr = &HiMask; |
| 3287 | MaskIdx = 1; |
| 3288 | LoIdx = 0; |
| 3289 | HiIdx = NumElems/2; |
| 3290 | } |
| 3291 | SDOperand Elt = PermMask.getOperand(i); |
| 3292 | if (Elt.getOpcode() == ISD::UNDEF) { |
| 3293 | Locs[i] = std::make_pair(-1, -1); |
| 3294 | } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) { |
| 3295 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
| 3296 | (*MaskPtr)[LoIdx] = Elt; |
| 3297 | LoIdx++; |
| 3298 | } else { |
| 3299 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
| 3300 | (*MaskPtr)[HiIdx] = Elt; |
| 3301 | HiIdx++; |
| 3302 | } |
| 3303 | } |
| 3304 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3305 | SDOperand LoShuffle = |
| 3306 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3307 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3308 | &LoMask[0], LoMask.size())); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3309 | SDOperand HiShuffle = |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3310 | DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3311 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3312 | &HiMask[0], HiMask.size())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3313 | std::vector<SDOperand> MaskOps; |
| 3314 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3315 | if (Locs[i].first == -1) { |
| 3316 | MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT)); |
| 3317 | } else { |
| 3318 | unsigned Idx = Locs[i].first * NumElems + Locs[i].second; |
| 3319 | MaskOps.push_back(DAG.getConstant(Idx, MaskEVT)); |
| 3320 | } |
| 3321 | } |
| 3322 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3323 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3324 | &MaskOps[0], MaskOps.size())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | return SDOperand(); |
| 3328 | } |
| 3329 | |
| 3330 | SDOperand |
| 3331 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
| 3332 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| 3333 | return SDOperand(); |
| 3334 | |
| 3335 | MVT::ValueType VT = Op.getValueType(); |
| 3336 | // TODO: handle v16i8. |
| 3337 | if (MVT::getSizeInBits(VT) == 16) { |
| 3338 | // Transform it so it match pextrw which produces a 32-bit result. |
| 3339 | MVT::ValueType EVT = (MVT::ValueType)(VT+1); |
| 3340 | SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT, |
| 3341 | Op.getOperand(0), Op.getOperand(1)); |
| 3342 | SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract, |
| 3343 | DAG.getValueType(VT)); |
| 3344 | return DAG.getNode(ISD::TRUNCATE, VT, Assert); |
| 3345 | } else if (MVT::getSizeInBits(VT) == 32) { |
| 3346 | SDOperand Vec = Op.getOperand(0); |
| 3347 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3348 | if (Idx == 0) |
| 3349 | return Op; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3350 | // SHUFPS the element to the lowest double word, then movss. |
| 3351 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3352 | std::vector<SDOperand> IdxVec; |
| 3353 | IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT))); |
| 3354 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 3355 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
| 3356 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3357 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3358 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3359 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 3360 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3361 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3362 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3363 | } else if (MVT::getSizeInBits(VT) == 64) { |
| 3364 | SDOperand Vec = Op.getOperand(0); |
| 3365 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
| 3366 | if (Idx == 0) |
| 3367 | return Op; |
| 3368 | |
| 3369 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 3370 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 3371 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
| 3372 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 3373 | std::vector<SDOperand> IdxVec; |
| 3374 | IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT))); |
| 3375 | IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT))); |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3376 | SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3377 | &IdxVec[0], IdxVec.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3378 | Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(), |
| 3379 | Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask); |
| 3380 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3381 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | return SDOperand(); |
| 3385 | } |
| 3386 | |
| 3387 | SDOperand |
| 3388 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 3389 | // 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] | 3390 | // as its second argument. |
| 3391 | MVT::ValueType VT = Op.getValueType(); |
| 3392 | MVT::ValueType BaseVT = MVT::getVectorBaseType(VT); |
| 3393 | SDOperand N0 = Op.getOperand(0); |
| 3394 | SDOperand N1 = Op.getOperand(1); |
| 3395 | SDOperand N2 = Op.getOperand(2); |
| 3396 | if (MVT::getSizeInBits(BaseVT) == 16) { |
| 3397 | if (N1.getValueType() != MVT::i32) |
| 3398 | N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1); |
| 3399 | if (N2.getValueType() != MVT::i32) |
| 3400 | N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32); |
| 3401 | return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2); |
| 3402 | } else if (MVT::getSizeInBits(BaseVT) == 32) { |
| 3403 | unsigned Idx = cast<ConstantSDNode>(N2)->getValue(); |
| 3404 | if (Idx == 0) { |
| 3405 | // Use a movss. |
| 3406 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1); |
| 3407 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4); |
| 3408 | MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT); |
| 3409 | std::vector<SDOperand> MaskVec; |
| 3410 | MaskVec.push_back(DAG.getConstant(4, BaseVT)); |
| 3411 | for (unsigned i = 1; i <= 3; ++i) |
| 3412 | MaskVec.push_back(DAG.getConstant(i, BaseVT)); |
| 3413 | return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3414 | DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3415 | &MaskVec[0], MaskVec.size())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3416 | } else { |
| 3417 | // Use two pinsrw instructions to insert a 32 bit value. |
| 3418 | Idx <<= 1; |
| 3419 | if (MVT::isFloatingPoint(N1.getValueType())) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3420 | if (ISD::isNON_EXTLoad(N1.Val)) { |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 3421 | // Just load directly from f32mem to GR32. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3422 | LoadSDNode *LD = cast<LoadSDNode>(N1); |
| 3423 | N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(), |
| 3424 | LD->getSrcValue(), LD->getSrcValueOffset()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3425 | } else { |
| 3426 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1); |
| 3427 | N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1); |
| 3428 | N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3429 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0); |
| 3433 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3434 | DAG.getConstant(Idx, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3435 | N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8)); |
| 3436 | N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1, |
Evan Cheng | 015188f | 2006-06-15 08:14:54 +0000 | [diff] [blame] | 3437 | DAG.getConstant(Idx+1, getPointerTy())); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3438 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | return SDOperand(); |
| 3443 | } |
| 3444 | |
| 3445 | SDOperand |
| 3446 | X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) { |
| 3447 | SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0)); |
| 3448 | return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt); |
| 3449 | } |
| 3450 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3451 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3452 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 3453 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 3454 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 3455 | // be used to form addressing mode. These wrapped nodes will be selected |
| 3456 | // into MOV32ri. |
| 3457 | SDOperand |
| 3458 | X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) { |
| 3459 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3460 | SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(), |
| 3461 | getPointerTy(), |
| 3462 | CP->getAlignment()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3463 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3464 | // With PIC, the address is actually $g + Offset. |
| 3465 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3466 | !Subtarget->isPICStyleRIPRel()) { |
| 3467 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3468 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3469 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | return Result; |
| 3473 | } |
| 3474 | |
| 3475 | SDOperand |
| 3476 | X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) { |
| 3477 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3478 | SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3479 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3480 | // With PIC, the address is actually $g + Offset. |
| 3481 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3482 | !Subtarget->isPICStyleRIPRel()) { |
| 3483 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3484 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3485 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3486 | } |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 3487 | |
| 3488 | // For Darwin & Mingw32, external and weak symbols are indirect, so we want to |
| 3489 | // load the value at address GV, not the value of GV itself. This means that |
| 3490 | // the GlobalAddress must be in the base or index register of the address, not |
| 3491 | // the GV offset field. Platform check is inside GVRequiresExtraLoad() call |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3492 | // The same applies for external symbols during PIC codegen |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 3493 | if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)) |
| 3494 | Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3495 | |
| 3496 | return Result; |
| 3497 | } |
| 3498 | |
| 3499 | SDOperand |
| 3500 | X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) { |
| 3501 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame] | 3502 | SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy()); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 3503 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 3504 | // With PIC, the address is actually $g + Offset. |
| 3505 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3506 | !Subtarget->isPICStyleRIPRel()) { |
| 3507 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3508 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3509 | Result); |
| 3510 | } |
| 3511 | |
| 3512 | return Result; |
| 3513 | } |
| 3514 | |
| 3515 | SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) { |
| 3516 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 3517 | SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy()); |
| 3518 | Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result); |
| 3519 | // With PIC, the address is actually $g + Offset. |
| 3520 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 3521 | !Subtarget->isPICStyleRIPRel()) { |
| 3522 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 3523 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), |
| 3524 | Result); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | return Result; |
| 3528 | } |
| 3529 | |
| 3530 | SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3531 | assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && |
| 3532 | "Not an i64 shift!"); |
| 3533 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| 3534 | SDOperand ShOpLo = Op.getOperand(0); |
| 3535 | SDOperand ShOpHi = Op.getOperand(1); |
| 3536 | SDOperand ShAmt = Op.getOperand(2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3537 | SDOperand Tmp1 = isSRA ? |
| 3538 | DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) : |
| 3539 | DAG.getConstant(0, MVT::i32); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3540 | |
| 3541 | SDOperand Tmp2, Tmp3; |
| 3542 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3543 | Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); |
| 3544 | Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); |
| 3545 | } else { |
| 3546 | Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); |
Evan Cheng | b7b5706 | 2006-01-19 01:46:14 +0000 | [diff] [blame] | 3547 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3550 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3551 | SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt, |
| 3552 | DAG.getConstant(32, MVT::i8)); |
| 3553 | SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)}; |
| 3554 | SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3555 | |
| 3556 | SDOperand Hi, Lo; |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3557 | SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3558 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3559 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag); |
| 3560 | SmallVector<SDOperand, 4> Ops; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3561 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 3562 | Ops.push_back(Tmp2); |
| 3563 | Ops.push_back(Tmp3); |
| 3564 | Ops.push_back(CC); |
| 3565 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3566 | Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3567 | InFlag = Hi.getValue(1); |
| 3568 | |
| 3569 | Ops.clear(); |
| 3570 | Ops.push_back(Tmp3); |
| 3571 | Ops.push_back(Tmp1); |
| 3572 | Ops.push_back(CC); |
| 3573 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3574 | Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3575 | } else { |
| 3576 | Ops.push_back(Tmp2); |
| 3577 | Ops.push_back(Tmp3); |
| 3578 | Ops.push_back(CC); |
Evan Cheng | 910cd3c | 2006-01-09 22:29:54 +0000 | [diff] [blame] | 3579 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3580 | Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3581 | InFlag = Lo.getValue(1); |
| 3582 | |
| 3583 | Ops.clear(); |
| 3584 | Ops.push_back(Tmp3); |
| 3585 | Ops.push_back(Tmp1); |
| 3586 | Ops.push_back(CC); |
| 3587 | Ops.push_back(InFlag); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3588 | Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3591 | VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 3592 | Ops.clear(); |
| 3593 | Ops.push_back(Lo); |
| 3594 | Ops.push_back(Hi); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3595 | return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3596 | } |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3597 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3598 | SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) { |
| 3599 | assert(Op.getOperand(0).getValueType() <= MVT::i64 && |
| 3600 | Op.getOperand(0).getValueType() >= MVT::i16 && |
| 3601 | "Unknown SINT_TO_FP to lower!"); |
| 3602 | |
| 3603 | SDOperand Result; |
| 3604 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3605 | unsigned Size = MVT::getSizeInBits(SrcVT)/8; |
| 3606 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3607 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 3608 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3609 | SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3610 | StackSlot, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3611 | |
| 3612 | // Build the FILD |
| 3613 | std::vector<MVT::ValueType> Tys; |
| 3614 | Tys.push_back(MVT::f64); |
| 3615 | Tys.push_back(MVT::Other); |
| 3616 | if (X86ScalarSSE) Tys.push_back(MVT::Flag); |
| 3617 | std::vector<SDOperand> Ops; |
| 3618 | Ops.push_back(Chain); |
| 3619 | Ops.push_back(StackSlot); |
| 3620 | Ops.push_back(DAG.getValueType(SrcVT)); |
| 3621 | Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3622 | Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3623 | |
| 3624 | if (X86ScalarSSE) { |
| 3625 | Chain = Result.getValue(1); |
| 3626 | SDOperand InFlag = Result.getValue(2); |
| 3627 | |
| 3628 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 3629 | // shouldn't be necessary except that RFP cannot be live across |
| 3630 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3631 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3632 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3633 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3634 | std::vector<MVT::ValueType> Tys; |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 3635 | Tys.push_back(MVT::Other); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3636 | std::vector<SDOperand> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 3637 | Ops.push_back(Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3638 | Ops.push_back(Result); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3639 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3640 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
| 3641 | Ops.push_back(InFlag); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3642 | Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3643 | Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3644 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3645 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3646 | return Result; |
| 3647 | } |
| 3648 | |
| 3649 | SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) { |
| 3650 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
| 3651 | "Unknown FP_TO_SINT to lower!"); |
| 3652 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 3653 | // stack slot. |
| 3654 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3655 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3656 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3657 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3658 | |
| 3659 | unsigned Opc; |
| 3660 | switch (Op.getValueType()) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3661 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 3662 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 3663 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 3664 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3665 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3666 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3667 | SDOperand Chain = DAG.getEntryNode(); |
| 3668 | SDOperand Value = Op.getOperand(0); |
| 3669 | if (X86ScalarSSE) { |
| 3670 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3671 | Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3672 | std::vector<MVT::ValueType> Tys; |
| 3673 | Tys.push_back(MVT::f64); |
| 3674 | Tys.push_back(MVT::Other); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3675 | std::vector<SDOperand> Ops; |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 3676 | Ops.push_back(Chain); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3677 | Ops.push_back(StackSlot); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3678 | Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType())); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3679 | Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3680 | Chain = Value.getValue(1); |
| 3681 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 3682 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 3683 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3684 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3685 | // Build the FP_TO_INT*_IN_MEM |
| 3686 | std::vector<SDOperand> Ops; |
| 3687 | Ops.push_back(Chain); |
| 3688 | Ops.push_back(Value); |
| 3689 | Ops.push_back(StackSlot); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 3690 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size()); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3691 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3692 | // Load the result. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3693 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) { |
| 3697 | MVT::ValueType VT = Op.getValueType(); |
| 3698 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 3699 | std::vector<Constant*> CV; |
| 3700 | if (VT == MVT::f64) { |
| 3701 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)))); |
| 3702 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3703 | } else { |
| 3704 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)))); |
| 3705 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3706 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3707 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3708 | } |
| 3709 | Constant *CS = ConstantStruct::get(CV); |
| 3710 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 3711 | std::vector<MVT::ValueType> Tys; |
| 3712 | Tys.push_back(VT); |
| 3713 | Tys.push_back(MVT::Other); |
| 3714 | SmallVector<SDOperand, 3> Ops; |
| 3715 | Ops.push_back(DAG.getEntryNode()); |
| 3716 | Ops.push_back(CPIdx); |
| 3717 | Ops.push_back(DAG.getSrcValue(NULL)); |
| 3718 | SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3719 | return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); |
| 3720 | } |
| 3721 | |
| 3722 | SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) { |
| 3723 | MVT::ValueType VT = Op.getValueType(); |
| 3724 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 3725 | std::vector<Constant*> CV; |
| 3726 | if (VT == MVT::f64) { |
| 3727 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63))); |
| 3728 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3729 | } else { |
| 3730 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31))); |
| 3731 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3732 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3733 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 3734 | } |
| 3735 | Constant *CS = ConstantStruct::get(CV); |
| 3736 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 3737 | std::vector<MVT::ValueType> Tys; |
| 3738 | Tys.push_back(VT); |
| 3739 | Tys.push_back(MVT::Other); |
| 3740 | SmallVector<SDOperand, 3> Ops; |
| 3741 | Ops.push_back(DAG.getEntryNode()); |
| 3742 | Ops.push_back(CPIdx); |
| 3743 | Ops.push_back(DAG.getSrcValue(NULL)); |
| 3744 | SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3745 | return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); |
| 3746 | } |
| 3747 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3748 | SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3749 | SDOperand Op0 = Op.getOperand(0); |
| 3750 | SDOperand Op1 = Op.getOperand(1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3751 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3752 | MVT::ValueType SrcVT = Op1.getValueType(); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3753 | const Type *SrcTy = MVT::getTypeForValueType(SrcVT); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3754 | |
| 3755 | // If second operand is smaller, extend it first. |
| 3756 | if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) { |
| 3757 | Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1); |
| 3758 | SrcVT = VT; |
| 3759 | } |
| 3760 | |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3761 | // First get the sign bit of second operand. |
| 3762 | std::vector<Constant*> CV; |
| 3763 | if (SrcVT == MVT::f64) { |
| 3764 | CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63))); |
| 3765 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3766 | } else { |
| 3767 | CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31))); |
| 3768 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3769 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3770 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3771 | } |
| 3772 | Constant *CS = ConstantStruct::get(CV); |
| 3773 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 3774 | std::vector<MVT::ValueType> Tys; |
Evan Cheng | 1722eee | 2007-01-05 08:32:24 +0000 | [diff] [blame] | 3775 | Tys.push_back(SrcVT); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3776 | Tys.push_back(MVT::Other); |
| 3777 | SmallVector<SDOperand, 3> Ops; |
| 3778 | Ops.push_back(DAG.getEntryNode()); |
| 3779 | Ops.push_back(CPIdx); |
| 3780 | Ops.push_back(DAG.getSrcValue(NULL)); |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3781 | SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); |
| 3782 | SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3783 | |
| 3784 | // Shift sign bit right or left if the two operands have different types. |
| 3785 | if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) { |
| 3786 | // Op0 is MVT::f32, Op1 is MVT::f64. |
| 3787 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit); |
| 3788 | SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit, |
| 3789 | DAG.getConstant(32, MVT::i32)); |
| 3790 | SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit); |
| 3791 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit, |
| 3792 | DAG.getConstant(0, getPointerTy())); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 3795 | // Clear first operand sign bit. |
| 3796 | CV.clear(); |
| 3797 | if (VT == MVT::f64) { |
| 3798 | CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63)))); |
| 3799 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3800 | } else { |
| 3801 | CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31)))); |
| 3802 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3803 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3804 | CV.push_back(ConstantFP::get(SrcTy, 0.0)); |
| 3805 | } |
| 3806 | CS = ConstantStruct::get(CV); |
| 3807 | CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 3808 | Tys.clear(); |
| 3809 | Tys.push_back(VT); |
| 3810 | Tys.push_back(MVT::Other); |
| 3811 | Ops.clear(); |
| 3812 | Ops.push_back(DAG.getEntryNode()); |
| 3813 | Ops.push_back(CPIdx); |
| 3814 | Ops.push_back(DAG.getSrcValue(NULL)); |
| 3815 | SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size()); |
| 3816 | SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2); |
| 3817 | |
| 3818 | // Or the value with the sign bit. |
| 3819 | return DAG.getNode(X86ISD::FOR, VT, Val, SignBit); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3822 | SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG, |
| 3823 | SDOperand Chain) { |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3824 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
| 3825 | SDOperand Cond; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3826 | SDOperand Op0 = Op.getOperand(0); |
| 3827 | SDOperand Op1 = Op.getOperand(1); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3828 | SDOperand CC = Op.getOperand(2); |
| 3829 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3830 | const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3831 | const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3832 | bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3833 | unsigned X86CC; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3834 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3835 | if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 3836 | Op0, Op1, DAG)) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3837 | SDOperand Ops1[] = { Chain, Op0, Op1 }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3838 | Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3839 | SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3840 | return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | assert(isFP && "Illegal integer SetCC!"); |
| 3844 | |
| 3845 | SDOperand COps[] = { Chain, Op0, Op1 }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3846 | Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3847 | |
| 3848 | switch (SetCCOpcode) { |
| 3849 | default: assert(false && "Illegal floating point SetCC!"); |
| 3850 | case ISD::SETOEQ: { // !PF & ZF |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3851 | SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3852 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3853 | SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3854 | Tmp1.getValue(1) }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3855 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3856 | return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); |
| 3857 | } |
| 3858 | case ISD::SETUNE: { // PF | !ZF |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3859 | SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3860 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2); |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3861 | SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3862 | Tmp1.getValue(1) }; |
Evan Cheng | cf12ec4 | 2006-10-12 19:12:56 +0000 | [diff] [blame] | 3863 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3864 | return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); |
| 3865 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 3866 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3867 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3868 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3869 | SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3870 | bool addTest = true; |
| 3871 | SDOperand Chain = DAG.getEntryNode(); |
| 3872 | SDOperand Cond = Op.getOperand(0); |
| 3873 | SDOperand CC; |
| 3874 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3875 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3876 | if (Cond.getOpcode() == ISD::SETCC) |
| 3877 | Cond = LowerSETCC(Cond, DAG, Chain); |
| 3878 | |
| 3879 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 3880 | CC = Cond.getOperand(0); |
| 3881 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3882 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3883 | // (since flag operand cannot be shared). Use it as the condition setting |
| 3884 | // operand in place of the X86ISD::SETCC. |
| 3885 | // If the X86ISD::SETCC has more than one use, then perhaps it's better |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3886 | // to use a test instead of duplicating the X86ISD::CMP (for register |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3887 | // pressure reason)? |
| 3888 | SDOperand Cmp = Cond.getOperand(1); |
| 3889 | unsigned Opc = Cmp.getOpcode(); |
| 3890 | bool IllegalFPCMov = !X86ScalarSSE && |
| 3891 | MVT::isFloatingPoint(Op.getValueType()) && |
| 3892 | !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); |
| 3893 | if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) && |
| 3894 | !IllegalFPCMov) { |
| 3895 | SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; |
| 3896 | Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); |
| 3897 | addTest = false; |
| 3898 | } |
| 3899 | } |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3900 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3901 | if (addTest) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3902 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3903 | SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; |
| 3904 | Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 3905 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 3906 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3907 | VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag); |
| 3908 | SmallVector<SDOperand, 4> Ops; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3909 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 3910 | // condition is true. |
| 3911 | Ops.push_back(Op.getOperand(2)); |
| 3912 | Ops.push_back(Op.getOperand(1)); |
| 3913 | Ops.push_back(CC); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3914 | Ops.push_back(Cond.getValue(1)); |
| 3915 | return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3916 | } |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 3917 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3918 | SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3919 | bool addTest = true; |
| 3920 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3921 | SDOperand Cond = Op.getOperand(1); |
| 3922 | SDOperand Dest = Op.getOperand(2); |
| 3923 | SDOperand CC; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3924 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
| 3925 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3926 | if (Cond.getOpcode() == ISD::SETCC) |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3927 | Cond = LowerSETCC(Cond, DAG, Chain); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3928 | |
| 3929 | if (Cond.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3930 | CC = Cond.getOperand(0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3931 | |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3932 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 3933 | // (since flag operand cannot be shared). Use it as the condition setting |
| 3934 | // operand in place of the X86ISD::SETCC. |
| 3935 | // If the X86ISD::SETCC has more than one use, then perhaps it's better |
| 3936 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 3937 | // pressure reason)? |
| 3938 | SDOperand Cmp = Cond.getOperand(1); |
| 3939 | unsigned Opc = Cmp.getOpcode(); |
| 3940 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) { |
| 3941 | SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) }; |
| 3942 | Cond = DAG.getNode(Opc, VTs, 2, Ops, 3); |
| 3943 | addTest = false; |
| 3944 | } |
| 3945 | } |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 3946 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3947 | if (addTest) { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3948 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3949 | SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) }; |
| 3950 | Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3); |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 3951 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3952 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 3953 | Cond, Op.getOperand(2), CC, Cond.getValue(1)); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3954 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 3955 | |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3956 | SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) { |
| 3957 | unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3958 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3959 | if (Subtarget->is64Bit()) |
| 3960 | return LowerX86_64CCCCallTo(Op, DAG); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3961 | else |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3962 | switch (CallingConv) { |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3963 | default: |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3964 | assert(0 && "Unsupported calling convention"); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3965 | case CallingConv::Fast: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3966 | if (EnableFastCC) { |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3967 | return LowerFastCCCallTo(Op, DAG); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3968 | } |
| 3969 | // Falls through |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3970 | case CallingConv::C: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3971 | return LowerCCCCallTo(Op, DAG); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3972 | case CallingConv::X86_StdCall: |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3973 | return LowerCCCCallTo(Op, DAG, true); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 3974 | case CallingConv::X86_FastCall: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3975 | return LowerFastCCCallTo(Op, DAG, true); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 3976 | } |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3979 | SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) { |
| 3980 | SDOperand Copy; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3981 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3982 | switch(Op.getNumOperands()) { |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3983 | default: |
| 3984 | assert(0 && "Do not know how to return this many arguments!"); |
| 3985 | abort(); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3986 | case 1: // ret void. |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3987 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0), |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 3988 | DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); |
Evan Cheng | 6848be1 | 2006-05-26 23:10:12 +0000 | [diff] [blame] | 3989 | case 3: { |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 3990 | MVT::ValueType ArgVT = Op.getOperand(1).getValueType(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3991 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3992 | if (MVT::isVector(ArgVT) || |
| 3993 | (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) { |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 3994 | // Integer or FP vector result -> XMM0. |
| 3995 | if (DAG.getMachineFunction().liveout_empty()) |
| 3996 | DAG.getMachineFunction().addLiveOut(X86::XMM0); |
| 3997 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1), |
| 3998 | SDOperand()); |
| 3999 | } else if (MVT::isInteger(ArgVT)) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4000 | // Integer result -> EAX / RAX. |
| 4001 | // The C calling convention guarantees the return value has been |
| 4002 | // promoted to at least MVT::i32. The X86-64 ABI doesn't require the |
| 4003 | // value to be promoted MVT::i64. So we don't have to extend it to |
| 4004 | // 64-bit. Return the value in EAX, but mark RAX as liveout. |
| 4005 | unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX; |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4006 | if (DAG.getMachineFunction().liveout_empty()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4007 | DAG.getMachineFunction().addLiveOut(Reg); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4008 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4009 | Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX; |
| 4010 | Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1), |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4011 | SDOperand()); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4012 | } else if (!X86ScalarSSE) { |
| 4013 | // FP return with fp-stack value. |
| 4014 | if (DAG.getMachineFunction().liveout_empty()) |
| 4015 | DAG.getMachineFunction().addLiveOut(X86::ST0); |
| 4016 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4017 | std::vector<MVT::ValueType> Tys; |
| 4018 | Tys.push_back(MVT::Other); |
| 4019 | Tys.push_back(MVT::Flag); |
| 4020 | std::vector<SDOperand> Ops; |
| 4021 | Ops.push_back(Op.getOperand(0)); |
| 4022 | Ops.push_back(Op.getOperand(1)); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4023 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size()); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4024 | } else { |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4025 | // FP return with ScalarSSE (return on fp-stack). |
| 4026 | if (DAG.getMachineFunction().liveout_empty()) |
| 4027 | DAG.getMachineFunction().addLiveOut(X86::ST0); |
| 4028 | |
Evan Cheng | 0d084c9 | 2006-02-01 00:20:21 +0000 | [diff] [blame] | 4029 | SDOperand MemLoc; |
| 4030 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 4031 | SDOperand Value = Op.getOperand(1); |
| 4032 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4033 | if (ISD::isNON_EXTLoad(Value.Val) && |
Evan Cheng | 760df29 | 2006-02-01 01:19:32 +0000 | [diff] [blame] | 4034 | (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) { |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 4035 | Chain = Value.getOperand(0); |
| 4036 | MemLoc = Value.getOperand(1); |
| 4037 | } else { |
| 4038 | // Spill the value to memory and reload it into top of stack. |
| 4039 | unsigned Size = MVT::getSizeInBits(ArgVT)/8; |
| 4040 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4041 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 4042 | MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4043 | Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 4044 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4045 | std::vector<MVT::ValueType> Tys; |
| 4046 | Tys.push_back(MVT::f64); |
| 4047 | Tys.push_back(MVT::Other); |
| 4048 | std::vector<SDOperand> Ops; |
| 4049 | Ops.push_back(Chain); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 4050 | Ops.push_back(MemLoc); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4051 | Ops.push_back(DAG.getValueType(ArgVT)); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4052 | Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size()); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4053 | Tys.clear(); |
| 4054 | Tys.push_back(MVT::Other); |
| 4055 | Tys.push_back(MVT::Flag); |
| 4056 | Ops.clear(); |
| 4057 | Ops.push_back(Copy.getValue(1)); |
| 4058 | Ops.push_back(Copy); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4059 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size()); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4060 | } |
| 4061 | break; |
| 4062 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4063 | case 5: { |
| 4064 | unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX; |
| 4065 | unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX; |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4066 | if (DAG.getMachineFunction().liveout_empty()) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4067 | DAG.getMachineFunction().addLiveOut(Reg1); |
| 4068 | DAG.getMachineFunction().addLiveOut(Reg2); |
Chris Lattner | b2be403 | 2006-04-17 20:32:50 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4071 | Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3), |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4072 | SDOperand()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4073 | Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1)); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4074 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4075 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 4076 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4077 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4078 | Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16), |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4079 | Copy.getValue(1)); |
| 4080 | } |
| 4081 | |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 4082 | SDOperand |
| 4083 | X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 4084 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4085 | const Function* Fn = MF.getFunction(); |
| 4086 | if (Fn->hasExternalLinkage() && |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 4087 | Subtarget->isTargetCygMing() && |
Evan Cheng | b12223e | 2006-06-09 06:24:42 +0000 | [diff] [blame] | 4088 | Fn->getName() == "main") |
Evan Cheng | e8bd0a3 | 2006-06-06 23:30:24 +0000 | [diff] [blame] | 4089 | MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true); |
| 4090 | |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 4091 | unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4092 | if (Subtarget->is64Bit()) |
| 4093 | return LowerX86_64CCCArguments(Op, DAG); |
Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 4094 | else |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4095 | switch(CC) { |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 4096 | default: |
| 4097 | assert(0 && "Unsupported calling convention"); |
| 4098 | case CallingConv::Fast: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4099 | if (EnableFastCC) { |
| 4100 | return LowerFastCCArguments(Op, DAG); |
| 4101 | } |
| 4102 | // Falls through |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 4103 | case CallingConv::C: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4104 | return LowerCCCArguments(Op, DAG); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 4105 | case CallingConv::X86_StdCall: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4106 | MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 4107 | return LowerCCCArguments(Op, DAG, true); |
Chris Lattner | f38f543 | 2006-09-27 18:29:38 +0000 | [diff] [blame] | 4108 | case CallingConv::X86_FastCall: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4109 | MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 4110 | return LowerFastCCArguments(Op, DAG, true); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 4111 | } |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4114 | SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) { |
| 4115 | SDOperand InFlag(0, 0); |
| 4116 | SDOperand Chain = Op.getOperand(0); |
| 4117 | unsigned Align = |
| 4118 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 4119 | if (Align == 0) Align = 1; |
| 4120 | |
| 4121 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 4122 | // If not DWORD aligned, call memset if size is less than the threshold. |
| 4123 | // It knows how to align to the right boundary first. |
| 4124 | if ((Align & 3) != 0 || |
| 4125 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
| 4126 | MVT::ValueType IntPtr = getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 4127 | const Type *IntPtrTy = getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4128 | TargetLowering::ArgListTy Args; |
| 4129 | TargetLowering::ArgListEntry Entry; |
| 4130 | Entry.Node = Op.getOperand(1); |
| 4131 | Entry.Ty = IntPtrTy; |
| 4132 | Entry.isSigned = false; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 4133 | Entry.isInReg = false; |
| 4134 | Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4135 | Args.push_back(Entry); |
Reid Spencer | aff9387 | 2007-01-03 17:24:59 +0000 | [diff] [blame] | 4136 | // Extend the unsigned i8 argument to be an int value for the call. |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4137 | Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); |
| 4138 | Entry.Ty = IntPtrTy; |
| 4139 | Entry.isSigned = false; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 4140 | Entry.isInReg = false; |
| 4141 | Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4142 | Args.push_back(Entry); |
| 4143 | Entry.Node = Op.getOperand(3); |
| 4144 | Args.push_back(Entry); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4145 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4146 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4147 | DAG.getExternalSymbol("memset", IntPtr), Args, DAG); |
| 4148 | return CallResult.second; |
Evan Cheng | 48090aa | 2006-03-21 23:01:21 +0000 | [diff] [blame] | 4149 | } |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 4150 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4151 | MVT::ValueType AVT; |
| 4152 | SDOperand Count; |
| 4153 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 4154 | unsigned BytesLeft = 0; |
| 4155 | bool TwoRepStos = false; |
| 4156 | if (ValC) { |
| 4157 | unsigned ValReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4158 | uint64_t Val = ValC->getValue() & 255; |
Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4159 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4160 | // If the value is a constant, then we can potentially use larger sets. |
| 4161 | switch (Align & 3) { |
| 4162 | case 2: // WORD aligned |
| 4163 | AVT = MVT::i16; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4164 | ValReg = X86::AX; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4165 | Val = (Val << 8) | Val; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4166 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4167 | case 0: // DWORD aligned |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4168 | AVT = MVT::i32; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4169 | ValReg = X86::EAX; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4170 | Val = (Val << 8) | Val; |
| 4171 | Val = (Val << 16) | Val; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4172 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned |
| 4173 | AVT = MVT::i64; |
| 4174 | ValReg = X86::RAX; |
| 4175 | Val = (Val << 32) | Val; |
| 4176 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4177 | break; |
| 4178 | default: // Byte aligned |
| 4179 | AVT = MVT::i8; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4180 | ValReg = X86::AL; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4181 | Count = Op.getOperand(3); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4182 | break; |
Evan Cheng | 80d428c | 2006-04-19 22:48:17 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4185 | if (AVT > MVT::i8) { |
| 4186 | if (I) { |
| 4187 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
| 4188 | Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); |
| 4189 | BytesLeft = I->getValue() % UBytes; |
| 4190 | } else { |
| 4191 | assert(AVT >= MVT::i32 && |
| 4192 | "Do not use rep;stos if not at least DWORD aligned"); |
| 4193 | Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), |
| 4194 | Op.getOperand(3), DAG.getConstant(2, MVT::i8)); |
| 4195 | TwoRepStos = true; |
| 4196 | } |
| 4197 | } |
| 4198 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4199 | Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), |
| 4200 | InFlag); |
| 4201 | InFlag = Chain.getValue(1); |
| 4202 | } else { |
| 4203 | AVT = MVT::i8; |
| 4204 | Count = Op.getOperand(3); |
| 4205 | Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); |
| 4206 | InFlag = Chain.getValue(1); |
Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 4207 | } |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4208 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4209 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 4210 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4211 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4212 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
| 4213 | Op.getOperand(1), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4214 | InFlag = Chain.getValue(1); |
Evan Cheng | a0b3afb | 2006-03-27 07:00:16 +0000 | [diff] [blame] | 4215 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4216 | std::vector<MVT::ValueType> Tys; |
| 4217 | Tys.push_back(MVT::Other); |
| 4218 | Tys.push_back(MVT::Flag); |
| 4219 | std::vector<SDOperand> Ops; |
| 4220 | Ops.push_back(Chain); |
| 4221 | Ops.push_back(DAG.getValueType(AVT)); |
| 4222 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4223 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4224 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4225 | if (TwoRepStos) { |
| 4226 | InFlag = Chain.getValue(1); |
| 4227 | Count = Op.getOperand(3); |
| 4228 | MVT::ValueType CVT = Count.getValueType(); |
| 4229 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4230 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 4231 | Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, |
| 4232 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4233 | InFlag = Chain.getValue(1); |
| 4234 | Tys.clear(); |
| 4235 | Tys.push_back(MVT::Other); |
| 4236 | Tys.push_back(MVT::Flag); |
| 4237 | Ops.clear(); |
| 4238 | Ops.push_back(Chain); |
| 4239 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 4240 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4241 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4242 | } else if (BytesLeft) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4243 | // Issue stores for the last 1 - 7 bytes. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4244 | SDOperand Value; |
| 4245 | unsigned Val = ValC->getValue() & 255; |
| 4246 | unsigned Offset = I->getValue() - BytesLeft; |
| 4247 | SDOperand DstAddr = Op.getOperand(1); |
| 4248 | MVT::ValueType AddrVT = DstAddr.getValueType(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4249 | if (BytesLeft >= 4) { |
| 4250 | Val = (Val << 8) | Val; |
| 4251 | Val = (Val << 16) | Val; |
| 4252 | Value = DAG.getConstant(Val, MVT::i32); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4253 | Chain = DAG.getStore(Chain, Value, |
| 4254 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4255 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4256 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4257 | BytesLeft -= 4; |
| 4258 | Offset += 4; |
| 4259 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4260 | if (BytesLeft >= 2) { |
| 4261 | Value = DAG.getConstant((Val << 8) | Val, MVT::i16); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4262 | Chain = DAG.getStore(Chain, Value, |
| 4263 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4264 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4265 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4266 | BytesLeft -= 2; |
| 4267 | Offset += 2; |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 4268 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4269 | if (BytesLeft == 1) { |
| 4270 | Value = DAG.getConstant(Val, MVT::i8); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4271 | Chain = DAG.getStore(Chain, Value, |
| 4272 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 4273 | DAG.getConstant(Offset, AddrVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4274 | NULL, 0); |
Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 4275 | } |
Evan Cheng | 386031a | 2006-03-24 07:29:27 +0000 | [diff] [blame] | 4276 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 4277 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4278 | return Chain; |
| 4279 | } |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 4280 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4281 | SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) { |
| 4282 | SDOperand Chain = Op.getOperand(0); |
| 4283 | unsigned Align = |
| 4284 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 4285 | if (Align == 0) Align = 1; |
Evan Cheng | 11e15b3 | 2006-04-03 20:53:28 +0000 | [diff] [blame] | 4286 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4287 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 4288 | // If not DWORD aligned, call memcpy if size is less than the threshold. |
| 4289 | // It knows how to align to the right boundary first. |
| 4290 | if ((Align & 3) != 0 || |
| 4291 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
| 4292 | MVT::ValueType IntPtr = getPointerTy(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4293 | TargetLowering::ArgListTy Args; |
| 4294 | TargetLowering::ArgListEntry Entry; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 4295 | Entry.Ty = getTargetData()->getIntPtrType(); |
| 4296 | Entry.isSigned = false; |
| 4297 | Entry.isInReg = false; |
| 4298 | Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4299 | Entry.Node = Op.getOperand(1); Args.push_back(Entry); |
| 4300 | Entry.Node = Op.getOperand(2); Args.push_back(Entry); |
| 4301 | Entry.Node = Op.getOperand(3); Args.push_back(Entry); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4302 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4303 | LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false, |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4304 | DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); |
| 4305 | return CallResult.second; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4306 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4307 | |
| 4308 | MVT::ValueType AVT; |
| 4309 | SDOperand Count; |
| 4310 | unsigned BytesLeft = 0; |
| 4311 | bool TwoRepMovs = false; |
| 4312 | switch (Align & 3) { |
| 4313 | case 2: // WORD aligned |
| 4314 | AVT = MVT::i16; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4315 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4316 | case 0: // DWORD aligned |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4317 | AVT = MVT::i32; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4318 | if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned |
| 4319 | AVT = MVT::i64; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4320 | break; |
| 4321 | default: // Byte aligned |
| 4322 | AVT = MVT::i8; |
| 4323 | Count = Op.getOperand(3); |
| 4324 | break; |
| 4325 | } |
| 4326 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4327 | if (AVT > MVT::i8) { |
| 4328 | if (I) { |
| 4329 | unsigned UBytes = MVT::getSizeInBits(AVT) / 8; |
| 4330 | Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy()); |
| 4331 | BytesLeft = I->getValue() % UBytes; |
| 4332 | } else { |
| 4333 | assert(AVT >= MVT::i32 && |
| 4334 | "Do not use rep;movs if not at least DWORD aligned"); |
| 4335 | Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(), |
| 4336 | Op.getOperand(3), DAG.getConstant(2, MVT::i8)); |
| 4337 | TwoRepMovs = true; |
| 4338 | } |
| 4339 | } |
| 4340 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4341 | SDOperand InFlag(0, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4342 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX, |
| 4343 | Count, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4344 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4345 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI, |
| 4346 | Op.getOperand(1), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4347 | InFlag = Chain.getValue(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4348 | Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI, |
| 4349 | Op.getOperand(2), InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4350 | InFlag = Chain.getValue(1); |
| 4351 | |
| 4352 | std::vector<MVT::ValueType> Tys; |
| 4353 | Tys.push_back(MVT::Other); |
| 4354 | Tys.push_back(MVT::Flag); |
| 4355 | std::vector<SDOperand> Ops; |
| 4356 | Ops.push_back(Chain); |
| 4357 | Ops.push_back(DAG.getValueType(AVT)); |
| 4358 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4359 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4360 | |
| 4361 | if (TwoRepMovs) { |
| 4362 | InFlag = Chain.getValue(1); |
| 4363 | Count = Op.getOperand(3); |
| 4364 | MVT::ValueType CVT = Count.getValueType(); |
| 4365 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4366 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 4367 | Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX, |
| 4368 | Left, InFlag); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4369 | InFlag = Chain.getValue(1); |
| 4370 | Tys.clear(); |
| 4371 | Tys.push_back(MVT::Other); |
| 4372 | Tys.push_back(MVT::Flag); |
| 4373 | Ops.clear(); |
| 4374 | Ops.push_back(Chain); |
| 4375 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 4376 | Ops.push_back(InFlag); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4377 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4378 | } else if (BytesLeft) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4379 | // Issue loads and stores for the last 1 - 7 bytes. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4380 | unsigned Offset = I->getValue() - BytesLeft; |
| 4381 | SDOperand DstAddr = Op.getOperand(1); |
| 4382 | MVT::ValueType DstVT = DstAddr.getValueType(); |
| 4383 | SDOperand SrcAddr = Op.getOperand(2); |
| 4384 | MVT::ValueType SrcVT = SrcAddr.getValueType(); |
| 4385 | SDOperand Value; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4386 | if (BytesLeft >= 4) { |
| 4387 | Value = DAG.getLoad(MVT::i32, Chain, |
| 4388 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4389 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4390 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4391 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4392 | Chain = DAG.getStore(Chain, Value, |
| 4393 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4394 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4395 | NULL, 0); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4396 | BytesLeft -= 4; |
| 4397 | Offset += 4; |
| 4398 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4399 | if (BytesLeft >= 2) { |
| 4400 | Value = DAG.getLoad(MVT::i16, Chain, |
| 4401 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4402 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4403 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4404 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4405 | Chain = DAG.getStore(Chain, Value, |
| 4406 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4407 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4408 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4409 | BytesLeft -= 2; |
| 4410 | Offset += 2; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4413 | if (BytesLeft == 1) { |
| 4414 | Value = DAG.getLoad(MVT::i8, Chain, |
| 4415 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 4416 | DAG.getConstant(Offset, SrcVT)), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4417 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4418 | Chain = Value.getValue(1); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4419 | Chain = DAG.getStore(Chain, Value, |
| 4420 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 4421 | DAG.getConstant(Offset, DstVT)), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4422 | NULL, 0); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4423 | } |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4424 | } |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4425 | |
| 4426 | return Chain; |
| 4427 | } |
| 4428 | |
| 4429 | SDOperand |
| 4430 | X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) { |
| 4431 | std::vector<MVT::ValueType> Tys; |
| 4432 | Tys.push_back(MVT::Other); |
| 4433 | Tys.push_back(MVT::Flag); |
| 4434 | std::vector<SDOperand> Ops; |
| 4435 | Ops.push_back(Op.getOperand(0)); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4436 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4437 | Ops.clear(); |
Evan Cheng | 3fa9dff | 2006-11-29 08:28:13 +0000 | [diff] [blame] | 4438 | if (Subtarget->is64Bit()) { |
| 4439 | SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1)); |
| 4440 | SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX, |
| 4441 | MVT::i64, Copy1.getValue(2)); |
| 4442 | SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2, |
| 4443 | DAG.getConstant(32, MVT::i8)); |
| 4444 | Ops.push_back(DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp)); |
| 4445 | Ops.push_back(Copy2.getValue(1)); |
| 4446 | Tys[0] = MVT::i64; |
| 4447 | Tys[1] = MVT::Other; |
| 4448 | } else { |
| 4449 | SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)); |
| 4450 | SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX, |
| 4451 | MVT::i32, Copy1.getValue(2)); |
| 4452 | Ops.push_back(Copy1); |
| 4453 | Ops.push_back(Copy2); |
| 4454 | Ops.push_back(Copy2.getValue(1)); |
| 4455 | Tys[0] = Tys[1] = MVT::i32; |
| 4456 | Tys.push_back(MVT::Other); |
| 4457 | } |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 4458 | return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4462 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2)); |
| 4463 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4464 | if (!Subtarget->is64Bit()) { |
| 4465 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 4466 | // memory location argument. |
| 4467 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4468 | return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(), |
| 4469 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | // __va_list_tag: |
| 4473 | // gp_offset (0 - 6 * 8) |
| 4474 | // fp_offset (48 - 48 + 8 * 16) |
| 4475 | // overflow_arg_area (point to parameters coming in memory). |
| 4476 | // reg_save_area |
| 4477 | std::vector<SDOperand> MemOps; |
| 4478 | SDOperand FIN = Op.getOperand(1); |
| 4479 | // Store gp_offset |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4480 | SDOperand Store = DAG.getStore(Op.getOperand(0), |
| 4481 | DAG.getConstant(VarArgsGPOffset, MVT::i32), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4482 | FIN, SV->getValue(), SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4483 | MemOps.push_back(Store); |
| 4484 | |
| 4485 | // Store fp_offset |
| 4486 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4487 | DAG.getConstant(4, getPointerTy())); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4488 | Store = DAG.getStore(Op.getOperand(0), |
| 4489 | DAG.getConstant(VarArgsFPOffset, MVT::i32), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4490 | FIN, SV->getValue(), SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4491 | MemOps.push_back(Store); |
| 4492 | |
| 4493 | // Store ptr to overflow_arg_area |
| 4494 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4495 | DAG.getConstant(4, getPointerTy())); |
| 4496 | SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4497 | Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(), |
| 4498 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4499 | MemOps.push_back(Store); |
| 4500 | |
| 4501 | // Store ptr to reg_save_area. |
| 4502 | FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, |
| 4503 | DAG.getConstant(8, getPointerTy())); |
| 4504 | SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4505 | Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(), |
| 4506 | SV->getOffset()); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 4507 | MemOps.push_back(Store); |
| 4508 | return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | SDOperand |
| 4512 | X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) { |
| 4513 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue(); |
| 4514 | switch (IntNo) { |
| 4515 | default: return SDOperand(); // Don't custom lower most intrinsics. |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4516 | // Comparison intrinsics. |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4517 | case Intrinsic::x86_sse_comieq_ss: |
| 4518 | case Intrinsic::x86_sse_comilt_ss: |
| 4519 | case Intrinsic::x86_sse_comile_ss: |
| 4520 | case Intrinsic::x86_sse_comigt_ss: |
| 4521 | case Intrinsic::x86_sse_comige_ss: |
| 4522 | case Intrinsic::x86_sse_comineq_ss: |
| 4523 | case Intrinsic::x86_sse_ucomieq_ss: |
| 4524 | case Intrinsic::x86_sse_ucomilt_ss: |
| 4525 | case Intrinsic::x86_sse_ucomile_ss: |
| 4526 | case Intrinsic::x86_sse_ucomigt_ss: |
| 4527 | case Intrinsic::x86_sse_ucomige_ss: |
| 4528 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4529 | case Intrinsic::x86_sse2_comieq_sd: |
| 4530 | case Intrinsic::x86_sse2_comilt_sd: |
| 4531 | case Intrinsic::x86_sse2_comile_sd: |
| 4532 | case Intrinsic::x86_sse2_comigt_sd: |
| 4533 | case Intrinsic::x86_sse2_comige_sd: |
| 4534 | case Intrinsic::x86_sse2_comineq_sd: |
| 4535 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 4536 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 4537 | case Intrinsic::x86_sse2_ucomile_sd: |
| 4538 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 4539 | case Intrinsic::x86_sse2_ucomige_sd: |
| 4540 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| 4541 | unsigned Opc = 0; |
| 4542 | ISD::CondCode CC = ISD::SETCC_INVALID; |
| 4543 | switch (IntNo) { |
| 4544 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4545 | case Intrinsic::x86_sse_comieq_ss: |
| 4546 | case Intrinsic::x86_sse2_comieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4547 | Opc = X86ISD::COMI; |
| 4548 | CC = ISD::SETEQ; |
| 4549 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4550 | case Intrinsic::x86_sse_comilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4551 | case Intrinsic::x86_sse2_comilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4552 | Opc = X86ISD::COMI; |
| 4553 | CC = ISD::SETLT; |
| 4554 | break; |
| 4555 | case Intrinsic::x86_sse_comile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4556 | case Intrinsic::x86_sse2_comile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4557 | Opc = X86ISD::COMI; |
| 4558 | CC = ISD::SETLE; |
| 4559 | break; |
| 4560 | case Intrinsic::x86_sse_comigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4561 | case Intrinsic::x86_sse2_comigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4562 | Opc = X86ISD::COMI; |
| 4563 | CC = ISD::SETGT; |
| 4564 | break; |
| 4565 | case Intrinsic::x86_sse_comige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4566 | case Intrinsic::x86_sse2_comige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4567 | Opc = X86ISD::COMI; |
| 4568 | CC = ISD::SETGE; |
| 4569 | break; |
| 4570 | case Intrinsic::x86_sse_comineq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4571 | case Intrinsic::x86_sse2_comineq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4572 | Opc = X86ISD::COMI; |
| 4573 | CC = ISD::SETNE; |
| 4574 | break; |
| 4575 | case Intrinsic::x86_sse_ucomieq_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4576 | case Intrinsic::x86_sse2_ucomieq_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4577 | Opc = X86ISD::UCOMI; |
| 4578 | CC = ISD::SETEQ; |
| 4579 | break; |
| 4580 | case Intrinsic::x86_sse_ucomilt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4581 | case Intrinsic::x86_sse2_ucomilt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4582 | Opc = X86ISD::UCOMI; |
| 4583 | CC = ISD::SETLT; |
| 4584 | break; |
| 4585 | case Intrinsic::x86_sse_ucomile_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4586 | case Intrinsic::x86_sse2_ucomile_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4587 | Opc = X86ISD::UCOMI; |
| 4588 | CC = ISD::SETLE; |
| 4589 | break; |
| 4590 | case Intrinsic::x86_sse_ucomigt_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4591 | case Intrinsic::x86_sse2_ucomigt_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4592 | Opc = X86ISD::UCOMI; |
| 4593 | CC = ISD::SETGT; |
| 4594 | break; |
| 4595 | case Intrinsic::x86_sse_ucomige_ss: |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4596 | case Intrinsic::x86_sse2_ucomige_sd: |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4597 | Opc = X86ISD::UCOMI; |
| 4598 | CC = ISD::SETGE; |
| 4599 | break; |
| 4600 | case Intrinsic::x86_sse_ucomineq_ss: |
| 4601 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 4602 | Opc = X86ISD::UCOMI; |
| 4603 | CC = ISD::SETNE; |
| 4604 | break; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4605 | } |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4606 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4607 | unsigned X86CC; |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 4608 | SDOperand LHS = Op.getOperand(1); |
| 4609 | SDOperand RHS = Op.getOperand(2); |
| 4610 | translateX86CC(CC, true, X86CC, LHS, RHS, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4611 | |
| 4612 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag); |
Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 4613 | SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS }; |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4614 | SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3); |
| 4615 | VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag); |
| 4616 | SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond }; |
| 4617 | SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4618 | return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC); |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4619 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 4620 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 4621 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4622 | |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 4623 | SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4624 | // Depths > 0 not supported yet! |
| 4625 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4626 | return SDOperand(); |
| 4627 | |
| 4628 | // Just load the return address |
| 4629 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4630 | return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0); |
| 4631 | } |
| 4632 | |
| 4633 | SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) { |
| 4634 | // Depths > 0 not supported yet! |
| 4635 | if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0) |
| 4636 | return SDOperand(); |
| 4637 | |
| 4638 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 4639 | return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI, |
| 4640 | DAG.getConstant(4, getPointerTy())); |
| 4641 | } |
| 4642 | |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4643 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 4644 | /// |
| 4645 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 4646 | switch (Op.getOpcode()) { |
| 4647 | default: assert(0 && "Should not custom lower this!"); |
| 4648 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 4649 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 4650 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 4651 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| 4652 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 4653 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 4654 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 4655 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
| 4656 | case ISD::SHL_PARTS: |
| 4657 | case ISD::SRA_PARTS: |
| 4658 | case ISD::SRL_PARTS: return LowerShift(Op, DAG); |
| 4659 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
| 4660 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| 4661 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 4662 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4663 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 4664 | case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode()); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4665 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 4666 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
| 4667 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 4668 | case ISD::CALL: return LowerCALL(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4669 | case ISD::RET: return LowerRET(Op, DAG); |
Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 4670 | case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4671 | case ISD::MEMSET: return LowerMEMSET(Op, DAG); |
| 4672 | case ISD::MEMCPY: return LowerMEMCPY(Op, DAG); |
| 4673 | case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG); |
| 4674 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
| 4675 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 4676 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 4677 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Jim Laskey | 62819f3 | 2007-02-21 22:54:50 +0000 | [diff] [blame^] | 4678 | // Exception address and exception selector. Currently unimplemented. |
| 4679 | case ISD::EXCEPTIONADDR: break; |
| 4680 | case ISD::EHSELECTION: break; |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4681 | } |
Jim Laskey | 62819f3 | 2007-02-21 22:54:50 +0000 | [diff] [blame^] | 4682 | return SDOperand(); |
Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4685 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 4686 | switch (Opcode) { |
| 4687 | default: return NULL; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 4688 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 4689 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 4690 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4691 | case X86ISD::FOR: return "X86ISD::FOR"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 4692 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 4693 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 4694 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 4695 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4696 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 4697 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 4698 | 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] | 4699 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 4700 | case X86ISD::FST: return "X86ISD::FST"; |
| 4701 | case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 4702 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4703 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 4704 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 4705 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 4706 | case X86ISD::CMP: return "X86ISD::CMP"; |
Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 4707 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 4708 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 4709 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4710 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 4711 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 4712 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 4713 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 4714 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 4715 | case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK"; |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4716 | case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 4717 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 4718 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Evan Cheng | bc4832b | 2006-03-24 23:15:12 +0000 | [diff] [blame] | 4719 | case X86ISD::S2VEC: return "X86ISD::S2VEC"; |
Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 4720 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 4721 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 4722 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 4723 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4726 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4727 | /// isLegalAddressImmediate - Return true if the integer value or |
| 4728 | /// GlobalValue can be used as the offset of the target addressing mode. |
| 4729 | bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const { |
| 4730 | // X86 allows a sign-extended 32-bit immediate field. |
| 4731 | return (V > -(1LL << 32) && V < (1LL << 32)-1); |
| 4732 | } |
| 4733 | |
| 4734 | bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const { |
Evan Cheng | c8306bd | 2006-11-29 23:48:14 +0000 | [diff] [blame] | 4735 | // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement |
| 4736 | // field unless we are in small code model. |
| 4737 | if (Subtarget->is64Bit() && |
| 4738 | getTargetMachine().getCodeModel() != CodeModel::Small) |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4739 | return false; |
Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 4740 | |
| 4741 | return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false)); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 4745 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 4746 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 4747 | /// are assumed to be legal. |
| 4748 | bool |
| 4749 | X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const { |
| 4750 | // Only do shuffles on 128-bit vector types for now. |
| 4751 | if (MVT::getSizeInBits(VT) == 64) return false; |
| 4752 | return (Mask.Val->getNumOperands() <= 4 || |
| 4753 | isSplatMask(Mask.Val) || |
| 4754 | isPSHUFHW_PSHUFLWMask(Mask.Val) || |
| 4755 | X86::isUNPCKLMask(Mask.Val) || |
| 4756 | X86::isUNPCKL_v_undef_Mask(Mask.Val) || |
| 4757 | X86::isUNPCKHMask(Mask.Val)); |
| 4758 | } |
| 4759 | |
| 4760 | bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps, |
| 4761 | MVT::ValueType EVT, |
| 4762 | SelectionDAG &DAG) const { |
| 4763 | unsigned NumElts = BVOps.size(); |
| 4764 | // Only do shuffles on 128-bit vector types for now. |
| 4765 | if (MVT::getSizeInBits(EVT) * NumElts == 64) return false; |
| 4766 | if (NumElts == 2) return true; |
| 4767 | if (NumElts == 4) { |
| 4768 | return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) || |
| 4769 | isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps)); |
| 4770 | } |
| 4771 | return false; |
| 4772 | } |
| 4773 | |
| 4774 | //===----------------------------------------------------------------------===// |
| 4775 | // X86 Scheduler Hooks |
| 4776 | //===----------------------------------------------------------------------===// |
| 4777 | |
| 4778 | MachineBasicBlock * |
| 4779 | X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 4780 | MachineBasicBlock *BB) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4781 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4782 | switch (MI->getOpcode()) { |
| 4783 | default: assert(false && "Unexpected instr type to insert"); |
| 4784 | case X86::CMOV_FR32: |
| 4785 | case X86::CMOV_FR64: |
| 4786 | case X86::CMOV_V4F32: |
| 4787 | case X86::CMOV_V2F64: |
| 4788 | case X86::CMOV_V2I64: { |
| 4789 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 4790 | // diamond control-flow pattern. The incoming instruction knows the |
| 4791 | // destination vreg to set, the condition code register to branch on, the |
| 4792 | // true/false values to select between, and a branch opcode to use. |
| 4793 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 4794 | ilist<MachineBasicBlock>::iterator It = BB; |
| 4795 | ++It; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4796 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4797 | // thisMBB: |
| 4798 | // ... |
| 4799 | // TrueVal = ... |
| 4800 | // cmpTY ccX, r1, r2 |
| 4801 | // bCC copy1MBB |
| 4802 | // fallthrough --> copy0MBB |
| 4803 | MachineBasicBlock *thisMBB = BB; |
| 4804 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 4805 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4806 | unsigned Opc = |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4807 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4808 | BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4809 | MachineFunction *F = BB->getParent(); |
| 4810 | F->getBasicBlockList().insert(It, copy0MBB); |
| 4811 | F->getBasicBlockList().insert(It, sinkMBB); |
| 4812 | // Update machine-CFG edges by first adding all successors of the current |
| 4813 | // block to the new block which will contain the Phi node for the select. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4814 | for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4815 | e = BB->succ_end(); i != e; ++i) |
| 4816 | sinkMBB->addSuccessor(*i); |
| 4817 | // Next, remove all successors of the current block, and add the true |
| 4818 | // and fallthrough blocks as its successors. |
| 4819 | while(!BB->succ_empty()) |
| 4820 | BB->removeSuccessor(BB->succ_begin()); |
| 4821 | BB->addSuccessor(copy0MBB); |
| 4822 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4823 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4824 | // copy0MBB: |
| 4825 | // %FalseValue = ... |
| 4826 | // # fallthrough to sinkMBB |
| 4827 | BB = copy0MBB; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4828 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4829 | // Update machine-CFG edges |
| 4830 | BB->addSuccessor(sinkMBB); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4831 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4832 | // sinkMBB: |
| 4833 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 4834 | // ... |
| 4835 | BB = sinkMBB; |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4836 | BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4837 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 4838 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 4839 | |
| 4840 | delete MI; // The pseudo instruction is gone now. |
| 4841 | return BB; |
| 4842 | } |
| 4843 | |
| 4844 | case X86::FP_TO_INT16_IN_MEM: |
| 4845 | case X86::FP_TO_INT32_IN_MEM: |
| 4846 | case X86::FP_TO_INT64_IN_MEM: { |
| 4847 | // Change the floating point control register to use "round towards zero" |
| 4848 | // mode when truncating to an integer value. |
| 4849 | MachineFunction *F = BB->getParent(); |
| 4850 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4851 | addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4852 | |
| 4853 | // Load the old value of the high byte of the control word... |
| 4854 | unsigned OldCW = |
| 4855 | F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4856 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4857 | |
| 4858 | // Set the high part to be round to zero... |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4859 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx) |
| 4860 | .addImm(0xC7F); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4861 | |
| 4862 | // Reload the modified control word now... |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4863 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4864 | |
| 4865 | // Restore the memory image of control word to original value |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4866 | addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx) |
| 4867 | .addReg(OldCW); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4868 | |
| 4869 | // Get the X86 opcode to use. |
| 4870 | unsigned Opc; |
| 4871 | switch (MI->getOpcode()) { |
| 4872 | default: assert(0 && "illegal opcode!"); |
| 4873 | case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break; |
| 4874 | case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break; |
| 4875 | case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break; |
| 4876 | } |
| 4877 | |
| 4878 | X86AddressMode AM; |
| 4879 | MachineOperand &Op = MI->getOperand(0); |
| 4880 | if (Op.isRegister()) { |
| 4881 | AM.BaseType = X86AddressMode::RegBase; |
| 4882 | AM.Base.Reg = Op.getReg(); |
| 4883 | } else { |
| 4884 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 4885 | AM.Base.FrameIndex = Op.getFrameIndex(); |
| 4886 | } |
| 4887 | Op = MI->getOperand(1); |
| 4888 | if (Op.isImmediate()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4889 | AM.Scale = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4890 | Op = MI->getOperand(2); |
| 4891 | if (Op.isImmediate()) |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4892 | AM.IndexReg = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4893 | Op = MI->getOperand(3); |
| 4894 | if (Op.isGlobalAddress()) { |
| 4895 | AM.GV = Op.getGlobal(); |
| 4896 | } else { |
Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 4897 | AM.Disp = Op.getImm(); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4898 | } |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4899 | addFullAddress(BuildMI(BB, TII->get(Opc)), AM) |
| 4900 | .addReg(MI->getOperand(4).getReg()); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4901 | |
| 4902 | // Reload the original control word now. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4903 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 4904 | |
| 4905 | delete MI; // The pseudo instruction is gone now. |
| 4906 | return BB; |
| 4907 | } |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | //===----------------------------------------------------------------------===// |
| 4912 | // X86 Optimization Hooks |
| 4913 | //===----------------------------------------------------------------------===// |
| 4914 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4915 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 4916 | uint64_t Mask, |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4917 | uint64_t &KnownZero, |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4918 | uint64_t &KnownOne, |
| 4919 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4920 | unsigned Opc = Op.getOpcode(); |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4921 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 4922 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 4923 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 4924 | Opc == ISD::INTRINSIC_VOID) && |
| 4925 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 4926 | " is a target node!"); |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4927 | |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4928 | KnownZero = KnownOne = 0; // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4929 | switch (Opc) { |
Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 4930 | default: break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4931 | case X86ISD::SETCC: |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 4932 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 4933 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4934 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 4935 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 4936 | |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4937 | /// getShuffleScalarElt - Returns the scalar element that will make up the ith |
| 4938 | /// element of the result of the vector shuffle. |
| 4939 | static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) { |
| 4940 | MVT::ValueType VT = N->getValueType(0); |
| 4941 | SDOperand PermMask = N->getOperand(2); |
| 4942 | unsigned NumElems = PermMask.getNumOperands(); |
| 4943 | SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1); |
| 4944 | i %= NumElems; |
| 4945 | if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 4946 | return (i == 0) |
| 4947 | ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT)); |
| 4948 | } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) { |
| 4949 | SDOperand Idx = PermMask.getOperand(i); |
| 4950 | if (Idx.getOpcode() == ISD::UNDEF) |
| 4951 | return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT)); |
| 4952 | return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG); |
| 4953 | } |
| 4954 | return SDOperand(); |
| 4955 | } |
| 4956 | |
| 4957 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
| 4958 | /// node is a GlobalAddress + an offset. |
| 4959 | static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) { |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 4960 | unsigned Opc = N->getOpcode(); |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 4961 | if (Opc == X86ISD::Wrapper) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4962 | if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) { |
| 4963 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
| 4964 | return true; |
| 4965 | } |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 4966 | } else if (Opc == ISD::ADD) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 4967 | SDOperand N1 = N->getOperand(0); |
| 4968 | SDOperand N2 = N->getOperand(1); |
| 4969 | if (isGAPlusOffset(N1.Val, GA, Offset)) { |
| 4970 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2); |
| 4971 | if (V) { |
| 4972 | Offset += V->getSignExtended(); |
| 4973 | return true; |
| 4974 | } |
| 4975 | } else if (isGAPlusOffset(N2.Val, GA, Offset)) { |
| 4976 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1); |
| 4977 | if (V) { |
| 4978 | Offset += V->getSignExtended(); |
| 4979 | return true; |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
| 4986 | /// isConsecutiveLoad - Returns true if N is loading from an address of Base |
| 4987 | /// + Dist * Size. |
| 4988 | static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size, |
| 4989 | MachineFrameInfo *MFI) { |
| 4990 | if (N->getOperand(0).Val != Base->getOperand(0).Val) |
| 4991 | return false; |
| 4992 | |
| 4993 | SDOperand Loc = N->getOperand(1); |
| 4994 | SDOperand BaseLoc = Base->getOperand(1); |
| 4995 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 4996 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 4997 | return false; |
| 4998 | int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 4999 | int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
| 5000 | int FS = MFI->getObjectSize(FI); |
| 5001 | int BFS = MFI->getObjectSize(BFI); |
| 5002 | if (FS != BFS || FS != Size) return false; |
| 5003 | return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size); |
| 5004 | } else { |
| 5005 | GlobalValue *GV1 = NULL; |
| 5006 | GlobalValue *GV2 = NULL; |
| 5007 | int64_t Offset1 = 0; |
| 5008 | int64_t Offset2 = 0; |
| 5009 | bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1); |
| 5010 | bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2); |
| 5011 | if (isGA1 && isGA2 && GV1 == GV2) |
| 5012 | return Offset1 == (Offset2 + Dist*Size); |
| 5013 | } |
| 5014 | |
| 5015 | return false; |
| 5016 | } |
| 5017 | |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 5018 | static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI, |
| 5019 | const X86Subtarget *Subtarget) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5020 | GlobalValue *GV; |
| 5021 | int64_t Offset; |
| 5022 | if (isGAPlusOffset(Base, GV, Offset)) |
| 5023 | return (GV->getAlignment() >= 16 && (Offset % 16) == 0); |
| 5024 | else { |
| 5025 | assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!"); |
| 5026 | int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex(); |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 5027 | if (BFI < 0) |
| 5028 | // Fixed objects do not specify alignment, however the offsets are known. |
| 5029 | return ((Subtarget->getStackAlignment() % 16) == 0 && |
| 5030 | (MFI->getObjectOffset(BFI) % 16) == 0); |
| 5031 | else |
| 5032 | return MFI->getObjectAlignment(BFI) >= 16; |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5033 | } |
| 5034 | return false; |
| 5035 | } |
| 5036 | |
| 5037 | |
| 5038 | /// PerformShuffleCombine - Combine a vector_shuffle that is equal to |
| 5039 | /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load |
| 5040 | /// if the load addresses are consecutive, non-overlapping, and in the right |
| 5041 | /// order. |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 5042 | static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
| 5043 | const X86Subtarget *Subtarget) { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5044 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5045 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 5046 | MVT::ValueType VT = N->getValueType(0); |
| 5047 | MVT::ValueType EVT = MVT::getVectorBaseType(VT); |
| 5048 | SDOperand PermMask = N->getOperand(2); |
| 5049 | int NumElems = (int)PermMask.getNumOperands(); |
| 5050 | SDNode *Base = NULL; |
| 5051 | for (int i = 0; i < NumElems; ++i) { |
| 5052 | SDOperand Idx = PermMask.getOperand(i); |
| 5053 | if (Idx.getOpcode() == ISD::UNDEF) { |
| 5054 | if (!Base) return SDOperand(); |
| 5055 | } else { |
| 5056 | SDOperand Arg = |
| 5057 | getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5058 | if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val)) |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5059 | return SDOperand(); |
| 5060 | if (!Base) |
| 5061 | Base = Arg.Val; |
| 5062 | else if (!isConsecutiveLoad(Arg.Val, Base, |
| 5063 | i, MVT::getSizeInBits(EVT)/8,MFI)) |
| 5064 | return SDOperand(); |
| 5065 | } |
| 5066 | } |
| 5067 | |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 5068 | bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5069 | if (isAlign16) { |
| 5070 | LoadSDNode *LD = cast<LoadSDNode>(Base); |
| 5071 | return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(), |
| 5072 | LD->getSrcValueOffset()); |
| 5073 | } else { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5074 | // Just use movups, it's shorter. |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 5075 | std::vector<MVT::ValueType> Tys; |
| 5076 | Tys.push_back(MVT::v4f32); |
| 5077 | Tys.push_back(MVT::Other); |
| 5078 | SmallVector<SDOperand, 3> Ops; |
| 5079 | Ops.push_back(Base->getOperand(0)); |
| 5080 | Ops.push_back(Base->getOperand(1)); |
| 5081 | Ops.push_back(Base->getOperand(2)); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5082 | return DAG.getNode(ISD::BIT_CONVERT, VT, |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 5083 | DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size())); |
Evan Cheng | 311ace0 | 2006-08-11 07:35:45 +0000 | [diff] [blame] | 5084 | } |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5087 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes. |
| 5088 | static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
| 5089 | const X86Subtarget *Subtarget) { |
| 5090 | SDOperand Cond = N->getOperand(0); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5091 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5092 | // If we have SSE[12] support, try to form min/max nodes. |
| 5093 | if (Subtarget->hasSSE2() && |
| 5094 | (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) { |
| 5095 | if (Cond.getOpcode() == ISD::SETCC) { |
| 5096 | // Get the LHS/RHS of the select. |
| 5097 | SDOperand LHS = N->getOperand(1); |
| 5098 | SDOperand RHS = N->getOperand(2); |
| 5099 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5100 | |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5101 | unsigned Opcode = 0; |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5102 | if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) { |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5103 | switch (CC) { |
| 5104 | default: break; |
| 5105 | case ISD::SETOLE: // (X <= Y) ? X : Y -> min |
| 5106 | case ISD::SETULE: |
| 5107 | case ISD::SETLE: |
| 5108 | if (!UnsafeFPMath) break; |
| 5109 | // FALL THROUGH. |
| 5110 | case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min |
| 5111 | case ISD::SETLT: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5112 | Opcode = X86ISD::FMIN; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5113 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5114 | |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5115 | case ISD::SETOGT: // (X > Y) ? X : Y -> max |
| 5116 | case ISD::SETUGT: |
| 5117 | case ISD::SETGT: |
| 5118 | if (!UnsafeFPMath) break; |
| 5119 | // FALL THROUGH. |
| 5120 | case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max |
| 5121 | case ISD::SETGE: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5122 | Opcode = X86ISD::FMAX; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5123 | break; |
| 5124 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5125 | } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) { |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5126 | switch (CC) { |
| 5127 | default: break; |
| 5128 | case ISD::SETOGT: // (X > Y) ? Y : X -> min |
| 5129 | case ISD::SETUGT: |
| 5130 | case ISD::SETGT: |
| 5131 | if (!UnsafeFPMath) break; |
| 5132 | // FALL THROUGH. |
| 5133 | case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min |
| 5134 | case ISD::SETGE: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5135 | Opcode = X86ISD::FMIN; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5136 | break; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5137 | |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5138 | case ISD::SETOLE: // (X <= Y) ? Y : X -> max |
| 5139 | case ISD::SETULE: |
| 5140 | case ISD::SETLE: |
| 5141 | if (!UnsafeFPMath) break; |
| 5142 | // FALL THROUGH. |
| 5143 | case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max |
| 5144 | case ISD::SETLT: |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5145 | Opcode = X86ISD::FMAX; |
Chris Lattner | 1907a7b | 2006-10-05 04:11:26 +0000 | [diff] [blame] | 5146 | break; |
| 5147 | } |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5148 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5149 | |
Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 5150 | if (Opcode) |
| 5151 | return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5152 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5153 | |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | return SDOperand(); |
| 5157 | } |
| 5158 | |
| 5159 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5160 | SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N, |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5161 | DAGCombinerInfo &DCI) const { |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5162 | SelectionDAG &DAG = DCI.DAG; |
| 5163 | switch (N->getOpcode()) { |
| 5164 | default: break; |
| 5165 | case ISD::VECTOR_SHUFFLE: |
Evan Cheng | 1e60c09 | 2006-07-10 21:37:44 +0000 | [diff] [blame] | 5166 | return PerformShuffleCombine(N, DAG, Subtarget); |
Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 5167 | case ISD::SELECT: |
| 5168 | return PerformSELECTCombine(N, DAG, Subtarget); |
Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | return SDOperand(); |
| 5172 | } |
| 5173 | |
Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 5174 | //===----------------------------------------------------------------------===// |
| 5175 | // X86 Inline Assembly Support |
| 5176 | //===----------------------------------------------------------------------===// |
| 5177 | |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5178 | /// getConstraintType - Given a constraint letter, return the type of |
| 5179 | /// constraint it is for this target. |
| 5180 | X86TargetLowering::ConstraintType |
| 5181 | X86TargetLowering::getConstraintType(char ConstraintLetter) const { |
| 5182 | switch (ConstraintLetter) { |
Chris Lattner | 6d34657 | 2006-07-12 16:59:49 +0000 | [diff] [blame] | 5183 | case 'A': |
| 5184 | case 'r': |
| 5185 | case 'R': |
| 5186 | case 'l': |
| 5187 | case 'q': |
| 5188 | case 'Q': |
| 5189 | case 'x': |
| 5190 | case 'Y': |
| 5191 | return C_RegisterClass; |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5192 | default: return TargetLowering::getConstraintType(ConstraintLetter); |
| 5193 | } |
| 5194 | } |
| 5195 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5196 | /// isOperandValidForConstraint - Return the specified operand (possibly |
| 5197 | /// modified) if the specified SDOperand is valid for the specified target |
| 5198 | /// constraint letter, otherwise return null. |
| 5199 | SDOperand X86TargetLowering:: |
| 5200 | isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) { |
| 5201 | switch (Constraint) { |
| 5202 | default: break; |
| 5203 | case 'i': |
| 5204 | // Literal immediates are always ok. |
| 5205 | if (isa<ConstantSDNode>(Op)) return Op; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5206 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5207 | // If we are in non-pic codegen mode, we allow the address of a global to |
| 5208 | // be used with 'i'. |
| 5209 | if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) { |
| 5210 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_) |
| 5211 | return SDOperand(0, 0); |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5212 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5213 | if (GA->getOpcode() != ISD::TargetGlobalAddress) |
| 5214 | Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0), |
| 5215 | GA->getOffset()); |
| 5216 | return Op; |
| 5217 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5218 | |
Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 5219 | // Otherwise, not valid for this mode. |
| 5220 | return SDOperand(0, 0); |
| 5221 | } |
| 5222 | return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG); |
| 5223 | } |
| 5224 | |
| 5225 | |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5226 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 5227 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 5228 | MVT::ValueType VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5229 | if (Constraint.size() == 1) { |
| 5230 | // FIXME: not handling fp-stack yet! |
| 5231 | // FIXME: not handling MMX registers yet ('y' constraint). |
| 5232 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 5233 | default: break; // Unknown constraint letter |
| 5234 | case 'A': // EAX/EDX |
| 5235 | if (VT == MVT::i32 || VT == MVT::i64) |
| 5236 | return make_vector<unsigned>(X86::EAX, X86::EDX, 0); |
| 5237 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5238 | case 'r': // GENERAL_REGS |
| 5239 | case 'R': // LEGACY_REGS |
Chris Lattner | 98ae09c | 2006-12-04 22:38:21 +0000 | [diff] [blame] | 5240 | if (VT == MVT::i64 && Subtarget->is64Bit()) |
| 5241 | return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, |
| 5242 | X86::RSI, X86::RDI, X86::RBP, X86::RSP, |
| 5243 | X86::R8, X86::R9, X86::R10, X86::R11, |
| 5244 | X86::R12, X86::R13, X86::R14, X86::R15, 0); |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5245 | if (VT == MVT::i32) |
| 5246 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, |
| 5247 | X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0); |
| 5248 | else if (VT == MVT::i16) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5249 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5250 | X86::SI, X86::DI, X86::BP, X86::SP, 0); |
| 5251 | else if (VT == MVT::i8) |
Chris Lattner | af21f4f | 2006-12-05 17:29:40 +0000 | [diff] [blame] | 5252 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0); |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5253 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5254 | case 'l': // INDEX_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5255 | if (VT == MVT::i32) |
| 5256 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, |
| 5257 | X86::ESI, X86::EDI, X86::EBP, 0); |
| 5258 | else if (VT == MVT::i16) |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5259 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5260 | X86::SI, X86::DI, X86::BP, 0); |
| 5261 | else if (VT == MVT::i8) |
| 5262 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); |
| 5263 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5264 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 5265 | case 'Q': // Q_REGS |
Chris Lattner | 80a7ecc | 2006-05-06 00:29:37 +0000 | [diff] [blame] | 5266 | if (VT == MVT::i32) |
| 5267 | return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0); |
| 5268 | else if (VT == MVT::i16) |
| 5269 | return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0); |
| 5270 | else if (VT == MVT::i8) |
| 5271 | return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0); |
| 5272 | break; |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5273 | case 'x': // SSE_REGS if SSE1 allowed |
| 5274 | if (Subtarget->hasSSE1()) |
| 5275 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 5276 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 5277 | 0); |
| 5278 | return std::vector<unsigned>(); |
| 5279 | case 'Y': // SSE_REGS if SSE2 allowed |
| 5280 | if (Subtarget->hasSSE2()) |
| 5281 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 5282 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 5283 | 0); |
| 5284 | return std::vector<unsigned>(); |
| 5285 | } |
| 5286 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5287 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 5288 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 5289 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5290 | |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5291 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5292 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
| 5293 | MVT::ValueType VT) const { |
| 5294 | // Use the default implementation in TargetLowering to convert the register |
| 5295 | // constraint into a member of a register class. |
| 5296 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 5297 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 5298 | |
| 5299 | // Not found as a standard register? |
| 5300 | if (Res.second == 0) { |
| 5301 | // GCC calls "st(0)" just plain "st". |
| 5302 | if (StringsEqualNoCase("{st}", Constraint)) { |
| 5303 | Res.first = X86::ST0; |
| 5304 | Res.second = X86::RSTRegisterClass; |
| 5305 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5306 | |
Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 5307 | return Res; |
| 5308 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5309 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5310 | // Otherwise, check to see if this is a register class of the wrong value |
| 5311 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 5312 | // turn into {ax},{dx}. |
| 5313 | if (Res.second->hasType(VT)) |
| 5314 | return Res; // Correct type already, nothing to do. |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5315 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5316 | // All of the single-register GCC register classes map their values onto |
| 5317 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 5318 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 5319 | // class and return the appropriate register. |
| 5320 | if (Res.second != X86::GR16RegisterClass) |
| 5321 | return Res; |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5322 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5323 | if (VT == MVT::i8) { |
| 5324 | unsigned DestReg = 0; |
| 5325 | switch (Res.first) { |
| 5326 | default: break; |
| 5327 | case X86::AX: DestReg = X86::AL; break; |
| 5328 | case X86::DX: DestReg = X86::DL; break; |
| 5329 | case X86::CX: DestReg = X86::CL; break; |
| 5330 | case X86::BX: DestReg = X86::BL; break; |
| 5331 | } |
| 5332 | if (DestReg) { |
| 5333 | Res.first = DestReg; |
| 5334 | Res.second = Res.second = X86::GR8RegisterClass; |
| 5335 | } |
| 5336 | } else if (VT == MVT::i32) { |
| 5337 | unsigned DestReg = 0; |
| 5338 | switch (Res.first) { |
| 5339 | default: break; |
| 5340 | case X86::AX: DestReg = X86::EAX; break; |
| 5341 | case X86::DX: DestReg = X86::EDX; break; |
| 5342 | case X86::CX: DestReg = X86::ECX; break; |
| 5343 | case X86::BX: DestReg = X86::EBX; break; |
| 5344 | case X86::SI: DestReg = X86::ESI; break; |
| 5345 | case X86::DI: DestReg = X86::EDI; break; |
| 5346 | case X86::BP: DestReg = X86::EBP; break; |
| 5347 | case X86::SP: DestReg = X86::ESP; break; |
| 5348 | } |
| 5349 | if (DestReg) { |
| 5350 | Res.first = DestReg; |
| 5351 | Res.second = Res.second = X86::GR32RegisterClass; |
| 5352 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 5353 | } else if (VT == MVT::i64) { |
| 5354 | unsigned DestReg = 0; |
| 5355 | switch (Res.first) { |
| 5356 | default: break; |
| 5357 | case X86::AX: DestReg = X86::RAX; break; |
| 5358 | case X86::DX: DestReg = X86::RDX; break; |
| 5359 | case X86::CX: DestReg = X86::RCX; break; |
| 5360 | case X86::BX: DestReg = X86::RBX; break; |
| 5361 | case X86::SI: DestReg = X86::RSI; break; |
| 5362 | case X86::DI: DestReg = X86::RDI; break; |
| 5363 | case X86::BP: DestReg = X86::RBP; break; |
| 5364 | case X86::SP: DestReg = X86::RSP; break; |
| 5365 | } |
| 5366 | if (DestReg) { |
| 5367 | Res.first = DestReg; |
| 5368 | Res.second = Res.second = X86::GR64RegisterClass; |
| 5369 | } |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5370 | } |
Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 5371 | |
Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 5372 | return Res; |
| 5373 | } |