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