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