Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a DAG pattern matching instruction selector for X86, |
| 11 | // converting from a legalized dag to a X86 dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "x86-isel" |
| 16 | #include "X86.h" |
| 17 | #include "X86InstrBuilder.h" |
| 18 | #include "X86ISelLowering.h" |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 19 | #include "X86MachineFunctionInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "X86RegisterInfo.h" |
| 21 | #include "X86Subtarget.h" |
| 22 | #include "X86TargetMachine.h" |
| 23 | #include "llvm/GlobalValue.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Intrinsics.h" |
| 26 | #include "llvm/Support/CFG.h" |
| 27 | #include "llvm/Type.h" |
| 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 13559d6 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
| 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/MathExtras.h" |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Streams.h" |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
| 42 | #include <queue> |
| 43 | #include <set> |
| 44 | using namespace llvm; |
| 45 | |
| 46 | STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added"); |
| 47 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 48 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Pattern Matcher Implementation |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | namespace { |
| 54 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 55 | /// SDValue's instead of register numbers for the leaves of the matched |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | /// tree. |
| 57 | struct X86ISelAddressMode { |
| 58 | enum { |
| 59 | RegBase, |
| 60 | FrameIndexBase |
| 61 | } BaseType; |
| 62 | |
| 63 | struct { // This is really a union, discriminated by BaseType! |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 64 | SDValue Reg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | int FrameIndex; |
| 66 | } Base; |
| 67 | |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 68 | bool isRIPRel; // RIP as base? |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | unsigned Scale; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 70 | SDValue IndexReg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | unsigned Disp; |
| 72 | GlobalValue *GV; |
| 73 | Constant *CP; |
| 74 | const char *ES; |
| 75 | int JT; |
| 76 | unsigned Align; // CP alignment. |
| 77 | |
| 78 | X86ISelAddressMode() |
| 79 | : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0), |
| 80 | GV(0), CP(0), ES(0), JT(-1), Align(0) { |
| 81 | } |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 82 | void dump() { |
| 83 | cerr << "X86ISelAddressMode " << this << "\n"; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 84 | cerr << "Base.Reg "; |
| 85 | if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump(); |
| 86 | else cerr << "nul"; |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 87 | cerr << " Base.FrameIndex " << Base.FrameIndex << "\n"; |
| 88 | cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n"; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 89 | cerr << "IndexReg "; |
| 90 | if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); |
| 91 | else cerr << "nul"; |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 92 | cerr << " Disp " << Disp << "\n"; |
| 93 | cerr << "GV "; if (GV) GV->dump(); |
| 94 | else cerr << "nul"; |
| 95 | cerr << " CP "; if (CP) CP->dump(); |
| 96 | else cerr << "nul"; |
| 97 | cerr << "\n"; |
| 98 | cerr << "ES "; if (ES) cerr << ES; else cerr << "nul"; |
| 99 | cerr << " JT" << JT << " Align" << Align << "\n"; |
| 100 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 101 | }; |
| 102 | } |
| 103 | |
| 104 | namespace { |
| 105 | //===--------------------------------------------------------------------===// |
| 106 | /// ISel - X86 specific code to select X86 machine instructions for |
| 107 | /// SelectionDAG operations. |
| 108 | /// |
| 109 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | /// TM - Keep a reference to X86TargetMachine. |
| 111 | /// |
| 112 | X86TargetMachine &TM; |
| 113 | |
| 114 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 115 | /// X86-specific SelectionDAG. |
Dan Gohman | f2b2957 | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 116 | X86TargetLowering &X86Lowering; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | |
| 118 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 119 | /// make the right decision when generating code for different targets. |
| 120 | const X86Subtarget *Subtarget; |
| 121 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 122 | /// CurBB - Current BB being isel'd. |
| 123 | /// |
| 124 | MachineBasicBlock *CurBB; |
| 125 | |
Evan Cheng | 13559d6 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 126 | /// OptForSize - If true, selector should try to optimize for code size |
| 127 | /// instead of performance. |
| 128 | bool OptForSize; |
| 129 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | public: |
| 131 | X86DAGToDAGISel(X86TargetMachine &tm, bool fast) |
Dan Gohman | f2b2957 | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 132 | : SelectionDAGISel(*tm.getTargetLowering(), fast), |
Dan Gohman | 61ad864 | 2008-10-03 16:17:33 +0000 | [diff] [blame] | 133 | TM(tm), X86Lowering(*TM.getTargetLowering()), |
Evan Cheng | 13559d6 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 134 | Subtarget(&TM.getSubtarget<X86Subtarget>()), |
Devang Patel | 93698d9 | 2008-10-01 23:18:38 +0000 | [diff] [blame] | 135 | OptForSize(false) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 136 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | virtual const char *getPassName() const { |
| 138 | return "X86 DAG->DAG Instruction Selection"; |
| 139 | } |
| 140 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 141 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 143 | virtual void InstructionSelect(); |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 144 | |
| 145 | /// InstructionSelectPostProcessing - Post processing of selected and |
| 146 | /// scheduled basic blocks. |
Dan Gohman | b552df7 | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 147 | virtual void InstructionSelectPostProcessing(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 149 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 150 | |
Dan Gohman | d609827 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 151 | virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | |
| 153 | // Include the pieces autogenerated from the target description. |
| 154 | #include "X86GenDAGISel.inc" |
| 155 | |
| 156 | private: |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 157 | SDNode *Select(SDValue N); |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 158 | SDNode *SelectAtomic64(SDNode *Node, unsigned Opc); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 159 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 160 | bool MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 161 | bool isRoot = true, unsigned Depth = 0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 162 | bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 163 | bool isRoot, unsigned Depth); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 164 | bool SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
| 165 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
| 166 | bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base, |
| 167 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
| 168 | bool SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 169 | SDValue N, SDValue &Base, SDValue &Scale, |
| 170 | SDValue &Index, SDValue &Disp, |
| 171 | SDValue &InChain, SDValue &OutChain); |
| 172 | bool TryFoldLoad(SDValue P, SDValue N, |
| 173 | SDValue &Base, SDValue &Scale, |
| 174 | SDValue &Index, SDValue &Disp); |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 175 | void PreprocessForRMW(); |
| 176 | void PreprocessForFPConvert(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 177 | |
| 178 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 179 | /// inline asm expressions. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 180 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | char ConstraintCode, |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 182 | std::vector<SDValue> &OutOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 184 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 185 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 186 | inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, |
| 187 | SDValue &Scale, SDValue &Index, |
| 188 | SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 190 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : |
| 191 | AM.Base.Reg; |
| 192 | Scale = getI8Imm(AM.Scale); |
| 193 | Index = AM.IndexReg; |
| 194 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 195 | // is 32-bit. |
| 196 | if (AM.GV) |
| 197 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp); |
| 198 | else if (AM.CP) |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 199 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
| 200 | AM.Align, AM.Disp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | else if (AM.ES) |
Bill Wendling | fef0605 | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 202 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 203 | else if (AM.JT != -1) |
| 204 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32); |
| 205 | else |
| 206 | Disp = getI32Imm(AM.Disp); |
| 207 | } |
| 208 | |
| 209 | /// getI8Imm - Return a target constant with the specified value, of type |
| 210 | /// i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | inline SDValue getI8Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 212 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 213 | } |
| 214 | |
| 215 | /// getI16Imm - Return a target constant with the specified value, of type |
| 216 | /// i16. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 217 | inline SDValue getI16Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 218 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 219 | } |
| 220 | |
| 221 | /// getI32Imm - Return a target constant with the specified value, of type |
| 222 | /// i32. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 223 | inline SDValue getI32Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 224 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 225 | } |
| 226 | |
Dan Gohman | b60482f | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 227 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 228 | /// the global base register. Output instructions required to |
| 229 | /// initialize the global base register, if necessary. |
| 230 | /// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | SDNode *getGlobalBaseReg(); |
| 232 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 233 | /// getTruncateTo8Bit - return an SDNode that implements a subreg based |
| 234 | /// truncate of the specified operand to i8. This can be done with tablegen, |
| 235 | /// except that this code uses MVT::Flag in a tricky way that happens to |
| 236 | /// improve scheduling in some cases. |
| 237 | SDNode *getTruncateTo8Bit(SDValue N0); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 238 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | #ifndef NDEBUG |
| 240 | unsigned Indent; |
| 241 | #endif |
| 242 | }; |
| 243 | } |
| 244 | |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 245 | /// findFlagUse - Return use of MVT::Flag value produced by the specified |
| 246 | /// SDNode. |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 247 | /// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 248 | static SDNode *findFlagUse(SDNode *N) { |
| 249 | unsigned FlagResNo = N->getNumValues()-1; |
| 250 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
Dan Gohman | 0c97f1d | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 251 | SDNode *User = *I; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 253 | SDValue Op = User->getOperand(i); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 254 | if (Op.getNode() == N && Op.getResNo() == FlagResNo) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 255 | return User; |
| 256 | } |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 261 | /// findNonImmUse - Return true by reference in "found" if "Use" is an |
| 262 | /// non-immediate use of "Def". This function recursively traversing |
| 263 | /// up the operand chain ignoring certain nodes. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 264 | static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 265 | SDNode *Root, bool &found, |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 266 | SmallPtrSet<SDNode*, 16> &Visited) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 267 | if (found || |
Dan Gohman | 2d2a7a3 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 268 | Use->getNodeId() < Def->getNodeId() || |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 269 | !Visited.insert(Use)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 270 | return; |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 271 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 273 | SDNode *N = Use->getOperand(i).getNode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 274 | if (N == Def) { |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 275 | if (Use == ImmedUse || Use == Root) |
Evan Cheng | 9ea310c | 2008-04-25 08:55:28 +0000 | [diff] [blame] | 276 | continue; // We are not looking for immediate use. |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 277 | assert(N != Root); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | found = true; |
| 279 | break; |
| 280 | } |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 281 | |
| 282 | // Traverse up the operand chain. |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 283 | findNonImmUse(N, Def, ImmedUse, Root, found, Visited); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | /// isNonImmUse - Start searching from Root up the DAG to check is Def can |
| 288 | /// be reached. Return true if that's the case. However, ignore direct uses |
| 289 | /// by ImmedUse (which would be U in the example illustrated in |
| 290 | /// CanBeFoldedBy) and by Root (which can happen in the store case). |
| 291 | /// FIXME: to be really generic, we should allow direct use by any node |
| 292 | /// that is being folded. But realisticly since we only fold loads which |
| 293 | /// have one non-chain use, we only need to watch out for load/op/store |
| 294 | /// and load/op/cmp case where the root (store / cmp) may reach the load via |
| 295 | /// its chain operand. |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 296 | static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) { |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 297 | SmallPtrSet<SDNode*, 16> Visited; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 298 | bool found = false; |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 299 | findNonImmUse(Root, Def, ImmedUse, Root, found, Visited); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 300 | return found; |
| 301 | } |
| 302 | |
| 303 | |
Dan Gohman | d609827 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 304 | bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const { |
Dan Gohman | a29efcf | 2008-08-13 19:55:00 +0000 | [diff] [blame] | 305 | if (Fast) return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 306 | |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 307 | // If Root use can somehow reach N through a path that that doesn't contain |
| 308 | // U then folding N would create a cycle. e.g. In the following |
| 309 | // diagram, Root can reach N through X. If N is folded into into Root, then |
| 310 | // X is both a predecessor and a successor of U. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 311 | // |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 312 | // [N*] // |
| 313 | // ^ ^ // |
| 314 | // / \ // |
| 315 | // [U*] [X]? // |
| 316 | // ^ ^ // |
| 317 | // \ / // |
| 318 | // \ / // |
| 319 | // [Root*] // |
| 320 | // |
| 321 | // * indicates nodes to be folded together. |
| 322 | // |
| 323 | // If Root produces a flag, then it gets (even more) interesting. Since it |
| 324 | // will be "glued" together with its flag use in the scheduler, we need to |
| 325 | // check if it might reach N. |
| 326 | // |
| 327 | // [N*] // |
| 328 | // ^ ^ // |
| 329 | // / \ // |
| 330 | // [U*] [X]? // |
| 331 | // ^ ^ // |
| 332 | // \ \ // |
| 333 | // \ | // |
| 334 | // [Root*] | // |
| 335 | // ^ | // |
| 336 | // f | // |
| 337 | // | / // |
| 338 | // [Y] / // |
| 339 | // ^ / // |
| 340 | // f / // |
| 341 | // | / // |
| 342 | // [FU] // |
| 343 | // |
| 344 | // If FU (flag use) indirectly reaches N (the load), and Root folds N |
| 345 | // (call it Fold), then X is a predecessor of FU and a successor of |
| 346 | // Fold. But since Fold and FU are flagged together, this will create |
| 347 | // a cycle in the scheduling graph. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 348 | |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 349 | MVT VT = Root->getValueType(Root->getNumValues()-1); |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 350 | while (VT == MVT::Flag) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 351 | SDNode *FU = findFlagUse(Root); |
| 352 | if (FU == NULL) |
| 353 | break; |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 354 | Root = FU; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 355 | VT = Root->getValueType(Root->getNumValues()-1); |
| 356 | } |
| 357 | |
Dan Gohman | 602d44a | 2008-09-17 01:39:10 +0000 | [diff] [blame] | 358 | return !isNonImmUse(Root, N, U); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand |
| 362 | /// and move load below the TokenFactor. Replace store's chain operand with |
| 363 | /// load's chain result. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 364 | static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load, |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 365 | SDValue Store, SDValue TF) { |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 366 | SmallVector<SDValue, 4> Ops; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 367 | for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i) |
| 368 | if (Load.getNode() == TF.getOperand(i).getNode()) |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 369 | Ops.push_back(Load.getOperand(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 370 | else |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 371 | Ops.push_back(TF.getOperand(i)); |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 372 | CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size()); |
| 373 | CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2)); |
| 374 | CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1), |
| 375 | Store.getOperand(2), Store.getOperand(3)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Evan Cheng | 2b2a701 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 378 | /// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. |
| 379 | /// |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 380 | static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address, |
| 381 | SDValue &Load) { |
Evan Cheng | 2b2a701 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 382 | if (N.getOpcode() == ISD::BIT_CONVERT) |
| 383 | N = N.getOperand(0); |
| 384 | |
| 385 | LoadSDNode *LD = dyn_cast<LoadSDNode>(N); |
| 386 | if (!LD || LD->isVolatile()) |
| 387 | return false; |
| 388 | if (LD->getAddressingMode() != ISD::UNINDEXED) |
| 389 | return false; |
| 390 | |
| 391 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 392 | if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD) |
| 393 | return false; |
| 394 | |
| 395 | if (N.hasOneUse() && |
| 396 | N.getOperand(1) == Address && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 397 | N.getNode()->isOperandOf(Chain.getNode())) { |
Evan Cheng | 2b2a701 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 398 | Load = N; |
| 399 | return true; |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 404 | /// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain |
| 405 | /// operand and move load below the call's chain operand. |
| 406 | static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load, |
| 407 | SDValue Call, SDValue Chain) { |
| 408 | SmallVector<SDValue, 8> Ops; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 409 | for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i) |
| 410 | if (Load.getNode() == Chain.getOperand(i).getNode()) |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 411 | Ops.push_back(Load.getOperand(0)); |
| 412 | else |
| 413 | Ops.push_back(Chain.getOperand(i)); |
| 414 | CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size()); |
| 415 | CurDAG->UpdateNodeOperands(Load, Call.getOperand(0), |
| 416 | Load.getOperand(1), Load.getOperand(2)); |
| 417 | Ops.clear(); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 418 | Ops.push_back(SDValue(Load.getNode(), 1)); |
| 419 | for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i) |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 420 | Ops.push_back(Call.getOperand(i)); |
| 421 | CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size()); |
| 422 | } |
| 423 | |
| 424 | /// isCalleeLoad - Return true if call address is a load and it can be |
| 425 | /// moved below CALLSEQ_START and the chains leading up to the call. |
| 426 | /// Return the CALLSEQ_START by reference as a second output. |
| 427 | static bool isCalleeLoad(SDValue Callee, SDValue &Chain) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 428 | if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse()) |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 429 | return false; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 430 | LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode()); |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 431 | if (!LD || |
| 432 | LD->isVolatile() || |
| 433 | LD->getAddressingMode() != ISD::UNINDEXED || |
| 434 | LD->getExtensionType() != ISD::NON_EXTLOAD) |
| 435 | return false; |
| 436 | |
| 437 | // Now let's find the callseq_start. |
| 438 | while (Chain.getOpcode() != ISD::CALLSEQ_START) { |
| 439 | if (!Chain.hasOneUse()) |
| 440 | return false; |
| 441 | Chain = Chain.getOperand(0); |
| 442 | } |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 443 | return Chain.getOperand(0).getNode() == Callee.getNode(); |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 447 | /// PreprocessForRMW - Preprocess the DAG to make instruction selection better. |
| 448 | /// This is only run if not in -fast mode (aka -O0). |
| 449 | /// This allows the instruction selector to pick more read-modify-write |
| 450 | /// instructions. This is a common case: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 451 | /// |
| 452 | /// [Load chain] |
| 453 | /// ^ |
| 454 | /// | |
| 455 | /// [Load] |
| 456 | /// ^ ^ |
| 457 | /// | | |
| 458 | /// / \- |
| 459 | /// / | |
| 460 | /// [TokenFactor] [Op] |
| 461 | /// ^ ^ |
| 462 | /// | | |
| 463 | /// \ / |
| 464 | /// \ / |
| 465 | /// [Store] |
| 466 | /// |
| 467 | /// The fact the store's chain operand != load's chain will prevent the |
| 468 | /// (store (op (load))) instruction from being selected. We can transform it to: |
| 469 | /// |
| 470 | /// [Load chain] |
| 471 | /// ^ |
| 472 | /// | |
| 473 | /// [TokenFactor] |
| 474 | /// ^ |
| 475 | /// | |
| 476 | /// [Load] |
| 477 | /// ^ ^ |
| 478 | /// | | |
| 479 | /// | \- |
| 480 | /// | | |
| 481 | /// | [Op] |
| 482 | /// | ^ |
| 483 | /// | | |
| 484 | /// \ / |
| 485 | /// \ / |
| 486 | /// [Store] |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 487 | void X86DAGToDAGISel::PreprocessForRMW() { |
| 488 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 489 | E = CurDAG->allnodes_end(); I != E; ++I) { |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 490 | if (I->getOpcode() == X86ISD::CALL) { |
| 491 | /// Also try moving call address load from outside callseq_start to just |
| 492 | /// before the call to allow it to be folded. |
| 493 | /// |
| 494 | /// [Load chain] |
| 495 | /// ^ |
| 496 | /// | |
| 497 | /// [Load] |
| 498 | /// ^ ^ |
| 499 | /// | | |
| 500 | /// / \-- |
| 501 | /// / | |
| 502 | ///[CALLSEQ_START] | |
| 503 | /// ^ | |
| 504 | /// | | |
| 505 | /// [LOAD/C2Reg] | |
| 506 | /// | | |
| 507 | /// \ / |
| 508 | /// \ / |
| 509 | /// [CALL] |
| 510 | SDValue Chain = I->getOperand(0); |
| 511 | SDValue Load = I->getOperand(1); |
| 512 | if (!isCalleeLoad(Load, Chain)) |
| 513 | continue; |
| 514 | MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain); |
| 515 | ++NumLoadMoved; |
| 516 | continue; |
| 517 | } |
| 518 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 519 | if (!ISD::isNON_TRUNCStore(I)) |
| 520 | continue; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 521 | SDValue Chain = I->getOperand(0); |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 522 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 523 | if (Chain.getNode()->getOpcode() != ISD::TokenFactor) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 524 | continue; |
| 525 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 526 | SDValue N1 = I->getOperand(1); |
| 527 | SDValue N2 = I->getOperand(2); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 528 | if ((N1.getValueType().isFloatingPoint() && |
| 529 | !N1.getValueType().isVector()) || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 530 | !N1.hasOneUse()) |
| 531 | continue; |
| 532 | |
| 533 | bool RModW = false; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 534 | SDValue Load; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 535 | unsigned Opcode = N1.getNode()->getOpcode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 536 | switch (Opcode) { |
Evan Cheng | 98cfaf8 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 537 | case ISD::ADD: |
| 538 | case ISD::MUL: |
| 539 | case ISD::AND: |
| 540 | case ISD::OR: |
| 541 | case ISD::XOR: |
| 542 | case ISD::ADDC: |
| 543 | case ISD::ADDE: |
| 544 | case ISD::VECTOR_SHUFFLE: { |
| 545 | SDValue N10 = N1.getOperand(0); |
| 546 | SDValue N11 = N1.getOperand(1); |
| 547 | RModW = isRMWLoad(N10, Chain, N2, Load); |
| 548 | if (!RModW) |
| 549 | RModW = isRMWLoad(N11, Chain, N2, Load); |
| 550 | break; |
| 551 | } |
| 552 | case ISD::SUB: |
| 553 | case ISD::SHL: |
| 554 | case ISD::SRA: |
| 555 | case ISD::SRL: |
| 556 | case ISD::ROTL: |
| 557 | case ISD::ROTR: |
| 558 | case ISD::SUBC: |
| 559 | case ISD::SUBE: |
| 560 | case X86ISD::SHLD: |
| 561 | case X86ISD::SHRD: { |
| 562 | SDValue N10 = N1.getOperand(0); |
| 563 | RModW = isRMWLoad(N10, Chain, N2, Load); |
| 564 | break; |
| 565 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | if (RModW) { |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 569 | MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 570 | ++NumLoadMoved; |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 575 | |
| 576 | /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend |
| 577 | /// nodes that target the FP stack to be store and load to the stack. This is a |
| 578 | /// gross hack. We would like to simply mark these as being illegal, but when |
| 579 | /// we do that, legalize produces these when it expands calls, then expands |
| 580 | /// these in the same legalize pass. We would like dag combine to be able to |
| 581 | /// hack on these between the call expansion and the node legalization. As such |
| 582 | /// this pass basically does "really late" legalization of these inline with the |
| 583 | /// X86 isel pass. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 584 | void X86DAGToDAGISel::PreprocessForFPConvert() { |
| 585 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 586 | E = CurDAG->allnodes_end(); I != E; ) { |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 587 | SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
| 588 | if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) |
| 589 | continue; |
| 590 | |
| 591 | // If the source and destination are SSE registers, then this is a legal |
| 592 | // conversion that should not be lowered. |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 593 | MVT SrcVT = N->getOperand(0).getValueType(); |
| 594 | MVT DstVT = N->getValueType(0); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 595 | bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT); |
| 596 | bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT); |
| 597 | if (SrcIsSSE && DstIsSSE) |
| 598 | continue; |
| 599 | |
Chris Lattner | 5d294e5 | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 600 | if (!SrcIsSSE && !DstIsSSE) { |
| 601 | // If this is an FPStack extension, it is a noop. |
| 602 | if (N->getOpcode() == ISD::FP_EXTEND) |
| 603 | continue; |
| 604 | // If this is a value-preserving FPStack truncation, it is a noop. |
| 605 | if (N->getConstantOperandVal(1)) |
| 606 | continue; |
| 607 | } |
| 608 | |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 609 | // Here we could have an FP stack truncation or an FPStack <-> SSE convert. |
| 610 | // FPStack has extload and truncstore. SSE can fold direct loads into other |
| 611 | // operations. Based on this, decide what we want to do. |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 612 | MVT MemVT; |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 613 | if (N->getOpcode() == ISD::FP_ROUND) |
| 614 | MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. |
| 615 | else |
| 616 | MemVT = SrcIsSSE ? SrcVT : DstVT; |
| 617 | |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 618 | SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 619 | |
| 620 | // FIXME: optimize the case where the src/dest is a load or store? |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 621 | SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), |
| 622 | N->getOperand(0), |
| 623 | MemTmp, NULL, 0, MemVT); |
| 624 | SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp, |
| 625 | NULL, 0, MemVT); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 626 | |
| 627 | // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the |
| 628 | // extload we created. This will cause general havok on the dag because |
| 629 | // anything below the conversion could be folded into other existing nodes. |
| 630 | // To avoid invalidating 'I', back it up to the convert node. |
| 631 | --I; |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 632 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 633 | |
| 634 | // Now that we did that, the node is dead. Increment the iterator to the |
| 635 | // next node to process, then delete N. |
| 636 | ++I; |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 637 | CurDAG->DeleteNode(N); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 641 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 642 | /// when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 643 | void X86DAGToDAGISel::InstructionSelect() { |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 644 | CurBB = BB; // BB can change as result of isel. |
Evan Cheng | 13559d6 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 645 | if (!OptForSize) { |
| 646 | const Function *F = CurDAG->getMachineFunction().getFunction(); |
Daniel Dunbar | 1b1e7ef | 2008-09-27 00:22:09 +0000 | [diff] [blame] | 647 | OptForSize = !F->isDeclaration() && |
| 648 | F->hasFnAttr(Attribute::OptimizeForSize); |
Evan Cheng | 13559d6 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 649 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 650 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 651 | DEBUG(BB->dump()); |
Dan Gohman | a29efcf | 2008-08-13 19:55:00 +0000 | [diff] [blame] | 652 | if (!Fast) |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 653 | PreprocessForRMW(); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 654 | |
| 655 | // FIXME: This should only happen when not -fast. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 656 | PreprocessForFPConvert(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 657 | |
| 658 | // Codegen the basic block. |
| 659 | #ifndef NDEBUG |
| 660 | DOUT << "===== Instruction selection begins:\n"; |
| 661 | Indent = 0; |
| 662 | #endif |
Dan Gohman | bd3f882 | 2008-08-21 16:36:34 +0000 | [diff] [blame] | 663 | SelectRoot(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 664 | #ifndef NDEBUG |
| 665 | DOUT << "===== Instruction selection ends:\n"; |
| 666 | #endif |
| 667 | |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 668 | CurDAG->RemoveDeadNodes(); |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 669 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 670 | |
Dan Gohman | b552df7 | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 671 | void X86DAGToDAGISel::InstructionSelectPostProcessing() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 672 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 673 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 674 | // the terminator of the block. |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 675 | |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 676 | // Note that FP stack instructions are used in all modes for long double, |
| 677 | // so we always need to do this check. |
| 678 | // Also note that it's possible for an FP stack register to be live across |
| 679 | // an instruction that produces multiple basic blocks (SSE CMOV) so we |
| 680 | // must check all the generated basic blocks. |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 681 | |
| 682 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 683 | // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.) |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 684 | MachineFunction::iterator MBBI = CurBB; |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 685 | MachineFunction::iterator EndMBB = BB; ++EndMBB; |
| 686 | for (; MBBI != EndMBB; ++MBBI) { |
| 687 | MachineBasicBlock *MBB = MBBI; |
| 688 | |
| 689 | // If this block returns, ignore it. We don't want to insert an FP_REG_KILL |
| 690 | // before the return. |
| 691 | if (!MBB->empty()) { |
| 692 | MachineBasicBlock::iterator EndI = MBB->end(); |
| 693 | --EndI; |
| 694 | if (EndI->getDesc().isReturn()) |
| 695 | continue; |
| 696 | } |
| 697 | |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 698 | bool ContainsFPCode = false; |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 699 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 700 | !ContainsFPCode && I != E; ++I) { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 701 | if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) { |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 702 | const TargetRegisterClass *clas; |
| 703 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 704 | if (I->getOperand(op).isReg() && I->getOperand(op).isDef() && |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 705 | TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 706 | ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) == |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 707 | X86::RFP32RegisterClass || |
| 708 | clas == X86::RFP64RegisterClass || |
| 709 | clas == X86::RFP80RegisterClass)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 710 | ContainsFPCode = true; |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 716 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 717 | // a copy of the input value in this block. In SSE mode, we only care about |
| 718 | // 80-bit values. |
| 719 | if (!ContainsFPCode) { |
| 720 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 721 | // corresponding to BB for FP PHI nodes. |
| 722 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 723 | const PHINode *PN; |
| 724 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 725 | !ContainsFPCode && SI != E; ++SI) { |
| 726 | for (BasicBlock::const_iterator II = SI->begin(); |
| 727 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 728 | if (PN->getType()==Type::X86_FP80Ty || |
| 729 | (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) || |
| 730 | (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) { |
| 731 | ContainsFPCode = true; |
| 732 | break; |
| 733 | } |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 736 | } |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 737 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 738 | if (ContainsFPCode) { |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 739 | BuildMI(*MBB, MBBI->getFirstTerminator(), |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 740 | TM.getInstrInfo()->get(X86::FP_REG_KILL)); |
| 741 | ++NumFPKill; |
| 742 | } |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 743 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 746 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 747 | /// the main function. |
| 748 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 749 | MachineFrameInfo *MFI) { |
| 750 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 751 | if (Subtarget->isTargetCygMing()) |
| 752 | BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
| 753 | } |
| 754 | |
| 755 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 756 | // If this is main, emit special code for main. |
| 757 | MachineBasicBlock *BB = MF.begin(); |
| 758 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 759 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 760 | } |
| 761 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 762 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 763 | /// returning true if it cannot be done. This just pattern matches for the |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 764 | /// addressing mode. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 765 | bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 766 | bool isRoot, unsigned Depth) { |
Evan Cheng | 7f250d6 | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 767 | DOUT << "MatchAddress: "; DEBUG(AM.dump()); |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 768 | // Limit recursion. |
| 769 | if (Depth > 5) |
| 770 | return MatchAddressBase(N, AM, isRoot, Depth); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 771 | |
| 772 | // RIP relative addressing: %rip + 32-bit displacement! |
| 773 | if (AM.isRIPRel) { |
| 774 | if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) { |
Dan Gohman | 4068673 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 775 | int64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 776 | if (isInt32(AM.Disp + Val)) { |
| 777 | AM.Disp += Val; |
| 778 | return false; |
| 779 | } |
| 780 | } |
| 781 | return true; |
| 782 | } |
| 783 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 784 | int id = N.getNode()->getNodeId(); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 785 | bool AlreadySelected = isSelected(id); // Already selected, not yet replaced. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 786 | |
| 787 | switch (N.getOpcode()) { |
| 788 | default: break; |
| 789 | case ISD::Constant: { |
Dan Gohman | 4068673 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 790 | int64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 791 | if (isInt32(AM.Disp + Val)) { |
| 792 | AM.Disp += Val; |
| 793 | return false; |
| 794 | } |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | case X86ISD::Wrapper: { |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 799 | DOUT << "Wrapper: 64bit " << Subtarget->is64Bit(); |
| 800 | DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n"; |
| 801 | DOUT << "AlreadySelected " << AlreadySelected << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 802 | bool is64Bit = Subtarget->is64Bit(); |
| 803 | // Under X86-64 non-small code model, GV (and friends) are 64-bits. |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 804 | // Also, base and index reg must be 0 in order to use rip as base. |
| 805 | if (is64Bit && (TM.getCodeModel() != CodeModel::Small || |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 806 | AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 807 | break; |
| 808 | if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1) |
| 809 | break; |
| 810 | // If value is available in a register both base and index components have |
| 811 | // been picked, we can't fit the result available in the register in the |
| 812 | // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 813 | if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 814 | SDValue N0 = N.getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 815 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 816 | GlobalValue *GV = G->getGlobal(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 817 | AM.GV = GV; |
| 818 | AM.Disp += G->getOffset(); |
Dan Gohman | c641336 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 819 | AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 820 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 821 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 822 | AM.CP = CP->getConstVal(); |
| 823 | AM.Align = CP->getAlignment(); |
| 824 | AM.Disp += CP->getOffset(); |
Dan Gohman | c641336 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 825 | AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 826 | return false; |
Bill Wendling | fef0605 | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 827 | } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 828 | AM.ES = S->getSymbol(); |
Dan Gohman | c641336 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 829 | AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 830 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 831 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 832 | AM.JT = J->getIndex(); |
Dan Gohman | c641336 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 833 | AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 834 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | case ISD::FrameIndex: |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 841 | if (AM.BaseType == X86ISelAddressMode::RegBase |
| 842 | && AM.Base.Reg.getNode() == 0) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 843 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 844 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 845 | return false; |
| 846 | } |
| 847 | break; |
| 848 | |
| 849 | case ISD::SHL: |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 850 | if (AlreadySelected || AM.IndexReg.getNode() != 0 |
| 851 | || AM.Scale != 1 || AM.isRIPRel) |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 852 | break; |
| 853 | |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 854 | if (ConstantSDNode |
| 855 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) { |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 856 | unsigned Val = CN->getZExtValue(); |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 857 | if (Val == 1 || Val == 2 || Val == 3) { |
| 858 | AM.Scale = 1 << Val; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 859 | SDValue ShVal = N.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 860 | |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 861 | // Okay, we know that we have a scale by now. However, if the scaled |
| 862 | // value is an add of something and a constant, we can fold the |
| 863 | // constant into the disp field here. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 864 | if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 865 | isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) { |
| 866 | AM.IndexReg = ShVal.getNode()->getOperand(0); |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 867 | ConstantSDNode *AddVal = |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 868 | cast<ConstantSDNode>(ShVal.getNode()->getOperand(1)); |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 869 | uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val); |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 870 | if (isInt32(Disp)) |
| 871 | AM.Disp = Disp; |
| 872 | else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 873 | AM.IndexReg = ShVal; |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 874 | } else { |
| 875 | AM.IndexReg = ShVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 876 | } |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 877 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 878 | } |
| 879 | break; |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 880 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 881 | |
Dan Gohman | 35b9922 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 882 | case ISD::SMUL_LOHI: |
| 883 | case ISD::UMUL_LOHI: |
| 884 | // A mul_lohi where we need the low part can be folded as a plain multiply. |
Gabor Greif | 46bf547 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 885 | if (N.getResNo() != 0) break; |
Dan Gohman | 35b9922 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 886 | // FALL THROUGH |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 887 | case ISD::MUL: |
| 888 | // X*[3,5,9] -> X+X*[2,4,8] |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 889 | if (!AlreadySelected && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 890 | AM.BaseType == X86ISelAddressMode::RegBase && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 891 | AM.Base.Reg.getNode() == 0 && |
| 892 | AM.IndexReg.getNode() == 0 && |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 893 | !AM.isRIPRel) { |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 894 | if (ConstantSDNode |
| 895 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 896 | if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || |
| 897 | CN->getZExtValue() == 9) { |
| 898 | AM.Scale = unsigned(CN->getZExtValue())-1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 899 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 900 | SDValue MulVal = N.getNode()->getOperand(0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 901 | SDValue Reg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 902 | |
| 903 | // Okay, we know that we have a scale by now. However, if the scaled |
| 904 | // value is an add of something and a constant, we can fold the |
| 905 | // constant into the disp field here. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 906 | if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 907 | isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { |
| 908 | Reg = MulVal.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 909 | ConstantSDNode *AddVal = |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 910 | cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 911 | uint64_t Disp = AM.Disp + AddVal->getZExtValue() * |
| 912 | CN->getZExtValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 913 | if (isInt32(Disp)) |
| 914 | AM.Disp = Disp; |
| 915 | else |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 916 | Reg = N.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 917 | } else { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 918 | Reg = N.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | AM.IndexReg = AM.Base.Reg = Reg; |
| 922 | return false; |
| 923 | } |
| 924 | } |
| 925 | break; |
| 926 | |
| 927 | case ISD::ADD: |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 928 | if (!AlreadySelected) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 929 | X86ISelAddressMode Backup = AM; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 930 | if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) && |
| 931 | !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 932 | return false; |
| 933 | AM = Backup; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 934 | if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) && |
| 935 | !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 936 | return false; |
| 937 | AM = Backup; |
| 938 | } |
| 939 | break; |
| 940 | |
| 941 | case ISD::OR: |
| 942 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 943 | if (AlreadySelected) break; |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 944 | |
| 945 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 946 | X86ISelAddressMode Backup = AM; |
| 947 | // Start with the LHS as an addr mode. |
| 948 | if (!MatchAddress(N.getOperand(0), AM, false) && |
| 949 | // Address could not have picked a GV address for the displacement. |
| 950 | AM.GV == NULL && |
| 951 | // On x86-64, the resultant disp must fit in 32-bits. |
Dan Gohman | 4068673 | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 952 | isInt32(AM.Disp + CN->getSExtValue()) && |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 953 | // Check to see if the LHS & C is zero. |
Dan Gohman | 07961cd | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 954 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 955 | AM.Disp += CN->getZExtValue(); |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 956 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 957 | } |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 958 | AM = Backup; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 959 | } |
| 960 | break; |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 961 | |
| 962 | case ISD::AND: { |
| 963 | // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this |
| 964 | // allows us to fold the shift into this addressing mode. |
| 965 | if (AlreadySelected) break; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 966 | SDValue Shift = N.getOperand(0); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 967 | if (Shift.getOpcode() != ISD::SHL) break; |
| 968 | |
| 969 | // Scale must not be used already. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 970 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break; |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 971 | |
| 972 | // Not when RIP is used as the base. |
| 973 | if (AM.isRIPRel) break; |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 974 | |
| 975 | ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 976 | ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); |
| 977 | if (!C1 || !C2) break; |
| 978 | |
| 979 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 980 | // than one use (unless all uses are for address computation). Besides, |
| 981 | // isel mechanism requires their node ids to be reused. |
| 982 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 983 | break; |
| 984 | |
| 985 | // Verify that the shift amount is something we can fold. |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 986 | unsigned ShiftCst = C1->getZExtValue(); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 987 | if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) |
| 988 | break; |
| 989 | |
| 990 | // Get the new AND mask, this folds to a constant. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 991 | SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(), |
| 992 | SDValue(C2, 0), SDValue(C1, 0)); |
| 993 | SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 994 | Shift.getOperand(0), NewANDMask); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 995 | NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 996 | NewAND.getNode()->setNodeId(N.getNode()->getNodeId()); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 997 | |
| 998 | AM.Scale = 1 << ShiftCst; |
| 999 | AM.IndexReg = NewAND; |
| 1000 | return false; |
| 1001 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1004 | return MatchAddressBase(N, AM, isRoot, Depth); |
| 1005 | } |
| 1006 | |
| 1007 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 1008 | /// specified addressing mode without any further recursion. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1009 | bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1010 | bool isRoot, unsigned Depth) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1011 | // Is the base register already occupied? |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1012 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1013 | // If so, check to see if the scale index register is set. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1014 | if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1015 | AM.IndexReg = N; |
| 1016 | AM.Scale = 1; |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | // Otherwise, we cannot select it. |
| 1021 | return true; |
| 1022 | } |
| 1023 | |
| 1024 | // Default, generate it as a register. |
| 1025 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 1026 | AM.Base.Reg = N; |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 1031 | /// It returns the operands which make up the maximal addressing mode it can |
| 1032 | /// match by reference. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1033 | bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
| 1034 | SDValue &Scale, SDValue &Index, |
| 1035 | SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1036 | X86ISelAddressMode AM; |
| 1037 | if (MatchAddress(N, AM)) |
| 1038 | return false; |
| 1039 | |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1040 | MVT VT = N.getValueType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1041 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1042 | if (!AM.Base.Reg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1043 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 1044 | } |
| 1045 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1046 | if (!AM.IndexReg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1047 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 1048 | |
| 1049 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1053 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 1054 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 1055 | /// is derived from the type of N, which is either v4f32 or v2f64. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1056 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 1057 | SDValue N, SDValue &Base, |
| 1058 | SDValue &Scale, SDValue &Index, |
| 1059 | SDValue &Disp, SDValue &InChain, |
| 1060 | SDValue &OutChain) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1061 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 1062 | InChain = N.getOperand(0).getValue(1); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1063 | if (ISD::isNON_EXTLoad(InChain.getNode()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1064 | InChain.getValue(0).hasOneUse() && |
| 1065 | N.hasOneUse() && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1066 | CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1067 | LoadSDNode *LD = cast<LoadSDNode>(InChain); |
| 1068 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
| 1069 | return false; |
| 1070 | OutChain = LD->getChain(); |
| 1071 | return true; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | // Also handle the case where we explicitly require zeros in the top |
| 1076 | // elements. This is a vector shuffle from the zero vector. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1077 | if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 1078 | // Check to see if the top elements are all zeros (or bitcast of zeros). |
Evan Cheng | 40ee6e5 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1079 | N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1080 | N.getOperand(0).getNode()->hasOneUse() && |
| 1081 | ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && |
Evan Cheng | 40ee6e5 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1082 | N.getOperand(0).getOperand(0).hasOneUse()) { |
| 1083 | // Okay, this is a zero extending load. Fold it. |
| 1084 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); |
| 1085 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
| 1086 | return false; |
| 1087 | OutChain = LD->getChain(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1088 | InChain = SDValue(LD, 1); |
Evan Cheng | 40ee6e5 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1089 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1090 | } |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 1096 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1097 | bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N, |
| 1098 | SDValue &Base, SDValue &Scale, |
| 1099 | SDValue &Index, SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1100 | X86ISelAddressMode AM; |
| 1101 | if (MatchAddress(N, AM)) |
| 1102 | return false; |
| 1103 | |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1104 | MVT VT = N.getValueType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1105 | unsigned Complexity = 0; |
| 1106 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1107 | if (AM.Base.Reg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1108 | Complexity = 1; |
| 1109 | else |
| 1110 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 1111 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1112 | Complexity = 4; |
| 1113 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1114 | if (AM.IndexReg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1115 | Complexity++; |
| 1116 | else |
| 1117 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 1118 | |
| 1119 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1120 | // a simple shift. |
| 1121 | if (AM.Scale > 1) |
| 1122 | Complexity++; |
| 1123 | |
| 1124 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 1125 | // to a LEA. This is determined with some expermentation but is by no means |
| 1126 | // optimal (especially for code size consideration). LEA is nice because of |
| 1127 | // its three-address nature. Tweak the cost function again when we can run |
| 1128 | // convertToThreeAddress() at register allocation time. |
| 1129 | if (AM.GV || AM.CP || AM.ES || AM.JT != -1) { |
| 1130 | // For X86-64, we should always use lea to materialize RIP relative |
| 1131 | // addresses. |
| 1132 | if (Subtarget->is64Bit()) |
| 1133 | Complexity = 4; |
| 1134 | else |
| 1135 | Complexity += 2; |
| 1136 | } |
| 1137 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1138 | if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1139 | Complexity++; |
| 1140 | |
| 1141 | if (Complexity > 2) { |
| 1142 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 1143 | return true; |
| 1144 | } |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1148 | bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N, |
| 1149 | SDValue &Base, SDValue &Scale, |
| 1150 | SDValue &Index, SDValue &Disp) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1151 | if (ISD::isNON_EXTLoad(N.getNode()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1152 | N.hasOneUse() && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1153 | CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1154 | return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp); |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
Dan Gohman | b60482f | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 1158 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 1159 | /// the global base register. Output instructions required to |
| 1160 | /// initialize the global base register, if necessary. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1161 | /// |
| 1162 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | 882ab73 | 2008-09-30 00:58:23 +0000 | [diff] [blame] | 1163 | MachineFunction *MF = CurBB->getParent(); |
| 1164 | unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1165 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 1169 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 1170 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 1171 | "Node doesn't have a token chain argument!"); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1172 | return FindCallStartFromCall(Node->getOperand(0).getNode()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1175 | /// getTruncateTo8Bit - return an SDNode that implements a subreg based |
| 1176 | /// truncate of the specified operand to i8. This can be done with tablegen, |
| 1177 | /// except that this code uses MVT::Flag in a tricky way that happens to |
| 1178 | /// improve scheduling in some cases. |
| 1179 | SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) { |
| 1180 | assert(!Subtarget->is64Bit() && |
| 1181 | "getTruncateTo8Bit is only needed on x86-32!"); |
| 1182 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1183 | |
| 1184 | // Ensure that the source register has an 8-bit subreg on 32-bit targets |
| 1185 | unsigned Opc; |
| 1186 | MVT N0VT = N0.getValueType(); |
| 1187 | switch (N0VT.getSimpleVT()) { |
| 1188 | default: assert(0 && "Unknown truncate!"); |
| 1189 | case MVT::i16: |
| 1190 | Opc = X86::MOV16to16_; |
| 1191 | break; |
| 1192 | case MVT::i32: |
| 1193 | Opc = X86::MOV32to32_; |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | // The use of MVT::Flag here is not strictly accurate, but it helps |
| 1198 | // scheduling in some cases. |
| 1199 | N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0); |
| 1200 | return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
| 1201 | MVT::i8, N0, SRIdx, N0.getValue(1)); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1204 | SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) { |
| 1205 | SDValue Chain = Node->getOperand(0); |
| 1206 | SDValue In1 = Node->getOperand(1); |
| 1207 | SDValue In2L = Node->getOperand(2); |
| 1208 | SDValue In2H = Node->getOperand(3); |
| 1209 | SDValue Tmp0, Tmp1, Tmp2, Tmp3; |
| 1210 | if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3)) |
| 1211 | return NULL; |
Dale Johannesen | 44eb537 | 2008-10-03 19:41:08 +0000 | [diff] [blame^] | 1212 | SDValue LSI = Node->getOperand(4); // MemOperand |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1213 | AddToISelQueue(Tmp0); |
| 1214 | AddToISelQueue(Tmp1); |
| 1215 | AddToISelQueue(Tmp2); |
| 1216 | AddToISelQueue(Tmp3); |
| 1217 | AddToISelQueue(In2L); |
| 1218 | AddToISelQueue(In2H); |
Dale Johannesen | 44eb537 | 2008-10-03 19:41:08 +0000 | [diff] [blame^] | 1219 | // For now, don't select the MemOperand object, we don't know how. |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1220 | AddToISelQueue(Chain); |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1221 | const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain }; |
| 1222 | return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8); |
| 1223 | } |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1224 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1225 | SDNode *X86DAGToDAGISel::Select(SDValue N) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1226 | SDNode *Node = N.getNode(); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1227 | MVT NVT = Node->getValueType(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1228 | unsigned Opc, MOpc; |
| 1229 | unsigned Opcode = Node->getOpcode(); |
| 1230 | |
| 1231 | #ifndef NDEBUG |
| 1232 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
| 1233 | DEBUG(Node->dump(CurDAG)); |
| 1234 | DOUT << "\n"; |
| 1235 | Indent += 2; |
| 1236 | #endif |
| 1237 | |
Dan Gohman | bd68c79 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1238 | if (Node->isMachineOpcode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1239 | #ifndef NDEBUG |
| 1240 | DOUT << std::string(Indent-2, ' ') << "== "; |
| 1241 | DEBUG(Node->dump(CurDAG)); |
| 1242 | DOUT << "\n"; |
| 1243 | Indent -= 2; |
| 1244 | #endif |
| 1245 | return NULL; // Already selected. |
| 1246 | } |
| 1247 | |
| 1248 | switch (Opcode) { |
| 1249 | default: break; |
| 1250 | case X86ISD::GlobalBaseReg: |
| 1251 | return getGlobalBaseReg(); |
| 1252 | |
| 1253 | case ISD::ADD: { |
| 1254 | // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd |
| 1255 | // code and is matched first so to prevent it from being turned into |
| 1256 | // LEA32r X+c. |
Evan Cheng | 17e39d6 | 2008-01-08 02:06:11 +0000 | [diff] [blame] | 1257 | // In 64-bit small code size mode, use LEA to take advantage of |
| 1258 | // RIP-relative addressing. |
| 1259 | if (TM.getCodeModel() != CodeModel::Small) |
| 1260 | break; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1261 | MVT PtrVT = TLI.getPointerTy(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1262 | SDValue N0 = N.getOperand(0); |
| 1263 | SDValue N1 = N.getOperand(1); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1264 | if (N.getNode()->getValueType(0) == PtrVT && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1265 | N0.getOpcode() == X86ISD::Wrapper && |
| 1266 | N1.getOpcode() == ISD::Constant) { |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1267 | unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1268 | SDValue C(0, 0); |
Bill Wendling | fef0605 | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1269 | // TODO: handle ExternalSymbolSDNode. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1270 | if (GlobalAddressSDNode *G = |
| 1271 | dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { |
| 1272 | C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT, |
| 1273 | G->getOffset() + Offset); |
| 1274 | } else if (ConstantPoolSDNode *CP = |
| 1275 | dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { |
| 1276 | C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT, |
| 1277 | CP->getAlignment(), |
| 1278 | CP->getOffset()+Offset); |
| 1279 | } |
| 1280 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1281 | if (C.getNode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1282 | if (Subtarget->is64Bit()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1283 | SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1284 | CurDAG->getRegister(0, PtrVT), C }; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1285 | return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r, |
| 1286 | MVT::i64, Ops, 4); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1287 | } else |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1288 | return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | // Other cases are handled by auto-generated code. |
| 1293 | break; |
| 1294 | } |
| 1295 | |
Dale Johannesen | f160d80 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1296 | case X86ISD::ATOMOR64_DAG: |
| 1297 | return SelectAtomic64(Node, X86::ATOMOR6432); |
| 1298 | case X86ISD::ATOMXOR64_DAG: |
| 1299 | return SelectAtomic64(Node, X86::ATOMXOR6432); |
| 1300 | case X86ISD::ATOMADD64_DAG: |
| 1301 | return SelectAtomic64(Node, X86::ATOMADD6432); |
| 1302 | case X86ISD::ATOMSUB64_DAG: |
| 1303 | return SelectAtomic64(Node, X86::ATOMSUB6432); |
| 1304 | case X86ISD::ATOMNAND64_DAG: |
| 1305 | return SelectAtomic64(Node, X86::ATOMNAND6432); |
| 1306 | case X86ISD::ATOMAND64_DAG: |
| 1307 | return SelectAtomic64(Node, X86::ATOMAND6432); |
| 1308 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1309 | case ISD::SMUL_LOHI: |
| 1310 | case ISD::UMUL_LOHI: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1311 | SDValue N0 = Node->getOperand(0); |
| 1312 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1313 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1314 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
| 1315 | if (!isSigned) |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1316 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1317 | default: assert(0 && "Unsupported VT!"); |
| 1318 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1319 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1320 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
| 1321 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
| 1322 | } |
| 1323 | else |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1324 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1325 | default: assert(0 && "Unsupported VT!"); |
| 1326 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1327 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1328 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 1329 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
| 1330 | } |
| 1331 | |
| 1332 | unsigned LoReg, HiReg; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1333 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1334 | default: assert(0 && "Unsupported VT!"); |
| 1335 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1336 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1337 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
| 1338 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
| 1339 | } |
| 1340 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1341 | SDValue Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 508fe8b | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1342 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1343 | // multiplty is commmutative |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1344 | if (!foldedLoad) { |
| 1345 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 508fe8b | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1346 | if (foldedLoad) |
| 1347 | std::swap(N0, N1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1350 | AddToISelQueue(N0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1351 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, |
| 1352 | N0, SDValue()).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (foldedLoad) { |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1355 | AddToISelQueue(N1.getOperand(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1356 | AddToISelQueue(Tmp0); |
| 1357 | AddToISelQueue(Tmp1); |
| 1358 | AddToISelQueue(Tmp2); |
| 1359 | AddToISelQueue(Tmp3); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1360 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1361 | SDNode *CNode = |
| 1362 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1363 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1364 | // Update the chain. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1365 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1366 | } else { |
| 1367 | AddToISelQueue(N1); |
| 1368 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1369 | SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1372 | // Copy the low half of the result, if it is needed. |
| 1373 | if (!N.getValue(0).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1374 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1375 | LoReg, NVT, InFlag); |
| 1376 | InFlag = Result.getValue(2); |
| 1377 | ReplaceUses(N.getValue(0), Result); |
| 1378 | #ifndef NDEBUG |
| 1379 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1380 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1381 | DOUT << "\n"; |
| 1382 | #endif |
Evan Cheng | 6f0f0dd | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1383 | } |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1384 | // Copy the high half of the result, if it is needed. |
| 1385 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1386 | SDValue Result; |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1387 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1388 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1389 | // Shift it down 8 bits. |
| 1390 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1391 | X86::AX, MVT::i16, InFlag); |
| 1392 | InFlag = Result.getValue(2); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1393 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1394 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1395 | // Then truncate it down to i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1396 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1397 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1398 | MVT::i8, Result, SRIdx), 0); |
| 1399 | } else { |
| 1400 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1401 | HiReg, NVT, InFlag); |
| 1402 | InFlag = Result.getValue(2); |
| 1403 | } |
| 1404 | ReplaceUses(N.getValue(1), Result); |
| 1405 | #ifndef NDEBUG |
| 1406 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1407 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1408 | DOUT << "\n"; |
| 1409 | #endif |
| 1410 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1411 | |
| 1412 | #ifndef NDEBUG |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1413 | Indent -= 2; |
| 1414 | #endif |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1415 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1416 | return NULL; |
| 1417 | } |
| 1418 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1419 | case ISD::SDIVREM: |
| 1420 | case ISD::UDIVREM: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1421 | SDValue N0 = Node->getOperand(0); |
| 1422 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1423 | |
| 1424 | bool isSigned = Opcode == ISD::SDIVREM; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1425 | if (!isSigned) |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1426 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1427 | default: assert(0 && "Unsupported VT!"); |
| 1428 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1429 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1430 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 1431 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
| 1432 | } |
| 1433 | else |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1434 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1435 | default: assert(0 && "Unsupported VT!"); |
| 1436 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1437 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1438 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 1439 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
| 1440 | } |
| 1441 | |
| 1442 | unsigned LoReg, HiReg; |
| 1443 | unsigned ClrOpcode, SExtOpcode; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1444 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1445 | default: assert(0 && "Unsupported VT!"); |
| 1446 | case MVT::i8: |
| 1447 | LoReg = X86::AL; HiReg = X86::AH; |
| 1448 | ClrOpcode = 0; |
| 1449 | SExtOpcode = X86::CBW; |
| 1450 | break; |
| 1451 | case MVT::i16: |
| 1452 | LoReg = X86::AX; HiReg = X86::DX; |
| 1453 | ClrOpcode = X86::MOV16r0; |
| 1454 | SExtOpcode = X86::CWD; |
| 1455 | break; |
| 1456 | case MVT::i32: |
| 1457 | LoReg = X86::EAX; HiReg = X86::EDX; |
| 1458 | ClrOpcode = X86::MOV32r0; |
| 1459 | SExtOpcode = X86::CDQ; |
| 1460 | break; |
| 1461 | case MVT::i64: |
| 1462 | LoReg = X86::RAX; HiReg = X86::RDX; |
| 1463 | ClrOpcode = X86::MOV64r0; |
| 1464 | SExtOpcode = X86::CQO; |
| 1465 | break; |
| 1466 | } |
| 1467 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1468 | SDValue Tmp0, Tmp1, Tmp2, Tmp3; |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1469 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
| 1470 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1471 | SDValue InFlag; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1472 | if (NVT == MVT::i8 && !isSigned) { |
| 1473 | // Special case for div8, just use a move with zero extension to AX to |
| 1474 | // clear the upper 8 bits (AH). |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1475 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1476 | if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1477 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1478 | AddToISelQueue(N0.getOperand(0)); |
| 1479 | AddToISelQueue(Tmp0); |
| 1480 | AddToISelQueue(Tmp1); |
| 1481 | AddToISelQueue(Tmp2); |
| 1482 | AddToISelQueue(Tmp3); |
| 1483 | Move = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1484 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1485 | Ops, 5), 0); |
| 1486 | Chain = Move.getValue(1); |
| 1487 | ReplaceUses(N0.getValue(1), Chain); |
| 1488 | } else { |
| 1489 | AddToISelQueue(N0); |
| 1490 | Move = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1491 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1492 | Chain = CurDAG->getEntryNode(); |
| 1493 | } |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1494 | Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1495 | InFlag = Chain.getValue(1); |
| 1496 | } else { |
| 1497 | AddToISelQueue(N0); |
| 1498 | InFlag = |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1499 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1500 | LoReg, N0, SDValue()).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1501 | if (isSigned) { |
| 1502 | // Sign extend the low part into the high part. |
| 1503 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1504 | SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1505 | } else { |
| 1506 | // Zero out the high part, effectively zero extending the input. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1507 | SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1508 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, |
| 1509 | ClrNode, InFlag).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1513 | if (foldedLoad) { |
| 1514 | AddToISelQueue(N1.getOperand(0)); |
| 1515 | AddToISelQueue(Tmp0); |
| 1516 | AddToISelQueue(Tmp1); |
| 1517 | AddToISelQueue(Tmp2); |
| 1518 | AddToISelQueue(Tmp3); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1519 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1520 | SDNode *CNode = |
| 1521 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1522 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1523 | // Update the chain. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1524 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1525 | } else { |
| 1526 | AddToISelQueue(N1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1527 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1528 | SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1531 | // Copy the division (low) result, if it is needed. |
| 1532 | if (!N.getValue(0).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1533 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1534 | LoReg, NVT, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1535 | InFlag = Result.getValue(2); |
| 1536 | ReplaceUses(N.getValue(0), Result); |
| 1537 | #ifndef NDEBUG |
| 1538 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1539 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1540 | DOUT << "\n"; |
| 1541 | #endif |
Evan Cheng | 6f0f0dd | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1542 | } |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1543 | // Copy the remainder (high) result, if it is needed. |
| 1544 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1545 | SDValue Result; |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1546 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1547 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1548 | // Shift it down 8 bits. |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1549 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1550 | X86::AX, MVT::i16, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1551 | InFlag = Result.getValue(2); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1552 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1553 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1554 | // Then truncate it down to i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1555 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1556 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1557 | MVT::i8, Result, SRIdx), 0); |
| 1558 | } else { |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1559 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1560 | HiReg, NVT, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1561 | InFlag = Result.getValue(2); |
| 1562 | } |
| 1563 | ReplaceUses(N.getValue(1), Result); |
| 1564 | #ifndef NDEBUG |
| 1565 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1566 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1567 | DOUT << "\n"; |
| 1568 | #endif |
| 1569 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1570 | |
| 1571 | #ifndef NDEBUG |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1572 | Indent -= 2; |
| 1573 | #endif |
| 1574 | |
| 1575 | return NULL; |
| 1576 | } |
Christopher Lamb | 422213d | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1577 | |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1578 | case ISD::SIGN_EXTEND_INREG: { |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1579 | MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1580 | if (SVT == MVT::i8 && !Subtarget->is64Bit()) { |
| 1581 | SDValue N0 = Node->getOperand(0); |
| 1582 | AddToISelQueue(N0); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1583 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1584 | SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0); |
| 1585 | unsigned Opc = 0; |
| 1586 | switch (NVT.getSimpleVT()) { |
| 1587 | default: assert(0 && "Unknown sign_extend_inreg!"); |
| 1588 | case MVT::i16: |
| 1589 | Opc = X86::MOVSX16rr8; |
| 1590 | break; |
| 1591 | case MVT::i32: |
| 1592 | Opc = X86::MOVSX32rr8; |
| 1593 | break; |
| 1594 | } |
| 1595 | |
| 1596 | SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1597 | |
| 1598 | #ifndef NDEBUG |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1599 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1600 | DEBUG(TruncOp.getNode()->dump(CurDAG)); |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1601 | DOUT << "\n"; |
| 1602 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1603 | DEBUG(ResNode->dump(CurDAG)); |
| 1604 | DOUT << "\n"; |
| 1605 | Indent -= 2; |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1606 | #endif |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1607 | return ResNode; |
| 1608 | } |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1609 | break; |
| 1610 | } |
| 1611 | |
| 1612 | case ISD::TRUNCATE: { |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1613 | if (NVT == MVT::i8 && !Subtarget->is64Bit()) { |
| 1614 | SDValue Input = Node->getOperand(0); |
| 1615 | AddToISelQueue(Node->getOperand(0)); |
| 1616 | SDNode *ResNode = getTruncateTo8Bit(Input); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1617 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1618 | #ifndef NDEBUG |
| 1619 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1620 | DEBUG(ResNode->dump(CurDAG)); |
| 1621 | DOUT << "\n"; |
| 1622 | Indent -= 2; |
| 1623 | #endif |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1624 | return ResNode; |
| 1625 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | } |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1628 | |
| 1629 | case ISD::DECLARE: { |
| 1630 | // Handle DECLARE nodes here because the second operand may have been |
| 1631 | // wrapped in X86ISD::Wrapper. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1632 | SDValue Chain = Node->getOperand(0); |
| 1633 | SDValue N1 = Node->getOperand(1); |
| 1634 | SDValue N2 = Node->getOperand(2); |
Evan Cheng | 651e144 | 2008-06-18 02:48:27 +0000 | [diff] [blame] | 1635 | if (!isa<FrameIndexSDNode>(N1)) |
| 1636 | break; |
| 1637 | int FI = cast<FrameIndexSDNode>(N1)->getIndex(); |
| 1638 | if (N2.getOpcode() == ISD::ADD && |
| 1639 | N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg) |
| 1640 | N2 = N2.getOperand(1); |
| 1641 | if (N2.getOpcode() == X86ISD::Wrapper && |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1642 | isa<GlobalAddressSDNode>(N2.getOperand(0))) { |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1643 | GlobalValue *GV = |
| 1644 | cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1645 | SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy()); |
| 1646 | SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy()); |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1647 | AddToISelQueue(Chain); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1648 | SDValue Ops[] = { Tmp1, Tmp2, Chain }; |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1649 | return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, |
| 1650 | MVT::Other, Ops, 3); |
| 1651 | } |
| 1652 | break; |
| 1653 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | SDNode *ResNode = SelectCode(N); |
| 1657 | |
| 1658 | #ifndef NDEBUG |
| 1659 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1660 | if (ResNode == NULL || ResNode == N.getNode()) |
| 1661 | DEBUG(N.getNode()->dump(CurDAG)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1662 | else |
| 1663 | DEBUG(ResNode->dump(CurDAG)); |
| 1664 | DOUT << "\n"; |
| 1665 | Indent -= 2; |
| 1666 | #endif |
| 1667 | |
| 1668 | return ResNode; |
| 1669 | } |
| 1670 | |
| 1671 | bool X86DAGToDAGISel:: |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1672 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1673 | std::vector<SDValue> &OutOps) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1674 | SDValue Op0, Op1, Op2, Op3; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1675 | switch (ConstraintCode) { |
| 1676 | case 'o': // offsetable ?? |
| 1677 | case 'v': // not offsetable ?? |
| 1678 | default: return true; |
| 1679 | case 'm': // memory |
| 1680 | if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3)) |
| 1681 | return true; |
| 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | OutOps.push_back(Op0); |
| 1686 | OutOps.push_back(Op1); |
| 1687 | OutOps.push_back(Op2); |
| 1688 | OutOps.push_back(Op3); |
| 1689 | AddToISelQueue(Op0); |
| 1690 | AddToISelQueue(Op1); |
| 1691 | AddToISelQueue(Op2); |
| 1692 | AddToISelQueue(Op3); |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1697 | /// X86-specific DAG, ready for instruction scheduling. |
| 1698 | /// |
| 1699 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) { |
| 1700 | return new X86DAGToDAGISel(TM, Fast); |
| 1701 | } |