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