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