Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1 | //===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===// |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for X86. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86.h" |
| 15 | #include "X86InstrBuilder.h" |
| 16 | #include "X86RegisterInfo.h" |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 17 | #include "X86Subtarget.h" |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Chris Lattner | 6972c31 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/Instructions.h" |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Chris Lattner | 6972c31 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetLowering.h" |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | db68d39 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 6972c31 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CFG.h" |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
| 34 | #include "llvm/ADT/Statistic.h" |
| 35 | #include <set> |
Jeff Cohen | 407aa01 | 2005-01-12 04:29:05 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 39 | // FIXME: temporary. |
| 40 | #include "llvm/Support/CommandLine.h" |
| 41 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 42 | cl::desc("Enable fastcc on X86")); |
| 43 | |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | // X86 Specific DAG Nodes |
| 46 | namespace X86ISD { |
| 47 | enum NodeType { |
| 48 | // Start the numbering where the builtin ops leave off. |
| 49 | FIRST_NUMBER = ISD::BUILTIN_OP_END, |
| 50 | |
| 51 | /// FILD64m - This instruction implements SINT_TO_FP with a |
| 52 | /// 64-bit source in memory and a FP reg result. This corresponds to |
| 53 | /// the X86::FILD64m instruction. It has two inputs (token chain and |
| 54 | /// address) and two outputs (FP value and token chain). |
| 55 | FILD64m, |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 57 | /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the |
| 58 | /// integer destination in memory and a FP reg source. This corresponds |
| 59 | /// to the X86::FIST*m instructions and the rounding mode change stuff. It |
| 60 | /// has two inputs (token chain and address) and two outputs (FP value and |
| 61 | /// token chain). |
| 62 | FP_TO_INT64_IN_MEM, |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 64 | /// CALL/TAILCALL - These operations represent an abstract X86 call |
| 65 | /// instruction, which includes a bunch of information. In particular the |
| 66 | /// operands of these node are: |
| 67 | /// |
| 68 | /// #0 - The incoming token chain |
| 69 | /// #1 - The callee |
| 70 | /// #2 - The number of arg bytes the caller pushes on the stack. |
| 71 | /// #3 - The number of arg bytes the callee pops off the stack. |
| 72 | /// #4 - The value to pass in AL/AX/EAX (optional) |
| 73 | /// #5 - The value to pass in DL/DX/EDX (optional) |
| 74 | /// |
| 75 | /// The result values of these nodes are: |
| 76 | /// |
| 77 | /// #0 - The outgoing token chain |
| 78 | /// #1 - The first register result value (optional) |
| 79 | /// #2 - The second register result value (optional) |
| 80 | /// |
| 81 | /// The CALL vs TAILCALL distinction boils down to whether the callee is |
| 82 | /// known not to modify the caller's stack frame, as is standard with |
| 83 | /// LLVM. |
| 84 | CALL, |
| 85 | TAILCALL, |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 86 | }; |
| 87 | } |
| 88 | } |
| 89 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | // X86TargetLowering - X86 Implementation of the TargetLowering interface |
| 92 | namespace { |
| 93 | class X86TargetLowering : public TargetLowering { |
| 94 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 95 | int ReturnAddrIndex; // FrameIndex for return slot. |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 96 | int BytesToPopOnReturn; // Number of arg bytes ret should pop. |
| 97 | int BytesCallerReserves; // Number of arg bytes caller makes. |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 98 | public: |
| 99 | X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
| 100 | // Set up the TargetLowering object. |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 5011ff0 | 2005-05-13 22:46:57 +0000 | [diff] [blame] | 102 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 103 | setShiftAmountType(MVT::i8); |
| 104 | setSetCCResultType(MVT::i8); |
Chris Lattner | 38fd970 | 2005-04-07 19:41:46 +0000 | [diff] [blame] | 105 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Chris Lattner | d8d3066 | 2005-01-19 03:36:30 +0000 | [diff] [blame] | 106 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 107 | |
| 108 | // Set up the register classes. |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 109 | // FIXME: Eliminate these two classes when legalize can handle promotions |
| 110 | // well. |
| 111 | addRegisterClass(MVT::i1, X86::R8RegisterClass); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 112 | addRegisterClass(MVT::i8, X86::R8RegisterClass); |
| 113 | addRegisterClass(MVT::i16, X86::R16RegisterClass); |
| 114 | addRegisterClass(MVT::i32, X86::R32RegisterClass); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 116 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 117 | // operation. |
| 118 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 119 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 120 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
| 121 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 122 | |
| 123 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 124 | // this operation. |
| 125 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 126 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 128 | if (!X86ScalarSSE) { |
| 129 | // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64 |
| 130 | // isn't legal. |
| 131 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 132 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 133 | } |
| 134 | |
Chris Lattner | bc85c32 | 2005-07-29 01:00:29 +0000 | [diff] [blame] | 135 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 136 | // conversion. |
| 137 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 138 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 139 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 140 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 141 | |
| 142 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 143 | // this operation. |
| 144 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 145 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 146 | |
Chris Lattner | a3a135a | 2005-04-09 03:22:37 +0000 | [diff] [blame] | 147 | setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand); |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 148 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 149 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 150 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 151 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 152 | setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand); |
| 153 | setOperationAction(ISD::SREM , MVT::f64 , Expand); |
Chris Lattner | 05ad4b8 | 2005-05-11 05:00:34 +0000 | [diff] [blame] | 154 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 155 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 156 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 157 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 158 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 159 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 160 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 161 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Andrew Lenharth | b8e94c3 | 2005-05-04 19:25:37 +0000 | [diff] [blame] | 162 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 6c6a39a | 2005-05-09 20:37:29 +0000 | [diff] [blame] | 164 | setOperationAction(ISD::READIO , MVT::i1 , Expand); |
| 165 | setOperationAction(ISD::READIO , MVT::i8 , Expand); |
| 166 | setOperationAction(ISD::READIO , MVT::i16 , Expand); |
| 167 | setOperationAction(ISD::READIO , MVT::i32 , Expand); |
| 168 | setOperationAction(ISD::WRITEIO , MVT::i1 , Expand); |
| 169 | setOperationAction(ISD::WRITEIO , MVT::i8 , Expand); |
| 170 | setOperationAction(ISD::WRITEIO , MVT::i16 , Expand); |
| 171 | setOperationAction(ISD::WRITEIO , MVT::i32 , Expand); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 172 | |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 173 | // These should be promoted to a larger select which is supported. |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 174 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 175 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 176 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 177 | if (X86ScalarSSE) { |
| 178 | // Set up the FP register classes. |
| 179 | addRegisterClass(MVT::f32, X86::RXMMRegisterClass); |
| 180 | addRegisterClass(MVT::f64, X86::RXMMRegisterClass); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 181 | |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 182 | // SSE has no load+extend ops |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 183 | setOperationAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 184 | setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand); |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 185 | |
| 186 | // SSE has no i16 to fp conversion, only i32 |
| 187 | setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote); |
| 188 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 189 | // We don't support sin/cos/sqrt/fmod |
| 190 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 191 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 192 | setOperationAction(ISD::FABS , MVT::f64, Expand); |
| 193 | setOperationAction(ISD::FNEG , MVT::f64, Expand); |
| 194 | setOperationAction(ISD::SREM , MVT::f64, Expand); |
| 195 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 196 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 197 | setOperationAction(ISD::FABS , MVT::f32, Expand); |
| 198 | setOperationAction(ISD::FNEG , MVT::f32, Expand); |
| 199 | setOperationAction(ISD::SREM , MVT::f32, Expand); |
| 200 | } else { |
| 201 | // Set up the FP register classes. |
| 202 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 203 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 204 | if (!UnsafeFPMath) { |
| 205 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 206 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 207 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 208 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 209 | addLegalFPImmediate(+0.0); // FLD0 |
| 210 | addLegalFPImmediate(+1.0); // FLD1 |
| 211 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 212 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 213 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 214 | computeRegisterProperties(); |
Reid Spencer | d37d854 | 2005-07-19 04:52:44 +0000 | [diff] [blame] | 215 | |
| 216 | maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores |
| 217 | maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores |
| 218 | maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores |
| 219 | allowUnalignedStores = true; // x86 supports it! |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 220 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 221 | |
Chris Lattner | c0e369e | 2005-05-13 21:44:04 +0000 | [diff] [blame] | 222 | // Return the number of bytes that a function should pop when it returns (in |
| 223 | // addition to the space used by the return address). |
| 224 | // |
| 225 | unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; } |
| 226 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 227 | // Return the number of bytes that the caller reserves for arguments passed |
| 228 | // to this function. |
| 229 | unsigned getBytesCallerReserves() const { return BytesCallerReserves; } |
| 230 | |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 231 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 232 | /// |
| 233 | virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG); |
| 234 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 235 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 236 | /// lower the arguments for the specified function, into the specified DAG. |
| 237 | virtual std::vector<SDOperand> |
| 238 | LowerArguments(Function &F, SelectionDAG &DAG); |
| 239 | |
| 240 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 241 | /// actual call. |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 242 | virtual std::pair<SDOperand, SDOperand> |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 243 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC, |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 244 | bool isTailCall, SDOperand Callee, ArgListTy &Args, |
| 245 | SelectionDAG &DAG); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 246 | |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 247 | virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 248 | Value *VAListV, SelectionDAG &DAG); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 249 | virtual std::pair<SDOperand,SDOperand> |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 250 | LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 251 | const Type *ArgTy, SelectionDAG &DAG); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 253 | virtual std::pair<SDOperand, SDOperand> |
| 254 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 255 | SelectionDAG &DAG); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 256 | |
| 257 | SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG); |
| 258 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 259 | private: |
| 260 | // C Calling Convention implementation. |
| 261 | std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG); |
| 262 | std::pair<SDOperand, SDOperand> |
| 263 | LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, |
Chris Lattner | f27e31d | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 264 | bool isTailCall, |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 265 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 267 | // Fast Calling Convention implementation. |
| 268 | std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG); |
| 269 | std::pair<SDOperand, SDOperand> |
Chris Lattner | f27e31d | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 270 | LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall, |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 271 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 272 | }; |
| 273 | } |
| 274 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 275 | std::vector<SDOperand> |
| 276 | X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 277 | if (F.getCallingConv() == CallingConv::Fast && EnableFastCC) |
| 278 | return LowerFastCCArguments(F, DAG); |
| 279 | return LowerCCCArguments(F, DAG); |
| 280 | } |
| 281 | |
| 282 | std::pair<SDOperand, SDOperand> |
| 283 | X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 284 | bool isVarArg, unsigned CallingConv, |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 285 | bool isTailCall, |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 286 | SDOperand Callee, ArgListTy &Args, |
| 287 | SelectionDAG &DAG) { |
| 288 | assert((!isVarArg || CallingConv == CallingConv::C) && |
| 289 | "Only C takes varargs!"); |
| 290 | if (CallingConv == CallingConv::Fast && EnableFastCC) |
Chris Lattner | f27e31d | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 291 | return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG); |
| 292 | return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5011ff0 | 2005-05-13 22:46:57 +0000 | [diff] [blame] | 296 | // C Calling Convention implementation |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 297 | //===----------------------------------------------------------------------===// |
| 298 | |
| 299 | std::vector<SDOperand> |
| 300 | X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 301 | std::vector<SDOperand> ArgValues; |
| 302 | |
Chris Lattner | d8145bc | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 303 | MachineFunction &MF = DAG.getMachineFunction(); |
| 304 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 305 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 306 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 307 | // the stack frame looks like this: |
| 308 | // |
| 309 | // [ESP] -- return address |
| 310 | // [ESP + 4] -- first argument (leftmost lexically) |
| 311 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 312 | // ... |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 313 | // |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 314 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 315 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 316 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 317 | unsigned ArgIncrement = 4; |
| 318 | unsigned ObjSize; |
| 319 | switch (ObjectVT) { |
| 320 | default: assert(0 && "Unhandled argument type!"); |
| 321 | case MVT::i1: |
| 322 | case MVT::i8: ObjSize = 1; break; |
| 323 | case MVT::i16: ObjSize = 2; break; |
| 324 | case MVT::i32: ObjSize = 4; break; |
| 325 | case MVT::i64: ObjSize = ArgIncrement = 8; break; |
| 326 | case MVT::f32: ObjSize = 4; break; |
| 327 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 328 | } |
| 329 | // Create the frame index object for this incoming parameter... |
| 330 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 332 | // Create the SelectionDAG nodes corresponding to a load from this parameter |
| 333 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 334 | |
| 335 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 336 | // dead loads. |
| 337 | SDOperand ArgValue; |
| 338 | if (!I->use_empty()) |
Chris Lattner | daa064d | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 339 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 340 | DAG.getSrcValue(NULL)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 341 | else { |
| 342 | if (MVT::isInteger(ObjectVT)) |
| 343 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 344 | else |
| 345 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 346 | } |
| 347 | ArgValues.push_back(ArgValue); |
| 348 | |
| 349 | ArgOffset += ArgIncrement; // Move on to the next argument... |
| 350 | } |
| 351 | |
| 352 | // If the function takes variable number of arguments, make a frame index for |
| 353 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 354 | if (F.isVarArg()) |
| 355 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
Chris Lattner | c0e369e | 2005-05-13 21:44:04 +0000 | [diff] [blame] | 356 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 357 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 358 | BytesCallerReserves = ArgOffset; |
Chris Lattner | b59006c | 2005-04-09 15:23:56 +0000 | [diff] [blame] | 359 | |
| 360 | // Finally, inform the code generator which regs we return values in. |
| 361 | switch (getValueType(F.getReturnType())) { |
| 362 | default: assert(0 && "Unknown type!"); |
| 363 | case MVT::isVoid: break; |
| 364 | case MVT::i1: |
| 365 | case MVT::i8: |
| 366 | case MVT::i16: |
| 367 | case MVT::i32: |
| 368 | MF.addLiveOut(X86::EAX); |
| 369 | break; |
| 370 | case MVT::i64: |
| 371 | MF.addLiveOut(X86::EAX); |
| 372 | MF.addLiveOut(X86::EDX); |
| 373 | break; |
| 374 | case MVT::f32: |
| 375 | case MVT::f64: |
| 376 | MF.addLiveOut(X86::ST0); |
| 377 | break; |
| 378 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 379 | return ArgValues; |
| 380 | } |
| 381 | |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 382 | std::pair<SDOperand, SDOperand> |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 383 | X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, |
Chris Lattner | f27e31d | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 384 | bool isVarArg, bool isTailCall, |
| 385 | SDOperand Callee, ArgListTy &Args, |
| 386 | SelectionDAG &DAG) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 387 | // Count how many bytes are to be pushed on the stack. |
| 388 | unsigned NumBytes = 0; |
| 389 | |
| 390 | if (Args.empty()) { |
| 391 | // Save zero bytes. |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 392 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 393 | DAG.getConstant(0, getPointerTy())); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 394 | } else { |
| 395 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 396 | switch (getValueType(Args[i].second)) { |
| 397 | default: assert(0 && "Unknown value type!"); |
| 398 | case MVT::i1: |
| 399 | case MVT::i8: |
| 400 | case MVT::i16: |
| 401 | case MVT::i32: |
| 402 | case MVT::f32: |
| 403 | NumBytes += 4; |
| 404 | break; |
| 405 | case MVT::i64: |
| 406 | case MVT::f64: |
| 407 | NumBytes += 8; |
| 408 | break; |
| 409 | } |
| 410 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 411 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 412 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 413 | |
| 414 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 415 | unsigned ArgOffset = 0; |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 416 | SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32, |
| 417 | DAG.getEntryNode()); |
Chris Lattner | c78776d | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 418 | std::vector<SDOperand> Stores; |
| 419 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 420 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 421 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 422 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 423 | |
| 424 | switch (getValueType(Args[i].second)) { |
| 425 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 426 | case MVT::i1: |
| 427 | case MVT::i8: |
| 428 | case MVT::i16: |
| 429 | // Promote the integer to 32 bits. If the input type is signed use a |
| 430 | // sign extend, otherwise use a zero extend. |
| 431 | if (Args[i].second->isSigned()) |
| 432 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 433 | else |
| 434 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 435 | |
| 436 | // FALL THROUGH |
| 437 | case MVT::i32: |
| 438 | case MVT::f32: |
Chris Lattner | c78776d | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 439 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | daa064d | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 440 | Args[i].first, PtrOff, |
| 441 | DAG.getSrcValue(NULL))); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 442 | ArgOffset += 4; |
| 443 | break; |
| 444 | case MVT::i64: |
| 445 | case MVT::f64: |
Chris Lattner | c78776d | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 446 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | daa064d | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 447 | Args[i].first, PtrOff, |
| 448 | DAG.getSrcValue(NULL))); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 449 | ArgOffset += 8; |
| 450 | break; |
| 451 | } |
| 452 | } |
Chris Lattner | c78776d | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 453 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | std::vector<MVT::ValueType> RetVals; |
| 457 | MVT::ValueType RetTyVT = getValueType(RetTy); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 458 | RetVals.push_back(MVT::Other); |
| 459 | |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 460 | // The result values produced have to be legal. Promote the result. |
| 461 | switch (RetTyVT) { |
| 462 | case MVT::isVoid: break; |
| 463 | default: |
| 464 | RetVals.push_back(RetTyVT); |
| 465 | break; |
| 466 | case MVT::i1: |
| 467 | case MVT::i8: |
| 468 | case MVT::i16: |
| 469 | RetVals.push_back(MVT::i32); |
| 470 | break; |
| 471 | case MVT::f32: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 472 | if (X86ScalarSSE) |
| 473 | RetVals.push_back(MVT::f32); |
| 474 | else |
| 475 | RetVals.push_back(MVT::f64); |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 476 | break; |
| 477 | case MVT::i64: |
| 478 | RetVals.push_back(MVT::i32); |
| 479 | RetVals.push_back(MVT::i32); |
| 480 | break; |
| 481 | } |
| 482 | std::vector<SDOperand> Ops; |
| 483 | Ops.push_back(Chain); |
| 484 | Ops.push_back(Callee); |
| 485 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 486 | Ops.push_back(DAG.getConstant(0, getPointerTy())); |
| 487 | SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 488 | RetVals, Ops); |
| 489 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall); |
| 490 | |
| 491 | SDOperand ResultVal; |
| 492 | switch (RetTyVT) { |
| 493 | case MVT::isVoid: break; |
| 494 | default: |
| 495 | ResultVal = TheCall.getValue(1); |
| 496 | break; |
| 497 | case MVT::i1: |
| 498 | case MVT::i8: |
| 499 | case MVT::i16: |
| 500 | ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1)); |
| 501 | break; |
| 502 | case MVT::f32: |
| 503 | // FIXME: we would really like to remember that this FP_ROUND operation is |
| 504 | // okay to eliminate if we allow excess FP precision. |
| 505 | ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1)); |
| 506 | break; |
| 507 | case MVT::i64: |
| 508 | ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1), |
| 509 | TheCall.getValue(2)); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | return std::make_pair(ResultVal, Chain); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 516 | SDOperand |
| 517 | X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 518 | Value *VAListV, SelectionDAG &DAG) { |
Andrew Lenharth | 9144ec4 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 519 | // vastart just stores the address of the VarArgsFrameIndex slot. |
| 520 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 521 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 522 | DAG.getSrcValue(VAListV)); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 525 | |
| 526 | std::pair<SDOperand,SDOperand> |
| 527 | X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, |
| 528 | Value *VAListV, const Type *ArgTy, |
| 529 | SelectionDAG &DAG) { |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 530 | MVT::ValueType ArgVT = getValueType(ArgTy); |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 531 | SDOperand Val = DAG.getLoad(MVT::i32, Chain, |
| 532 | VAListP, DAG.getSrcValue(VAListV)); |
| 533 | SDOperand Result = DAG.getLoad(ArgVT, Chain, Val, |
Chris Lattner | 91ae129 | 2005-07-05 17:50:16 +0000 | [diff] [blame] | 534 | DAG.getSrcValue(NULL)); |
Andrew Lenharth | 9144ec4 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 535 | unsigned Amt; |
| 536 | if (ArgVT == MVT::i32) |
| 537 | Amt = 4; |
| 538 | else { |
| 539 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 540 | "Other types should have been promoted for varargs!"); |
| 541 | Amt = 8; |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 542 | } |
Andrew Lenharth | 9144ec4 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 543 | Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val, |
| 544 | DAG.getConstant(Amt, Val.getValueType())); |
| 545 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | a722085 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 546 | Val, VAListP, DAG.getSrcValue(VAListV)); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 547 | return std::make_pair(Result, Chain); |
| 548 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 550 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5011ff0 | 2005-05-13 22:46:57 +0000 | [diff] [blame] | 551 | // Fast Calling Convention implementation |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 552 | //===----------------------------------------------------------------------===// |
| 553 | // |
| 554 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 555 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 556 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 557 | // tail calls), and has the same return value conventions as C calling convs. |
| 558 | // |
Chris Lattner | 9b29fe2 | 2005-05-13 23:49:10 +0000 | [diff] [blame] | 559 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 560 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 561 | // reasons. |
| 562 | // |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 563 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 564 | // (when we have a global fp allocator) and do other tricks. |
| 565 | // |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 566 | |
| 567 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 568 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 569 | /// register for it. |
| 570 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 571 | TargetRegisterClass *RC) { |
| 572 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 573 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 574 | MF.addLiveIn(PReg, VReg); |
| 575 | return VReg; |
| 576 | } |
| 577 | |
| 578 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 579 | std::vector<SDOperand> |
| 580 | X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) { |
| 581 | std::vector<SDOperand> ArgValues; |
| 582 | |
| 583 | MachineFunction &MF = DAG.getMachineFunction(); |
| 584 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 585 | |
| 586 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 587 | // frame looks like this: |
| 588 | // |
| 589 | // [ESP] -- return address |
| 590 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
| 591 | // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size |
| 592 | // ... |
| 593 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 594 | |
| 595 | // Keep track of the number of integer regs passed so far. This can be either |
| 596 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 597 | // used). |
| 598 | unsigned NumIntRegs = 0; |
| 599 | |
| 600 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 601 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 602 | unsigned ArgIncrement = 4; |
| 603 | unsigned ObjSize = 0; |
| 604 | SDOperand ArgValue; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 606 | switch (ObjectVT) { |
| 607 | default: assert(0 && "Unhandled argument type!"); |
| 608 | case MVT::i1: |
| 609 | case MVT::i8: |
| 610 | if (NumIntRegs < 2) { |
| 611 | if (!I->use_empty()) { |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 612 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL, |
| 613 | X86::R8RegisterClass); |
| 614 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot()); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 615 | DAG.setRoot(ArgValue.getValue(1)); |
| 616 | } |
| 617 | ++NumIntRegs; |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | ObjSize = 1; |
| 622 | break; |
| 623 | case MVT::i16: |
| 624 | if (NumIntRegs < 2) { |
| 625 | if (!I->use_empty()) { |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 626 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX, |
| 627 | X86::R16RegisterClass); |
| 628 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot()); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 629 | DAG.setRoot(ArgValue.getValue(1)); |
| 630 | } |
| 631 | ++NumIntRegs; |
| 632 | break; |
| 633 | } |
| 634 | ObjSize = 2; |
| 635 | break; |
| 636 | case MVT::i32: |
| 637 | if (NumIntRegs < 2) { |
| 638 | if (!I->use_empty()) { |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 639 | unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX, |
| 640 | X86::R32RegisterClass); |
| 641 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot()); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 642 | DAG.setRoot(ArgValue.getValue(1)); |
| 643 | } |
| 644 | ++NumIntRegs; |
| 645 | break; |
| 646 | } |
| 647 | ObjSize = 4; |
| 648 | break; |
| 649 | case MVT::i64: |
| 650 | if (NumIntRegs == 0) { |
| 651 | if (!I->use_empty()) { |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 652 | unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass); |
| 653 | unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 655 | SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot()); |
| 656 | SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1)); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 657 | DAG.setRoot(Hi.getValue(1)); |
| 658 | |
| 659 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 660 | } |
| 661 | NumIntRegs = 2; |
| 662 | break; |
| 663 | } else if (NumIntRegs == 1) { |
| 664 | if (!I->use_empty()) { |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 665 | unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 666 | SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot()); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 667 | DAG.setRoot(Low.getValue(1)); |
| 668 | |
| 669 | // Load the high part from memory. |
| 670 | // Create the frame index object for this incoming parameter... |
| 671 | int FI = MFI->CreateFixedObject(4, ArgOffset); |
| 672 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 673 | SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 674 | DAG.getSrcValue(NULL)); |
| 675 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 676 | } |
| 677 | ArgOffset += 4; |
| 678 | NumIntRegs = 2; |
| 679 | break; |
| 680 | } |
| 681 | ObjSize = ArgIncrement = 8; |
| 682 | break; |
| 683 | case MVT::f32: ObjSize = 4; break; |
| 684 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 685 | } |
| 686 | |
| 687 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 688 | // dead loads. |
| 689 | if (ObjSize && !I->use_empty()) { |
| 690 | // Create the frame index object for this incoming parameter... |
| 691 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 692 | |
| 693 | // Create the SelectionDAG nodes corresponding to a load from this |
| 694 | // parameter. |
| 695 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 696 | |
| 697 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 698 | DAG.getSrcValue(NULL)); |
| 699 | } else if (ArgValue.Val == 0) { |
| 700 | if (MVT::isInteger(ObjectVT)) |
| 701 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 702 | else |
| 703 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 704 | } |
| 705 | ArgValues.push_back(ArgValue); |
| 706 | |
| 707 | if (ObjSize) |
| 708 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 709 | } |
| 710 | |
Chris Lattner | 9b29fe2 | 2005-05-13 23:49:10 +0000 | [diff] [blame] | 711 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 712 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 713 | if ((ArgOffset & 7) == 0) |
| 714 | ArgOffset += 4; |
| 715 | |
Chris Lattner | c0e369e | 2005-05-13 21:44:04 +0000 | [diff] [blame] | 716 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 717 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 718 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 719 | BytesCallerReserves = 0; |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 720 | |
| 721 | // Finally, inform the code generator which regs we return values in. |
| 722 | switch (getValueType(F.getReturnType())) { |
| 723 | default: assert(0 && "Unknown type!"); |
| 724 | case MVT::isVoid: break; |
| 725 | case MVT::i1: |
| 726 | case MVT::i8: |
| 727 | case MVT::i16: |
| 728 | case MVT::i32: |
| 729 | MF.addLiveOut(X86::EAX); |
| 730 | break; |
| 731 | case MVT::i64: |
| 732 | MF.addLiveOut(X86::EAX); |
| 733 | MF.addLiveOut(X86::EDX); |
| 734 | break; |
| 735 | case MVT::f32: |
| 736 | case MVT::f64: |
| 737 | MF.addLiveOut(X86::ST0); |
| 738 | break; |
| 739 | } |
| 740 | return ArgValues; |
| 741 | } |
| 742 | |
| 743 | std::pair<SDOperand, SDOperand> |
| 744 | X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, |
Chris Lattner | f27e31d | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 745 | bool isTailCall, SDOperand Callee, |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 746 | ArgListTy &Args, SelectionDAG &DAG) { |
| 747 | // Count how many bytes are to be pushed on the stack. |
| 748 | unsigned NumBytes = 0; |
| 749 | |
| 750 | // Keep track of the number of integer regs passed so far. This can be either |
| 751 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 752 | // used). |
| 753 | unsigned NumIntRegs = 0; |
| 754 | |
| 755 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 756 | switch (getValueType(Args[i].second)) { |
| 757 | default: assert(0 && "Unknown value type!"); |
| 758 | case MVT::i1: |
| 759 | case MVT::i8: |
| 760 | case MVT::i16: |
| 761 | case MVT::i32: |
| 762 | if (NumIntRegs < 2) { |
| 763 | ++NumIntRegs; |
| 764 | break; |
| 765 | } |
| 766 | // fall through |
| 767 | case MVT::f32: |
| 768 | NumBytes += 4; |
| 769 | break; |
| 770 | case MVT::i64: |
| 771 | if (NumIntRegs == 0) { |
| 772 | NumIntRegs = 2; |
| 773 | break; |
| 774 | } else if (NumIntRegs == 1) { |
| 775 | NumIntRegs = 2; |
| 776 | NumBytes += 4; |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | // fall through |
| 781 | case MVT::f64: |
| 782 | NumBytes += 8; |
| 783 | break; |
| 784 | } |
| 785 | |
Chris Lattner | 9b29fe2 | 2005-05-13 23:49:10 +0000 | [diff] [blame] | 786 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 787 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 788 | if ((NumBytes & 7) == 0) |
| 789 | NumBytes += 4; |
| 790 | |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 791 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 792 | DAG.getConstant(NumBytes, getPointerTy())); |
| 793 | |
| 794 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 795 | unsigned ArgOffset = 0; |
| 796 | SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32, |
| 797 | DAG.getEntryNode()); |
| 798 | NumIntRegs = 0; |
| 799 | std::vector<SDOperand> Stores; |
| 800 | std::vector<SDOperand> RegValuesToPass; |
| 801 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 802 | switch (getValueType(Args[i].second)) { |
| 803 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 804 | case MVT::i1: |
| 805 | case MVT::i8: |
| 806 | case MVT::i16: |
| 807 | case MVT::i32: |
| 808 | if (NumIntRegs < 2) { |
| 809 | RegValuesToPass.push_back(Args[i].first); |
| 810 | ++NumIntRegs; |
| 811 | break; |
| 812 | } |
| 813 | // Fall through |
| 814 | case MVT::f32: { |
| 815 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 816 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 817 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 818 | Args[i].first, PtrOff, |
| 819 | DAG.getSrcValue(NULL))); |
| 820 | ArgOffset += 4; |
| 821 | break; |
| 822 | } |
| 823 | case MVT::i64: |
| 824 | if (NumIntRegs < 2) { // Can pass part of it in regs? |
| 825 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 826 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 827 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 828 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 829 | RegValuesToPass.push_back(Lo); |
| 830 | ++NumIntRegs; |
| 831 | if (NumIntRegs < 2) { // Pass both parts in regs? |
| 832 | RegValuesToPass.push_back(Hi); |
| 833 | ++NumIntRegs; |
| 834 | } else { |
| 835 | // Pass the high part in memory. |
| 836 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 837 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 838 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 18b2c2f | 2005-05-14 12:03:10 +0000 | [diff] [blame] | 839 | Hi, PtrOff, DAG.getSrcValue(NULL))); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 840 | ArgOffset += 4; |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | // Fall through |
| 845 | case MVT::f64: |
| 846 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 847 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 848 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 849 | Args[i].first, PtrOff, |
| 850 | DAG.getSrcValue(NULL))); |
| 851 | ArgOffset += 8; |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | if (!Stores.empty()) |
| 856 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 857 | |
Chris Lattner | 9b29fe2 | 2005-05-13 23:49:10 +0000 | [diff] [blame] | 858 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 859 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 860 | if ((ArgOffset & 7) == 0) |
| 861 | ArgOffset += 4; |
| 862 | |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 863 | std::vector<MVT::ValueType> RetVals; |
| 864 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 865 | |
| 866 | RetVals.push_back(MVT::Other); |
| 867 | |
| 868 | // The result values produced have to be legal. Promote the result. |
| 869 | switch (RetTyVT) { |
| 870 | case MVT::isVoid: break; |
| 871 | default: |
| 872 | RetVals.push_back(RetTyVT); |
| 873 | break; |
| 874 | case MVT::i1: |
| 875 | case MVT::i8: |
| 876 | case MVT::i16: |
| 877 | RetVals.push_back(MVT::i32); |
| 878 | break; |
| 879 | case MVT::f32: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 880 | if (X86ScalarSSE) |
| 881 | RetVals.push_back(MVT::f32); |
| 882 | else |
| 883 | RetVals.push_back(MVT::f64); |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 884 | break; |
| 885 | case MVT::i64: |
| 886 | RetVals.push_back(MVT::i32); |
| 887 | RetVals.push_back(MVT::i32); |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | std::vector<SDOperand> Ops; |
| 892 | Ops.push_back(Chain); |
| 893 | Ops.push_back(Callee); |
| 894 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 895 | // Callee pops all arg values on the stack. |
| 896 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 897 | |
| 898 | // Pass register arguments as needed. |
| 899 | Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end()); |
| 900 | |
| 901 | SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 902 | RetVals, Ops); |
| 903 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall); |
| 904 | |
| 905 | SDOperand ResultVal; |
| 906 | switch (RetTyVT) { |
| 907 | case MVT::isVoid: break; |
| 908 | default: |
| 909 | ResultVal = TheCall.getValue(1); |
| 910 | break; |
| 911 | case MVT::i1: |
| 912 | case MVT::i8: |
| 913 | case MVT::i16: |
| 914 | ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1)); |
| 915 | break; |
| 916 | case MVT::f32: |
| 917 | // FIXME: we would really like to remember that this FP_ROUND operation is |
| 918 | // okay to eliminate if we allow excess FP precision. |
| 919 | ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1)); |
| 920 | break; |
| 921 | case MVT::i64: |
| 922 | ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1), |
| 923 | TheCall.getValue(2)); |
| 924 | break; |
| 925 | } |
| 926 | |
| 927 | return std::make_pair(ResultVal, Chain); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 930 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
| 931 | if (ReturnAddrIndex == 0) { |
| 932 | // Set up a frame object for the return address. |
| 933 | MachineFunction &MF = DAG.getMachineFunction(); |
| 934 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
| 935 | } |
| 936 | |
| 937 | return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32); |
| 938 | } |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 939 | |
| 940 | |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 941 | |
| 942 | std::pair<SDOperand, SDOperand> X86TargetLowering:: |
| 943 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 944 | SelectionDAG &DAG) { |
| 945 | SDOperand Result; |
| 946 | if (Depth) // Depths > 0 not supported yet! |
| 947 | Result = DAG.getConstant(0, getPointerTy()); |
| 948 | else { |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 949 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 950 | if (!isFrameAddress) |
| 951 | // Just load the return address |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 952 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, |
| 953 | DAG.getSrcValue(NULL)); |
Chris Lattner | 9f59d28 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 954 | else |
| 955 | Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI, |
| 956 | DAG.getConstant(4, MVT::i32)); |
| 957 | } |
| 958 | return std::make_pair(Result, Chain); |
| 959 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 960 | |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 961 | //===----------------------------------------------------------------------===// |
| 962 | // X86 Custom Lowering Hooks |
| 963 | //===----------------------------------------------------------------------===// |
| 964 | |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 965 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 966 | /// |
| 967 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 968 | switch (Op.getOpcode()) { |
| 969 | default: assert(0 && "Should not custom lower this!"); |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 970 | case ISD::SINT_TO_FP: { |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 971 | assert(Op.getValueType() == MVT::f64 && |
| 972 | Op.getOperand(0).getValueType() == MVT::i64 && |
| 973 | "Unknown SINT_TO_FP to lower!"); |
| 974 | // We lower sint64->FP into a store to a temporary stack slot, followed by a |
| 975 | // FILD64m node. |
| 976 | MachineFunction &MF = DAG.getMachineFunction(); |
| 977 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 978 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 979 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 980 | Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL)); |
| 981 | std::vector<MVT::ValueType> RTs; |
| 982 | RTs.push_back(MVT::f64); |
| 983 | RTs.push_back(MVT::Other); |
| 984 | std::vector<SDOperand> Ops; |
| 985 | Ops.push_back(Store); |
| 986 | Ops.push_back(StackSlot); |
| 987 | return DAG.getNode(X86ISD::FILD64m, RTs, Ops); |
| 988 | } |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 989 | case ISD::FP_TO_SINT: { |
| 990 | assert(Op.getValueType() == MVT::i64 && |
| 991 | Op.getOperand(0).getValueType() == MVT::f64 && |
| 992 | "Unknown FP_TO_SINT to lower!"); |
| 993 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 994 | // stack slot. |
| 995 | MachineFunction &MF = DAG.getMachineFunction(); |
| 996 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 997 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 998 | |
| 999 | // Build the FISTP64 |
| 1000 | std::vector<SDOperand> Ops; |
| 1001 | Ops.push_back(DAG.getEntryNode()); |
| 1002 | Ops.push_back(Op.getOperand(0)); |
| 1003 | Ops.push_back(StackSlot); |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 1004 | SDOperand FIST = DAG.getNode(X86ISD::FP_TO_INT64_IN_MEM, MVT::Other, Ops); |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Load the result. |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 1007 | return DAG.getLoad(MVT::i64, FIST, StackSlot, DAG.getSrcValue(NULL)); |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | // Pattern Matcher Implementation |
| 1015 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1017 | namespace { |
| 1018 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 1019 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 1020 | /// tree. |
| 1021 | struct X86ISelAddressMode { |
| 1022 | enum { |
| 1023 | RegBase, |
| 1024 | FrameIndexBase, |
| 1025 | } BaseType; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1027 | struct { // This is really a union, discriminated by BaseType! |
| 1028 | SDOperand Reg; |
| 1029 | int FrameIndex; |
| 1030 | } Base; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1032 | unsigned Scale; |
| 1033 | SDOperand IndexReg; |
| 1034 | unsigned Disp; |
| 1035 | GlobalValue *GV; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1037 | X86ISelAddressMode() |
| 1038 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) { |
| 1039 | } |
| 1040 | }; |
| 1041 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1042 | |
| 1043 | |
| 1044 | namespace { |
| 1045 | Statistic<> |
| 1046 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 1047 | |
| 1048 | //===--------------------------------------------------------------------===// |
| 1049 | /// ISel - X86 specific code to select X86 machine instructions for |
| 1050 | /// SelectionDAG operations. |
| 1051 | /// |
| 1052 | class ISel : public SelectionDAGISel { |
| 1053 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 1054 | /// register should set this to true. |
| 1055 | bool ContainsFPCode; |
| 1056 | |
| 1057 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 1058 | /// X86-specific SelectionDAG. |
| 1059 | X86TargetLowering X86Lowering; |
| 1060 | |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1061 | /// RegPressureMap - This keeps an approximate count of the number of |
| 1062 | /// registers required to evaluate each node in the graph. |
| 1063 | std::map<SDNode*, unsigned> RegPressureMap; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1064 | |
| 1065 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 1066 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 1067 | /// tree. |
| 1068 | std::map<SDOperand, unsigned> ExprMap; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 1070 | /// TheDAG - The DAG being selected during Select* operations. |
| 1071 | SelectionDAG *TheDAG; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1072 | |
| 1073 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 1074 | /// make the right decision when generating code for different targets. |
| 1075 | const X86Subtarget *Subtarget; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1076 | public: |
| 1077 | ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) { |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 1078 | Subtarget = TM.getSubtarget<const X86Subtarget>(); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | e1e844c | 2005-01-21 21:35:14 +0000 | [diff] [blame] | 1081 | virtual const char *getPassName() const { |
| 1082 | return "X86 Pattern Instruction Selection"; |
| 1083 | } |
| 1084 | |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1085 | unsigned getRegPressure(SDOperand O) { |
| 1086 | return RegPressureMap[O.Val]; |
| 1087 | } |
| 1088 | unsigned ComputeRegPressure(SDOperand O); |
| 1089 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1090 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 1091 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1092 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 1094 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 1095 | |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 1096 | bool isFoldableLoad(SDOperand Op, SDOperand OtherOp, |
| 1097 | bool FloatPromoteOk = false); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1098 | void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM); |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 1099 | bool TryToFoldLoadOpStore(SDNode *Node); |
Chris Lattner | 29f5819 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 1100 | bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg); |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1101 | void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse); |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1102 | bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond); |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1103 | void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT, |
| 1104 | unsigned RTrue, unsigned RFalse, unsigned RDest); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1105 | unsigned SelectExpr(SDOperand N); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1106 | |
| 1107 | X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM); |
| 1108 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM); |
| 1109 | void SelectAddress(SDOperand N, X86AddressMode &AM); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 1110 | bool EmitPotentialTailCall(SDNode *Node); |
| 1111 | void EmitFastCCToFastCCTailCall(SDNode *TailCallNode); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1112 | void Select(SDOperand N); |
| 1113 | }; |
| 1114 | } |
| 1115 | |
Chris Lattner | d8145bc | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 1116 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 1117 | /// the main function. |
| 1118 | static void EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 1119 | MachineFrameInfo *MFI) { |
| 1120 | // Switch the FPU to 64-bit precision mode for better compatibility and speed. |
| 1121 | int CWFrameIdx = MFI->CreateStackObject(2, 2); |
| 1122 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 1123 | |
| 1124 | // Set the high part to be 64-bit precision. |
| 1125 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 1126 | CWFrameIdx, 1).addImm(2); |
| 1127 | |
| 1128 | // Reload the modified control word now. |
| 1129 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1130 | } |
| 1131 | |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 1132 | void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 1133 | // If this function has live-in values, emit the copies from pregs to vregs at |
| 1134 | // the top of the function, before anything else. |
| 1135 | MachineBasicBlock *BB = MF.begin(); |
| 1136 | if (MF.livein_begin() != MF.livein_end()) { |
| 1137 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 1138 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 1139 | E = MF.livein_end(); LI != E; ++LI) { |
| 1140 | const TargetRegisterClass *RC = RegMap->getRegClass(LI->second); |
| 1141 | if (RC == X86::R8RegisterClass) { |
| 1142 | BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first); |
| 1143 | } else if (RC == X86::R16RegisterClass) { |
| 1144 | BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first); |
| 1145 | } else if (RC == X86::R32RegisterClass) { |
| 1146 | BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first); |
| 1147 | } else if (RC == X86::RFPRegisterClass) { |
| 1148 | BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1149 | } else if (RC == X86::RXMMRegisterClass) { |
| 1150 | BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first); |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 1151 | } else { |
| 1152 | assert(0 && "Unknown regclass!"); |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | // If this is main, emit special code for main. |
| 1159 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 1160 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 1161 | } |
| 1162 | |
| 1163 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1164 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 1165 | /// when it has created a SelectionDAG for us to codegen. |
| 1166 | void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 1167 | // While we're doing this, keep track of whether we see any FP code for |
| 1168 | // FP_REG_KILL insertion. |
| 1169 | ContainsFPCode = false; |
Chris Lattner | d8145bc | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 1170 | MachineFunction *MF = BB->getParent(); |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Scan the PHI nodes that already are inserted into this basic block. If any |
| 1173 | // of them is a PHI of a floating point value, we need to insert an |
| 1174 | // FP_REG_KILL. |
Chris Lattner | d8145bc | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 1175 | SSARegMap *RegMap = MF->getSSARegMap(); |
Chris Lattner | 0b17b45 | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 1176 | if (BB != MF->begin()) |
| 1177 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 1178 | I != E; ++I) { |
| 1179 | assert(I->getOpcode() == X86::PHI && |
| 1180 | "Isn't just PHI nodes?"); |
| 1181 | if (RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 1182 | X86::RFPRegisterClass) { |
| 1183 | ContainsFPCode = true; |
| 1184 | break; |
| 1185 | } |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1186 | } |
Chris Lattner | d8145bc | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1188 | // Compute the RegPressureMap, which is an approximation for the number of |
| 1189 | // registers required to compute each node. |
| 1190 | ComputeRegPressure(DAG.getRoot()); |
| 1191 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 1192 | TheDAG = &DAG; |
| 1193 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1194 | // Codegen the basic block. |
| 1195 | Select(DAG.getRoot()); |
| 1196 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 1197 | TheDAG = 0; |
| 1198 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1199 | // Finally, look at all of the successors of this block. If any contain a PHI |
| 1200 | // node of FP type, we need to insert an FP_REG_KILL in this block. |
| 1201 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 1202 | E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI) |
| 1203 | for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end(); |
| 1204 | I != E && I->getOpcode() == X86::PHI; ++I) { |
| 1205 | if (RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 1206 | X86::RFPRegisterClass) { |
| 1207 | ContainsFPCode = true; |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 6972c31 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 1212 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 1213 | // corresponding to BB for FP PHI nodes. |
| 1214 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 1215 | const PHINode *PN; |
| 1216 | if (!ContainsFPCode) |
| 1217 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 1218 | SI != E && !ContainsFPCode; ++SI) |
| 1219 | for (BasicBlock::const_iterator II = SI->begin(); |
| 1220 | (PN = dyn_cast<PHINode>(II)); ++II) |
| 1221 | if (PN->getType()->isFloatingPoint()) { |
| 1222 | ContainsFPCode = true; |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1227 | // Insert FP_REG_KILL instructions into basic blocks that need them. This |
| 1228 | // only occurs due to the floating point stackifier not being aggressive |
| 1229 | // enough to handle arbitrary global stackification. |
| 1230 | // |
| 1231 | // Currently we insert an FP_REG_KILL instruction into each block that uses or |
| 1232 | // defines a floating point virtual register. |
| 1233 | // |
| 1234 | // When the global register allocators (like linear scan) finally update live |
| 1235 | // variable analysis, we can keep floating point values in registers across |
| 1236 | // basic blocks. This will be a huge win, but we are waiting on the global |
| 1237 | // allocators before we can do this. |
| 1238 | // |
Chris Lattner | 472a265 | 2005-03-30 01:10:00 +0000 | [diff] [blame] | 1239 | if (ContainsFPCode) { |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1240 | BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); |
| 1241 | ++NumFPKill; |
| 1242 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1244 | // Clear state used for selection. |
| 1245 | ExprMap.clear(); |
Chris Lattner | 6fba62d6 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 1246 | RegPressureMap.clear(); |
| 1247 | } |
| 1248 | |
| 1249 | |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1250 | // ComputeRegPressure - Compute the RegPressureMap, which is an approximation |
| 1251 | // for the number of registers required to compute each node. This is basically |
| 1252 | // computing a generalized form of the Sethi-Ullman number for each node. |
| 1253 | unsigned ISel::ComputeRegPressure(SDOperand O) { |
| 1254 | SDNode *N = O.Val; |
| 1255 | unsigned &Result = RegPressureMap[N]; |
| 1256 | if (Result) return Result; |
| 1257 | |
Chris Lattner | 8fea42b | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 1258 | // FIXME: Should operations like CALL (which clobber lots o regs) have a |
| 1259 | // higher fixed cost?? |
| 1260 | |
Chris Lattner | 8aa10fc | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 1261 | if (N->getNumOperands() == 0) { |
| 1262 | Result = 1; |
| 1263 | } else { |
| 1264 | unsigned MaxRegUse = 0; |
| 1265 | unsigned NumExtraMaxRegUsers = 0; |
| 1266 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 1267 | unsigned Regs; |
| 1268 | if (N->getOperand(i).getOpcode() == ISD::Constant) |
| 1269 | Regs = 0; |
| 1270 | else |
| 1271 | Regs = ComputeRegPressure(N->getOperand(i)); |
| 1272 | if (Regs > MaxRegUse) { |
| 1273 | MaxRegUse = Regs; |
| 1274 | NumExtraMaxRegUsers = 0; |
| 1275 | } else if (Regs == MaxRegUse && |
| 1276 | N->getOperand(i).getValueType() != MVT::Other) { |
| 1277 | ++NumExtraMaxRegUsers; |
| 1278 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | ca318ed | 2005-01-17 22:56:09 +0000 | [diff] [blame] | 1280 | |
| 1281 | if (O.getOpcode() != ISD::TokenFactor) |
| 1282 | Result = MaxRegUse+NumExtraMaxRegUsers; |
| 1283 | else |
Chris Lattner | a5d137f | 2005-01-17 23:02:13 +0000 | [diff] [blame] | 1284 | Result = MaxRegUse == 1 ? 0 : MaxRegUse-1; |
Chris Lattner | 8aa10fc | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 1285 | } |
Chris Lattner | b7fe57a | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | 75bac9f | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 1287 | //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n"; |
Chris Lattner | 8aa10fc | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 1288 | return Result; |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Chris Lattner | 5b04f33 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1291 | /// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op. |
| 1292 | /// The DAG cannot have cycles in it, by definition, so the visited set is not |
| 1293 | /// needed to prevent infinite loops. The DAG CAN, however, have unbounded |
| 1294 | /// reuse, so it prevents exponential cases. |
| 1295 | /// |
| 1296 | static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op, |
| 1297 | std::set<SDNode*> &Visited) { |
| 1298 | if (N == Op) return true; // Found it. |
| 1299 | SDNode *Node = N.Val; |
Chris Lattner | e70eb9da | 2005-01-21 21:43:02 +0000 | [diff] [blame] | 1300 | if (Node->getNumOperands() == 0 || // Leaf? |
| 1301 | Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it? |
Chris Lattner | 5b04f33 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1302 | if (!Visited.insert(Node).second) return false; // Already visited? |
| 1303 | |
| 1304 | // Recurse for the first N-1 operands. |
| 1305 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1306 | if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited)) |
| 1307 | return true; |
| 1308 | |
| 1309 | // Tail recurse for the last operand. |
| 1310 | return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited); |
| 1311 | } |
| 1312 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1313 | X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) { |
| 1314 | X86AddressMode Result; |
| 1315 | |
| 1316 | // If we need to emit two register operands, emit the one with the highest |
| 1317 | // register pressure first. |
| 1318 | if (IAM.BaseType == X86ISelAddressMode::RegBase && |
| 1319 | IAM.Base.Reg.Val && IAM.IndexReg.Val) { |
Chris Lattner | 5b04f33 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1320 | bool EmitBaseThenIndex; |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1321 | if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) { |
Chris Lattner | 5b04f33 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1322 | std::set<SDNode*> Visited; |
| 1323 | EmitBaseThenIndex = true; |
| 1324 | // If Base ends up pointing to Index, we must emit index first. This is |
| 1325 | // because of the way we fold loads, we may end up doing bad things with |
| 1326 | // the folded add. |
| 1327 | if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited)) |
| 1328 | EmitBaseThenIndex = false; |
| 1329 | } else { |
| 1330 | std::set<SDNode*> Visited; |
| 1331 | EmitBaseThenIndex = false; |
| 1332 | // If Base ends up pointing to Index, we must emit index first. This is |
| 1333 | // because of the way we fold loads, we may end up doing bad things with |
| 1334 | // the folded add. |
| 1335 | if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited)) |
| 1336 | EmitBaseThenIndex = true; |
| 1337 | } |
| 1338 | |
| 1339 | if (EmitBaseThenIndex) { |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1340 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1341 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1342 | } else { |
| 1343 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1344 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1345 | } |
Chris Lattner | 5b04f33 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1347 | } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) { |
| 1348 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1349 | } else if (IAM.IndexReg.Val) { |
| 1350 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1351 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1353 | switch (IAM.BaseType) { |
| 1354 | case X86ISelAddressMode::RegBase: |
| 1355 | Result.BaseType = X86AddressMode::RegBase; |
| 1356 | break; |
| 1357 | case X86ISelAddressMode::FrameIndexBase: |
| 1358 | Result.BaseType = X86AddressMode::FrameIndexBase; |
| 1359 | Result.Base.FrameIndex = IAM.Base.FrameIndex; |
| 1360 | break; |
| 1361 | default: |
| 1362 | assert(0 && "Unknown base type!"); |
| 1363 | break; |
| 1364 | } |
| 1365 | Result.Scale = IAM.Scale; |
| 1366 | Result.Disp = IAM.Disp; |
| 1367 | Result.GV = IAM.GV; |
| 1368 | return Result; |
| 1369 | } |
| 1370 | |
| 1371 | /// SelectAddress - Pattern match the maximal addressing mode for this node and |
| 1372 | /// emit all of the leaf registers. |
| 1373 | void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) { |
| 1374 | X86ISelAddressMode IAM; |
| 1375 | MatchAddress(N, IAM); |
| 1376 | AM = SelectAddrExprs(IAM); |
| 1377 | } |
| 1378 | |
| 1379 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 1380 | /// returning true if it cannot be done. This just pattern matches for the |
| 1381 | /// addressing mode, it does not cause any code to be emitted. For that, use |
| 1382 | /// SelectAddress. |
| 1383 | bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1384 | switch (N.getOpcode()) { |
| 1385 | default: break; |
| 1386 | case ISD::FrameIndex: |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1387 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 1388 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1389 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 1390 | return false; |
| 1391 | } |
| 1392 | break; |
| 1393 | case ISD::GlobalAddress: |
| 1394 | if (AM.GV == 0) { |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 1395 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 1396 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 1397 | // the value at address GV, not the value of GV itself. This means that |
| 1398 | // the GlobalAddress must be in the base or index register of the address, |
| 1399 | // not the GV offset field. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1400 | if (Subtarget->getIndirectExternAndWeakGlobals() && |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 1401 | (GV->hasWeakLinkage() || GV->isExternal())) { |
| 1402 | break; |
| 1403 | } else { |
| 1404 | AM.GV = GV; |
| 1405 | return false; |
| 1406 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1407 | } |
| 1408 | break; |
| 1409 | case ISD::Constant: |
| 1410 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 1411 | return false; |
| 1412 | case ISD::SHL: |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1413 | // We might have folded the load into this shift, so don't regen the value |
| 1414 | // if so. |
| 1415 | if (ExprMap.count(N)) break; |
| 1416 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1417 | if (AM.IndexReg.Val == 0 && AM.Scale == 1) |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1418 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 1419 | unsigned Val = CN->getValue(); |
| 1420 | if (Val == 1 || Val == 2 || Val == 3) { |
| 1421 | AM.Scale = 1 << Val; |
Chris Lattner | b74ec4c | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1422 | SDOperand ShVal = N.Val->getOperand(0); |
| 1423 | |
| 1424 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1425 | // value is an add of something and a constant, we can fold the |
| 1426 | // constant into the disp field here. |
Chris Lattner | ed246ec | 2005-01-18 04:18:32 +0000 | [diff] [blame] | 1427 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
Chris Lattner | b74ec4c | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1428 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1429 | AM.IndexReg = ShVal.Val->getOperand(0); |
Chris Lattner | b74ec4c | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1430 | ConstantSDNode *AddVal = |
| 1431 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 1432 | AM.Disp += AddVal->getValue() << Val; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1433 | } else { |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1434 | AM.IndexReg = ShVal; |
Chris Lattner | b74ec4c | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1435 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1436 | return false; |
| 1437 | } |
| 1438 | } |
| 1439 | break; |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1440 | case ISD::MUL: |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1441 | // We might have folded the load into this mul, so don't regen the value if |
| 1442 | // so. |
| 1443 | if (ExprMap.count(N)) break; |
| 1444 | |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1445 | // X*[3,5,9] -> X+X*[2,4,8] |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1446 | if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase && |
| 1447 | AM.Base.Reg.Val == 0) |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1448 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 1449 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 1450 | AM.Scale = unsigned(CN->getValue())-1; |
| 1451 | |
| 1452 | SDOperand MulVal = N.Val->getOperand(0); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1453 | SDOperand Reg; |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1456 | // value is an add of something and a constant, we can fold the |
| 1457 | // constant into the disp field here. |
Chris Lattner | ed246ec | 2005-01-18 04:18:32 +0000 | [diff] [blame] | 1458 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1459 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1460 | Reg = MulVal.Val->getOperand(0); |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1461 | ConstantSDNode *AddVal = |
| 1462 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 1463 | AM.Disp += AddVal->getValue() * CN->getValue(); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1464 | } else { |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1465 | Reg = N.Val->getOperand(0); |
Chris Lattner | 8cf9cda | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | AM.IndexReg = AM.Base.Reg = Reg; |
| 1469 | return false; |
| 1470 | } |
| 1471 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1472 | |
| 1473 | case ISD::ADD: { |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1474 | // We might have folded the load into this mul, so don't regen the value if |
| 1475 | // so. |
| 1476 | if (ExprMap.count(N)) break; |
| 1477 | |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1478 | X86ISelAddressMode Backup = AM; |
| 1479 | if (!MatchAddress(N.Val->getOperand(0), AM) && |
| 1480 | !MatchAddress(N.Val->getOperand(1), AM)) |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1481 | return false; |
| 1482 | AM = Backup; |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1483 | if (!MatchAddress(N.Val->getOperand(1), AM) && |
| 1484 | !MatchAddress(N.Val->getOperand(0), AM)) |
Chris Lattner | 1755360 | 2005-01-12 18:08:53 +0000 | [diff] [blame] | 1485 | return false; |
| 1486 | AM = Backup; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | } |
| 1489 | } |
| 1490 | |
Chris Lattner | 378262d | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1491 | // Is the base register already occupied? |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1492 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
Chris Lattner | 378262d | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1493 | // If so, check to see if the scale index register is set. |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1494 | if (AM.IndexReg.Val == 0) { |
| 1495 | AM.IndexReg = N; |
Chris Lattner | 378262d | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1496 | AM.Scale = 1; |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | // Otherwise, we cannot select it. |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1501 | return true; |
Chris Lattner | 378262d | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1502 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Default, generate it as a register. |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1505 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 1506 | AM.Base.Reg = N; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1507 | return false; |
| 1508 | } |
| 1509 | |
| 1510 | /// Emit2SetCCsAndLogical - Emit the following sequence of instructions, |
| 1511 | /// assuming that the temporary registers are in the 8-bit register class. |
| 1512 | /// |
| 1513 | /// Tmp1 = setcc1 |
| 1514 | /// Tmp2 = setcc2 |
| 1515 | /// DestReg = logicalop Tmp1, Tmp2 |
| 1516 | /// |
| 1517 | static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1, |
| 1518 | unsigned SetCC2, unsigned LogicalOp, |
| 1519 | unsigned DestReg) { |
| 1520 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 1521 | unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 1522 | unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 1523 | BuildMI(BB, SetCC1, 0, Tmp1); |
| 1524 | BuildMI(BB, SetCC2, 0, Tmp2); |
| 1525 | BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2); |
| 1526 | } |
| 1527 | |
| 1528 | /// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the |
| 1529 | /// condition codes match the specified SetCCOpcode. Note that some conditions |
| 1530 | /// require multiple instructions to generate the correct value. |
| 1531 | static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg, |
| 1532 | ISD::CondCode SetCCOpcode, bool isFP) { |
| 1533 | unsigned Opc; |
| 1534 | if (!isFP) { |
| 1535 | switch (SetCCOpcode) { |
| 1536 | default: assert(0 && "Illegal integer SetCC!"); |
| 1537 | case ISD::SETEQ: Opc = X86::SETEr; break; |
| 1538 | case ISD::SETGT: Opc = X86::SETGr; break; |
| 1539 | case ISD::SETGE: Opc = X86::SETGEr; break; |
| 1540 | case ISD::SETLT: Opc = X86::SETLr; break; |
| 1541 | case ISD::SETLE: Opc = X86::SETLEr; break; |
| 1542 | case ISD::SETNE: Opc = X86::SETNEr; break; |
| 1543 | case ISD::SETULT: Opc = X86::SETBr; break; |
| 1544 | case ISD::SETUGT: Opc = X86::SETAr; break; |
| 1545 | case ISD::SETULE: Opc = X86::SETBEr; break; |
| 1546 | case ISD::SETUGE: Opc = X86::SETAEr; break; |
| 1547 | } |
| 1548 | } else { |
| 1549 | // On a floating point condition, the flags are set as follows: |
| 1550 | // ZF PF CF op |
| 1551 | // 0 | 0 | 0 | X > Y |
| 1552 | // 0 | 0 | 1 | X < Y |
| 1553 | // 1 | 0 | 0 | X == Y |
| 1554 | // 1 | 1 | 1 | unordered |
| 1555 | // |
| 1556 | switch (SetCCOpcode) { |
| 1557 | default: assert(0 && "Invalid FP setcc!"); |
| 1558 | case ISD::SETUEQ: |
| 1559 | case ISD::SETEQ: |
| 1560 | Opc = X86::SETEr; // True if ZF = 1 |
| 1561 | break; |
| 1562 | case ISD::SETOGT: |
| 1563 | case ISD::SETGT: |
| 1564 | Opc = X86::SETAr; // True if CF = 0 and ZF = 0 |
| 1565 | break; |
| 1566 | case ISD::SETOGE: |
| 1567 | case ISD::SETGE: |
| 1568 | Opc = X86::SETAEr; // True if CF = 0 |
| 1569 | break; |
| 1570 | case ISD::SETULT: |
| 1571 | case ISD::SETLT: |
| 1572 | Opc = X86::SETBr; // True if CF = 1 |
| 1573 | break; |
| 1574 | case ISD::SETULE: |
| 1575 | case ISD::SETLE: |
| 1576 | Opc = X86::SETBEr; // True if CF = 1 or ZF = 1 |
| 1577 | break; |
| 1578 | case ISD::SETONE: |
| 1579 | case ISD::SETNE: |
| 1580 | Opc = X86::SETNEr; // True if ZF = 0 |
| 1581 | break; |
| 1582 | case ISD::SETUO: |
| 1583 | Opc = X86::SETPr; // True if PF = 1 |
| 1584 | break; |
| 1585 | case ISD::SETO: |
| 1586 | Opc = X86::SETNPr; // True if PF = 0 |
| 1587 | break; |
| 1588 | case ISD::SETOEQ: // !PF & ZF |
| 1589 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg); |
| 1590 | return; |
| 1591 | case ISD::SETOLT: // !PF & CF |
| 1592 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg); |
| 1593 | return; |
| 1594 | case ISD::SETOLE: // !PF & (CF || ZF) |
| 1595 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg); |
| 1596 | return; |
| 1597 | case ISD::SETUGT: // PF | (!ZF & !CF) |
| 1598 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg); |
| 1599 | return; |
| 1600 | case ISD::SETUGE: // PF | !CF |
| 1601 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg); |
| 1602 | return; |
| 1603 | case ISD::SETUNE: // PF | !ZF |
| 1604 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg); |
| 1605 | return; |
| 1606 | } |
| 1607 | } |
| 1608 | BuildMI(BB, Opc, 0, DestReg); |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | /// EmitBranchCC - Emit code into BB that arranges for control to transfer to |
| 1613 | /// the Dest block if the Cond condition is true. If we cannot fold this |
| 1614 | /// condition into the branch, return true. |
| 1615 | /// |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1616 | bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, |
| 1617 | SDOperand Cond) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1618 | // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A > |
| 1619 | // B) using two conditional branches instead of one condbr, two setcc's, and |
| 1620 | // an or. |
| 1621 | if ((Cond.getOpcode() == ISD::OR || |
| 1622 | Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) { |
| 1623 | // And and or set the flags for us, so there is no need to emit a TST of the |
| 1624 | // result. It is only safe to do this if there is only a single use of the |
| 1625 | // AND/OR though, otherwise we don't know it will be emitted here. |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1626 | Select(Chain); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1627 | SelectExpr(Cond); |
| 1628 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 1629 | return false; |
| 1630 | } |
| 1631 | |
| 1632 | // Codegen br not C -> JE. |
| 1633 | if (Cond.getOpcode() == ISD::XOR) |
| 1634 | if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1))) |
| 1635 | if (NC->isAllOnesValue()) { |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1636 | unsigned CondR; |
| 1637 | if (getRegPressure(Chain) > getRegPressure(Cond)) { |
| 1638 | Select(Chain); |
| 1639 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 1640 | } else { |
| 1641 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 1642 | Select(Chain); |
| 1643 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1644 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR); |
| 1645 | BuildMI(BB, X86::JE, 1).addMBB(Dest); |
| 1646 | return false; |
| 1647 | } |
| 1648 | |
| 1649 | SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond); |
| 1650 | if (SetCC == 0) |
| 1651 | return true; // Can only handle simple setcc's so far. |
| 1652 | |
| 1653 | unsigned Opc; |
| 1654 | |
| 1655 | // Handle integer conditions first. |
| 1656 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 1657 | switch (SetCC->getCondition()) { |
| 1658 | default: assert(0 && "Illegal integer SetCC!"); |
| 1659 | case ISD::SETEQ: Opc = X86::JE; break; |
| 1660 | case ISD::SETGT: Opc = X86::JG; break; |
| 1661 | case ISD::SETGE: Opc = X86::JGE; break; |
| 1662 | case ISD::SETLT: Opc = X86::JL; break; |
| 1663 | case ISD::SETLE: Opc = X86::JLE; break; |
| 1664 | case ISD::SETNE: Opc = X86::JNE; break; |
| 1665 | case ISD::SETULT: Opc = X86::JB; break; |
| 1666 | case ISD::SETUGT: Opc = X86::JA; break; |
| 1667 | case ISD::SETULE: Opc = X86::JBE; break; |
| 1668 | case ISD::SETUGE: Opc = X86::JAE; break; |
| 1669 | } |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1670 | Select(Chain); |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1671 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse()); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1672 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1676 | unsigned Opc2 = 0; // Second branch if needed. |
| 1677 | |
| 1678 | // On a floating point condition, the flags are set as follows: |
| 1679 | // ZF PF CF op |
| 1680 | // 0 | 0 | 0 | X > Y |
| 1681 | // 0 | 0 | 1 | X < Y |
| 1682 | // 1 | 0 | 0 | X == Y |
| 1683 | // 1 | 1 | 1 | unordered |
| 1684 | // |
| 1685 | switch (SetCC->getCondition()) { |
| 1686 | default: assert(0 && "Invalid FP setcc!"); |
| 1687 | case ISD::SETUEQ: |
| 1688 | case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1 |
| 1689 | case ISD::SETOGT: |
| 1690 | case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0 |
| 1691 | case ISD::SETOGE: |
| 1692 | case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0 |
| 1693 | case ISD::SETULT: |
| 1694 | case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1 |
| 1695 | case ISD::SETULE: |
| 1696 | case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1 |
| 1697 | case ISD::SETONE: |
| 1698 | case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0 |
| 1699 | case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1 |
| 1700 | case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0 |
| 1701 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 1702 | Opc = X86::JA; // ZF = 0 & CF = 0 |
| 1703 | Opc2 = X86::JP; // PF = 1 |
| 1704 | break; |
| 1705 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 1706 | Opc = X86::JAE; // CF = 0 |
| 1707 | Opc2 = X86::JP; // PF = 1 |
| 1708 | break; |
| 1709 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 1710 | Opc = X86::JNE; // ZF = 0 |
| 1711 | Opc2 = X86::JP; // PF = 1 |
| 1712 | break; |
| 1713 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 1714 | //X86::JNP, X86::JE |
| 1715 | //X86::AND8rr |
| 1716 | return true; // FIXME: Emit more efficient code for this branch. |
| 1717 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 1718 | //X86::JNP, X86::JB |
| 1719 | //X86::AND8rr |
| 1720 | return true; // FIXME: Emit more efficient code for this branch. |
| 1721 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 1722 | //X86::JNP, X86::JBE |
| 1723 | //X86::AND8rr |
| 1724 | return true; // FIXME: Emit more efficient code for this branch. |
| 1725 | } |
| 1726 | |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1727 | Select(Chain); |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1728 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse()); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1729 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 1730 | if (Opc2) |
| 1731 | BuildMI(BB, Opc2, 1).addMBB(Dest); |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1735 | /// EmitSelectCC - Emit code into BB that performs a select operation between |
| 1736 | /// the two registers RTrue and RFalse, generating a result into RDest. Return |
| 1737 | /// true if the fold cannot be performed. |
| 1738 | /// |
| 1739 | void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT, |
| 1740 | unsigned RTrue, unsigned RFalse, unsigned RDest) { |
| 1741 | enum Condition { |
| 1742 | EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP, |
| 1743 | NOT_SET |
| 1744 | } CondCode = NOT_SET; |
| 1745 | |
| 1746 | static const unsigned CMOVTAB16[] = { |
| 1747 | X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr, |
| 1748 | X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr, |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1749 | X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr, |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1750 | }; |
| 1751 | static const unsigned CMOVTAB32[] = { |
| 1752 | X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr, |
| 1753 | X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr, |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1754 | X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr, |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1755 | }; |
| 1756 | static const unsigned CMOVTABFP[] = { |
| 1757 | X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0, |
| 1758 | /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE, |
| 1759 | X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP |
| 1760 | }; |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 1761 | static const int SSE_CMOVTAB[] = { |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1762 | 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */, |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 1763 | 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0, |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1764 | /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0 |
| 1765 | }; |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1766 | |
| 1767 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) { |
| 1768 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 1769 | switch (SetCC->getCondition()) { |
| 1770 | default: assert(0 && "Unknown integer comparison!"); |
| 1771 | case ISD::SETEQ: CondCode = EQ; break; |
| 1772 | case ISD::SETGT: CondCode = GT; break; |
| 1773 | case ISD::SETGE: CondCode = GE; break; |
| 1774 | case ISD::SETLT: CondCode = LT; break; |
| 1775 | case ISD::SETLE: CondCode = LE; break; |
| 1776 | case ISD::SETNE: CondCode = NE; break; |
| 1777 | case ISD::SETULT: CondCode = B; break; |
| 1778 | case ISD::SETUGT: CondCode = A; break; |
| 1779 | case ISD::SETULE: CondCode = BE; break; |
| 1780 | case ISD::SETUGE: CondCode = AE; break; |
| 1781 | } |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1782 | } else if (X86ScalarSSE) { |
| 1783 | switch (SetCC->getCondition()) { |
| 1784 | default: assert(0 && "Unknown scalar fp comparison!"); |
| 1785 | case ISD::SETEQ: CondCode = EQ; break; |
| 1786 | case ISD::SETNE: CondCode = NE; break; |
| 1787 | case ISD::SETULT: |
| 1788 | case ISD::SETLT: CondCode = LT; break; |
| 1789 | case ISD::SETULE: |
| 1790 | case ISD::SETLE: CondCode = LE; break; |
| 1791 | case ISD::SETUGT: |
| 1792 | case ISD::SETGT: CondCode = GT; break; |
| 1793 | case ISD::SETUGE: |
| 1794 | case ISD::SETGE: CondCode = GE; break; |
| 1795 | } |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1796 | } else { |
| 1797 | // On a floating point condition, the flags are set as follows: |
| 1798 | // ZF PF CF op |
| 1799 | // 0 | 0 | 0 | X > Y |
| 1800 | // 0 | 0 | 1 | X < Y |
| 1801 | // 1 | 0 | 0 | X == Y |
| 1802 | // 1 | 1 | 1 | unordered |
| 1803 | // |
| 1804 | switch (SetCC->getCondition()) { |
| 1805 | default: assert(0 && "Unknown FP comparison!"); |
| 1806 | case ISD::SETUEQ: |
| 1807 | case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1 |
| 1808 | case ISD::SETOGT: |
| 1809 | case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0 |
| 1810 | case ISD::SETOGE: |
| 1811 | case ISD::SETGE: CondCode = AE; break; // True if CF = 0 |
| 1812 | case ISD::SETULT: |
| 1813 | case ISD::SETLT: CondCode = B; break; // True if CF = 1 |
| 1814 | case ISD::SETULE: |
| 1815 | case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1 |
| 1816 | case ISD::SETONE: |
| 1817 | case ISD::SETNE: CondCode = NE; break; // True if ZF = 0 |
| 1818 | case ISD::SETUO: CondCode = P; break; // True if PF = 1 |
| 1819 | case ISD::SETO: CondCode = NP; break; // True if PF = 0 |
| 1820 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 1821 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 1822 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 1823 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 1824 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 1825 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 1826 | // We cannot emit this comparison as a single cmov. |
| 1827 | break; |
| 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1832 | // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in |
| 1833 | // Others we will have to do the PowerPC thing and generate an MBB for the |
| 1834 | // true and false values and select between them with a PHI. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1835 | if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) { |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 1836 | if (0 && CondCode != NOT_SET) { |
| 1837 | // FIXME: check for min and max |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1838 | } else { |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 1839 | // FIXME: emit a direct compare and branch rather than setting a cond reg |
| 1840 | // and testing it. |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1841 | unsigned CondReg = SelectExpr(Cond); |
| 1842 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg); |
| 1843 | |
| 1844 | // Create an iterator with which to insert the MBB for copying the false |
| 1845 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1846 | MachineBasicBlock *thisMBB = BB; |
| 1847 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1848 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1849 | ++It; |
| 1850 | |
| 1851 | // thisMBB: |
| 1852 | // ... |
| 1853 | // TrueVal = ... |
| 1854 | // cmpTY ccX, r1, r2 |
| 1855 | // bCC sinkMBB |
| 1856 | // fallthrough --> copy0MBB |
| 1857 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1858 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1859 | BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB); |
| 1860 | MachineFunction *F = BB->getParent(); |
| 1861 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1862 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1863 | // Update machine-CFG edges |
| 1864 | BB->addSuccessor(copy0MBB); |
| 1865 | BB->addSuccessor(sinkMBB); |
| 1866 | |
| 1867 | // copy0MBB: |
| 1868 | // %FalseValue = ... |
| 1869 | // # fallthrough to sinkMBB |
| 1870 | BB = copy0MBB; |
| 1871 | // Update machine-CFG edges |
| 1872 | BB->addSuccessor(sinkMBB); |
| 1873 | |
| 1874 | // sinkMBB: |
| 1875 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1876 | // ... |
| 1877 | BB = sinkMBB; |
| 1878 | BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse) |
| 1879 | .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB); |
| 1880 | } |
| 1881 | return; |
| 1882 | } |
| 1883 | |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1884 | unsigned Opc = 0; |
| 1885 | if (CondCode != NOT_SET) { |
| 1886 | switch (SVT) { |
| 1887 | default: assert(0 && "Cannot select this type!"); |
| 1888 | case MVT::i16: Opc = CMOVTAB16[CondCode]; break; |
| 1889 | case MVT::i32: Opc = CMOVTAB32[CondCode]; break; |
Chris Lattner | e44e6d1 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1890 | case MVT::f64: Opc = CMOVTABFP[CondCode]; break; |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1893 | |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1894 | // Finally, if we weren't able to fold this, just emit the condition and test |
| 1895 | // it. |
| 1896 | if (CondCode == NOT_SET || Opc == 0) { |
| 1897 | // Get the condition into the zero flag. |
| 1898 | unsigned CondReg = SelectExpr(Cond); |
| 1899 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg); |
| 1900 | |
| 1901 | switch (SVT) { |
| 1902 | default: assert(0 && "Cannot select this type!"); |
| 1903 | case MVT::i16: Opc = X86::CMOVE16rr; break; |
| 1904 | case MVT::i32: Opc = X86::CMOVE32rr; break; |
Chris Lattner | e44e6d1 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1905 | case MVT::f64: Opc = X86::FCMOVE; break; |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } else { |
| 1908 | // FIXME: CMP R, 0 -> TEST R, R |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1909 | EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse()); |
Chris Lattner | 8fea42b | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 1910 | std::swap(RTrue, RFalse); |
Chris Lattner | 1d13a92 | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1911 | } |
| 1912 | BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse); |
| 1913 | } |
| 1914 | |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1915 | void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1916 | unsigned Opc; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1917 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) { |
| 1918 | Opc = 0; |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1919 | if (HasOneUse && isFoldableLoad(LHS, RHS)) { |
Chris Lattner | 2cfce68 | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1920 | switch (RHS.getValueType()) { |
| 1921 | default: break; |
| 1922 | case MVT::i1: |
| 1923 | case MVT::i8: Opc = X86::CMP8mi; break; |
| 1924 | case MVT::i16: Opc = X86::CMP16mi; break; |
| 1925 | case MVT::i32: Opc = X86::CMP32mi; break; |
| 1926 | } |
| 1927 | if (Opc) { |
| 1928 | X86AddressMode AM; |
| 1929 | EmitFoldedLoad(LHS, AM); |
| 1930 | addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue()); |
| 1931 | return; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1935 | switch (RHS.getValueType()) { |
| 1936 | default: break; |
| 1937 | case MVT::i1: |
| 1938 | case MVT::i8: Opc = X86::CMP8ri; break; |
| 1939 | case MVT::i16: Opc = X86::CMP16ri; break; |
| 1940 | case MVT::i32: Opc = X86::CMP32ri; break; |
| 1941 | } |
| 1942 | if (Opc) { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1943 | unsigned Tmp1 = SelectExpr(LHS); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1944 | BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue()); |
| 1945 | return; |
| 1946 | } |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1947 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) { |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1948 | if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) || |
| 1949 | CN->isExactlyValue(-0.0))) { |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1950 | unsigned Reg = SelectExpr(LHS); |
| 1951 | BuildMI(BB, X86::FTST, 1).addReg(Reg); |
| 1952 | BuildMI(BB, X86::FNSTSW8r, 0); |
| 1953 | BuildMI(BB, X86::SAHF, 1); |
Chris Lattner | 43832b0 | 2005-03-17 16:29:26 +0000 | [diff] [blame] | 1954 | return; |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1955 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Chris Lattner | 2cfce68 | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1958 | Opc = 0; |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1959 | if (HasOneUse && isFoldableLoad(LHS, RHS)) { |
Chris Lattner | 2cfce68 | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1960 | switch (RHS.getValueType()) { |
| 1961 | default: break; |
| 1962 | case MVT::i1: |
| 1963 | case MVT::i8: Opc = X86::CMP8mr; break; |
| 1964 | case MVT::i16: Opc = X86::CMP16mr; break; |
| 1965 | case MVT::i32: Opc = X86::CMP32mr; break; |
| 1966 | } |
| 1967 | if (Opc) { |
| 1968 | X86AddressMode AM; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1969 | EmitFoldedLoad(LHS, AM); |
| 1970 | unsigned Reg = SelectExpr(RHS); |
Chris Lattner | 2cfce68 | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1971 | addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg); |
| 1972 | return; |
| 1973 | } |
| 1974 | } |
| 1975 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1976 | switch (LHS.getValueType()) { |
| 1977 | default: assert(0 && "Cannot compare this value!"); |
| 1978 | case MVT::i1: |
| 1979 | case MVT::i8: Opc = X86::CMP8rr; break; |
| 1980 | case MVT::i16: Opc = X86::CMP16rr; break; |
| 1981 | case MVT::i32: Opc = X86::CMP32rr; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 1982 | case MVT::f32: Opc = X86::UCOMISSrr; break; |
| 1983 | case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1984 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1985 | unsigned Tmp1, Tmp2; |
| 1986 | if (getRegPressure(LHS) > getRegPressure(RHS)) { |
| 1987 | Tmp1 = SelectExpr(LHS); |
| 1988 | Tmp2 = SelectExpr(RHS); |
| 1989 | } else { |
| 1990 | Tmp2 = SelectExpr(RHS); |
| 1991 | Tmp1 = SelectExpr(LHS); |
| 1992 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1993 | BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2); |
| 1994 | } |
| 1995 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1996 | /// isFoldableLoad - Return true if this is a load instruction that can safely |
| 1997 | /// be folded into an operation that uses it. |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 1998 | bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){ |
| 1999 | if (Op.getOpcode() == ISD::LOAD) { |
| 2000 | // FIXME: currently can't fold constant pool indexes. |
| 2001 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 2002 | return false; |
| 2003 | } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD && |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 2004 | cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) { |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2005 | // FIXME: currently can't fold constant pool indexes. |
| 2006 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 2007 | return false; |
| 2008 | } else { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2009 | return false; |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2010 | } |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2011 | |
| 2012 | // If this load has already been emitted, we clearly can't fold it. |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2013 | assert(Op.ResNo == 0 && "Not a use of the value of the load?"); |
| 2014 | if (ExprMap.count(Op.getValue(1))) return false; |
| 2015 | assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?"); |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 2016 | assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?"); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2018 | // If there is not just one use of its value, we cannot fold. |
| 2019 | if (!Op.Val->hasNUsesOfValue(1, 0)) return false; |
| 2020 | |
| 2021 | // Finally, we cannot fold the load into the operation if this would induce a |
| 2022 | // cycle into the resultant dag. To check for this, see if OtherOp (the other |
| 2023 | // operand of the operation we are folding the load into) can possible use the |
| 2024 | // chain node defined by the load. |
| 2025 | if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain? |
| 2026 | std::set<SDNode*> Visited; |
| 2027 | if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited)) |
| 2028 | return false; |
| 2029 | } |
| 2030 | return true; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2033 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2034 | /// EmitFoldedLoad - Ensure that the arguments of the load are code generated, |
| 2035 | /// and compute the address being loaded into AM. |
| 2036 | void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) { |
| 2037 | SDOperand Chain = Op.getOperand(0); |
| 2038 | SDOperand Address = Op.getOperand(1); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 2039 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2040 | if (getRegPressure(Chain) > getRegPressure(Address)) { |
| 2041 | Select(Chain); |
| 2042 | SelectAddress(Address, AM); |
| 2043 | } else { |
| 2044 | SelectAddress(Address, AM); |
| 2045 | Select(Chain); |
| 2046 | } |
| 2047 | |
| 2048 | // The chain for this load is now lowered. |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2049 | assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 && |
| 2050 | "Load emitted more than once?"); |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 2051 | if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second) |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2052 | assert(0 && "Load emitted more than once!"); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Chris Lattner | 29f5819 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 2055 | // EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1 |
| 2056 | // and op2 are i8/i16/i32 values with one use each (the or). If we can form a |
| 2057 | // SHLD or SHRD, emit the instruction (generating the value into DestReg) and |
| 2058 | // return true. |
| 2059 | bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) { |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2060 | if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) { |
| 2061 | // good! |
| 2062 | } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) { |
| 2063 | std::swap(Op1, Op2); // Op1 is the SHL now. |
| 2064 | } else { |
| 2065 | return false; // No match |
| 2066 | } |
| 2067 | |
| 2068 | SDOperand ShlVal = Op1.getOperand(0); |
| 2069 | SDOperand ShlAmt = Op1.getOperand(1); |
| 2070 | SDOperand ShrVal = Op2.getOperand(0); |
| 2071 | SDOperand ShrAmt = Op2.getOperand(1); |
| 2072 | |
Chris Lattner | 29f5819 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 2073 | unsigned RegSize = MVT::getSizeInBits(Op1.getValueType()); |
| 2074 | |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2075 | // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt. |
| 2076 | if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt) |
| 2077 | if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0))) |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2078 | if (SubCST->getValue() == RegSize) { |
| 2079 | // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2080 | // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2081 | if (ShrVal == ShlVal) { |
| 2082 | unsigned Reg, ShAmt; |
| 2083 | if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) { |
| 2084 | Reg = SelectExpr(ShrVal); |
| 2085 | ShAmt = SelectExpr(ShrAmt); |
| 2086 | } else { |
| 2087 | ShAmt = SelectExpr(ShrAmt); |
| 2088 | Reg = SelectExpr(ShrVal); |
| 2089 | } |
| 2090 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 2091 | unsigned Opc = RegSize == 8 ? X86::ROR8rCL : |
| 2092 | (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL); |
| 2093 | BuildMI(BB, Opc, 1, DestReg).addReg(Reg); |
| 2094 | return true; |
| 2095 | } else if (RegSize != 8) { |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2096 | unsigned AReg, BReg; |
| 2097 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2098 | BReg = SelectExpr(ShlVal); |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2099 | AReg = SelectExpr(ShrVal); |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2100 | } else { |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2101 | AReg = SelectExpr(ShrVal); |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2102 | BReg = SelectExpr(ShlVal); |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2103 | } |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2104 | unsigned ShAmt = SelectExpr(ShrAmt); |
| 2105 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 2106 | unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL; |
| 2107 | BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg); |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2108 | return true; |
| 2109 | } |
| 2110 | } |
| 2111 | |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2112 | if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt) |
| 2113 | if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0))) |
| 2114 | if (SubCST->getValue() == RegSize) { |
| 2115 | // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt |
| 2116 | // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt |
| 2117 | if (ShrVal == ShlVal) { |
| 2118 | unsigned Reg, ShAmt; |
| 2119 | if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) { |
| 2120 | Reg = SelectExpr(ShrVal); |
| 2121 | ShAmt = SelectExpr(ShlAmt); |
| 2122 | } else { |
| 2123 | ShAmt = SelectExpr(ShlAmt); |
| 2124 | Reg = SelectExpr(ShrVal); |
| 2125 | } |
| 2126 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 2127 | unsigned Opc = RegSize == 8 ? X86::ROL8rCL : |
| 2128 | (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL); |
| 2129 | BuildMI(BB, Opc, 1, DestReg).addReg(Reg); |
| 2130 | return true; |
| 2131 | } else if (RegSize != 8) { |
| 2132 | unsigned AReg, BReg; |
| 2133 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2134 | AReg = SelectExpr(ShlVal); |
| 2135 | BReg = SelectExpr(ShrVal); |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2136 | } else { |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2137 | BReg = SelectExpr(ShrVal); |
| 2138 | AReg = SelectExpr(ShlVal); |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2139 | } |
| 2140 | unsigned ShAmt = SelectExpr(ShlAmt); |
| 2141 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 2142 | unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL; |
| 2143 | BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg); |
| 2144 | return true; |
| 2145 | } |
| 2146 | } |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2147 | |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2148 | if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt)) |
| 2149 | if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt)) |
| 2150 | if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize) |
| 2151 | if (ShrCst->getValue() == RegSize-ShlCst->getValue()) { |
| 2152 | // (A >> 5) | (A << 27) --> ROR A, 5 |
| 2153 | // (A >> 5) | (B << 27) --> SHRD A, B, 5 |
| 2154 | if (ShrVal == ShlVal) { |
| 2155 | unsigned Reg = SelectExpr(ShrVal); |
| 2156 | unsigned Opc = RegSize == 8 ? X86::ROR8ri : |
| 2157 | (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri); |
| 2158 | BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue()); |
| 2159 | return true; |
| 2160 | } else if (RegSize != 8) { |
| 2161 | unsigned AReg, BReg; |
| 2162 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2163 | BReg = SelectExpr(ShlVal); |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2164 | AReg = SelectExpr(ShrVal); |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2165 | } else { |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2166 | AReg = SelectExpr(ShrVal); |
Chris Lattner | 474aac4 | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 2167 | BReg = SelectExpr(ShlVal); |
Chris Lattner | de87d146 | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 2168 | } |
| 2169 | unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8; |
| 2170 | BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg) |
| 2171 | .addImm(ShrCst->getValue()); |
| 2172 | return true; |
| 2173 | } |
| 2174 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2175 | |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2176 | return false; |
| 2177 | } |
| 2178 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2179 | unsigned ISel::SelectExpr(SDOperand N) { |
| 2180 | unsigned Result; |
| 2181 | unsigned Tmp1, Tmp2, Tmp3; |
| 2182 | unsigned Opc = 0; |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 2183 | SDNode *Node = N.Val; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2184 | SDOperand Op0, Op1; |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 2186 | if (Node->getOpcode() == ISD::CopyFromReg) { |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 2187 | if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) || |
| 2188 | cast<RegSDNode>(Node)->getReg() == X86::ESP) { |
| 2189 | // Just use the specified register as our input. |
| 2190 | return cast<RegSDNode>(Node)->getReg(); |
| 2191 | } |
Chris Lattner | 720a62e | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 2192 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2193 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2194 | unsigned &Reg = ExprMap[N]; |
| 2195 | if (Reg) return Reg; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2196 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2197 | switch (N.getOpcode()) { |
| 2198 | default: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2199 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2200 | MakeReg(N.getValueType()) : 1; |
| 2201 | break; |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 2202 | case X86ISD::TAILCALL: |
| 2203 | case X86ISD::CALL: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2204 | // If this is a call instruction, make sure to prepare ALL of the result |
| 2205 | // values as well as the chain. |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 2206 | ExprMap[N.getValue(0)] = 1; |
| 2207 | if (Node->getNumValues() > 1) { |
| 2208 | Result = MakeReg(Node->getValueType(1)); |
| 2209 | ExprMap[N.getValue(1)] = Result; |
| 2210 | for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2211 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 2212 | } else { |
| 2213 | Result = 1; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2214 | } |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2215 | break; |
| 2216 | case ISD::ADD_PARTS: |
| 2217 | case ISD::SUB_PARTS: |
| 2218 | case ISD::SHL_PARTS: |
| 2219 | case ISD::SRL_PARTS: |
| 2220 | case ISD::SRA_PARTS: |
| 2221 | Result = MakeReg(Node->getValueType(0)); |
| 2222 | ExprMap[N.getValue(0)] = Result; |
| 2223 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 2224 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 2225 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2226 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2227 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2228 | switch (N.getOpcode()) { |
| 2229 | default: |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 2230 | Node->dump(); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2231 | assert(0 && "Node not handled!\n"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2232 | case ISD::FP_EXTEND: |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2233 | assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2234 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2235 | BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1); |
| 2236 | return Result; |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 2237 | case ISD::FP_ROUND: |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2238 | assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32"); |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 2239 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2240 | BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1); |
| 2241 | return Result; |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 2242 | case ISD::CopyFromReg: |
| 2243 | Select(N.getOperand(0)); |
| 2244 | if (Result == 1) { |
| 2245 | Reg = Result = ExprMap[N.getValue(0)] = |
| 2246 | MakeReg(N.getValue(0).getValueType()); |
| 2247 | } |
| 2248 | switch (Node->getValueType(0)) { |
| 2249 | default: assert(0 && "Cannot CopyFromReg this!"); |
| 2250 | case MVT::i1: |
| 2251 | case MVT::i8: |
| 2252 | BuildMI(BB, X86::MOV8rr, 1, |
| 2253 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 2254 | return Result; |
| 2255 | case MVT::i16: |
| 2256 | BuildMI(BB, X86::MOV16rr, 1, |
| 2257 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 2258 | return Result; |
| 2259 | case MVT::i32: |
| 2260 | BuildMI(BB, X86::MOV32rr, 1, |
| 2261 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 2262 | return Result; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2263 | } |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2265 | case ISD::FrameIndex: |
| 2266 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
| 2267 | addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1); |
| 2268 | return Result; |
| 2269 | case ISD::ConstantPool: |
| 2270 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 2271 | addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1); |
| 2272 | return Result; |
| 2273 | case ISD::ConstantFP: |
| 2274 | ContainsFPCode = true; |
| 2275 | Tmp1 = Result; // Intermediate Register |
| 2276 | if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 || |
| 2277 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 2278 | Tmp1 = MakeReg(MVT::f64); |
| 2279 | |
| 2280 | if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) || |
| 2281 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 2282 | BuildMI(BB, X86::FLD0, 0, Tmp1); |
| 2283 | else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) || |
| 2284 | cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0)) |
| 2285 | BuildMI(BB, X86::FLD1, 0, Tmp1); |
| 2286 | else |
| 2287 | assert(0 && "Unexpected constant!"); |
| 2288 | if (Tmp1 != Result) |
| 2289 | BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); |
| 2290 | return Result; |
| 2291 | case ISD::Constant: |
| 2292 | switch (N.getValueType()) { |
| 2293 | default: assert(0 && "Cannot use constants of this type!"); |
| 2294 | case MVT::i1: |
| 2295 | case MVT::i8: Opc = X86::MOV8ri; break; |
| 2296 | case MVT::i16: Opc = X86::MOV16ri; break; |
| 2297 | case MVT::i32: Opc = X86::MOV32ri; break; |
| 2298 | } |
| 2299 | BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue()); |
| 2300 | return Result; |
Chris Lattner | f4b985d | 2005-04-01 22:46:45 +0000 | [diff] [blame] | 2301 | case ISD::UNDEF: |
| 2302 | if (Node->getValueType(0) == MVT::f64) { |
| 2303 | // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES! |
| 2304 | BuildMI(BB, X86::FLD0, 0, Result); |
| 2305 | } else { |
| 2306 | BuildMI(BB, X86::IMPLICIT_DEF, 0, Result); |
| 2307 | } |
| 2308 | return Result; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2309 | case ISD::GlobalAddress: { |
| 2310 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 2311 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 2312 | // the value at address GV, not the value of GV itself. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2313 | if (Subtarget->getIndirectExternAndWeakGlobals() && |
Nate Begeman | f26625e | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 2314 | (GV->hasWeakLinkage() || GV->isExternal())) { |
| 2315 | BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0) |
| 2316 | .addGlobalAddress(GV, false, 0); |
| 2317 | } else { |
| 2318 | BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV); |
| 2319 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2320 | return Result; |
| 2321 | } |
| 2322 | case ISD::ExternalSymbol: { |
| 2323 | const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol(); |
| 2324 | BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym); |
| 2325 | return Result; |
| 2326 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2327 | case ISD::ZERO_EXTEND: { |
| 2328 | int DestIs16 = N.getValueType() == MVT::i16; |
| 2329 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2330 | |
| 2331 | // FIXME: This hack is here for zero extension casts from bool to i8. This |
| 2332 | // would not be needed if bools were promoted by Legalize. |
| 2333 | if (N.getValueType() == MVT::i8) { |
Chris Lattner | b0eef82 | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 2334 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2335 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1); |
| 2336 | return Result; |
| 2337 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2339 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | b0eef82 | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 2340 | static const unsigned Opc[3] = { |
| 2341 | X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8 |
| 2342 | }; |
| 2343 | |
| 2344 | X86AddressMode AM; |
| 2345 | EmitFoldedLoad(N.getOperand(0), AM); |
| 2346 | addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | b0eef82 | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 2348 | return Result; |
| 2349 | } |
| 2350 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2351 | static const unsigned Opc[3] = { |
| 2352 | X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8 |
| 2353 | }; |
Chris Lattner | b0eef82 | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 2354 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2355 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 2356 | return Result; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2357 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2358 | case ISD::SIGN_EXTEND: { |
| 2359 | int DestIs16 = N.getValueType() == MVT::i16; |
| 2360 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
| 2361 | |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2362 | // FIXME: Legalize should promote bools to i8! |
| 2363 | assert(N.getOperand(0).getValueType() != MVT::i1 && |
| 2364 | "Sign extend from bool not implemented!"); |
| 2365 | |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2366 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | b0eef82 | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 2367 | static const unsigned Opc[3] = { |
| 2368 | X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8 |
| 2369 | }; |
| 2370 | |
| 2371 | X86AddressMode AM; |
| 2372 | EmitFoldedLoad(N.getOperand(0), AM); |
| 2373 | addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM); |
| 2374 | return Result; |
| 2375 | } |
| 2376 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2377 | static const unsigned Opc[3] = { |
| 2378 | X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8 |
| 2379 | }; |
| 2380 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2381 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 2382 | return Result; |
| 2383 | } |
| 2384 | case ISD::TRUNCATE: |
Chris Lattner | b7fe57a | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 2385 | // Fold TRUNCATE (LOAD P) into a smaller load from P. |
Chris Lattner | 14947c3 | 2005-01-18 20:05:56 +0000 | [diff] [blame] | 2386 | // FIXME: This should be performed by the DAGCombiner. |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2387 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | b7fe57a | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 2388 | switch (N.getValueType()) { |
| 2389 | default: assert(0 && "Unknown truncate!"); |
| 2390 | case MVT::i1: |
| 2391 | case MVT::i8: Opc = X86::MOV8rm; break; |
| 2392 | case MVT::i16: Opc = X86::MOV16rm; break; |
| 2393 | } |
| 2394 | X86AddressMode AM; |
| 2395 | EmitFoldedLoad(N.getOperand(0), AM); |
| 2396 | addFullAddress(BuildMI(BB, Opc, 4, Result), AM); |
| 2397 | return Result; |
| 2398 | } |
| 2399 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2400 | // Handle cast of LARGER int to SMALLER int using a move to EAX followed by |
| 2401 | // a move out of AX or AL. |
| 2402 | switch (N.getOperand(0).getValueType()) { |
| 2403 | default: assert(0 && "Unknown truncate!"); |
| 2404 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 2405 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 2406 | case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break; |
| 2407 | } |
| 2408 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2409 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 2410 | |
| 2411 | switch (N.getValueType()) { |
| 2412 | default: assert(0 && "Unknown truncate!"); |
| 2413 | case MVT::i1: |
| 2414 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 2415 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 2416 | } |
| 2417 | BuildMI(BB, Opc, 1, Result).addReg(Tmp2); |
| 2418 | return Result; |
| 2419 | |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 2420 | case ISD::SINT_TO_FP: { |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2421 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2422 | unsigned PromoteOpcode = 0; |
| 2423 | |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 2424 | // We can handle any sint to fp with the direct sse conversion instructions. |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2425 | if (X86ScalarSSE) { |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 2426 | Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2427 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2428 | return Result; |
| 2429 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2430 | |
Chris Lattner | e44e6d1 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 2431 | ContainsFPCode = true; |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2432 | |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2433 | // Spill the integer to memory and reload it from there. |
Nate Begeman | 7e74c83 | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 2434 | MVT::ValueType SrcTy = N.getOperand(0).getValueType(); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2435 | unsigned Size = MVT::getSizeInBits(SrcTy)/8; |
| 2436 | MachineFunction *F = BB->getParent(); |
| 2437 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 2438 | |
| 2439 | switch (SrcTy) { |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2440 | case MVT::i32: |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 2441 | addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2442 | addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx); |
| 2443 | break; |
| 2444 | case MVT::i16: |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 2445 | addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2446 | addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx); |
| 2447 | break; |
| 2448 | default: break; // No promotion required. |
| 2449 | } |
Chris Lattner | 507a275 | 2005-07-16 00:28:20 +0000 | [diff] [blame] | 2450 | return Result; |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2451 | } |
Chris Lattner | bc85c32 | 2005-07-29 01:00:29 +0000 | [diff] [blame] | 2452 | case ISD::FP_TO_SINT: { |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2453 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2454 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2455 | // If the target supports SSE2 and is performing FP operations in SSE regs |
| 2456 | // instead of the FP stack, then we can use the efficient CVTSS2SI and |
| 2457 | // CVTSD2SI instructions. |
Chris Lattner | bc85c32 | 2005-07-29 01:00:29 +0000 | [diff] [blame] | 2458 | if (X86ScalarSSE) { |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2459 | if (MVT::f32 == N.getOperand(0).getValueType()) { |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 2460 | BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2461 | } else if (MVT::f64 == N.getOperand(0).getValueType()) { |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 2462 | BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2463 | } else { |
| 2464 | assert(0 && "Not an f32 or f64?"); |
| 2465 | abort(); |
| 2466 | } |
| 2467 | return Result; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2468 | } |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2470 | // Change the floating point control register to use "round towards zero" |
| 2471 | // mode when truncating to an integer value. |
| 2472 | // |
| 2473 | MachineFunction *F = BB->getParent(); |
| 2474 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 2475 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 2476 | |
| 2477 | // Load the old value of the high byte of the control word... |
| 2478 | unsigned HighPartOfCW = MakeReg(MVT::i8); |
| 2479 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW), |
| 2480 | CWFrameIdx, 1); |
| 2481 | |
| 2482 | // Set the high part to be round to zero... |
| 2483 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 2484 | CWFrameIdx, 1).addImm(12); |
| 2485 | |
| 2486 | // Reload the modified control word now... |
| 2487 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2489 | // Restore the memory image of control word to original value |
| 2490 | addFrameReference(BuildMI(BB, X86::MOV8mr, 5), |
| 2491 | CWFrameIdx, 1).addReg(HighPartOfCW); |
| 2492 | |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2493 | // Spill the integer to memory and reload it from there. |
Chris Lattner | bc85c32 | 2005-07-29 01:00:29 +0000 | [diff] [blame] | 2494 | unsigned Size = MVT::getSizeInBits(Node->getValueType(0))/8; |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2495 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 2496 | |
Chris Lattner | bc85c32 | 2005-07-29 01:00:29 +0000 | [diff] [blame] | 2497 | switch (Node->getValueType(0)) { |
| 2498 | default: assert(0 && "Unsupported store class!"); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2499 | case MVT::i16: |
| 2500 | addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1); |
| 2501 | break; |
| 2502 | case MVT::i32: |
Chris Lattner | 66d3430 | 2005-01-09 19:49:59 +0000 | [diff] [blame] | 2503 | addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2504 | break; |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 2505 | } |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2506 | |
| 2507 | switch (Node->getValueType(0)) { |
| 2508 | default: |
| 2509 | assert(0 && "Unknown integer type!"); |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2510 | case MVT::i32: |
| 2511 | addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx); |
| 2512 | break; |
| 2513 | case MVT::i16: |
| 2514 | addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx); |
| 2515 | break; |
Chris Lattner | 282781c | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | // Reload the original control word now. |
| 2519 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 2520 | return Result; |
| 2521 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2522 | case ISD::ADD: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2523 | Op0 = N.getOperand(0); |
| 2524 | Op1 = N.getOperand(1); |
| 2525 | |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2526 | if (isFoldableLoad(Op0, Op1, true)) { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2527 | std::swap(Op0, Op1); |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2528 | goto FoldAdd; |
| 2529 | } |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2531 | if (isFoldableLoad(Op1, Op0, true)) { |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2532 | FoldAdd: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2533 | switch (N.getValueType()) { |
| 2534 | default: assert(0 && "Cannot add this type!"); |
| 2535 | case MVT::i1: |
| 2536 | case MVT::i8: Opc = X86::ADD8rm; break; |
| 2537 | case MVT::i16: Opc = X86::ADD16rm; break; |
| 2538 | case MVT::i32: Opc = X86::ADD32rm; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2539 | case MVT::f32: Opc = X86::ADDSSrm; break; |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2540 | case MVT::f64: |
| 2541 | // For F64, handle promoted load operations (from F32) as well! |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2542 | if (X86ScalarSSE) { |
| 2543 | assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted"); |
| 2544 | Opc = X86::ADDSDrm; |
| 2545 | } else { |
| 2546 | Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m; |
| 2547 | } |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2548 | break; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2549 | } |
| 2550 | X86AddressMode AM; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2551 | EmitFoldedLoad(Op1, AM); |
| 2552 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2553 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2554 | return Result; |
| 2555 | } |
| 2556 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2557 | // See if we can codegen this as an LEA to fold operations together. |
| 2558 | if (N.getValueType() == MVT::i32) { |
Chris Lattner | d7f9395 | 2005-01-18 02:25:52 +0000 | [diff] [blame] | 2559 | ExprMap.erase(N); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 2560 | X86ISelAddressMode AM; |
Chris Lattner | d7f9395 | 2005-01-18 02:25:52 +0000 | [diff] [blame] | 2561 | MatchAddress(N, AM); |
| 2562 | ExprMap[N] = Result; |
| 2563 | |
| 2564 | // If this is not just an add, emit the LEA. For a simple add (like |
| 2565 | // reg+reg or reg+imm), we just emit an add. It might be a good idea to |
| 2566 | // leave this as LEA, then peephole it to 'ADD' after two address elim |
| 2567 | // happens. |
| 2568 | if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase|| |
| 2569 | AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) { |
| 2570 | X86AddressMode XAM = SelectAddrExprs(AM); |
| 2571 | addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM); |
| 2572 | return Result; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2573 | } |
| 2574 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2576 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2577 | Opc = 0; |
| 2578 | if (CN->getValue() == 1) { // add X, 1 -> inc X |
| 2579 | switch (N.getValueType()) { |
| 2580 | default: assert(0 && "Cannot integer add this type!"); |
| 2581 | case MVT::i8: Opc = X86::INC8r; break; |
| 2582 | case MVT::i16: Opc = X86::INC16r; break; |
| 2583 | case MVT::i32: Opc = X86::INC32r; break; |
| 2584 | } |
| 2585 | } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X |
| 2586 | switch (N.getValueType()) { |
| 2587 | default: assert(0 && "Cannot integer add this type!"); |
| 2588 | case MVT::i8: Opc = X86::DEC8r; break; |
| 2589 | case MVT::i16: Opc = X86::DEC16r; break; |
| 2590 | case MVT::i32: Opc = X86::DEC32r; break; |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | if (Opc) { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2595 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2596 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2597 | return Result; |
| 2598 | } |
| 2599 | |
| 2600 | switch (N.getValueType()) { |
| 2601 | default: assert(0 && "Cannot add this type!"); |
| 2602 | case MVT::i8: Opc = X86::ADD8ri; break; |
| 2603 | case MVT::i16: Opc = X86::ADD16ri; break; |
| 2604 | case MVT::i32: Opc = X86::ADD32ri; break; |
| 2605 | } |
| 2606 | if (Opc) { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2607 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2608 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2609 | return Result; |
| 2610 | } |
| 2611 | } |
| 2612 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2613 | switch (N.getValueType()) { |
| 2614 | default: assert(0 && "Cannot add this type!"); |
| 2615 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 2616 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 2617 | case MVT::i32: Opc = X86::ADD32rr; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2618 | case MVT::f32: Opc = X86::ADDSSrr; break; |
| 2619 | case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2620 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2621 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2622 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2623 | Tmp1 = SelectExpr(Op0); |
| 2624 | Tmp2 = SelectExpr(Op1); |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2625 | } else { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2626 | Tmp2 = SelectExpr(Op1); |
| 2627 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2630 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2631 | return Result; |
Chris Lattner | 0e0b599 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2632 | |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2633 | case ISD::FSQRT: |
| 2634 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 2635 | if (X86ScalarSSE) { |
| 2636 | Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr; |
| 2637 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2638 | } else { |
| 2639 | BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); |
| 2640 | } |
| 2641 | return Result; |
| 2642 | |
| 2643 | // FIXME: |
| 2644 | // Once we can spill 16 byte constants into the constant pool, we can |
| 2645 | // implement SSE equivalents of FABS and FCHS. |
Chris Lattner | 0e0b599 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2646 | case ISD::FABS: |
Chris Lattner | 0e0b599 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2647 | case ISD::FNEG: |
Chris Lattner | db68d39 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 2648 | case ISD::FSIN: |
| 2649 | case ISD::FCOS: |
Chris Lattner | 014d2c4 | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2650 | assert(N.getValueType()==MVT::f64 && "Illegal type for this operation"); |
Chris Lattner | 0e0b599 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2651 | Tmp1 = SelectExpr(Node->getOperand(0)); |
Chris Lattner | 014d2c4 | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2652 | switch (N.getOpcode()) { |
| 2653 | default: assert(0 && "Unreachable!"); |
| 2654 | case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break; |
| 2655 | case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break; |
Chris Lattner | db68d39 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 2656 | case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break; |
| 2657 | case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break; |
Chris Lattner | 014d2c4 | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2658 | } |
Chris Lattner | 0e0b599 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2659 | return Result; |
| 2660 | |
Chris Lattner | 4fbb4af | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2661 | case ISD::MULHU: |
| 2662 | switch (N.getValueType()) { |
| 2663 | default: assert(0 && "Unsupported VT!"); |
| 2664 | case MVT::i8: Tmp2 = X86::MUL8r; break; |
| 2665 | case MVT::i16: Tmp2 = X86::MUL16r; break; |
| 2666 | case MVT::i32: Tmp2 = X86::MUL32r; break; |
| 2667 | } |
| 2668 | // FALL THROUGH |
| 2669 | case ISD::MULHS: { |
| 2670 | unsigned MovOpc, LowReg, HiReg; |
| 2671 | switch (N.getValueType()) { |
| 2672 | default: assert(0 && "Unsupported VT!"); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2673 | case MVT::i8: |
Chris Lattner | 4fbb4af | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2674 | MovOpc = X86::MOV8rr; |
| 2675 | LowReg = X86::AL; |
| 2676 | HiReg = X86::AH; |
| 2677 | Opc = X86::IMUL8r; |
| 2678 | break; |
| 2679 | case MVT::i16: |
| 2680 | MovOpc = X86::MOV16rr; |
| 2681 | LowReg = X86::AX; |
| 2682 | HiReg = X86::DX; |
| 2683 | Opc = X86::IMUL16r; |
| 2684 | break; |
| 2685 | case MVT::i32: |
| 2686 | MovOpc = X86::MOV32rr; |
| 2687 | LowReg = X86::EAX; |
| 2688 | HiReg = X86::EDX; |
| 2689 | Opc = X86::IMUL32r; |
| 2690 | break; |
| 2691 | } |
| 2692 | if (Node->getOpcode() != ISD::MULHS) |
| 2693 | Opc = Tmp2; // Get the MULHU opcode. |
| 2694 | |
| 2695 | Op0 = Node->getOperand(0); |
| 2696 | Op1 = Node->getOperand(1); |
| 2697 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2698 | Tmp1 = SelectExpr(Op0); |
| 2699 | Tmp2 = SelectExpr(Op1); |
| 2700 | } else { |
| 2701 | Tmp2 = SelectExpr(Op1); |
| 2702 | Tmp1 = SelectExpr(Op0); |
| 2703 | } |
| 2704 | |
| 2705 | // FIXME: Implement folding of loads into the memory operands here! |
| 2706 | BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1); |
| 2707 | BuildMI(BB, Opc, 1).addReg(Tmp2); |
| 2708 | BuildMI(BB, MovOpc, 1, Result).addReg(HiReg); |
| 2709 | return Result; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2710 | } |
Chris Lattner | 4fbb4af | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2712 | case ISD::SUB: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2713 | case ISD::MUL: |
| 2714 | case ISD::AND: |
| 2715 | case ISD::OR: |
Chris Lattner | efe9020 | 2005-01-12 04:23:22 +0000 | [diff] [blame] | 2716 | case ISD::XOR: { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2717 | static const unsigned SUBTab[] = { |
| 2718 | X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0, |
| 2719 | X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m, |
| 2720 | X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB, |
| 2721 | }; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2722 | static const unsigned SSE_SUBTab[] = { |
| 2723 | X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0, |
| 2724 | X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm, |
| 2725 | X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr, |
| 2726 | }; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2727 | static const unsigned MULTab[] = { |
| 2728 | 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0, |
| 2729 | 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m, |
| 2730 | 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL, |
| 2731 | }; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2732 | static const unsigned SSE_MULTab[] = { |
| 2733 | 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0, |
| 2734 | 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm, |
| 2735 | 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr, |
| 2736 | }; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2737 | static const unsigned ANDTab[] = { |
| 2738 | X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0, |
| 2739 | X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0, |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2740 | X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0, |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2741 | }; |
| 2742 | static const unsigned ORTab[] = { |
| 2743 | X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0, |
| 2744 | X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0, |
| 2745 | X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0, |
| 2746 | }; |
| 2747 | static const unsigned XORTab[] = { |
| 2748 | X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0, |
| 2749 | X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0, |
| 2750 | X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0, |
| 2751 | }; |
| 2752 | |
| 2753 | Op0 = Node->getOperand(0); |
| 2754 | Op1 = Node->getOperand(1); |
| 2755 | |
Chris Lattner | 29f5819 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 2756 | if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse()) |
| 2757 | if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates. |
Chris Lattner | 41fe201 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2758 | return Result; |
| 2759 | |
| 2760 | if (Node->getOpcode() == ISD::SUB) |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2761 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0))) |
| 2762 | if (CN->isNullValue()) { // 0 - N -> neg N |
| 2763 | switch (N.getValueType()) { |
| 2764 | default: assert(0 && "Cannot sub this type!"); |
| 2765 | case MVT::i1: |
| 2766 | case MVT::i8: Opc = X86::NEG8r; break; |
| 2767 | case MVT::i16: Opc = X86::NEG16r; break; |
| 2768 | case MVT::i32: Opc = X86::NEG32r; break; |
| 2769 | } |
| 2770 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2771 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2772 | return Result; |
| 2773 | } |
| 2774 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2775 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
| 2776 | if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) { |
Chris Lattner | 0cd6b9a | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2777 | Opc = 0; |
Chris Lattner | 9d7cf99 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2778 | switch (N.getValueType()) { |
| 2779 | default: assert(0 && "Cannot add this type!"); |
Chris Lattner | 0cd6b9a | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2780 | case MVT::i1: break; // Not supported, don't invert upper bits! |
Chris Lattner | 9d7cf99 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2781 | case MVT::i8: Opc = X86::NOT8r; break; |
| 2782 | case MVT::i16: Opc = X86::NOT16r; break; |
| 2783 | case MVT::i32: Opc = X86::NOT32r; break; |
| 2784 | } |
Chris Lattner | 0cd6b9a | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2785 | if (Opc) { |
| 2786 | Tmp1 = SelectExpr(Op0); |
| 2787 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2788 | return Result; |
| 2789 | } |
Chris Lattner | 9d7cf99 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Chris Lattner | b72ea1b | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2792 | // Fold common multiplies into LEA instructions. |
| 2793 | if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) { |
| 2794 | switch ((int)CN->getValue()) { |
| 2795 | default: break; |
| 2796 | case 3: |
| 2797 | case 5: |
| 2798 | case 9: |
Chris Lattner | b72ea1b | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2799 | // Remove N from exprmap so SelectAddress doesn't get confused. |
| 2800 | ExprMap.erase(N); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 2801 | X86AddressMode AM; |
Chris Lattner | b72ea1b | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2802 | SelectAddress(N, AM); |
| 2803 | // Restore it to the map. |
| 2804 | ExprMap[N] = Result; |
| 2805 | addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM); |
| 2806 | return Result; |
| 2807 | } |
| 2808 | } |
| 2809 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2810 | switch (N.getValueType()) { |
Chris Lattner | 9d7cf99 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2811 | default: assert(0 && "Cannot xor this type!"); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2812 | case MVT::i1: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2813 | case MVT::i8: Opc = 0; break; |
| 2814 | case MVT::i16: Opc = 1; break; |
| 2815 | case MVT::i32: Opc = 2; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2816 | } |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2817 | switch (Node->getOpcode()) { |
| 2818 | default: assert(0 && "Unreachable!"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2819 | case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break; |
| 2820 | case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2821 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2822 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2823 | case ISD::XOR: Opc = XORTab[Opc]; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2824 | } |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2825 | if (Opc) { // Can't fold MUL:i8 R, imm |
| 2826 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2827 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2828 | return Result; |
| 2829 | } |
| 2830 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2831 | |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2832 | if (isFoldableLoad(Op0, Op1, true)) |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2833 | if (Node->getOpcode() != ISD::SUB) { |
| 2834 | std::swap(Op0, Op1); |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2835 | goto FoldOps; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2836 | } else { |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2837 | // For FP, emit 'reverse' subract, with a memory operand. |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2838 | if (N.getValueType() == MVT::f64 && !X86ScalarSSE) { |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2839 | if (Op0.getOpcode() == ISD::EXTLOAD) |
| 2840 | Opc = X86::FSUBR32m; |
| 2841 | else |
| 2842 | Opc = X86::FSUBR64m; |
| 2843 | |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2844 | X86AddressMode AM; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2845 | EmitFoldedLoad(Op0, AM); |
| 2846 | Tmp1 = SelectExpr(Op1); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2847 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2848 | return Result; |
| 2849 | } |
| 2850 | } |
| 2851 | |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2852 | if (isFoldableLoad(Op1, Op0, true)) { |
Chris Lattner | a56d29d | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2853 | FoldOps: |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2854 | switch (N.getValueType()) { |
| 2855 | default: assert(0 && "Cannot operate on this type!"); |
| 2856 | case MVT::i1: |
| 2857 | case MVT::i8: Opc = 5; break; |
| 2858 | case MVT::i16: Opc = 6; break; |
| 2859 | case MVT::i32: Opc = 7; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2860 | case MVT::f32: Opc = 8; break; |
Chris Lattner | 30607ec | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2861 | // For F64, handle promoted load operations (from F32) as well! |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2862 | case MVT::f64: |
| 2863 | assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) && |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2864 | "SSE load should have been promoted"); |
| 2865 | Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2866 | } |
| 2867 | switch (Node->getOpcode()) { |
| 2868 | default: assert(0 && "Unreachable!"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2869 | case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break; |
| 2870 | case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2871 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2872 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2873 | case ISD::XOR: Opc = XORTab[Opc]; break; |
| 2874 | } |
| 2875 | |
| 2876 | X86AddressMode AM; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2877 | EmitFoldedLoad(Op1, AM); |
| 2878 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2879 | if (Opc) { |
| 2880 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2881 | } else { |
| 2882 | assert(Node->getOpcode() == ISD::MUL && |
| 2883 | N.getValueType() == MVT::i8 && "Unexpected situation!"); |
| 2884 | // Must use the MUL instruction, which forces use of AL. |
| 2885 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 2886 | addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM); |
| 2887 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 2888 | } |
| 2889 | return Result; |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2890 | } |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2891 | |
| 2892 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2893 | Tmp1 = SelectExpr(Op0); |
| 2894 | Tmp2 = SelectExpr(Op1); |
| 2895 | } else { |
| 2896 | Tmp2 = SelectExpr(Op1); |
| 2897 | Tmp1 = SelectExpr(Op0); |
| 2898 | } |
| 2899 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2900 | switch (N.getValueType()) { |
| 2901 | default: assert(0 && "Cannot add this type!"); |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2902 | case MVT::i1: |
| 2903 | case MVT::i8: Opc = 10; break; |
| 2904 | case MVT::i16: Opc = 11; break; |
| 2905 | case MVT::i32: Opc = 12; break; |
| 2906 | case MVT::f32: Opc = 13; break; |
| 2907 | case MVT::f64: Opc = 14; break; |
| 2908 | } |
| 2909 | switch (Node->getOpcode()) { |
| 2910 | default: assert(0 && "Unreachable!"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 2911 | case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break; |
| 2912 | case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break; |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2913 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2914 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2915 | case ISD::XOR: Opc = XORTab[Opc]; break; |
| 2916 | } |
| 2917 | if (Opc) { |
| 2918 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2919 | } else { |
| 2920 | assert(Node->getOpcode() == ISD::MUL && |
| 2921 | N.getValueType() == MVT::i8 && "Unexpected situation!"); |
Chris Lattner | 750d38b | 2005-01-10 20:55:48 +0000 | [diff] [blame] | 2922 | // Must use the MUL instruction, which forces use of AL. |
| 2923 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 2924 | BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2); |
| 2925 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2926 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2927 | return Result; |
Chris Lattner | efe9020 | 2005-01-12 04:23:22 +0000 | [diff] [blame] | 2928 | } |
Chris Lattner | 2a631fa | 2005-01-20 18:53:00 +0000 | [diff] [blame] | 2929 | case ISD::ADD_PARTS: |
| 2930 | case ISD::SUB_PARTS: { |
| 2931 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 2932 | "Not an i64 add/sub!"); |
| 2933 | // Emit all of the operands. |
| 2934 | std::vector<unsigned> InVals; |
| 2935 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 2936 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 2937 | if (N.getOpcode() == ISD::ADD_PARTS) { |
| 2938 | BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 2939 | BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]); |
| 2940 | } else { |
| 2941 | BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 2942 | BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]); |
| 2943 | } |
| 2944 | return Result+N.ResNo; |
| 2945 | } |
| 2946 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2947 | case ISD::SHL_PARTS: |
| 2948 | case ISD::SRA_PARTS: |
| 2949 | case ISD::SRL_PARTS: { |
| 2950 | assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 && |
| 2951 | "Not an i64 shift!"); |
| 2952 | unsigned ShiftOpLo = SelectExpr(N.getOperand(0)); |
| 2953 | unsigned ShiftOpHi = SelectExpr(N.getOperand(1)); |
| 2954 | unsigned TmpReg = MakeReg(MVT::i32); |
| 2955 | if (N.getOpcode() == ISD::SRA_PARTS) { |
| 2956 | // If this is a SHR of a Long, then we need to do funny sign extension |
| 2957 | // stuff. TmpReg gets the value to use as the high-part if we are |
| 2958 | // shifting more than 32 bits. |
| 2959 | BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31); |
| 2960 | } else { |
| 2961 | // Other shifts use a fixed zero value if the shift is more than 32 bits. |
| 2962 | BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0); |
| 2963 | } |
| 2964 | |
| 2965 | // Initialize CL with the shift amount. |
| 2966 | unsigned ShiftAmountReg = SelectExpr(N.getOperand(2)); |
| 2967 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg); |
| 2968 | |
| 2969 | unsigned TmpReg2 = MakeReg(MVT::i32); |
| 2970 | unsigned TmpReg3 = MakeReg(MVT::i32); |
| 2971 | if (N.getOpcode() == ISD::SHL_PARTS) { |
| 2972 | // TmpReg2 = shld inHi, inLo |
| 2973 | BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi) |
| 2974 | .addReg(ShiftOpLo); |
| 2975 | // TmpReg3 = shl inLo, CL |
| 2976 | BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2977 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2978 | // Set the flags to indicate whether the shift was by more than 32 bits. |
| 2979 | BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2981 | // DestHi = (>32) ? TmpReg3 : TmpReg2; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2982 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2983 | Result+1).addReg(TmpReg2).addReg(TmpReg3); |
| 2984 | // DestLo = (>32) ? TmpReg : TmpReg3; |
| 2985 | BuildMI(BB, X86::CMOVNE32rr, 2, |
| 2986 | Result).addReg(TmpReg3).addReg(TmpReg); |
| 2987 | } else { |
| 2988 | // TmpReg2 = shrd inLo, inHi |
| 2989 | BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo) |
| 2990 | .addReg(ShiftOpHi); |
| 2991 | // TmpReg3 = s[ah]r inHi, CL |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2992 | BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2993 | : X86::SHR32rCL, 1, TmpReg3) |
| 2994 | .addReg(ShiftOpHi); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2995 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2996 | // Set the flags to indicate whether the shift was by more than 32 bits. |
| 2997 | BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2999 | // DestLo = (>32) ? TmpReg3 : TmpReg2; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3000 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 3001 | Result).addReg(TmpReg2).addReg(TmpReg3); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 3003 | // DestHi = (>32) ? TmpReg : TmpReg3; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3004 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | a31d4c7 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 3005 | Result+1).addReg(TmpReg3).addReg(TmpReg); |
| 3006 | } |
| 3007 | return Result+N.ResNo; |
| 3008 | } |
| 3009 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3010 | case ISD::SELECT: |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3011 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 3012 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3013 | Tmp3 = SelectExpr(N.getOperand(2)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3014 | } else { |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3015 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 3016 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3017 | } |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3018 | EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result); |
| 3019 | return Result; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3020 | |
| 3021 | case ISD::SDIV: |
| 3022 | case ISD::UDIV: |
| 3023 | case ISD::SREM: |
| 3024 | case ISD::UREM: { |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3025 | assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) && |
| 3026 | "We don't support this operator!"); |
| 3027 | |
Chris Lattner | 60c23bd | 2005-04-13 03:29:53 +0000 | [diff] [blame] | 3028 | if (N.getOpcode() == ISD::SDIV) { |
Chris Lattner | 1b20615 | 2005-01-25 20:35:10 +0000 | [diff] [blame] | 3029 | // We can fold loads into FpDIVs, but not really into any others. |
Nate Begeman | fcd2f76 | 2005-07-07 06:32:01 +0000 | [diff] [blame] | 3030 | if (N.getValueType() == MVT::f64 && !X86ScalarSSE) { |
Chris Lattner | 1b20615 | 2005-01-25 20:35:10 +0000 | [diff] [blame] | 3031 | // Check for reversed and unreversed DIV. |
| 3032 | if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) { |
| 3033 | if (N.getOperand(0).getOpcode() == ISD::EXTLOAD) |
| 3034 | Opc = X86::FDIVR32m; |
| 3035 | else |
| 3036 | Opc = X86::FDIVR64m; |
| 3037 | X86AddressMode AM; |
| 3038 | EmitFoldedLoad(N.getOperand(0), AM); |
| 3039 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3040 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 3041 | return Result; |
| 3042 | } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) && |
| 3043 | N.getOperand(1).getOpcode() == ISD::LOAD) { |
| 3044 | if (N.getOperand(1).getOpcode() == ISD::EXTLOAD) |
| 3045 | Opc = X86::FDIV32m; |
| 3046 | else |
| 3047 | Opc = X86::FDIV64m; |
| 3048 | X86AddressMode AM; |
| 3049 | EmitFoldedLoad(N.getOperand(1), AM); |
| 3050 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3051 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 3052 | return Result; |
| 3053 | } |
| 3054 | } |
| 3055 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3056 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 3057 | // FIXME: These special cases should be handled by the lowering impl! |
| 3058 | unsigned RHS = CN->getValue(); |
| 3059 | bool isNeg = false; |
| 3060 | if ((int)RHS < 0) { |
| 3061 | isNeg = true; |
| 3062 | RHS = -RHS; |
| 3063 | } |
| 3064 | if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2? |
| 3065 | unsigned Log = log2(RHS); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3066 | unsigned SAROpc, SHROpc, ADDOpc, NEGOpc; |
| 3067 | switch (N.getValueType()) { |
| 3068 | default: assert("Unknown type to signed divide!"); |
| 3069 | case MVT::i8: |
| 3070 | SAROpc = X86::SAR8ri; |
| 3071 | SHROpc = X86::SHR8ri; |
| 3072 | ADDOpc = X86::ADD8rr; |
| 3073 | NEGOpc = X86::NEG8r; |
| 3074 | break; |
| 3075 | case MVT::i16: |
| 3076 | SAROpc = X86::SAR16ri; |
| 3077 | SHROpc = X86::SHR16ri; |
| 3078 | ADDOpc = X86::ADD16rr; |
| 3079 | NEGOpc = X86::NEG16r; |
| 3080 | break; |
| 3081 | case MVT::i32: |
| 3082 | SAROpc = X86::SAR32ri; |
| 3083 | SHROpc = X86::SHR32ri; |
| 3084 | ADDOpc = X86::ADD32rr; |
| 3085 | NEGOpc = X86::NEG32r; |
| 3086 | break; |
| 3087 | } |
Chris Lattner | 7d387d2 | 2005-05-13 21:48:20 +0000 | [diff] [blame] | 3088 | unsigned RegSize = MVT::getSizeInBits(N.getValueType()); |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3089 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 79e9fa5 | 2005-05-13 21:50:27 +0000 | [diff] [blame] | 3090 | unsigned TmpReg; |
| 3091 | if (Log != 1) { |
| 3092 | TmpReg = MakeReg(N.getValueType()); |
| 3093 | BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1); |
| 3094 | } else { |
| 3095 | TmpReg = Tmp1; |
| 3096 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3097 | unsigned TmpReg2 = MakeReg(N.getValueType()); |
Chris Lattner | 7d387d2 | 2005-05-13 21:48:20 +0000 | [diff] [blame] | 3098 | BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3099 | unsigned TmpReg3 = MakeReg(N.getValueType()); |
| 3100 | BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3101 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3102 | unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result; |
| 3103 | BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log); |
| 3104 | if (isNeg) |
| 3105 | BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4); |
| 3106 | return Result; |
| 3107 | } |
| 3108 | } |
Chris Lattner | 60c23bd | 2005-04-13 03:29:53 +0000 | [diff] [blame] | 3109 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3111 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3112 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3113 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3114 | } else { |
| 3115 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3116 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3117 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3118 | |
| 3119 | bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM; |
| 3120 | bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV; |
| 3121 | unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode; |
| 3122 | switch (N.getValueType()) { |
| 3123 | default: assert(0 && "Cannot sdiv this type!"); |
| 3124 | case MVT::i8: |
| 3125 | DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r; |
| 3126 | LoReg = X86::AL; |
| 3127 | HiReg = X86::AH; |
| 3128 | MovOpcode = X86::MOV8rr; |
| 3129 | ClrOpcode = X86::MOV8ri; |
| 3130 | SExtOpcode = X86::CBW; |
| 3131 | break; |
| 3132 | case MVT::i16: |
| 3133 | DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r; |
| 3134 | LoReg = X86::AX; |
| 3135 | HiReg = X86::DX; |
| 3136 | MovOpcode = X86::MOV16rr; |
| 3137 | ClrOpcode = X86::MOV16ri; |
| 3138 | SExtOpcode = X86::CWD; |
| 3139 | break; |
| 3140 | case MVT::i32: |
| 3141 | DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r; |
Chris Lattner | 3278ce8 | 2005-01-12 03:16:09 +0000 | [diff] [blame] | 3142 | LoReg = X86::EAX; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3143 | HiReg = X86::EDX; |
| 3144 | MovOpcode = X86::MOV32rr; |
| 3145 | ClrOpcode = X86::MOV32ri; |
| 3146 | SExtOpcode = X86::CDQ; |
| 3147 | break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3148 | case MVT::f32: |
| 3149 | BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 3150 | return Result; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3151 | case MVT::f64: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3152 | Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV; |
| 3153 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3154 | return Result; |
| 3155 | } |
| 3156 | |
| 3157 | // Set up the low part. |
| 3158 | BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1); |
| 3159 | |
| 3160 | if (isSigned) { |
| 3161 | // Sign extend the low part into the high part. |
| 3162 | BuildMI(BB, SExtOpcode, 0); |
| 3163 | } else { |
| 3164 | // Zero out the high part, effectively zero extending the input. |
| 3165 | BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0); |
| 3166 | } |
| 3167 | |
| 3168 | // Emit the DIV/IDIV instruction. |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3169 | BuildMI(BB, DivOpcode, 1).addReg(Tmp2); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3170 | |
| 3171 | // Get the result of the divide or rem. |
| 3172 | BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg); |
| 3173 | return Result; |
| 3174 | } |
| 3175 | |
| 3176 | case ISD::SHL: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3177 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 62b2242 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 3178 | if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y |
| 3179 | switch (N.getValueType()) { |
| 3180 | default: assert(0 && "Cannot shift this type!"); |
| 3181 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 3182 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 3183 | case MVT::i32: Opc = X86::ADD32rr; break; |
| 3184 | } |
| 3185 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3186 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 3187 | return Result; |
| 3188 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3190 | switch (N.getValueType()) { |
| 3191 | default: assert(0 && "Cannot shift this type!"); |
| 3192 | case MVT::i8: Opc = X86::SHL8ri; break; |
| 3193 | case MVT::i16: Opc = X86::SHL16ri; break; |
| 3194 | case MVT::i32: Opc = X86::SHL32ri; break; |
| 3195 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3196 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3197 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 3198 | return Result; |
| 3199 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3200 | |
| 3201 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3202 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3203 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3204 | } else { |
| 3205 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3206 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3207 | } |
| 3208 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3209 | switch (N.getValueType()) { |
| 3210 | default: assert(0 && "Cannot shift this type!"); |
| 3211 | case MVT::i8 : Opc = X86::SHL8rCL; break; |
| 3212 | case MVT::i16: Opc = X86::SHL16rCL; break; |
| 3213 | case MVT::i32: Opc = X86::SHL32rCL; break; |
| 3214 | } |
| 3215 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 3216 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 3217 | return Result; |
| 3218 | case ISD::SRL: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3219 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 3220 | switch (N.getValueType()) { |
| 3221 | default: assert(0 && "Cannot shift this type!"); |
| 3222 | case MVT::i8: Opc = X86::SHR8ri; break; |
| 3223 | case MVT::i16: Opc = X86::SHR16ri; break; |
| 3224 | case MVT::i32: Opc = X86::SHR32ri; break; |
| 3225 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3226 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3227 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 3228 | return Result; |
| 3229 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3230 | |
| 3231 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3232 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3233 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3234 | } else { |
| 3235 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3236 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3237 | } |
| 3238 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3239 | switch (N.getValueType()) { |
| 3240 | default: assert(0 && "Cannot shift this type!"); |
| 3241 | case MVT::i8 : Opc = X86::SHR8rCL; break; |
| 3242 | case MVT::i16: Opc = X86::SHR16rCL; break; |
| 3243 | case MVT::i32: Opc = X86::SHR32rCL; break; |
| 3244 | } |
| 3245 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 3246 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 3247 | return Result; |
| 3248 | case ISD::SRA: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3249 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 3250 | switch (N.getValueType()) { |
| 3251 | default: assert(0 && "Cannot shift this type!"); |
| 3252 | case MVT::i8: Opc = X86::SAR8ri; break; |
| 3253 | case MVT::i16: Opc = X86::SAR16ri; break; |
| 3254 | case MVT::i32: Opc = X86::SAR32ri; break; |
| 3255 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3256 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3257 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 3258 | return Result; |
| 3259 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3260 | |
| 3261 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3262 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3263 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3264 | } else { |
| 3265 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 3266 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 3267 | } |
| 3268 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3269 | switch (N.getValueType()) { |
| 3270 | default: assert(0 && "Cannot shift this type!"); |
| 3271 | case MVT::i8 : Opc = X86::SAR8rCL; break; |
| 3272 | case MVT::i16: Opc = X86::SAR16rCL; break; |
| 3273 | case MVT::i32: Opc = X86::SAR32rCL; break; |
| 3274 | } |
| 3275 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 3276 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 3277 | return Result; |
| 3278 | |
| 3279 | case ISD::SETCC: |
Chris Lattner | 3be6cd5 | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 3280 | EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse()); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3281 | EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(), |
| 3282 | MVT::isFloatingPoint(N.getOperand(1).getValueType())); |
| 3283 | return Result; |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3284 | case ISD::LOAD: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3285 | // Make sure we generate both values. |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3286 | if (Result != 1) { // Generate the token |
| 3287 | if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second) |
| 3288 | assert(0 && "Load already emitted!?"); |
| 3289 | } else |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3290 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3291 | |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3292 | switch (Node->getValueType(0)) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3293 | default: assert(0 && "Cannot load this type!"); |
| 3294 | case MVT::i1: |
| 3295 | case MVT::i8: Opc = X86::MOV8rm; break; |
| 3296 | case MVT::i16: Opc = X86::MOV16rm; break; |
| 3297 | case MVT::i32: Opc = X86::MOV32rm; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3298 | case MVT::f32: Opc = X86::MOVSSrm; break; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3299 | case MVT::f64: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3300 | if (X86ScalarSSE) { |
| 3301 | Opc = X86::MOVSDrm; |
| 3302 | } else { |
| 3303 | Opc = X86::FLD64m; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3304 | ContainsFPCode = true; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3305 | } |
| 3306 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3307 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3308 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3309 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){ |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3310 | Select(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3311 | addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex()); |
| 3312 | } else { |
| 3313 | X86AddressMode AM; |
Chris Lattner | 3676cd6 | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 3314 | |
| 3315 | SDOperand Chain = N.getOperand(0); |
| 3316 | SDOperand Address = N.getOperand(1); |
| 3317 | if (getRegPressure(Chain) > getRegPressure(Address)) { |
| 3318 | Select(Chain); |
| 3319 | SelectAddress(Address, AM); |
| 3320 | } else { |
| 3321 | SelectAddress(Address, AM); |
| 3322 | Select(Chain); |
| 3323 | } |
| 3324 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3325 | addFullAddress(BuildMI(BB, Opc, 4, Result), AM); |
| 3326 | } |
| 3327 | return Result; |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 3328 | case X86ISD::FILD64m: |
| 3329 | // Make sure we generate both values. |
| 3330 | assert(Result != 1 && N.getValueType() == MVT::f64); |
| 3331 | if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second) |
| 3332 | assert(0 && "Load already emitted!?"); |
| 3333 | |
| 3334 | { |
| 3335 | X86AddressMode AM; |
| 3336 | |
| 3337 | SDOperand Chain = N.getOperand(0); |
| 3338 | SDOperand Address = N.getOperand(1); |
| 3339 | if (getRegPressure(Chain) > getRegPressure(Address)) { |
| 3340 | Select(Chain); |
| 3341 | SelectAddress(Address, AM); |
| 3342 | } else { |
| 3343 | SelectAddress(Address, AM); |
| 3344 | Select(Chain); |
| 3345 | } |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 3346 | |
| 3347 | addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM); |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 3348 | } |
| 3349 | return Result; |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 3350 | |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3351 | case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX* |
| 3352 | case ISD::ZEXTLOAD: { |
| 3353 | // Make sure we generate both values. |
| 3354 | if (Result != 1) |
| 3355 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 3356 | else |
| 3357 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3358 | |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3359 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))) |
| 3360 | if (Node->getValueType(0) == MVT::f64) { |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3361 | assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 && |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3362 | "Bad EXTLOAD!"); |
| 3363 | addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result), |
| 3364 | CP->getIndex()); |
| 3365 | return Result; |
| 3366 | } |
| 3367 | |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3368 | X86AddressMode AM; |
| 3369 | if (getRegPressure(Node->getOperand(0)) > |
| 3370 | getRegPressure(Node->getOperand(1))) { |
| 3371 | Select(Node->getOperand(0)); // chain |
| 3372 | SelectAddress(Node->getOperand(1), AM); |
| 3373 | } else { |
| 3374 | SelectAddress(Node->getOperand(1), AM); |
| 3375 | Select(Node->getOperand(0)); // chain |
| 3376 | } |
| 3377 | |
| 3378 | switch (Node->getValueType(0)) { |
| 3379 | default: assert(0 && "Unknown type to sign extend to."); |
| 3380 | case MVT::f64: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3381 | assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 && |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3382 | "Bad EXTLOAD!"); |
| 3383 | addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM); |
| 3384 | break; |
| 3385 | case MVT::i32: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3386 | switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) { |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3387 | default: |
| 3388 | assert(0 && "Bad zero extend!"); |
| 3389 | case MVT::i1: |
| 3390 | case MVT::i8: |
| 3391 | addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM); |
| 3392 | break; |
| 3393 | case MVT::i16: |
| 3394 | addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM); |
| 3395 | break; |
| 3396 | } |
| 3397 | break; |
| 3398 | case MVT::i16: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3399 | assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 && |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3400 | "Bad zero extend!"); |
| 3401 | addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM); |
| 3402 | break; |
| 3403 | case MVT::i8: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3404 | assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 && |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3405 | "Bad zero extend!"); |
| 3406 | addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM); |
| 3407 | break; |
| 3408 | } |
| 3409 | return Result; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3410 | } |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3411 | case ISD::SEXTLOAD: { |
| 3412 | // Make sure we generate both values. |
| 3413 | if (Result != 1) |
| 3414 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 3415 | else |
| 3416 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3417 | |
| 3418 | X86AddressMode AM; |
| 3419 | if (getRegPressure(Node->getOperand(0)) > |
| 3420 | getRegPressure(Node->getOperand(1))) { |
| 3421 | Select(Node->getOperand(0)); // chain |
| 3422 | SelectAddress(Node->getOperand(1), AM); |
| 3423 | } else { |
| 3424 | SelectAddress(Node->getOperand(1), AM); |
| 3425 | Select(Node->getOperand(0)); // chain |
| 3426 | } |
| 3427 | |
| 3428 | switch (Node->getValueType(0)) { |
| 3429 | case MVT::i8: assert(0 && "Cannot sign extend from bool!"); |
| 3430 | default: assert(0 && "Unknown type to sign extend to."); |
| 3431 | case MVT::i32: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3432 | switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) { |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3433 | default: |
| 3434 | case MVT::i1: assert(0 && "Cannot sign extend from bool!"); |
| 3435 | case MVT::i8: |
| 3436 | addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM); |
| 3437 | break; |
| 3438 | case MVT::i16: |
| 3439 | addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM); |
| 3440 | break; |
| 3441 | } |
| 3442 | break; |
| 3443 | case MVT::i16: |
Chris Lattner | 53676df | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 3444 | assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 && |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3445 | "Cannot sign extend from bool!"); |
| 3446 | addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM); |
| 3447 | break; |
| 3448 | } |
| 3449 | return Result; |
| 3450 | } |
| 3451 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3452 | case ISD::DYNAMIC_STACKALLOC: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3453 | // Generate both result values. |
| 3454 | if (Result != 1) |
| 3455 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 3456 | else |
| 3457 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3458 | |
| 3459 | // FIXME: We are currently ignoring the requested alignment for handling |
| 3460 | // greater than the stack alignment. This will need to be revisited at some |
| 3461 | // point. Align = N.getOperand(2); |
| 3462 | |
| 3463 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 3464 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 3465 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 3466 | << " the stack alignment yet!"; |
| 3467 | abort(); |
| 3468 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3470 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3471 | Select(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3472 | BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP) |
| 3473 | .addImm(CN->getValue()); |
| 3474 | } else { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3475 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3476 | Select(N.getOperand(0)); |
| 3477 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3478 | } else { |
| 3479 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3480 | Select(N.getOperand(0)); |
| 3481 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3482 | |
| 3483 | // Subtract size from stack pointer, thereby allocating some space. |
| 3484 | BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1); |
| 3485 | } |
| 3486 | |
| 3487 | // Put a pointer to the space into the result register, by copying the stack |
| 3488 | // pointer. |
| 3489 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP); |
| 3490 | return Result; |
| 3491 | |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3492 | case X86ISD::TAILCALL: |
| 3493 | case X86ISD::CALL: { |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3494 | // The chain for this call is now lowered. |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3495 | ExprMap.insert(std::make_pair(N.getValue(0), 1)); |
Chris Lattner | b52e041 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3496 | |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3497 | bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) || |
| 3498 | isa<ExternalSymbolSDNode>(N.getOperand(1)); |
| 3499 | unsigned Callee = 0; |
| 3500 | if (isDirect) { |
| 3501 | Select(N.getOperand(0)); |
| 3502 | } else { |
| 3503 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3504 | Select(N.getOperand(0)); |
| 3505 | Callee = SelectExpr(N.getOperand(1)); |
| 3506 | } else { |
| 3507 | Callee = SelectExpr(N.getOperand(1)); |
| 3508 | Select(N.getOperand(0)); |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | // If this call has values to pass in registers, do so now. |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3513 | if (Node->getNumOperands() > 4) { |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3514 | // The first value is passed in (a part of) EAX, the second in EDX. |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3515 | unsigned RegOp1 = SelectExpr(N.getOperand(4)); |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3516 | unsigned RegOp2 = |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3517 | Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3518 | |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3519 | switch (N.getOperand(4).getValueType()) { |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3520 | default: assert(0 && "Bad thing to pass in regs"); |
| 3521 | case MVT::i1: |
| 3522 | case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break; |
| 3523 | case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break; |
| 3524 | case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break; |
| 3525 | } |
| 3526 | if (RegOp2) |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3527 | switch (N.getOperand(5).getValueType()) { |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3528 | default: assert(0 && "Bad thing to pass in regs"); |
| 3529 | case MVT::i1: |
| 3530 | case MVT::i8: |
| 3531 | BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2); |
| 3532 | break; |
| 3533 | case MVT::i16: |
| 3534 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2); |
| 3535 | break; |
| 3536 | case MVT::i32: |
| 3537 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2); |
| 3538 | break; |
| 3539 | } |
| 3540 | } |
| 3541 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3542 | if (GlobalAddressSDNode *GASD = |
| 3543 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 3544 | BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true); |
| 3545 | } else if (ExternalSymbolSDNode *ESSDN = |
| 3546 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 3547 | BuildMI(BB, X86::CALLpcrel32, |
| 3548 | 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 3549 | } else { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3550 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3551 | Select(N.getOperand(0)); |
| 3552 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3553 | } else { |
| 3554 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3555 | Select(N.getOperand(0)); |
| 3556 | } |
| 3557 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3558 | BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1); |
| 3559 | } |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3560 | |
| 3561 | // Get caller stack amount and amount the callee added to the stack pointer. |
| 3562 | Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue(); |
| 3563 | Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue(); |
| 3564 | BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2); |
| 3565 | |
| 3566 | if (Node->getNumValues() != 1) |
| 3567 | switch (Node->getValueType(1)) { |
| 3568 | default: assert(0 && "Unknown value type for call result!"); |
| 3569 | case MVT::Other: return 1; |
| 3570 | case MVT::i1: |
| 3571 | case MVT::i8: |
| 3572 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3573 | break; |
| 3574 | case MVT::i16: |
| 3575 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3576 | break; |
| 3577 | case MVT::i32: |
| 3578 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
| 3579 | if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32) |
| 3580 | BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX); |
| 3581 | break; |
| 3582 | case MVT::f64: // Floating-point return values live in %ST(0) |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 3583 | if (X86ScalarSSE) { |
| 3584 | ContainsFPCode = true; |
| 3585 | BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0); |
| 3586 | |
| 3587 | unsigned Size = MVT::getSizeInBits(MVT::f64)/8; |
| 3588 | MachineFunction *F = BB->getParent(); |
| 3589 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 3590 | addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0); |
| 3591 | addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx); |
| 3592 | break; |
| 3593 | } else { |
| 3594 | ContainsFPCode = true; |
| 3595 | BuildMI(BB, X86::FpGETRESULT, 1, Result); |
| 3596 | break; |
| 3597 | } |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 3598 | } |
| 3599 | return Result+N.ResNo-1; |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3600 | } |
Chris Lattner | 70ea07c | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3601 | case ISD::READPORT: |
| 3602 | // First, determine that the size of the operand falls within the acceptable |
| 3603 | // range for this architecture. |
| 3604 | // |
| 3605 | if (Node->getOperand(1).getValueType() != MVT::i16) { |
| 3606 | std::cerr << "llvm.readport: Address size is not 16 bits\n"; |
| 3607 | exit(1); |
| 3608 | } |
| 3609 | |
| 3610 | // Make sure we generate both values. |
| 3611 | if (Result != 1) { // Generate the token |
| 3612 | if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second) |
| 3613 | assert(0 && "readport already emitted!?"); |
| 3614 | } else |
| 3615 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3616 | |
Chris Lattner | 70ea07c | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3617 | Select(Node->getOperand(0)); // Select the chain. |
| 3618 | |
| 3619 | // If the port is a single-byte constant, use the immediate form. |
| 3620 | if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1))) |
| 3621 | if ((Port->getValue() & 255) == Port->getValue()) { |
| 3622 | switch (Node->getValueType(0)) { |
| 3623 | case MVT::i8: |
| 3624 | BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue()); |
| 3625 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3626 | return Result; |
| 3627 | case MVT::i16: |
| 3628 | BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue()); |
| 3629 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3630 | return Result; |
| 3631 | case MVT::i32: |
| 3632 | BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue()); |
| 3633 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
| 3634 | return Result; |
| 3635 | default: break; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | // Now, move the I/O port address into the DX register and use the IN |
| 3640 | // instruction to get the input data. |
| 3641 | // |
| 3642 | Tmp1 = SelectExpr(Node->getOperand(1)); |
| 3643 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1); |
| 3644 | switch (Node->getValueType(0)) { |
| 3645 | case MVT::i8: |
| 3646 | BuildMI(BB, X86::IN8rr, 0); |
| 3647 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3648 | return Result; |
| 3649 | case MVT::i16: |
| 3650 | BuildMI(BB, X86::IN16rr, 0); |
| 3651 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3652 | return Result; |
| 3653 | case MVT::i32: |
| 3654 | BuildMI(BB, X86::IN32rr, 0); |
| 3655 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
| 3656 | return Result; |
| 3657 | default: |
| 3658 | std::cerr << "Cannot do input on this data type"; |
| 3659 | exit(1); |
| 3660 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3661 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | return 0; |
| 3665 | } |
| 3666 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3667 | /// TryToFoldLoadOpStore - Given a store node, try to fold together a |
| 3668 | /// load/op/store instruction. If successful return true. |
| 3669 | bool ISel::TryToFoldLoadOpStore(SDNode *Node) { |
| 3670 | assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!"); |
| 3671 | SDOperand Chain = Node->getOperand(0); |
| 3672 | SDOperand StVal = Node->getOperand(1); |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3673 | SDOperand StPtr = Node->getOperand(2); |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3674 | |
| 3675 | // The chain has to be a load, the stored value must be an integer binary |
| 3676 | // operation with one use. |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3677 | if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 || |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3678 | MVT::isFloatingPoint(StVal.getValueType())) |
| 3679 | return false; |
| 3680 | |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3681 | // Token chain must either be a factor node or the load to fold. |
| 3682 | if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor) |
| 3683 | return false; |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3684 | |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3685 | SDOperand TheLoad; |
| 3686 | |
| 3687 | // Check to see if there is a load from the same pointer that we're storing |
| 3688 | // to in either operand of the binop. |
| 3689 | if (StVal.getOperand(0).getOpcode() == ISD::LOAD && |
| 3690 | StVal.getOperand(0).getOperand(1) == StPtr) |
| 3691 | TheLoad = StVal.getOperand(0); |
| 3692 | else if (StVal.getOperand(1).getOpcode() == ISD::LOAD && |
| 3693 | StVal.getOperand(1).getOperand(1) == StPtr) |
| 3694 | TheLoad = StVal.getOperand(1); |
| 3695 | else |
| 3696 | return false; // No matching load operand. |
| 3697 | |
| 3698 | // We can only fold the load if there are no intervening side-effecting |
| 3699 | // operations. This means that the store uses the load as its token chain, or |
| 3700 | // there are only token factor nodes in between the store and load. |
| 3701 | if (Chain != TheLoad.getValue(1)) { |
| 3702 | // Okay, the other option is that we have a store referring to (possibly |
| 3703 | // nested) token factor nodes. For now, just try peeking through one level |
| 3704 | // of token factors to see if this is the case. |
| 3705 | bool ChainOk = false; |
| 3706 | if (Chain.getOpcode() == ISD::TokenFactor) { |
| 3707 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 3708 | if (Chain.getOperand(i) == TheLoad.getValue(1)) { |
| 3709 | ChainOk = true; |
| 3710 | break; |
| 3711 | } |
| 3712 | } |
| 3713 | |
| 3714 | if (!ChainOk) return false; |
| 3715 | } |
| 3716 | |
| 3717 | if (TheLoad.getOperand(1) != StPtr) |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3718 | return false; |
| 3719 | |
| 3720 | // Make sure that one of the operands of the binop is the load, and that the |
| 3721 | // load folds into the binop. |
| 3722 | if (((StVal.getOperand(0) != TheLoad || |
| 3723 | !isFoldableLoad(TheLoad, StVal.getOperand(1))) && |
| 3724 | (StVal.getOperand(1) != TheLoad || |
| 3725 | !isFoldableLoad(TheLoad, StVal.getOperand(0))))) |
| 3726 | return false; |
| 3727 | |
| 3728 | // Finally, check to see if this is one of the ops we can handle! |
| 3729 | static const unsigned ADDTAB[] = { |
| 3730 | X86::ADD8mi, X86::ADD16mi, X86::ADD32mi, |
| 3731 | X86::ADD8mr, X86::ADD16mr, X86::ADD32mr, |
| 3732 | }; |
| 3733 | static const unsigned SUBTAB[] = { |
| 3734 | X86::SUB8mi, X86::SUB16mi, X86::SUB32mi, |
| 3735 | X86::SUB8mr, X86::SUB16mr, X86::SUB32mr, |
| 3736 | }; |
| 3737 | static const unsigned ANDTAB[] = { |
| 3738 | X86::AND8mi, X86::AND16mi, X86::AND32mi, |
| 3739 | X86::AND8mr, X86::AND16mr, X86::AND32mr, |
| 3740 | }; |
| 3741 | static const unsigned ORTAB[] = { |
| 3742 | X86::OR8mi, X86::OR16mi, X86::OR32mi, |
| 3743 | X86::OR8mr, X86::OR16mr, X86::OR32mr, |
| 3744 | }; |
| 3745 | static const unsigned XORTAB[] = { |
| 3746 | X86::XOR8mi, X86::XOR16mi, X86::XOR32mi, |
| 3747 | X86::XOR8mr, X86::XOR16mr, X86::XOR32mr, |
| 3748 | }; |
| 3749 | static const unsigned SHLTAB[] = { |
| 3750 | X86::SHL8mi, X86::SHL16mi, X86::SHL32mi, |
| 3751 | /*Have to put the reg in CL*/0, 0, 0, |
| 3752 | }; |
| 3753 | static const unsigned SARTAB[] = { |
| 3754 | X86::SAR8mi, X86::SAR16mi, X86::SAR32mi, |
| 3755 | /*Have to put the reg in CL*/0, 0, 0, |
| 3756 | }; |
| 3757 | static const unsigned SHRTAB[] = { |
| 3758 | X86::SHR8mi, X86::SHR16mi, X86::SHR32mi, |
| 3759 | /*Have to put the reg in CL*/0, 0, 0, |
| 3760 | }; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3762 | const unsigned *TabPtr = 0; |
| 3763 | switch (StVal.getOpcode()) { |
| 3764 | default: |
| 3765 | std::cerr << "CANNOT [mem] op= val: "; |
| 3766 | StVal.Val->dump(); std::cerr << "\n"; |
| 3767 | case ISD::MUL: |
| 3768 | case ISD::SDIV: |
| 3769 | case ISD::UDIV: |
| 3770 | case ISD::SREM: |
| 3771 | case ISD::UREM: return false; |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3773 | case ISD::ADD: TabPtr = ADDTAB; break; |
| 3774 | case ISD::SUB: TabPtr = SUBTAB; break; |
| 3775 | case ISD::AND: TabPtr = ANDTAB; break; |
| 3776 | case ISD:: OR: TabPtr = ORTAB; break; |
| 3777 | case ISD::XOR: TabPtr = XORTAB; break; |
| 3778 | case ISD::SHL: TabPtr = SHLTAB; break; |
| 3779 | case ISD::SRA: TabPtr = SARTAB; break; |
| 3780 | case ISD::SRL: TabPtr = SHRTAB; break; |
| 3781 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3782 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3783 | // Handle: [mem] op= CST |
| 3784 | SDOperand Op0 = StVal.getOperand(0); |
| 3785 | SDOperand Op1 = StVal.getOperand(1); |
Chris Lattner | 0e1de10 | 2005-01-23 23:20:06 +0000 | [diff] [blame] | 3786 | unsigned Opc = 0; |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3787 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
| 3788 | switch (Op0.getValueType()) { // Use Op0's type because of shifts. |
| 3789 | default: break; |
| 3790 | case MVT::i1: |
| 3791 | case MVT::i8: Opc = TabPtr[0]; break; |
| 3792 | case MVT::i16: Opc = TabPtr[1]; break; |
| 3793 | case MVT::i32: Opc = TabPtr[2]; break; |
| 3794 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3795 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3796 | if (Opc) { |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3797 | if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second) |
| 3798 | assert(0 && "Already emitted?"); |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3799 | Select(Chain); |
| 3800 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3801 | X86AddressMode AM; |
| 3802 | if (getRegPressure(TheLoad.getOperand(0)) > |
| 3803 | getRegPressure(TheLoad.getOperand(1))) { |
| 3804 | Select(TheLoad.getOperand(0)); |
| 3805 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3806 | } else { |
| 3807 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3808 | Select(TheLoad.getOperand(0)); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3809 | } |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3810 | |
| 3811 | if (StVal.getOpcode() == ISD::ADD) { |
| 3812 | if (CN->getValue() == 1) { |
| 3813 | switch (Op0.getValueType()) { |
| 3814 | default: break; |
| 3815 | case MVT::i8: |
| 3816 | addFullAddress(BuildMI(BB, X86::INC8m, 4), AM); |
| 3817 | return true; |
| 3818 | case MVT::i16: Opc = TabPtr[1]; |
| 3819 | addFullAddress(BuildMI(BB, X86::INC16m, 4), AM); |
| 3820 | return true; |
| 3821 | case MVT::i32: Opc = TabPtr[2]; |
| 3822 | addFullAddress(BuildMI(BB, X86::INC32m, 4), AM); |
| 3823 | return true; |
| 3824 | } |
| 3825 | } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X] |
| 3826 | switch (Op0.getValueType()) { |
| 3827 | default: break; |
| 3828 | case MVT::i8: |
| 3829 | addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM); |
| 3830 | return true; |
| 3831 | case MVT::i16: Opc = TabPtr[1]; |
| 3832 | addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM); |
| 3833 | return true; |
| 3834 | case MVT::i32: Opc = TabPtr[2]; |
| 3835 | addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM); |
| 3836 | return true; |
| 3837 | } |
| 3838 | } |
| 3839 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3840 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3841 | addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue()); |
| 3842 | return true; |
| 3843 | } |
| 3844 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3846 | // If we have [mem] = V op [mem], try to turn it into: |
| 3847 | // [mem] = [mem] op V. |
| 3848 | if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB && |
| 3849 | StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA && |
| 3850 | StVal.getOpcode() != ISD::SRL) |
| 3851 | std::swap(Op0, Op1); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3853 | if (Op0 != TheLoad) return false; |
| 3854 | |
| 3855 | switch (Op0.getValueType()) { |
| 3856 | default: return false; |
| 3857 | case MVT::i1: |
| 3858 | case MVT::i8: Opc = TabPtr[3]; break; |
| 3859 | case MVT::i16: Opc = TabPtr[4]; break; |
| 3860 | case MVT::i32: Opc = TabPtr[5]; break; |
| 3861 | } |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3862 | |
Chris Lattner | 479c711 | 2005-01-18 17:35:28 +0000 | [diff] [blame] | 3863 | // Table entry doesn't exist? |
| 3864 | if (Opc == 0) return false; |
| 3865 | |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3866 | if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second) |
| 3867 | assert(0 && "Already emitted?"); |
Chris Lattner | e86c933 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3868 | Select(Chain); |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3869 | Select(TheLoad.getOperand(0)); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 3870 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3871 | X86AddressMode AM; |
| 3872 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3873 | unsigned Reg = SelectExpr(Op1); |
Chris Lattner | a7acdda | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 3874 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg); |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3875 | return true; |
| 3876 | } |
| 3877 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 3878 | /// If node is a ret(tailcall) node, emit the specified tail call and return |
| 3879 | /// true, otherwise return false. |
| 3880 | /// |
| 3881 | /// FIXME: This whole thing should be a post-legalize optimization pass which |
| 3882 | /// recognizes and transforms the dag. We don't want the selection phase doing |
| 3883 | /// this stuff!! |
| 3884 | /// |
| 3885 | bool ISel::EmitPotentialTailCall(SDNode *RetNode) { |
| 3886 | assert(RetNode->getOpcode() == ISD::RET && "Not a return"); |
| 3887 | |
| 3888 | SDOperand Chain = RetNode->getOperand(0); |
| 3889 | |
| 3890 | // If this is a token factor node where one operand is a call, dig into it. |
| 3891 | SDOperand TokFactor; |
| 3892 | unsigned TokFactorOperand = 0; |
| 3893 | if (Chain.getOpcode() == ISD::TokenFactor) { |
| 3894 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 3895 | if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END || |
| 3896 | Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) { |
| 3897 | TokFactorOperand = i; |
| 3898 | TokFactor = Chain; |
| 3899 | Chain = Chain.getOperand(i); |
| 3900 | break; |
| 3901 | } |
| 3902 | if (TokFactor.Val == 0) return false; // No call operand. |
| 3903 | } |
| 3904 | |
| 3905 | // Skip the CALLSEQ_END node if present. |
| 3906 | if (Chain.getOpcode() == ISD::CALLSEQ_END) |
| 3907 | Chain = Chain.getOperand(0); |
| 3908 | |
| 3909 | // Is a tailcall the last control operation that occurs before the return? |
| 3910 | if (Chain.getOpcode() != X86ISD::TAILCALL) |
| 3911 | return false; |
| 3912 | |
| 3913 | // If we return a value, is it the value produced by the call? |
| 3914 | if (RetNode->getNumOperands() > 1) { |
| 3915 | // Not returning the ret val of the call? |
| 3916 | if (Chain.Val->getNumValues() == 1 || |
| 3917 | RetNode->getOperand(1) != Chain.getValue(1)) |
| 3918 | return false; |
| 3919 | |
| 3920 | if (RetNode->getNumOperands() > 2) { |
| 3921 | if (Chain.Val->getNumValues() == 2 || |
| 3922 | RetNode->getOperand(2) != Chain.getValue(2)) |
| 3923 | return false; |
| 3924 | } |
| 3925 | assert(RetNode->getNumOperands() <= 3); |
| 3926 | } |
| 3927 | |
| 3928 | // CalleeCallArgAmt - The total number of bytes used for the callee arg area. |
| 3929 | // For FastCC, this will always be > 0. |
| 3930 | unsigned CalleeCallArgAmt = |
| 3931 | cast<ConstantSDNode>(Chain.getOperand(2))->getValue(); |
| 3932 | |
| 3933 | // CalleeCallArgPopAmt - The number of bytes in the call area popped by the |
| 3934 | // callee. For FastCC this will always be > 0, for CCC this is always 0. |
| 3935 | unsigned CalleeCallArgPopAmt = |
| 3936 | cast<ConstantSDNode>(Chain.getOperand(3))->getValue(); |
| 3937 | |
| 3938 | // There are several cases we can handle here. First, if the caller and |
| 3939 | // callee are both CCC functions, we can tailcall if the callee takes <= the |
| 3940 | // number of argument bytes that the caller does. |
| 3941 | if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv? |
| 3942 | X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv? |
| 3943 | // Check to see if caller arg area size >= callee arg area size. |
| 3944 | if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) { |
| 3945 | //std::cerr << "CCC TAILCALL UNIMP!\n"; |
| 3946 | // If TokFactor is non-null, emit all operands. |
| 3947 | |
| 3948 | //EmitCCCToCCCTailCall(Chain.Val); |
| 3949 | //return true; |
| 3950 | } |
| 3951 | return false; |
| 3952 | } |
| 3953 | |
| 3954 | // Second, if both are FastCC functions, we can always perform the tail call. |
| 3955 | if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) { |
| 3956 | // If TokFactor is non-null, emit all operands before the call. |
| 3957 | if (TokFactor.Val) { |
| 3958 | for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i) |
| 3959 | if (i != TokFactorOperand) |
| 3960 | Select(TokFactor.getOperand(i)); |
| 3961 | } |
| 3962 | |
| 3963 | EmitFastCCToFastCCTailCall(Chain.Val); |
| 3964 | return true; |
| 3965 | } |
| 3966 | |
| 3967 | // We don't support mixed calls, due to issues with alignment. We could in |
| 3968 | // theory handle some mixed calls from CCC -> FastCC if the stack is properly |
| 3969 | // aligned (which depends on the number of arguments to the callee). TODO. |
| 3970 | return false; |
| 3971 | } |
| 3972 | |
| 3973 | static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset, |
| 3974 | SelectionDAG &DAG) { |
| 3975 | MVT::ValueType StoreVT; |
| 3976 | switch (Chain.getOpcode()) { |
| 3977 | case ISD::CALLSEQ_START: |
Chris Lattner | 1a61fa4 | 2005-05-15 06:07:10 +0000 | [diff] [blame] | 3978 | // If we found the start of the call sequence, we're done. We actually |
| 3979 | // strip off the CALLSEQ_START node, to avoid generating the |
| 3980 | // ADJCALLSTACKDOWN marker for the tail call. |
| 3981 | return Chain.getOperand(0); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 3982 | case ISD::TokenFactor: { |
| 3983 | std::vector<SDOperand> Ops; |
| 3984 | Ops.reserve(Chain.getNumOperands()); |
| 3985 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 3986 | Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG)); |
| 3987 | return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 3988 | } |
| 3989 | case ISD::STORE: // Normal store |
| 3990 | StoreVT = Chain.getOperand(1).getValueType(); |
| 3991 | break; |
| 3992 | case ISD::TRUNCSTORE: // FLOAT store |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 3993 | StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT(); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 3994 | break; |
| 3995 | } |
| 3996 | |
| 3997 | SDOperand OrigDest = Chain.getOperand(2); |
| 3998 | unsigned OrigOffset; |
| 3999 | |
| 4000 | if (OrigDest.getOpcode() == ISD::CopyFromReg) { |
| 4001 | OrigOffset = 0; |
| 4002 | assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP); |
| 4003 | } else { |
| 4004 | // We expect only (ESP+C) |
| 4005 | assert(OrigDest.getOpcode() == ISD::ADD && |
| 4006 | isa<ConstantSDNode>(OrigDest.getOperand(1)) && |
| 4007 | OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg && |
| 4008 | cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP); |
| 4009 | OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue(); |
| 4010 | } |
| 4011 | |
| 4012 | // Compute the new offset from the incoming ESP value we wish to use. |
| 4013 | unsigned NewOffset = OrigOffset + Offset; |
| 4014 | |
| 4015 | unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes |
| 4016 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4017 | int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset); |
| 4018 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 4019 | |
| 4020 | SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset, |
| 4021 | DAG); |
| 4022 | if (Chain.getOpcode() == ISD::STORE) |
| 4023 | return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1), |
| 4024 | FIN); |
| 4025 | assert(Chain.getOpcode() == ISD::TRUNCSTORE); |
| 4026 | return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1), |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 4027 | FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT)); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | |
| 4031 | /// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a |
| 4032 | /// fastcc function from a fastcc function, emit the code to emit a 'proper' |
| 4033 | /// tail call. |
| 4034 | void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) { |
| 4035 | unsigned CalleeCallArgSize = |
| 4036 | cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue(); |
| 4037 | unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn(); |
| 4038 | |
| 4039 | //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n"; |
| 4040 | |
| 4041 | // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame |
| 4042 | // indexes that are relative to the incoming ESP. If the incoming and |
| 4043 | // outgoing arg sizes are the same we will store to [InESP] instead of |
| 4044 | // [CurESP] and the ESP referenced will be relative to the incoming function |
| 4045 | // ESP. |
| 4046 | int ESPOffset = CallerArgSize-CalleeCallArgSize; |
| 4047 | SDOperand AdjustedArgStores = |
| 4048 | GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG); |
| 4049 | |
| 4050 | // Copy the return address of the caller into a virtual register so we don't |
| 4051 | // clobber it. |
| 4052 | SDOperand RetVal; |
| 4053 | if (ESPOffset) { |
| 4054 | SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG); |
| 4055 | RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(), |
| 4056 | RetValAddr, TheDAG->getSrcValue(NULL)); |
| 4057 | SelectExpr(RetVal); |
| 4058 | } |
| 4059 | |
| 4060 | // Codegen all of the argument stores. |
| 4061 | Select(AdjustedArgStores); |
| 4062 | |
| 4063 | if (RetVal.Val) { |
| 4064 | // Emit a store of the saved ret value to the new location. |
| 4065 | MachineFunction &MF = TheDAG->getMachineFunction(); |
| 4066 | int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4); |
| 4067 | SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32); |
| 4068 | Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(), |
| 4069 | RetVal, RetValAddr)); |
| 4070 | } |
| 4071 | |
| 4072 | // Get the destination value. |
| 4073 | SDOperand Callee = TailCallNode->getOperand(1); |
| 4074 | bool isDirect = isa<GlobalAddressSDNode>(Callee) || |
| 4075 | isa<ExternalSymbolSDNode>(Callee); |
Chris Lattner | 459a9cb | 2005-06-17 13:23:32 +0000 | [diff] [blame] | 4076 | unsigned CalleeReg = 0; |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 4077 | if (!isDirect) CalleeReg = SelectExpr(Callee); |
| 4078 | |
| 4079 | unsigned RegOp1 = 0; |
| 4080 | unsigned RegOp2 = 0; |
| 4081 | |
| 4082 | if (TailCallNode->getNumOperands() > 4) { |
| 4083 | // The first value is passed in (a part of) EAX, the second in EDX. |
| 4084 | RegOp1 = SelectExpr(TailCallNode->getOperand(4)); |
| 4085 | if (TailCallNode->getNumOperands() > 5) |
| 4086 | RegOp2 = SelectExpr(TailCallNode->getOperand(5)); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4087 | |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 4088 | switch (TailCallNode->getOperand(4).getValueType()) { |
| 4089 | default: assert(0 && "Bad thing to pass in regs"); |
| 4090 | case MVT::i1: |
| 4091 | case MVT::i8: |
| 4092 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1); |
| 4093 | RegOp1 = X86::AL; |
| 4094 | break; |
| 4095 | case MVT::i16: |
| 4096 | BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); |
| 4097 | RegOp1 = X86::AX; |
| 4098 | break; |
| 4099 | case MVT::i32: |
| 4100 | BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1); |
| 4101 | RegOp1 = X86::EAX; |
| 4102 | break; |
| 4103 | } |
| 4104 | if (RegOp2) |
| 4105 | switch (TailCallNode->getOperand(5).getValueType()) { |
| 4106 | default: assert(0 && "Bad thing to pass in regs"); |
| 4107 | case MVT::i1: |
| 4108 | case MVT::i8: |
| 4109 | BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2); |
| 4110 | RegOp2 = X86::DL; |
| 4111 | break; |
| 4112 | case MVT::i16: |
| 4113 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2); |
| 4114 | RegOp2 = X86::DX; |
| 4115 | break; |
| 4116 | case MVT::i32: |
| 4117 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2); |
| 4118 | RegOp2 = X86::EDX; |
| 4119 | break; |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | // Adjust ESP. |
| 4124 | if (ESPOffset) |
| 4125 | BuildMI(BB, X86::ADJSTACKPTRri, 2, |
| 4126 | X86::ESP).addReg(X86::ESP).addImm(ESPOffset); |
| 4127 | |
| 4128 | // TODO: handle jmp [mem] |
| 4129 | if (!isDirect) { |
| 4130 | BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg); |
| 4131 | } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){ |
Chris Lattner | 5727959 | 2005-05-19 05:54:33 +0000 | [diff] [blame] | 4132 | BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true); |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 4133 | } else { |
| 4134 | ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee); |
| 4135 | BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 4136 | } |
| 4137 | // ADD IMPLICIT USE RegOp1/RegOp2's |
| 4138 | } |
| 4139 | |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 4140 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4141 | void ISel::Select(SDOperand N) { |
| 4142 | unsigned Tmp1, Tmp2, Opc; |
| 4143 | |
Nate Begeman | 9521052 | 2005-03-24 04:39:54 +0000 | [diff] [blame] | 4144 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4145 | return; // Already selected. |
| 4146 | |
Chris Lattner | 36f7848 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 4147 | SDNode *Node = N.Val; |
| 4148 | |
| 4149 | switch (Node->getOpcode()) { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4150 | default: |
Chris Lattner | 36f7848 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 4151 | Node->dump(); std::cerr << "\n"; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4152 | assert(0 && "Node not handled yet!"); |
| 4153 | case ISD::EntryToken: return; // Noop |
Chris Lattner | c251fb6 | 2005-01-13 18:01:36 +0000 | [diff] [blame] | 4154 | case ISD::TokenFactor: |
Chris Lattner | 15bd19d | 2005-01-13 19:56:00 +0000 | [diff] [blame] | 4155 | if (Node->getNumOperands() == 2) { |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 4156 | bool OneFirst = |
Chris Lattner | 15bd19d | 2005-01-13 19:56:00 +0000 | [diff] [blame] | 4157 | getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0)); |
| 4158 | Select(Node->getOperand(OneFirst)); |
| 4159 | Select(Node->getOperand(!OneFirst)); |
| 4160 | } else { |
| 4161 | std::vector<std::pair<unsigned, unsigned> > OpsP; |
| 4162 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 4163 | OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i)); |
| 4164 | std::sort(OpsP.begin(), OpsP.end()); |
| 4165 | std::reverse(OpsP.begin(), OpsP.end()); |
| 4166 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 4167 | Select(Node->getOperand(OpsP[i].second)); |
| 4168 | } |
Chris Lattner | c251fb6 | 2005-01-13 18:01:36 +0000 | [diff] [blame] | 4169 | return; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4170 | case ISD::CopyToReg: |
Chris Lattner | 2cfce68 | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 4171 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 4172 | Select(N.getOperand(0)); |
| 4173 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4174 | } else { |
| 4175 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4176 | Select(N.getOperand(0)); |
| 4177 | } |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 4178 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 4179 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4180 | if (Tmp1 != Tmp2) { |
| 4181 | switch (N.getOperand(1).getValueType()) { |
| 4182 | default: assert(0 && "Invalid type for operation!"); |
| 4183 | case MVT::i1: |
| 4184 | case MVT::i8: Opc = X86::MOV8rr; break; |
| 4185 | case MVT::i16: Opc = X86::MOV16rr; break; |
| 4186 | case MVT::i32: Opc = X86::MOV32rr; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4187 | case MVT::f32: Opc = X86::MOVAPSrr; break; |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4188 | case MVT::f64: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4189 | if (X86ScalarSSE) { |
| 4190 | Opc = X86::MOVAPDrr; |
| 4191 | } else { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4192 | Opc = X86::FpMOV; |
| 4193 | ContainsFPCode = true; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4194 | } |
| 4195 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4196 | } |
| 4197 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 4198 | } |
| 4199 | return; |
| 4200 | case ISD::RET: |
Chris Lattner | dd66a41 | 2005-05-15 05:46:45 +0000 | [diff] [blame] | 4201 | if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END || |
| 4202 | N.getOperand(0).getOpcode() == X86ISD::TAILCALL || |
| 4203 | N.getOperand(0).getOpcode() == ISD::TokenFactor) |
| 4204 | if (EmitPotentialTailCall(Node)) |
| 4205 | return; |
| 4206 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4207 | switch (N.getNumOperands()) { |
| 4208 | default: |
| 4209 | assert(0 && "Unknown return instruction!"); |
| 4210 | case 3: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4211 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4212 | N.getOperand(2).getValueType() == MVT::i32 && |
| 4213 | "Unknown two-register value!"); |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4214 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 4215 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4216 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 4217 | } else { |
| 4218 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 4219 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4220 | } |
| 4221 | Select(N.getOperand(0)); |
| 4222 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4223 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 4224 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4225 | break; |
| 4226 | case 2: |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4227 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 4228 | Select(N.getOperand(0)); |
| 4229 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4230 | } else { |
| 4231 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4232 | Select(N.getOperand(0)); |
| 4233 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4234 | switch (N.getOperand(1).getValueType()) { |
| 4235 | default: assert(0 && "All other types should have been promoted!!"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4236 | case MVT::f32: |
| 4237 | if (X86ScalarSSE) { |
| 4238 | // Spill the value to memory and reload it into top of stack. |
| 4239 | unsigned Size = MVT::getSizeInBits(MVT::f32)/8; |
| 4240 | MachineFunction *F = BB->getParent(); |
| 4241 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 4242 | addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1); |
| 4243 | addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx); |
| 4244 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4245 | ContainsFPCode = true; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4246 | } else { |
| 4247 | assert(0 && "MVT::f32 only legal with scalar sse fp"); |
| 4248 | abort(); |
| 4249 | } |
| 4250 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4251 | case MVT::f64: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4252 | if (X86ScalarSSE) { |
| 4253 | // Spill the value to memory and reload it into top of stack. |
| 4254 | unsigned Size = MVT::getSizeInBits(MVT::f64)/8; |
| 4255 | MachineFunction *F = BB->getParent(); |
| 4256 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 4257 | addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1); |
| 4258 | addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx); |
| 4259 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4260 | ContainsFPCode = true; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4261 | } else { |
| 4262 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1); |
| 4263 | } |
| 4264 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4265 | case MVT::i32: |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4266 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 4267 | break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4268 | } |
| 4269 | break; |
| 4270 | case 1: |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4271 | Select(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4272 | break; |
| 4273 | } |
Chris Lattner | c0e369e | 2005-05-13 21:44:04 +0000 | [diff] [blame] | 4274 | if (X86Lowering.getBytesToPopOnReturn() == 0) |
| 4275 | BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction |
| 4276 | else |
| 4277 | BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn()); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4278 | return; |
| 4279 | case ISD::BR: { |
| 4280 | Select(N.getOperand(0)); |
| 4281 | MachineBasicBlock *Dest = |
| 4282 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
| 4283 | BuildMI(BB, X86::JMP, 1).addMBB(Dest); |
| 4284 | return; |
| 4285 | } |
| 4286 | |
| 4287 | case ISD::BRCOND: { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4288 | MachineBasicBlock *Dest = |
| 4289 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4290 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4291 | // Try to fold a setcc into the branch. If this fails, emit a test/jne |
| 4292 | // pair. |
Chris Lattner | 37ed285 | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 4293 | if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) { |
| 4294 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 4295 | Select(N.getOperand(0)); |
| 4296 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4297 | } else { |
| 4298 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4299 | Select(N.getOperand(0)); |
| 4300 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4301 | BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1); |
| 4302 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 4303 | } |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4304 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4305 | return; |
| 4306 | } |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4307 | |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 4308 | case ISD::LOAD: |
| 4309 | // If this load could be folded into the only using instruction, and if it |
| 4310 | // is safe to emit the instruction here, try to do so now. |
| 4311 | if (Node->hasNUsesOfValue(1, 0)) { |
| 4312 | SDOperand TheVal = N.getValue(0); |
| 4313 | SDNode *User = 0; |
| 4314 | for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) { |
| 4315 | assert(UI != Node->use_end() && "Didn't find use!"); |
| 4316 | SDNode *UN = *UI; |
| 4317 | for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i) |
| 4318 | if (UN->getOperand(i) == TheVal) { |
| 4319 | User = UN; |
| 4320 | goto FoundIt; |
| 4321 | } |
| 4322 | } |
| 4323 | FoundIt: |
| 4324 | // Only handle unary operators right now. |
| 4325 | if (User->getNumOperands() == 1) { |
Chris Lattner | 78d3028 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 4326 | ExprMap.erase(N); |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 4327 | SelectExpr(SDOperand(User, 0)); |
| 4328 | return; |
| 4329 | } |
| 4330 | } |
Chris Lattner | 28a205e | 2005-01-18 04:00:54 +0000 | [diff] [blame] | 4331 | ExprMap.erase(N); |
Chris Lattner | c1f386c | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 4332 | SelectExpr(N); |
| 4333 | return; |
Chris Lattner | 70ea07c | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 4334 | case ISD::READPORT: |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4335 | case ISD::EXTLOAD: |
| 4336 | case ISD::SEXTLOAD: |
| 4337 | case ISD::ZEXTLOAD: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4338 | case ISD::DYNAMIC_STACKALLOC: |
Chris Lattner | 1b3520c | 2005-05-14 08:48:15 +0000 | [diff] [blame] | 4339 | case X86ISD::TAILCALL: |
| 4340 | case X86ISD::CALL: |
Chris Lattner | 28a205e | 2005-01-18 04:00:54 +0000 | [diff] [blame] | 4341 | ExprMap.erase(N); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4342 | SelectExpr(N); |
| 4343 | return; |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 4344 | case ISD::CopyFromReg: |
Chris Lattner | a36117b | 2005-05-14 06:52:07 +0000 | [diff] [blame] | 4345 | case X86ISD::FILD64m: |
Chris Lattner | 7ce7a8f | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 4346 | ExprMap.erase(N); |
| 4347 | SelectExpr(N.getValue(0)); |
| 4348 | return; |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 4349 | |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 4350 | case X86ISD::FP_TO_INT64_IN_MEM: { |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 4351 | assert(N.getOperand(1).getValueType() == MVT::f64); |
| 4352 | X86AddressMode AM; |
| 4353 | Select(N.getOperand(0)); // Select the token chain |
| 4354 | |
| 4355 | unsigned ValReg; |
| 4356 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 4357 | ValReg = SelectExpr(N.getOperand(1)); |
| 4358 | SelectAddress(N.getOperand(2), AM); |
| 4359 | } else { |
| 4360 | SelectAddress(N.getOperand(2), AM); |
| 4361 | ValReg = SelectExpr(N.getOperand(1)); |
| 4362 | } |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 4363 | |
| 4364 | // Change the floating point control register to use "round towards zero" |
| 4365 | // mode when truncating to an integer value. |
| 4366 | // |
| 4367 | MachineFunction *F = BB->getParent(); |
| 4368 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 4369 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 4370 | |
| 4371 | // Load the old value of the high byte of the control word... |
| 4372 | unsigned HighPartOfCW = MakeReg(MVT::i8); |
| 4373 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW), |
| 4374 | CWFrameIdx, 1); |
| 4375 | |
| 4376 | // Set the high part to be round to zero... |
| 4377 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 4378 | CWFrameIdx, 1).addImm(12); |
| 4379 | |
| 4380 | // Reload the modified control word now... |
| 4381 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 4382 | |
| 4383 | // Restore the memory image of control word to original value |
| 4384 | addFrameReference(BuildMI(BB, X86::MOV8mr, 5), |
| 4385 | CWFrameIdx, 1).addReg(HighPartOfCW); |
| 4386 | |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 4387 | addFullAddress(BuildMI(BB, X86::FISTP64m, 5), AM).addReg(ValReg); |
Chris Lattner | 6dc60e8 | 2005-07-29 00:54:34 +0000 | [diff] [blame] | 4388 | |
| 4389 | // Reload the original control word now. |
| 4390 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
Chris Lattner | 67756e2 | 2005-07-29 00:40:01 +0000 | [diff] [blame] | 4391 | return; |
| 4392 | } |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4393 | |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 4394 | case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4395 | X86AddressMode AM; |
Chris Lattner | 36db1ed | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 4396 | MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT(); |
Chris Lattner | b14a63a | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 4397 | assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 || |
| 4398 | StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/) |
| 4399 | && "Unsupported TRUNCSTORE for this target!"); |
| 4400 | |
| 4401 | if (StoredTy == MVT::i16) { |
| 4402 | // FIXME: This is here just to allow testing. X86 doesn't really have a |
| 4403 | // TRUNCSTORE i16 operation, but this is required for targets that do not |
| 4404 | // have 16-bit integer registers. We occasionally disable 16-bit integer |
| 4405 | // registers to test the promotion code. |
| 4406 | Select(N.getOperand(0)); |
| 4407 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 4408 | SelectAddress(N.getOperand(2), AM); |
| 4409 | |
| 4410 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 4411 | addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX); |
| 4412 | return; |
| 4413 | } |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4414 | |
| 4415 | // Store of constant bool? |
| 4416 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 4417 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 4418 | Select(N.getOperand(0)); |
| 4419 | SelectAddress(N.getOperand(2), AM); |
| 4420 | } else { |
| 4421 | SelectAddress(N.getOperand(2), AM); |
| 4422 | Select(N.getOperand(0)); |
| 4423 | } |
| 4424 | addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue()); |
| 4425 | return; |
| 4426 | } |
| 4427 | |
| 4428 | switch (StoredTy) { |
| 4429 | default: assert(0 && "Cannot truncstore this type!"); |
| 4430 | case MVT::i1: Opc = X86::MOV8mr; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4431 | case MVT::f32: |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4432 | assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs"); |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4433 | Opc = X86::FST32m; break; |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4434 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 4435 | |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4436 | std::vector<std::pair<unsigned, unsigned> > RP; |
| 4437 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0)); |
| 4438 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1)); |
| 4439 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2)); |
| 4440 | std::sort(RP.begin(), RP.end()); |
| 4441 | |
Chris Lattner | 80c5b97 | 2005-02-23 05:57:21 +0000 | [diff] [blame] | 4442 | Tmp1 = 0; // Silence a warning. |
Chris Lattner | e18a4c4 | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 4443 | for (unsigned i = 0; i != 3; ++i) |
| 4444 | switch (RP[2-i].second) { |
| 4445 | default: assert(0 && "Unknown operand number!"); |
| 4446 | case 0: Select(N.getOperand(0)); break; |
| 4447 | case 1: Tmp1 = SelectExpr(N.getOperand(1)); break; |
| 4448 | case 2: SelectAddress(N.getOperand(2), AM); break; |
| 4449 | } |
| 4450 | |
| 4451 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1); |
| 4452 | return; |
| 4453 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4454 | case ISD::STORE: { |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4455 | X86AddressMode AM; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4456 | |
| 4457 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 4458 | Opc = 0; |
| 4459 | switch (CN->getValueType(0)) { |
| 4460 | default: assert(0 && "Invalid type for operation!"); |
| 4461 | case MVT::i1: |
| 4462 | case MVT::i8: Opc = X86::MOV8mi; break; |
| 4463 | case MVT::i16: Opc = X86::MOV16mi; break; |
| 4464 | case MVT::i32: Opc = X86::MOV32mi; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4465 | } |
| 4466 | if (Opc) { |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4467 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 4468 | Select(N.getOperand(0)); |
| 4469 | SelectAddress(N.getOperand(2), AM); |
| 4470 | } else { |
| 4471 | SelectAddress(N.getOperand(2), AM); |
| 4472 | Select(N.getOperand(0)); |
| 4473 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4474 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue()); |
| 4475 | return; |
| 4476 | } |
Chris Lattner | adcfc17 | 2005-04-21 19:03:24 +0000 | [diff] [blame] | 4477 | } else if (GlobalAddressSDNode *GA = |
| 4478 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 4479 | assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand"); |
| 4480 | |
| 4481 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 4482 | Select(N.getOperand(0)); |
| 4483 | SelectAddress(N.getOperand(2), AM); |
| 4484 | } else { |
| 4485 | SelectAddress(N.getOperand(2), AM); |
| 4486 | Select(N.getOperand(0)); |
| 4487 | } |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 4488 | GlobalValue *GV = GA->getGlobal(); |
| 4489 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 4490 | // the value at address GV, not the value of GV itself. |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4491 | if (Subtarget->getIndirectExternAndWeakGlobals() && |
Nate Begeman | a0b5e03 | 2005-07-15 00:38:55 +0000 | [diff] [blame] | 4492 | (GV->hasWeakLinkage() || GV->isExternal())) { |
| 4493 | Tmp1 = MakeReg(MVT::i32); |
| 4494 | BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0) |
| 4495 | .addGlobalAddress(GV, false, 0); |
| 4496 | addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1); |
| 4497 | } else { |
| 4498 | addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV); |
| 4499 | } |
Chris Lattner | adcfc17 | 2005-04-21 19:03:24 +0000 | [diff] [blame] | 4500 | return; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4501 | } |
Chris Lattner | 75bac9f | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 4502 | |
| 4503 | // Check to see if this is a load/op/store combination. |
Chris Lattner | 96113fd | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 4504 | if (TryToFoldLoadOpStore(Node)) |
| 4505 | return; |
Chris Lattner | 75bac9f | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4507 | switch (N.getOperand(1).getValueType()) { |
| 4508 | default: assert(0 && "Cannot store this type!"); |
| 4509 | case MVT::i1: |
| 4510 | case MVT::i8: Opc = X86::MOV8mr; break; |
| 4511 | case MVT::i16: Opc = X86::MOV16mr; break; |
| 4512 | case MVT::i32: Opc = X86::MOV32mr; break; |
Nate Begeman | 8a09336 | 2005-07-06 18:59:04 +0000 | [diff] [blame] | 4513 | case MVT::f32: Opc = X86::MOVSSmr; break; |
| 4514 | case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4515 | } |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 4516 | |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4517 | std::vector<std::pair<unsigned, unsigned> > RP; |
| 4518 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0)); |
| 4519 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1)); |
| 4520 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2)); |
| 4521 | std::sort(RP.begin(), RP.end()); |
| 4522 | |
Chris Lattner | 80c5b97 | 2005-02-23 05:57:21 +0000 | [diff] [blame] | 4523 | Tmp1 = 0; // Silence a warning. |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4524 | for (unsigned i = 0; i != 3; ++i) |
| 4525 | switch (RP[2-i].second) { |
| 4526 | default: assert(0 && "Unknown operand number!"); |
| 4527 | case 0: Select(N.getOperand(0)); break; |
| 4528 | case 1: Tmp1 = SelectExpr(N.getOperand(1)); break; |
Chris Lattner | 8fea42b | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 4529 | case 2: SelectAddress(N.getOperand(2), AM); break; |
Chris Lattner | 0d1f82a | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4532 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1); |
| 4533 | return; |
| 4534 | } |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 4535 | case ISD::CALLSEQ_START: |
Chris Lattner | c0e369e | 2005-05-13 21:44:04 +0000 | [diff] [blame] | 4536 | Select(N.getOperand(0)); |
| 4537 | // Stack amount |
| 4538 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 4539 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1); |
| 4540 | return; |
Chris Lattner | 2dce703 | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 4541 | case ISD::CALLSEQ_END: |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4542 | Select(N.getOperand(0)); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4543 | return; |
Chris Lattner | 36f7848 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 4544 | case ISD::MEMSET: { |
| 4545 | Select(N.getOperand(0)); // Select the chain. |
| 4546 | unsigned Align = |
| 4547 | (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue(); |
| 4548 | if (Align == 0) Align = 1; |
| 4549 | |
| 4550 | // Turn the byte code into # iterations |
| 4551 | unsigned CountReg; |
| 4552 | unsigned Opcode; |
| 4553 | if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) { |
| 4554 | unsigned Val = ValC->getValue() & 255; |
| 4555 | |
| 4556 | // If the value is a constant, then we can potentially use larger sets. |
| 4557 | switch (Align & 3) { |
| 4558 | case 2: // WORD aligned |
| 4559 | CountReg = MakeReg(MVT::i32); |
| 4560 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 4561 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2); |
| 4562 | } else { |
| 4563 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 4564 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1); |
| 4565 | } |
| 4566 | BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val); |
| 4567 | Opcode = X86::REP_STOSW; |
| 4568 | break; |
| 4569 | case 0: // DWORD aligned |
| 4570 | CountReg = MakeReg(MVT::i32); |
| 4571 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 4572 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4); |
| 4573 | } else { |
| 4574 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 4575 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2); |
| 4576 | } |
| 4577 | Val = (Val << 8) | Val; |
| 4578 | BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val); |
| 4579 | Opcode = X86::REP_STOSD; |
| 4580 | break; |
| 4581 | default: // BYTE aligned |
| 4582 | CountReg = SelectExpr(Node->getOperand(3)); |
| 4583 | BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val); |
| 4584 | Opcode = X86::REP_STOSB; |
| 4585 | break; |
| 4586 | } |
| 4587 | } else { |
| 4588 | // If it's not a constant value we are storing, just fall back. We could |
| 4589 | // try to be clever to form 16 bit and 32 bit values, but we don't yet. |
| 4590 | unsigned ValReg = SelectExpr(Node->getOperand(2)); |
| 4591 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg); |
| 4592 | CountReg = SelectExpr(Node->getOperand(3)); |
| 4593 | Opcode = X86::REP_STOSB; |
| 4594 | } |
| 4595 | |
| 4596 | // No matter what the alignment is, we put the source in ESI, the |
| 4597 | // destination in EDI, and the count in ECX. |
| 4598 | unsigned TmpReg1 = SelectExpr(Node->getOperand(1)); |
| 4599 | BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg); |
| 4600 | BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1); |
| 4601 | BuildMI(BB, Opcode, 0); |
| 4602 | return; |
| 4603 | } |
Chris Lattner | 70ea07c | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 4604 | case ISD::MEMCPY: { |
Chris Lattner | c07164e | 2005-01-11 06:19:26 +0000 | [diff] [blame] | 4605 | Select(N.getOperand(0)); // Select the chain. |
| 4606 | unsigned Align = |
| 4607 | (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue(); |
| 4608 | if (Align == 0) Align = 1; |
| 4609 | |
| 4610 | // Turn the byte code into # iterations |
| 4611 | unsigned CountReg; |
| 4612 | unsigned Opcode; |
| 4613 | switch (Align & 3) { |
| 4614 | case 2: // WORD aligned |
| 4615 | CountReg = MakeReg(MVT::i32); |
| 4616 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 4617 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2); |
| 4618 | } else { |
| 4619 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 4620 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1); |
| 4621 | } |
| 4622 | Opcode = X86::REP_MOVSW; |
| 4623 | break; |
| 4624 | case 0: // DWORD aligned |
| 4625 | CountReg = MakeReg(MVT::i32); |
| 4626 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 4627 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4); |
| 4628 | } else { |
| 4629 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 4630 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2); |
| 4631 | } |
| 4632 | Opcode = X86::REP_MOVSD; |
| 4633 | break; |
| 4634 | default: // BYTE aligned |
| 4635 | CountReg = SelectExpr(Node->getOperand(3)); |
| 4636 | Opcode = X86::REP_MOVSB; |
| 4637 | break; |
| 4638 | } |
| 4639 | |
| 4640 | // No matter what the alignment is, we put the source in ESI, the |
| 4641 | // destination in EDI, and the count in ECX. |
| 4642 | unsigned TmpReg1 = SelectExpr(Node->getOperand(1)); |
| 4643 | unsigned TmpReg2 = SelectExpr(Node->getOperand(2)); |
| 4644 | BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg); |
| 4645 | BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1); |
| 4646 | BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2); |
| 4647 | BuildMI(BB, Opcode, 0); |
| 4648 | return; |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4649 | } |
Chris Lattner | 70ea07c | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 4650 | case ISD::WRITEPORT: |
| 4651 | if (Node->getOperand(2).getValueType() != MVT::i16) { |
| 4652 | std::cerr << "llvm.writeport: Address size is not 16 bits\n"; |
| 4653 | exit(1); |
| 4654 | } |
| 4655 | Select(Node->getOperand(0)); // Emit the chain. |
| 4656 | |
| 4657 | Tmp1 = SelectExpr(Node->getOperand(1)); |
| 4658 | switch (Node->getOperand(1).getValueType()) { |
| 4659 | case MVT::i8: |
| 4660 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 4661 | Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr; |
| 4662 | break; |
| 4663 | case MVT::i16: |
| 4664 | BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1); |
| 4665 | Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr; |
| 4666 | break; |
| 4667 | case MVT::i32: |
| 4668 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 4669 | Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr; |
| 4670 | break; |
| 4671 | default: |
| 4672 | std::cerr << "llvm.writeport: invalid data type for X86 target"; |
| 4673 | exit(1); |
| 4674 | } |
| 4675 | |
| 4676 | // If the port is a single-byte constant, use the immediate form. |
| 4677 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2))) |
| 4678 | if ((CN->getValue() & 255) == CN->getValue()) { |
| 4679 | BuildMI(BB, Tmp2, 1).addImm(CN->getValue()); |
| 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | // Otherwise, move the I/O port address into the DX register. |
| 4684 | unsigned Reg = SelectExpr(Node->getOperand(2)); |
| 4685 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg); |
| 4686 | BuildMI(BB, Opc, 0); |
| 4687 | return; |
| 4688 | } |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4689 | assert(0 && "Should not be reached!"); |
| 4690 | } |
| 4691 | |
| 4692 | |
| 4693 | /// createX86PatternInstructionSelector - This pass converts an LLVM function |
| 4694 | /// into a machine code representation using pattern matching and a machine |
| 4695 | /// description file. |
| 4696 | /// |
| 4697 | FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) { |
Misha Brukman | c88330a | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 4698 | return new ISel(TM); |
Chris Lattner | 88c8a23 | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 4699 | } |