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