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