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