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