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