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