Chris Lattner | 7a12537 | 2005-11-16 22:59:19 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +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 | |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "x86-isel" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 16 | #include "X86.h" |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 18 | #include "X86ISelLowering.h" |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 19 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 20 | #include "X86RegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 21 | #include "X86Subtarget.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 22 | #include "X86TargetMachine.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalValue.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Chris Lattner | 420736d | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7aa8a45 | 2007-01-12 23:22:14 +0000 | [diff] [blame] | 27 | #include "llvm/Type.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | b7a75a5 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Streams.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | cdda25d | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Statistic.h" |
| 44 | using namespace llvm; |
| 45 | |
Evan Cheng | 4d95232 | 2009-03-31 01:13:53 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CommandLine.h" |
| 47 | static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden); |
| 48 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 49 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 50 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Pattern Matcher Implementation |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 56 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 57 | /// SDValue's instead of register numbers for the leaves of the matched |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 58 | /// tree. |
| 59 | struct X86ISelAddressMode { |
| 60 | enum { |
| 61 | RegBase, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 62 | FrameIndexBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 63 | } BaseType; |
| 64 | |
| 65 | struct { // This is really a union, discriminated by BaseType! |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 66 | SDValue Reg; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 67 | int FrameIndex; |
| 68 | } Base; |
| 69 | |
| 70 | unsigned Scale; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 71 | SDValue IndexReg; |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 72 | int32_t Disp; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 73 | SDValue Segment; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 74 | GlobalValue *GV; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 75 | Constant *CP; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 76 | const char *ES; |
| 77 | int JT; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 78 | unsigned Align; // CP alignment. |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 79 | unsigned char SymbolFlags; // X86II::MO_* |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 80 | |
| 81 | X86ISelAddressMode() |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 82 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 83 | Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 84 | } |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 85 | |
| 86 | bool hasSymbolicDisplacement() const { |
| 87 | return GV != 0 || CP != 0 || ES != 0 || JT != -1; |
| 88 | } |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 89 | |
| 90 | bool hasBaseOrIndexReg() const { |
| 91 | return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0; |
| 92 | } |
| 93 | |
| 94 | /// isRIPRelative - Return true if this addressing mode is already RIP |
| 95 | /// relative. |
| 96 | bool isRIPRelative() const { |
| 97 | if (BaseType != RegBase) return false; |
| 98 | if (RegisterSDNode *RegNode = |
| 99 | dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode())) |
| 100 | return RegNode->getReg() == X86::RIP; |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | void setBaseReg(SDValue Reg) { |
| 105 | BaseType = RegBase; |
| 106 | Base.Reg = Reg; |
| 107 | } |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 108 | |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 109 | void dump() { |
| 110 | cerr << "X86ISelAddressMode " << this << "\n"; |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 111 | cerr << "Base.Reg "; |
| 112 | if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump(); |
| 113 | else cerr << "nul"; |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 114 | cerr << " Base.FrameIndex " << Base.FrameIndex << "\n"; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 115 | cerr << " Scale" << Scale << "\n"; |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 116 | cerr << "IndexReg "; |
| 117 | if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); |
| 118 | else cerr << "nul"; |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 119 | cerr << " Disp " << Disp << "\n"; |
| 120 | cerr << "GV "; if (GV) GV->dump(); |
| 121 | else cerr << "nul"; |
| 122 | cerr << " CP "; if (CP) CP->dump(); |
| 123 | else cerr << "nul"; |
| 124 | cerr << "\n"; |
| 125 | cerr << "ES "; if (ES) cerr << ES; else cerr << "nul"; |
| 126 | cerr << " JT" << JT << " Align" << Align << "\n"; |
| 127 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 128 | }; |
| 129 | } |
| 130 | |
| 131 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 132 | //===--------------------------------------------------------------------===// |
| 133 | /// ISel - X86 specific code to select X86 machine instructions for |
| 134 | /// SelectionDAG operations. |
| 135 | /// |
Chris Lattner | 2c79de8 | 2006-06-28 23:27:49 +0000 | [diff] [blame] | 136 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 137 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 138 | /// X86-specific SelectionDAG. |
Dan Gohman | da8ac5f | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 139 | X86TargetLowering &X86Lowering; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 140 | |
| 141 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 142 | /// make the right decision when generating code for different targets. |
| 143 | const X86Subtarget *Subtarget; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 144 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 145 | /// CurBB - Current BB being isel'd. |
| 146 | /// |
| 147 | MachineBasicBlock *CurBB; |
| 148 | |
Evan Cheng | b7a75a5 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 149 | /// OptForSize - If true, selector should try to optimize for code size |
| 150 | /// instead of performance. |
| 151 | bool OptForSize; |
| 152 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 153 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 154 | explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel) |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 155 | : SelectionDAGISel(tm, OptLevel), |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 156 | X86Lowering(*tm.getTargetLowering()), |
| 157 | Subtarget(&tm.getSubtarget<X86Subtarget>()), |
Devang Patel | 4ae641f | 2008-10-01 23:18:38 +0000 | [diff] [blame] | 158 | OptForSize(false) {} |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 159 | |
| 160 | virtual const char *getPassName() const { |
| 161 | return "X86 DAG->DAG Instruction Selection"; |
| 162 | } |
| 163 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 164 | /// InstructionSelect - This callback is invoked by |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 165 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 166 | virtual void InstructionSelect(); |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 167 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 168 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 169 | |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 170 | virtual |
| 171 | bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 172 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 173 | // Include the pieces autogenerated from the target description. |
| 174 | #include "X86GenDAGISel.inc" |
| 175 | |
| 176 | private: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 177 | SDNode *Select(SDValue N); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 178 | SDNode *SelectAtomic64(SDNode *Node, unsigned Opc); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 179 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 180 | bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM); |
| 181 | bool MatchLoad(SDValue N, X86ISelAddressMode &AM); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 182 | bool MatchWrapper(SDValue N, X86ISelAddressMode &AM); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 183 | bool MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 184 | unsigned Depth = 0); |
| 185 | bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 186 | bool SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 187 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 188 | SDValue &Segment); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 189 | bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base, |
| 190 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 191 | bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base, |
| 192 | SDValue &Scale, SDValue &Index, SDValue &Disp); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 193 | bool SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 194 | SDValue N, SDValue &Base, SDValue &Scale, |
| 195 | SDValue &Index, SDValue &Disp, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 196 | SDValue &Segment, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 197 | SDValue &InChain, SDValue &OutChain); |
| 198 | bool TryFoldLoad(SDValue P, SDValue N, |
| 199 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 200 | SDValue &Index, SDValue &Disp, |
| 201 | SDValue &Segment); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 202 | void PreprocessForRMW(); |
| 203 | void PreprocessForFPConvert(); |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 204 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 205 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 206 | /// inline asm expressions. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 207 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 208 | char ConstraintCode, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 209 | std::vector<SDValue> &OutOps); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 210 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 211 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 212 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 213 | inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, |
| 214 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 215 | SDValue &Disp, SDValue &Segment) { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 216 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 217 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : |
| 218 | AM.Base.Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 219 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 220 | Index = AM.IndexReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 221 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 222 | // is 32-bit. |
| 223 | if (AM.GV) |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 224 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp, |
| 225 | AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 226 | else if (AM.CP) |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 227 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 228 | AM.Align, AM.Disp, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 229 | else if (AM.ES) |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 230 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 231 | else if (AM.JT != -1) |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 232 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 233 | else |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 234 | Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 235 | |
| 236 | if (AM.Segment.getNode()) |
| 237 | Segment = AM.Segment; |
| 238 | else |
| 239 | Segment = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 242 | /// getI8Imm - Return a target constant with the specified value, of type |
| 243 | /// i8. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 244 | inline SDValue getI8Imm(unsigned Imm) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 245 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 246 | } |
| 247 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 248 | /// getI16Imm - Return a target constant with the specified value, of type |
| 249 | /// i16. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 250 | inline SDValue getI16Imm(unsigned Imm) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 251 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 252 | } |
| 253 | |
| 254 | /// getI32Imm - Return a target constant with the specified value, of type |
| 255 | /// i32. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 256 | inline SDValue getI32Imm(unsigned Imm) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 257 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 258 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 259 | |
Dan Gohman | 8b74696 | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 260 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 261 | /// the global base register. Output instructions required to |
| 262 | /// initialize the global base register, if necessary. |
| 263 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 264 | SDNode *getGlobalBaseReg(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 265 | |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 266 | /// getTargetMachine - Return a reference to the TargetMachine, casted |
| 267 | /// to the target-specific type. |
| 268 | const X86TargetMachine &getTargetMachine() { |
| 269 | return static_cast<const X86TargetMachine &>(TM); |
| 270 | } |
| 271 | |
| 272 | /// getInstrInfo - Return a reference to the TargetInstrInfo, casted |
| 273 | /// to the target-specific type. |
| 274 | const X86InstrInfo *getInstrInfo() { |
| 275 | return getTargetMachine().getInstrInfo(); |
| 276 | } |
| 277 | |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 278 | #ifndef NDEBUG |
| 279 | unsigned Indent; |
| 280 | #endif |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 281 | }; |
| 282 | } |
| 283 | |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 284 | |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 285 | bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U, |
| 286 | SDNode *Root) const { |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 287 | if (OptLevel == CodeGenOpt::None) return false; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 288 | |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 289 | if (U == Root) |
| 290 | switch (U->getOpcode()) { |
| 291 | default: break; |
| 292 | case ISD::ADD: |
| 293 | case ISD::ADDC: |
| 294 | case ISD::ADDE: |
| 295 | case ISD::AND: |
| 296 | case ISD::OR: |
| 297 | case ISD::XOR: { |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 298 | SDValue Op1 = U->getOperand(1); |
| 299 | |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 300 | // If the other operand is a 8-bit immediate we should fold the immediate |
| 301 | // instead. This reduces code size. |
| 302 | // e.g. |
| 303 | // movl 4(%esp), %eax |
| 304 | // addl $4, %eax |
| 305 | // vs. |
| 306 | // movl $4, %eax |
| 307 | // addl 4(%esp), %eax |
| 308 | // The former is 2 bytes shorter. In case where the increment is 1, then |
| 309 | // the saving can be 4 bytes (by using incl %eax). |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 310 | if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1)) |
Dan Gohman | 9a49d31 | 2009-03-14 02:07:16 +0000 | [diff] [blame] | 311 | if (Imm->getAPIntValue().isSignedIntN(8)) |
| 312 | return false; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 313 | |
| 314 | // If the other operand is a TLS address, we should fold it instead. |
| 315 | // This produces |
| 316 | // movl %gs:0, %eax |
| 317 | // leal i@NTPOFF(%eax), %eax |
| 318 | // instead of |
| 319 | // movl $i@NTPOFF, %eax |
| 320 | // addl %gs:0, %eax |
| 321 | // if the block also has an access to a second TLS address this will save |
| 322 | // a load. |
| 323 | // FIXME: This is probably also true for non TLS addresses. |
| 324 | if (Op1.getOpcode() == X86ISD::Wrapper) { |
| 325 | SDValue Val = Op1.getOperand(0); |
| 326 | if (Val.getOpcode() == ISD::TargetGlobalTLSAddress) |
| 327 | return false; |
| 328 | } |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Anton Korobeynikov | c1c6ef8 | 2009-05-08 18:51:58 +0000 | [diff] [blame] | 332 | // Proceed to 'generic' cycle finder code |
| 333 | return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root); |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 336 | /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand |
| 337 | /// and move load below the TokenFactor. Replace store's chain operand with |
| 338 | /// load's chain result. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 339 | static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 340 | SDValue Store, SDValue TF) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 341 | SmallVector<SDValue, 4> Ops; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 342 | for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i) |
| 343 | if (Load.getNode() == TF.getOperand(i).getNode()) |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 344 | Ops.push_back(Load.getOperand(0)); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 345 | else |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 346 | Ops.push_back(TF.getOperand(i)); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 347 | CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size()); |
| 348 | CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2)); |
| 349 | CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1), |
| 350 | Store.getOperand(2), Store.getOperand(3)); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Evan Cheng | cd0baf2 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 353 | /// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. |
| 354 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 355 | static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address, |
| 356 | SDValue &Load) { |
Evan Cheng | cd0baf2 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 357 | if (N.getOpcode() == ISD::BIT_CONVERT) |
| 358 | N = N.getOperand(0); |
| 359 | |
| 360 | LoadSDNode *LD = dyn_cast<LoadSDNode>(N); |
| 361 | if (!LD || LD->isVolatile()) |
| 362 | return false; |
| 363 | if (LD->getAddressingMode() != ISD::UNINDEXED) |
| 364 | return false; |
| 365 | |
| 366 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 367 | if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD) |
| 368 | return false; |
| 369 | |
| 370 | if (N.hasOneUse() && |
| 371 | N.getOperand(1) == Address && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 372 | N.getNode()->isOperandOf(Chain.getNode())) { |
Evan Cheng | cd0baf2 | 2008-05-23 21:23:16 +0000 | [diff] [blame] | 373 | Load = N; |
| 374 | return true; |
| 375 | } |
| 376 | return false; |
| 377 | } |
| 378 | |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 379 | /// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain |
| 380 | /// operand and move load below the call's chain operand. |
| 381 | static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load, |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 382 | SDValue Call, SDValue CallSeqStart) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 383 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 384 | SDValue Chain = CallSeqStart.getOperand(0); |
| 385 | if (Chain.getNode() == Load.getNode()) |
| 386 | Ops.push_back(Load.getOperand(0)); |
| 387 | else { |
| 388 | assert(Chain.getOpcode() == ISD::TokenFactor && |
| 389 | "Unexpected CallSeqStart chain operand"); |
| 390 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 391 | if (Chain.getOperand(i).getNode() == Load.getNode()) |
| 392 | Ops.push_back(Load.getOperand(0)); |
| 393 | else |
| 394 | Ops.push_back(Chain.getOperand(i)); |
| 395 | SDValue NewChain = |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 396 | CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(), |
| 397 | MVT::Other, &Ops[0], Ops.size()); |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 398 | Ops.clear(); |
| 399 | Ops.push_back(NewChain); |
| 400 | } |
| 401 | for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i) |
| 402 | Ops.push_back(CallSeqStart.getOperand(i)); |
| 403 | CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size()); |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 404 | CurDAG->UpdateNodeOperands(Load, Call.getOperand(0), |
| 405 | Load.getOperand(1), Load.getOperand(2)); |
| 406 | Ops.clear(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 407 | Ops.push_back(SDValue(Load.getNode(), 1)); |
| 408 | for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i) |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 409 | Ops.push_back(Call.getOperand(i)); |
| 410 | CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size()); |
| 411 | } |
| 412 | |
| 413 | /// isCalleeLoad - Return true if call address is a load and it can be |
| 414 | /// moved below CALLSEQ_START and the chains leading up to the call. |
| 415 | /// Return the CALLSEQ_START by reference as a second output. |
| 416 | static bool isCalleeLoad(SDValue Callee, SDValue &Chain) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 417 | if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse()) |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 418 | return false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 419 | LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode()); |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 420 | if (!LD || |
| 421 | LD->isVolatile() || |
| 422 | LD->getAddressingMode() != ISD::UNINDEXED || |
| 423 | LD->getExtensionType() != ISD::NON_EXTLOAD) |
| 424 | return false; |
| 425 | |
| 426 | // Now let's find the callseq_start. |
| 427 | while (Chain.getOpcode() != ISD::CALLSEQ_START) { |
| 428 | if (!Chain.hasOneUse()) |
| 429 | return false; |
| 430 | Chain = Chain.getOperand(0); |
| 431 | } |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 432 | |
| 433 | if (Chain.getOperand(0).getNode() == Callee.getNode()) |
| 434 | return true; |
| 435 | if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor && |
| 436 | Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode())) |
| 437 | return true; |
| 438 | return false; |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 442 | /// PreprocessForRMW - Preprocess the DAG to make instruction selection better. |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 443 | /// This is only run if not in -O0 mode. |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 444 | /// This allows the instruction selector to pick more read-modify-write |
| 445 | /// instructions. This is a common case: |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 446 | /// |
| 447 | /// [Load chain] |
| 448 | /// ^ |
| 449 | /// | |
| 450 | /// [Load] |
| 451 | /// ^ ^ |
| 452 | /// | | |
| 453 | /// / \- |
| 454 | /// / | |
| 455 | /// [TokenFactor] [Op] |
| 456 | /// ^ ^ |
| 457 | /// | | |
| 458 | /// \ / |
| 459 | /// \ / |
| 460 | /// [Store] |
| 461 | /// |
| 462 | /// The fact the store's chain operand != load's chain will prevent the |
| 463 | /// (store (op (load))) instruction from being selected. We can transform it to: |
| 464 | /// |
| 465 | /// [Load chain] |
| 466 | /// ^ |
| 467 | /// | |
| 468 | /// [TokenFactor] |
| 469 | /// ^ |
| 470 | /// | |
| 471 | /// [Load] |
| 472 | /// ^ ^ |
| 473 | /// | | |
| 474 | /// | \- |
| 475 | /// | | |
| 476 | /// | [Op] |
| 477 | /// | ^ |
| 478 | /// | | |
| 479 | /// \ / |
| 480 | /// \ / |
| 481 | /// [Store] |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 482 | void X86DAGToDAGISel::PreprocessForRMW() { |
| 483 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 484 | E = CurDAG->allnodes_end(); I != E; ++I) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 485 | if (I->getOpcode() == X86ISD::CALL) { |
| 486 | /// Also try moving call address load from outside callseq_start to just |
| 487 | /// before the call to allow it to be folded. |
| 488 | /// |
| 489 | /// [Load chain] |
| 490 | /// ^ |
| 491 | /// | |
| 492 | /// [Load] |
| 493 | /// ^ ^ |
| 494 | /// | | |
| 495 | /// / \-- |
| 496 | /// / | |
| 497 | ///[CALLSEQ_START] | |
| 498 | /// ^ | |
| 499 | /// | | |
| 500 | /// [LOAD/C2Reg] | |
| 501 | /// | | |
| 502 | /// \ / |
| 503 | /// \ / |
| 504 | /// [CALL] |
| 505 | SDValue Chain = I->getOperand(0); |
| 506 | SDValue Load = I->getOperand(1); |
| 507 | if (!isCalleeLoad(Load, Chain)) |
| 508 | continue; |
| 509 | MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain); |
| 510 | ++NumLoadMoved; |
| 511 | continue; |
| 512 | } |
| 513 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 514 | if (!ISD::isNON_TRUNCStore(I)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 515 | continue; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 516 | SDValue Chain = I->getOperand(0); |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 517 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 518 | if (Chain.getNode()->getOpcode() != ISD::TokenFactor) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 519 | continue; |
| 520 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 521 | SDValue N1 = I->getOperand(1); |
| 522 | SDValue N2 = I->getOperand(2); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 523 | if ((N1.getValueType().isFloatingPoint() && |
| 524 | !N1.getValueType().isVector()) || |
Evan Cheng | 780413d | 2006-08-29 18:37:37 +0000 | [diff] [blame] | 525 | !N1.hasOneUse()) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 526 | continue; |
| 527 | |
| 528 | bool RModW = false; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 529 | SDValue Load; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 530 | unsigned Opcode = N1.getNode()->getOpcode(); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 531 | switch (Opcode) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 532 | case ISD::ADD: |
| 533 | case ISD::MUL: |
| 534 | case ISD::AND: |
| 535 | case ISD::OR: |
| 536 | case ISD::XOR: |
| 537 | case ISD::ADDC: |
| 538 | case ISD::ADDE: |
| 539 | case ISD::VECTOR_SHUFFLE: { |
| 540 | SDValue N10 = N1.getOperand(0); |
| 541 | SDValue N11 = N1.getOperand(1); |
| 542 | RModW = isRMWLoad(N10, Chain, N2, Load); |
| 543 | if (!RModW) |
| 544 | RModW = isRMWLoad(N11, Chain, N2, Load); |
| 545 | break; |
| 546 | } |
| 547 | case ISD::SUB: |
| 548 | case ISD::SHL: |
| 549 | case ISD::SRA: |
| 550 | case ISD::SRL: |
| 551 | case ISD::ROTL: |
| 552 | case ISD::ROTR: |
| 553 | case ISD::SUBC: |
| 554 | case ISD::SUBE: |
| 555 | case X86ISD::SHLD: |
| 556 | case X86ISD::SHRD: { |
| 557 | SDValue N10 = N1.getOperand(0); |
| 558 | RModW = isRMWLoad(N10, Chain, N2, Load); |
| 559 | break; |
| 560 | } |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 563 | if (RModW) { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 564 | MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain); |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 565 | ++NumLoadMoved; |
| 566 | } |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 570 | |
| 571 | /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend |
| 572 | /// nodes that target the FP stack to be store and load to the stack. This is a |
| 573 | /// gross hack. We would like to simply mark these as being illegal, but when |
| 574 | /// we do that, legalize produces these when it expands calls, then expands |
| 575 | /// these in the same legalize pass. We would like dag combine to be able to |
| 576 | /// hack on these between the call expansion and the node legalization. As such |
| 577 | /// this pass basically does "really late" legalization of these inline with the |
| 578 | /// X86 isel pass. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 579 | void X86DAGToDAGISel::PreprocessForFPConvert() { |
| 580 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 581 | E = CurDAG->allnodes_end(); I != E; ) { |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 582 | SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
| 583 | if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) |
| 584 | continue; |
| 585 | |
| 586 | // If the source and destination are SSE registers, then this is a legal |
| 587 | // conversion that should not be lowered. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 588 | MVT SrcVT = N->getOperand(0).getValueType(); |
| 589 | MVT DstVT = N->getValueType(0); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 590 | bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT); |
| 591 | bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT); |
| 592 | if (SrcIsSSE && DstIsSSE) |
| 593 | continue; |
| 594 | |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 595 | if (!SrcIsSSE && !DstIsSSE) { |
| 596 | // If this is an FPStack extension, it is a noop. |
| 597 | if (N->getOpcode() == ISD::FP_EXTEND) |
| 598 | continue; |
| 599 | // If this is a value-preserving FPStack truncation, it is a noop. |
| 600 | if (N->getConstantOperandVal(1)) |
| 601 | continue; |
| 602 | } |
| 603 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 604 | // Here we could have an FP stack truncation or an FPStack <-> SSE convert. |
| 605 | // FPStack has extload and truncstore. SSE can fold direct loads into other |
| 606 | // operations. Based on this, decide what we want to do. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 607 | MVT MemVT; |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 608 | if (N->getOpcode() == ISD::FP_ROUND) |
| 609 | MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. |
| 610 | else |
| 611 | MemVT = SrcIsSSE ? SrcVT : DstVT; |
| 612 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 613 | SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 614 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 615 | |
| 616 | // FIXME: optimize the case where the src/dest is a load or store? |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 617 | SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 618 | N->getOperand(0), |
| 619 | MemTmp, NULL, 0, MemVT); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 620 | SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 621 | NULL, 0, MemVT); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 622 | |
| 623 | // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the |
| 624 | // extload we created. This will cause general havok on the dag because |
| 625 | // anything below the conversion could be folded into other existing nodes. |
| 626 | // To avoid invalidating 'I', back it up to the convert node. |
| 627 | --I; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 628 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 629 | |
| 630 | // Now that we did that, the node is dead. Increment the iterator to the |
| 631 | // next node to process, then delete N. |
| 632 | ++I; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 633 | CurDAG->DeleteNode(N); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 637 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 638 | /// when it has created a SelectionDAG for us to codegen. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 639 | void X86DAGToDAGISel::InstructionSelect() { |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 640 | CurBB = BB; // BB can change as result of isel. |
Devang Patel | e76225a | 2008-10-06 18:03:39 +0000 | [diff] [blame] | 641 | const Function *F = CurDAG->getMachineFunction().getFunction(); |
| 642 | OptForSize = F->hasFnAttr(Attribute::OptimizeForSize); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 643 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 644 | DEBUG(BB->dump()); |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 645 | if (OptLevel != CodeGenOpt::None) |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 646 | PreprocessForRMW(); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 647 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 648 | // FIXME: This should only happen when not compiled with -O0. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 649 | PreprocessForFPConvert(); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 650 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 651 | // Codegen the basic block. |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 652 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 653 | DOUT << "===== Instruction selection begins:\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 654 | Indent = 0; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 655 | #endif |
David Greene | 8ad4c00 | 2008-10-27 21:56:29 +0000 | [diff] [blame] | 656 | SelectRoot(*CurDAG); |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 657 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 658 | DOUT << "===== Instruction selection ends:\n"; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 659 | #endif |
Evan Cheng | 63ce568 | 2006-07-28 00:10:59 +0000 | [diff] [blame] | 660 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 661 | CurDAG->RemoveDeadNodes(); |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 663 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 664 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 665 | /// the main function. |
| 666 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 667 | MachineFrameInfo *MFI) { |
| 668 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 669 | if (Subtarget->isTargetCygMing()) |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 670 | BuildMI(BB, DebugLoc::getUnknownLoc(), |
| 671 | TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 675 | // If this is main, emit special code for main. |
| 676 | MachineBasicBlock *BB = MF.begin(); |
| 677 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 678 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 679 | } |
| 680 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 681 | |
| 682 | bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N, |
| 683 | X86ISelAddressMode &AM) { |
| 684 | assert(N.getOpcode() == X86ISD::SegmentBaseAddress); |
| 685 | SDValue Segment = N.getOperand(0); |
| 686 | |
| 687 | if (AM.Segment.getNode() == 0) { |
| 688 | AM.Segment = Segment; |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) { |
| 696 | // This optimization is valid because the GNU TLS model defines that |
| 697 | // gs:0 (or fs:0 on X86-64) contains its own address. |
| 698 | // For more information see http://people.redhat.com/drepper/tls.pdf |
| 699 | |
| 700 | SDValue Address = N.getOperand(1); |
| 701 | if (Address.getOpcode() == X86ISD::SegmentBaseAddress && |
| 702 | !MatchSegmentBaseAddress (Address, AM)) |
| 703 | return false; |
| 704 | |
| 705 | return true; |
| 706 | } |
| 707 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 708 | /// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes |
| 709 | /// into an addressing mode. These wrap things that will resolve down into a |
| 710 | /// symbol reference. If no match is possible, this returns true, otherwise it |
| 711 | /// returns false. |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 712 | bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 713 | // If the addressing mode already has a symbol as the displacement, we can |
| 714 | // never match another symbol. |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 715 | if (AM.hasSymbolicDisplacement()) |
| 716 | return true; |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 717 | |
| 718 | SDValue N0 = N.getOperand(0); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 719 | |
| 720 | // Handle X86-64 rip-relative addresses. We check this before checking direct |
| 721 | // folding because RIP is preferable to non-RIP accesses. |
| 722 | if (Subtarget->is64Bit() && |
| 723 | // Under X86-64 non-small code model, GV (and friends) are 64-bits, so |
| 724 | // they cannot be folded into immediate fields. |
| 725 | // FIXME: This can be improved for kernel and other models? |
| 726 | TM.getCodeModel() == CodeModel::Small && |
| 727 | |
| 728 | // Base and index reg must be 0 in order to use %rip as base and lowering |
| 729 | // must allow RIP. |
| 730 | !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) { |
| 731 | |
| 732 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 733 | int64_t Offset = AM.Disp + G->getOffset(); |
| 734 | if (!isInt32(Offset)) return true; |
| 735 | AM.GV = G->getGlobal(); |
| 736 | AM.Disp = Offset; |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 737 | AM.SymbolFlags = G->getTargetFlags(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 738 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
| 739 | int64_t Offset = AM.Disp + CP->getOffset(); |
| 740 | if (!isInt32(Offset)) return true; |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 741 | AM.CP = CP->getConstVal(); |
| 742 | AM.Align = CP->getAlignment(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 743 | AM.Disp = Offset; |
Chris Lattner | 0b0deab | 2009-06-26 05:56:49 +0000 | [diff] [blame] | 744 | AM.SymbolFlags = CP->getTargetFlags(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 745 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 746 | AM.ES = S->getSymbol(); |
| 747 | AM.SymbolFlags = S->getTargetFlags(); |
| 748 | } else { |
| 749 | JumpTableSDNode *J = cast<JumpTableSDNode>(N0); |
| 750 | AM.JT = J->getIndex(); |
| 751 | AM.SymbolFlags = J->getTargetFlags(); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 752 | } |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 753 | |
| 754 | if (N.getOpcode() == X86ISD::WrapperRIP) |
| 755 | AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64)); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 756 | return false; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // Handle the case when globals fit in our immediate field: This is true for |
| 760 | // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit |
| 761 | // mode, this results in a non-RIP-relative computation. |
| 762 | if (!Subtarget->is64Bit() || |
| 763 | (TM.getCodeModel() == CodeModel::Small && |
| 764 | TM.getRelocationModel() == Reloc::Static)) { |
| 765 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 766 | AM.GV = G->getGlobal(); |
| 767 | AM.Disp += G->getOffset(); |
| 768 | AM.SymbolFlags = G->getTargetFlags(); |
| 769 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
| 770 | AM.CP = CP->getConstVal(); |
| 771 | AM.Align = CP->getAlignment(); |
| 772 | AM.Disp += CP->getOffset(); |
| 773 | AM.SymbolFlags = CP->getTargetFlags(); |
| 774 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 775 | AM.ES = S->getSymbol(); |
| 776 | AM.SymbolFlags = S->getTargetFlags(); |
| 777 | } else { |
| 778 | JumpTableSDNode *J = cast<JumpTableSDNode>(N0); |
| 779 | AM.JT = J->getIndex(); |
| 780 | AM.SymbolFlags = J->getTargetFlags(); |
| 781 | } |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
| 785 | return true; |
| 786 | } |
| 787 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 788 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 789 | /// returning true if it cannot be done. This just pattern matches for the |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 790 | /// addressing mode. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 791 | bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM, |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 792 | unsigned Depth) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 793 | bool is64Bit = Subtarget->is64Bit(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 794 | DebugLoc dl = N.getDebugLoc(); |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 795 | DOUT << "MatchAddress: "; DEBUG(AM.dump()); |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 796 | // Limit recursion. |
| 797 | if (Depth > 5) |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 798 | return MatchAddressBase(N, AM); |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 800 | // If this is already a %rip relative address, we can only merge immediates |
| 801 | // into it. Instead of handling this in every case, we handle it here. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 802 | // RIP relative addressing: %rip + 32-bit displacement! |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 803 | if (AM.isRIPRelative()) { |
| 804 | // FIXME: JumpTable and ExternalSymbol address currently don't like |
| 805 | // displacements. It isn't very important, but this should be fixed for |
| 806 | // consistency. |
| 807 | if (!AM.ES && AM.JT != -1) return true; |
| 808 | |
| 809 | if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) { |
| 810 | int64_t Val = AM.Disp + Cst->getSExtValue(); |
| 811 | if (isInt32(Val)) { |
| 812 | AM.Disp = Val; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 813 | return false; |
| 814 | } |
| 815 | } |
| 816 | return true; |
| 817 | } |
| 818 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 819 | switch (N.getOpcode()) { |
| 820 | default: break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 821 | case ISD::Constant: { |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 822 | uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 823 | if (!is64Bit || isInt32(AM.Disp + Val)) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 824 | AM.Disp += Val; |
| 825 | return false; |
| 826 | } |
| 827 | break; |
| 828 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 829 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 830 | case X86ISD::SegmentBaseAddress: |
| 831 | if (!MatchSegmentBaseAddress(N, AM)) |
| 832 | return false; |
| 833 | break; |
| 834 | |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 835 | case X86ISD::Wrapper: |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 836 | case X86ISD::WrapperRIP: |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 837 | if (!MatchWrapper(N, AM)) |
| 838 | return false; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 839 | break; |
| 840 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 841 | case ISD::LOAD: |
| 842 | if (!MatchLoad(N, AM)) |
| 843 | return false; |
| 844 | break; |
| 845 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 846 | case ISD::FrameIndex: |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 847 | if (AM.BaseType == X86ISelAddressMode::RegBase |
| 848 | && AM.Base.Reg.getNode() == 0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 849 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 850 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 851 | return false; |
| 852 | } |
| 853 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 854 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 855 | case ISD::SHL: |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 856 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 857 | break; |
| 858 | |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 859 | if (ConstantSDNode |
| 860 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 861 | unsigned Val = CN->getZExtValue(); |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 862 | if (Val == 1 || Val == 2 || Val == 3) { |
| 863 | AM.Scale = 1 << Val; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 864 | SDValue ShVal = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 865 | |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 866 | // Okay, we know that we have a scale by now. However, if the scaled |
| 867 | // value is an add of something and a constant, we can fold the |
| 868 | // constant into the disp field here. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 869 | if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 870 | isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) { |
| 871 | AM.IndexReg = ShVal.getNode()->getOperand(0); |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 872 | ConstantSDNode *AddVal = |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 873 | cast<ConstantSDNode>(ShVal.getNode()->getOperand(1)); |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 874 | uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 875 | if (!is64Bit || isInt32(Disp)) |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 876 | AM.Disp = Disp; |
| 877 | else |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 878 | AM.IndexReg = ShVal; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 879 | } else { |
| 880 | AM.IndexReg = ShVal; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 882 | return false; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 883 | } |
| 884 | break; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 885 | } |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 886 | |
Dan Gohman | 8368805 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 887 | case ISD::SMUL_LOHI: |
| 888 | case ISD::UMUL_LOHI: |
| 889 | // A mul_lohi where we need the low part can be folded as a plain multiply. |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 890 | if (N.getResNo() != 0) break; |
Dan Gohman | 8368805 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 891 | // FALL THROUGH |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 892 | case ISD::MUL: |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 893 | case X86ISD::MUL_IMM: |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 894 | // X*[3,5,9] -> X+X*[2,4,8] |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 895 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 896 | AM.Base.Reg.getNode() == 0 && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 897 | AM.IndexReg.getNode() == 0) { |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 898 | if (ConstantSDNode |
| 899 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 900 | if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || |
| 901 | CN->getZExtValue() == 9) { |
| 902 | AM.Scale = unsigned(CN->getZExtValue())-1; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 903 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 904 | SDValue MulVal = N.getNode()->getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 905 | SDValue Reg; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 906 | |
| 907 | // Okay, we know that we have a scale by now. However, if the scaled |
| 908 | // value is an add of something and a constant, we can fold the |
| 909 | // constant into the disp field here. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 910 | if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 911 | isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { |
| 912 | Reg = MulVal.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 913 | ConstantSDNode *AddVal = |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 914 | cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 915 | uint64_t Disp = AM.Disp + AddVal->getSExtValue() * |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 916 | CN->getZExtValue(); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 917 | if (!is64Bit || isInt32(Disp)) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 918 | AM.Disp = Disp; |
| 919 | else |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 920 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 921 | } else { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 922 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | AM.IndexReg = AM.Base.Reg = Reg; |
| 926 | return false; |
| 927 | } |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 928 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 929 | break; |
| 930 | |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 931 | case ISD::SUB: { |
| 932 | // Given A-B, if A can be completely folded into the address and |
| 933 | // the index field with the index field unused, use -B as the index. |
| 934 | // This is a win if a has multiple parts that can be folded into |
| 935 | // the address. Also, this saves a mov if the base register has |
| 936 | // other uses, since it avoids a two-address sub instruction, however |
| 937 | // it costs an additional mov if the index register has other uses. |
| 938 | |
| 939 | // Test if the LHS of the sub can be folded. |
| 940 | X86ISelAddressMode Backup = AM; |
| 941 | if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) { |
| 942 | AM = Backup; |
| 943 | break; |
| 944 | } |
| 945 | // Test if the index field is free for use. |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 946 | if (AM.IndexReg.getNode() || AM.isRIPRelative()) { |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 947 | AM = Backup; |
| 948 | break; |
| 949 | } |
| 950 | int Cost = 0; |
| 951 | SDValue RHS = N.getNode()->getOperand(1); |
| 952 | // If the RHS involves a register with multiple uses, this |
| 953 | // transformation incurs an extra mov, due to the neg instruction |
| 954 | // clobbering its operand. |
| 955 | if (!RHS.getNode()->hasOneUse() || |
| 956 | RHS.getNode()->getOpcode() == ISD::CopyFromReg || |
| 957 | RHS.getNode()->getOpcode() == ISD::TRUNCATE || |
| 958 | RHS.getNode()->getOpcode() == ISD::ANY_EXTEND || |
| 959 | (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND && |
| 960 | RHS.getNode()->getOperand(0).getValueType() == MVT::i32)) |
| 961 | ++Cost; |
| 962 | // If the base is a register with multiple uses, this |
| 963 | // transformation may save a mov. |
| 964 | if ((AM.BaseType == X86ISelAddressMode::RegBase && |
| 965 | AM.Base.Reg.getNode() && |
| 966 | !AM.Base.Reg.getNode()->hasOneUse()) || |
| 967 | AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 968 | --Cost; |
| 969 | // If the folded LHS was interesting, this transformation saves |
| 970 | // address arithmetic. |
| 971 | if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) + |
| 972 | ((AM.Disp != 0) && (Backup.Disp == 0)) + |
| 973 | (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2) |
| 974 | --Cost; |
| 975 | // If it doesn't look like it may be an overall win, don't do it. |
| 976 | if (Cost >= 0) { |
| 977 | AM = Backup; |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | // Ok, the transformation is legal and appears profitable. Go for it. |
| 982 | SDValue Zero = CurDAG->getConstant(0, N.getValueType()); |
| 983 | SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS); |
| 984 | AM.IndexReg = Neg; |
| 985 | AM.Scale = 1; |
| 986 | |
| 987 | // Insert the new nodes into the topological ordering. |
| 988 | if (Zero.getNode()->getNodeId() == -1 || |
| 989 | Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 990 | CurDAG->RepositionNode(N.getNode(), Zero.getNode()); |
| 991 | Zero.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 992 | } |
| 993 | if (Neg.getNode()->getNodeId() == -1 || |
| 994 | Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 995 | CurDAG->RepositionNode(N.getNode(), Neg.getNode()); |
| 996 | Neg.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 997 | } |
| 998 | return false; |
| 999 | } |
| 1000 | |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1001 | case ISD::ADD: { |
| 1002 | X86ISelAddressMode Backup = AM; |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1003 | if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) && |
| 1004 | !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1)) |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | AM = Backup; |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1007 | if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) && |
| 1008 | !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1009 | return false; |
| 1010 | AM = Backup; |
Dan Gohman | 77502c9 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 1011 | |
| 1012 | // If we couldn't fold both operands into the address at the same time, |
| 1013 | // see if we can just put each operand into a register and fold at least |
| 1014 | // the add. |
| 1015 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
| 1016 | !AM.Base.Reg.getNode() && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1017 | !AM.IndexReg.getNode()) { |
Dan Gohman | 77502c9 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 1018 | AM.Base.Reg = N.getNode()->getOperand(0); |
| 1019 | AM.IndexReg = N.getNode()->getOperand(1); |
| 1020 | AM.Scale = 1; |
| 1021 | return false; |
| 1022 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1023 | break; |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1024 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 1026 | case ISD::OR: |
| 1027 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1028 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1029 | X86ISelAddressMode Backup = AM; |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 1030 | uint64_t Offset = CN->getSExtValue(); |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1031 | // Start with the LHS as an addr mode. |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1032 | if (!MatchAddress(N.getOperand(0), AM, Depth+1) && |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1033 | // Address could not have picked a GV address for the displacement. |
| 1034 | AM.GV == NULL && |
| 1035 | // On x86-64, the resultant disp must fit in 32-bits. |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 1036 | (!is64Bit || isInt32(AM.Disp + Offset)) && |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1037 | // Check to see if the LHS & C is zero. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1038 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 1039 | AM.Disp += Offset; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1040 | return false; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1041 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1042 | AM = Backup; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1043 | } |
| 1044 | break; |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1045 | |
| 1046 | case ISD::AND: { |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1047 | // Perform some heroic transforms on an and of a constant-count shift |
| 1048 | // with a constant to enable use of the scaled offset field. |
| 1049 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1050 | SDValue Shift = N.getOperand(0); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1051 | if (Shift.getNumOperands() != 2) break; |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1052 | |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1053 | // Scale must not be used already. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1054 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break; |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 1055 | |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1056 | SDValue X = Shift.getOperand(0); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1057 | ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 1058 | ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); |
| 1059 | if (!C1 || !C2) break; |
| 1060 | |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1061 | // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This |
| 1062 | // allows us to convert the shift and and into an h-register extract and |
| 1063 | // a scaled index. |
| 1064 | if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) { |
| 1065 | unsigned ScaleLog = 8 - C1->getZExtValue(); |
Rafael Espindola | 7c36683 | 2009-04-16 12:34:53 +0000 | [diff] [blame] | 1066 | if (ScaleLog > 0 && ScaleLog < 4 && |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1067 | C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) { |
| 1068 | SDValue Eight = CurDAG->getConstant(8, MVT::i8); |
| 1069 | SDValue Mask = CurDAG->getConstant(0xff, N.getValueType()); |
| 1070 | SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(), |
| 1071 | X, Eight); |
| 1072 | SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(), |
| 1073 | Srl, Mask); |
Dan Gohman | 62ad138 | 2009-04-14 22:45:05 +0000 | [diff] [blame] | 1074 | SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8); |
| 1075 | SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(), |
| 1076 | And, ShlCount); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Insert the new nodes into the topological ordering. |
| 1079 | if (Eight.getNode()->getNodeId() == -1 || |
| 1080 | Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1081 | CurDAG->RepositionNode(X.getNode(), Eight.getNode()); |
| 1082 | Eight.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1083 | } |
| 1084 | if (Mask.getNode()->getNodeId() == -1 || |
| 1085 | Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1086 | CurDAG->RepositionNode(X.getNode(), Mask.getNode()); |
| 1087 | Mask.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1088 | } |
| 1089 | if (Srl.getNode()->getNodeId() == -1 || |
| 1090 | Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) { |
| 1091 | CurDAG->RepositionNode(Shift.getNode(), Srl.getNode()); |
| 1092 | Srl.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 1093 | } |
| 1094 | if (And.getNode()->getNodeId() == -1 || |
| 1095 | And.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1096 | CurDAG->RepositionNode(N.getNode(), And.getNode()); |
| 1097 | And.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1098 | } |
Dan Gohman | 62ad138 | 2009-04-14 22:45:05 +0000 | [diff] [blame] | 1099 | if (ShlCount.getNode()->getNodeId() == -1 || |
| 1100 | ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1101 | CurDAG->RepositionNode(X.getNode(), ShlCount.getNode()); |
| 1102 | ShlCount.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1103 | } |
| 1104 | if (Shl.getNode()->getNodeId() == -1 || |
| 1105 | Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1106 | CurDAG->RepositionNode(N.getNode(), Shl.getNode()); |
| 1107 | Shl.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1108 | } |
| 1109 | CurDAG->ReplaceAllUsesWith(N, Shl); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1110 | AM.IndexReg = And; |
| 1111 | AM.Scale = (1 << ScaleLog); |
| 1112 | return false; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this |
| 1117 | // allows us to fold the shift into this addressing mode. |
| 1118 | if (Shift.getOpcode() != ISD::SHL) break; |
| 1119 | |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1120 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 1121 | // than one use (unless all uses are for address computation). Besides, |
| 1122 | // isel mechanism requires their node ids to be reused. |
| 1123 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 1124 | break; |
| 1125 | |
| 1126 | // Verify that the shift amount is something we can fold. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1127 | unsigned ShiftCst = C1->getZExtValue(); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1128 | if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) |
| 1129 | break; |
| 1130 | |
| 1131 | // Get the new AND mask, this folds to a constant. |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1132 | SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(), |
Evan Cheng | 552e3be | 2008-10-14 17:15:39 +0000 | [diff] [blame] | 1133 | SDValue(C2, 0), SDValue(C1, 0)); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1134 | SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X, |
| 1135 | NewANDMask); |
| 1136 | SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(), |
Dan Gohman | 7b8e964 | 2008-10-13 20:52:04 +0000 | [diff] [blame] | 1137 | NewAND, SDValue(C1, 0)); |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Insert the new nodes into the topological ordering. |
| 1140 | if (C1->getNodeId() > X.getNode()->getNodeId()) { |
| 1141 | CurDAG->RepositionNode(X.getNode(), C1); |
| 1142 | C1->setNodeId(X.getNode()->getNodeId()); |
| 1143 | } |
| 1144 | if (NewANDMask.getNode()->getNodeId() == -1 || |
| 1145 | NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1146 | CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode()); |
| 1147 | NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1148 | } |
| 1149 | if (NewAND.getNode()->getNodeId() == -1 || |
| 1150 | NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) { |
| 1151 | CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode()); |
| 1152 | NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 1153 | } |
| 1154 | if (NewSHIFT.getNode()->getNodeId() == -1 || |
| 1155 | NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1156 | CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode()); |
| 1157 | NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1158 | } |
| 1159 | |
Dan Gohman | 7b8e964 | 2008-10-13 20:52:04 +0000 | [diff] [blame] | 1160 | CurDAG->ReplaceAllUsesWith(N, NewSHIFT); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1161 | |
| 1162 | AM.Scale = 1 << ShiftCst; |
| 1163 | AM.IndexReg = NewAND; |
| 1164 | return false; |
| 1165 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1166 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1167 | |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1168 | return MatchAddressBase(N, AM); |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 1172 | /// specified addressing mode without any further recursion. |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1173 | bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1174 | // Is the base register already occupied? |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1175 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1176 | // If so, check to see if the scale index register is set. |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1177 | if (AM.IndexReg.getNode() == 0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1178 | AM.IndexReg = N; |
| 1179 | AM.Scale = 1; |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | // Otherwise, we cannot select it. |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
| 1187 | // Default, generate it as a register. |
| 1188 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 1189 | AM.Base.Reg = N; |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1193 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 1194 | /// It returns the operands which make up the maximal addressing mode it can |
| 1195 | /// match by reference. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1196 | bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base, |
| 1197 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1198 | SDValue &Disp, SDValue &Segment) { |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1199 | X86ISelAddressMode AM; |
Evan Cheng | 4d95232 | 2009-03-31 01:13:53 +0000 | [diff] [blame] | 1200 | bool Done = false; |
| 1201 | if (AvoidDupAddrCompute && !N.hasOneUse()) { |
| 1202 | unsigned Opcode = N.getOpcode(); |
| 1203 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1204 | Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) { |
Evan Cheng | 4d95232 | 2009-03-31 01:13:53 +0000 | [diff] [blame] | 1205 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 1206 | // if N has multiple uses. In general, addressing computation is used as |
| 1207 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 1208 | // means the address computation is liveout. It will be computed by a LEA |
| 1209 | // so we want to avoid computing the address twice. |
| 1210 | for (SDNode::use_iterator UI = N.getNode()->use_begin(), |
| 1211 | UE = N.getNode()->use_end(); UI != UE; ++UI) { |
| 1212 | if (UI->getOpcode() == ISD::CopyToReg) { |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1213 | MatchAddressBase(N, AM); |
Evan Cheng | 4d95232 | 2009-03-31 01:13:53 +0000 | [diff] [blame] | 1214 | Done = true; |
| 1215 | break; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | if (!Done && MatchAddress(N, AM)) |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1222 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1223 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1224 | MVT VT = N.getValueType(); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1225 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1226 | if (!AM.Base.Reg.getNode()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1227 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1228 | } |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1229 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1230 | if (!AM.IndexReg.getNode()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1231 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1232 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1233 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1234 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1237 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 1238 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 1239 | /// is derived from the type of N, which is either v4f32 or v2f64. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1240 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred, |
| 1241 | SDValue N, SDValue &Base, |
| 1242 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1243 | SDValue &Disp, SDValue &Segment, |
| 1244 | SDValue &InChain, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1245 | SDValue &OutChain) { |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1246 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1247 | InChain = N.getOperand(0).getValue(1); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1248 | if (ISD::isNON_EXTLoad(InChain.getNode()) && |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 1249 | InChain.getValue(0).hasOneUse() && |
Evan Cheng | d6373bc | 2006-11-10 21:23:04 +0000 | [diff] [blame] | 1250 | N.hasOneUse() && |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 1251 | IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) { |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 1252 | LoadSDNode *LD = cast<LoadSDNode>(InChain); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1253 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1254 | return false; |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 1255 | OutChain = LD->getChain(); |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1256 | return true; |
| 1257 | } |
| 1258 | } |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1259 | |
| 1260 | // Also handle the case where we explicitly require zeros in the top |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1261 | // elements. This is a vector shuffle from the zero vector. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1262 | if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 1263 | // Check to see if the top elements are all zeros (or bitcast of zeros). |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1264 | N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1265 | N.getOperand(0).getNode()->hasOneUse() && |
| 1266 | ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1267 | N.getOperand(0).getOperand(0).hasOneUse()) { |
| 1268 | // Okay, this is a zero extending load. Fold it. |
| 1269 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1270 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1271 | return false; |
| 1272 | OutChain = LD->getChain(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1273 | InChain = SDValue(LD, 1); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1274 | return true; |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1275 | } |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | } |
| 1278 | |
| 1279 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1280 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 1281 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1282 | bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N, |
| 1283 | SDValue &Base, SDValue &Scale, |
| 1284 | SDValue &Index, SDValue &Disp) { |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1285 | X86ISelAddressMode AM; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support |
| 1288 | // segments. |
| 1289 | SDValue Copy = AM.Segment; |
| 1290 | SDValue T = CurDAG->getRegister(0, MVT::i32); |
| 1291 | AM.Segment = T; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1292 | if (MatchAddress(N, AM)) |
| 1293 | return false; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1294 | assert (T == AM.Segment); |
| 1295 | AM.Segment = Copy; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1296 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1297 | MVT VT = N.getValueType(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1298 | unsigned Complexity = 0; |
| 1299 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1300 | if (AM.Base.Reg.getNode()) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1301 | Complexity = 1; |
| 1302 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1303 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1304 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1305 | Complexity = 4; |
| 1306 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1307 | if (AM.IndexReg.getNode()) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1308 | Complexity++; |
| 1309 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1310 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 1312 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1313 | // a simple shift. |
| 1314 | if (AM.Scale > 1) |
Evan Cheng | 8c03fe4 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 1315 | Complexity++; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1316 | |
| 1317 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 1318 | // to a LEA. This is determined with some expermentation but is by no means |
| 1319 | // optimal (especially for code size consideration). LEA is nice because of |
| 1320 | // its three-address nature. Tweak the cost function again when we can run |
| 1321 | // convertToThreeAddress() at register allocation time. |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 1322 | if (AM.hasSymbolicDisplacement()) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1323 | // For X86-64, we should always use lea to materialize RIP relative |
| 1324 | // addresses. |
Evan Cheng | 953fa04 | 2006-12-05 22:03:40 +0000 | [diff] [blame] | 1325 | if (Subtarget->is64Bit()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1326 | Complexity = 4; |
| 1327 | else |
| 1328 | Complexity += 2; |
| 1329 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1330 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1331 | if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1332 | Complexity++; |
| 1333 | |
| 1334 | if (Complexity > 2) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1335 | SDValue Segment; |
| 1336 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1337 | return true; |
| 1338 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1342 | /// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes. |
| 1343 | bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base, |
| 1344 | SDValue &Scale, SDValue &Index, |
| 1345 | SDValue &Disp) { |
| 1346 | assert(Op.getOpcode() == X86ISD::TLSADDR); |
| 1347 | assert(N.getOpcode() == ISD::TargetGlobalTLSAddress); |
| 1348 | const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); |
| 1349 | |
| 1350 | X86ISelAddressMode AM; |
| 1351 | AM.GV = GA->getGlobal(); |
| 1352 | AM.Disp += GA->getOffset(); |
| 1353 | AM.Base.Reg = CurDAG->getRegister(0, N.getValueType()); |
Chris Lattner | ba8ef45 | 2009-06-26 21:18:37 +0000 | [diff] [blame] | 1354 | AM.SymbolFlags = GA->getTargetFlags(); |
| 1355 | |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1356 | if (N.getValueType() == MVT::i32) { |
| 1357 | AM.Scale = 1; |
| 1358 | AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32); |
| 1359 | } else { |
| 1360 | AM.IndexReg = CurDAG->getRegister(0, MVT::i64); |
| 1361 | } |
| 1362 | |
| 1363 | SDValue Segment; |
| 1364 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
| 1368 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1369 | bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N, |
| 1370 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1371 | SDValue &Index, SDValue &Disp, |
| 1372 | SDValue &Segment) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1373 | if (ISD::isNON_EXTLoad(N.getNode()) && |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1374 | N.hasOneUse() && |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 1375 | IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode())) |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1376 | return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
Dan Gohman | 8b74696 | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 1380 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 1381 | /// the global base register. Output instructions required to |
| 1382 | /// initialize the global base register, if necessary. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1383 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1384 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | 57c3dac | 2008-09-30 00:58:23 +0000 | [diff] [blame] | 1385 | MachineFunction *MF = CurBB->getParent(); |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 1386 | unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1387 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Evan Cheng | b245d92 | 2006-05-20 01:36:52 +0000 | [diff] [blame] | 1390 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 1391 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 1392 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 1393 | "Node doesn't have a token chain argument!"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1394 | return FindCallStartFromCall(Node->getOperand(0).getNode()); |
Evan Cheng | b245d92 | 2006-05-20 01:36:52 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1397 | SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) { |
| 1398 | SDValue Chain = Node->getOperand(0); |
| 1399 | SDValue In1 = Node->getOperand(1); |
| 1400 | SDValue In2L = Node->getOperand(2); |
| 1401 | SDValue In2H = Node->getOperand(3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1402 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
| 1403 | if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1404 | return NULL; |
Dale Johannesen | 1b54c7f | 2008-10-03 19:41:08 +0000 | [diff] [blame] | 1405 | SDValue LSI = Node->getOperand(4); // MemOperand |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1406 | const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain}; |
Rafael Espindola | e4d5d34 | 2009-03-27 15:45:05 +0000 | [diff] [blame] | 1407 | return CurDAG->getTargetNode(Opc, Node->getDebugLoc(), |
| 1408 | MVT::i32, MVT::i32, MVT::Other, Ops, |
Rafael Espindola | a0a4f07 | 2009-03-28 19:02:18 +0000 | [diff] [blame] | 1409 | array_lengthof(Ops)); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1410 | } |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1411 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1412 | SDNode *X86DAGToDAGISel::Select(SDValue N) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1413 | SDNode *Node = N.getNode(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1414 | MVT NVT = Node->getValueType(0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1415 | unsigned Opc, MOpc; |
| 1416 | unsigned Opcode = Node->getOpcode(); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1417 | DebugLoc dl = Node->getDebugLoc(); |
| 1418 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1419 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1420 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1421 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1422 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1423 | Indent += 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1424 | #endif |
| 1425 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1426 | if (Node->isMachineOpcode()) { |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1427 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1428 | DOUT << std::string(Indent-2, ' ') << "== "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1429 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1430 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1431 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1432 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1433 | return NULL; // Already selected. |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1434 | } |
Evan Cheng | 38262ca | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 1435 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1436 | switch (Opcode) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1437 | default: break; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1438 | case X86ISD::GlobalBaseReg: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1439 | return getGlobalBaseReg(); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1440 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1441 | case X86ISD::ATOMOR64_DAG: |
| 1442 | return SelectAtomic64(Node, X86::ATOMOR6432); |
| 1443 | case X86ISD::ATOMXOR64_DAG: |
| 1444 | return SelectAtomic64(Node, X86::ATOMXOR6432); |
| 1445 | case X86ISD::ATOMADD64_DAG: |
| 1446 | return SelectAtomic64(Node, X86::ATOMADD6432); |
| 1447 | case X86ISD::ATOMSUB64_DAG: |
| 1448 | return SelectAtomic64(Node, X86::ATOMSUB6432); |
| 1449 | case X86ISD::ATOMNAND64_DAG: |
| 1450 | return SelectAtomic64(Node, X86::ATOMNAND6432); |
| 1451 | case X86ISD::ATOMAND64_DAG: |
| 1452 | return SelectAtomic64(Node, X86::ATOMAND6432); |
Dale Johannesen | 880ae36 | 2008-10-03 22:25:52 +0000 | [diff] [blame] | 1453 | case X86ISD::ATOMSWAP64_DAG: |
| 1454 | return SelectAtomic64(Node, X86::ATOMSWAP6432); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1455 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1456 | case ISD::SMUL_LOHI: |
| 1457 | case ISD::UMUL_LOHI: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1458 | SDValue N0 = Node->getOperand(0); |
| 1459 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1460 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1461 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
| 1462 | if (!isSigned) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1463 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1464 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1465 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1466 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1467 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1468 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1469 | } |
| 1470 | else |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1471 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1472 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1473 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1474 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1475 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1476 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | unsigned LoReg, HiReg; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1480 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1481 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1482 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1483 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1484 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1485 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1488 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
| 1489 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1490 | // multiplty is commmutative |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1491 | if (!foldedLoad) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1492 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Evan Cheng | 7afa166 | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1493 | if (foldedLoad) |
| 1494 | std::swap(N0, N1); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1497 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1498 | N0, SDValue()).getValue(1); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1499 | |
| 1500 | if (foldedLoad) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1501 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 1502 | InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1503 | SDNode *CNode = |
Rafael Espindola | e4d5d34 | 2009-03-27 15:45:05 +0000 | [diff] [blame] | 1504 | CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, |
Rafael Espindola | a0a4f07 | 2009-03-28 19:02:18 +0000 | [diff] [blame] | 1505 | array_lengthof(Ops)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1506 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1507 | // Update the chain. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1508 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1509 | } else { |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1510 | InFlag = |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1511 | SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1514 | // Copy the low half of the result, if it is needed. |
| 1515 | if (!N.getValue(0).use_empty()) { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1516 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1517 | LoReg, NVT, InFlag); |
| 1518 | InFlag = Result.getValue(2); |
| 1519 | ReplaceUses(N.getValue(0), Result); |
| 1520 | #ifndef NDEBUG |
| 1521 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1522 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1523 | DOUT << "\n"; |
| 1524 | #endif |
Evan Cheng | f7ef26e | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1525 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1526 | // Copy the high half of the result, if it is needed. |
| 1527 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1528 | SDValue Result; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1529 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1530 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1531 | // Shift it down 8 bits. |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1532 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1533 | X86::AX, MVT::i16, InFlag); |
| 1534 | InFlag = Result.getValue(2); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1535 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16, |
| 1536 | Result, |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1537 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1538 | // Then truncate it down to i8. |
Dan Gohman | 3cd0aa3 | 2009-04-13 15:14:03 +0000 | [diff] [blame] | 1539 | SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1540 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1541 | MVT::i8, Result, SRIdx), 0); |
| 1542 | } else { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1543 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1544 | HiReg, NVT, InFlag); |
| 1545 | InFlag = Result.getValue(2); |
| 1546 | } |
| 1547 | ReplaceUses(N.getValue(1), Result); |
| 1548 | #ifndef NDEBUG |
| 1549 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1550 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1551 | DOUT << "\n"; |
| 1552 | #endif |
| 1553 | } |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1554 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1555 | #ifndef NDEBUG |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1556 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1557 | #endif |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1558 | |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1559 | return NULL; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1560 | } |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1561 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1562 | case ISD::SDIVREM: |
| 1563 | case ISD::UDIVREM: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1564 | SDValue N0 = Node->getOperand(0); |
| 1565 | SDValue N1 = Node->getOperand(1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1566 | |
| 1567 | bool isSigned = Opcode == ISD::SDIVREM; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1568 | if (!isSigned) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1569 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1570 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1571 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1572 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1573 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1574 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1575 | } |
| 1576 | else |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1577 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1578 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1579 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1580 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1581 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1582 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | unsigned LoReg, HiReg; |
| 1586 | unsigned ClrOpcode, SExtOpcode; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1587 | switch (NVT.getSimpleVT()) { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 1588 | default: LLVM_UNREACHABLE("Unsupported VT!"); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1589 | case MVT::i8: |
| 1590 | LoReg = X86::AL; HiReg = X86::AH; |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1591 | ClrOpcode = 0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1592 | SExtOpcode = X86::CBW; |
| 1593 | break; |
| 1594 | case MVT::i16: |
| 1595 | LoReg = X86::AX; HiReg = X86::DX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1596 | ClrOpcode = X86::MOV16r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1597 | SExtOpcode = X86::CWD; |
| 1598 | break; |
| 1599 | case MVT::i32: |
| 1600 | LoReg = X86::EAX; HiReg = X86::EDX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1601 | ClrOpcode = X86::MOV32r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1602 | SExtOpcode = X86::CDQ; |
| 1603 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1604 | case MVT::i64: |
| 1605 | LoReg = X86::RAX; HiReg = X86::RDX; |
| 1606 | ClrOpcode = X86::MOV64r0; |
| 1607 | SExtOpcode = X86::CQO; |
| 1608 | break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1611 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
| 1612 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 1ef4d8f | 2009-01-21 14:50:16 +0000 | [diff] [blame] | 1613 | bool signBitIsZero = CurDAG->SignBitIsZero(N0); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1614 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1615 | SDValue InFlag; |
Dan Gohman | 1ef4d8f | 2009-01-21 14:50:16 +0000 | [diff] [blame] | 1616 | if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1617 | // Special case for div8, just use a move with zero extension to AX to |
| 1618 | // clear the upper 8 bits (AH). |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1619 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain; |
| 1620 | if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) { |
| 1621 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) }; |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1622 | Move = |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1623 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16, |
Rafael Espindola | e4d5d34 | 2009-03-27 15:45:05 +0000 | [diff] [blame] | 1624 | MVT::Other, Ops, |
Rafael Espindola | a0a4f07 | 2009-03-28 19:02:18 +0000 | [diff] [blame] | 1625 | array_lengthof(Ops)), 0); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1626 | Chain = Move.getValue(1); |
| 1627 | ReplaceUses(N0.getValue(1), Chain); |
| 1628 | } else { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1629 | Move = |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1630 | SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1631 | Chain = CurDAG->getEntryNode(); |
| 1632 | } |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1633 | Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue()); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1634 | InFlag = Chain.getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1635 | } else { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1636 | InFlag = |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1637 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1638 | LoReg, N0, SDValue()).getValue(1); |
Dan Gohman | 1ef4d8f | 2009-01-21 14:50:16 +0000 | [diff] [blame] | 1639 | if (isSigned && !signBitIsZero) { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1640 | // Sign extend the low part into the high part. |
| 1641 | InFlag = |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1642 | SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1643 | } else { |
| 1644 | // Zero out the high part, effectively zero extending the input. |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1645 | SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), |
| 1646 | 0); |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1647 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1648 | ClrNode, InFlag).getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1649 | } |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | if (foldedLoad) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1653 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 1654 | InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1655 | SDNode *CNode = |
Rafael Espindola | e4d5d34 | 2009-03-27 15:45:05 +0000 | [diff] [blame] | 1656 | CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, |
Rafael Espindola | a0a4f07 | 2009-03-28 19:02:18 +0000 | [diff] [blame] | 1657 | array_lengthof(Ops)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1658 | InFlag = SDValue(CNode, 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1659 | // Update the chain. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1660 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1661 | } else { |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1662 | InFlag = |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1663 | SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1666 | // Copy the division (low) result, if it is needed. |
| 1667 | if (!N.getValue(0).use_empty()) { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1668 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1669 | LoReg, NVT, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1670 | InFlag = Result.getValue(2); |
| 1671 | ReplaceUses(N.getValue(0), Result); |
| 1672 | #ifndef NDEBUG |
| 1673 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1674 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1675 | DOUT << "\n"; |
| 1676 | #endif |
Evan Cheng | f7ef26e | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1677 | } |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1678 | // Copy the remainder (high) result, if it is needed. |
| 1679 | if (!N.getValue(1).use_empty()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1680 | SDValue Result; |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1681 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1682 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1683 | // Shift it down 8 bits. |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1684 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1685 | X86::AX, MVT::i16, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1686 | InFlag = Result.getValue(2); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1687 | Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16, |
| 1688 | Result, |
| 1689 | CurDAG->getTargetConstant(8, MVT::i8)), |
| 1690 | 0); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1691 | // Then truncate it down to i8. |
Dan Gohman | 3cd0aa3 | 2009-04-13 15:14:03 +0000 | [diff] [blame] | 1692 | SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1693 | Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl, |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1694 | MVT::i8, Result, SRIdx), 0); |
| 1695 | } else { |
Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1696 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1697 | HiReg, NVT, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1698 | InFlag = Result.getValue(2); |
| 1699 | } |
| 1700 | ReplaceUses(N.getValue(1), Result); |
| 1701 | #ifndef NDEBUG |
| 1702 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1703 | DEBUG(Result.getNode()->dump(CurDAG)); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1704 | DOUT << "\n"; |
| 1705 | #endif |
| 1706 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1707 | |
| 1708 | #ifndef NDEBUG |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1709 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1710 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1711 | |
| 1712 | return NULL; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1713 | } |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1714 | |
Evan Cheng | 851bc04 | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1715 | case ISD::DECLARE: { |
| 1716 | // Handle DECLARE nodes here because the second operand may have been |
| 1717 | // wrapped in X86ISD::Wrapper. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1718 | SDValue Chain = Node->getOperand(0); |
| 1719 | SDValue N1 = Node->getOperand(1); |
| 1720 | SDValue N2 = Node->getOperand(2); |
Evan Cheng | 6bb14ca | 2008-12-10 21:49:05 +0000 | [diff] [blame] | 1721 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1); |
Chris Lattner | 1823c92 | 2009-02-12 17:33:11 +0000 | [diff] [blame] | 1722 | |
| 1723 | // FIXME: We need to handle this for VLAs. |
| 1724 | if (!FINode) { |
| 1725 | ReplaceUses(N.getValue(0), Chain); |
| 1726 | return NULL; |
| 1727 | } |
| 1728 | |
Evan Cheng | fab8387 | 2008-06-18 02:48:27 +0000 | [diff] [blame] | 1729 | if (N2.getOpcode() == ISD::ADD && |
| 1730 | N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg) |
| 1731 | N2 = N2.getOperand(1); |
Chris Lattner | 1823c92 | 2009-02-12 17:33:11 +0000 | [diff] [blame] | 1732 | |
| 1733 | // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled |
| 1734 | // somehow, just ignore it. |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1735 | if (N2.getOpcode() != X86ISD::Wrapper && |
| 1736 | N2.getOpcode() != X86ISD::WrapperRIP) { |
Chris Lattner | 1823c92 | 2009-02-12 17:33:11 +0000 | [diff] [blame] | 1737 | ReplaceUses(N.getValue(0), Chain); |
| 1738 | return NULL; |
| 1739 | } |
Evan Cheng | f2accb5 | 2009-01-10 03:33:22 +0000 | [diff] [blame] | 1740 | GlobalAddressSDNode *GVNode = |
| 1741 | dyn_cast<GlobalAddressSDNode>(N2.getOperand(0)); |
Chris Lattner | 1823c92 | 2009-02-12 17:33:11 +0000 | [diff] [blame] | 1742 | if (GVNode == 0) { |
| 1743 | ReplaceUses(N.getValue(0), Chain); |
| 1744 | return NULL; |
| 1745 | } |
Evan Cheng | 6bb14ca | 2008-12-10 21:49:05 +0000 | [diff] [blame] | 1746 | SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(), |
| 1747 | TLI.getPointerTy()); |
| 1748 | SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(), |
| 1749 | TLI.getPointerTy()); |
| 1750 | SDValue Ops[] = { Tmp1, Tmp2, Chain }; |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1751 | return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl, |
Rafael Espindola | e4d5d34 | 2009-03-27 15:45:05 +0000 | [diff] [blame] | 1752 | MVT::Other, Ops, |
Rafael Espindola | a0a4f07 | 2009-03-28 19:02:18 +0000 | [diff] [blame] | 1753 | array_lengthof(Ops)); |
Evan Cheng | 851bc04 | 2008-06-17 02:01:22 +0000 | [diff] [blame] | 1754 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1757 | SDNode *ResNode = SelectCode(N); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1758 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1759 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1760 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1761 | if (ResNode == NULL || ResNode == N.getNode()) |
| 1762 | DEBUG(N.getNode()->dump(CurDAG)); |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1763 | else |
| 1764 | DEBUG(ResNode->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1765 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1766 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1767 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1768 | |
| 1769 | return ResNode; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1772 | bool X86DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1773 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1774 | std::vector<SDValue> &OutOps) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1775 | SDValue Op0, Op1, Op2, Op3, Op4; |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1776 | switch (ConstraintCode) { |
| 1777 | case 'o': // offsetable ?? |
| 1778 | case 'v': // not offsetable ?? |
| 1779 | default: return true; |
| 1780 | case 'm': // memory |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1781 | if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4)) |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1782 | return true; |
| 1783 | break; |
| 1784 | } |
| 1785 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1786 | OutOps.push_back(Op0); |
| 1787 | OutOps.push_back(Op1); |
| 1788 | OutOps.push_back(Op2); |
| 1789 | OutOps.push_back(Op3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1790 | OutOps.push_back(Op4); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1791 | return false; |
| 1792 | } |
| 1793 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1794 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1795 | /// X86-specific DAG, ready for instruction scheduling. |
| 1796 | /// |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 1797 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, |
| 1798 | llvm::CodeGenOpt::Level OptLevel) { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 1799 | return new X86DAGToDAGISel(TM, OptLevel); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1800 | } |