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