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