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