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