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); |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 365 | bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, 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 | |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame^] | 446 | // Is the base register already occupied? |
| 447 | if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) { |
| 448 | // If so, check to see if the scale index register is set. |
| 449 | if (AM.IndexReg == 0) { |
| 450 | AM.IndexReg = SelectExpr(N); |
| 451 | AM.Scale = 1; |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | // Otherwise, we cannot select it. |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 456 | return true; |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame^] | 457 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 458 | |
| 459 | // Default, generate it as a register. |
| 460 | AM.BaseType = X86AddressMode::RegBase; |
| 461 | AM.Base.Reg = SelectExpr(N); |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | /// Emit2SetCCsAndLogical - Emit the following sequence of instructions, |
| 466 | /// assuming that the temporary registers are in the 8-bit register class. |
| 467 | /// |
| 468 | /// Tmp1 = setcc1 |
| 469 | /// Tmp2 = setcc2 |
| 470 | /// DestReg = logicalop Tmp1, Tmp2 |
| 471 | /// |
| 472 | static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1, |
| 473 | unsigned SetCC2, unsigned LogicalOp, |
| 474 | unsigned DestReg) { |
| 475 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 476 | unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 477 | unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 478 | BuildMI(BB, SetCC1, 0, Tmp1); |
| 479 | BuildMI(BB, SetCC2, 0, Tmp2); |
| 480 | BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2); |
| 481 | } |
| 482 | |
| 483 | /// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the |
| 484 | /// condition codes match the specified SetCCOpcode. Note that some conditions |
| 485 | /// require multiple instructions to generate the correct value. |
| 486 | static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg, |
| 487 | ISD::CondCode SetCCOpcode, bool isFP) { |
| 488 | unsigned Opc; |
| 489 | if (!isFP) { |
| 490 | switch (SetCCOpcode) { |
| 491 | default: assert(0 && "Illegal integer SetCC!"); |
| 492 | case ISD::SETEQ: Opc = X86::SETEr; break; |
| 493 | case ISD::SETGT: Opc = X86::SETGr; break; |
| 494 | case ISD::SETGE: Opc = X86::SETGEr; break; |
| 495 | case ISD::SETLT: Opc = X86::SETLr; break; |
| 496 | case ISD::SETLE: Opc = X86::SETLEr; break; |
| 497 | case ISD::SETNE: Opc = X86::SETNEr; break; |
| 498 | case ISD::SETULT: Opc = X86::SETBr; break; |
| 499 | case ISD::SETUGT: Opc = X86::SETAr; break; |
| 500 | case ISD::SETULE: Opc = X86::SETBEr; break; |
| 501 | case ISD::SETUGE: Opc = X86::SETAEr; break; |
| 502 | } |
| 503 | } else { |
| 504 | // On a floating point condition, the flags are set as follows: |
| 505 | // ZF PF CF op |
| 506 | // 0 | 0 | 0 | X > Y |
| 507 | // 0 | 0 | 1 | X < Y |
| 508 | // 1 | 0 | 0 | X == Y |
| 509 | // 1 | 1 | 1 | unordered |
| 510 | // |
| 511 | switch (SetCCOpcode) { |
| 512 | default: assert(0 && "Invalid FP setcc!"); |
| 513 | case ISD::SETUEQ: |
| 514 | case ISD::SETEQ: |
| 515 | Opc = X86::SETEr; // True if ZF = 1 |
| 516 | break; |
| 517 | case ISD::SETOGT: |
| 518 | case ISD::SETGT: |
| 519 | Opc = X86::SETAr; // True if CF = 0 and ZF = 0 |
| 520 | break; |
| 521 | case ISD::SETOGE: |
| 522 | case ISD::SETGE: |
| 523 | Opc = X86::SETAEr; // True if CF = 0 |
| 524 | break; |
| 525 | case ISD::SETULT: |
| 526 | case ISD::SETLT: |
| 527 | Opc = X86::SETBr; // True if CF = 1 |
| 528 | break; |
| 529 | case ISD::SETULE: |
| 530 | case ISD::SETLE: |
| 531 | Opc = X86::SETBEr; // True if CF = 1 or ZF = 1 |
| 532 | break; |
| 533 | case ISD::SETONE: |
| 534 | case ISD::SETNE: |
| 535 | Opc = X86::SETNEr; // True if ZF = 0 |
| 536 | break; |
| 537 | case ISD::SETUO: |
| 538 | Opc = X86::SETPr; // True if PF = 1 |
| 539 | break; |
| 540 | case ISD::SETO: |
| 541 | Opc = X86::SETNPr; // True if PF = 0 |
| 542 | break; |
| 543 | case ISD::SETOEQ: // !PF & ZF |
| 544 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg); |
| 545 | return; |
| 546 | case ISD::SETOLT: // !PF & CF |
| 547 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg); |
| 548 | return; |
| 549 | case ISD::SETOLE: // !PF & (CF || ZF) |
| 550 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg); |
| 551 | return; |
| 552 | case ISD::SETUGT: // PF | (!ZF & !CF) |
| 553 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg); |
| 554 | return; |
| 555 | case ISD::SETUGE: // PF | !CF |
| 556 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg); |
| 557 | return; |
| 558 | case ISD::SETUNE: // PF | !ZF |
| 559 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg); |
| 560 | return; |
| 561 | } |
| 562 | } |
| 563 | BuildMI(BB, Opc, 0, DestReg); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /// EmitBranchCC - Emit code into BB that arranges for control to transfer to |
| 568 | /// the Dest block if the Cond condition is true. If we cannot fold this |
| 569 | /// condition into the branch, return true. |
| 570 | /// |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 571 | bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, |
| 572 | SDOperand Cond) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 573 | // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A > |
| 574 | // B) using two conditional branches instead of one condbr, two setcc's, and |
| 575 | // an or. |
| 576 | if ((Cond.getOpcode() == ISD::OR || |
| 577 | Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) { |
| 578 | // And and or set the flags for us, so there is no need to emit a TST of the |
| 579 | // result. It is only safe to do this if there is only a single use of the |
| 580 | // AND/OR though, otherwise we don't know it will be emitted here. |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 581 | Select(Chain); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 582 | SelectExpr(Cond); |
| 583 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | // Codegen br not C -> JE. |
| 588 | if (Cond.getOpcode() == ISD::XOR) |
| 589 | if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1))) |
| 590 | if (NC->isAllOnesValue()) { |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 591 | unsigned CondR; |
| 592 | if (getRegPressure(Chain) > getRegPressure(Cond)) { |
| 593 | Select(Chain); |
| 594 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 595 | } else { |
| 596 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 597 | Select(Chain); |
| 598 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 599 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR); |
| 600 | BuildMI(BB, X86::JE, 1).addMBB(Dest); |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond); |
| 605 | if (SetCC == 0) |
| 606 | return true; // Can only handle simple setcc's so far. |
| 607 | |
| 608 | unsigned Opc; |
| 609 | |
| 610 | // Handle integer conditions first. |
| 611 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 612 | switch (SetCC->getCondition()) { |
| 613 | default: assert(0 && "Illegal integer SetCC!"); |
| 614 | case ISD::SETEQ: Opc = X86::JE; break; |
| 615 | case ISD::SETGT: Opc = X86::JG; break; |
| 616 | case ISD::SETGE: Opc = X86::JGE; break; |
| 617 | case ISD::SETLT: Opc = X86::JL; break; |
| 618 | case ISD::SETLE: Opc = X86::JLE; break; |
| 619 | case ISD::SETNE: Opc = X86::JNE; break; |
| 620 | case ISD::SETULT: Opc = X86::JB; break; |
| 621 | case ISD::SETUGT: Opc = X86::JA; break; |
| 622 | case ISD::SETULE: Opc = X86::JBE; break; |
| 623 | case ISD::SETUGE: Opc = X86::JAE; break; |
| 624 | } |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 625 | Select(Chain); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 626 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1)); |
| 627 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 628 | return false; |
| 629 | } |
| 630 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 631 | unsigned Opc2 = 0; // Second branch if needed. |
| 632 | |
| 633 | // On a floating point condition, the flags are set as follows: |
| 634 | // ZF PF CF op |
| 635 | // 0 | 0 | 0 | X > Y |
| 636 | // 0 | 0 | 1 | X < Y |
| 637 | // 1 | 0 | 0 | X == Y |
| 638 | // 1 | 1 | 1 | unordered |
| 639 | // |
| 640 | switch (SetCC->getCondition()) { |
| 641 | default: assert(0 && "Invalid FP setcc!"); |
| 642 | case ISD::SETUEQ: |
| 643 | case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1 |
| 644 | case ISD::SETOGT: |
| 645 | case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0 |
| 646 | case ISD::SETOGE: |
| 647 | case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0 |
| 648 | case ISD::SETULT: |
| 649 | case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1 |
| 650 | case ISD::SETULE: |
| 651 | case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1 |
| 652 | case ISD::SETONE: |
| 653 | case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0 |
| 654 | case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1 |
| 655 | case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0 |
| 656 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 657 | Opc = X86::JA; // ZF = 0 & CF = 0 |
| 658 | Opc2 = X86::JP; // PF = 1 |
| 659 | break; |
| 660 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 661 | Opc = X86::JAE; // CF = 0 |
| 662 | Opc2 = X86::JP; // PF = 1 |
| 663 | break; |
| 664 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 665 | Opc = X86::JNE; // ZF = 0 |
| 666 | Opc2 = X86::JP; // PF = 1 |
| 667 | break; |
| 668 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 669 | //X86::JNP, X86::JE |
| 670 | //X86::AND8rr |
| 671 | return true; // FIXME: Emit more efficient code for this branch. |
| 672 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 673 | //X86::JNP, X86::JB |
| 674 | //X86::AND8rr |
| 675 | return true; // FIXME: Emit more efficient code for this branch. |
| 676 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 677 | //X86::JNP, X86::JBE |
| 678 | //X86::AND8rr |
| 679 | return true; // FIXME: Emit more efficient code for this branch. |
| 680 | } |
| 681 | |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 682 | Select(Chain); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 683 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1)); |
| 684 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 685 | if (Opc2) |
| 686 | BuildMI(BB, Opc2, 1).addMBB(Dest); |
| 687 | return false; |
| 688 | } |
| 689 | |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 690 | /// EmitSelectCC - Emit code into BB that performs a select operation between |
| 691 | /// the two registers RTrue and RFalse, generating a result into RDest. Return |
| 692 | /// true if the fold cannot be performed. |
| 693 | /// |
| 694 | void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT, |
| 695 | unsigned RTrue, unsigned RFalse, unsigned RDest) { |
| 696 | enum Condition { |
| 697 | EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP, |
| 698 | NOT_SET |
| 699 | } CondCode = NOT_SET; |
| 700 | |
| 701 | static const unsigned CMOVTAB16[] = { |
| 702 | X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr, |
| 703 | X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr, |
| 704 | X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr, |
| 705 | }; |
| 706 | static const unsigned CMOVTAB32[] = { |
| 707 | X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr, |
| 708 | X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr, |
| 709 | X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr, |
| 710 | }; |
| 711 | static const unsigned CMOVTABFP[] = { |
| 712 | X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0, |
| 713 | /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE, |
| 714 | X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP |
| 715 | }; |
| 716 | |
| 717 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) { |
| 718 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 719 | switch (SetCC->getCondition()) { |
| 720 | default: assert(0 && "Unknown integer comparison!"); |
| 721 | case ISD::SETEQ: CondCode = EQ; break; |
| 722 | case ISD::SETGT: CondCode = GT; break; |
| 723 | case ISD::SETGE: CondCode = GE; break; |
| 724 | case ISD::SETLT: CondCode = LT; break; |
| 725 | case ISD::SETLE: CondCode = LE; break; |
| 726 | case ISD::SETNE: CondCode = NE; break; |
| 727 | case ISD::SETULT: CondCode = B; break; |
| 728 | case ISD::SETUGT: CondCode = A; break; |
| 729 | case ISD::SETULE: CondCode = BE; break; |
| 730 | case ISD::SETUGE: CondCode = AE; break; |
| 731 | } |
| 732 | } else { |
| 733 | // On a floating point condition, the flags are set as follows: |
| 734 | // ZF PF CF op |
| 735 | // 0 | 0 | 0 | X > Y |
| 736 | // 0 | 0 | 1 | X < Y |
| 737 | // 1 | 0 | 0 | X == Y |
| 738 | // 1 | 1 | 1 | unordered |
| 739 | // |
| 740 | switch (SetCC->getCondition()) { |
| 741 | default: assert(0 && "Unknown FP comparison!"); |
| 742 | case ISD::SETUEQ: |
| 743 | case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1 |
| 744 | case ISD::SETOGT: |
| 745 | case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0 |
| 746 | case ISD::SETOGE: |
| 747 | case ISD::SETGE: CondCode = AE; break; // True if CF = 0 |
| 748 | case ISD::SETULT: |
| 749 | case ISD::SETLT: CondCode = B; break; // True if CF = 1 |
| 750 | case ISD::SETULE: |
| 751 | case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1 |
| 752 | case ISD::SETONE: |
| 753 | case ISD::SETNE: CondCode = NE; break; // True if ZF = 0 |
| 754 | case ISD::SETUO: CondCode = P; break; // True if PF = 1 |
| 755 | case ISD::SETO: CondCode = NP; break; // True if PF = 0 |
| 756 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 757 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 758 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 759 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 760 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 761 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 762 | // We cannot emit this comparison as a single cmov. |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | unsigned Opc = 0; |
| 769 | if (CondCode != NOT_SET) { |
| 770 | switch (SVT) { |
| 771 | default: assert(0 && "Cannot select this type!"); |
| 772 | case MVT::i16: Opc = CMOVTAB16[CondCode]; break; |
| 773 | case MVT::i32: Opc = CMOVTAB32[CondCode]; break; |
| 774 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 775 | case MVT::f64: Opc = CMOVTABFP[CondCode]; break; |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | // Finally, if we weren't able to fold this, just emit the condition and test |
| 780 | // it. |
| 781 | if (CondCode == NOT_SET || Opc == 0) { |
| 782 | // Get the condition into the zero flag. |
| 783 | unsigned CondReg = SelectExpr(Cond); |
| 784 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg); |
| 785 | |
| 786 | switch (SVT) { |
| 787 | default: assert(0 && "Cannot select this type!"); |
| 788 | case MVT::i16: Opc = X86::CMOVE16rr; break; |
| 789 | case MVT::i32: Opc = X86::CMOVE32rr; break; |
| 790 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 791 | case MVT::f64: Opc = X86::FCMOVE; break; |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 792 | } |
| 793 | } else { |
| 794 | // FIXME: CMP R, 0 -> TEST R, R |
| 795 | EmitCMP(Cond.getOperand(0), Cond.getOperand(1)); |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 796 | std::swap(RTrue, RFalse); |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 797 | } |
| 798 | BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse); |
| 799 | } |
| 800 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 801 | void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 802 | unsigned Opc; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 803 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) { |
| 804 | Opc = 0; |
| 805 | switch (RHS.getValueType()) { |
| 806 | default: break; |
| 807 | case MVT::i1: |
| 808 | case MVT::i8: Opc = X86::CMP8ri; break; |
| 809 | case MVT::i16: Opc = X86::CMP16ri; break; |
| 810 | case MVT::i32: Opc = X86::CMP32ri; break; |
| 811 | } |
| 812 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 813 | unsigned Tmp1 = SelectExpr(LHS); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 814 | BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue()); |
| 815 | return; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | switch (LHS.getValueType()) { |
| 820 | default: assert(0 && "Cannot compare this value!"); |
| 821 | case MVT::i1: |
| 822 | case MVT::i8: Opc = X86::CMP8rr; break; |
| 823 | case MVT::i16: Opc = X86::CMP16rr; break; |
| 824 | case MVT::i32: Opc = X86::CMP32rr; break; |
| 825 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 826 | case MVT::f64: Opc = X86::FUCOMIr; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 827 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 828 | unsigned Tmp1, Tmp2; |
| 829 | if (getRegPressure(LHS) > getRegPressure(RHS)) { |
| 830 | Tmp1 = SelectExpr(LHS); |
| 831 | Tmp2 = SelectExpr(RHS); |
| 832 | } else { |
| 833 | Tmp2 = SelectExpr(RHS); |
| 834 | Tmp1 = SelectExpr(LHS); |
| 835 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 836 | BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2); |
| 837 | } |
| 838 | |
| 839 | unsigned ISel::SelectExpr(SDOperand N) { |
| 840 | unsigned Result; |
| 841 | unsigned Tmp1, Tmp2, Tmp3; |
| 842 | unsigned Opc = 0; |
| 843 | |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 844 | SDNode *Node = N.Val; |
| 845 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 846 | if (Node->getOpcode() == ISD::CopyFromReg) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 847 | // Just use the specified register as our input. |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 848 | return dyn_cast<CopyRegSDNode>(Node)->getReg(); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 849 | |
| 850 | // If there are multiple uses of this expression, memorize the |
| 851 | // register it is code generated in, instead of emitting it multiple |
| 852 | // times. |
| 853 | // FIXME: Disabled for our current selection model. |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 854 | if (1 || !Node->hasOneUse()) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 855 | unsigned &Reg = ExprMap[N]; |
| 856 | if (Reg) return Reg; |
| 857 | |
| 858 | if (N.getOpcode() != ISD::CALL) |
| 859 | Reg = Result = (N.getValueType() != MVT::Other) ? |
| 860 | MakeReg(N.getValueType()) : 1; |
| 861 | else { |
| 862 | // If this is a call instruction, make sure to prepare ALL of the result |
| 863 | // values as well as the chain. |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 864 | if (Node->getNumValues() == 1) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 865 | Reg = Result = 1; // Void call, just a chain. |
| 866 | else { |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 867 | Result = MakeReg(Node->getValueType(0)); |
| 868 | ExprMap[N.getValue(0)] = Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 869 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 870 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 871 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } else { |
| 875 | Result = MakeReg(N.getValueType()); |
| 876 | } |
| 877 | |
| 878 | switch (N.getOpcode()) { |
| 879 | default: |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 880 | Node->dump(); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 881 | assert(0 && "Node not handled!\n"); |
| 882 | case ISD::FrameIndex: |
| 883 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
| 884 | addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1); |
| 885 | return Result; |
| 886 | case ISD::ConstantPool: |
| 887 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 888 | addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1); |
| 889 | return Result; |
| 890 | case ISD::ConstantFP: |
| 891 | ContainsFPCode = true; |
| 892 | Tmp1 = Result; // Intermediate Register |
| 893 | if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 || |
| 894 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 895 | Tmp1 = MakeReg(MVT::f64); |
| 896 | |
| 897 | if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) || |
| 898 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 899 | BuildMI(BB, X86::FLD0, 0, Tmp1); |
| 900 | else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) || |
| 901 | cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0)) |
| 902 | BuildMI(BB, X86::FLD1, 0, Tmp1); |
| 903 | else |
| 904 | assert(0 && "Unexpected constant!"); |
| 905 | if (Tmp1 != Result) |
| 906 | BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); |
| 907 | return Result; |
| 908 | case ISD::Constant: |
| 909 | switch (N.getValueType()) { |
| 910 | default: assert(0 && "Cannot use constants of this type!"); |
| 911 | case MVT::i1: |
| 912 | case MVT::i8: Opc = X86::MOV8ri; break; |
| 913 | case MVT::i16: Opc = X86::MOV16ri; break; |
| 914 | case MVT::i32: Opc = X86::MOV32ri; break; |
| 915 | } |
| 916 | BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue()); |
| 917 | return Result; |
| 918 | case ISD::GlobalAddress: { |
| 919 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 920 | BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV); |
| 921 | return Result; |
| 922 | } |
| 923 | case ISD::ExternalSymbol: { |
| 924 | const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol(); |
| 925 | BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym); |
| 926 | return Result; |
| 927 | } |
| 928 | case ISD::FP_EXTEND: |
| 929 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 930 | BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 931 | return Result; |
| 932 | case ISD::ZERO_EXTEND: { |
| 933 | int DestIs16 = N.getValueType() == MVT::i16; |
| 934 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 935 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 936 | |
| 937 | // FIXME: This hack is here for zero extension casts from bool to i8. This |
| 938 | // would not be needed if bools were promoted by Legalize. |
| 939 | if (N.getValueType() == MVT::i8) { |
| 940 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1); |
| 941 | return Result; |
| 942 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 943 | |
| 944 | static const unsigned Opc[3] = { |
| 945 | X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8 |
| 946 | }; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 947 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 948 | return Result; |
| 949 | } |
| 950 | case ISD::SIGN_EXTEND: { |
| 951 | int DestIs16 = N.getValueType() == MVT::i16; |
| 952 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
| 953 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 954 | // FIXME: Legalize should promote bools to i8! |
| 955 | assert(N.getOperand(0).getValueType() != MVT::i1 && |
| 956 | "Sign extend from bool not implemented!"); |
| 957 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 958 | static const unsigned Opc[3] = { |
| 959 | X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8 |
| 960 | }; |
| 961 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 962 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 963 | return Result; |
| 964 | } |
| 965 | case ISD::TRUNCATE: |
| 966 | // Handle cast of LARGER int to SMALLER int using a move to EAX followed by |
| 967 | // a move out of AX or AL. |
| 968 | switch (N.getOperand(0).getValueType()) { |
| 969 | default: assert(0 && "Unknown truncate!"); |
| 970 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 971 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 972 | case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break; |
| 973 | } |
| 974 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 975 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 976 | |
| 977 | switch (N.getValueType()) { |
| 978 | default: assert(0 && "Unknown truncate!"); |
| 979 | case MVT::i1: |
| 980 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 981 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 982 | } |
| 983 | BuildMI(BB, Opc, 1, Result).addReg(Tmp2); |
| 984 | return Result; |
| 985 | |
| 986 | case ISD::FP_ROUND: |
| 987 | // Truncate from double to float by storing to memory as float, |
| 988 | // then reading it back into a register. |
| 989 | |
| 990 | // Create as stack slot to use. |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 991 | // FIXME: This should automatically be made by the Legalizer! |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 992 | Tmp1 = TLI.getTargetData().getFloatAlignment(); |
| 993 | Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1); |
| 994 | |
| 995 | // Codegen the input. |
| 996 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 997 | |
| 998 | // Emit the store, then the reload. |
| 999 | addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1); |
| 1000 | addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1001 | return Result; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1002 | |
| 1003 | case ISD::SINT_TO_FP: |
| 1004 | case ISD::UINT_TO_FP: { |
| 1005 | // FIXME: Most of this grunt work should be done by legalize! |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1006 | ContainsFPCode = true; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Promote the integer to a type supported by FLD. We do this because there |
| 1009 | // are no unsigned FLD instructions, so we must promote an unsigned value to |
| 1010 | // a larger signed value, then use FLD on the larger value. |
| 1011 | // |
| 1012 | MVT::ValueType PromoteType = MVT::Other; |
| 1013 | MVT::ValueType SrcTy = N.getOperand(0).getValueType(); |
| 1014 | unsigned PromoteOpcode = 0; |
| 1015 | unsigned RealDestReg = Result; |
| 1016 | switch (SrcTy) { |
| 1017 | case MVT::i1: |
| 1018 | case MVT::i8: |
| 1019 | // We don't have the facilities for directly loading byte sized data from |
| 1020 | // memory (even signed). Promote it to 16 bits. |
| 1021 | PromoteType = MVT::i16; |
| 1022 | PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ? |
| 1023 | X86::MOVSX16rr8 : X86::MOVZX16rr8; |
| 1024 | break; |
| 1025 | case MVT::i16: |
| 1026 | if (Node->getOpcode() == ISD::UINT_TO_FP) { |
| 1027 | PromoteType = MVT::i32; |
| 1028 | PromoteOpcode = X86::MOVZX32rr16; |
| 1029 | } |
| 1030 | break; |
| 1031 | default: |
| 1032 | // Don't fild into the real destination. |
| 1033 | if (Node->getOpcode() == ISD::UINT_TO_FP) |
| 1034 | Result = MakeReg(Node->getValueType(0)); |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 1039 | |
| 1040 | if (PromoteType != MVT::Other) { |
| 1041 | Tmp2 = MakeReg(PromoteType); |
| 1042 | BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1); |
| 1043 | SrcTy = PromoteType; |
| 1044 | Tmp1 = Tmp2; |
| 1045 | } |
| 1046 | |
| 1047 | // Spill the integer to memory and reload it from there. |
| 1048 | unsigned Size = MVT::getSizeInBits(SrcTy)/8; |
| 1049 | MachineFunction *F = BB->getParent(); |
| 1050 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 1051 | |
| 1052 | switch (SrcTy) { |
| 1053 | case MVT::i64: |
| 1054 | // FIXME: this won't work for cast [u]long to FP |
| 1055 | addFrameReference(BuildMI(BB, X86::MOV32mr, 5), |
| 1056 | FrameIdx).addReg(Tmp1); |
| 1057 | addFrameReference(BuildMI(BB, X86::MOV32mr, 5), |
| 1058 | FrameIdx, 4).addReg(Tmp1+1); |
| 1059 | addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx); |
| 1060 | break; |
| 1061 | case MVT::i32: |
| 1062 | addFrameReference(BuildMI(BB, X86::MOV32mr, 5), |
| 1063 | FrameIdx).addReg(Tmp1); |
| 1064 | addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx); |
| 1065 | break; |
| 1066 | case MVT::i16: |
| 1067 | addFrameReference(BuildMI(BB, X86::MOV16mr, 5), |
| 1068 | FrameIdx).addReg(Tmp1); |
| 1069 | addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx); |
| 1070 | break; |
| 1071 | default: break; // No promotion required. |
| 1072 | } |
| 1073 | |
| 1074 | if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) { |
| 1075 | // If this is a cast from uint -> double, we need to be careful when if |
| 1076 | // the "sign" bit is set. If so, we don't want to make a negative number, |
| 1077 | // we want to make a positive number. Emit code to add an offset if the |
| 1078 | // sign bit is set. |
| 1079 | |
| 1080 | // Compute whether the sign bit is set by shifting the reg right 31 bits. |
| 1081 | unsigned IsNeg = MakeReg(MVT::i32); |
| 1082 | BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31); |
| 1083 | |
| 1084 | // Create a CP value that has the offset in one word and 0 in the other. |
| 1085 | static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy, |
| 1086 | 0x4f80000000000000ULL); |
| 1087 | unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset); |
| 1088 | BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result) |
| 1089 | .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0); |
| 1090 | |
| 1091 | } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) { |
| 1092 | // We need special handling for unsigned 64-bit integer sources. If the |
| 1093 | // input number has the "sign bit" set, then we loaded it incorrectly as a |
| 1094 | // negative 64-bit number. In this case, add an offset value. |
| 1095 | |
| 1096 | // Emit a test instruction to see if the dynamic input value was signed. |
| 1097 | BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1); |
| 1098 | |
| 1099 | // If the sign bit is set, get a pointer to an offset, otherwise get a |
| 1100 | // pointer to a zero. |
| 1101 | MachineConstantPool *CP = F->getConstantPool(); |
| 1102 | unsigned Zero = MakeReg(MVT::i32); |
| 1103 | Constant *Null = Constant::getNullValue(Type::UIntTy); |
| 1104 | addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero), |
| 1105 | CP->getConstantPoolIndex(Null)); |
| 1106 | unsigned Offset = MakeReg(MVT::i32); |
| 1107 | Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000); |
| 1108 | |
| 1109 | addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset), |
| 1110 | CP->getConstantPoolIndex(OffsetCst)); |
| 1111 | unsigned Addr = MakeReg(MVT::i32); |
| 1112 | BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset); |
| 1113 | |
| 1114 | // Load the constant for an add. FIXME: this could make an 'fadd' that |
| 1115 | // reads directly from memory, but we don't support these yet. |
| 1116 | unsigned ConstReg = MakeReg(MVT::f64); |
| 1117 | addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr); |
| 1118 | |
| 1119 | BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result); |
| 1120 | } |
| 1121 | return RealDestReg; |
| 1122 | } |
| 1123 | case ISD::FP_TO_SINT: |
| 1124 | case ISD::FP_TO_UINT: { |
| 1125 | // FIXME: Most of this grunt work should be done by legalize! |
| 1126 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 1127 | |
| 1128 | // Change the floating point control register to use "round towards zero" |
| 1129 | // mode when truncating to an integer value. |
| 1130 | // |
| 1131 | MachineFunction *F = BB->getParent(); |
| 1132 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 1133 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 1134 | |
| 1135 | // Load the old value of the high byte of the control word... |
| 1136 | unsigned HighPartOfCW = MakeReg(MVT::i8); |
| 1137 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW), |
| 1138 | CWFrameIdx, 1); |
| 1139 | |
| 1140 | // Set the high part to be round to zero... |
| 1141 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 1142 | CWFrameIdx, 1).addImm(12); |
| 1143 | |
| 1144 | // Reload the modified control word now... |
| 1145 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1146 | |
| 1147 | // Restore the memory image of control word to original value |
| 1148 | addFrameReference(BuildMI(BB, X86::MOV8mr, 5), |
| 1149 | CWFrameIdx, 1).addReg(HighPartOfCW); |
| 1150 | |
| 1151 | // We don't have the facilities for directly storing byte sized data to |
| 1152 | // memory. Promote it to 16 bits. We also must promote unsigned values to |
| 1153 | // larger classes because we only have signed FP stores. |
| 1154 | MVT::ValueType StoreClass = Node->getValueType(0); |
| 1155 | if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT) |
| 1156 | switch (StoreClass) { |
| 1157 | case MVT::i8: StoreClass = MVT::i16; break; |
| 1158 | case MVT::i16: StoreClass = MVT::i32; break; |
| 1159 | case MVT::i32: StoreClass = MVT::i64; break; |
| 1160 | // The following treatment of cLong may not be perfectly right, |
| 1161 | // but it survives chains of casts of the form |
| 1162 | // double->ulong->double. |
| 1163 | case MVT::i64: StoreClass = MVT::i64; break; |
| 1164 | default: assert(0 && "Unknown store class!"); |
| 1165 | } |
| 1166 | |
| 1167 | // Spill the integer to memory and reload it from there. |
| 1168 | unsigned Size = MVT::getSizeInBits(StoreClass)/8; |
| 1169 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 1170 | |
| 1171 | switch (StoreClass) { |
| 1172 | default: assert(0 && "Unknown store class!"); |
| 1173 | case MVT::i16: |
| 1174 | addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1); |
| 1175 | break; |
| 1176 | case MVT::i32: |
Chris Lattner | 2502085 | 2005-01-09 19:49:59 +0000 | [diff] [blame] | 1177 | addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | case MVT::i64: |
Chris Lattner | 2502085 | 2005-01-09 19:49:59 +0000 | [diff] [blame] | 1180 | addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | switch (Node->getValueType(0)) { |
| 1185 | default: |
| 1186 | assert(0 && "Unknown integer type!"); |
| 1187 | case MVT::i64: |
| 1188 | // FIXME: this isn't gunna work. |
| 1189 | addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx); |
| 1190 | addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4); |
| 1191 | case MVT::i32: |
| 1192 | addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx); |
| 1193 | break; |
| 1194 | case MVT::i16: |
| 1195 | addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx); |
| 1196 | break; |
| 1197 | case MVT::i8: |
| 1198 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx); |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | // Reload the original control word now. |
| 1203 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 1204 | return Result; |
| 1205 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1206 | case ISD::ADD: |
| 1207 | // See if we can codegen this as an LEA to fold operations together. |
| 1208 | if (N.getValueType() == MVT::i32) { |
| 1209 | X86AddressMode AM; |
| 1210 | if (!SelectAddress(N.getOperand(0), AM) && |
| 1211 | !SelectAddress(N.getOperand(1), AM)) { |
| 1212 | // 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] | 1213 | // 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] | 1214 | // leave this as LEA, then peephole it to 'ADD' after two address elim |
| 1215 | // happens. |
| 1216 | if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase || |
Chris Lattner | bd9f0ee | 2005-01-09 20:20:29 +0000 | [diff] [blame] | 1217 | AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1218 | addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM); |
| 1219 | return Result; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1224 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1225 | Opc = 0; |
| 1226 | if (CN->getValue() == 1) { // add X, 1 -> inc X |
| 1227 | switch (N.getValueType()) { |
| 1228 | default: assert(0 && "Cannot integer add this type!"); |
| 1229 | case MVT::i8: Opc = X86::INC8r; break; |
| 1230 | case MVT::i16: Opc = X86::INC16r; break; |
| 1231 | case MVT::i32: Opc = X86::INC32r; break; |
| 1232 | } |
| 1233 | } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X |
| 1234 | switch (N.getValueType()) { |
| 1235 | default: assert(0 && "Cannot integer add this type!"); |
| 1236 | case MVT::i8: Opc = X86::DEC8r; break; |
| 1237 | case MVT::i16: Opc = X86::DEC16r; break; |
| 1238 | case MVT::i32: Opc = X86::DEC32r; break; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1243 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1244 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 1245 | return Result; |
| 1246 | } |
| 1247 | |
| 1248 | switch (N.getValueType()) { |
| 1249 | default: assert(0 && "Cannot add this type!"); |
| 1250 | case MVT::i8: Opc = X86::ADD8ri; break; |
| 1251 | case MVT::i16: Opc = X86::ADD16ri; break; |
| 1252 | case MVT::i32: Opc = X86::ADD32ri; break; |
| 1253 | } |
| 1254 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1255 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1256 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1257 | return Result; |
| 1258 | } |
| 1259 | } |
| 1260 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1261 | switch (N.getValueType()) { |
| 1262 | default: assert(0 && "Cannot add this type!"); |
| 1263 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 1264 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 1265 | case MVT::i32: Opc = X86::ADD32rr; break; |
| 1266 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1267 | case MVT::f64: Opc = X86::FpADD; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1268 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1271 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1272 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1273 | } else { |
| 1274 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1275 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1276 | } |
| 1277 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1278 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1279 | return Result; |
| 1280 | case ISD::SUB: |
| 1281 | if (MVT::isInteger(N.getValueType())) |
| 1282 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0))) |
| 1283 | if (CN->isNullValue()) { // 0 - N -> neg N |
| 1284 | switch (N.getValueType()) { |
| 1285 | default: assert(0 && "Cannot sub this type!"); |
| 1286 | case MVT::i1: |
| 1287 | case MVT::i8: Opc = X86::NEG8r; break; |
| 1288 | case MVT::i16: Opc = X86::NEG16r; break; |
| 1289 | case MVT::i32: Opc = X86::NEG32r; break; |
| 1290 | } |
| 1291 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1292 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 1293 | return Result; |
| 1294 | } |
| 1295 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1296 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1297 | switch (N.getValueType()) { |
| 1298 | default: assert(0 && "Cannot sub this type!"); |
| 1299 | case MVT::i1: |
| 1300 | case MVT::i8: Opc = X86::SUB8ri; break; |
| 1301 | case MVT::i16: Opc = X86::SUB16ri; break; |
| 1302 | case MVT::i32: Opc = X86::SUB32ri; break; |
| 1303 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1304 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1305 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1306 | return Result; |
| 1307 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1310 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1311 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1312 | } else { |
| 1313 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1314 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1315 | } |
| 1316 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1317 | switch (N.getValueType()) { |
| 1318 | default: assert(0 && "Cannot add this type!"); |
| 1319 | case MVT::i1: |
| 1320 | case MVT::i8: Opc = X86::SUB8rr; break; |
| 1321 | case MVT::i16: Opc = X86::SUB16rr; break; |
| 1322 | case MVT::i32: Opc = X86::SUB32rr; break; |
| 1323 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1324 | case MVT::f64: Opc = X86::FpSUB; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1325 | } |
| 1326 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1327 | return Result; |
| 1328 | |
| 1329 | case ISD::AND: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1330 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1331 | switch (N.getValueType()) { |
| 1332 | default: assert(0 && "Cannot add this type!"); |
| 1333 | case MVT::i1: |
| 1334 | case MVT::i8: Opc = X86::AND8ri; break; |
| 1335 | case MVT::i16: Opc = X86::AND16ri; break; |
| 1336 | case MVT::i32: Opc = X86::AND32ri; break; |
| 1337 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1338 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1339 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1340 | return Result; |
| 1341 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1342 | |
| 1343 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1344 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1345 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1346 | } else { |
| 1347 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1348 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1349 | } |
| 1350 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1351 | switch (N.getValueType()) { |
| 1352 | default: assert(0 && "Cannot add this type!"); |
| 1353 | case MVT::i1: |
| 1354 | case MVT::i8: Opc = X86::AND8rr; break; |
| 1355 | case MVT::i16: Opc = X86::AND16rr; break; |
| 1356 | case MVT::i32: Opc = X86::AND32rr; break; |
| 1357 | } |
| 1358 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1359 | return Result; |
| 1360 | case ISD::OR: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1361 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1362 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1363 | switch (N.getValueType()) { |
| 1364 | default: assert(0 && "Cannot add this type!"); |
| 1365 | case MVT::i1: |
| 1366 | case MVT::i8: Opc = X86::OR8ri; break; |
| 1367 | case MVT::i16: Opc = X86::OR16ri; break; |
| 1368 | case MVT::i32: Opc = X86::OR32ri; break; |
| 1369 | } |
| 1370 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1371 | return Result; |
| 1372 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1373 | |
| 1374 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1375 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1376 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1377 | } else { |
| 1378 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1379 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1380 | } |
| 1381 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1382 | switch (N.getValueType()) { |
| 1383 | default: assert(0 && "Cannot add this type!"); |
| 1384 | case MVT::i1: |
| 1385 | case MVT::i8: Opc = X86::OR8rr; break; |
| 1386 | case MVT::i16: Opc = X86::OR16rr; break; |
| 1387 | case MVT::i32: Opc = X86::OR32rr; break; |
| 1388 | } |
| 1389 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1390 | return Result; |
| 1391 | case ISD::XOR: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1392 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1393 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (CN->isAllOnesValue()) { |
| 1396 | switch (N.getValueType()) { |
| 1397 | default: assert(0 && "Cannot add this type!"); |
| 1398 | case MVT::i1: |
| 1399 | case MVT::i8: Opc = X86::NOT8r; break; |
| 1400 | case MVT::i16: Opc = X86::NOT16r; break; |
| 1401 | case MVT::i32: Opc = X86::NOT32r; break; |
| 1402 | } |
| 1403 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 1404 | return Result; |
| 1405 | } |
| 1406 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1407 | switch (N.getValueType()) { |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 1408 | default: assert(0 && "Cannot xor this type!"); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1409 | case MVT::i1: |
| 1410 | case MVT::i8: Opc = X86::XOR8ri; break; |
| 1411 | case MVT::i16: Opc = X86::XOR16ri; break; |
| 1412 | case MVT::i32: Opc = X86::XOR32ri; break; |
| 1413 | } |
| 1414 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1415 | return Result; |
| 1416 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1419 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1420 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1421 | } else { |
| 1422 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1423 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1424 | } |
| 1425 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1426 | switch (N.getValueType()) { |
| 1427 | default: assert(0 && "Cannot add this type!"); |
| 1428 | case MVT::i1: |
| 1429 | case MVT::i8: Opc = X86::XOR8rr; break; |
| 1430 | case MVT::i16: Opc = X86::XOR16rr; break; |
| 1431 | case MVT::i32: Opc = X86::XOR32rr; break; |
| 1432 | } |
| 1433 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1434 | return Result; |
| 1435 | |
| 1436 | case ISD::MUL: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1437 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1438 | Opc = 0; |
| 1439 | switch (N.getValueType()) { |
| 1440 | default: assert(0 && "Cannot multiply this type!"); |
| 1441 | case MVT::i8: break; |
| 1442 | case MVT::i16: Opc = X86::IMUL16rri; break; |
| 1443 | case MVT::i32: Opc = X86::IMUL32rri; break; |
| 1444 | } |
| 1445 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1446 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1447 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1448 | return Result; |
| 1449 | } |
| 1450 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1451 | |
| 1452 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1453 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1454 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1455 | } else { |
| 1456 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1457 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1458 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1459 | switch (N.getValueType()) { |
| 1460 | default: assert(0 && "Cannot add this type!"); |
Chris Lattner | a13d323 | 2005-01-10 20:55:48 +0000 | [diff] [blame] | 1461 | case MVT::i8: |
| 1462 | // Must use the MUL instruction, which forces use of AL. |
| 1463 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 1464 | BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2); |
| 1465 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 1466 | return Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1467 | case MVT::i16: Opc = X86::IMUL16rr; break; |
| 1468 | case MVT::i32: Opc = X86::IMUL32rr; break; |
| 1469 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1470 | case MVT::f64: Opc = X86::FpMUL; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1471 | } |
| 1472 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1473 | return Result; |
| 1474 | |
| 1475 | case ISD::SELECT: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1476 | if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1477 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 1478 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1479 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 1480 | } else { |
| 1481 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 1482 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1483 | } |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1484 | EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1485 | return Result; |
| 1486 | } else { |
| 1487 | // FIXME: This should not be implemented here, it should be in the generic |
| 1488 | // code! |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 1489 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 1490 | Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16, |
| 1491 | N.getOperand(1))); |
| 1492 | Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16, |
| 1493 | N.getOperand(2))); |
| 1494 | } else { |
| 1495 | Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16, |
| 1496 | N.getOperand(2))); |
| 1497 | Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16, |
| 1498 | N.getOperand(1))); |
| 1499 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1500 | unsigned TmpReg = MakeReg(MVT::i16); |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1501 | EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg); |
| 1502 | // FIXME: need subregs to do better than this! |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1503 | BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg); |
| 1504 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 1505 | return Result; |
| 1506 | } |
| 1507 | |
| 1508 | case ISD::SDIV: |
| 1509 | case ISD::UDIV: |
| 1510 | case ISD::SREM: |
| 1511 | case ISD::UREM: { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1512 | if (N.getOpcode() == ISD::SDIV) |
| 1513 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1514 | // FIXME: These special cases should be handled by the lowering impl! |
| 1515 | unsigned RHS = CN->getValue(); |
| 1516 | bool isNeg = false; |
| 1517 | if ((int)RHS < 0) { |
| 1518 | isNeg = true; |
| 1519 | RHS = -RHS; |
| 1520 | } |
| 1521 | if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2? |
| 1522 | unsigned Log = log2(RHS); |
| 1523 | unsigned TmpReg = MakeReg(N.getValueType()); |
| 1524 | unsigned SAROpc, SHROpc, ADDOpc, NEGOpc; |
| 1525 | switch (N.getValueType()) { |
| 1526 | default: assert("Unknown type to signed divide!"); |
| 1527 | case MVT::i8: |
| 1528 | SAROpc = X86::SAR8ri; |
| 1529 | SHROpc = X86::SHR8ri; |
| 1530 | ADDOpc = X86::ADD8rr; |
| 1531 | NEGOpc = X86::NEG8r; |
| 1532 | break; |
| 1533 | case MVT::i16: |
| 1534 | SAROpc = X86::SAR16ri; |
| 1535 | SHROpc = X86::SHR16ri; |
| 1536 | ADDOpc = X86::ADD16rr; |
| 1537 | NEGOpc = X86::NEG16r; |
| 1538 | break; |
| 1539 | case MVT::i32: |
| 1540 | SAROpc = X86::SAR32ri; |
| 1541 | SHROpc = X86::SHR32ri; |
| 1542 | ADDOpc = X86::ADD32rr; |
| 1543 | NEGOpc = X86::NEG32r; |
| 1544 | break; |
| 1545 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1546 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1547 | BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1); |
| 1548 | unsigned TmpReg2 = MakeReg(N.getValueType()); |
| 1549 | BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log); |
| 1550 | unsigned TmpReg3 = MakeReg(N.getValueType()); |
| 1551 | BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2); |
| 1552 | |
| 1553 | unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result; |
| 1554 | BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log); |
| 1555 | if (isNeg) |
| 1556 | BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4); |
| 1557 | return Result; |
| 1558 | } |
| 1559 | } |
| 1560 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1561 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1562 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1563 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1564 | } else { |
| 1565 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1566 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1567 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1568 | |
| 1569 | bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM; |
| 1570 | bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV; |
| 1571 | unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode; |
| 1572 | switch (N.getValueType()) { |
| 1573 | default: assert(0 && "Cannot sdiv this type!"); |
| 1574 | case MVT::i8: |
| 1575 | DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r; |
| 1576 | LoReg = X86::AL; |
| 1577 | HiReg = X86::AH; |
| 1578 | MovOpcode = X86::MOV8rr; |
| 1579 | ClrOpcode = X86::MOV8ri; |
| 1580 | SExtOpcode = X86::CBW; |
| 1581 | break; |
| 1582 | case MVT::i16: |
| 1583 | DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r; |
| 1584 | LoReg = X86::AX; |
| 1585 | HiReg = X86::DX; |
| 1586 | MovOpcode = X86::MOV16rr; |
| 1587 | ClrOpcode = X86::MOV16ri; |
| 1588 | SExtOpcode = X86::CWD; |
| 1589 | break; |
| 1590 | case MVT::i32: |
| 1591 | DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r; |
| 1592 | LoReg =X86::EAX; |
| 1593 | HiReg = X86::EDX; |
| 1594 | MovOpcode = X86::MOV32rr; |
| 1595 | ClrOpcode = X86::MOV32ri; |
| 1596 | SExtOpcode = X86::CDQ; |
| 1597 | break; |
| 1598 | case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!"); |
| 1599 | case MVT::f32: |
| 1600 | case MVT::f64: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1601 | if (N.getOpcode() == ISD::SDIV) |
| 1602 | BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1603 | else |
| 1604 | assert(0 && "FIXME: Emit frem libcall to fmod!"); |
| 1605 | return Result; |
| 1606 | } |
| 1607 | |
| 1608 | // Set up the low part. |
| 1609 | BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1); |
| 1610 | |
| 1611 | if (isSigned) { |
| 1612 | // Sign extend the low part into the high part. |
| 1613 | BuildMI(BB, SExtOpcode, 0); |
| 1614 | } else { |
| 1615 | // Zero out the high part, effectively zero extending the input. |
| 1616 | BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0); |
| 1617 | } |
| 1618 | |
| 1619 | // Emit the DIV/IDIV instruction. |
| 1620 | BuildMI(BB, DivOpcode, 1).addReg(Tmp2); |
| 1621 | |
| 1622 | // Get the result of the divide or rem. |
| 1623 | BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg); |
| 1624 | return Result; |
| 1625 | } |
| 1626 | |
| 1627 | case ISD::SHL: |
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::SHL8ri; break; |
| 1632 | case MVT::i16: Opc = X86::SHL16ri; break; |
| 1633 | case MVT::i32: Opc = X86::SHL32ri; 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::SHL8rCL; break; |
| 1651 | case MVT::i16: Opc = X86::SHL16rCL; break; |
| 1652 | case MVT::i32: Opc = X86::SHL32rCL; 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::SRL: |
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::SHR8ri; break; |
| 1662 | case MVT::i16: Opc = X86::SHR16ri; break; |
| 1663 | case MVT::i32: Opc = X86::SHR32ri; 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::SHR8rCL; break; |
| 1681 | case MVT::i16: Opc = X86::SHR16rCL; break; |
| 1682 | case MVT::i32: Opc = X86::SHR32rCL; 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 | case ISD::SRA: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1688 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1689 | switch (N.getValueType()) { |
| 1690 | default: assert(0 && "Cannot shift this type!"); |
| 1691 | case MVT::i8: Opc = X86::SAR8ri; break; |
| 1692 | case MVT::i16: Opc = X86::SAR16ri; break; |
| 1693 | case MVT::i32: Opc = X86::SAR32ri; break; |
| 1694 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1695 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1696 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 1697 | return Result; |
| 1698 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1701 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1702 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1703 | } else { |
| 1704 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1705 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1706 | } |
| 1707 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1708 | switch (N.getValueType()) { |
| 1709 | default: assert(0 && "Cannot shift this type!"); |
| 1710 | case MVT::i8 : Opc = X86::SAR8rCL; break; |
| 1711 | case MVT::i16: Opc = X86::SAR16rCL; break; |
| 1712 | case MVT::i32: Opc = X86::SAR32rCL; break; |
| 1713 | } |
| 1714 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 1715 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1716 | return Result; |
| 1717 | |
| 1718 | case ISD::SETCC: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1719 | EmitCMP(N.getOperand(0), N.getOperand(1)); |
| 1720 | EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(), |
| 1721 | MVT::isFloatingPoint(N.getOperand(1).getValueType())); |
| 1722 | return Result; |
| 1723 | case ISD::LOAD: { |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1724 | // The chain for this load is now lowered. |
| 1725 | LoweredTokens.insert(SDOperand(Node, 1)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1726 | |
| 1727 | // Make sure we generate both values. |
| 1728 | if (Result != 1) |
| 1729 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 1730 | else |
| 1731 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1732 | |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1733 | switch (Node->getValueType(0)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1734 | default: assert(0 && "Cannot load this type!"); |
| 1735 | case MVT::i1: |
| 1736 | case MVT::i8: Opc = X86::MOV8rm; break; |
| 1737 | case MVT::i16: Opc = X86::MOV16rm; break; |
| 1738 | case MVT::i32: Opc = X86::MOV32rm; break; |
| 1739 | case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break; |
| 1740 | case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break; |
| 1741 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1742 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1743 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){ |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1744 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1745 | addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex()); |
| 1746 | } else { |
| 1747 | X86AddressMode AM; |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1748 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1749 | Select(N.getOperand(0)); |
| 1750 | SelectAddress(N.getOperand(1), AM); |
| 1751 | } else { |
| 1752 | SelectAddress(N.getOperand(1), AM); |
| 1753 | Select(N.getOperand(0)); |
| 1754 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1755 | addFullAddress(BuildMI(BB, Opc, 4, Result), AM); |
| 1756 | } |
| 1757 | return Result; |
| 1758 | } |
| 1759 | case ISD::DYNAMIC_STACKALLOC: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1760 | // Generate both result values. |
| 1761 | if (Result != 1) |
| 1762 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 1763 | else |
| 1764 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1765 | |
| 1766 | // FIXME: We are currently ignoring the requested alignment for handling |
| 1767 | // greater than the stack alignment. This will need to be revisited at some |
| 1768 | // point. Align = N.getOperand(2); |
| 1769 | |
| 1770 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 1771 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 1772 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 1773 | << " the stack alignment yet!"; |
| 1774 | abort(); |
| 1775 | } |
| 1776 | |
| 1777 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1778 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1779 | BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP) |
| 1780 | .addImm(CN->getValue()); |
| 1781 | } else { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1782 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1783 | Select(N.getOperand(0)); |
| 1784 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1785 | } else { |
| 1786 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1787 | Select(N.getOperand(0)); |
| 1788 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1789 | |
| 1790 | // Subtract size from stack pointer, thereby allocating some space. |
| 1791 | BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1); |
| 1792 | } |
| 1793 | |
| 1794 | // Put a pointer to the space into the result register, by copying the stack |
| 1795 | // pointer. |
| 1796 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP); |
| 1797 | return Result; |
| 1798 | |
| 1799 | case ISD::CALL: |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1800 | // The chain for this call is now lowered. |
| 1801 | LoweredTokens.insert(N.getValue(Node->getNumValues()-1)); |
| 1802 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1803 | if (GlobalAddressSDNode *GASD = |
| 1804 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1805 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1806 | BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true); |
| 1807 | } else if (ExternalSymbolSDNode *ESSDN = |
| 1808 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1809 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1810 | BuildMI(BB, X86::CALLpcrel32, |
| 1811 | 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 1812 | } else { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1813 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1814 | Select(N.getOperand(0)); |
| 1815 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1816 | } else { |
| 1817 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1818 | Select(N.getOperand(0)); |
| 1819 | } |
| 1820 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1821 | BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1); |
| 1822 | } |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1823 | switch (Node->getValueType(0)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1824 | default: assert(0 && "Unknown value type for call result!"); |
| 1825 | case MVT::Other: return 1; |
| 1826 | case MVT::i1: |
| 1827 | case MVT::i8: |
| 1828 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 1829 | break; |
| 1830 | case MVT::i16: |
| 1831 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 1832 | break; |
| 1833 | case MVT::i32: |
| 1834 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1835 | if (Node->getValueType(1) == MVT::i32) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1836 | BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX); |
| 1837 | break; |
| 1838 | case MVT::f32: |
| 1839 | case MVT::f64: // Floating-point return values live in %ST(0) |
| 1840 | ContainsFPCode = true; |
| 1841 | BuildMI(BB, X86::FpGETRESULT, 1, Result); |
| 1842 | break; |
| 1843 | } |
| 1844 | return Result+N.ResNo; |
| 1845 | } |
| 1846 | |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | void ISel::Select(SDOperand N) { |
| 1851 | unsigned Tmp1, Tmp2, Opc; |
| 1852 | |
| 1853 | // FIXME: Disable for our current expansion model! |
| 1854 | if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second) |
| 1855 | return; // Already selected. |
| 1856 | |
| 1857 | switch (N.getOpcode()) { |
| 1858 | default: |
| 1859 | N.Val->dump(); std::cerr << "\n"; |
| 1860 | assert(0 && "Node not handled yet!"); |
| 1861 | case ISD::EntryToken: return; // Noop |
| 1862 | case ISD::CopyToReg: |
| 1863 | Select(N.getOperand(0)); |
| 1864 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1865 | Tmp2 = cast<CopyRegSDNode>(N)->getReg(); |
| 1866 | |
| 1867 | if (Tmp1 != Tmp2) { |
| 1868 | switch (N.getOperand(1).getValueType()) { |
| 1869 | default: assert(0 && "Invalid type for operation!"); |
| 1870 | case MVT::i1: |
| 1871 | case MVT::i8: Opc = X86::MOV8rr; break; |
| 1872 | case MVT::i16: Opc = X86::MOV16rr; break; |
| 1873 | case MVT::i32: Opc = X86::MOV32rr; break; |
| 1874 | case MVT::f32: |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1875 | case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1876 | } |
| 1877 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 1878 | } |
| 1879 | return; |
| 1880 | case ISD::RET: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1881 | switch (N.getNumOperands()) { |
| 1882 | default: |
| 1883 | assert(0 && "Unknown return instruction!"); |
| 1884 | case 3: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1885 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1886 | N.getOperand(2).getValueType() == MVT::i32 && |
| 1887 | "Unknown two-register value!"); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1888 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 1889 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1890 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 1891 | } else { |
| 1892 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 1893 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1894 | } |
| 1895 | Select(N.getOperand(0)); |
| 1896 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1897 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 1898 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2); |
| 1899 | // Declare that EAX & EDX are live on exit. |
| 1900 | BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX) |
| 1901 | .addReg(X86::ESP); |
| 1902 | break; |
| 1903 | case 2: |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1904 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1905 | Select(N.getOperand(0)); |
| 1906 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1907 | } else { |
| 1908 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1909 | Select(N.getOperand(0)); |
| 1910 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1911 | switch (N.getOperand(1).getValueType()) { |
| 1912 | default: assert(0 && "All other types should have been promoted!!"); |
| 1913 | case MVT::f64: |
| 1914 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1); |
| 1915 | // Declare that top-of-stack is live on exit |
| 1916 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP); |
| 1917 | break; |
| 1918 | case MVT::i32: |
| 1919 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 1920 | BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP); |
| 1921 | break; |
| 1922 | } |
| 1923 | break; |
| 1924 | case 1: |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1925 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | } |
| 1928 | BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction |
| 1929 | return; |
| 1930 | case ISD::BR: { |
| 1931 | Select(N.getOperand(0)); |
| 1932 | MachineBasicBlock *Dest = |
| 1933 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
| 1934 | BuildMI(BB, X86::JMP, 1).addMBB(Dest); |
| 1935 | return; |
| 1936 | } |
| 1937 | |
| 1938 | case ISD::BRCOND: { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1939 | MachineBasicBlock *Dest = |
| 1940 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1942 | // Try to fold a setcc into the branch. If this fails, emit a test/jne |
| 1943 | // pair. |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1944 | if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) { |
| 1945 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 1946 | Select(N.getOperand(0)); |
| 1947 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1948 | } else { |
| 1949 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1950 | Select(N.getOperand(0)); |
| 1951 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1952 | BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1); |
| 1953 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 1954 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1956 | return; |
| 1957 | } |
| 1958 | case ISD::LOAD: |
| 1959 | case ISD::CALL: |
| 1960 | case ISD::DYNAMIC_STACKALLOC: |
| 1961 | SelectExpr(N); |
| 1962 | return; |
| 1963 | case ISD::STORE: { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1964 | // Select the address. |
| 1965 | X86AddressMode AM; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1968 | Opc = 0; |
| 1969 | switch (CN->getValueType(0)) { |
| 1970 | default: assert(0 && "Invalid type for operation!"); |
| 1971 | case MVT::i1: |
| 1972 | case MVT::i8: Opc = X86::MOV8mi; break; |
| 1973 | case MVT::i16: Opc = X86::MOV16mi; break; |
| 1974 | case MVT::i32: Opc = X86::MOV32mi; break; |
| 1975 | case MVT::f32: |
| 1976 | case MVT::f64: break; |
| 1977 | } |
| 1978 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1979 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 1980 | Select(N.getOperand(0)); |
| 1981 | SelectAddress(N.getOperand(2), AM); |
| 1982 | } else { |
| 1983 | SelectAddress(N.getOperand(2), AM); |
| 1984 | Select(N.getOperand(0)); |
| 1985 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1986 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue()); |
| 1987 | return; |
| 1988 | } |
| 1989 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1990 | switch (N.getOperand(1).getValueType()) { |
| 1991 | default: assert(0 && "Cannot store this type!"); |
| 1992 | case MVT::i1: |
| 1993 | case MVT::i8: Opc = X86::MOV8mr; break; |
| 1994 | case MVT::i16: Opc = X86::MOV16mr; break; |
| 1995 | case MVT::i32: Opc = X86::MOV32mr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1996 | case MVT::f32: Opc = X86::FST32m; break; |
| 1997 | case MVT::f64: Opc = X86::FST64m; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1998 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1999 | |
| 2000 | std::vector<std::pair<unsigned, unsigned> > RP; |
| 2001 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0)); |
| 2002 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1)); |
| 2003 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2)); |
| 2004 | std::sort(RP.begin(), RP.end()); |
| 2005 | |
| 2006 | for (unsigned i = 0; i != 3; ++i) |
| 2007 | switch (RP[2-i].second) { |
| 2008 | default: assert(0 && "Unknown operand number!"); |
| 2009 | case 0: Select(N.getOperand(0)); break; |
| 2010 | case 1: Tmp1 = SelectExpr(N.getOperand(1)); break; |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 2011 | case 2: SelectAddress(N.getOperand(2), AM); break; |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2014 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1); |
| 2015 | return; |
| 2016 | } |
| 2017 | case ISD::ADJCALLSTACKDOWN: |
| 2018 | case ISD::ADJCALLSTACKUP: |
| 2019 | Select(N.getOperand(0)); |
| 2020 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 2021 | |
| 2022 | Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN : |
| 2023 | X86::ADJCALLSTACKUP; |
| 2024 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 2025 | return; |
| 2026 | } |
| 2027 | assert(0 && "Should not be reached!"); |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | /// createX86PatternInstructionSelector - This pass converts an LLVM function |
| 2032 | /// into a machine code representation using pattern matching and a machine |
| 2033 | /// description file. |
| 2034 | /// |
| 2035 | FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) { |
| 2036 | return new ISel(TM); |
| 2037 | } |