Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1 | //===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that X86 uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86.h" |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 16 | #include "X86InstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 17 | #include "X86ISelLowering.h" |
| 18 | #include "X86TargetMachine.h" |
| 19 | #include "llvm/CallingConv.h" |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SSARegMap.h" |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/VectorExtras.h" |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | // FIXME: temporary. |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 35 | cl::desc("Enable fastcc on X86")); |
| 36 | |
| 37 | X86TargetLowering::X86TargetLowering(TargetMachine &TM) |
| 38 | : TargetLowering(TM) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 39 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 40 | X86ScalarSSE = Subtarget->hasSSE2(); |
| 41 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 42 | // Set up the TargetLowering object. |
| 43 | |
| 44 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 45 | setShiftAmountType(MVT::i8); |
| 46 | setSetCCResultType(MVT::i8); |
| 47 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Evan Cheng | 0b2afbd | 2006-01-25 09:15:17 +0000 | [diff] [blame] | 48 | setSchedulingPreference(SchedulingForRegPressure); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 49 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Chris Lattner | 9edba76 | 2006-01-13 18:00:54 +0000 | [diff] [blame] | 50 | setStackPointerRegisterToSaveRestore(X86::ESP); |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 51 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 52 | // Set up the register classes. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 53 | addRegisterClass(MVT::i8, X86::R8RegisterClass); |
| 54 | addRegisterClass(MVT::i16, X86::R16RegisterClass); |
| 55 | addRegisterClass(MVT::i32, X86::R32RegisterClass); |
| 56 | |
| 57 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 58 | // operation. |
| 59 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 60 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 61 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 62 | |
| 63 | if (X86ScalarSSE) |
| 64 | // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead. |
| 65 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand); |
| 66 | else |
| 67 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 68 | |
| 69 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 70 | // this operation. |
| 71 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 72 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 73 | // SSE has no i16 to fp conversion, only i32 |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 74 | if (X86ScalarSSE) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 75 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 76 | else { |
| 77 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 78 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| 79 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 80 | |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 81 | // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64 |
| 82 | // isn't legal. |
| 83 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 84 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 85 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 86 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 87 | // this operation. |
| 88 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 89 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 90 | |
| 91 | if (X86ScalarSSE) { |
| 92 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| 93 | } else { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 99 | // conversion. |
| 100 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 101 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 102 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 103 | |
Evan Cheng | 45af8fd | 2006-02-18 07:26:17 +0000 | [diff] [blame] | 104 | if (X86ScalarSSE && !Subtarget->hasSSE3()) |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 105 | // Expand FP_TO_UINT into a select. |
| 106 | // FIXME: We would like to use a Custom expander here eventually to do |
| 107 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 108 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 109 | else |
Evan Cheng | 45af8fd | 2006-02-18 07:26:17 +0000 | [diff] [blame] | 110 | // With SSE3 we can use fisttpll to convert to a signed i64. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 111 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 112 | |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 113 | setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand); |
| 114 | setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand); |
Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 115 | |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 117 | setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand); |
| 118 | setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 119 | setOperationAction(ISD::BR_CC , MVT::Other, Expand); |
| 120 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 121 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 122 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | e80242a | 2005-12-07 17:59:14 +0000 | [diff] [blame] | 123 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 124 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 125 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 126 | setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand); |
| 127 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
| 128 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 129 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 130 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 131 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 132 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 133 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
| 134 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 135 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 136 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 137 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 138 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 139 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 140 | // These should be promoted to a larger select which is supported. |
| 141 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 142 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 143 | |
| 144 | // X86 wants to expand cmov itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 145 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 146 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 147 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 148 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
| 149 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 150 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 151 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 152 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 153 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 154 | // X86 ret instruction may pop stack. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 155 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 156 | // Darwin ABI issue. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 157 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 158 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 159 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 160 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 161 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 162 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 163 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 164 | // X86 wants to expand memset / memcpy itself. |
Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 165 | setOperationAction(ISD::MEMSET , MVT::Other, Custom); |
| 166 | setOperationAction(ISD::MEMCPY , MVT::Other, Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 167 | |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 168 | // We don't have line number support yet. |
| 169 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 171 | // FIXME - use subtarget debug flags |
| 172 | if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin()) |
| 173 | setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand); |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 174 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 175 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 176 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| 177 | |
| 178 | // Use the default implementation. |
| 179 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 180 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 181 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Chris Lattner | e112552 | 2006-01-15 09:00:21 +0000 | [diff] [blame] | 182 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 183 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 184 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 9601a86 | 2006-03-05 05:08:37 +0000 | [diff] [blame] | 186 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 187 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 188 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 189 | if (X86ScalarSSE) { |
| 190 | // Set up the FP register classes. |
Evan Cheng | 5ee4ccc | 2006-01-12 08:27:59 +0000 | [diff] [blame] | 191 | addRegisterClass(MVT::f32, X86::FR32RegisterClass); |
| 192 | addRegisterClass(MVT::f64, X86::FR64RegisterClass); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 193 | |
| 194 | // SSE has no load+extend ops |
| 195 | setOperationAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 196 | setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand); |
| 197 | |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 198 | // Use ANDPD to simulate FABS. |
| 199 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 200 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| 201 | |
| 202 | // Use XORP to simulate FNEG. |
| 203 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 204 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| 205 | |
Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 206 | // We don't support sin/cos/fmod |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 207 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 208 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 209 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 210 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 211 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 212 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 213 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 214 | // Expand FP immediates into loads from the stack, except for the special |
| 215 | // cases we handle. |
| 216 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 217 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 218 | addLegalFPImmediate(+0.0); // xorps / xorpd |
| 219 | } else { |
| 220 | // Set up the FP register classes. |
| 221 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
Chris Lattner | 44d9b9b | 2006-01-29 06:44:22 +0000 | [diff] [blame] | 222 | |
| 223 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 224 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 225 | if (!UnsafeFPMath) { |
| 226 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 227 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 228 | } |
| 229 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 230 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 231 | addLegalFPImmediate(+0.0); // FLD0 |
| 232 | addLegalFPImmediate(+1.0); // FLD1 |
| 233 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 234 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 235 | } |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 236 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 237 | // First set operation action for all vector types to expand. Then we |
| 238 | // will selectively turn on ones that can be effectively codegen'd. |
| 239 | for (unsigned VT = (unsigned)MVT::Vector + 1; |
| 240 | VT != (unsigned)MVT::LAST_VALUETYPE; VT++) { |
| 241 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 242 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
| 243 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
| 244 | setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand); |
| 245 | } |
| 246 | |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 247 | if (TM.getSubtarget<X86Subtarget>().hasMMX()) { |
| 248 | addRegisterClass(MVT::v8i8, X86::VR64RegisterClass); |
| 249 | addRegisterClass(MVT::v4i16, X86::VR64RegisterClass); |
| 250 | addRegisterClass(MVT::v2i32, X86::VR64RegisterClass); |
| 251 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 252 | // FIXME: add MMX packed arithmetics |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 253 | setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand); |
| 254 | setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand); |
| 255 | setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand); |
| 256 | } |
| 257 | |
| 258 | if (TM.getSubtarget<X86Subtarget>().hasSSE1()) { |
| 259 | addRegisterClass(MVT::v4f32, X86::VR128RegisterClass); |
| 260 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 261 | setOperationAction(ISD::ADD , MVT::v4f32, Legal); |
| 262 | setOperationAction(ISD::SUB , MVT::v4f32, Legal); |
| 263 | setOperationAction(ISD::MUL , MVT::v4f32, Legal); |
| 264 | setOperationAction(ISD::LOAD , MVT::v4f32, Legal); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 265 | setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand); |
| 266 | } |
| 267 | |
| 268 | if (TM.getSubtarget<X86Subtarget>().hasSSE2()) { |
| 269 | addRegisterClass(MVT::v2f64, X86::VR128RegisterClass); |
| 270 | addRegisterClass(MVT::v16i8, X86::VR128RegisterClass); |
| 271 | addRegisterClass(MVT::v8i16, X86::VR128RegisterClass); |
| 272 | addRegisterClass(MVT::v4i32, X86::VR128RegisterClass); |
| 273 | addRegisterClass(MVT::v2i64, X86::VR128RegisterClass); |
| 274 | |
| 275 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 276 | setOperationAction(ISD::ADD , MVT::v2f64, Legal); |
| 277 | setOperationAction(ISD::SUB , MVT::v2f64, Legal); |
| 278 | setOperationAction(ISD::MUL , MVT::v2f64, Legal); |
| 279 | setOperationAction(ISD::LOAD , MVT::v2f64, Legal); |
Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 280 | setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand); |
| 281 | setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand); |
| 282 | setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand); |
| 283 | setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand); |
| 284 | setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand); |
| 285 | } |
| 286 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 287 | computeRegisterProperties(); |
| 288 | |
Evan Cheng | 87ed716 | 2006-02-14 08:25:08 +0000 | [diff] [blame] | 289 | // FIXME: These should be based on subtarget info. Plus, the values should |
| 290 | // be smaller when we are in optimizing for size mode. |
Evan Cheng | a03a5dc | 2006-02-14 08:38:30 +0000 | [diff] [blame] | 291 | maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores |
| 292 | maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores |
| 293 | maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 294 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 295 | } |
| 296 | |
| 297 | std::vector<SDOperand> |
| 298 | X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 299 | if (F.getCallingConv() == CallingConv::Fast && EnableFastCC) |
| 300 | return LowerFastCCArguments(F, DAG); |
| 301 | return LowerCCCArguments(F, DAG); |
| 302 | } |
| 303 | |
| 304 | std::pair<SDOperand, SDOperand> |
| 305 | X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 306 | bool isVarArg, unsigned CallingConv, |
| 307 | bool isTailCall, |
| 308 | SDOperand Callee, ArgListTy &Args, |
| 309 | SelectionDAG &DAG) { |
| 310 | assert((!isVarArg || CallingConv == CallingConv::C) && |
| 311 | "Only C takes varargs!"); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 312 | |
| 313 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 314 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 315 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 316 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy()); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 317 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 318 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy()); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 319 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 320 | if (CallingConv == CallingConv::Fast && EnableFastCC) |
| 321 | return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG); |
| 322 | return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG); |
| 323 | } |
| 324 | |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | // C Calling Convention implementation |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
| 329 | std::vector<SDOperand> |
| 330 | X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) { |
| 331 | std::vector<SDOperand> ArgValues; |
| 332 | |
| 333 | MachineFunction &MF = DAG.getMachineFunction(); |
| 334 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 335 | |
| 336 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 337 | // the stack frame looks like this: |
| 338 | // |
| 339 | // [ESP] -- return address |
| 340 | // [ESP + 4] -- first argument (leftmost lexically) |
| 341 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
| 342 | // ... |
| 343 | // |
| 344 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 345 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 346 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 347 | unsigned ArgIncrement = 4; |
| 348 | unsigned ObjSize; |
| 349 | switch (ObjectVT) { |
| 350 | default: assert(0 && "Unhandled argument type!"); |
| 351 | case MVT::i1: |
| 352 | case MVT::i8: ObjSize = 1; break; |
| 353 | case MVT::i16: ObjSize = 2; break; |
| 354 | case MVT::i32: ObjSize = 4; break; |
| 355 | case MVT::i64: ObjSize = ArgIncrement = 8; break; |
| 356 | case MVT::f32: ObjSize = 4; break; |
| 357 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 358 | } |
| 359 | // Create the frame index object for this incoming parameter... |
| 360 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 361 | |
| 362 | // Create the SelectionDAG nodes corresponding to a load from this parameter |
| 363 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 364 | |
| 365 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 366 | // dead loads. |
| 367 | SDOperand ArgValue; |
| 368 | if (!I->use_empty()) |
| 369 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 370 | DAG.getSrcValue(NULL)); |
| 371 | else { |
| 372 | if (MVT::isInteger(ObjectVT)) |
| 373 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 374 | else |
| 375 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 376 | } |
| 377 | ArgValues.push_back(ArgValue); |
| 378 | |
| 379 | ArgOffset += ArgIncrement; // Move on to the next argument... |
| 380 | } |
| 381 | |
| 382 | // If the function takes variable number of arguments, make a frame index for |
| 383 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 384 | if (F.isVarArg()) |
| 385 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
| 386 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 387 | BytesToPopOnReturn = 0; // Callee pops nothing. |
| 388 | BytesCallerReserves = ArgOffset; |
| 389 | |
| 390 | // Finally, inform the code generator which regs we return values in. |
| 391 | switch (getValueType(F.getReturnType())) { |
| 392 | default: assert(0 && "Unknown type!"); |
| 393 | case MVT::isVoid: break; |
| 394 | case MVT::i1: |
| 395 | case MVT::i8: |
| 396 | case MVT::i16: |
| 397 | case MVT::i32: |
| 398 | MF.addLiveOut(X86::EAX); |
| 399 | break; |
| 400 | case MVT::i64: |
| 401 | MF.addLiveOut(X86::EAX); |
| 402 | MF.addLiveOut(X86::EDX); |
| 403 | break; |
| 404 | case MVT::f32: |
| 405 | case MVT::f64: |
| 406 | MF.addLiveOut(X86::ST0); |
| 407 | break; |
| 408 | } |
| 409 | return ArgValues; |
| 410 | } |
| 411 | |
| 412 | std::pair<SDOperand, SDOperand> |
| 413 | X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, |
| 414 | bool isVarArg, bool isTailCall, |
| 415 | SDOperand Callee, ArgListTy &Args, |
| 416 | SelectionDAG &DAG) { |
| 417 | // Count how many bytes are to be pushed on the stack. |
| 418 | unsigned NumBytes = 0; |
| 419 | |
| 420 | if (Args.empty()) { |
| 421 | // Save zero bytes. |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 422 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 423 | } else { |
| 424 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 425 | switch (getValueType(Args[i].second)) { |
| 426 | default: assert(0 && "Unknown value type!"); |
| 427 | case MVT::i1: |
| 428 | case MVT::i8: |
| 429 | case MVT::i16: |
| 430 | case MVT::i32: |
| 431 | case MVT::f32: |
| 432 | NumBytes += 4; |
| 433 | break; |
| 434 | case MVT::i64: |
| 435 | case MVT::f64: |
| 436 | NumBytes += 8; |
| 437 | break; |
| 438 | } |
| 439 | |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 440 | Chain = DAG.getCALLSEQ_START(Chain, |
| 441 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 442 | |
| 443 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 444 | unsigned ArgOffset = 0; |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 445 | SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 446 | std::vector<SDOperand> Stores; |
| 447 | |
| 448 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 449 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 450 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 451 | |
| 452 | switch (getValueType(Args[i].second)) { |
| 453 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 454 | case MVT::i1: |
| 455 | case MVT::i8: |
| 456 | case MVT::i16: |
| 457 | // Promote the integer to 32 bits. If the input type is signed use a |
| 458 | // sign extend, otherwise use a zero extend. |
| 459 | if (Args[i].second->isSigned()) |
| 460 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 461 | else |
| 462 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 463 | |
| 464 | // FALL THROUGH |
| 465 | case MVT::i32: |
| 466 | case MVT::f32: |
| 467 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 468 | Args[i].first, PtrOff, |
| 469 | DAG.getSrcValue(NULL))); |
| 470 | ArgOffset += 4; |
| 471 | break; |
| 472 | case MVT::i64: |
| 473 | case MVT::f64: |
| 474 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 475 | Args[i].first, PtrOff, |
| 476 | DAG.getSrcValue(NULL))); |
| 477 | ArgOffset += 8; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 482 | } |
| 483 | |
| 484 | std::vector<MVT::ValueType> RetVals; |
| 485 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 486 | RetVals.push_back(MVT::Other); |
| 487 | |
| 488 | // The result values produced have to be legal. Promote the result. |
| 489 | switch (RetTyVT) { |
| 490 | case MVT::isVoid: break; |
| 491 | default: |
| 492 | RetVals.push_back(RetTyVT); |
| 493 | break; |
| 494 | case MVT::i1: |
| 495 | case MVT::i8: |
| 496 | case MVT::i16: |
| 497 | RetVals.push_back(MVT::i32); |
| 498 | break; |
| 499 | case MVT::f32: |
| 500 | if (X86ScalarSSE) |
| 501 | RetVals.push_back(MVT::f32); |
| 502 | else |
| 503 | RetVals.push_back(MVT::f64); |
| 504 | break; |
| 505 | case MVT::i64: |
| 506 | RetVals.push_back(MVT::i32); |
| 507 | RetVals.push_back(MVT::i32); |
| 508 | break; |
| 509 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 510 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 511 | std::vector<MVT::ValueType> NodeTys; |
| 512 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 513 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 514 | std::vector<SDOperand> Ops; |
| 515 | Ops.push_back(Chain); |
| 516 | Ops.push_back(Callee); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 517 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 518 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
| 519 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops); |
| 520 | SDOperand InFlag = Chain.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 521 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 522 | NodeTys.clear(); |
| 523 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 524 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 525 | Ops.clear(); |
| 526 | Ops.push_back(Chain); |
| 527 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 528 | Ops.push_back(DAG.getConstant(0, getPointerTy())); |
| 529 | Ops.push_back(InFlag); |
| 530 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops); |
| 531 | InFlag = Chain.getValue(1); |
| 532 | |
| 533 | SDOperand RetVal; |
| 534 | if (RetTyVT != MVT::isVoid) { |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 535 | switch (RetTyVT) { |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 536 | default: assert(0 && "Unknown value type to return!"); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 537 | case MVT::i1: |
| 538 | case MVT::i8: |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 539 | RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag); |
| 540 | Chain = RetVal.getValue(1); |
| 541 | if (RetTyVT == MVT::i1) |
| 542 | RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal); |
| 543 | break; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 544 | case MVT::i16: |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 545 | RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag); |
| 546 | Chain = RetVal.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 547 | break; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 548 | case MVT::i32: |
| 549 | RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag); |
| 550 | Chain = RetVal.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 551 | break; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 552 | case MVT::i64: { |
| 553 | SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag); |
| 554 | SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, |
| 555 | Lo.getValue(2)); |
| 556 | RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi); |
| 557 | Chain = Hi.getValue(1); |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 560 | case MVT::f32: |
| 561 | case MVT::f64: { |
| 562 | std::vector<MVT::ValueType> Tys; |
| 563 | Tys.push_back(MVT::f64); |
| 564 | Tys.push_back(MVT::Other); |
| 565 | Tys.push_back(MVT::Flag); |
| 566 | std::vector<SDOperand> Ops; |
| 567 | Ops.push_back(Chain); |
| 568 | Ops.push_back(InFlag); |
| 569 | RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops); |
| 570 | Chain = RetVal.getValue(1); |
| 571 | InFlag = RetVal.getValue(2); |
| 572 | if (X86ScalarSSE) { |
| 573 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 574 | // shouldn't be necessary except that RFP cannot be live across |
| 575 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 576 | MachineFunction &MF = DAG.getMachineFunction(); |
| 577 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 578 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 579 | Tys.clear(); |
| 580 | Tys.push_back(MVT::Other); |
| 581 | Ops.clear(); |
| 582 | Ops.push_back(Chain); |
| 583 | Ops.push_back(RetVal); |
| 584 | Ops.push_back(StackSlot); |
| 585 | Ops.push_back(DAG.getValueType(RetTyVT)); |
| 586 | Ops.push_back(InFlag); |
| 587 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 588 | RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot, |
| 589 | DAG.getSrcValue(NULL)); |
| 590 | Chain = RetVal.getValue(1); |
| 591 | } |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 592 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 593 | if (RetTyVT == MVT::f32 && !X86ScalarSSE) |
| 594 | // FIXME: we would really like to remember that this FP_ROUND |
| 595 | // operation is okay to eliminate if we allow excess FP precision. |
| 596 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 597 | break; |
| 598 | } |
| 599 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 600 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 601 | |
| 602 | return std::make_pair(RetVal, Chain); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 605 | //===----------------------------------------------------------------------===// |
| 606 | // Fast Calling Convention implementation |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | // |
| 609 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 610 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 611 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 612 | // tail calls), and has the same return value conventions as C calling convs. |
| 613 | // |
| 614 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 615 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 616 | // reasons. |
| 617 | // |
| 618 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 619 | // (when we have a global fp allocator) and do other tricks. |
| 620 | // |
| 621 | |
| 622 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 623 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 624 | /// register for it. |
| 625 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 626 | TargetRegisterClass *RC) { |
| 627 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 628 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 629 | MF.addLiveIn(PReg, VReg); |
| 630 | return VReg; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | std::vector<SDOperand> |
| 635 | X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) { |
| 636 | std::vector<SDOperand> ArgValues; |
| 637 | |
| 638 | MachineFunction &MF = DAG.getMachineFunction(); |
| 639 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 640 | |
| 641 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 642 | // frame looks like this: |
| 643 | // |
| 644 | // [ESP] -- return address |
| 645 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
| 646 | // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size |
| 647 | // ... |
| 648 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 649 | |
| 650 | // Keep track of the number of integer regs passed so far. This can be either |
| 651 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 652 | // used). |
| 653 | unsigned NumIntRegs = 0; |
| 654 | |
| 655 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 656 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 657 | unsigned ArgIncrement = 4; |
| 658 | unsigned ObjSize = 0; |
| 659 | SDOperand ArgValue; |
| 660 | |
| 661 | switch (ObjectVT) { |
| 662 | default: assert(0 && "Unhandled argument type!"); |
| 663 | case MVT::i1: |
| 664 | case MVT::i8: |
| 665 | if (NumIntRegs < 2) { |
| 666 | if (!I->use_empty()) { |
| 667 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL, |
| 668 | X86::R8RegisterClass); |
| 669 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8); |
| 670 | DAG.setRoot(ArgValue.getValue(1)); |
Chris Lattner | f31d193 | 2005-12-27 03:02:18 +0000 | [diff] [blame] | 671 | if (ObjectVT == MVT::i1) |
| 672 | // FIXME: Should insert a assertzext here. |
| 673 | ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 674 | } |
| 675 | ++NumIntRegs; |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | ObjSize = 1; |
| 680 | break; |
| 681 | case MVT::i16: |
| 682 | if (NumIntRegs < 2) { |
| 683 | if (!I->use_empty()) { |
| 684 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX, |
| 685 | X86::R16RegisterClass); |
| 686 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16); |
| 687 | DAG.setRoot(ArgValue.getValue(1)); |
| 688 | } |
| 689 | ++NumIntRegs; |
| 690 | break; |
| 691 | } |
| 692 | ObjSize = 2; |
| 693 | break; |
| 694 | case MVT::i32: |
| 695 | if (NumIntRegs < 2) { |
| 696 | if (!I->use_empty()) { |
| 697 | unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX, |
| 698 | X86::R32RegisterClass); |
| 699 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32); |
| 700 | DAG.setRoot(ArgValue.getValue(1)); |
| 701 | } |
| 702 | ++NumIntRegs; |
| 703 | break; |
| 704 | } |
| 705 | ObjSize = 4; |
| 706 | break; |
| 707 | case MVT::i64: |
| 708 | if (NumIntRegs == 0) { |
| 709 | if (!I->use_empty()) { |
| 710 | unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass); |
| 711 | unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 712 | |
| 713 | SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32); |
| 714 | SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32); |
| 715 | DAG.setRoot(Hi.getValue(1)); |
| 716 | |
| 717 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 718 | } |
| 719 | NumIntRegs = 2; |
| 720 | break; |
| 721 | } else if (NumIntRegs == 1) { |
| 722 | if (!I->use_empty()) { |
| 723 | unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 724 | SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32); |
| 725 | DAG.setRoot(Low.getValue(1)); |
| 726 | |
| 727 | // Load the high part from memory. |
| 728 | // Create the frame index object for this incoming parameter... |
| 729 | int FI = MFI->CreateFixedObject(4, ArgOffset); |
| 730 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 731 | SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 732 | DAG.getSrcValue(NULL)); |
| 733 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 734 | } |
| 735 | ArgOffset += 4; |
| 736 | NumIntRegs = 2; |
| 737 | break; |
| 738 | } |
| 739 | ObjSize = ArgIncrement = 8; |
| 740 | break; |
| 741 | case MVT::f32: ObjSize = 4; break; |
| 742 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 743 | } |
| 744 | |
| 745 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 746 | // dead loads. |
| 747 | if (ObjSize && !I->use_empty()) { |
| 748 | // Create the frame index object for this incoming parameter... |
| 749 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 750 | |
| 751 | // Create the SelectionDAG nodes corresponding to a load from this |
| 752 | // parameter. |
| 753 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 754 | |
| 755 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 756 | DAG.getSrcValue(NULL)); |
| 757 | } else if (ArgValue.Val == 0) { |
| 758 | if (MVT::isInteger(ObjectVT)) |
| 759 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 760 | else |
| 761 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 762 | } |
| 763 | ArgValues.push_back(ArgValue); |
| 764 | |
| 765 | if (ObjSize) |
| 766 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 767 | } |
| 768 | |
| 769 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 770 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 771 | if ((ArgOffset & 7) == 0) |
| 772 | ArgOffset += 4; |
| 773 | |
| 774 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 775 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 776 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
| 777 | BytesCallerReserves = 0; |
| 778 | |
| 779 | // Finally, inform the code generator which regs we return values in. |
| 780 | switch (getValueType(F.getReturnType())) { |
| 781 | default: assert(0 && "Unknown type!"); |
| 782 | case MVT::isVoid: break; |
| 783 | case MVT::i1: |
| 784 | case MVT::i8: |
| 785 | case MVT::i16: |
| 786 | case MVT::i32: |
| 787 | MF.addLiveOut(X86::EAX); |
| 788 | break; |
| 789 | case MVT::i64: |
| 790 | MF.addLiveOut(X86::EAX); |
| 791 | MF.addLiveOut(X86::EDX); |
| 792 | break; |
| 793 | case MVT::f32: |
| 794 | case MVT::f64: |
| 795 | MF.addLiveOut(X86::ST0); |
| 796 | break; |
| 797 | } |
| 798 | return ArgValues; |
| 799 | } |
| 800 | |
| 801 | std::pair<SDOperand, SDOperand> |
| 802 | X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, |
| 803 | bool isTailCall, SDOperand Callee, |
| 804 | ArgListTy &Args, SelectionDAG &DAG) { |
| 805 | // Count how many bytes are to be pushed on the stack. |
| 806 | unsigned NumBytes = 0; |
| 807 | |
| 808 | // Keep track of the number of integer regs passed so far. This can be either |
| 809 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 810 | // used). |
| 811 | unsigned NumIntRegs = 0; |
| 812 | |
| 813 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 814 | switch (getValueType(Args[i].second)) { |
| 815 | default: assert(0 && "Unknown value type!"); |
| 816 | case MVT::i1: |
| 817 | case MVT::i8: |
| 818 | case MVT::i16: |
| 819 | case MVT::i32: |
| 820 | if (NumIntRegs < 2) { |
| 821 | ++NumIntRegs; |
| 822 | break; |
| 823 | } |
| 824 | // fall through |
| 825 | case MVT::f32: |
| 826 | NumBytes += 4; |
| 827 | break; |
| 828 | case MVT::i64: |
| 829 | if (NumIntRegs == 0) { |
| 830 | NumIntRegs = 2; |
| 831 | break; |
| 832 | } else if (NumIntRegs == 1) { |
| 833 | NumIntRegs = 2; |
| 834 | NumBytes += 4; |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | // fall through |
| 839 | case MVT::f64: |
| 840 | NumBytes += 8; |
| 841 | break; |
| 842 | } |
| 843 | |
| 844 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 845 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 846 | if ((NumBytes & 7) == 0) |
| 847 | NumBytes += 4; |
| 848 | |
Chris Lattner | 94dd292 | 2006-02-13 09:00:43 +0000 | [diff] [blame] | 849 | Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 850 | |
| 851 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 852 | unsigned ArgOffset = 0; |
Chris Lattner | 91cacc8 | 2006-01-24 06:14:44 +0000 | [diff] [blame] | 853 | SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 854 | NumIntRegs = 0; |
| 855 | std::vector<SDOperand> Stores; |
| 856 | std::vector<SDOperand> RegValuesToPass; |
| 857 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 858 | switch (getValueType(Args[i].second)) { |
| 859 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 860 | case MVT::i1: |
Chris Lattner | f31d193 | 2005-12-27 03:02:18 +0000 | [diff] [blame] | 861 | Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first); |
| 862 | // Fall through. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 863 | case MVT::i8: |
| 864 | case MVT::i16: |
| 865 | case MVT::i32: |
| 866 | if (NumIntRegs < 2) { |
| 867 | RegValuesToPass.push_back(Args[i].first); |
| 868 | ++NumIntRegs; |
| 869 | break; |
| 870 | } |
| 871 | // Fall through |
| 872 | case MVT::f32: { |
| 873 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 874 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 875 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 876 | Args[i].first, PtrOff, |
| 877 | DAG.getSrcValue(NULL))); |
| 878 | ArgOffset += 4; |
| 879 | break; |
| 880 | } |
| 881 | case MVT::i64: |
| 882 | if (NumIntRegs < 2) { // Can pass part of it in regs? |
| 883 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 884 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 885 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 886 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 887 | RegValuesToPass.push_back(Lo); |
| 888 | ++NumIntRegs; |
| 889 | if (NumIntRegs < 2) { // Pass both parts in regs? |
| 890 | RegValuesToPass.push_back(Hi); |
| 891 | ++NumIntRegs; |
| 892 | } else { |
| 893 | // Pass the high part in memory. |
| 894 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 895 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 896 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 897 | Hi, PtrOff, DAG.getSrcValue(NULL))); |
| 898 | ArgOffset += 4; |
| 899 | } |
| 900 | break; |
| 901 | } |
| 902 | // Fall through |
| 903 | case MVT::f64: |
| 904 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 905 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 906 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 907 | Args[i].first, PtrOff, |
| 908 | DAG.getSrcValue(NULL))); |
| 909 | ArgOffset += 8; |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | if (!Stores.empty()) |
| 914 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 915 | |
| 916 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 917 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 918 | if ((ArgOffset & 7) == 0) |
| 919 | ArgOffset += 4; |
| 920 | |
| 921 | std::vector<MVT::ValueType> RetVals; |
| 922 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 923 | |
| 924 | RetVals.push_back(MVT::Other); |
| 925 | |
| 926 | // The result values produced have to be legal. Promote the result. |
| 927 | switch (RetTyVT) { |
| 928 | case MVT::isVoid: break; |
| 929 | default: |
| 930 | RetVals.push_back(RetTyVT); |
| 931 | break; |
| 932 | case MVT::i1: |
| 933 | case MVT::i8: |
| 934 | case MVT::i16: |
| 935 | RetVals.push_back(MVT::i32); |
| 936 | break; |
| 937 | case MVT::f32: |
| 938 | if (X86ScalarSSE) |
| 939 | RetVals.push_back(MVT::f32); |
| 940 | else |
| 941 | RetVals.push_back(MVT::f64); |
| 942 | break; |
| 943 | case MVT::i64: |
| 944 | RetVals.push_back(MVT::i32); |
| 945 | RetVals.push_back(MVT::i32); |
| 946 | break; |
| 947 | } |
| 948 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 949 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 950 | // and flag operands which copy the outgoing args into registers. |
| 951 | SDOperand InFlag; |
| 952 | for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) { |
| 953 | unsigned CCReg; |
| 954 | SDOperand RegToPass = RegValuesToPass[i]; |
| 955 | switch (RegToPass.getValueType()) { |
| 956 | default: assert(0 && "Bad thing to pass in regs"); |
| 957 | case MVT::i8: |
| 958 | CCReg = (i == 0) ? X86::AL : X86::DL; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 959 | break; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 960 | case MVT::i16: |
| 961 | CCReg = (i == 0) ? X86::AX : X86::DX; |
| 962 | break; |
| 963 | case MVT::i32: |
| 964 | CCReg = (i == 0) ? X86::EAX : X86::EDX; |
| 965 | break; |
| 966 | } |
| 967 | |
| 968 | Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag); |
| 969 | InFlag = Chain.getValue(1); |
| 970 | } |
| 971 | |
| 972 | std::vector<MVT::ValueType> NodeTys; |
| 973 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 974 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 975 | std::vector<SDOperand> Ops; |
| 976 | Ops.push_back(Chain); |
| 977 | Ops.push_back(Callee); |
| 978 | if (InFlag.Val) |
| 979 | Ops.push_back(InFlag); |
| 980 | |
| 981 | // FIXME: Do not generate X86ISD::TAILCALL for now. |
| 982 | Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops); |
| 983 | InFlag = Chain.getValue(1); |
| 984 | |
| 985 | NodeTys.clear(); |
| 986 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 987 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 988 | Ops.clear(); |
| 989 | Ops.push_back(Chain); |
| 990 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 991 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 992 | Ops.push_back(InFlag); |
| 993 | Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops); |
| 994 | InFlag = Chain.getValue(1); |
| 995 | |
| 996 | SDOperand RetVal; |
| 997 | if (RetTyVT != MVT::isVoid) { |
| 998 | switch (RetTyVT) { |
| 999 | default: assert(0 && "Unknown value type to return!"); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1000 | case MVT::i1: |
| 1001 | case MVT::i8: |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1002 | RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag); |
| 1003 | Chain = RetVal.getValue(1); |
| 1004 | if (RetTyVT == MVT::i1) |
| 1005 | RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal); |
| 1006 | break; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1007 | case MVT::i16: |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1008 | RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag); |
| 1009 | Chain = RetVal.getValue(1); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1010 | break; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1011 | case MVT::i32: |
| 1012 | RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag); |
| 1013 | Chain = RetVal.getValue(1); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1014 | break; |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1015 | case MVT::i64: { |
| 1016 | SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag); |
| 1017 | SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, |
| 1018 | Lo.getValue(2)); |
| 1019 | RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi); |
| 1020 | Chain = Hi.getValue(1); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1023 | case MVT::f32: |
| 1024 | case MVT::f64: { |
| 1025 | std::vector<MVT::ValueType> Tys; |
| 1026 | Tys.push_back(MVT::f64); |
| 1027 | Tys.push_back(MVT::Other); |
| 1028 | Tys.push_back(MVT::Flag); |
| 1029 | std::vector<SDOperand> Ops; |
| 1030 | Ops.push_back(Chain); |
| 1031 | Ops.push_back(InFlag); |
| 1032 | RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops); |
| 1033 | Chain = RetVal.getValue(1); |
| 1034 | InFlag = RetVal.getValue(2); |
| 1035 | if (X86ScalarSSE) { |
| 1036 | // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This |
| 1037 | // shouldn't be necessary except that RFP cannot be live across |
| 1038 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 1039 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1040 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 1041 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1042 | Tys.clear(); |
| 1043 | Tys.push_back(MVT::Other); |
| 1044 | Ops.clear(); |
| 1045 | Ops.push_back(Chain); |
| 1046 | Ops.push_back(RetVal); |
| 1047 | Ops.push_back(StackSlot); |
| 1048 | Ops.push_back(DAG.getValueType(RetTyVT)); |
| 1049 | Ops.push_back(InFlag); |
| 1050 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 1051 | RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot, |
| 1052 | DAG.getSrcValue(NULL)); |
| 1053 | Chain = RetVal.getValue(1); |
| 1054 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1055 | |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1056 | if (RetTyVT == MVT::f32 && !X86ScalarSSE) |
| 1057 | // FIXME: we would really like to remember that this FP_ROUND |
| 1058 | // operation is okay to eliminate if we allow excess FP precision. |
| 1059 | RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal); |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1063 | } |
Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 1064 | |
| 1065 | return std::make_pair(RetVal, Chain); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
| 1069 | if (ReturnAddrIndex == 0) { |
| 1070 | // Set up a frame object for the return address. |
| 1071 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1072 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
| 1073 | } |
| 1074 | |
| 1075 | return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32); |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | std::pair<SDOperand, SDOperand> X86TargetLowering:: |
| 1081 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 1082 | SelectionDAG &DAG) { |
| 1083 | SDOperand Result; |
| 1084 | if (Depth) // Depths > 0 not supported yet! |
| 1085 | Result = DAG.getConstant(0, getPointerTy()); |
| 1086 | else { |
| 1087 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 1088 | if (!isFrameAddress) |
| 1089 | // Just load the return address |
| 1090 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, |
| 1091 | DAG.getSrcValue(NULL)); |
| 1092 | else |
| 1093 | Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI, |
| 1094 | DAG.getConstant(4, MVT::i32)); |
| 1095 | } |
| 1096 | return std::make_pair(Result, Chain); |
| 1097 | } |
| 1098 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1099 | /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode |
| 1100 | /// which corresponds to the condition code. |
| 1101 | static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) { |
| 1102 | switch (X86CC) { |
| 1103 | default: assert(0 && "Unknown X86 conditional code!"); |
| 1104 | case X86ISD::COND_A: return X86::JA; |
| 1105 | case X86ISD::COND_AE: return X86::JAE; |
| 1106 | case X86ISD::COND_B: return X86::JB; |
| 1107 | case X86ISD::COND_BE: return X86::JBE; |
| 1108 | case X86ISD::COND_E: return X86::JE; |
| 1109 | case X86ISD::COND_G: return X86::JG; |
| 1110 | case X86ISD::COND_GE: return X86::JGE; |
| 1111 | case X86ISD::COND_L: return X86::JL; |
| 1112 | case X86ISD::COND_LE: return X86::JLE; |
| 1113 | case X86ISD::COND_NE: return X86::JNE; |
| 1114 | case X86ISD::COND_NO: return X86::JNO; |
| 1115 | case X86ISD::COND_NP: return X86::JNP; |
| 1116 | case X86ISD::COND_NS: return X86::JNS; |
| 1117 | case X86ISD::COND_O: return X86::JO; |
| 1118 | case X86ISD::COND_P: return X86::JP; |
| 1119 | case X86ISD::COND_S: return X86::JS; |
| 1120 | } |
| 1121 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1122 | |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1123 | /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 1124 | /// specific condition code. It returns a false if it cannot do a direct |
| 1125 | /// translation. X86CC is the translated CondCode. Flip is set to true if the |
| 1126 | /// the order of comparison operands should be flipped. |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 1127 | static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC, |
| 1128 | bool &Flip) { |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1129 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1130 | Flip = false; |
| 1131 | X86CC = X86ISD::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1132 | if (!isFP) { |
| 1133 | switch (SetCCOpcode) { |
| 1134 | default: break; |
| 1135 | case ISD::SETEQ: X86CC = X86ISD::COND_E; break; |
| 1136 | case ISD::SETGT: X86CC = X86ISD::COND_G; break; |
| 1137 | case ISD::SETGE: X86CC = X86ISD::COND_GE; break; |
| 1138 | case ISD::SETLT: X86CC = X86ISD::COND_L; break; |
| 1139 | case ISD::SETLE: X86CC = X86ISD::COND_LE; break; |
| 1140 | case ISD::SETNE: X86CC = X86ISD::COND_NE; break; |
| 1141 | case ISD::SETULT: X86CC = X86ISD::COND_B; break; |
| 1142 | case ISD::SETUGT: X86CC = X86ISD::COND_A; break; |
| 1143 | case ISD::SETULE: X86CC = X86ISD::COND_BE; break; |
| 1144 | case ISD::SETUGE: X86CC = X86ISD::COND_AE; break; |
| 1145 | } |
| 1146 | } else { |
| 1147 | // On a floating point condition, the flags are set as follows: |
| 1148 | // ZF PF CF op |
| 1149 | // 0 | 0 | 0 | X > Y |
| 1150 | // 0 | 0 | 1 | X < Y |
| 1151 | // 1 | 0 | 0 | X == Y |
| 1152 | // 1 | 1 | 1 | unordered |
| 1153 | switch (SetCCOpcode) { |
| 1154 | default: break; |
| 1155 | case ISD::SETUEQ: |
| 1156 | case ISD::SETEQ: X86CC = X86ISD::COND_E; break; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1157 | case ISD::SETOLE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1158 | case ISD::SETOGT: |
| 1159 | case ISD::SETGT: X86CC = X86ISD::COND_A; break; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1160 | case ISD::SETOLT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1161 | case ISD::SETOGE: |
| 1162 | case ISD::SETGE: X86CC = X86ISD::COND_AE; break; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1163 | case ISD::SETUGE: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1164 | case ISD::SETULT: |
| 1165 | case ISD::SETLT: X86CC = X86ISD::COND_B; break; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1166 | case ISD::SETUGT: Flip = true; // Fallthrough |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1167 | case ISD::SETULE: |
| 1168 | case ISD::SETLE: X86CC = X86ISD::COND_BE; break; |
| 1169 | case ISD::SETONE: |
| 1170 | case ISD::SETNE: X86CC = X86ISD::COND_NE; break; |
| 1171 | case ISD::SETUO: X86CC = X86ISD::COND_P; break; |
| 1172 | case ISD::SETO: X86CC = X86ISD::COND_NP; break; |
| 1173 | } |
| 1174 | } |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1175 | |
| 1176 | return X86CC != X86ISD::COND_INVALID; |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1179 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 1180 | /// code. Current x86 isa includes the following FP cmov instructions: |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1181 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1182 | static bool hasFPCMov(unsigned X86CC) { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1183 | switch (X86CC) { |
| 1184 | default: |
| 1185 | return false; |
| 1186 | case X86ISD::COND_B: |
| 1187 | case X86ISD::COND_BE: |
| 1188 | case X86ISD::COND_E: |
| 1189 | case X86ISD::COND_P: |
| 1190 | case X86ISD::COND_A: |
| 1191 | case X86ISD::COND_AE: |
| 1192 | case X86ISD::COND_NE: |
| 1193 | case X86ISD::COND_NP: |
| 1194 | return true; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1198 | MachineBasicBlock * |
| 1199 | X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1200 | MachineBasicBlock *BB) { |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1201 | switch (MI->getOpcode()) { |
| 1202 | default: assert(false && "Unexpected instr type to insert"); |
| 1203 | case X86::CMOV_FR32: |
| 1204 | case X86::CMOV_FR64: { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 1205 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 1206 | // diamond control-flow pattern. The incoming instruction knows the |
| 1207 | // destination vreg to set, the condition code register to branch on, the |
| 1208 | // true/false values to select between, and a branch opcode to use. |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1209 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1210 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1211 | ++It; |
| 1212 | |
| 1213 | // thisMBB: |
| 1214 | // ... |
| 1215 | // TrueVal = ... |
| 1216 | // cmpTY ccX, r1, r2 |
| 1217 | // bCC copy1MBB |
| 1218 | // fallthrough --> copy0MBB |
| 1219 | MachineBasicBlock *thisMBB = BB; |
| 1220 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1221 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1222 | unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue()); |
| 1223 | BuildMI(BB, Opc, 1).addMBB(sinkMBB); |
| 1224 | MachineFunction *F = BB->getParent(); |
| 1225 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1226 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1227 | // Update machine-CFG edges |
| 1228 | BB->addSuccessor(copy0MBB); |
| 1229 | BB->addSuccessor(sinkMBB); |
| 1230 | |
| 1231 | // copy0MBB: |
| 1232 | // %FalseValue = ... |
| 1233 | // # fallthrough to sinkMBB |
| 1234 | BB = copy0MBB; |
| 1235 | |
| 1236 | // Update machine-CFG edges |
| 1237 | BB->addSuccessor(sinkMBB); |
| 1238 | |
| 1239 | // sinkMBB: |
| 1240 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1241 | // ... |
| 1242 | BB = sinkMBB; |
| 1243 | BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg()) |
| 1244 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 1245 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1246 | |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1247 | delete MI; // The pseudo instruction is gone now. |
| 1248 | return BB; |
| 1249 | } |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1250 | |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1251 | case X86::FP_TO_INT16_IN_MEM: |
| 1252 | case X86::FP_TO_INT32_IN_MEM: |
| 1253 | case X86::FP_TO_INT64_IN_MEM: { |
| 1254 | // Change the floating point control register to use "round towards zero" |
| 1255 | // mode when truncating to an integer value. |
| 1256 | MachineFunction *F = BB->getParent(); |
| 1257 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 1258 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 1259 | |
| 1260 | // Load the old value of the high byte of the control word... |
| 1261 | unsigned OldCW = |
| 1262 | F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass); |
| 1263 | addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx); |
| 1264 | |
| 1265 | // Set the high part to be round to zero... |
| 1266 | addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F); |
| 1267 | |
| 1268 | // Reload the modified control word now... |
| 1269 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1270 | |
| 1271 | // Restore the memory image of control word to original value |
| 1272 | addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW); |
| 1273 | |
| 1274 | // Get the X86 opcode to use. |
| 1275 | unsigned Opc; |
| 1276 | switch (MI->getOpcode()) { |
Chris Lattner | 6b2469c | 2006-01-28 10:34:47 +0000 | [diff] [blame] | 1277 | default: assert(0 && "illegal opcode!"); |
Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 1278 | case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break; |
| 1279 | case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break; |
| 1280 | case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break; |
| 1281 | } |
| 1282 | |
| 1283 | X86AddressMode AM; |
| 1284 | MachineOperand &Op = MI->getOperand(0); |
| 1285 | if (Op.isRegister()) { |
| 1286 | AM.BaseType = X86AddressMode::RegBase; |
| 1287 | AM.Base.Reg = Op.getReg(); |
| 1288 | } else { |
| 1289 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 1290 | AM.Base.FrameIndex = Op.getFrameIndex(); |
| 1291 | } |
| 1292 | Op = MI->getOperand(1); |
| 1293 | if (Op.isImmediate()) |
| 1294 | AM.Scale = Op.getImmedValue(); |
| 1295 | Op = MI->getOperand(2); |
| 1296 | if (Op.isImmediate()) |
| 1297 | AM.IndexReg = Op.getImmedValue(); |
| 1298 | Op = MI->getOperand(3); |
| 1299 | if (Op.isGlobalAddress()) { |
| 1300 | AM.GV = Op.getGlobal(); |
| 1301 | } else { |
| 1302 | AM.Disp = Op.getImmedValue(); |
| 1303 | } |
| 1304 | addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg()); |
| 1305 | |
| 1306 | // Reload the original control word now. |
| 1307 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1308 | |
| 1309 | delete MI; // The pseudo instruction is gone now. |
| 1310 | return BB; |
| 1311 | } |
| 1312 | } |
Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | //===----------------------------------------------------------------------===// |
| 1317 | // X86 Custom Lowering Hooks |
| 1318 | //===----------------------------------------------------------------------===// |
| 1319 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1320 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 1321 | /// |
| 1322 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 1323 | switch (Op.getOpcode()) { |
| 1324 | default: assert(0 && "Should not custom lower this!"); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1325 | case ISD::SHL_PARTS: |
| 1326 | case ISD::SRA_PARTS: |
| 1327 | case ISD::SRL_PARTS: { |
| 1328 | assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 && |
| 1329 | "Not an i64 shift!"); |
| 1330 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| 1331 | SDOperand ShOpLo = Op.getOperand(0); |
| 1332 | SDOperand ShOpHi = Op.getOperand(1); |
| 1333 | SDOperand ShAmt = Op.getOperand(2); |
| 1334 | SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, |
Evan Cheng | 99fa0a1 | 2006-01-18 09:26:46 +0000 | [diff] [blame] | 1335 | DAG.getConstant(31, MVT::i8)) |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1336 | : DAG.getConstant(0, MVT::i32); |
| 1337 | |
| 1338 | SDOperand Tmp2, Tmp3; |
| 1339 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 1340 | Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt); |
| 1341 | Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt); |
| 1342 | } else { |
| 1343 | Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt); |
Evan Cheng | b7b5706 | 2006-01-19 01:46:14 +0000 | [diff] [blame] | 1344 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag, |
| 1348 | ShAmt, DAG.getConstant(32, MVT::i8)); |
| 1349 | |
| 1350 | SDOperand Hi, Lo; |
Evan Cheng | 82a24b9 | 2006-01-09 20:49:21 +0000 | [diff] [blame] | 1351 | SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1352 | |
| 1353 | std::vector<MVT::ValueType> Tys; |
| 1354 | Tys.push_back(MVT::i32); |
| 1355 | Tys.push_back(MVT::Flag); |
| 1356 | std::vector<SDOperand> Ops; |
| 1357 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| 1358 | Ops.push_back(Tmp2); |
| 1359 | Ops.push_back(Tmp3); |
| 1360 | Ops.push_back(CC); |
| 1361 | Ops.push_back(InFlag); |
| 1362 | Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 1363 | InFlag = Hi.getValue(1); |
| 1364 | |
| 1365 | Ops.clear(); |
| 1366 | Ops.push_back(Tmp3); |
| 1367 | Ops.push_back(Tmp1); |
| 1368 | Ops.push_back(CC); |
| 1369 | Ops.push_back(InFlag); |
| 1370 | Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 1371 | } else { |
| 1372 | Ops.push_back(Tmp2); |
| 1373 | Ops.push_back(Tmp3); |
| 1374 | Ops.push_back(CC); |
Evan Cheng | 910cd3c | 2006-01-09 22:29:54 +0000 | [diff] [blame] | 1375 | Ops.push_back(InFlag); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1376 | Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 1377 | InFlag = Lo.getValue(1); |
| 1378 | |
| 1379 | Ops.clear(); |
| 1380 | Ops.push_back(Tmp3); |
| 1381 | Ops.push_back(Tmp1); |
| 1382 | Ops.push_back(CC); |
| 1383 | Ops.push_back(InFlag); |
| 1384 | Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops); |
| 1385 | } |
| 1386 | |
| 1387 | Tys.clear(); |
| 1388 | Tys.push_back(MVT::i32); |
| 1389 | Tys.push_back(MVT::i32); |
| 1390 | Ops.clear(); |
| 1391 | Ops.push_back(Lo); |
| 1392 | Ops.push_back(Hi); |
| 1393 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops); |
| 1394 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1395 | case ISD::SINT_TO_FP: { |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 1396 | assert(Op.getOperand(0).getValueType() <= MVT::i64 && |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1397 | Op.getOperand(0).getValueType() >= MVT::i16 && |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1398 | "Unknown SINT_TO_FP to lower!"); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1399 | |
| 1400 | SDOperand Result; |
| 1401 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 1402 | unsigned Size = MVT::getSizeInBits(SrcVT)/8; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1403 | MachineFunction &MF = DAG.getMachineFunction(); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1404 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1405 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1406 | SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other, |
| 1407 | DAG.getEntryNode(), Op.getOperand(0), |
| 1408 | StackSlot, DAG.getSrcValue(NULL)); |
| 1409 | |
| 1410 | // Build the FILD |
| 1411 | std::vector<MVT::ValueType> Tys; |
| 1412 | Tys.push_back(MVT::f64); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1413 | Tys.push_back(MVT::Other); |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 1414 | if (X86ScalarSSE) Tys.push_back(MVT::Flag); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1415 | std::vector<SDOperand> Ops; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1416 | Ops.push_back(Chain); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1417 | Ops.push_back(StackSlot); |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1418 | Ops.push_back(DAG.getValueType(SrcVT)); |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 1419 | Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD, |
| 1420 | Tys, Ops); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1421 | |
| 1422 | if (X86ScalarSSE) { |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1423 | Chain = Result.getValue(1); |
| 1424 | SDOperand InFlag = Result.getValue(2); |
| 1425 | |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 1426 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1427 | // shouldn't be necessary except that RFP cannot be live across |
| 1428 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| 1429 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1430 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 1431 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1432 | std::vector<MVT::ValueType> Tys; |
| 1433 | Tys.push_back(MVT::Other); |
| 1434 | std::vector<SDOperand> Ops; |
| 1435 | Ops.push_back(Chain); |
| 1436 | Ops.push_back(Result); |
| 1437 | Ops.push_back(StackSlot); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 1438 | Ops.push_back(DAG.getValueType(Op.getValueType())); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1439 | Ops.push_back(InFlag); |
| 1440 | Chain = DAG.getNode(X86ISD::FST, Tys, Ops); |
| 1441 | Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, |
| 1442 | DAG.getSrcValue(NULL)); |
| 1443 | } |
| 1444 | |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 1445 | return Result; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1446 | } |
| 1447 | case ISD::FP_TO_SINT: { |
| 1448 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1449 | "Unknown FP_TO_SINT to lower!"); |
| 1450 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 1451 | // stack slot. |
| 1452 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1453 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 1454 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 1455 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1456 | |
| 1457 | unsigned Opc; |
| 1458 | switch (Op.getValueType()) { |
| 1459 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 1460 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 1461 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 1462 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
| 1463 | } |
| 1464 | |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1465 | SDOperand Chain = DAG.getEntryNode(); |
| 1466 | SDOperand Value = Op.getOperand(0); |
| 1467 | if (X86ScalarSSE) { |
| 1468 | assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
| 1469 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, |
| 1470 | DAG.getSrcValue(0)); |
| 1471 | std::vector<MVT::ValueType> Tys; |
| 1472 | Tys.push_back(MVT::f64); |
| 1473 | Tys.push_back(MVT::Other); |
| 1474 | std::vector<SDOperand> Ops; |
| 1475 | Ops.push_back(Chain); |
| 1476 | Ops.push_back(StackSlot); |
Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 1477 | Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType())); |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1478 | Value = DAG.getNode(X86ISD::FLD, Tys, Ops); |
| 1479 | Chain = Value.getValue(1); |
| 1480 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 1481 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 1482 | } |
| 1483 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1484 | // Build the FP_TO_INT*_IN_MEM |
| 1485 | std::vector<SDOperand> Ops; |
Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 1486 | Ops.push_back(Chain); |
| 1487 | Ops.push_back(Value); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1488 | Ops.push_back(StackSlot); |
| 1489 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops); |
| 1490 | |
| 1491 | // Load the result. |
| 1492 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, |
| 1493 | DAG.getSrcValue(NULL)); |
| 1494 | } |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 1495 | case ISD::READCYCLECOUNTER: { |
Chris Lattner | 81363c3 | 2005-11-20 22:01:40 +0000 | [diff] [blame] | 1496 | std::vector<MVT::ValueType> Tys; |
| 1497 | Tys.push_back(MVT::Other); |
| 1498 | Tys.push_back(MVT::Flag); |
| 1499 | std::vector<SDOperand> Ops; |
| 1500 | Ops.push_back(Op.getOperand(0)); |
| 1501 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops); |
Chris Lattner | 81f803d | 2005-11-20 22:57:19 +0000 | [diff] [blame] | 1502 | Ops.clear(); |
| 1503 | Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1))); |
| 1504 | Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, |
| 1505 | MVT::i32, Ops[0].getValue(2))); |
| 1506 | Ops.push_back(Ops[1].getValue(1)); |
| 1507 | Tys[0] = Tys[1] = MVT::i32; |
| 1508 | Tys.push_back(MVT::Other); |
| 1509 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops); |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 1510 | } |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 1511 | case ISD::FABS: { |
| 1512 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 1513 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 1514 | std::vector<Constant*> CV; |
| 1515 | if (VT == MVT::f64) { |
| 1516 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)))); |
| 1517 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1518 | } else { |
| 1519 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)))); |
| 1520 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1521 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1522 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1523 | } |
| 1524 | Constant *CS = ConstantStruct::get(CV); |
| 1525 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 1526 | SDOperand Mask |
| 1527 | = DAG.getNode(X86ISD::LOAD_PACK, |
| 1528 | VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 1529 | return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask); |
| 1530 | } |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 1531 | case ISD::FNEG: { |
| 1532 | MVT::ValueType VT = Op.getValueType(); |
| 1533 | const Type *OpNTy = MVT::getTypeForValueType(VT); |
| 1534 | std::vector<Constant*> CV; |
| 1535 | if (VT == MVT::f64) { |
| 1536 | CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63))); |
| 1537 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1538 | } else { |
| 1539 | CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31))); |
| 1540 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1541 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1542 | CV.push_back(ConstantFP::get(OpNTy, 0.0)); |
| 1543 | } |
| 1544 | Constant *CS = ConstantStruct::get(CV); |
| 1545 | SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4); |
| 1546 | SDOperand Mask |
| 1547 | = DAG.getNode(X86ISD::LOAD_PACK, |
| 1548 | VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL)); |
| 1549 | return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask); |
| 1550 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1551 | case ISD::SETCC: { |
| 1552 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1553 | SDOperand Cond; |
| 1554 | SDOperand CC = Op.getOperand(2); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1555 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| 1556 | bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType()); |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1557 | bool Flip; |
| 1558 | unsigned X86CC; |
| 1559 | if (translateX86CC(CC, isFP, X86CC, Flip)) { |
| 1560 | if (Flip) |
| 1561 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1562 | Op.getOperand(1), Op.getOperand(0)); |
| 1563 | else |
| 1564 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1565 | Op.getOperand(0), Op.getOperand(1)); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1566 | return DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 1567 | DAG.getConstant(X86CC, MVT::i8), Cond); |
| 1568 | } else { |
| 1569 | assert(isFP && "Illegal integer SetCC!"); |
| 1570 | |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1571 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1572 | Op.getOperand(0), Op.getOperand(1)); |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1573 | std::vector<MVT::ValueType> Tys; |
| 1574 | std::vector<SDOperand> Ops; |
| 1575 | switch (SetCCOpcode) { |
| 1576 | default: assert(false && "Illegal floating point SetCC!"); |
| 1577 | case ISD::SETOEQ: { // !PF & ZF |
| 1578 | Tys.push_back(MVT::i8); |
| 1579 | Tys.push_back(MVT::Flag); |
| 1580 | Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8)); |
| 1581 | Ops.push_back(Cond); |
| 1582 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 1583 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 1584 | DAG.getConstant(X86ISD::COND_E, MVT::i8), |
| 1585 | Tmp1.getValue(1)); |
| 1586 | return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2); |
| 1587 | } |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1588 | case ISD::SETUNE: { // PF | !ZF |
| 1589 | Tys.push_back(MVT::i8); |
| 1590 | Tys.push_back(MVT::Flag); |
| 1591 | Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8)); |
| 1592 | Ops.push_back(Cond); |
| 1593 | SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 1594 | SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8, |
| 1595 | DAG.getConstant(X86ISD::COND_NE, MVT::i8), |
| 1596 | Tmp1.getValue(1)); |
| 1597 | return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1601 | } |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1602 | case ISD::SELECT: { |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1603 | MVT::ValueType VT = Op.getValueType(); |
| 1604 | bool isFP = MVT::isFloatingPoint(VT); |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 1605 | bool isFPStack = isFP && !X86ScalarSSE; |
| 1606 | bool isFPSSE = isFP && X86ScalarSSE; |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1607 | bool addTest = false; |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1608 | SDOperand Op0 = Op.getOperand(0); |
| 1609 | SDOperand Cond, CC; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1610 | if (Op0.getOpcode() == ISD::SETCC) |
| 1611 | Op0 = LowerOperation(Op0, DAG); |
| 1612 | |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1613 | if (Op0.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1614 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 1615 | // (since flag operand cannot be shared). If the X86ISD::SETCC does not |
| 1616 | // have another use it will be eliminated. |
| 1617 | // If the X86ISD::SETCC has more than one use, then it's probably better |
| 1618 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 1619 | // pressure reason). |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 1620 | if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) { |
| 1621 | if (!Op0.hasOneUse()) { |
| 1622 | std::vector<MVT::ValueType> Tys; |
| 1623 | for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i) |
| 1624 | Tys.push_back(Op0.Val->getValueType(i)); |
| 1625 | std::vector<SDOperand> Ops; |
| 1626 | for (unsigned i = 0; i < Op0.getNumOperands(); ++i) |
| 1627 | Ops.push_back(Op0.getOperand(i)); |
| 1628 | Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 1629 | } |
| 1630 | |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1631 | CC = Op0.getOperand(0); |
| 1632 | Cond = Op0.getOperand(1); |
Evan Cheng | 0d718e9 | 2006-01-25 09:05:09 +0000 | [diff] [blame] | 1633 | // Make a copy as flag result cannot be used by more than one. |
| 1634 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1635 | Cond.getOperand(0), Cond.getOperand(1)); |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1636 | addTest = |
Evan Cheng | 80ebe38 | 2006-01-13 01:17:24 +0000 | [diff] [blame] | 1637 | isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended()); |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1638 | } else |
| 1639 | addTest = true; |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1640 | } else |
| 1641 | addTest = true; |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1642 | |
Evan Cheng | 189d01e | 2006-01-13 01:06:49 +0000 | [diff] [blame] | 1643 | if (addTest) { |
Evan Cheng | e90da97 | 2006-01-13 19:51:46 +0000 | [diff] [blame] | 1644 | CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 1645 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1646 | } |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1647 | |
| 1648 | std::vector<MVT::ValueType> Tys; |
| 1649 | Tys.push_back(Op.getValueType()); |
| 1650 | Tys.push_back(MVT::Flag); |
| 1651 | std::vector<SDOperand> Ops; |
Evan Cheng | e90da97 | 2006-01-13 19:51:46 +0000 | [diff] [blame] | 1652 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 1653 | // condition is true. |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1654 | Ops.push_back(Op.getOperand(2)); |
Evan Cheng | e90da97 | 2006-01-13 19:51:46 +0000 | [diff] [blame] | 1655 | Ops.push_back(Op.getOperand(1)); |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 1656 | Ops.push_back(CC); |
| 1657 | Ops.push_back(Cond); |
| 1658 | return DAG.getNode(X86ISD::CMOV, Tys, Ops); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1659 | } |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1660 | case ISD::BRCOND: { |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1661 | bool addTest = false; |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1662 | SDOperand Cond = Op.getOperand(1); |
| 1663 | SDOperand Dest = Op.getOperand(2); |
| 1664 | SDOperand CC; |
Evan Cheng | 6dfa999 | 2006-01-30 23:41:35 +0000 | [diff] [blame] | 1665 | if (Cond.getOpcode() == ISD::SETCC) |
| 1666 | Cond = LowerOperation(Cond, DAG); |
| 1667 | |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1668 | if (Cond.getOpcode() == X86ISD::SETCC) { |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1669 | // If condition flag is set by a X86ISD::CMP, then make a copy of it |
| 1670 | // (since flag operand cannot be shared). If the X86ISD::SETCC does not |
| 1671 | // have another use it will be eliminated. |
| 1672 | // If the X86ISD::SETCC has more than one use, then it's probably better |
| 1673 | // to use a test instead of duplicating the X86ISD::CMP (for register |
| 1674 | // pressure reason). |
Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 1675 | if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) { |
| 1676 | if (!Cond.hasOneUse()) { |
| 1677 | std::vector<MVT::ValueType> Tys; |
| 1678 | for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i) |
| 1679 | Tys.push_back(Cond.Val->getValueType(i)); |
| 1680 | std::vector<SDOperand> Ops; |
| 1681 | for (unsigned i = 0; i < Cond.getNumOperands(); ++i) |
| 1682 | Ops.push_back(Cond.getOperand(i)); |
| 1683 | Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops); |
| 1684 | } |
| 1685 | |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1686 | CC = Cond.getOperand(0); |
Evan Cheng | 0d718e9 | 2006-01-25 09:05:09 +0000 | [diff] [blame] | 1687 | Cond = Cond.getOperand(1); |
| 1688 | // Make a copy as flag result cannot be used by more than one. |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1689 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
Evan Cheng | 0d718e9 | 2006-01-25 09:05:09 +0000 | [diff] [blame] | 1690 | Cond.getOperand(0), Cond.getOperand(1)); |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1691 | } else |
| 1692 | addTest = true; |
Evan Cheng | 1bcee36 | 2006-01-13 01:03:02 +0000 | [diff] [blame] | 1693 | } else |
| 1694 | addTest = true; |
| 1695 | |
| 1696 | if (addTest) { |
Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 1697 | CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8); |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1698 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond); |
| 1699 | } |
| 1700 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
| 1701 | Op.getOperand(0), Op.getOperand(2), CC, Cond); |
| 1702 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1703 | case ISD::MEMSET: { |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1704 | SDOperand InFlag(0, 0); |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1705 | SDOperand Chain = Op.getOperand(0); |
| 1706 | unsigned Align = |
| 1707 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 1708 | if (Align == 0) Align = 1; |
| 1709 | |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 1710 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 1711 | // If not DWORD aligned, call memset if size is less than the threshold. |
| 1712 | // It knows how to align to the right boundary first. |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1713 | if ((Align & 3) != 0 || |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1714 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 1715 | MVT::ValueType IntPtr = getPointerTy(); |
| 1716 | const Type *IntPtrTy = getTargetData().getIntPtrType(); |
| 1717 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1718 | Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy)); |
| 1719 | // Extend the ubyte argument to be an int value for the call. |
| 1720 | SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2)); |
| 1721 | Args.push_back(std::make_pair(Val, IntPtrTy)); |
| 1722 | Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy)); |
| 1723 | std::pair<SDOperand,SDOperand> CallResult = |
| 1724 | LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false, |
| 1725 | DAG.getExternalSymbol("memset", IntPtr), Args, DAG); |
| 1726 | return CallResult.second; |
| 1727 | } |
| 1728 | |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1729 | MVT::ValueType AVT; |
| 1730 | SDOperand Count; |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1731 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2)); |
| 1732 | unsigned BytesLeft = 0; |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1733 | bool TwoRepStos = false; |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1734 | if (ValC) { |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1735 | unsigned ValReg; |
| 1736 | unsigned Val = ValC->getValue() & 255; |
| 1737 | |
| 1738 | // If the value is a constant, then we can potentially use larger sets. |
| 1739 | switch (Align & 3) { |
| 1740 | case 2: // WORD aligned |
| 1741 | AVT = MVT::i16; |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1742 | Count = DAG.getConstant(I->getValue() / 2, MVT::i32); |
| 1743 | BytesLeft = I->getValue() % 2; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1744 | Val = (Val << 8) | Val; |
| 1745 | ValReg = X86::AX; |
| 1746 | break; |
| 1747 | case 0: // DWORD aligned |
| 1748 | AVT = MVT::i32; |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1749 | if (I) { |
| 1750 | Count = DAG.getConstant(I->getValue() / 4, MVT::i32); |
| 1751 | BytesLeft = I->getValue() % 4; |
| 1752 | } else { |
| 1753 | Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3), |
| 1754 | DAG.getConstant(2, MVT::i8)); |
| 1755 | TwoRepStos = true; |
| 1756 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1757 | Val = (Val << 8) | Val; |
| 1758 | Val = (Val << 16) | Val; |
| 1759 | ValReg = X86::EAX; |
| 1760 | break; |
| 1761 | default: // Byte aligned |
| 1762 | AVT = MVT::i8; |
| 1763 | Count = Op.getOperand(3); |
| 1764 | ValReg = X86::AL; |
| 1765 | break; |
| 1766 | } |
| 1767 | |
| 1768 | Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT), |
| 1769 | InFlag); |
| 1770 | InFlag = Chain.getValue(1); |
| 1771 | } else { |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 1772 | AVT = MVT::i8; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1773 | Count = Op.getOperand(3); |
| 1774 | Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag); |
| 1775 | InFlag = Chain.getValue(1); |
| 1776 | } |
| 1777 | |
| 1778 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag); |
| 1779 | InFlag = Chain.getValue(1); |
| 1780 | Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag); |
| 1781 | InFlag = Chain.getValue(1); |
| 1782 | |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1783 | std::vector<MVT::ValueType> Tys; |
| 1784 | Tys.push_back(MVT::Other); |
| 1785 | Tys.push_back(MVT::Flag); |
| 1786 | std::vector<SDOperand> Ops; |
| 1787 | Ops.push_back(Chain); |
| 1788 | Ops.push_back(DAG.getValueType(AVT)); |
| 1789 | Ops.push_back(InFlag); |
| 1790 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops); |
| 1791 | |
| 1792 | if (TwoRepStos) { |
| 1793 | InFlag = Chain.getValue(1); |
| 1794 | Count = Op.getOperand(3); |
| 1795 | MVT::ValueType CVT = Count.getValueType(); |
| 1796 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
| 1797 | DAG.getConstant(3, CVT)); |
| 1798 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag); |
| 1799 | InFlag = Chain.getValue(1); |
| 1800 | Tys.clear(); |
| 1801 | Tys.push_back(MVT::Other); |
| 1802 | Tys.push_back(MVT::Flag); |
| 1803 | Ops.clear(); |
| 1804 | Ops.push_back(Chain); |
| 1805 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 1806 | Ops.push_back(InFlag); |
| 1807 | Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops); |
| 1808 | } else if (BytesLeft) { |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1809 | // Issue stores for the last 1 - 3 bytes. |
| 1810 | SDOperand Value; |
| 1811 | unsigned Val = ValC->getValue() & 255; |
| 1812 | unsigned Offset = I->getValue() - BytesLeft; |
| 1813 | SDOperand DstAddr = Op.getOperand(1); |
| 1814 | MVT::ValueType AddrVT = DstAddr.getValueType(); |
| 1815 | if (BytesLeft >= 2) { |
| 1816 | Value = DAG.getConstant((Val << 8) | Val, MVT::i16); |
| 1817 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 1818 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 1819 | DAG.getConstant(Offset, AddrVT)), |
| 1820 | DAG.getSrcValue(NULL)); |
| 1821 | BytesLeft -= 2; |
| 1822 | Offset += 2; |
| 1823 | } |
| 1824 | |
| 1825 | if (BytesLeft == 1) { |
| 1826 | Value = DAG.getConstant(Val, MVT::i8); |
| 1827 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 1828 | DAG.getNode(ISD::ADD, AddrVT, DstAddr, |
| 1829 | DAG.getConstant(Offset, AddrVT)), |
| 1830 | DAG.getSrcValue(NULL)); |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | return Chain; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1835 | } |
| 1836 | case ISD::MEMCPY: { |
| 1837 | SDOperand Chain = Op.getOperand(0); |
| 1838 | unsigned Align = |
| 1839 | (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue(); |
| 1840 | if (Align == 0) Align = 1; |
| 1841 | |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 1842 | ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 1843 | // If not DWORD aligned, call memcpy if size is less than the threshold. |
| 1844 | // It knows how to align to the right boundary first. |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1845 | if ((Align & 3) != 0 || |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1846 | (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) { |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 1847 | MVT::ValueType IntPtr = getPointerTy(); |
| 1848 | const Type *IntPtrTy = getTargetData().getIntPtrType(); |
| 1849 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1850 | Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy)); |
| 1851 | Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy)); |
| 1852 | Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy)); |
| 1853 | std::pair<SDOperand,SDOperand> CallResult = |
| 1854 | LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false, |
| 1855 | DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG); |
| 1856 | return CallResult.second; |
| 1857 | } |
| 1858 | |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1859 | MVT::ValueType AVT; |
| 1860 | SDOperand Count; |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1861 | unsigned BytesLeft = 0; |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1862 | bool TwoRepMovs = false; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1863 | switch (Align & 3) { |
| 1864 | case 2: // WORD aligned |
| 1865 | AVT = MVT::i16; |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1866 | Count = DAG.getConstant(I->getValue() / 2, MVT::i32); |
| 1867 | BytesLeft = I->getValue() % 2; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | case 0: // DWORD aligned |
| 1870 | AVT = MVT::i32; |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1871 | if (I) { |
| 1872 | Count = DAG.getConstant(I->getValue() / 4, MVT::i32); |
| 1873 | BytesLeft = I->getValue() % 4; |
| 1874 | } else { |
| 1875 | Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3), |
| 1876 | DAG.getConstant(2, MVT::i8)); |
| 1877 | TwoRepMovs = true; |
| 1878 | } |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1879 | break; |
| 1880 | default: // Byte aligned |
| 1881 | AVT = MVT::i8; |
| 1882 | Count = Op.getOperand(3); |
| 1883 | break; |
| 1884 | } |
| 1885 | |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1886 | SDOperand InFlag(0, 0); |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1887 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag); |
| 1888 | InFlag = Chain.getValue(1); |
| 1889 | Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag); |
| 1890 | InFlag = Chain.getValue(1); |
| 1891 | Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag); |
| 1892 | InFlag = Chain.getValue(1); |
| 1893 | |
Evan Cheng | ff90992 | 2006-03-07 23:29:39 +0000 | [diff] [blame^] | 1894 | std::vector<MVT::ValueType> Tys; |
| 1895 | Tys.push_back(MVT::Other); |
| 1896 | Tys.push_back(MVT::Flag); |
| 1897 | std::vector<SDOperand> Ops; |
| 1898 | Ops.push_back(Chain); |
| 1899 | Ops.push_back(DAG.getValueType(AVT)); |
| 1900 | Ops.push_back(InFlag); |
| 1901 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops); |
| 1902 | |
| 1903 | if (TwoRepMovs) { |
| 1904 | InFlag = Chain.getValue(1); |
| 1905 | Count = Op.getOperand(3); |
| 1906 | MVT::ValueType CVT = Count.getValueType(); |
| 1907 | SDOperand Left = DAG.getNode(ISD::AND, CVT, Count, |
| 1908 | DAG.getConstant(3, CVT)); |
| 1909 | Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag); |
| 1910 | InFlag = Chain.getValue(1); |
| 1911 | Tys.clear(); |
| 1912 | Tys.push_back(MVT::Other); |
| 1913 | Tys.push_back(MVT::Flag); |
| 1914 | Ops.clear(); |
| 1915 | Ops.push_back(Chain); |
| 1916 | Ops.push_back(DAG.getValueType(MVT::i8)); |
| 1917 | Ops.push_back(InFlag); |
| 1918 | Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops); |
| 1919 | } else if (BytesLeft) { |
Evan Cheng | 62bec2c | 2006-03-04 02:48:56 +0000 | [diff] [blame] | 1920 | // Issue loads and stores for the last 1 - 3 bytes. |
| 1921 | unsigned Offset = I->getValue() - BytesLeft; |
| 1922 | SDOperand DstAddr = Op.getOperand(1); |
| 1923 | MVT::ValueType DstVT = DstAddr.getValueType(); |
| 1924 | SDOperand SrcAddr = Op.getOperand(2); |
| 1925 | MVT::ValueType SrcVT = SrcAddr.getValueType(); |
| 1926 | SDOperand Value; |
| 1927 | if (BytesLeft >= 2) { |
| 1928 | Value = DAG.getLoad(MVT::i16, Chain, |
| 1929 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 1930 | DAG.getConstant(Offset, SrcVT)), |
| 1931 | DAG.getSrcValue(NULL)); |
| 1932 | Chain = Value.getValue(1); |
| 1933 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 1934 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 1935 | DAG.getConstant(Offset, DstVT)), |
| 1936 | DAG.getSrcValue(NULL)); |
| 1937 | BytesLeft -= 2; |
| 1938 | Offset += 2; |
| 1939 | } |
| 1940 | |
| 1941 | if (BytesLeft == 1) { |
| 1942 | Value = DAG.getLoad(MVT::i8, Chain, |
| 1943 | DAG.getNode(ISD::ADD, SrcVT, SrcAddr, |
| 1944 | DAG.getConstant(Offset, SrcVT)), |
| 1945 | DAG.getSrcValue(NULL)); |
| 1946 | Chain = Value.getValue(1); |
| 1947 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 1948 | DAG.getNode(ISD::ADD, DstVT, DstAddr, |
| 1949 | DAG.getConstant(Offset, DstVT)), |
| 1950 | DAG.getSrcValue(NULL)); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | return Chain; |
Evan Cheng | 67f92a7 | 2006-01-11 22:15:48 +0000 | [diff] [blame] | 1955 | } |
Evan Cheng | bbbb2fb | 2006-02-25 09:55:19 +0000 | [diff] [blame] | 1956 | |
| 1957 | // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their |
| 1958 | // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 1959 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 1960 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 1961 | // be used to form addressing mode. These wrapped nodes will be selected |
| 1962 | // into MOV32ri. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1963 | case ISD::ConstantPool: { |
| 1964 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1965 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
| 1966 | DAG.getTargetConstantPool(CP->get(), getPointerTy(), |
| 1967 | CP->getAlignment())); |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 1968 | if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) { |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1969 | // With PIC, the address is actually $g + Offset. |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 1970 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1971 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 1972 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result); |
| 1973 | } |
| 1974 | |
| 1975 | return Result; |
| 1976 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 1977 | case ISD::GlobalAddress: { |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1978 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 1979 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
| 1980 | DAG.getTargetGlobalAddress(GV, getPointerTy())); |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 1981 | if (getTargetMachine(). |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1982 | getSubtarget<X86Subtarget>().isTargetDarwin()) { |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1983 | // With PIC, the address is actually $g + Offset. |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 1984 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 1985 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 1986 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1987 | |
| 1988 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 1989 | // the value at address GV, not the value of GV itself. This means that |
| 1990 | // the GlobalAddress must be in the base or index register of the address, |
| 1991 | // not the GV offset field. |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 1992 | if (getTargetMachine().getRelocationModel() != Reloc::Static && |
| 1993 | (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || |
| 1994 | (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))) |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 1995 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 1996 | Result, DAG.getSrcValue(NULL)); |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 1997 | } |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1998 | |
Evan Cheng | 002fe9b | 2006-01-12 07:56:47 +0000 | [diff] [blame] | 1999 | return Result; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2000 | } |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 2001 | case ISD::ExternalSymbol: { |
| 2002 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| 2003 | SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), |
| 2004 | DAG.getTargetExternalSymbol(Sym, getPointerTy())); |
| 2005 | if (getTargetMachine(). |
| 2006 | getSubtarget<X86Subtarget>().isTargetDarwin()) { |
| 2007 | // With PIC, the address is actually $g + Offset. |
| 2008 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) |
| 2009 | Result = DAG.getNode(ISD::ADD, getPointerTy(), |
| 2010 | DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result); |
| 2011 | } |
| 2012 | |
| 2013 | return Result; |
| 2014 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2015 | case ISD::VASTART: { |
| 2016 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 2017 | // memory location argument. |
| 2018 | // FIXME: Replace MVT::i32 with PointerTy |
| 2019 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 2020 | return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, |
| 2021 | Op.getOperand(1), Op.getOperand(2)); |
| 2022 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 2023 | case ISD::RET: { |
| 2024 | SDOperand Copy; |
| 2025 | |
| 2026 | switch(Op.getNumOperands()) { |
| 2027 | default: |
| 2028 | assert(0 && "Do not know how to return this many arguments!"); |
| 2029 | abort(); |
| 2030 | case 1: |
| 2031 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0), |
| 2032 | DAG.getConstant(getBytesToPopOnReturn(), MVT::i16)); |
| 2033 | case 2: { |
| 2034 | MVT::ValueType ArgVT = Op.getOperand(1).getValueType(); |
| 2035 | if (MVT::isInteger(ArgVT)) |
| 2036 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1), |
| 2037 | SDOperand()); |
| 2038 | else if (!X86ScalarSSE) { |
| 2039 | std::vector<MVT::ValueType> Tys; |
| 2040 | Tys.push_back(MVT::Other); |
| 2041 | Tys.push_back(MVT::Flag); |
| 2042 | std::vector<SDOperand> Ops; |
| 2043 | Ops.push_back(Op.getOperand(0)); |
| 2044 | Ops.push_back(Op.getOperand(1)); |
| 2045 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 2046 | } else { |
Evan Cheng | 0d084c9 | 2006-02-01 00:20:21 +0000 | [diff] [blame] | 2047 | SDOperand MemLoc; |
| 2048 | SDOperand Chain = Op.getOperand(0); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 2049 | SDOperand Value = Op.getOperand(1); |
| 2050 | |
Evan Cheng | 760df29 | 2006-02-01 01:19:32 +0000 | [diff] [blame] | 2051 | if (Value.getOpcode() == ISD::LOAD && |
| 2052 | (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) { |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 2053 | Chain = Value.getOperand(0); |
| 2054 | MemLoc = Value.getOperand(1); |
| 2055 | } else { |
| 2056 | // Spill the value to memory and reload it into top of stack. |
| 2057 | unsigned Size = MVT::getSizeInBits(ArgVT)/8; |
| 2058 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2059 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 2060 | MemLoc = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 2061 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), |
| 2062 | Value, MemLoc, DAG.getSrcValue(0)); |
| 2063 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 2064 | std::vector<MVT::ValueType> Tys; |
| 2065 | Tys.push_back(MVT::f64); |
| 2066 | Tys.push_back(MVT::Other); |
| 2067 | std::vector<SDOperand> Ops; |
| 2068 | Ops.push_back(Chain); |
Evan Cheng | 0e8671b | 2006-01-31 23:19:54 +0000 | [diff] [blame] | 2069 | Ops.push_back(MemLoc); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 2070 | Ops.push_back(DAG.getValueType(ArgVT)); |
| 2071 | Copy = DAG.getNode(X86ISD::FLD, Tys, Ops); |
| 2072 | Tys.clear(); |
| 2073 | Tys.push_back(MVT::Other); |
| 2074 | Tys.push_back(MVT::Flag); |
| 2075 | Ops.clear(); |
| 2076 | Ops.push_back(Copy.getValue(1)); |
| 2077 | Ops.push_back(Copy); |
| 2078 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 2079 | } |
| 2080 | break; |
| 2081 | } |
| 2082 | case 3: |
| 2083 | Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), |
| 2084 | SDOperand()); |
| 2085 | Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1)); |
| 2086 | break; |
| 2087 | } |
| 2088 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, |
| 2089 | Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16), |
| 2090 | Copy.getValue(1)); |
| 2091 | } |
Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 2092 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2093 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2094 | |
| 2095 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 2096 | switch (Opcode) { |
| 2097 | default: return NULL; |
Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 2098 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 2099 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 2100 | case X86ISD::FAND: return "X86ISD::FAND"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 2101 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 2102 | case X86ISD::FILD: return "X86ISD::FILD"; |
Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 2103 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2104 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 2105 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 2106 | 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] | 2107 | case X86ISD::FLD: return "X86ISD::FLD"; |
Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 2108 | case X86ISD::FST: return "X86ISD::FST"; |
| 2109 | case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 2110 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2111 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 2112 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 2113 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 2114 | case X86ISD::CMP: return "X86ISD::CMP"; |
| 2115 | case X86ISD::TEST: return "X86ISD::TEST"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 2116 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2117 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 2118 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 2119 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 2120 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 2121 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 2122 | case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK"; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 2123 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 2124 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2125 | } |
| 2126 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 2127 | |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 2128 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op, |
| 2129 | uint64_t Mask, |
| 2130 | uint64_t &KnownZero, |
| 2131 | uint64_t &KnownOne, |
| 2132 | unsigned Depth) const { |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 2133 | |
| 2134 | unsigned Opc = Op.getOpcode(); |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 2135 | KnownZero = KnownOne = 0; // Don't know anything. |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 2136 | |
| 2137 | switch (Opc) { |
| 2138 | default: |
| 2139 | assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node"); |
| 2140 | break; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 2141 | case X86ISD::SETCC: |
| 2142 | KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL); |
| 2143 | break; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 2144 | } |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 2145 | } |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 2146 | |
| 2147 | std::vector<unsigned> X86TargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2148 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 2149 | MVT::ValueType VT) const { |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 2150 | if (Constraint.size() == 1) { |
| 2151 | // FIXME: not handling fp-stack yet! |
| 2152 | // FIXME: not handling MMX registers yet ('y' constraint). |
| 2153 | switch (Constraint[0]) { // GCC X86 Constraint Letters |
| 2154 | default: break; // Unknown constriant letter |
| 2155 | case 'r': // GENERAL_REGS |
| 2156 | case 'R': // LEGACY_REGS |
| 2157 | return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, |
| 2158 | X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0); |
| 2159 | case 'l': // INDEX_REGS |
| 2160 | return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, |
| 2161 | X86::ESI, X86::EDI, X86::EBP, 0); |
| 2162 | case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode) |
| 2163 | case 'Q': // Q_REGS |
| 2164 | return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0); |
| 2165 | case 'x': // SSE_REGS if SSE1 allowed |
| 2166 | if (Subtarget->hasSSE1()) |
| 2167 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 2168 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 2169 | 0); |
| 2170 | return std::vector<unsigned>(); |
| 2171 | case 'Y': // SSE_REGS if SSE2 allowed |
| 2172 | if (Subtarget->hasSSE2()) |
| 2173 | return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 2174 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7, |
| 2175 | 0); |
| 2176 | return std::vector<unsigned>(); |
| 2177 | } |
| 2178 | } |
| 2179 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2180 | return std::vector<unsigned>(); |
Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 2181 | } |