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