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