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" |
| 35 | #include "llvm/Support/Compiler.h" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Streams.h" |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Statistic.h" |
| 41 | #include <queue> |
| 42 | #include <set> |
| 43 | using namespace llvm; |
| 44 | |
| 45 | STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added"); |
| 46 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 47 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Pattern Matcher Implementation |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | namespace { |
| 53 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 54 | /// SDValue's instead of register numbers for the leaves of the matched |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | /// tree. |
| 56 | struct X86ISelAddressMode { |
| 57 | enum { |
| 58 | RegBase, |
| 59 | FrameIndexBase |
| 60 | } BaseType; |
| 61 | |
| 62 | struct { // This is really a union, discriminated by BaseType! |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 63 | SDValue Reg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | int FrameIndex; |
| 65 | } Base; |
| 66 | |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 67 | bool isRIPRel; // RIP as base? |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | unsigned Scale; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 69 | SDValue IndexReg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | unsigned Disp; |
| 71 | GlobalValue *GV; |
| 72 | Constant *CP; |
| 73 | const char *ES; |
| 74 | int JT; |
| 75 | unsigned Align; // CP alignment. |
| 76 | |
| 77 | X86ISelAddressMode() |
| 78 | : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0), |
| 79 | GV(0), CP(0), ES(0), JT(-1), Align(0) { |
| 80 | } |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 81 | void dump() { |
| 82 | cerr << "X86ISelAddressMode " << this << "\n"; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 83 | cerr << "Base.Reg "; |
| 84 | if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump(); |
| 85 | else cerr << "nul"; |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 86 | cerr << " Base.FrameIndex " << Base.FrameIndex << "\n"; |
| 87 | cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n"; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 88 | cerr << "IndexReg "; |
| 89 | if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); |
| 90 | else cerr << "nul"; |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 91 | cerr << " Disp " << Disp << "\n"; |
| 92 | cerr << "GV "; if (GV) GV->dump(); |
| 93 | else cerr << "nul"; |
| 94 | cerr << " CP "; if (CP) CP->dump(); |
| 95 | else cerr << "nul"; |
| 96 | cerr << "\n"; |
| 97 | cerr << "ES "; if (ES) cerr << ES; else cerr << "nul"; |
| 98 | cerr << " JT" << JT << " Align" << Align << "\n"; |
| 99 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | |
| 103 | namespace { |
| 104 | //===--------------------------------------------------------------------===// |
| 105 | /// ISel - X86 specific code to select X86 machine instructions for |
| 106 | /// SelectionDAG operations. |
| 107 | /// |
| 108 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
| 109 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 110 | /// register should set this to true. |
| 111 | bool ContainsFPCode; |
| 112 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | /// TM - Keep a reference to X86TargetMachine. |
| 114 | /// |
| 115 | X86TargetMachine &TM; |
| 116 | |
| 117 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 118 | /// X86-specific SelectionDAG. |
| 119 | X86TargetLowering X86Lowering; |
| 120 | |
| 121 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 122 | /// make the right decision when generating code for different targets. |
| 123 | const X86Subtarget *Subtarget; |
| 124 | |
| 125 | /// GlobalBaseReg - keeps track of the virtual register mapped onto global |
| 126 | /// base register. |
| 127 | unsigned GlobalBaseReg; |
| 128 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 129 | /// CurBB - Current BB being isel'd. |
| 130 | /// |
| 131 | MachineBasicBlock *CurBB; |
| 132 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | public: |
| 134 | X86DAGToDAGISel(X86TargetMachine &tm, bool fast) |
Evan Cheng | 9b77cae | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 135 | : SelectionDAGISel(X86Lowering, fast), |
| 136 | ContainsFPCode(false), TM(tm), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | X86Lowering(*TM.getTargetLowering()), |
| 138 | Subtarget(&TM.getSubtarget<X86Subtarget>()) {} |
| 139 | |
| 140 | virtual bool runOnFunction(Function &Fn) { |
| 141 | // Make sure we re-emit a set of the global base reg if necessary |
| 142 | GlobalBaseReg = 0; |
| 143 | return SelectionDAGISel::runOnFunction(Fn); |
| 144 | } |
| 145 | |
| 146 | virtual const char *getPassName() const { |
| 147 | return "X86 DAG->DAG Instruction Selection"; |
| 148 | } |
| 149 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 150 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 152 | virtual void InstructionSelect(); |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 153 | |
| 154 | /// InstructionSelectPostProcessing - Post processing of selected and |
| 155 | /// scheduled basic blocks. |
Dan Gohman | b552df7 | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 156 | virtual void InstructionSelectPostProcessing(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 158 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 159 | |
Dan Gohman | d609827 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 160 | virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 161 | |
| 162 | // Include the pieces autogenerated from the target description. |
| 163 | #include "X86GenDAGISel.inc" |
| 164 | |
| 165 | private: |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 166 | SDNode *Select(SDValue N); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 168 | bool MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | bool isRoot = true, unsigned Depth = 0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 170 | bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 171 | bool isRoot, unsigned Depth); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 172 | bool SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
| 173 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
| 174 | bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base, |
| 175 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
| 176 | bool SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 177 | SDValue N, SDValue &Base, SDValue &Scale, |
| 178 | SDValue &Index, SDValue &Disp, |
| 179 | SDValue &InChain, SDValue &OutChain); |
| 180 | bool TryFoldLoad(SDValue P, SDValue N, |
| 181 | SDValue &Base, SDValue &Scale, |
| 182 | SDValue &Index, SDValue &Disp); |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 183 | void PreprocessForRMW(); |
| 184 | void PreprocessForFPConvert(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 185 | |
| 186 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 187 | /// inline asm expressions. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 188 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | char ConstraintCode, |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 190 | std::vector<SDValue> &OutOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 192 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 193 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 194 | inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, |
| 195 | SDValue &Scale, SDValue &Index, |
| 196 | SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 197 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 198 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : |
| 199 | AM.Base.Reg; |
| 200 | Scale = getI8Imm(AM.Scale); |
| 201 | Index = AM.IndexReg; |
| 202 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 203 | // is 32-bit. |
| 204 | if (AM.GV) |
| 205 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp); |
| 206 | else if (AM.CP) |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 207 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
| 208 | AM.Align, AM.Disp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 209 | else if (AM.ES) |
| 210 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32); |
| 211 | else if (AM.JT != -1) |
| 212 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32); |
| 213 | else |
| 214 | Disp = getI32Imm(AM.Disp); |
| 215 | } |
| 216 | |
| 217 | /// getI8Imm - Return a target constant with the specified value, of type |
| 218 | /// i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 219 | inline SDValue getI8Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 220 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 221 | } |
| 222 | |
| 223 | /// getI16Imm - Return a target constant with the specified value, of type |
| 224 | /// i16. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 225 | inline SDValue getI16Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 227 | } |
| 228 | |
| 229 | /// getI32Imm - Return a target constant with the specified value, of type |
| 230 | /// i32. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 231 | inline SDValue getI32Imm(unsigned Imm) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 233 | } |
| 234 | |
| 235 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 236 | /// base register. Return the virtual register that holds this value. |
| 237 | SDNode *getGlobalBaseReg(); |
| 238 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 239 | /// getTruncateTo8Bit - return an SDNode that implements a subreg based |
| 240 | /// truncate of the specified operand to i8. This can be done with tablegen, |
| 241 | /// except that this code uses MVT::Flag in a tricky way that happens to |
| 242 | /// improve scheduling in some cases. |
| 243 | SDNode *getTruncateTo8Bit(SDValue N0); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 244 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | #ifndef NDEBUG |
| 246 | unsigned Indent; |
| 247 | #endif |
| 248 | }; |
| 249 | } |
| 250 | |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 251 | /// findFlagUse - Return use of MVT::Flag value produced by the specified |
| 252 | /// SDNode. |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 253 | /// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 254 | static SDNode *findFlagUse(SDNode *N) { |
| 255 | unsigned FlagResNo = N->getNumValues()-1; |
| 256 | 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] | 257 | SDNode *User = *I; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 258 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 259 | SDValue Op = User->getOperand(i); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 260 | if (Op.getNode() == N && Op.getResNo() == FlagResNo) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 261 | return User; |
| 262 | } |
| 263 | } |
| 264 | return NULL; |
| 265 | } |
| 266 | |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 267 | /// findNonImmUse - Return true by reference in "found" if "Use" is an |
| 268 | /// non-immediate use of "Def". This function recursively traversing |
| 269 | /// up the operand chain ignoring certain nodes. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 270 | static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, |
| 271 | SDNode *Root, SDNode *Skip, bool &found, |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 272 | SmallPtrSet<SDNode*, 16> &Visited) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | if (found || |
| 274 | Use->getNodeId() > Def->getNodeId() || |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 275 | !Visited.insert(Use)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 276 | return; |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 277 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 279 | SDNode *N = Use->getOperand(i).getNode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 280 | if (N == Skip) |
| 281 | continue; |
| 282 | if (N == Def) { |
| 283 | if (Use == ImmedUse) |
Evan Cheng | 9ea310c | 2008-04-25 08:55:28 +0000 | [diff] [blame] | 284 | continue; // We are not looking for immediate use. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 285 | if (Use == Root) { |
Evan Cheng | 9ea310c | 2008-04-25 08:55:28 +0000 | [diff] [blame] | 286 | // Must be a chain reading node where it is possible to reach its own |
| 287 | // chain operand through a path started from another operand. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 288 | assert(Use->getOpcode() == ISD::STORE || |
Chris Lattner | cfbb272 | 2008-04-25 05:13:01 +0000 | [diff] [blame] | 289 | Use->getOpcode() == X86ISD::CMP || |
Chris Lattner | cfbb272 | 2008-04-25 05:13:01 +0000 | [diff] [blame] | 290 | Use->getOpcode() == ISD::INTRINSIC_W_CHAIN || |
| 291 | Use->getOpcode() == ISD::INTRINSIC_VOID); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 292 | continue; |
| 293 | } |
| 294 | found = true; |
| 295 | break; |
| 296 | } |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 297 | |
| 298 | // Traverse up the operand chain. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 299 | findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /// isNonImmUse - Start searching from Root up the DAG to check is Def can |
| 304 | /// be reached. Return true if that's the case. However, ignore direct uses |
| 305 | /// by ImmedUse (which would be U in the example illustrated in |
| 306 | /// CanBeFoldedBy) and by Root (which can happen in the store case). |
| 307 | /// FIXME: to be really generic, we should allow direct use by any node |
| 308 | /// that is being folded. But realisticly since we only fold loads which |
| 309 | /// have one non-chain use, we only need to watch out for load/op/store |
| 310 | /// and load/op/cmp case where the root (store / cmp) may reach the load via |
| 311 | /// its chain operand. |
| 312 | static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse, |
| 313 | SDNode *Skip = NULL) { |
Evan Cheng | 656269e | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 314 | SmallPtrSet<SDNode*, 16> Visited; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 315 | bool found = false; |
| 316 | findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited); |
| 317 | return found; |
| 318 | } |
| 319 | |
| 320 | |
Dan Gohman | d609827 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 321 | bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const { |
Dan Gohman | a29efcf | 2008-08-13 19:55:00 +0000 | [diff] [blame] | 322 | if (Fast) return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 323 | |
| 324 | // If U use can somehow reach N through another path then U can't fold N or |
| 325 | // it will create a cycle. e.g. In the following diagram, U can reach N |
| 326 | // through X. If N is folded into into U, then X is both a predecessor and |
| 327 | // a successor of U. |
| 328 | // |
| 329 | // [ N ] |
| 330 | // ^ ^ |
| 331 | // | | |
| 332 | // / \--- |
| 333 | // / [X] |
| 334 | // | ^ |
| 335 | // [U]--------| |
| 336 | |
| 337 | if (isNonImmUse(Root, N, U)) |
| 338 | return false; |
| 339 | |
| 340 | // If U produces a flag, then it gets (even more) interesting. Since it |
| 341 | // would have been "glued" together with its flag use, we need to check if |
| 342 | // it might reach N: |
| 343 | // |
| 344 | // [ N ] |
| 345 | // ^ ^ |
| 346 | // | | |
| 347 | // [U] \-- |
| 348 | // ^ [TF] |
| 349 | // | ^ |
| 350 | // | | |
| 351 | // \ / |
| 352 | // [FU] |
| 353 | // |
| 354 | // If FU (flag use) indirectly reach N (the load), and U fold N (call it |
| 355 | // NU), then TF is a predecessor of FU and a successor of NU. But since |
| 356 | // NU and FU are flagged together, this effectively creates a cycle. |
| 357 | bool HasFlagUse = false; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 358 | MVT VT = Root->getValueType(Root->getNumValues()-1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 359 | while ((VT == MVT::Flag && !Root->use_empty())) { |
| 360 | SDNode *FU = findFlagUse(Root); |
| 361 | if (FU == NULL) |
| 362 | break; |
| 363 | else { |
| 364 | Root = FU; |
| 365 | HasFlagUse = true; |
| 366 | } |
| 367 | VT = Root->getValueType(Root->getNumValues()-1); |
| 368 | } |
| 369 | |
| 370 | if (HasFlagUse) |
| 371 | return !isNonImmUse(Root, N, Root, U); |
| 372 | return true; |
| 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 659 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 660 | DEBUG(BB->dump()); |
Dan Gohman | a29efcf | 2008-08-13 19:55:00 +0000 | [diff] [blame] | 661 | if (!Fast) |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 662 | PreprocessForRMW(); |
Chris Lattner | dec9cb5 | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 663 | |
| 664 | // FIXME: This should only happen when not -fast. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 665 | PreprocessForFPConvert(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 666 | |
| 667 | // Codegen the basic block. |
| 668 | #ifndef NDEBUG |
| 669 | DOUT << "===== Instruction selection begins:\n"; |
| 670 | Indent = 0; |
| 671 | #endif |
Dan Gohman | bd3f882 | 2008-08-21 16:36:34 +0000 | [diff] [blame] | 672 | SelectRoot(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 673 | #ifndef NDEBUG |
| 674 | DOUT << "===== Instruction selection ends:\n"; |
| 675 | #endif |
| 676 | |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 677 | CurDAG->RemoveDeadNodes(); |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 678 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 679 | |
Dan Gohman | b552df7 | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 680 | void X86DAGToDAGISel::InstructionSelectPostProcessing() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 681 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 682 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 683 | // the terminator of the block. |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 684 | |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 685 | // Note that FP stack instructions are used in all modes for long double, |
| 686 | // so we always need to do this check. |
| 687 | // Also note that it's possible for an FP stack register to be live across |
| 688 | // an instruction that produces multiple basic blocks (SSE CMOV) so we |
| 689 | // must check all the generated basic blocks. |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 690 | |
| 691 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 692 | // 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] | 693 | MachineFunction::iterator MBBI = CurBB; |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 694 | MachineFunction::iterator EndMBB = BB; ++EndMBB; |
| 695 | for (; MBBI != EndMBB; ++MBBI) { |
| 696 | MachineBasicBlock *MBB = MBBI; |
| 697 | |
| 698 | // If this block returns, ignore it. We don't want to insert an FP_REG_KILL |
| 699 | // before the return. |
| 700 | if (!MBB->empty()) { |
| 701 | MachineBasicBlock::iterator EndI = MBB->end(); |
| 702 | --EndI; |
| 703 | if (EndI->getDesc().isReturn()) |
| 704 | continue; |
| 705 | } |
| 706 | |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 707 | bool ContainsFPCode = false; |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 708 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 709 | !ContainsFPCode && I != E; ++I) { |
| 710 | if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) { |
| 711 | const TargetRegisterClass *clas; |
| 712 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 713 | if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 714 | TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 715 | ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) == |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 716 | X86::RFP32RegisterClass || |
| 717 | clas == X86::RFP64RegisterClass || |
| 718 | clas == X86::RFP80RegisterClass)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 719 | ContainsFPCode = true; |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 725 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 726 | // a copy of the input value in this block. In SSE mode, we only care about |
| 727 | // 80-bit values. |
| 728 | if (!ContainsFPCode) { |
| 729 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 730 | // corresponding to BB for FP PHI nodes. |
| 731 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 732 | const PHINode *PN; |
| 733 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 734 | !ContainsFPCode && SI != E; ++SI) { |
| 735 | for (BasicBlock::const_iterator II = SI->begin(); |
| 736 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 737 | if (PN->getType()==Type::X86_FP80Ty || |
| 738 | (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) || |
| 739 | (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) { |
| 740 | ContainsFPCode = true; |
| 741 | break; |
| 742 | } |
Dale Johannesen | c428e0f | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 745 | } |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 746 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 747 | if (ContainsFPCode) { |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 748 | BuildMI(*MBB, MBBI->getFirstTerminator(), |
Dale Johannesen | 684887e | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 749 | TM.getInstrInfo()->get(X86::FP_REG_KILL)); |
| 750 | ++NumFPKill; |
| 751 | } |
Chris Lattner | 04d64b2 | 2008-03-10 23:34:12 +0000 | [diff] [blame] | 752 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Anton Korobeynikov | 34ef31e | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 755 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 756 | /// the main function. |
| 757 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 758 | MachineFrameInfo *MFI) { |
| 759 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 760 | if (Subtarget->isTargetCygMing()) |
| 761 | BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
| 762 | } |
| 763 | |
| 764 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 765 | // If this is main, emit special code for main. |
| 766 | MachineBasicBlock *BB = MF.begin(); |
| 767 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 768 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 769 | } |
| 770 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 771 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 772 | /// 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] | 773 | /// addressing mode. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 774 | bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 775 | bool isRoot, unsigned Depth) { |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 776 | DOUT << "MatchAddress: "; DEBUG(AM.dump()); |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 777 | // Limit recursion. |
| 778 | if (Depth > 5) |
| 779 | return MatchAddressBase(N, AM, isRoot, Depth); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 780 | |
| 781 | // RIP relative addressing: %rip + 32-bit displacement! |
| 782 | if (AM.isRIPRel) { |
| 783 | if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) { |
| 784 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
| 785 | if (isInt32(AM.Disp + Val)) { |
| 786 | AM.Disp += Val; |
| 787 | return false; |
| 788 | } |
| 789 | } |
| 790 | return true; |
| 791 | } |
| 792 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 793 | int id = N.getNode()->getNodeId(); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 794 | bool AlreadySelected = isSelected(id); // Already selected, not yet replaced. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 795 | |
| 796 | switch (N.getOpcode()) { |
| 797 | default: break; |
| 798 | case ISD::Constant: { |
| 799 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
| 800 | if (isInt32(AM.Disp + Val)) { |
| 801 | AM.Disp += Val; |
| 802 | return false; |
| 803 | } |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | case X86ISD::Wrapper: { |
Dale Johannesen | c501c08 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 808 | DOUT << "Wrapper: 64bit " << Subtarget->is64Bit(); |
| 809 | DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n"; |
| 810 | DOUT << "AlreadySelected " << AlreadySelected << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 811 | bool is64Bit = Subtarget->is64Bit(); |
| 812 | // 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] | 813 | // Also, base and index reg must be 0 in order to use rip as base. |
| 814 | if (is64Bit && (TM.getCodeModel() != CodeModel::Small || |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 815 | AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 816 | break; |
| 817 | if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1) |
| 818 | break; |
| 819 | // If value is available in a register both base and index components have |
| 820 | // been picked, we can't fit the result available in the register in the |
| 821 | // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 822 | if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 823 | SDValue N0 = N.getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 824 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 825 | GlobalValue *GV = G->getGlobal(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 826 | AM.GV = GV; |
| 827 | AM.Disp += G->getOffset(); |
Evan Cheng | a54e14f | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 828 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 829 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 830 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 831 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 832 | AM.CP = CP->getConstVal(); |
| 833 | AM.Align = CP->getAlignment(); |
| 834 | AM.Disp += CP->getOffset(); |
Evan Cheng | a54e14f | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 835 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 836 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 837 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 838 | } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 839 | AM.ES = S->getSymbol(); |
Evan Cheng | a54e14f | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 840 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 841 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 842 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 843 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 844 | AM.JT = J->getIndex(); |
Evan Cheng | a54e14f | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 845 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 846 | Subtarget->isPICStyleRIPRel(); |
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))) { |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 869 | unsigned Val = CN->getValue(); |
| 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)); |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 882 | uint64_t Disp = AM.Disp + (AddVal->getValue() << Val); |
| 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 | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 909 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 910 | AM.Scale = unsigned(CN->getValue())-1; |
| 911 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 912 | SDValue MulVal = N.getNode()->getOperand(0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 913 | SDValue Reg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 914 | |
| 915 | // Okay, we know that we have a scale by now. However, if the scaled |
| 916 | // value is an add of something and a constant, we can fold the |
| 917 | // constant into the disp field here. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 918 | if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 919 | isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { |
| 920 | Reg = MulVal.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 921 | ConstantSDNode *AddVal = |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 922 | cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 923 | uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue(); |
| 924 | if (isInt32(Disp)) |
| 925 | AM.Disp = Disp; |
| 926 | else |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 927 | Reg = N.getNode()->getOperand(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | } |
| 931 | |
| 932 | AM.IndexReg = AM.Base.Reg = Reg; |
| 933 | return false; |
| 934 | } |
| 935 | } |
| 936 | break; |
| 937 | |
| 938 | case ISD::ADD: |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 939 | if (!AlreadySelected) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 940 | X86ISelAddressMode Backup = AM; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 941 | if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) && |
| 942 | !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 943 | return false; |
| 944 | AM = Backup; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 945 | if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) && |
| 946 | !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 947 | return false; |
| 948 | AM = Backup; |
| 949 | } |
| 950 | break; |
| 951 | |
| 952 | case ISD::OR: |
| 953 | // 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] | 954 | if (AlreadySelected) break; |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 955 | |
| 956 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 957 | X86ISelAddressMode Backup = AM; |
| 958 | // Start with the LHS as an addr mode. |
| 959 | if (!MatchAddress(N.getOperand(0), AM, false) && |
| 960 | // Address could not have picked a GV address for the displacement. |
| 961 | AM.GV == NULL && |
| 962 | // On x86-64, the resultant disp must fit in 32-bits. |
| 963 | isInt32(AM.Disp + CN->getSignExtended()) && |
| 964 | // Check to see if the LHS & C is zero. |
Dan Gohman | 07961cd | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 965 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 966 | AM.Disp += CN->getValue(); |
| 967 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 7f06edd | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 969 | AM = Backup; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 970 | } |
| 971 | break; |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 972 | |
| 973 | case ISD::AND: { |
| 974 | // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this |
| 975 | // allows us to fold the shift into this addressing mode. |
| 976 | if (AlreadySelected) break; |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 977 | SDValue Shift = N.getOperand(0); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 978 | if (Shift.getOpcode() != ISD::SHL) break; |
| 979 | |
| 980 | // Scale must not be used already. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 981 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break; |
Evan Cheng | 3b5a127 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 982 | |
| 983 | // Not when RIP is used as the base. |
| 984 | if (AM.isRIPRel) break; |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 985 | |
| 986 | ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 987 | ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); |
| 988 | if (!C1 || !C2) break; |
| 989 | |
| 990 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 991 | // than one use (unless all uses are for address computation). Besides, |
| 992 | // isel mechanism requires their node ids to be reused. |
| 993 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 994 | break; |
| 995 | |
| 996 | // Verify that the shift amount is something we can fold. |
| 997 | unsigned ShiftCst = C1->getValue(); |
| 998 | if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) |
| 999 | break; |
| 1000 | |
| 1001 | // Get the new AND mask, this folds to a constant. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1002 | SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(), |
| 1003 | SDValue(C2, 0), SDValue(C1, 0)); |
| 1004 | SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1005 | Shift.getOperand(0), NewANDMask); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1006 | NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 1007 | NewAND.getNode()->setNodeId(N.getNode()->getNodeId()); |
Evan Cheng | f2abee7 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1008 | |
| 1009 | AM.Scale = 1 << ShiftCst; |
| 1010 | AM.IndexReg = NewAND; |
| 1011 | return false; |
| 1012 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1015 | return MatchAddressBase(N, AM, isRoot, Depth); |
| 1016 | } |
| 1017 | |
| 1018 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 1019 | /// specified addressing mode without any further recursion. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1020 | bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | a60c1b3 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1021 | bool isRoot, unsigned Depth) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1022 | // Is the base register already occupied? |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1023 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1024 | // If so, check to see if the scale index register is set. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1025 | if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1026 | AM.IndexReg = N; |
| 1027 | AM.Scale = 1; |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
| 1031 | // Otherwise, we cannot select it. |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | // Default, generate it as a register. |
| 1036 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 1037 | AM.Base.Reg = N; |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 1042 | /// It returns the operands which make up the maximal addressing mode it can |
| 1043 | /// match by reference. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1044 | bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
| 1045 | SDValue &Scale, SDValue &Index, |
| 1046 | SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1047 | X86ISelAddressMode AM; |
| 1048 | if (MatchAddress(N, AM)) |
| 1049 | return false; |
| 1050 | |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1051 | MVT VT = N.getValueType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1052 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1053 | if (!AM.Base.Reg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1054 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 1055 | } |
| 1056 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1057 | if (!AM.IndexReg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1058 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 1059 | |
| 1060 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 1061 | return true; |
| 1062 | } |
| 1063 | |
| 1064 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 1065 | /// constant +0.0. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1066 | static inline bool isZeroNode(SDValue Elt) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1067 | return ((isa<ConstantSDNode>(Elt) && |
| 1068 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 1069 | (isa<ConstantFPSDNode>(Elt) && |
Dale Johannesen | df8a831 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1070 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 1075 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 1076 | /// 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] | 1077 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 1078 | SDValue N, SDValue &Base, |
| 1079 | SDValue &Scale, SDValue &Index, |
| 1080 | SDValue &Disp, SDValue &InChain, |
| 1081 | SDValue &OutChain) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1082 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
| 1083 | InChain = N.getOperand(0).getValue(1); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1084 | if (ISD::isNON_EXTLoad(InChain.getNode()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1085 | InChain.getValue(0).hasOneUse() && |
| 1086 | N.hasOneUse() && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1087 | CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1088 | LoadSDNode *LD = cast<LoadSDNode>(InChain); |
| 1089 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
| 1090 | return false; |
| 1091 | OutChain = LD->getChain(); |
| 1092 | return true; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | // Also handle the case where we explicitly require zeros in the top |
| 1097 | // elements. This is a vector shuffle from the zero vector. |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1098 | if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && |
Chris Lattner | e6aa386 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 1099 | // 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] | 1100 | N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1101 | N.getOperand(0).getNode()->hasOneUse() && |
| 1102 | ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && |
Evan Cheng | 40ee6e5 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1103 | N.getOperand(0).getOperand(0).hasOneUse()) { |
| 1104 | // Okay, this is a zero extending load. Fold it. |
| 1105 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); |
| 1106 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
| 1107 | return false; |
| 1108 | OutChain = LD->getChain(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1109 | InChain = SDValue(LD, 1); |
Evan Cheng | 40ee6e5 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1110 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1111 | } |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 1117 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1118 | bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N, |
| 1119 | SDValue &Base, SDValue &Scale, |
| 1120 | SDValue &Index, SDValue &Disp) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1121 | X86ISelAddressMode AM; |
| 1122 | if (MatchAddress(N, AM)) |
| 1123 | return false; |
| 1124 | |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1125 | MVT VT = N.getValueType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1126 | unsigned Complexity = 0; |
| 1127 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1128 | if (AM.Base.Reg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1129 | Complexity = 1; |
| 1130 | else |
| 1131 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 1132 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1133 | Complexity = 4; |
| 1134 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1135 | if (AM.IndexReg.getNode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1136 | Complexity++; |
| 1137 | else |
| 1138 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 1139 | |
| 1140 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1141 | // a simple shift. |
| 1142 | if (AM.Scale > 1) |
| 1143 | Complexity++; |
| 1144 | |
| 1145 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 1146 | // to a LEA. This is determined with some expermentation but is by no means |
| 1147 | // optimal (especially for code size consideration). LEA is nice because of |
| 1148 | // its three-address nature. Tweak the cost function again when we can run |
| 1149 | // convertToThreeAddress() at register allocation time. |
| 1150 | if (AM.GV || AM.CP || AM.ES || AM.JT != -1) { |
| 1151 | // For X86-64, we should always use lea to materialize RIP relative |
| 1152 | // addresses. |
| 1153 | if (Subtarget->is64Bit()) |
| 1154 | Complexity = 4; |
| 1155 | else |
| 1156 | Complexity += 2; |
| 1157 | } |
| 1158 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1159 | if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1160 | Complexity++; |
| 1161 | |
| 1162 | if (Complexity > 2) { |
| 1163 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 1164 | return true; |
| 1165 | } |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1169 | bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N, |
| 1170 | SDValue &Base, SDValue &Scale, |
| 1171 | SDValue &Index, SDValue &Disp) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1172 | if (ISD::isNON_EXTLoad(N.getNode()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1173 | N.hasOneUse() && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1174 | CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1175 | return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp); |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | /// getGlobalBaseReg - Output the instructions required to put the |
| 1180 | /// base address to use for accessing globals into a register. |
| 1181 | /// |
| 1182 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
| 1183 | assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing"); |
| 1184 | if (!GlobalBaseReg) { |
| 1185 | // Insert the set of GlobalBaseReg into the first MBB of the function |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1186 | MachineFunction *MF = BB->getParent(); |
| 1187 | MachineBasicBlock &FirstMBB = MF->front(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1188 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1189 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1190 | unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1191 | |
| 1192 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
Evan Cheng | 34f9371 | 2007-12-22 02:26:46 +0000 | [diff] [blame] | 1193 | // Operand of MovePCtoStack is completely ignored by asm printer. It's |
| 1194 | // only used in JIT code emission as displacement to pc. |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1195 | BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1196 | |
| 1197 | // If we're using vanilla 'GOT' PIC style, we should use relative addressing |
| 1198 | // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external |
| 1199 | if (TM.getRelocationModel() == Reloc::PIC_ && |
| 1200 | Subtarget->isPICStyleGOT()) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1201 | GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass); |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1202 | BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg) |
| 1203 | .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1204 | } else { |
| 1205 | GlobalBaseReg = PC; |
| 1206 | } |
| 1207 | |
| 1208 | } |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1209 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 1213 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 1214 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 1215 | "Node doesn't have a token chain argument!"); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1216 | return FindCallStartFromCall(Node->getOperand(0).getNode()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1219 | /// getTruncateTo8Bit - return an SDNode that implements a subreg based |
| 1220 | /// truncate of the specified operand to i8. This can be done with tablegen, |
| 1221 | /// except that this code uses MVT::Flag in a tricky way that happens to |
| 1222 | /// improve scheduling in some cases. |
| 1223 | SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) { |
| 1224 | assert(!Subtarget->is64Bit() && |
| 1225 | "getTruncateTo8Bit is only needed on x86-32!"); |
| 1226 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1227 | |
| 1228 | // Ensure that the source register has an 8-bit subreg on 32-bit targets |
| 1229 | unsigned Opc; |
| 1230 | MVT N0VT = N0.getValueType(); |
| 1231 | switch (N0VT.getSimpleVT()) { |
| 1232 | default: assert(0 && "Unknown truncate!"); |
| 1233 | case MVT::i16: |
| 1234 | Opc = X86::MOV16to16_; |
| 1235 | break; |
| 1236 | case MVT::i32: |
| 1237 | Opc = X86::MOV32to32_; |
| 1238 | break; |
| 1239 | } |
| 1240 | |
| 1241 | // The use of MVT::Flag here is not strictly accurate, but it helps |
| 1242 | // scheduling in some cases. |
| 1243 | N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0); |
| 1244 | return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
| 1245 | MVT::i8, N0, SRIdx, N0.getValue(1)); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1249 | SDNode *X86DAGToDAGISel::Select(SDValue N) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1250 | SDNode *Node = N.getNode(); |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1251 | MVT NVT = Node->getValueType(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1252 | unsigned Opc, MOpc; |
| 1253 | unsigned Opcode = Node->getOpcode(); |
| 1254 | |
| 1255 | #ifndef NDEBUG |
| 1256 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
| 1257 | DEBUG(Node->dump(CurDAG)); |
| 1258 | DOUT << "\n"; |
| 1259 | Indent += 2; |
| 1260 | #endif |
| 1261 | |
Dan Gohman | bd68c79 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1262 | if (Node->isMachineOpcode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1263 | #ifndef NDEBUG |
| 1264 | DOUT << std::string(Indent-2, ' ') << "== "; |
| 1265 | DEBUG(Node->dump(CurDAG)); |
| 1266 | DOUT << "\n"; |
| 1267 | Indent -= 2; |
| 1268 | #endif |
| 1269 | return NULL; // Already selected. |
| 1270 | } |
| 1271 | |
| 1272 | switch (Opcode) { |
| 1273 | default: break; |
| 1274 | case X86ISD::GlobalBaseReg: |
| 1275 | return getGlobalBaseReg(); |
| 1276 | |
| 1277 | case ISD::ADD: { |
| 1278 | // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd |
| 1279 | // code and is matched first so to prevent it from being turned into |
| 1280 | // LEA32r X+c. |
Evan Cheng | 17e39d6 | 2008-01-08 02:06:11 +0000 | [diff] [blame] | 1281 | // In 64-bit small code size mode, use LEA to take advantage of |
| 1282 | // RIP-relative addressing. |
| 1283 | if (TM.getCodeModel() != CodeModel::Small) |
| 1284 | break; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1285 | MVT PtrVT = TLI.getPointerTy(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1286 | SDValue N0 = N.getOperand(0); |
| 1287 | SDValue N1 = N.getOperand(1); |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1288 | if (N.getNode()->getValueType(0) == PtrVT && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1289 | N0.getOpcode() == X86ISD::Wrapper && |
| 1290 | N1.getOpcode() == ISD::Constant) { |
| 1291 | unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1292 | SDValue C(0, 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1293 | // TODO: handle ExternalSymbolSDNode. |
| 1294 | if (GlobalAddressSDNode *G = |
| 1295 | dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { |
| 1296 | C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT, |
| 1297 | G->getOffset() + Offset); |
| 1298 | } else if (ConstantPoolSDNode *CP = |
| 1299 | dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { |
| 1300 | C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT, |
| 1301 | CP->getAlignment(), |
| 1302 | CP->getOffset()+Offset); |
| 1303 | } |
| 1304 | |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1305 | if (C.getNode()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1306 | if (Subtarget->is64Bit()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1307 | SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1308 | CurDAG->getRegister(0, PtrVT), C }; |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 1309 | return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r, |
| 1310 | MVT::i64, Ops, 4); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1311 | } else |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1312 | return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | // Other cases are handled by auto-generated code. |
| 1317 | break; |
| 1318 | } |
| 1319 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1320 | case ISD::SMUL_LOHI: |
| 1321 | case ISD::UMUL_LOHI: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1322 | SDValue N0 = Node->getOperand(0); |
| 1323 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1324 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1325 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
| 1326 | if (!isSigned) |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1327 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1328 | default: assert(0 && "Unsupported VT!"); |
| 1329 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1330 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1331 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
| 1332 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
| 1333 | } |
| 1334 | else |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1335 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1336 | default: assert(0 && "Unsupported VT!"); |
| 1337 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1338 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1339 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 1340 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
| 1341 | } |
| 1342 | |
| 1343 | unsigned LoReg, HiReg; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1344 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1345 | default: assert(0 && "Unsupported VT!"); |
| 1346 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1347 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1348 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
| 1349 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
| 1350 | } |
| 1351 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1352 | SDValue Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 508fe8b | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1353 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1354 | // multiplty is commmutative |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1355 | if (!foldedLoad) { |
| 1356 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 508fe8b | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1357 | if (foldedLoad) |
| 1358 | std::swap(N0, N1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1361 | AddToISelQueue(N0); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1362 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, |
| 1363 | N0, SDValue()).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (foldedLoad) { |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1366 | AddToISelQueue(N1.getOperand(0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1367 | AddToISelQueue(Tmp0); |
| 1368 | AddToISelQueue(Tmp1); |
| 1369 | AddToISelQueue(Tmp2); |
| 1370 | AddToISelQueue(Tmp3); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1371 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1372 | SDNode *CNode = |
| 1373 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1374 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1375 | // Update the chain. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1376 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1377 | } else { |
| 1378 | AddToISelQueue(N1); |
| 1379 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1380 | SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1383 | // Copy the low half of the result, if it is needed. |
| 1384 | if (!N.getValue(0).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1385 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1386 | LoReg, NVT, InFlag); |
| 1387 | InFlag = Result.getValue(2); |
| 1388 | ReplaceUses(N.getValue(0), Result); |
| 1389 | #ifndef NDEBUG |
| 1390 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1391 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1392 | DOUT << "\n"; |
| 1393 | #endif |
Evan Cheng | 6f0f0dd | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1394 | } |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1395 | // Copy the high half of the result, if it is needed. |
| 1396 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1397 | SDValue Result; |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1398 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1399 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1400 | // Shift it down 8 bits. |
| 1401 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1402 | X86::AX, MVT::i16, InFlag); |
| 1403 | InFlag = Result.getValue(2); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1404 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 1405 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1406 | // Then truncate it down to i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1407 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1408 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1409 | MVT::i8, Result, SRIdx), 0); |
| 1410 | } else { |
| 1411 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1412 | HiReg, NVT, InFlag); |
| 1413 | InFlag = Result.getValue(2); |
| 1414 | } |
| 1415 | ReplaceUses(N.getValue(1), Result); |
| 1416 | #ifndef NDEBUG |
| 1417 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1418 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1419 | DOUT << "\n"; |
| 1420 | #endif |
| 1421 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1422 | |
| 1423 | #ifndef NDEBUG |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1424 | Indent -= 2; |
| 1425 | #endif |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1426 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1430 | case ISD::SDIVREM: |
| 1431 | case ISD::UDIVREM: { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1432 | SDValue N0 = Node->getOperand(0); |
| 1433 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1434 | |
| 1435 | bool isSigned = Opcode == ISD::SDIVREM; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1436 | if (!isSigned) |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1437 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1438 | default: assert(0 && "Unsupported VT!"); |
| 1439 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1440 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1441 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 1442 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
| 1443 | } |
| 1444 | else |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1445 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1446 | default: assert(0 && "Unsupported VT!"); |
| 1447 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1448 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1449 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 1450 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
| 1451 | } |
| 1452 | |
| 1453 | unsigned LoReg, HiReg; |
| 1454 | unsigned ClrOpcode, SExtOpcode; |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1455 | switch (NVT.getSimpleVT()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1456 | default: assert(0 && "Unsupported VT!"); |
| 1457 | case MVT::i8: |
| 1458 | LoReg = X86::AL; HiReg = X86::AH; |
| 1459 | ClrOpcode = 0; |
| 1460 | SExtOpcode = X86::CBW; |
| 1461 | break; |
| 1462 | case MVT::i16: |
| 1463 | LoReg = X86::AX; HiReg = X86::DX; |
| 1464 | ClrOpcode = X86::MOV16r0; |
| 1465 | SExtOpcode = X86::CWD; |
| 1466 | break; |
| 1467 | case MVT::i32: |
| 1468 | LoReg = X86::EAX; HiReg = X86::EDX; |
| 1469 | ClrOpcode = X86::MOV32r0; |
| 1470 | SExtOpcode = X86::CDQ; |
| 1471 | break; |
| 1472 | case MVT::i64: |
| 1473 | LoReg = X86::RAX; HiReg = X86::RDX; |
| 1474 | ClrOpcode = X86::MOV64r0; |
| 1475 | SExtOpcode = X86::CQO; |
| 1476 | break; |
| 1477 | } |
| 1478 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1479 | SDValue Tmp0, Tmp1, Tmp2, Tmp3; |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1480 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
| 1481 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1482 | SDValue InFlag; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1483 | if (NVT == MVT::i8 && !isSigned) { |
| 1484 | // Special case for div8, just use a move with zero extension to AX to |
| 1485 | // clear the upper 8 bits (AH). |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1486 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1487 | if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1488 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1489 | AddToISelQueue(N0.getOperand(0)); |
| 1490 | AddToISelQueue(Tmp0); |
| 1491 | AddToISelQueue(Tmp1); |
| 1492 | AddToISelQueue(Tmp2); |
| 1493 | AddToISelQueue(Tmp3); |
| 1494 | Move = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1495 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1496 | Ops, 5), 0); |
| 1497 | Chain = Move.getValue(1); |
| 1498 | ReplaceUses(N0.getValue(1), Chain); |
| 1499 | } else { |
| 1500 | AddToISelQueue(N0); |
| 1501 | Move = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1502 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1503 | Chain = CurDAG->getEntryNode(); |
| 1504 | } |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1505 | Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1506 | InFlag = Chain.getValue(1); |
| 1507 | } else { |
| 1508 | AddToISelQueue(N0); |
| 1509 | InFlag = |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1510 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1511 | LoReg, N0, SDValue()).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1512 | if (isSigned) { |
| 1513 | // Sign extend the low part into the high part. |
| 1514 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1515 | SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1516 | } else { |
| 1517 | // Zero out the high part, effectively zero extending the input. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1518 | SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1519 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, |
| 1520 | ClrNode, InFlag).getValue(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1524 | if (foldedLoad) { |
| 1525 | AddToISelQueue(N1.getOperand(0)); |
| 1526 | AddToISelQueue(Tmp0); |
| 1527 | AddToISelQueue(Tmp1); |
| 1528 | AddToISelQueue(Tmp2); |
| 1529 | AddToISelQueue(Tmp3); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1530 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1531 | SDNode *CNode = |
| 1532 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1533 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1534 | // Update the chain. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1535 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1536 | } else { |
| 1537 | AddToISelQueue(N1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1538 | InFlag = |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1539 | SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1542 | // Copy the division (low) result, if it is needed. |
| 1543 | if (!N.getValue(0).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1544 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1545 | LoReg, NVT, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1546 | InFlag = Result.getValue(2); |
| 1547 | ReplaceUses(N.getValue(0), Result); |
| 1548 | #ifndef NDEBUG |
| 1549 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1550 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1551 | DOUT << "\n"; |
| 1552 | #endif |
Evan Cheng | 6f0f0dd | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1553 | } |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1554 | // Copy the remainder (high) result, if it is needed. |
| 1555 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1556 | SDValue Result; |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1557 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1558 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1559 | // Shift it down 8 bits. |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1560 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1561 | X86::AX, MVT::i16, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1562 | InFlag = Result.getValue(2); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1563 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
Gabor Greif | e9f7f58 | 2008-08-31 15:37:04 +0000 | [diff] [blame^] | 1564 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1565 | // Then truncate it down to i8. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1566 | SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1567 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1568 | MVT::i8, Result, SRIdx), 0); |
| 1569 | } else { |
Dan Gohman | 5a19955 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1570 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1571 | HiReg, NVT, InFlag); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1572 | InFlag = Result.getValue(2); |
| 1573 | } |
| 1574 | ReplaceUses(N.getValue(1), Result); |
| 1575 | #ifndef NDEBUG |
| 1576 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1577 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 242a5ba | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1578 | DOUT << "\n"; |
| 1579 | #endif |
| 1580 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1581 | |
| 1582 | #ifndef NDEBUG |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1583 | Indent -= 2; |
| 1584 | #endif |
| 1585 | |
| 1586 | return NULL; |
| 1587 | } |
Christopher Lamb | 422213d | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1588 | |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1589 | case ISD::SIGN_EXTEND_INREG: { |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1590 | MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1591 | if (SVT == MVT::i8 && !Subtarget->is64Bit()) { |
| 1592 | SDValue N0 = Node->getOperand(0); |
| 1593 | AddToISelQueue(N0); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1594 | |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1595 | SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0); |
| 1596 | unsigned Opc = 0; |
| 1597 | switch (NVT.getSimpleVT()) { |
| 1598 | default: assert(0 && "Unknown sign_extend_inreg!"); |
| 1599 | case MVT::i16: |
| 1600 | Opc = X86::MOVSX16rr8; |
| 1601 | break; |
| 1602 | case MVT::i32: |
| 1603 | Opc = X86::MOVSX32rr8; |
| 1604 | break; |
| 1605 | } |
| 1606 | |
| 1607 | SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1608 | |
| 1609 | #ifndef NDEBUG |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1610 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1611 | DEBUG(TruncOp.getNode()->dump(CurDAG)); |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1612 | DOUT << "\n"; |
| 1613 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1614 | DEBUG(ResNode->dump(CurDAG)); |
| 1615 | DOUT << "\n"; |
| 1616 | Indent -= 2; |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1617 | #endif |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1618 | return ResNode; |
| 1619 | } |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | } |
| 1622 | |
| 1623 | case ISD::TRUNCATE: { |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1624 | if (NVT == MVT::i8 && !Subtarget->is64Bit()) { |
| 1625 | SDValue Input = Node->getOperand(0); |
| 1626 | AddToISelQueue(Node->getOperand(0)); |
| 1627 | SDNode *ResNode = getTruncateTo8Bit(Input); |
Christopher Lamb | 0a7c866 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1628 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1629 | #ifndef NDEBUG |
| 1630 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1631 | DEBUG(ResNode->dump(CurDAG)); |
| 1632 | DOUT << "\n"; |
| 1633 | Indent -= 2; |
| 1634 | #endif |
Dan Gohman | dd612bb | 2008-08-20 21:27:32 +0000 | [diff] [blame] | 1635 | return ResNode; |
| 1636 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | } |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1639 | |
| 1640 | case ISD::DECLARE: { |
| 1641 | // Handle DECLARE nodes here because the second operand may have been |
| 1642 | // wrapped in X86ISD::Wrapper. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1643 | SDValue Chain = Node->getOperand(0); |
| 1644 | SDValue N1 = Node->getOperand(1); |
| 1645 | SDValue N2 = Node->getOperand(2); |
Evan Cheng | 651e144 | 2008-06-18 02:48:27 +0000 | [diff] [blame] | 1646 | if (!isa<FrameIndexSDNode>(N1)) |
| 1647 | break; |
| 1648 | int FI = cast<FrameIndexSDNode>(N1)->getIndex(); |
| 1649 | if (N2.getOpcode() == ISD::ADD && |
| 1650 | N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg) |
| 1651 | N2 = N2.getOperand(1); |
| 1652 | if (N2.getOpcode() == X86ISD::Wrapper && |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1653 | isa<GlobalAddressSDNode>(N2.getOperand(0))) { |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1654 | GlobalValue *GV = |
| 1655 | cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal(); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1656 | SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy()); |
| 1657 | SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy()); |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1658 | AddToISelQueue(Chain); |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1659 | SDValue Ops[] = { Tmp1, Tmp2, Chain }; |
Evan Cheng | d4cebcd | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1660 | return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, |
| 1661 | MVT::Other, Ops, 3); |
| 1662 | } |
| 1663 | break; |
| 1664 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | SDNode *ResNode = SelectCode(N); |
| 1668 | |
| 1669 | #ifndef NDEBUG |
| 1670 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1671 | if (ResNode == NULL || ResNode == N.getNode()) |
| 1672 | DEBUG(N.getNode()->dump(CurDAG)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1673 | else |
| 1674 | DEBUG(ResNode->dump(CurDAG)); |
| 1675 | DOUT << "\n"; |
| 1676 | Indent -= 2; |
| 1677 | #endif |
| 1678 | |
| 1679 | return ResNode; |
| 1680 | } |
| 1681 | |
| 1682 | bool X86DAGToDAGISel:: |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1683 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1684 | std::vector<SDValue> &OutOps) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1685 | SDValue Op0, Op1, Op2, Op3; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1686 | switch (ConstraintCode) { |
| 1687 | case 'o': // offsetable ?? |
| 1688 | case 'v': // not offsetable ?? |
| 1689 | default: return true; |
| 1690 | case 'm': // memory |
| 1691 | if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3)) |
| 1692 | return true; |
| 1693 | break; |
| 1694 | } |
| 1695 | |
| 1696 | OutOps.push_back(Op0); |
| 1697 | OutOps.push_back(Op1); |
| 1698 | OutOps.push_back(Op2); |
| 1699 | OutOps.push_back(Op3); |
| 1700 | AddToISelQueue(Op0); |
| 1701 | AddToISelQueue(Op1); |
| 1702 | AddToISelQueue(Op2); |
| 1703 | AddToISelQueue(Op3); |
| 1704 | return false; |
| 1705 | } |
| 1706 | |
| 1707 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1708 | /// X86-specific DAG, ready for instruction scheduling. |
| 1709 | /// |
| 1710 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) { |
| 1711 | return new X86DAGToDAGISel(TM, Fast); |
| 1712 | } |