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