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 | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 18 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 19 | #include "X86RegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 20 | #include "X86Subtarget.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 21 | #include "X86TargetMachine.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Chris Lattner | 420736d | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7aa8a45 | 2007-01-12 23:22:14 +0000 | [diff] [blame] | 25 | #include "llvm/Type.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | b7a75a5 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | cdda25d | 2008-04-25 08:22:20 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
| 40 | using namespace llvm; |
| 41 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 42 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 43 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // Pattern Matcher Implementation |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 49 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 50 | /// SDValue's instead of register numbers for the leaves of the matched |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 51 | /// tree. |
| 52 | struct X86ISelAddressMode { |
| 53 | enum { |
| 54 | RegBase, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 55 | FrameIndexBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 56 | } BaseType; |
| 57 | |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 58 | // This is really a union, discriminated by BaseType! |
| 59 | SDValue Base_Reg; |
| 60 | int Base_FrameIndex; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 61 | |
| 62 | unsigned Scale; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 63 | SDValue IndexReg; |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 64 | int32_t Disp; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 65 | SDValue Segment; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 66 | const GlobalValue *GV; |
| 67 | const Constant *CP; |
| 68 | const BlockAddress *BlockAddr; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 69 | const char *ES; |
| 70 | int JT; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 71 | unsigned Align; // CP alignment. |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 72 | unsigned char SymbolFlags; // X86II::MO_* |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 73 | |
| 74 | X86ISelAddressMode() |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 75 | : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0), |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 76 | Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0), |
Dan Gohman | 79b765d | 2009-08-25 17:47:44 +0000 | [diff] [blame] | 77 | SymbolFlags(X86II::MO_NO_FLAG) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 78 | } |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 79 | |
| 80 | bool hasSymbolicDisplacement() const { |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 81 | return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0; |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 83 | |
| 84 | bool hasBaseOrIndexReg() const { |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 85 | return IndexReg.getNode() != 0 || Base_Reg.getNode() != 0; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /// isRIPRelative - Return true if this addressing mode is already RIP |
| 89 | /// relative. |
| 90 | bool isRIPRelative() const { |
| 91 | if (BaseType != RegBase) return false; |
| 92 | if (RegisterSDNode *RegNode = |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 93 | dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode())) |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 94 | return RegNode->getReg() == X86::RIP; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | void setBaseReg(SDValue Reg) { |
| 99 | BaseType = RegBase; |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 100 | Base_Reg = Reg; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 101 | } |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 102 | |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 103 | void dump() { |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 104 | dbgs() << "X86ISelAddressMode " << this << '\n'; |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 105 | dbgs() << "Base_Reg "; |
| 106 | if (Base_Reg.getNode() != 0) |
| 107 | Base_Reg.getNode()->dump(); |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 108 | else |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 109 | dbgs() << "nul"; |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 110 | dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n' |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 111 | << " Scale" << Scale << '\n' |
| 112 | << "IndexReg "; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 113 | if (IndexReg.getNode() != 0) |
| 114 | IndexReg.getNode()->dump(); |
| 115 | else |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 116 | dbgs() << "nul"; |
| 117 | dbgs() << " Disp " << Disp << '\n' |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 118 | << "GV "; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 119 | if (GV) |
| 120 | GV->dump(); |
| 121 | else |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 122 | dbgs() << "nul"; |
| 123 | dbgs() << " CP "; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 124 | if (CP) |
| 125 | CP->dump(); |
| 126 | else |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 127 | dbgs() << "nul"; |
| 128 | dbgs() << '\n' |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 129 | << "ES "; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 130 | if (ES) |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 131 | dbgs() << ES; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 132 | else |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 133 | dbgs() << "nul"; |
| 134 | dbgs() << " JT" << JT << " Align" << Align << '\n'; |
Dale Johannesen | 50dd1d0 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 136 | }; |
| 137 | } |
| 138 | |
| 139 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 140 | //===--------------------------------------------------------------------===// |
| 141 | /// ISel - X86 specific code to select X86 machine instructions for |
| 142 | /// SelectionDAG operations. |
| 143 | /// |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 144 | class X86DAGToDAGISel : public SelectionDAGISel { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 145 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 146 | /// X86-specific SelectionDAG. |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 147 | const X86TargetLowering &X86Lowering; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 148 | |
| 149 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 150 | /// make the right decision when generating code for different targets. |
| 151 | const X86Subtarget *Subtarget; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 152 | |
Evan Cheng | b7a75a5 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 153 | /// OptForSize - If true, selector should try to optimize for code size |
| 154 | /// instead of performance. |
| 155 | bool OptForSize; |
| 156 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 157 | public: |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 158 | explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel) |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 159 | : SelectionDAGISel(tm, OptLevel), |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 160 | X86Lowering(*tm.getTargetLowering()), |
| 161 | Subtarget(&tm.getSubtarget<X86Subtarget>()), |
Devang Patel | 4ae641f | 2008-10-01 23:18:38 +0000 | [diff] [blame] | 162 | OptForSize(false) {} |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 163 | |
| 164 | virtual const char *getPassName() const { |
| 165 | return "X86 DAG->DAG Instruction Selection"; |
| 166 | } |
| 167 | |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 168 | virtual void EmitFunctionEntryCode(); |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 169 | |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 170 | virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const; |
| 171 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 172 | virtual void PreprocessISelDAG(); |
| 173 | |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 174 | inline bool immSext8(SDNode *N) const { |
| 175 | return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue()); |
| 176 | } |
| 177 | |
| 178 | // i64immSExt32 predicate - True if the 64-bit immediate fits in a 32-bit |
| 179 | // sign extended field. |
| 180 | inline bool i64immSExt32(SDNode *N) const { |
| 181 | uint64_t v = cast<ConstantSDNode>(N)->getZExtValue(); |
| 182 | return (int64_t)v == (int32_t)v; |
| 183 | } |
| 184 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 185 | // Include the pieces autogenerated from the target description. |
| 186 | #include "X86GenDAGISel.inc" |
| 187 | |
| 188 | private: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 189 | SDNode *Select(SDNode *N); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 190 | SDNode *SelectAtomic64(SDNode *Node, unsigned Opc); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 191 | SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 193 | bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM); |
| 194 | bool MatchLoad(SDValue N, X86ISelAddressMode &AM); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 195 | bool MatchWrapper(SDValue N, X86ISelAddressMode &AM); |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 196 | bool MatchAddress(SDValue N, X86ISelAddressMode &AM); |
| 197 | bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM, |
| 198 | unsigned Depth); |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 199 | bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM); |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 200 | bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 201 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 202 | SDValue &Segment); |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 203 | bool SelectLEAAddr(SDValue N, SDValue &Base, |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 204 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 205 | SDValue &Segment); |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 206 | bool SelectTLSADDRAddr(SDValue N, SDValue &Base, |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 207 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 208 | SDValue &Segment); |
Chris Lattner | e60f7b4 | 2010-03-01 22:51:11 +0000 | [diff] [blame] | 209 | bool SelectScalarSSELoad(SDNode *Root, SDValue N, |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 210 | SDValue &Base, SDValue &Scale, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | SDValue &Index, SDValue &Disp, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 212 | SDValue &Segment, |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 213 | SDValue &NodeWithChain); |
| 214 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 215 | bool TryFoldLoad(SDNode *P, SDValue N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 216 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 217 | SDValue &Index, SDValue &Disp, |
| 218 | SDValue &Segment); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 219 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 220 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 221 | /// inline asm expressions. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 222 | virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 223 | char ConstraintCode, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 224 | std::vector<SDValue> &OutOps); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 225 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 226 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 227 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 228 | inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, |
| 229 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 230 | SDValue &Disp, SDValue &Segment) { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 231 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 232 | CurDAG->getTargetFrameIndex(AM.Base_FrameIndex, TLI.getPointerTy()) : |
| 233 | AM.Base_Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 234 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 235 | Index = AM.IndexReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 236 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 237 | // is 32-bit. |
| 238 | if (AM.GV) |
Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 239 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, DebugLoc(), |
| 240 | MVT::i32, AM.Disp, |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 241 | AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 242 | else if (AM.CP) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 243 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 244 | AM.Align, AM.Disp, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 245 | else if (AM.ES) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 246 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 247 | else if (AM.JT != -1) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 248 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags); |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 249 | else if (AM.BlockAddr) |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 250 | Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32, |
| 251 | true, AM.SymbolFlags); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 252 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 253 | Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 254 | |
| 255 | if (AM.Segment.getNode()) |
| 256 | Segment = AM.Segment; |
| 257 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 258 | Segment = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 261 | /// getI8Imm - Return a target constant with the specified value, of type |
| 262 | /// i8. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 263 | inline SDValue getI8Imm(unsigned Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 264 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 267 | /// getI32Imm - Return a target constant with the specified value, of type |
| 268 | /// i32. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 269 | inline SDValue getI32Imm(unsigned Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 270 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 271 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 272 | |
Dan Gohman | 8b74696 | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 273 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 274 | /// the global base register. Output instructions required to |
| 275 | /// initialize the global base register, if necessary. |
| 276 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 277 | SDNode *getGlobalBaseReg(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 278 | |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 279 | /// getTargetMachine - Return a reference to the TargetMachine, casted |
| 280 | /// to the target-specific type. |
| 281 | const X86TargetMachine &getTargetMachine() { |
| 282 | return static_cast<const X86TargetMachine &>(TM); |
| 283 | } |
| 284 | |
| 285 | /// getInstrInfo - Return a reference to the TargetInstrInfo, casted |
| 286 | /// to the target-specific type. |
| 287 | const X86InstrInfo *getInstrInfo() { |
| 288 | return getTargetMachine().getInstrInfo(); |
| 289 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 290 | }; |
| 291 | } |
| 292 | |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 293 | |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 294 | bool |
| 295 | X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const { |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 296 | if (OptLevel == CodeGenOpt::None) return false; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 297 | |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 298 | if (!N.hasOneUse()) |
| 299 | return false; |
| 300 | |
| 301 | if (N.getOpcode() != ISD::LOAD) |
| 302 | return true; |
| 303 | |
| 304 | // If N is a load, do additional profitability checks. |
| 305 | if (U == Root) { |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 306 | switch (U->getOpcode()) { |
| 307 | default: break; |
Dan Gohman | 9ef51c8 | 2010-01-04 20:51:50 +0000 | [diff] [blame] | 308 | case X86ISD::ADD: |
| 309 | case X86ISD::SUB: |
| 310 | case X86ISD::AND: |
| 311 | case X86ISD::XOR: |
| 312 | case X86ISD::OR: |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 313 | case ISD::ADD: |
| 314 | case ISD::ADDC: |
| 315 | case ISD::ADDE: |
| 316 | case ISD::AND: |
| 317 | case ISD::OR: |
| 318 | case ISD::XOR: { |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 319 | SDValue Op1 = U->getOperand(1); |
| 320 | |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 321 | // If the other operand is a 8-bit immediate we should fold the immediate |
| 322 | // instead. This reduces code size. |
| 323 | // e.g. |
| 324 | // movl 4(%esp), %eax |
| 325 | // addl $4, %eax |
| 326 | // vs. |
| 327 | // movl $4, %eax |
| 328 | // addl 4(%esp), %eax |
| 329 | // The former is 2 bytes shorter. In case where the increment is 1, then |
| 330 | // the saving can be 4 bytes (by using incl %eax). |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 331 | if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1)) |
Dan Gohman | 9a49d31 | 2009-03-14 02:07:16 +0000 | [diff] [blame] | 332 | if (Imm->getAPIntValue().isSignedIntN(8)) |
| 333 | return false; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 334 | |
| 335 | // If the other operand is a TLS address, we should fold it instead. |
| 336 | // This produces |
| 337 | // movl %gs:0, %eax |
| 338 | // leal i@NTPOFF(%eax), %eax |
| 339 | // instead of |
| 340 | // movl $i@NTPOFF, %eax |
| 341 | // addl %gs:0, %eax |
| 342 | // if the block also has an access to a second TLS address this will save |
| 343 | // a load. |
| 344 | // FIXME: This is probably also true for non TLS addresses. |
| 345 | if (Op1.getOpcode() == X86ISD::Wrapper) { |
| 346 | SDValue Val = Op1.getOperand(0); |
| 347 | if (Val.getOpcode() == ISD::TargetGlobalTLSAddress) |
| 348 | return false; |
| 349 | } |
Evan Cheng | 884c70c | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
Evan Cheng | 014bf21 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 357 | /// MoveBelowCallOrigChain - Replace the original chain operand of the call with |
| 358 | /// load's chain operand and move load below the call's chain operand. |
| 359 | static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load, |
| 360 | SDValue Call, SDValue OrigChain) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 361 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 362 | SDValue Chain = OrigChain.getOperand(0); |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 363 | if (Chain.getNode() == Load.getNode()) |
| 364 | Ops.push_back(Load.getOperand(0)); |
| 365 | else { |
| 366 | assert(Chain.getOpcode() == ISD::TokenFactor && |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 367 | "Unexpected chain operand"); |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 368 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 369 | if (Chain.getOperand(i).getNode() == Load.getNode()) |
| 370 | Ops.push_back(Load.getOperand(0)); |
| 371 | else |
| 372 | Ops.push_back(Chain.getOperand(i)); |
| 373 | SDValue NewChain = |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 374 | CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 375 | MVT::Other, &Ops[0], Ops.size()); |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 376 | Ops.clear(); |
| 377 | Ops.push_back(NewChain); |
| 378 | } |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 379 | for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i) |
| 380 | Ops.push_back(OrigChain.getOperand(i)); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 381 | CurDAG->UpdateNodeOperands(OrigChain.getNode(), &Ops[0], Ops.size()); |
| 382 | CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0), |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 383 | Load.getOperand(1), Load.getOperand(2)); |
| 384 | Ops.clear(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 385 | Ops.push_back(SDValue(Load.getNode(), 1)); |
| 386 | for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i) |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 387 | Ops.push_back(Call.getOperand(i)); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 388 | CurDAG->UpdateNodeOperands(Call.getNode(), &Ops[0], Ops.size()); |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /// isCalleeLoad - Return true if call address is a load and it can be |
| 392 | /// moved below CALLSEQ_START and the chains leading up to the call. |
| 393 | /// Return the CALLSEQ_START by reference as a second output. |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 394 | /// In the case of a tail call, there isn't a callseq node between the call |
| 395 | /// chain and the load. |
| 396 | static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 397 | if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse()) |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 398 | return false; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 399 | LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode()); |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 400 | if (!LD || |
| 401 | LD->isVolatile() || |
| 402 | LD->getAddressingMode() != ISD::UNINDEXED || |
| 403 | LD->getExtensionType() != ISD::NON_EXTLOAD) |
| 404 | return false; |
| 405 | |
Chris Lattner | 08bad54 | 2010-09-21 03:37:00 +0000 | [diff] [blame] | 406 | // FIXME: Calls can't fold loads through segment registers yet. |
Chris Lattner | 701cd62 | 2010-09-21 19:41:58 +0000 | [diff] [blame] | 407 | if (LD->getPointerInfo().getAddrSpace() > 255) |
| 408 | return false; |
Chris Lattner | 08bad54 | 2010-09-21 03:37:00 +0000 | [diff] [blame] | 409 | |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 410 | // Now let's find the callseq_start. |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 411 | while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) { |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 412 | if (!Chain.hasOneUse()) |
| 413 | return false; |
| 414 | Chain = Chain.getOperand(0); |
| 415 | } |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 416 | |
| 417 | if (!Chain.getNumOperands()) |
| 418 | return false; |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 419 | if (Chain.getOperand(0).getNode() == Callee.getNode()) |
| 420 | return true; |
| 421 | if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor && |
Dan Gohman | 1e038a8 | 2009-09-15 01:22:01 +0000 | [diff] [blame] | 422 | Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) && |
| 423 | Callee.getValue(1).hasOneUse()) |
Evan Cheng | 5b2e589 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 424 | return true; |
| 425 | return false; |
Evan Cheng | ab6c3bb | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 428 | void X86DAGToDAGISel::PreprocessISelDAG() { |
Chris Lattner | 97d8534 | 2010-03-04 01:43:43 +0000 | [diff] [blame] | 429 | // OptForSize is used in pattern predicates that isel is matching. |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 430 | OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize); |
| 431 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 432 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 433 | E = CurDAG->allnodes_end(); I != E; ) { |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 434 | SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 435 | |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 436 | if (OptLevel != CodeGenOpt::None && |
| 437 | (N->getOpcode() == X86ISD::CALL || |
| 438 | N->getOpcode() == X86ISD::TC_RETURN)) { |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 439 | /// Also try moving call address load from outside callseq_start to just |
| 440 | /// before the call to allow it to be folded. |
| 441 | /// |
| 442 | /// [Load chain] |
| 443 | /// ^ |
| 444 | /// | |
| 445 | /// [Load] |
| 446 | /// ^ ^ |
| 447 | /// | | |
| 448 | /// / \-- |
| 449 | /// / | |
| 450 | ///[CALLSEQ_START] | |
| 451 | /// ^ | |
| 452 | /// | | |
| 453 | /// [LOAD/C2Reg] | |
| 454 | /// | | |
| 455 | /// \ / |
| 456 | /// \ / |
| 457 | /// [CALL] |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 458 | bool HasCallSeq = N->getOpcode() == X86ISD::CALL; |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 459 | SDValue Chain = N->getOperand(0); |
| 460 | SDValue Load = N->getOperand(1); |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 461 | if (!isCalleeLoad(Load, Chain, HasCallSeq)) |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 462 | continue; |
Evan Cheng | f48ef03 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 463 | MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain); |
Chris Lattner | fb444af | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 464 | ++NumLoadMoved; |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | // Lower fpround and fpextend nodes that target the FP stack to be store and |
| 469 | // load to the stack. This is a gross hack. We would like to simply mark |
| 470 | // these as being illegal, but when we do that, legalize produces these when |
| 471 | // it expands calls, then expands these in the same legalize pass. We would |
| 472 | // like dag combine to be able to hack on these between the call expansion |
| 473 | // and the node legalization. As such this pass basically does "really |
| 474 | // late" legalization of these inline with the X86 isel pass. |
| 475 | // FIXME: This should only happen when not compiled with -O0. |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 476 | if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) |
| 477 | continue; |
| 478 | |
| 479 | // If the source and destination are SSE registers, then this is a legal |
| 480 | // conversion that should not be lowered. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 481 | EVT SrcVT = N->getOperand(0).getValueType(); |
| 482 | EVT DstVT = N->getValueType(0); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 483 | bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT); |
| 484 | bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT); |
| 485 | if (SrcIsSSE && DstIsSSE) |
| 486 | continue; |
| 487 | |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 488 | if (!SrcIsSSE && !DstIsSSE) { |
| 489 | // If this is an FPStack extension, it is a noop. |
| 490 | if (N->getOpcode() == ISD::FP_EXTEND) |
| 491 | continue; |
| 492 | // If this is a value-preserving FPStack truncation, it is a noop. |
| 493 | if (N->getConstantOperandVal(1)) |
| 494 | continue; |
| 495 | } |
| 496 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 497 | // Here we could have an FP stack truncation or an FPStack <-> SSE convert. |
| 498 | // FPStack has extload and truncstore. SSE can fold direct loads into other |
| 499 | // operations. Based on this, decide what we want to do. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 500 | EVT MemVT; |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 501 | if (N->getOpcode() == ISD::FP_ROUND) |
| 502 | MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. |
| 503 | else |
| 504 | MemVT = SrcIsSSE ? SrcVT : DstVT; |
| 505 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 506 | SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 507 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 508 | |
| 509 | // 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] | 510 | SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 511 | N->getOperand(0), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 512 | MemTmp, MachinePointerInfo(), MemVT, |
David Greene | db8d989 | 2010-02-15 16:57:43 +0000 | [diff] [blame] | 513 | false, false, 0); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 514 | SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, dl, Store, MemTmp, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 515 | MachinePointerInfo(), |
| 516 | MemVT, false, false, 0); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 517 | |
| 518 | // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the |
| 519 | // extload we created. This will cause general havok on the dag because |
| 520 | // anything below the conversion could be folded into other existing nodes. |
| 521 | // To avoid invalidating 'I', back it up to the convert node. |
| 522 | --I; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 523 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 524 | |
| 525 | // Now that we did that, the node is dead. Increment the iterator to the |
| 526 | // next node to process, then delete N. |
| 527 | ++I; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 528 | CurDAG->DeleteNode(N); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 532 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 533 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 534 | /// the main function. |
| 535 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 536 | MachineFrameInfo *MFI) { |
| 537 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 538 | if (Subtarget->isTargetCygMing()) |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 539 | BuildMI(BB, DebugLoc(), |
Dale Johannesen | 8d13f8f | 2009-02-13 02:33:27 +0000 | [diff] [blame] | 540 | TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 543 | void X86DAGToDAGISel::EmitFunctionEntryCode() { |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 544 | // If this is main, emit special code for main. |
Dan Gohman | 6465265 | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 545 | if (const Function *Fn = MF->getFunction()) |
| 546 | if (Fn->hasExternalLinkage() && Fn->getName() == "main") |
| 547 | EmitSpecialCodeForMain(MF->begin(), MF->getFrameInfo()); |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 550 | |
| 551 | bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N, |
| 552 | X86ISelAddressMode &AM) { |
| 553 | assert(N.getOpcode() == X86ISD::SegmentBaseAddress); |
| 554 | SDValue Segment = N.getOperand(0); |
| 555 | |
| 556 | if (AM.Segment.getNode() == 0) { |
| 557 | AM.Segment = Segment; |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) { |
| 565 | // This optimization is valid because the GNU TLS model defines that |
| 566 | // gs:0 (or fs:0 on X86-64) contains its own address. |
| 567 | // For more information see http://people.redhat.com/drepper/tls.pdf |
| 568 | |
| 569 | SDValue Address = N.getOperand(1); |
| 570 | if (Address.getOpcode() == X86ISD::SegmentBaseAddress && |
Chris Lattner | 08bad54 | 2010-09-21 03:37:00 +0000 | [diff] [blame] | 571 | !MatchSegmentBaseAddress(Address, AM)) |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 572 | return false; |
| 573 | |
| 574 | return true; |
| 575 | } |
| 576 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 577 | /// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes |
| 578 | /// into an addressing mode. These wrap things that will resolve down into a |
| 579 | /// symbol reference. If no match is possible, this returns true, otherwise it |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 580 | /// returns false. |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 581 | bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 582 | // If the addressing mode already has a symbol as the displacement, we can |
| 583 | // never match another symbol. |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 584 | if (AM.hasSymbolicDisplacement()) |
| 585 | return true; |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 586 | |
| 587 | SDValue N0 = N.getOperand(0); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 588 | CodeModel::Model M = TM.getCodeModel(); |
| 589 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 590 | // Handle X86-64 rip-relative addresses. We check this before checking direct |
| 591 | // folding because RIP is preferable to non-RIP accesses. |
| 592 | if (Subtarget->is64Bit() && |
| 593 | // Under X86-64 non-small code model, GV (and friends) are 64-bits, so |
| 594 | // they cannot be folded into immediate fields. |
| 595 | // FIXME: This can be improved for kernel and other models? |
Anton Korobeynikov | 25f1aa0 | 2009-08-21 15:41:56 +0000 | [diff] [blame] | 596 | (M == CodeModel::Small || M == CodeModel::Kernel) && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 597 | // Base and index reg must be 0 in order to use %rip as base and lowering |
| 598 | // must allow RIP. |
| 599 | !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 600 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 601 | int64_t Offset = AM.Disp + G->getOffset(); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 602 | if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 603 | AM.GV = G->getGlobal(); |
| 604 | AM.Disp = Offset; |
Chris Lattner | b8afeb9 | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 605 | AM.SymbolFlags = G->getTargetFlags(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 606 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
| 607 | int64_t Offset = AM.Disp + CP->getOffset(); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 608 | if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true; |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 609 | AM.CP = CP->getConstVal(); |
| 610 | AM.Align = CP->getAlignment(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 611 | AM.Disp = Offset; |
Chris Lattner | 0b0deab | 2009-06-26 05:56:49 +0000 | [diff] [blame] | 612 | AM.SymbolFlags = CP->getTargetFlags(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 613 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 614 | AM.ES = S->getSymbol(); |
| 615 | AM.SymbolFlags = S->getTargetFlags(); |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 616 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 617 | AM.JT = J->getIndex(); |
| 618 | AM.SymbolFlags = J->getTargetFlags(); |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 619 | } else { |
| 620 | AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress(); |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 621 | AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags(); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 622 | } |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 624 | if (N.getOpcode() == X86ISD::WrapperRIP) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 625 | AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64)); |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 626 | return false; |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // Handle the case when globals fit in our immediate field: This is true for |
| 630 | // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit |
| 631 | // mode, this results in a non-RIP-relative computation. |
| 632 | if (!Subtarget->is64Bit() || |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 633 | ((M == CodeModel::Small || M == CodeModel::Kernel) && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 634 | TM.getRelocationModel() == Reloc::Static)) { |
| 635 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 636 | AM.GV = G->getGlobal(); |
| 637 | AM.Disp += G->getOffset(); |
| 638 | AM.SymbolFlags = G->getTargetFlags(); |
| 639 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
| 640 | AM.CP = CP->getConstVal(); |
| 641 | AM.Align = CP->getAlignment(); |
| 642 | AM.Disp += CP->getOffset(); |
| 643 | AM.SymbolFlags = CP->getTargetFlags(); |
| 644 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 645 | AM.ES = S->getSymbol(); |
| 646 | AM.SymbolFlags = S->getTargetFlags(); |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 647 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 648 | AM.JT = J->getIndex(); |
| 649 | AM.SymbolFlags = J->getTargetFlags(); |
Chris Lattner | 43f44aa | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 650 | } else { |
| 651 | AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress(); |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 652 | AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags(); |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 653 | } |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | |
| 657 | return true; |
| 658 | } |
| 659 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 660 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 661 | /// 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] | 662 | /// addressing mode. |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 663 | bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) { |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 664 | if (MatchAddressRecursively(N, AM, 0)) |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 665 | return true; |
| 666 | |
| 667 | // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has |
| 668 | // a smaller encoding and avoids a scaled-index. |
| 669 | if (AM.Scale == 2 && |
| 670 | AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 671 | AM.Base_Reg.getNode() == 0) { |
| 672 | AM.Base_Reg = AM.IndexReg; |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 673 | AM.Scale = 1; |
| 674 | } |
| 675 | |
Dan Gohman | ef74e9b | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 676 | // Post-processing: Convert foo to foo(%rip), even in non-PIC mode, |
| 677 | // because it has a smaller encoding. |
| 678 | // TODO: Which other code models can use this? |
| 679 | if (TM.getCodeModel() == CodeModel::Small && |
| 680 | Subtarget->is64Bit() && |
| 681 | AM.Scale == 1 && |
| 682 | AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 683 | AM.Base_Reg.getNode() == 0 && |
Dan Gohman | ef74e9b | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 684 | AM.IndexReg.getNode() == 0 && |
Dan Gohman | 79b765d | 2009-08-25 17:47:44 +0000 | [diff] [blame] | 685 | AM.SymbolFlags == X86II::MO_NO_FLAG && |
Dan Gohman | ef74e9b | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 686 | AM.hasSymbolicDisplacement()) |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 687 | AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64); |
Dan Gohman | ef74e9b | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 688 | |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | |
Chris Lattner | d613942 | 2010-04-20 23:18:40 +0000 | [diff] [blame] | 692 | /// isLogicallyAddWithConstant - Return true if this node is semantically an |
| 693 | /// add of a value with a constantint. |
| 694 | static bool isLogicallyAddWithConstant(SDValue V, SelectionDAG *CurDAG) { |
| 695 | // Check for (add x, Cst) |
| 696 | if (V->getOpcode() == ISD::ADD) |
| 697 | return isa<ConstantSDNode>(V->getOperand(1)); |
| 698 | |
| 699 | // Check for (or x, Cst), where Cst & x == 0. |
| 700 | if (V->getOpcode() != ISD::OR || |
| 701 | !isa<ConstantSDNode>(V->getOperand(1))) |
| 702 | return false; |
| 703 | |
| 704 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
| 705 | ConstantSDNode *CN = cast<ConstantSDNode>(V->getOperand(1)); |
| 706 | |
| 707 | // Check to see if the LHS & C is zero. |
| 708 | return CurDAG->MaskedValueIsZero(V->getOperand(0), CN->getAPIntValue()); |
| 709 | } |
| 710 | |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 711 | bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM, |
| 712 | unsigned Depth) { |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 713 | bool is64Bit = Subtarget->is64Bit(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 714 | DebugLoc dl = N.getDebugLoc(); |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 715 | DEBUG({ |
David Greene | d7f4f24 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 716 | dbgs() << "MatchAddress: "; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 717 | AM.dump(); |
| 718 | }); |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 719 | // Limit recursion. |
| 720 | if (Depth > 5) |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 721 | return MatchAddressBase(N, AM); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 722 | |
| 723 | CodeModel::Model M = TM.getCodeModel(); |
| 724 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 725 | // If this is already a %rip relative address, we can only merge immediates |
| 726 | // 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] | 727 | // RIP relative addressing: %rip + 32-bit displacement! |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 728 | if (AM.isRIPRelative()) { |
| 729 | // FIXME: JumpTable and ExternalSymbol address currently don't like |
| 730 | // displacements. It isn't very important, but this should be fixed for |
| 731 | // consistency. |
| 732 | if (!AM.ES && AM.JT != -1) return true; |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 734 | if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) { |
| 735 | int64_t Val = AM.Disp + Cst->getSExtValue(); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 736 | if (X86::isOffsetSuitableForCodeModel(Val, M, |
| 737 | AM.hasSymbolicDisplacement())) { |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 738 | AM.Disp = Val; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 745 | switch (N.getOpcode()) { |
| 746 | default: break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 747 | case ISD::Constant: { |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 748 | uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 749 | if (!is64Bit || |
| 750 | X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M, |
| 751 | AM.hasSymbolicDisplacement())) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 752 | AM.Disp += Val; |
| 753 | return false; |
| 754 | } |
| 755 | break; |
| 756 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 757 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 758 | case X86ISD::SegmentBaseAddress: |
| 759 | if (!MatchSegmentBaseAddress(N, AM)) |
| 760 | return false; |
| 761 | break; |
| 762 | |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 763 | case X86ISD::Wrapper: |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 764 | case X86ISD::WrapperRIP: |
Rafael Espindola | 49a168d | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 765 | if (!MatchWrapper(N, AM)) |
| 766 | return false; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 767 | break; |
| 768 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 769 | case ISD::LOAD: |
| 770 | if (!MatchLoad(N, AM)) |
| 771 | return false; |
| 772 | break; |
| 773 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 774 | case ISD::FrameIndex: |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 775 | if (AM.BaseType == X86ISelAddressMode::RegBase |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 776 | && AM.Base_Reg.getNode() == 0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 777 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 778 | AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 779 | return false; |
| 780 | } |
| 781 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 782 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 783 | case ISD::SHL: |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 784 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 785 | break; |
| 786 | |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 787 | if (ConstantSDNode |
| 788 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 789 | unsigned Val = CN->getZExtValue(); |
Dan Gohman | 41d0b9d | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 790 | // Note that we handle x<<1 as (,x,2) rather than (x,x) here so |
| 791 | // that the base operand remains free for further matching. If |
| 792 | // the base doesn't end up getting used, a post-processing step |
| 793 | // in MatchAddress turns (,x,2) into (x,x), which is cheaper. |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 794 | if (Val == 1 || Val == 2 || Val == 3) { |
| 795 | AM.Scale = 1 << Val; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 796 | SDValue ShVal = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 798 | // Okay, we know that we have a scale by now. However, if the scaled |
| 799 | // value is an add of something and a constant, we can fold the |
| 800 | // constant into the disp field here. |
Chris Lattner | d613942 | 2010-04-20 23:18:40 +0000 | [diff] [blame] | 801 | if (isLogicallyAddWithConstant(ShVal, CurDAG)) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 802 | AM.IndexReg = ShVal.getNode()->getOperand(0); |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 803 | ConstantSDNode *AddVal = |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 804 | cast<ConstantSDNode>(ShVal.getNode()->getOperand(1)); |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 805 | uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 806 | if (!is64Bit || |
| 807 | X86::isOffsetSuitableForCodeModel(Disp, M, |
| 808 | AM.hasSymbolicDisplacement())) |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 809 | AM.Disp = Disp; |
| 810 | else |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 811 | AM.IndexReg = ShVal; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 812 | } else { |
| 813 | AM.IndexReg = ShVal; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 815 | return false; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 816 | } |
| 817 | break; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 818 | } |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 819 | |
Dan Gohman | 8368805 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 820 | case ISD::SMUL_LOHI: |
| 821 | case ISD::UMUL_LOHI: |
| 822 | // 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] | 823 | if (N.getResNo() != 0) break; |
Dan Gohman | 8368805 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 824 | // FALL THROUGH |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 825 | case ISD::MUL: |
Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 826 | case X86ISD::MUL_IMM: |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 827 | // X*[3,5,9] -> X+X*[2,4,8] |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 828 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 829 | AM.Base_Reg.getNode() == 0 && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 830 | AM.IndexReg.getNode() == 0) { |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 831 | if (ConstantSDNode |
| 832 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 833 | if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || |
| 834 | CN->getZExtValue() == 9) { |
| 835 | AM.Scale = unsigned(CN->getZExtValue())-1; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 836 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 837 | SDValue MulVal = N.getNode()->getOperand(0); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 838 | SDValue Reg; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 839 | |
| 840 | // Okay, we know that we have a scale by now. However, if the scaled |
| 841 | // value is an add of something and a constant, we can fold the |
| 842 | // constant into the disp field here. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 843 | if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 844 | isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { |
| 845 | Reg = MulVal.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 846 | ConstantSDNode *AddVal = |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 847 | cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 848 | uint64_t Disp = AM.Disp + AddVal->getSExtValue() * |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 849 | CN->getZExtValue(); |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 850 | if (!is64Bit || |
| 851 | X86::isOffsetSuitableForCodeModel(Disp, M, |
| 852 | AM.hasSymbolicDisplacement())) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 853 | AM.Disp = Disp; |
| 854 | else |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 855 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 856 | } else { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 857 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 860 | AM.IndexReg = AM.Base_Reg = Reg; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 861 | return false; |
| 862 | } |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 863 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 864 | break; |
| 865 | |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 866 | case ISD::SUB: { |
| 867 | // Given A-B, if A can be completely folded into the address and |
| 868 | // the index field with the index field unused, use -B as the index. |
| 869 | // This is a win if a has multiple parts that can be folded into |
| 870 | // the address. Also, this saves a mov if the base register has |
| 871 | // other uses, since it avoids a two-address sub instruction, however |
| 872 | // it costs an additional mov if the index register has other uses. |
| 873 | |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 874 | // Add an artificial use to this node so that we can keep track of |
| 875 | // it if it gets CSE'd with a different node. |
| 876 | HandleSDNode Handle(N); |
| 877 | |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 878 | // Test if the LHS of the sub can be folded. |
| 879 | X86ISelAddressMode Backup = AM; |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 880 | if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) { |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 881 | AM = Backup; |
| 882 | break; |
| 883 | } |
| 884 | // Test if the index field is free for use. |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 885 | if (AM.IndexReg.getNode() || AM.isRIPRelative()) { |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 886 | AM = Backup; |
| 887 | break; |
| 888 | } |
Evan Cheng | f3caa52 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 889 | |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 890 | int Cost = 0; |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 891 | SDValue RHS = Handle.getValue().getNode()->getOperand(1); |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 892 | // If the RHS involves a register with multiple uses, this |
| 893 | // transformation incurs an extra mov, due to the neg instruction |
| 894 | // clobbering its operand. |
| 895 | if (!RHS.getNode()->hasOneUse() || |
| 896 | RHS.getNode()->getOpcode() == ISD::CopyFromReg || |
| 897 | RHS.getNode()->getOpcode() == ISD::TRUNCATE || |
| 898 | RHS.getNode()->getOpcode() == ISD::ANY_EXTEND || |
| 899 | (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 900 | RHS.getNode()->getOperand(0).getValueType() == MVT::i32)) |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 901 | ++Cost; |
| 902 | // If the base is a register with multiple uses, this |
| 903 | // transformation may save a mov. |
| 904 | if ((AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 905 | AM.Base_Reg.getNode() && |
| 906 | !AM.Base_Reg.getNode()->hasOneUse()) || |
Dan Gohman | 3cd90a1 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 907 | AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 908 | --Cost; |
| 909 | // If the folded LHS was interesting, this transformation saves |
| 910 | // address arithmetic. |
| 911 | if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) + |
| 912 | ((AM.Disp != 0) && (Backup.Disp == 0)) + |
| 913 | (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2) |
| 914 | --Cost; |
| 915 | // If it doesn't look like it may be an overall win, don't do it. |
| 916 | if (Cost >= 0) { |
| 917 | AM = Backup; |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | // Ok, the transformation is legal and appears profitable. Go for it. |
| 922 | SDValue Zero = CurDAG->getConstant(0, N.getValueType()); |
| 923 | SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS); |
| 924 | AM.IndexReg = Neg; |
| 925 | AM.Scale = 1; |
| 926 | |
| 927 | // Insert the new nodes into the topological ordering. |
| 928 | if (Zero.getNode()->getNodeId() == -1 || |
| 929 | Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 930 | CurDAG->RepositionNode(N.getNode(), Zero.getNode()); |
| 931 | Zero.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 932 | } |
| 933 | if (Neg.getNode()->getNodeId() == -1 || |
| 934 | Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 935 | CurDAG->RepositionNode(N.getNode(), Neg.getNode()); |
| 936 | Neg.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 937 | } |
| 938 | return false; |
| 939 | } |
| 940 | |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 941 | case ISD::ADD: { |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 942 | // Add an artificial use to this node so that we can keep track of |
| 943 | // it if it gets CSE'd with a different node. |
| 944 | HandleSDNode Handle(N); |
| 945 | SDValue LHS = Handle.getValue().getNode()->getOperand(0); |
| 946 | SDValue RHS = Handle.getValue().getNode()->getOperand(1); |
| 947 | |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 948 | X86ISelAddressMode Backup = AM; |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 949 | if (!MatchAddressRecursively(LHS, AM, Depth+1) && |
| 950 | !MatchAddressRecursively(RHS, AM, Depth+1)) |
| 951 | return false; |
| 952 | AM = Backup; |
| 953 | LHS = Handle.getValue().getNode()->getOperand(0); |
| 954 | RHS = Handle.getValue().getNode()->getOperand(1); |
Evan Cheng | f3caa52 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 955 | |
| 956 | // Try again after commuting the operands. |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 957 | if (!MatchAddressRecursively(RHS, AM, Depth+1) && |
| 958 | !MatchAddressRecursively(LHS, AM, Depth+1)) |
| 959 | return false; |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 960 | AM = Backup; |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 961 | LHS = Handle.getValue().getNode()->getOperand(0); |
| 962 | RHS = Handle.getValue().getNode()->getOperand(1); |
Dan Gohman | 77502c9 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 963 | |
| 964 | // If we couldn't fold both operands into the address at the same time, |
| 965 | // see if we can just put each operand into a register and fold at least |
| 966 | // the add. |
| 967 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 968 | !AM.Base_Reg.getNode() && |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 969 | !AM.IndexReg.getNode()) { |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 970 | AM.Base_Reg = LHS; |
| 971 | AM.IndexReg = RHS; |
Dan Gohman | 77502c9 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 972 | AM.Scale = 1; |
| 973 | return false; |
| 974 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 975 | break; |
Evan Cheng | 8e27826 | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 976 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 978 | case ISD::OR: |
| 979 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
Chris Lattner | d613942 | 2010-04-20 23:18:40 +0000 | [diff] [blame] | 980 | if (isLogicallyAddWithConstant(N, CurDAG)) { |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 981 | X86ISelAddressMode Backup = AM; |
Chris Lattner | d613942 | 2010-04-20 23:18:40 +0000 | [diff] [blame] | 982 | ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1)); |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 983 | uint64_t Offset = CN->getSExtValue(); |
Evan Cheng | f3caa52 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 985 | // Start with the LHS as an addr mode. |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 986 | if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) && |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 987 | // Address could not have picked a GV address for the displacement. |
| 988 | AM.GV == NULL && |
| 989 | // On x86-64, the resultant disp must fit in 32-bits. |
Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 990 | (!is64Bit || |
| 991 | X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M, |
Evan Cheng | f3caa52 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 992 | AM.hasSymbolicDisplacement()))) { |
Dan Gohman | 27cae7b | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 993 | AM.Disp += Offset; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 994 | return false; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 996 | AM = Backup; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 997 | } |
| 998 | break; |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 999 | |
| 1000 | case ISD::AND: { |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1001 | // Perform some heroic transforms on an and of a constant-count shift |
| 1002 | // with a constant to enable use of the scaled offset field. |
| 1003 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1004 | SDValue Shift = N.getOperand(0); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1005 | if (Shift.getNumOperands() != 2) break; |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1006 | |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1007 | // Scale must not be used already. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1008 | if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break; |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 1009 | |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1010 | SDValue X = Shift.getOperand(0); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1011 | ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 1012 | ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); |
| 1013 | if (!C1 || !C2) break; |
| 1014 | |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1015 | // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This |
| 1016 | // allows us to convert the shift and and into an h-register extract and |
| 1017 | // a scaled index. |
| 1018 | if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) { |
| 1019 | unsigned ScaleLog = 8 - C1->getZExtValue(); |
Rafael Espindola | 7c36683 | 2009-04-16 12:34:53 +0000 | [diff] [blame] | 1020 | if (ScaleLog > 0 && ScaleLog < 4 && |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1021 | C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1022 | SDValue Eight = CurDAG->getConstant(8, MVT::i8); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1023 | SDValue Mask = CurDAG->getConstant(0xff, N.getValueType()); |
| 1024 | SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(), |
| 1025 | X, Eight); |
| 1026 | SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(), |
| 1027 | Srl, Mask); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1028 | SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8); |
Dan Gohman | 62ad138 | 2009-04-14 22:45:05 +0000 | [diff] [blame] | 1029 | SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(), |
| 1030 | And, ShlCount); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Insert the new nodes into the topological ordering. |
| 1033 | if (Eight.getNode()->getNodeId() == -1 || |
| 1034 | Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1035 | CurDAG->RepositionNode(X.getNode(), Eight.getNode()); |
| 1036 | Eight.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1037 | } |
| 1038 | if (Mask.getNode()->getNodeId() == -1 || |
| 1039 | Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1040 | CurDAG->RepositionNode(X.getNode(), Mask.getNode()); |
| 1041 | Mask.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1042 | } |
| 1043 | if (Srl.getNode()->getNodeId() == -1 || |
| 1044 | Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) { |
| 1045 | CurDAG->RepositionNode(Shift.getNode(), Srl.getNode()); |
| 1046 | Srl.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 1047 | } |
| 1048 | if (And.getNode()->getNodeId() == -1 || |
| 1049 | And.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1050 | CurDAG->RepositionNode(N.getNode(), And.getNode()); |
| 1051 | And.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1052 | } |
Dan Gohman | 62ad138 | 2009-04-14 22:45:05 +0000 | [diff] [blame] | 1053 | if (ShlCount.getNode()->getNodeId() == -1 || |
| 1054 | ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1055 | CurDAG->RepositionNode(X.getNode(), ShlCount.getNode()); |
| 1056 | ShlCount.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1057 | } |
| 1058 | if (Shl.getNode()->getNodeId() == -1 || |
| 1059 | Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1060 | CurDAG->RepositionNode(N.getNode(), Shl.getNode()); |
| 1061 | Shl.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1062 | } |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1063 | CurDAG->ReplaceAllUsesWith(N, Shl); |
Dan Gohman | 21e3dfb | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1064 | AM.IndexReg = And; |
| 1065 | AM.Scale = (1 << ScaleLog); |
| 1066 | return false; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this |
| 1071 | // allows us to fold the shift into this addressing mode. |
| 1072 | if (Shift.getOpcode() != ISD::SHL) break; |
| 1073 | |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1074 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 1075 | // than one use (unless all uses are for address computation). Besides, |
| 1076 | // isel mechanism requires their node ids to be reused. |
| 1077 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 1078 | break; |
| 1079 | |
| 1080 | // Verify that the shift amount is something we can fold. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1081 | unsigned ShiftCst = C1->getZExtValue(); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1082 | if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) |
| 1083 | break; |
| 1084 | |
| 1085 | // Get the new AND mask, this folds to a constant. |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1086 | SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(), |
Evan Cheng | 552e3be | 2008-10-14 17:15:39 +0000 | [diff] [blame] | 1087 | SDValue(C2, 0), SDValue(C1, 0)); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1088 | SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X, |
| 1089 | NewANDMask); |
| 1090 | SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(), |
Dan Gohman | 7b8e964 | 2008-10-13 20:52:04 +0000 | [diff] [blame] | 1091 | NewAND, SDValue(C1, 0)); |
Dan Gohman | 8be6bbe | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Insert the new nodes into the topological ordering. |
| 1094 | if (C1->getNodeId() > X.getNode()->getNodeId()) { |
| 1095 | CurDAG->RepositionNode(X.getNode(), C1); |
| 1096 | C1->setNodeId(X.getNode()->getNodeId()); |
| 1097 | } |
| 1098 | if (NewANDMask.getNode()->getNodeId() == -1 || |
| 1099 | NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) { |
| 1100 | CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode()); |
| 1101 | NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId()); |
| 1102 | } |
| 1103 | if (NewAND.getNode()->getNodeId() == -1 || |
| 1104 | NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) { |
| 1105 | CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode()); |
| 1106 | NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId()); |
| 1107 | } |
| 1108 | if (NewSHIFT.getNode()->getNodeId() == -1 || |
| 1109 | NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 1110 | CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode()); |
| 1111 | NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 1112 | } |
| 1113 | |
Dan Gohman | e540810 | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1114 | CurDAG->ReplaceAllUsesWith(N, NewSHIFT); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1115 | |
| 1116 | AM.Scale = 1 << ShiftCst; |
| 1117 | AM.IndexReg = NewAND; |
| 1118 | return false; |
| 1119 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1120 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1121 | |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1122 | return MatchAddressBase(N, AM); |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 1126 | /// specified addressing mode without any further recursion. |
Rafael Espindola | 523249f | 2009-03-31 16:16:57 +0000 | [diff] [blame] | 1127 | bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1128 | // Is the base register already occupied? |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1129 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1130 | // If so, check to see if the scale index register is set. |
Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1131 | if (AM.IndexReg.getNode() == 0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1132 | AM.IndexReg = N; |
| 1133 | AM.Scale = 1; |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | // Otherwise, we cannot select it. |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | // Default, generate it as a register. |
| 1142 | AM.BaseType = X86ISelAddressMode::RegBase; |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1143 | AM.Base_Reg = N; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1147 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 1148 | /// It returns the operands which make up the maximal addressing mode it can |
| 1149 | /// match by reference. |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1150 | /// |
| 1151 | /// Parent is the parent node of the addr operand that is being matched. It |
| 1152 | /// is always a load, store, atomic node, or null. It is only null when |
| 1153 | /// checking memory operands for inline asm nodes. |
| 1154 | bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1155 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1156 | SDValue &Disp, SDValue &Segment) { |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1157 | X86ISelAddressMode AM; |
Evan Cheng | c7928f8 | 2009-12-18 01:59:21 +0000 | [diff] [blame] | 1158 | if (MatchAddress(N, AM)) |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1159 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1160 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1161 | EVT VT = N.getValueType(); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1162 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1163 | if (!AM.Base_Reg.getNode()) |
| 1164 | AM.Base_Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1165 | } |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1166 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1167 | if (!AM.IndexReg.getNode()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1168 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1170 | if (Parent && |
| 1171 | // This list of opcodes are all the nodes that have an "addr:$ptr" operand |
| 1172 | // that are not a MemSDNode, and thus don't have proper addrspace info. |
| 1173 | Parent->getOpcode() != ISD::PREFETCH && |
| 1174 | Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme |
| 1175 | Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores. |
| 1176 | Parent->getOpcode() != X86ISD::VZEXT_LOAD && |
| 1177 | Parent->getOpcode() != X86ISD::FLD && |
| 1178 | Parent->getOpcode() != X86ISD::FILD && |
| 1179 | Parent->getOpcode() != X86ISD::FILD_FLAG && |
| 1180 | Parent->getOpcode() != X86ISD::FP_TO_INT16_IN_MEM && |
| 1181 | Parent->getOpcode() != X86ISD::FP_TO_INT32_IN_MEM && |
| 1182 | Parent->getOpcode() != X86ISD::FP_TO_INT64_IN_MEM && |
| 1183 | Parent->getOpcode() != X86ISD::LCMPXCHG_DAG && |
| 1184 | Parent->getOpcode() != X86ISD::FST) { |
| 1185 | unsigned AddrSpace = |
| 1186 | cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace(); |
| 1187 | // AddrSpace 256 -> GS, 257 -> FS. |
| 1188 | if (AddrSpace == 256) |
| 1189 | AM.Segment = CurDAG->getRegister(X86::GS, VT); |
| 1190 | if (AddrSpace == 257) |
| 1191 | AM.Segment = CurDAG->getRegister(X86::FS, VT); |
| 1192 | } |
| 1193 | |
| 1194 | |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1195 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1196 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1199 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 1200 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 1201 | /// is derived from the type of N, which is either v4f32 or v2f64. |
Chris Lattner | 64b4986 | 2010-02-17 06:07:47 +0000 | [diff] [blame] | 1202 | /// |
| 1203 | /// We also return: |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1204 | /// PatternChainNode: this is the matched node that has a chain input and |
| 1205 | /// output. |
Chris Lattner | e60f7b4 | 2010-03-01 22:51:11 +0000 | [diff] [blame] | 1206 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1207 | SDValue N, SDValue &Base, |
| 1208 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1209 | SDValue &Disp, SDValue &Segment, |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1210 | SDValue &PatternNodeWithChain) { |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1211 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1212 | PatternNodeWithChain = N.getOperand(0); |
| 1213 | if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) && |
| 1214 | PatternNodeWithChain.hasOneUse() && |
Chris Lattner | f1c6428 | 2010-02-21 04:53:34 +0000 | [diff] [blame] | 1215 | IsProfitableToFold(N.getOperand(0), N.getNode(), Root) && |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1216 | IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) { |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1217 | LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain); |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1218 | if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1219 | return false; |
| 1220 | return true; |
| 1221 | } |
| 1222 | } |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Also handle the case where we explicitly require zeros in the top |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1225 | // elements. This is a vector shuffle from the zero vector. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1226 | if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 1227 | // 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] | 1228 | N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1229 | N.getOperand(0).getNode()->hasOneUse() && |
| 1230 | ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && |
Chris Lattner | 92d3ada | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 1231 | N.getOperand(0).getOperand(0).hasOneUse() && |
| 1232 | IsProfitableToFold(N.getOperand(0), N.getNode(), Root) && |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1233 | IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) { |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1234 | // Okay, this is a zero extending load. Fold it. |
| 1235 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1236 | if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1237 | return false; |
Chris Lattner | a170b5e | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1238 | PatternNodeWithChain = SDValue(LD, 0); |
Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1239 | return true; |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1240 | } |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1241 | return false; |
| 1242 | } |
| 1243 | |
| 1244 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1245 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 1246 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 1247 | bool X86DAGToDAGISel::SelectLEAAddr(SDValue N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1248 | SDValue &Base, SDValue &Scale, |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 1249 | SDValue &Index, SDValue &Disp, |
| 1250 | SDValue &Segment) { |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1251 | X86ISelAddressMode AM; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support |
| 1254 | // segments. |
| 1255 | SDValue Copy = AM.Segment; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1256 | SDValue T = CurDAG->getRegister(0, MVT::i32); |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1257 | AM.Segment = T; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1258 | if (MatchAddress(N, AM)) |
| 1259 | return false; |
Rafael Espindola | dbcfb30 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1260 | assert (T == AM.Segment); |
| 1261 | AM.Segment = Copy; |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1262 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1263 | EVT VT = N.getValueType(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1264 | unsigned Complexity = 0; |
| 1265 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1266 | if (AM.Base_Reg.getNode()) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1267 | Complexity = 1; |
| 1268 | else |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1269 | AM.Base_Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1270 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1271 | Complexity = 4; |
| 1272 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1273 | if (AM.IndexReg.getNode()) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1274 | Complexity++; |
| 1275 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1276 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 1278 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1279 | // a simple shift. |
| 1280 | if (AM.Scale > 1) |
Evan Cheng | 8c03fe4 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 1281 | Complexity++; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1282 | |
| 1283 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 1284 | // to a LEA. This is determined with some expermentation but is by no means |
| 1285 | // optimal (especially for code size consideration). LEA is nice because of |
| 1286 | // its three-address nature. Tweak the cost function again when we can run |
| 1287 | // convertToThreeAddress() at register allocation time. |
Dan Gohman | 2d0a1cc | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 1288 | if (AM.hasSymbolicDisplacement()) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1289 | // For X86-64, we should always use lea to materialize RIP relative |
| 1290 | // addresses. |
Evan Cheng | 953fa04 | 2006-12-05 22:03:40 +0000 | [diff] [blame] | 1291 | if (Subtarget->is64Bit()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1292 | Complexity = 4; |
| 1293 | else |
| 1294 | Complexity += 2; |
| 1295 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1296 | |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1297 | if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode())) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1298 | Complexity++; |
| 1299 | |
Chris Lattner | 2514278 | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1300 | // If it isn't worth using an LEA, reject it. |
Chris Lattner | 14f7511 | 2009-07-11 23:07:30 +0000 | [diff] [blame] | 1301 | if (Complexity <= 2) |
Chris Lattner | 2514278 | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1302 | return false; |
| 1303 | |
Chris Lattner | 2514278 | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1304 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
| 1305 | return true; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1308 | /// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes. |
Chris Lattner | 52a261b | 2010-09-21 20:31:19 +0000 | [diff] [blame] | 1309 | bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue N, SDValue &Base, |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1310 | SDValue &Scale, SDValue &Index, |
Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 1311 | SDValue &Disp, SDValue &Segment) { |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1312 | assert(N.getOpcode() == ISD::TargetGlobalTLSAddress); |
| 1313 | const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); |
Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1315 | X86ISelAddressMode AM; |
| 1316 | AM.GV = GA->getGlobal(); |
| 1317 | AM.Disp += GA->getOffset(); |
Dan Gohman | ffce6f1 | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1318 | AM.Base_Reg = CurDAG->getRegister(0, N.getValueType()); |
Chris Lattner | ba8ef45 | 2009-06-26 21:18:37 +0000 | [diff] [blame] | 1319 | AM.SymbolFlags = GA->getTargetFlags(); |
| 1320 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1321 | if (N.getValueType() == MVT::i32) { |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1322 | AM.Scale = 1; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1323 | AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32); |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1324 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1325 | AM.IndexReg = CurDAG->getRegister(0, MVT::i64); |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Chris Lattner | 5c0b16d | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1328 | getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1333 | bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1334 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1335 | SDValue &Index, SDValue &Disp, |
| 1336 | SDValue &Segment) { |
Chris Lattner | d1b7382 | 2010-03-02 22:20:06 +0000 | [diff] [blame] | 1337 | if (!ISD::isNON_EXTLoad(N.getNode()) || |
| 1338 | !IsProfitableToFold(N, P, P) || |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1339 | !IsLegalToFold(N, P, P, OptLevel)) |
Chris Lattner | d1b7382 | 2010-03-02 22:20:06 +0000 | [diff] [blame] | 1340 | return false; |
| 1341 | |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1342 | return SelectAddr(N.getNode(), |
| 1343 | N.getOperand(1), Base, Scale, Index, Disp, Segment); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Dan Gohman | 8b74696 | 2008-09-23 18:22:58 +0000 | [diff] [blame] | 1346 | /// getGlobalBaseReg - Return an SDNode that returns the value of |
| 1347 | /// the global base register. Output instructions required to |
| 1348 | /// initialize the global base register, if necessary. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1349 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1350 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | c553462 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 1351 | unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1352 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1355 | SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) { |
| 1356 | SDValue Chain = Node->getOperand(0); |
| 1357 | SDValue In1 = Node->getOperand(1); |
| 1358 | SDValue In2L = Node->getOperand(2); |
| 1359 | SDValue In2H = Node->getOperand(3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1360 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1361 | if (!SelectAddr(Node, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1362 | return NULL; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1363 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 1364 | MemOp[0] = cast<MemSDNode>(Node)->getMemOperand(); |
| 1365 | const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain}; |
| 1366 | SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(), |
| 1367 | MVT::i32, MVT::i32, MVT::Other, Ops, |
| 1368 | array_lengthof(Ops)); |
| 1369 | cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1); |
| 1370 | return ResNode; |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1371 | } |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1372 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1373 | SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) { |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1374 | if (Node->hasAnyUseOfValue(0)) |
| 1375 | return 0; |
| 1376 | |
| 1377 | // Optimize common patterns for __sync_add_and_fetch and |
| 1378 | // __sync_sub_and_fetch where the result is not used. This allows us |
| 1379 | // to use "lock" version of add, sub, inc, dec instructions. |
| 1380 | // FIXME: Do not use special instructions but instead add the "lock" |
| 1381 | // prefix to the target node somehow. The extra information will then be |
| 1382 | // transferred to machine instruction and it denotes the prefix. |
| 1383 | SDValue Chain = Node->getOperand(0); |
| 1384 | SDValue Ptr = Node->getOperand(1); |
| 1385 | SDValue Val = Node->getOperand(2); |
| 1386 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 1387 | if (!SelectAddr(Node, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1388 | return 0; |
| 1389 | |
| 1390 | bool isInc = false, isDec = false, isSub = false, isCN = false; |
| 1391 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val); |
| 1392 | if (CN) { |
| 1393 | isCN = true; |
| 1394 | int64_t CNVal = CN->getSExtValue(); |
| 1395 | if (CNVal == 1) |
| 1396 | isInc = true; |
| 1397 | else if (CNVal == -1) |
| 1398 | isDec = true; |
| 1399 | else if (CNVal >= 0) |
| 1400 | Val = CurDAG->getTargetConstant(CNVal, NVT); |
| 1401 | else { |
| 1402 | isSub = true; |
| 1403 | Val = CurDAG->getTargetConstant(-CNVal, NVT); |
| 1404 | } |
| 1405 | } else if (Val.hasOneUse() && |
| 1406 | Val.getOpcode() == ISD::SUB && |
| 1407 | X86::isZeroNode(Val.getOperand(0))) { |
| 1408 | isSub = true; |
| 1409 | Val = Val.getOperand(1); |
| 1410 | } |
| 1411 | |
| 1412 | unsigned Opc = 0; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1413 | switch (NVT.getSimpleVT().SimpleTy) { |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1414 | default: return 0; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1415 | case MVT::i8: |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1416 | if (isInc) |
| 1417 | Opc = X86::LOCK_INC8m; |
| 1418 | else if (isDec) |
| 1419 | Opc = X86::LOCK_DEC8m; |
| 1420 | else if (isSub) { |
| 1421 | if (isCN) |
| 1422 | Opc = X86::LOCK_SUB8mi; |
| 1423 | else |
| 1424 | Opc = X86::LOCK_SUB8mr; |
| 1425 | } else { |
| 1426 | if (isCN) |
| 1427 | Opc = X86::LOCK_ADD8mi; |
| 1428 | else |
| 1429 | Opc = X86::LOCK_ADD8mr; |
| 1430 | } |
| 1431 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1432 | case MVT::i16: |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1433 | if (isInc) |
| 1434 | Opc = X86::LOCK_INC16m; |
| 1435 | else if (isDec) |
| 1436 | Opc = X86::LOCK_DEC16m; |
| 1437 | else if (isSub) { |
| 1438 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1439 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1440 | Opc = X86::LOCK_SUB16mi8; |
| 1441 | else |
| 1442 | Opc = X86::LOCK_SUB16mi; |
| 1443 | } else |
| 1444 | Opc = X86::LOCK_SUB16mr; |
| 1445 | } else { |
| 1446 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1447 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1448 | Opc = X86::LOCK_ADD16mi8; |
| 1449 | else |
| 1450 | Opc = X86::LOCK_ADD16mi; |
| 1451 | } else |
| 1452 | Opc = X86::LOCK_ADD16mr; |
| 1453 | } |
| 1454 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1455 | case MVT::i32: |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1456 | if (isInc) |
| 1457 | Opc = X86::LOCK_INC32m; |
| 1458 | else if (isDec) |
| 1459 | Opc = X86::LOCK_DEC32m; |
| 1460 | else if (isSub) { |
| 1461 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1462 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1463 | Opc = X86::LOCK_SUB32mi8; |
| 1464 | else |
| 1465 | Opc = X86::LOCK_SUB32mi; |
| 1466 | } else |
| 1467 | Opc = X86::LOCK_SUB32mr; |
| 1468 | } else { |
| 1469 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1470 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1471 | Opc = X86::LOCK_ADD32mi8; |
| 1472 | else |
| 1473 | Opc = X86::LOCK_ADD32mi; |
| 1474 | } else |
| 1475 | Opc = X86::LOCK_ADD32mr; |
| 1476 | } |
| 1477 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1478 | case MVT::i64: |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1479 | if (isInc) |
| 1480 | Opc = X86::LOCK_INC64m; |
| 1481 | else if (isDec) |
| 1482 | Opc = X86::LOCK_DEC64m; |
| 1483 | else if (isSub) { |
| 1484 | Opc = X86::LOCK_SUB64mr; |
| 1485 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1486 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1487 | Opc = X86::LOCK_SUB64mi8; |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1488 | else if (i64immSExt32(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1489 | Opc = X86::LOCK_SUB64mi32; |
| 1490 | } |
| 1491 | } else { |
| 1492 | Opc = X86::LOCK_ADD64mr; |
| 1493 | if (isCN) { |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1494 | if (immSext8(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1495 | Opc = X86::LOCK_ADD64mi8; |
Jakob Stoklund Olesen | 3061c44 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 1496 | else if (i64immSExt32(Val.getNode())) |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1497 | Opc = X86::LOCK_ADD64mi32; |
| 1498 | } |
| 1499 | } |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | DebugLoc dl = Node->getDebugLoc(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1504 | SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1505 | dl, NVT), 0); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1506 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 1507 | MemOp[0] = cast<MemSDNode>(Node)->getMemOperand(); |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1508 | if (isInc || isDec) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1509 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain }; |
| 1510 | SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0); |
| 1511 | cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1); |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1512 | SDValue RetVals[] = { Undef, Ret }; |
| 1513 | return CurDAG->getMergeValues(RetVals, 2, dl).getNode(); |
| 1514 | } else { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 1515 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain }; |
| 1516 | SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0); |
| 1517 | cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1); |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1518 | SDValue RetVals[] = { Undef, Ret }; |
| 1519 | return CurDAG->getMergeValues(RetVals, 2, dl).getNode(); |
| 1520 | } |
| 1521 | } |
| 1522 | |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1523 | /// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has |
| 1524 | /// any uses which require the SF or OF bits to be accurate. |
| 1525 | static bool HasNoSignedComparisonUses(SDNode *N) { |
| 1526 | // Examine each user of the node. |
| 1527 | for (SDNode::use_iterator UI = N->use_begin(), |
| 1528 | UE = N->use_end(); UI != UE; ++UI) { |
| 1529 | // Only examine CopyToReg uses. |
| 1530 | if (UI->getOpcode() != ISD::CopyToReg) |
| 1531 | return false; |
| 1532 | // Only examine CopyToReg uses that copy to EFLAGS. |
| 1533 | if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() != |
| 1534 | X86::EFLAGS) |
| 1535 | return false; |
| 1536 | // Examine each user of the CopyToReg use. |
| 1537 | for (SDNode::use_iterator FlagUI = UI->use_begin(), |
| 1538 | FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) { |
| 1539 | // Only examine the Flag result. |
| 1540 | if (FlagUI.getUse().getResNo() != 1) continue; |
| 1541 | // Anything unusual: assume conservatively. |
| 1542 | if (!FlagUI->isMachineOpcode()) return false; |
| 1543 | // Examine the opcode of the user. |
| 1544 | switch (FlagUI->getMachineOpcode()) { |
| 1545 | // These comparisons don't treat the most significant bit specially. |
| 1546 | case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr: |
| 1547 | case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr: |
| 1548 | case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm: |
| 1549 | case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm: |
Chris Lattner | bd13fb6 | 2010-02-11 19:25:55 +0000 | [diff] [blame] | 1550 | case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4: |
| 1551 | case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4: |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1552 | case X86::CMOVA16rr: case X86::CMOVA16rm: |
| 1553 | case X86::CMOVA32rr: case X86::CMOVA32rm: |
| 1554 | case X86::CMOVA64rr: case X86::CMOVA64rm: |
| 1555 | case X86::CMOVAE16rr: case X86::CMOVAE16rm: |
| 1556 | case X86::CMOVAE32rr: case X86::CMOVAE32rm: |
| 1557 | case X86::CMOVAE64rr: case X86::CMOVAE64rm: |
| 1558 | case X86::CMOVB16rr: case X86::CMOVB16rm: |
| 1559 | case X86::CMOVB32rr: case X86::CMOVB32rm: |
| 1560 | case X86::CMOVB64rr: case X86::CMOVB64rm: |
| 1561 | case X86::CMOVBE16rr: case X86::CMOVBE16rm: |
| 1562 | case X86::CMOVBE32rr: case X86::CMOVBE32rm: |
| 1563 | case X86::CMOVBE64rr: case X86::CMOVBE64rm: |
| 1564 | case X86::CMOVE16rr: case X86::CMOVE16rm: |
| 1565 | case X86::CMOVE32rr: case X86::CMOVE32rm: |
| 1566 | case X86::CMOVE64rr: case X86::CMOVE64rm: |
| 1567 | case X86::CMOVNE16rr: case X86::CMOVNE16rm: |
| 1568 | case X86::CMOVNE32rr: case X86::CMOVNE32rm: |
| 1569 | case X86::CMOVNE64rr: case X86::CMOVNE64rm: |
| 1570 | case X86::CMOVNP16rr: case X86::CMOVNP16rm: |
| 1571 | case X86::CMOVNP32rr: case X86::CMOVNP32rm: |
| 1572 | case X86::CMOVNP64rr: case X86::CMOVNP64rm: |
| 1573 | case X86::CMOVP16rr: case X86::CMOVP16rm: |
| 1574 | case X86::CMOVP32rr: case X86::CMOVP32rm: |
| 1575 | case X86::CMOVP64rr: case X86::CMOVP64rm: |
| 1576 | continue; |
| 1577 | // Anything else: assume conservatively. |
| 1578 | default: return false; |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1585 | SDNode *X86DAGToDAGISel::Select(SDNode *Node) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1586 | EVT NVT = Node->getValueType(0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1587 | unsigned Opc, MOpc; |
| 1588 | unsigned Opcode = Node->getOpcode(); |
Dale Johannesen | d839254 | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 1589 | DebugLoc dl = Node->getDebugLoc(); |
| 1590 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1591 | DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n'); |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1592 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1593 | if (Node->isMachineOpcode()) { |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1594 | DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n'); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1595 | return NULL; // Already selected. |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1596 | } |
Evan Cheng | 38262ca | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 1597 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1598 | switch (Opcode) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1599 | default: break; |
| 1600 | case X86ISD::GlobalBaseReg: |
| 1601 | return getGlobalBaseReg(); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1602 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1603 | case X86ISD::ATOMOR64_DAG: |
| 1604 | return SelectAtomic64(Node, X86::ATOMOR6432); |
| 1605 | case X86ISD::ATOMXOR64_DAG: |
| 1606 | return SelectAtomic64(Node, X86::ATOMXOR6432); |
| 1607 | case X86ISD::ATOMADD64_DAG: |
| 1608 | return SelectAtomic64(Node, X86::ATOMADD6432); |
| 1609 | case X86ISD::ATOMSUB64_DAG: |
| 1610 | return SelectAtomic64(Node, X86::ATOMSUB6432); |
| 1611 | case X86ISD::ATOMNAND64_DAG: |
| 1612 | return SelectAtomic64(Node, X86::ATOMNAND6432); |
| 1613 | case X86ISD::ATOMAND64_DAG: |
| 1614 | return SelectAtomic64(Node, X86::ATOMAND6432); |
| 1615 | case X86ISD::ATOMSWAP64_DAG: |
| 1616 | return SelectAtomic64(Node, X86::ATOMSWAP6432); |
Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 1617 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1618 | case ISD::ATOMIC_LOAD_ADD: { |
| 1619 | SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT); |
| 1620 | if (RetVal) |
| 1621 | return RetVal; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | case ISD::SMUL_LOHI: |
| 1626 | case ISD::UMUL_LOHI: { |
| 1627 | SDValue N0 = Node->getOperand(0); |
| 1628 | SDValue N1 = Node->getOperand(1); |
| 1629 | |
| 1630 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1631 | if (!isSigned) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1632 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1633 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1634 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1635 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1636 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
| 1637 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1638 | } |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1639 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1640 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1641 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1642 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1643 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1644 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 1645 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1646 | } |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1647 | } |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1648 | |
| 1649 | unsigned LoReg, HiReg; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1650 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1651 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1652 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1653 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1654 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
| 1655 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1659 | bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1660 | // Multiply is commmutative. |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1661 | if (!foldedLoad) { |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1662 | foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1663 | if (foldedLoad) |
| 1664 | std::swap(N0, N1); |
| 1665 | } |
| 1666 | |
| 1667 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg, |
| 1668 | N0, SDValue()).getValue(1); |
| 1669 | |
| 1670 | if (foldedLoad) { |
| 1671 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 1672 | InFlag }; |
| 1673 | SDNode *CNode = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1674 | CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, |
| 1675 | array_lengthof(Ops)); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1676 | InFlag = SDValue(CNode, 1); |
| 1677 | // Update the chain. |
| 1678 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
| 1679 | } else { |
| 1680 | InFlag = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1681 | SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Jakob Stoklund Olesen | 4f5d84e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 1684 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1685 | if (HiReg == X86::AH && Subtarget->is64Bit() && |
| 1686 | !SDValue(Node, 1).use_empty()) { |
| 1687 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1688 | X86::AX, MVT::i16, InFlag); |
| 1689 | InFlag = Result.getValue(2); |
| 1690 | // Get the low part if needed. Don't use getCopyFromReg for aliasing |
| 1691 | // registers. |
| 1692 | if (!SDValue(Node, 0).use_empty()) |
| 1693 | ReplaceUses(SDValue(Node, 1), |
| 1694 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 1695 | |
| 1696 | // Shift AX down 8 bits. |
| 1697 | Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16, |
| 1698 | Result, |
| 1699 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
| 1700 | // Then truncate it down to i8. |
| 1701 | ReplaceUses(SDValue(Node, 1), |
| 1702 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 1703 | } |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1704 | // Copy the low half of the result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1705 | if (!SDValue(Node, 0).use_empty()) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1706 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1707 | LoReg, NVT, InFlag); |
| 1708 | InFlag = Result.getValue(2); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1709 | ReplaceUses(SDValue(Node, 0), Result); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1710 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1711 | } |
| 1712 | // Copy the high half of the result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1713 | if (!SDValue(Node, 1).use_empty()) { |
Jakob Stoklund Olesen | 4f5d84e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 1714 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1715 | HiReg, NVT, InFlag); |
| 1716 | InFlag = Result.getValue(2); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1717 | ReplaceUses(SDValue(Node, 1), Result); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1718 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1721 | return NULL; |
| 1722 | } |
| 1723 | |
| 1724 | case ISD::SDIVREM: |
| 1725 | case ISD::UDIVREM: { |
| 1726 | SDValue N0 = Node->getOperand(0); |
| 1727 | SDValue N1 = Node->getOperand(1); |
| 1728 | |
| 1729 | bool isSigned = Opcode == ISD::SDIVREM; |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1730 | if (!isSigned) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1731 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1732 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1733 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1734 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1735 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 1736 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1737 | } |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1738 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1739 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1740 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1741 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1742 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1743 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 1744 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1745 | } |
Bill Wendling | 1232167 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1746 | } |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 9e32383 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 1748 | unsigned LoReg, HiReg, ClrReg; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1749 | unsigned ClrOpcode, SExtOpcode; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1750 | switch (NVT.getSimpleVT().SimpleTy) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1751 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1752 | case MVT::i8: |
Chris Lattner | 9e32383 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 1753 | LoReg = X86::AL; ClrReg = HiReg = X86::AH; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1754 | ClrOpcode = 0; |
| 1755 | SExtOpcode = X86::CBW; |
| 1756 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1757 | case MVT::i16: |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1758 | LoReg = X86::AX; HiReg = X86::DX; |
Dan Gohman | f1b4d26 | 2010-01-12 04:42:54 +0000 | [diff] [blame] | 1759 | ClrOpcode = X86::MOV16r0; ClrReg = X86::DX; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1760 | SExtOpcode = X86::CWD; |
| 1761 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1762 | case MVT::i32: |
Chris Lattner | 9e32383 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 1763 | LoReg = X86::EAX; ClrReg = HiReg = X86::EDX; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1764 | ClrOpcode = X86::MOV32r0; |
| 1765 | SExtOpcode = X86::CDQ; |
| 1766 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1767 | case MVT::i64: |
Chris Lattner | 9e32383 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 1768 | LoReg = X86::RAX; ClrReg = HiReg = X86::RDX; |
Dan Gohman | f1b4d26 | 2010-01-12 04:42:54 +0000 | [diff] [blame] | 1769 | ClrOpcode = X86::MOV64r0; |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1770 | SExtOpcode = X86::CQO; |
Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | } |
| 1773 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1774 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1775 | bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1776 | bool signBitIsZero = CurDAG->SignBitIsZero(N0); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1778 | SDValue InFlag; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1779 | if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1780 | // Special case for div8, just use a move with zero extension to AX to |
| 1781 | // clear the upper 8 bits (AH). |
| 1782 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1783 | if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1784 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) }; |
| 1785 | Move = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1786 | SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16, |
| 1787 | MVT::Other, Ops, |
| 1788 | array_lengthof(Ops)), 0); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1789 | Chain = Move.getValue(1); |
| 1790 | ReplaceUses(N0.getValue(1), Chain); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1791 | } else { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1792 | Move = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1793 | SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1794 | Chain = CurDAG->getEntryNode(); |
| 1795 | } |
| 1796 | Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue()); |
| 1797 | InFlag = Chain.getValue(1); |
| 1798 | } else { |
| 1799 | InFlag = |
| 1800 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, |
| 1801 | LoReg, N0, SDValue()).getValue(1); |
| 1802 | if (isSigned && !signBitIsZero) { |
| 1803 | // Sign extend the low part into the high part. |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1804 | InFlag = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1805 | SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1806 | } else { |
| 1807 | // Zero out the high part, effectively zero extending the input. |
Dan Gohman | f1b4d26 | 2010-01-12 04:42:54 +0000 | [diff] [blame] | 1808 | SDValue ClrNode = |
| 1809 | SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0); |
Chris Lattner | 9e32383 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 1810 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg, |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1811 | ClrNode, InFlag).getValue(1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1812 | } |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1813 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1814 | |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1815 | if (foldedLoad) { |
| 1816 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 1817 | InFlag }; |
| 1818 | SDNode *CNode = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1819 | CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, |
| 1820 | array_lengthof(Ops)); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1821 | InFlag = SDValue(CNode, 1); |
| 1822 | // Update the chain. |
| 1823 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
| 1824 | } else { |
| 1825 | InFlag = |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1826 | SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1827 | } |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1828 | |
Jakob Stoklund Olesen | 4f5d84e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 1829 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1830 | // Shift it down 8 bits. |
| 1831 | if (HiReg == X86::AH && Subtarget->is64Bit() && |
| 1832 | !SDValue(Node, 1).use_empty()) { |
| 1833 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1834 | X86::AX, MVT::i16, InFlag); |
| 1835 | InFlag = Result.getValue(2); |
| 1836 | |
| 1837 | // If we also need AL (the quotient), get it by extracting a subreg from |
| 1838 | // Result. The fast register allocator does not like multiple CopyFromReg |
| 1839 | // nodes using aliasing registers. |
| 1840 | if (!SDValue(Node, 0).use_empty()) |
| 1841 | ReplaceUses(SDValue(Node, 0), |
| 1842 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 1843 | |
| 1844 | // Shift AX right by 8 bits instead of using AH. |
| 1845 | Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16, |
| 1846 | Result, |
| 1847 | CurDAG->getTargetConstant(8, MVT::i8)), |
| 1848 | 0); |
| 1849 | ReplaceUses(SDValue(Node, 1), |
| 1850 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 1851 | } |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1852 | // Copy the division (low) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1853 | if (!SDValue(Node, 0).use_empty()) { |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1854 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1855 | LoReg, NVT, InFlag); |
| 1856 | InFlag = Result.getValue(2); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1857 | ReplaceUses(SDValue(Node, 0), Result); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1858 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1859 | } |
| 1860 | // Copy the remainder (high) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1861 | if (!SDValue(Node, 1).use_empty()) { |
Jakob Stoklund Olesen | 4f5d84e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 1862 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 1863 | HiReg, NVT, InFlag); |
| 1864 | InFlag = Result.getValue(2); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1865 | ReplaceUses(SDValue(Node, 1), Result); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1866 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1867 | } |
Dan Gohman | 7267734 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 1868 | return NULL; |
| 1869 | } |
| 1870 | |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1871 | case X86ISD::CMP: { |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1872 | SDValue N0 = Node->getOperand(0); |
| 1873 | SDValue N1 = Node->getOperand(1); |
| 1874 | |
| 1875 | // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to |
| 1876 | // use a smaller encoding. |
Eli Friedman | 7752442 | 2010-08-04 22:40:58 +0000 | [diff] [blame] | 1877 | if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() && |
| 1878 | HasNoSignedComparisonUses(Node)) |
Evan Cheng | 2bce5f4b | 2010-04-28 08:30:49 +0000 | [diff] [blame] | 1879 | // Look past the truncate if CMP is the only use of it. |
| 1880 | N0 = N0.getOperand(0); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1881 | if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() && |
| 1882 | N0.getValueType() != MVT::i8 && |
| 1883 | X86::isZeroNode(N1)) { |
| 1884 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1)); |
| 1885 | if (!C) break; |
| 1886 | |
| 1887 | // For example, convert "testl %eax, $8" to "testb %al, $8" |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1888 | if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 && |
| 1889 | (!(C->getZExtValue() & 0x80) || |
| 1890 | HasNoSignedComparisonUses(Node))) { |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1891 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8); |
| 1892 | SDValue Reg = N0.getNode()->getOperand(0); |
| 1893 | |
| 1894 | // On x86-32, only the ABCD registers have 8-bit subregisters. |
| 1895 | if (!Subtarget->is64Bit()) { |
| 1896 | TargetRegisterClass *TRC = 0; |
| 1897 | switch (N0.getValueType().getSimpleVT().SimpleTy) { |
| 1898 | case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break; |
| 1899 | case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break; |
| 1900 | default: llvm_unreachable("Unsupported TEST operand type!"); |
| 1901 | } |
| 1902 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1903 | Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl, |
| 1904 | Reg.getValueType(), Reg, RC), 0); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | // Extract the l-register. |
Jakob Stoklund Olesen | 3458e9e | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 1908 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1909 | MVT::i8, Reg); |
| 1910 | |
| 1911 | // Emit a testb. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1912 | return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | // For example, "testl %eax, $2048" to "testb %ah, $8". |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1916 | if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 && |
| 1917 | (!(C->getZExtValue() & 0x8000) || |
| 1918 | HasNoSignedComparisonUses(Node))) { |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1919 | // Shift the immediate right by 8 bits. |
| 1920 | SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8, |
| 1921 | MVT::i8); |
| 1922 | SDValue Reg = N0.getNode()->getOperand(0); |
| 1923 | |
| 1924 | // Put the value in an ABCD register. |
| 1925 | TargetRegisterClass *TRC = 0; |
| 1926 | switch (N0.getValueType().getSimpleVT().SimpleTy) { |
| 1927 | case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break; |
| 1928 | case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break; |
| 1929 | case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break; |
| 1930 | default: llvm_unreachable("Unsupported TEST operand type!"); |
| 1931 | } |
| 1932 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32); |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1933 | Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl, |
| 1934 | Reg.getValueType(), Reg, RC), 0); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Extract the h-register. |
Jakob Stoklund Olesen | 3458e9e | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 1937 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl, |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1938 | MVT::i8, Reg); |
| 1939 | |
| 1940 | // Emit a testb. No special NOREX tricks are needed since there's |
| 1941 | // only one GPR operand! |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1942 | return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, |
| 1943 | Subreg, ShiftedImm); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | // For example, "testl %eax, $32776" to "testw %ax, $32776". |
| 1947 | if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 && |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1948 | N0.getValueType() != MVT::i16 && |
| 1949 | (!(C->getZExtValue() & 0x8000) || |
| 1950 | HasNoSignedComparisonUses(Node))) { |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1951 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16); |
| 1952 | SDValue Reg = N0.getNode()->getOperand(0); |
| 1953 | |
| 1954 | // Extract the 16-bit subregister. |
Jakob Stoklund Olesen | 3458e9e | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 1955 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl, |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1956 | MVT::i16, Reg); |
| 1957 | |
| 1958 | // Emit a testw. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1959 | return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | // For example, "testq %rax, $268468232" to "testl %eax, $268468232". |
| 1963 | if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 && |
Dan Gohman | 11596ed | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 1964 | N0.getValueType() == MVT::i64 && |
| 1965 | (!(C->getZExtValue() & 0x80000000) || |
| 1966 | HasNoSignedComparisonUses(Node))) { |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1967 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32); |
| 1968 | SDValue Reg = N0.getNode()->getOperand(0); |
| 1969 | |
| 1970 | // Extract the 32-bit subregister. |
Jakob Stoklund Olesen | 3458e9e | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 1971 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl, |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1972 | MVT::i32, Reg); |
| 1973 | |
| 1974 | // Emit a testl. |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1975 | return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | break; |
| 1979 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 1982 | SDNode *ResNode = SelectCode(Node); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 1984 | DEBUG(dbgs() << "=> "; |
| 1985 | if (ResNode == NULL || ResNode == Node) |
| 1986 | Node->dump(CurDAG); |
| 1987 | else |
| 1988 | ResNode->dump(CurDAG); |
| 1989 | dbgs() << '\n'); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1990 | |
| 1991 | return ResNode; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1994 | bool X86DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1995 | SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1996 | std::vector<SDValue> &OutOps) { |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1997 | SDValue Op0, Op1, Op2, Op3, Op4; |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1998 | switch (ConstraintCode) { |
| 1999 | case 'o': // offsetable ?? |
| 2000 | case 'v': // not offsetable ?? |
| 2001 | default: return true; |
| 2002 | case 'm': // memory |
Chris Lattner | b86faa1 | 2010-09-21 22:07:31 +0000 | [diff] [blame^] | 2003 | if (!SelectAddr(0, Op, Op0, Op1, Op2, Op3, Op4)) |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 2004 | return true; |
| 2005 | break; |
| 2006 | } |
| 2007 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 2008 | OutOps.push_back(Op0); |
| 2009 | OutOps.push_back(Op1); |
| 2010 | OutOps.push_back(Op2); |
| 2011 | OutOps.push_back(Op3); |
Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 2012 | OutOps.push_back(Op4); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 2013 | return false; |
| 2014 | } |
| 2015 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2016 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 2017 | /// X86-specific DAG, ready for instruction scheduling. |
| 2018 | /// |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 2019 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, |
| 2020 | llvm::CodeGenOpt::Level OptLevel) { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 2021 | return new X86DAGToDAGISel(TM, OptLevel); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2022 | } |