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