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