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