blob: 06b801f62f9e7fb34410baf8b3abb4f06635de5a [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
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 Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000071 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000072 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000075 const char *ES;
76 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000077 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078
79 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000080 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
Rafael Espindola094fad32009-04-08 21:14:34 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000082 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000083
84 bool hasSymbolicDisplacement() const {
85 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
86 }
87
Dale Johannesen50dd1d02008-08-11 23:46:25 +000088 void dump() {
89 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000090 cerr << "Base.Reg ";
91 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
92 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000093 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
94 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000095 cerr << "IndexReg ";
96 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
97 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000098 cerr << " Disp " << Disp << "\n";
99 cerr << "GV "; if (GV) GV->dump();
100 else cerr << "nul";
101 cerr << " CP "; if (CP) CP->dump();
102 else cerr << "nul";
103 cerr << "\n";
104 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
105 cerr << " JT" << JT << " Align" << Align << "\n";
106 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000107 };
108}
109
110namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000111 //===--------------------------------------------------------------------===//
112 /// ISel - X86 specific code to select X86 machine instructions for
113 /// SelectionDAG operations.
114 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000115 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 /// TM - Keep a reference to X86TargetMachine.
117 ///
118 X86TargetMachine &TM;
119
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120 /// X86Lowering - This object fully describes how to lower LLVM code to an
121 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000122 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000123
124 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
125 /// make the right decision when generating code for different targets.
126 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000127
Evan Chengdb8d56b2008-06-30 20:45:06 +0000128 /// CurBB - Current BB being isel'd.
129 ///
130 MachineBasicBlock *CurBB;
131
Evan Chengb7a75a52008-09-26 23:41:32 +0000132 /// OptForSize - If true, selector should try to optimize for code size
133 /// instead of performance.
134 bool OptForSize;
135
Chris Lattnerc961eea2005-11-16 01:54:32 +0000136 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000137 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman79ce2762009-01-15 19:20:50 +0000138 : SelectionDAGISel(tm, fast),
Dan Gohman38217fe2008-10-03 16:17:33 +0000139 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000140 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000141 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142
143 virtual const char *getPassName() const {
144 return "X86 DAG->DAG Instruction Selection";
145 }
146
Evan Chengdb8d56b2008-06-30 20:45:06 +0000147 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000148 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000149 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000150
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000151 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
152
Evan Cheng884c70c2008-11-27 00:49:46 +0000153 virtual
154 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000155
Chris Lattnerc961eea2005-11-16 01:54:32 +0000156// Include the pieces autogenerated from the target description.
157#include "X86GenDAGISel.inc"
158
159 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000160 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000161 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162
Rafael Espindola094fad32009-04-08 21:14:34 +0000163 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
164 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000165 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000166 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000167 unsigned Depth = 0);
168 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000169 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000170 SDValue &Scale, SDValue &Index, SDValue &Disp,
171 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000172 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
175 SDValue N, SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000177 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue &InChain, SDValue &OutChain);
179 bool TryFoldLoad(SDValue P, SDValue N,
180 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000181 SDValue &Index, SDValue &Disp,
182 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000183 void PreprocessForRMW();
184 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000185
Chris Lattnerc0bad572006-06-08 18:03:49 +0000186 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
187 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000188 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000189 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000190 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000191
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000192 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
193
Dan Gohman475871a2008-07-27 21:46:04 +0000194 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
195 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000196 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000197 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
199 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000200 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000201 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 // These are 32-bit even in 64-bit mode since RIP relative offset
203 // is 32-bit.
204 if (AM.GV)
205 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
206 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000207 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
208 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000210 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 else if (AM.JT != -1)
212 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
213 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000214 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000215
216 if (AM.Segment.getNode())
217 Segment = AM.Segment;
218 else
219 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000220 }
221
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000222 /// getI8Imm - Return a target constant with the specified value, of type
223 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000224 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000225 return CurDAG->getTargetConstant(Imm, MVT::i8);
226 }
227
Chris Lattnerc961eea2005-11-16 01:54:32 +0000228 /// getI16Imm - Return a target constant with the specified value, of type
229 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000230 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000231 return CurDAG->getTargetConstant(Imm, MVT::i16);
232 }
233
234 /// getI32Imm - Return a target constant with the specified value, of type
235 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000236 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000237 return CurDAG->getTargetConstant(Imm, MVT::i32);
238 }
Evan Chengf597dc72006-02-10 22:24:32 +0000239
Dan Gohman8b746962008-09-23 18:22:58 +0000240 /// getGlobalBaseReg - Return an SDNode that returns the value of
241 /// the global base register. Output instructions required to
242 /// initialize the global base register, if necessary.
243 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000244 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000245
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000246 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
247 /// truncate of the specified operand to i8. This can be done with tablegen,
248 /// except that this code uses MVT::Flag in a tricky way that happens to
249 /// improve scheduling in some cases.
250 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000251
Evan Cheng23addc02006-02-10 22:46:26 +0000252#ifndef NDEBUG
253 unsigned Indent;
254#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000255 };
256}
257
Gabor Greif93c53e52008-08-31 15:37:04 +0000258/// findFlagUse - Return use of MVT::Flag value produced by the specified
259/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000260///
Evan Chenga275ecb2006-10-10 01:46:56 +0000261static SDNode *findFlagUse(SDNode *N) {
262 unsigned FlagResNo = N->getNumValues()-1;
263 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohmane8ecf482009-01-27 02:37:43 +0000264 SDUse &Use = I.getUse();
265 if (Use.getResNo() == FlagResNo)
266 return Use.getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000267 }
268 return NULL;
269}
270
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000271/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
272/// This function recursively traverses up the operand chain, ignoring
273/// certain nodes.
274static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
275 SDNode *Root,
Evan Chengcdda25d2008-04-25 08:22:20 +0000276 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000277 if (Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000278 !Visited.insert(Use))
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000279 return false;
280
281 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000282 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000283 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000284 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000285 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000286 assert(N != Root);
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000287 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000288 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000289
290 // Traverse up the operand chain.
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000291 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
292 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000293 }
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000294 return false;
Evan Chengf4b4c412006-08-08 00:31:00 +0000295}
296
Evan Cheng27e1fe92006-10-14 08:33:25 +0000297/// isNonImmUse - Start searching from Root up the DAG to check is Def can
298/// be reached. Return true if that's the case. However, ignore direct uses
299/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng884c70c2008-11-27 00:49:46 +0000300/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
301/// case).
Evan Cheng27e1fe92006-10-14 08:33:25 +0000302/// FIXME: to be really generic, we should allow direct use by any node
303/// that is being folded. But realisticly since we only fold loads which
304/// have one non-chain use, we only need to watch out for load/op/store
305/// and load/op/cmp case where the root (store / cmp) may reach the load via
306/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000307static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000308 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000309 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000310}
311
312
Evan Cheng884c70c2008-11-27 00:49:46 +0000313bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
314 SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000315 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000316
Evan Cheng884c70c2008-11-27 00:49:46 +0000317 if (U == Root)
318 switch (U->getOpcode()) {
319 default: break;
320 case ISD::ADD:
321 case ISD::ADDC:
322 case ISD::ADDE:
323 case ISD::AND:
324 case ISD::OR:
325 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000326 SDValue Op1 = U->getOperand(1);
327
Evan Cheng884c70c2008-11-27 00:49:46 +0000328 // If the other operand is a 8-bit immediate we should fold the immediate
329 // instead. This reduces code size.
330 // e.g.
331 // movl 4(%esp), %eax
332 // addl $4, %eax
333 // vs.
334 // movl $4, %eax
335 // addl 4(%esp), %eax
336 // The former is 2 bytes shorter. In case where the increment is 1, then
337 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000338 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000339 if (Imm->getAPIntValue().isSignedIntN(8))
340 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000341
342 // If the other operand is a TLS address, we should fold it instead.
343 // This produces
344 // movl %gs:0, %eax
345 // leal i@NTPOFF(%eax), %eax
346 // instead of
347 // movl $i@NTPOFF, %eax
348 // addl %gs:0, %eax
349 // if the block also has an access to a second TLS address this will save
350 // a load.
351 // FIXME: This is probably also true for non TLS addresses.
352 if (Op1.getOpcode() == X86ISD::Wrapper) {
353 SDValue Val = Op1.getOperand(0);
354 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
355 return false;
356 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000357 }
358 }
359
Dan Gohman682d5a82008-09-17 01:39:10 +0000360 // If Root use can somehow reach N through a path that that doesn't contain
361 // U then folding N would create a cycle. e.g. In the following
362 // diagram, Root can reach N through X. If N is folded into into Root, then
363 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000364 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000365 // [N*] //
366 // ^ ^ //
367 // / \ //
368 // [U*] [X]? //
369 // ^ ^ //
370 // \ / //
371 // \ / //
372 // [Root*] //
373 //
374 // * indicates nodes to be folded together.
375 //
376 // If Root produces a flag, then it gets (even more) interesting. Since it
377 // will be "glued" together with its flag use in the scheduler, we need to
378 // check if it might reach N.
379 //
380 // [N*] //
381 // ^ ^ //
382 // / \ //
383 // [U*] [X]? //
384 // ^ ^ //
385 // \ \ //
386 // \ | //
387 // [Root*] | //
388 // ^ | //
389 // f | //
390 // | / //
391 // [Y] / //
392 // ^ / //
393 // f / //
394 // | / //
395 // [FU] //
396 //
397 // If FU (flag use) indirectly reaches N (the load), and Root folds N
398 // (call it Fold), then X is a predecessor of FU and a successor of
399 // Fold. But since Fold and FU are flagged together, this will create
400 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000401
Duncan Sands83ec4b62008-06-06 12:08:01 +0000402 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000403 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000404 SDNode *FU = findFlagUse(Root);
405 if (FU == NULL)
406 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000407 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000408 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000409 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000410
Dan Gohman682d5a82008-09-17 01:39:10 +0000411 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000412}
413
Evan Cheng70e674e2006-08-28 20:10:17 +0000414/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
415/// and move load below the TokenFactor. Replace store's chain operand with
416/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000417static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000418 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000419 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000420 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
421 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000422 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000423 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000424 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000425 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
426 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
427 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
428 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000429}
430
Evan Chengcd0baf22008-05-23 21:23:16 +0000431/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
432///
Dan Gohman475871a2008-07-27 21:46:04 +0000433static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
434 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000435 if (N.getOpcode() == ISD::BIT_CONVERT)
436 N = N.getOperand(0);
437
438 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
439 if (!LD || LD->isVolatile())
440 return false;
441 if (LD->getAddressingMode() != ISD::UNINDEXED)
442 return false;
443
444 ISD::LoadExtType ExtType = LD->getExtensionType();
445 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
446 return false;
447
448 if (N.hasOneUse() &&
449 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000450 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000451 Load = N;
452 return true;
453 }
454 return false;
455}
456
Evan Chengab6c3bb2008-08-25 21:27:18 +0000457/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
458/// operand and move load below the call's chain operand.
459static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000460 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000461 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000462 SDValue Chain = CallSeqStart.getOperand(0);
463 if (Chain.getNode() == Load.getNode())
464 Ops.push_back(Load.getOperand(0));
465 else {
466 assert(Chain.getOpcode() == ISD::TokenFactor &&
467 "Unexpected CallSeqStart chain operand");
468 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
469 if (Chain.getOperand(i).getNode() == Load.getNode())
470 Ops.push_back(Load.getOperand(0));
471 else
472 Ops.push_back(Chain.getOperand(i));
473 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000474 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
475 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000476 Ops.clear();
477 Ops.push_back(NewChain);
478 }
479 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
480 Ops.push_back(CallSeqStart.getOperand(i));
481 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000482 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
483 Load.getOperand(1), Load.getOperand(2));
484 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000485 Ops.push_back(SDValue(Load.getNode(), 1));
486 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000487 Ops.push_back(Call.getOperand(i));
488 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
489}
490
491/// isCalleeLoad - Return true if call address is a load and it can be
492/// moved below CALLSEQ_START and the chains leading up to the call.
493/// Return the CALLSEQ_START by reference as a second output.
494static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000495 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000496 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000497 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000498 if (!LD ||
499 LD->isVolatile() ||
500 LD->getAddressingMode() != ISD::UNINDEXED ||
501 LD->getExtensionType() != ISD::NON_EXTLOAD)
502 return false;
503
504 // Now let's find the callseq_start.
505 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
506 if (!Chain.hasOneUse())
507 return false;
508 Chain = Chain.getOperand(0);
509 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000510
511 if (Chain.getOperand(0).getNode() == Callee.getNode())
512 return true;
513 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
514 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
515 return true;
516 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000517}
518
519
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000520/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
521/// This is only run if not in -fast mode (aka -O0).
522/// This allows the instruction selector to pick more read-modify-write
523/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000524///
525/// [Load chain]
526/// ^
527/// |
528/// [Load]
529/// ^ ^
530/// | |
531/// / \-
532/// / |
533/// [TokenFactor] [Op]
534/// ^ ^
535/// | |
536/// \ /
537/// \ /
538/// [Store]
539///
540/// The fact the store's chain operand != load's chain will prevent the
541/// (store (op (load))) instruction from being selected. We can transform it to:
542///
543/// [Load chain]
544/// ^
545/// |
546/// [TokenFactor]
547/// ^
548/// |
549/// [Load]
550/// ^ ^
551/// | |
552/// | \-
553/// | |
554/// | [Op]
555/// | ^
556/// | |
557/// \ /
558/// \ /
559/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000560void X86DAGToDAGISel::PreprocessForRMW() {
561 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
562 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000563 if (I->getOpcode() == X86ISD::CALL) {
564 /// Also try moving call address load from outside callseq_start to just
565 /// before the call to allow it to be folded.
566 ///
567 /// [Load chain]
568 /// ^
569 /// |
570 /// [Load]
571 /// ^ ^
572 /// | |
573 /// / \--
574 /// / |
575 ///[CALLSEQ_START] |
576 /// ^ |
577 /// | |
578 /// [LOAD/C2Reg] |
579 /// | |
580 /// \ /
581 /// \ /
582 /// [CALL]
583 SDValue Chain = I->getOperand(0);
584 SDValue Load = I->getOperand(1);
585 if (!isCalleeLoad(Load, Chain))
586 continue;
587 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
588 ++NumLoadMoved;
589 continue;
590 }
591
Evan Cheng8b2794a2006-10-13 21:14:26 +0000592 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000593 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000594 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000595
Gabor Greifba36cb52008-08-28 21:40:38 +0000596 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000597 continue;
598
Dan Gohman475871a2008-07-27 21:46:04 +0000599 SDValue N1 = I->getOperand(1);
600 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000601 if ((N1.getValueType().isFloatingPoint() &&
602 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000603 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000604 continue;
605
606 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000607 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000608 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000609 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000610 case ISD::ADD:
611 case ISD::MUL:
612 case ISD::AND:
613 case ISD::OR:
614 case ISD::XOR:
615 case ISD::ADDC:
616 case ISD::ADDE:
617 case ISD::VECTOR_SHUFFLE: {
618 SDValue N10 = N1.getOperand(0);
619 SDValue N11 = N1.getOperand(1);
620 RModW = isRMWLoad(N10, Chain, N2, Load);
621 if (!RModW)
622 RModW = isRMWLoad(N11, Chain, N2, Load);
623 break;
624 }
625 case ISD::SUB:
626 case ISD::SHL:
627 case ISD::SRA:
628 case ISD::SRL:
629 case ISD::ROTL:
630 case ISD::ROTR:
631 case ISD::SUBC:
632 case ISD::SUBE:
633 case X86ISD::SHLD:
634 case X86ISD::SHRD: {
635 SDValue N10 = N1.getOperand(0);
636 RModW = isRMWLoad(N10, Chain, N2, Load);
637 break;
638 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000639 }
640
Evan Cheng82a35b32006-08-29 06:44:17 +0000641 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000642 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000643 ++NumLoadMoved;
644 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000645 }
646}
647
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000648
649/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
650/// nodes that target the FP stack to be store and load to the stack. This is a
651/// gross hack. We would like to simply mark these as being illegal, but when
652/// we do that, legalize produces these when it expands calls, then expands
653/// these in the same legalize pass. We would like dag combine to be able to
654/// hack on these between the call expansion and the node legalization. As such
655/// this pass basically does "really late" legalization of these inline with the
656/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000657void X86DAGToDAGISel::PreprocessForFPConvert() {
658 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
659 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000660 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
661 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
662 continue;
663
664 // If the source and destination are SSE registers, then this is a legal
665 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000666 MVT SrcVT = N->getOperand(0).getValueType();
667 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000668 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
669 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
670 if (SrcIsSSE && DstIsSSE)
671 continue;
672
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000673 if (!SrcIsSSE && !DstIsSSE) {
674 // If this is an FPStack extension, it is a noop.
675 if (N->getOpcode() == ISD::FP_EXTEND)
676 continue;
677 // If this is a value-preserving FPStack truncation, it is a noop.
678 if (N->getConstantOperandVal(1))
679 continue;
680 }
681
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000682 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
683 // FPStack has extload and truncstore. SSE can fold direct loads into other
684 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000686 if (N->getOpcode() == ISD::FP_ROUND)
687 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
688 else
689 MemVT = SrcIsSSE ? SrcVT : DstVT;
690
Dan Gohmanf350b272008-08-23 02:25:05 +0000691 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000692 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000693
694 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000695 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000696 N->getOperand(0),
697 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000698 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000699 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000700
701 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
702 // extload we created. This will cause general havok on the dag because
703 // anything below the conversion could be folded into other existing nodes.
704 // To avoid invalidating 'I', back it up to the convert node.
705 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000706 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000707
708 // Now that we did that, the node is dead. Increment the iterator to the
709 // next node to process, then delete N.
710 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000711 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000712 }
713}
714
Chris Lattnerc961eea2005-11-16 01:54:32 +0000715/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
716/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000717void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000718 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000719 const Function *F = CurDAG->getMachineFunction().getFunction();
720 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000721
Evan Chengdb8d56b2008-06-30 20:45:06 +0000722 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000723 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000724 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000725
726 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000727 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000728
Chris Lattnerc961eea2005-11-16 01:54:32 +0000729 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000730#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000731 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000732 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000733#endif
David Greene8ad4c002008-10-27 21:56:29 +0000734 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000735#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000736 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000737#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000738
Dan Gohmanf350b272008-08-23 02:25:05 +0000739 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000740}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000741
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000742/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
743/// the main function.
744void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
745 MachineFrameInfo *MFI) {
746 const TargetInstrInfo *TII = TM.getInstrInfo();
747 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000748 BuildMI(BB, DebugLoc::getUnknownLoc(),
749 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000750}
751
752void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
753 // If this is main, emit special code for main.
754 MachineBasicBlock *BB = MF.begin();
755 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
756 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
757}
758
Rafael Espindola094fad32009-04-08 21:14:34 +0000759
760bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
761 X86ISelAddressMode &AM) {
762 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
763 SDValue Segment = N.getOperand(0);
764
765 if (AM.Segment.getNode() == 0) {
766 AM.Segment = Segment;
767 return false;
768 }
769
770 return true;
771}
772
773bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
774 // This optimization is valid because the GNU TLS model defines that
775 // gs:0 (or fs:0 on X86-64) contains its own address.
776 // For more information see http://people.redhat.com/drepper/tls.pdf
777
778 SDValue Address = N.getOperand(1);
779 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
780 !MatchSegmentBaseAddress (Address, AM))
781 return false;
782
783 return true;
784}
785
Rafael Espindola49a168d2009-04-12 21:55:03 +0000786bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
787 bool is64Bit = Subtarget->is64Bit();
788 DOUT << "Wrapper: 64bit " << is64Bit;
789 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Rafael Espindolab2157762009-04-12 23:00:38 +0000790
Rafael Espindola49a168d2009-04-12 21:55:03 +0000791 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Rafael Espindolab2157762009-04-12 23:00:38 +0000792 if (is64Bit && (TM.getCodeModel() != CodeModel::Small))
Rafael Espindola49a168d2009-04-12 21:55:03 +0000793 return true;
Rafael Espindolab2157762009-04-12 23:00:38 +0000794
795 // Base and index reg must be 0 in order to use rip as base.
796 bool canUsePICRel = !AM.Base.Reg.getNode() && !AM.IndexReg.getNode();
797 if (is64Bit && !canUsePICRel && TM.symbolicAddressesAreRIPRel())
798 return true;
799
Rafael Espindola49a168d2009-04-12 21:55:03 +0000800 if (AM.hasSymbolicDisplacement())
801 return true;
802 // If value is available in a register both base and index components have
803 // been picked, we can't fit the result available in the register in the
804 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
805
806 SDValue N0 = N.getOperand(0);
807 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
808 uint64_t Offset = G->getOffset();
809 if (!is64Bit || isInt32(AM.Disp + Offset)) {
810 GlobalValue *GV = G->getGlobal();
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000811 bool isRIPRel = TM.symbolicAddressesAreRIPRel();
812 if (N0.getOpcode() == llvm::ISD::TargetGlobalTLSAddress) {
813 TLSModel::Model model =
814 getTLSModel (GV, TM.getRelocationModel());
815 if (is64Bit && model == TLSModel::InitialExec)
816 isRIPRel = true;
817 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000818 AM.GV = GV;
819 AM.Disp += Offset;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000820 AM.isRIPRel = isRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000821 return false;
822 }
823 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
824 uint64_t Offset = CP->getOffset();
825 if (!is64Bit || isInt32(AM.Disp + Offset)) {
826 AM.CP = CP->getConstVal();
827 AM.Align = CP->getAlignment();
828 AM.Disp += Offset;
829 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
830 return false;
831 }
832 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
833 AM.ES = S->getSymbol();
834 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
835 return false;
836 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
837 AM.JT = J->getIndex();
838 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
839 return false;
840 }
841
842 return true;
843}
844
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845/// MatchAddress - Add the specified node to the specified addressing mode,
846/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000847/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000848bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000849 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000850 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000851 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000852 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000853 // Limit recursion.
854 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000855 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000856
Evan Cheng25ab6902006-09-08 06:48:29 +0000857 // RIP relative addressing: %rip + 32-bit displacement!
858 if (AM.isRIPRel) {
859 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000860 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000861 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000862 AM.Disp += Val;
863 return false;
864 }
865 }
866 return true;
867 }
868
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869 switch (N.getOpcode()) {
870 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000871 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000872 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000873 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000874 AM.Disp += Val;
875 return false;
876 }
877 break;
878 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000879
Rafael Espindola094fad32009-04-08 21:14:34 +0000880 case X86ISD::SegmentBaseAddress:
881 if (!MatchSegmentBaseAddress(N, AM))
882 return false;
883 break;
884
Rafael Espindola49a168d2009-04-12 21:55:03 +0000885 case X86ISD::Wrapper:
886 if (!MatchWrapper(N, AM))
887 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000888 break;
889
Rafael Espindola094fad32009-04-08 21:14:34 +0000890 case ISD::LOAD:
891 if (!MatchLoad(N, AM))
892 return false;
893 break;
894
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000895 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000896 if (AM.BaseType == X86ISelAddressMode::RegBase
897 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000898 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
899 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
900 return false;
901 }
902 break;
Evan Chengec693f72005-12-08 02:01:35 +0000903
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000904 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000905 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000906 break;
907
Gabor Greif93c53e52008-08-31 15:37:04 +0000908 if (ConstantSDNode
909 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000910 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000911 if (Val == 1 || Val == 2 || Val == 3) {
912 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000915 // Okay, we know that we have a scale by now. However, if the scaled
916 // value is an add of something and a constant, we can fold the
917 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
919 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
920 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000921 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000922 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000923 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000924 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000925 AM.Disp = Disp;
926 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000927 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000928 } else {
929 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000930 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000931 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000932 }
933 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000934 }
Evan Chengec693f72005-12-08 02:01:35 +0000935
Dan Gohman83688052007-10-22 20:22:24 +0000936 case ISD::SMUL_LOHI:
937 case ISD::UMUL_LOHI:
938 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000939 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000940 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000941 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000942 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000943 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000944 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000945 AM.Base.Reg.getNode() == 0 &&
946 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000947 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000948 if (ConstantSDNode
949 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000950 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
951 CN->getZExtValue() == 9) {
952 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000953
Gabor Greifba36cb52008-08-28 21:40:38 +0000954 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000956
957 // Okay, we know that we have a scale by now. However, if the scaled
958 // value is an add of something and a constant, we can fold the
959 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000960 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
961 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
962 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000963 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000964 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000965 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000966 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000967 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000968 AM.Disp = Disp;
969 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000970 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000971 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000972 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000973 }
974
975 AM.IndexReg = AM.Base.Reg = Reg;
976 return false;
977 }
Chris Lattner62412262007-02-04 20:18:17 +0000978 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000979 break;
980
Evan Cheng8e278262009-01-17 07:09:27 +0000981 case ISD::ADD: {
982 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +0000983 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
984 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000985 return false;
986 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +0000987 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
988 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000989 return false;
990 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000991
992 // If we couldn't fold both operands into the address at the same time,
993 // see if we can just put each operand into a register and fold at least
994 // the add.
995 if (AM.BaseType == X86ISelAddressMode::RegBase &&
996 !AM.Base.Reg.getNode() &&
997 !AM.IndexReg.getNode() &&
998 !AM.isRIPRel) {
999 AM.Base.Reg = N.getNode()->getOperand(0);
1000 AM.IndexReg = N.getNode()->getOperand(1);
1001 AM.Scale = 1;
1002 return false;
1003 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001004 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001005 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001006
Chris Lattner62412262007-02-04 20:18:17 +00001007 case ISD::OR:
1008 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001009 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1010 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001011 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001012 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +00001013 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001014 // Address could not have picked a GV address for the displacement.
1015 AM.GV == NULL &&
1016 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001017 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001018 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001019 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001020 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001021 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001022 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001023 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001024 }
1025 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001026
1027 case ISD::AND: {
1028 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1029 // allows us to fold the shift into this addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +00001030 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001031 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001032
Evan Cheng1314b002007-12-13 00:43:27 +00001033 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001034 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001035
1036 // Not when RIP is used as the base.
1037 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +00001038
1039 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1040 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1041 if (!C1 || !C2) break;
1042
1043 // Not likely to be profitable if either the AND or SHIFT node has more
1044 // than one use (unless all uses are for address computation). Besides,
1045 // isel mechanism requires their node ids to be reused.
1046 if (!N.hasOneUse() || !Shift.hasOneUse())
1047 break;
1048
1049 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001050 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001051 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1052 break;
1053
1054 // Get the new AND mask, this folds to a constant.
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001055 SDValue X = Shift.getOperand(0);
Dale Johannesend8392542009-02-03 21:48:12 +00001056 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001057 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001058 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1059 NewANDMask);
1060 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001061 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001062
1063 // Insert the new nodes into the topological ordering.
1064 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1065 CurDAG->RepositionNode(X.getNode(), C1);
1066 C1->setNodeId(X.getNode()->getNodeId());
1067 }
1068 if (NewANDMask.getNode()->getNodeId() == -1 ||
1069 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1070 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1071 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1072 }
1073 if (NewAND.getNode()->getNodeId() == -1 ||
1074 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1075 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1076 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1077 }
1078 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1079 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1080 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1081 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1082 }
1083
Dan Gohman7b8e9642008-10-13 20:52:04 +00001084 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001085
1086 AM.Scale = 1 << ShiftCst;
1087 AM.IndexReg = NewAND;
1088 return false;
1089 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001090 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001091
Rafael Espindola523249f2009-03-31 16:16:57 +00001092 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001093}
1094
1095/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1096/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001097bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001098 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001099 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001100 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001101 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001102 AM.IndexReg = N;
1103 AM.Scale = 1;
1104 return false;
1105 }
1106
1107 // Otherwise, we cannot select it.
1108 return true;
1109 }
1110
1111 // Default, generate it as a register.
1112 AM.BaseType = X86ISelAddressMode::RegBase;
1113 AM.Base.Reg = N;
1114 return false;
1115}
1116
Evan Chengec693f72005-12-08 02:01:35 +00001117/// SelectAddr - returns true if it is able pattern match an addressing mode.
1118/// It returns the operands which make up the maximal addressing mode it can
1119/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001120bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1121 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001122 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001123 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001124 bool Done = false;
1125 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1126 unsigned Opcode = N.getOpcode();
1127 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1128 Opcode != X86ISD::Wrapper) {
1129 // If we are able to fold N into addressing mode, then we'll allow it even
1130 // if N has multiple uses. In general, addressing computation is used as
1131 // addresses by all of its uses. But watch out for CopyToReg uses, that
1132 // means the address computation is liveout. It will be computed by a LEA
1133 // so we want to avoid computing the address twice.
1134 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1135 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1136 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001137 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001138 Done = true;
1139 break;
1140 }
1141 }
1142 }
1143 }
1144
1145 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001146 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001147
Duncan Sands83ec4b62008-06-06 12:08:01 +00001148 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001149 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001150 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001151 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001152 }
Evan Cheng8700e142006-01-11 06:09:51 +00001153
Gabor Greifba36cb52008-08-28 21:40:38 +00001154 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001155 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001156
Rafael Espindola094fad32009-04-08 21:14:34 +00001157 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001158 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001159}
1160
Chris Lattner3a7cd952006-10-07 21:55:32 +00001161/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1162/// match a load whose top elements are either undef or zeros. The load flavor
1163/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001164bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1165 SDValue N, SDValue &Base,
1166 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001167 SDValue &Disp, SDValue &Segment,
1168 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001169 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001170 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001171 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001172 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001173 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001174 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001175 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001176 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001177 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001178 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001179 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001180 return true;
1181 }
1182 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001183
1184 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001185 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001187 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001188 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001189 N.getOperand(0).getNode()->hasOneUse() &&
1190 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001191 N.getOperand(0).getOperand(0).hasOneUse()) {
1192 // Okay, this is a zero extending load. Fold it.
1193 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001194 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001195 return false;
1196 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001197 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001198 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001199 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001200 return false;
1201}
1202
1203
Evan Cheng51a9ed92006-02-25 10:09:08 +00001204/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1205/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001206bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1207 SDValue &Base, SDValue &Scale,
1208 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001209 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001210
1211 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1212 // segments.
1213 SDValue Copy = AM.Segment;
1214 SDValue T = CurDAG->getRegister(0, MVT::i32);
1215 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001216 if (MatchAddress(N, AM))
1217 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001218 assert (T == AM.Segment);
1219 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001220
Duncan Sands83ec4b62008-06-06 12:08:01 +00001221 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001222 unsigned Complexity = 0;
1223 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001225 Complexity = 1;
1226 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001227 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001228 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1229 Complexity = 4;
1230
Gabor Greifba36cb52008-08-28 21:40:38 +00001231 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001232 Complexity++;
1233 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001234 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001236 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1237 // a simple shift.
1238 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001239 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001240
1241 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1242 // to a LEA. This is determined with some expermentation but is by no means
1243 // optimal (especially for code size consideration). LEA is nice because of
1244 // its three-address nature. Tweak the cost function again when we can run
1245 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001246 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001247 // For X86-64, we should always use lea to materialize RIP relative
1248 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001249 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001250 Complexity = 4;
1251 else
1252 Complexity += 2;
1253 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001254
Gabor Greifba36cb52008-08-28 21:40:38 +00001255 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001256 Complexity++;
1257
1258 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001259 SDValue Segment;
1260 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001261 return true;
1262 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001263 return false;
1264}
1265
Dan Gohman475871a2008-07-27 21:46:04 +00001266bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1267 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001268 SDValue &Index, SDValue &Disp,
1269 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001271 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001272 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001273 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001274 return false;
1275}
1276
Dan Gohman8b746962008-09-23 18:22:58 +00001277/// getGlobalBaseReg - Return an SDNode that returns the value of
1278/// the global base register. Output instructions required to
1279/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001280///
Evan Cheng9ade2182006-08-26 05:34:46 +00001281SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001282 MachineFunction *MF = CurBB->getParent();
1283 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001285}
1286
Evan Chengb245d922006-05-20 01:36:52 +00001287static SDNode *FindCallStartFromCall(SDNode *Node) {
1288 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1289 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1290 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001291 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001292}
1293
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001294/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1295/// truncate of the specified operand to i8. This can be done with tablegen,
1296/// except that this code uses MVT::Flag in a tricky way that happens to
1297/// improve scheduling in some cases.
1298SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1299 assert(!Subtarget->is64Bit() &&
1300 "getTruncateTo8Bit is only needed on x86-32!");
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001301 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001302 DebugLoc dl = N0.getDebugLoc();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001303
1304 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1305 unsigned Opc;
1306 MVT N0VT = N0.getValueType();
1307 switch (N0VT.getSimpleVT()) {
1308 default: assert(0 && "Unknown truncate!");
1309 case MVT::i16:
1310 Opc = X86::MOV16to16_;
1311 break;
1312 case MVT::i32:
1313 Opc = X86::MOV32to32_;
1314 break;
1315 }
1316
1317 // The use of MVT::Flag here is not strictly accurate, but it helps
1318 // scheduling in some cases.
Dale Johannesend8392542009-02-03 21:48:12 +00001319 N0 = SDValue(CurDAG->getTargetNode(Opc, dl, N0VT, MVT::Flag, N0), 0);
1320 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001321 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001322}
1323
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001324SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1325 SDValue Chain = Node->getOperand(0);
1326 SDValue In1 = Node->getOperand(1);
1327 SDValue In2L = Node->getOperand(2);
1328 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001329 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1330 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001331 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001332 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001333 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001334 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1335 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001336 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001337}
Christopher Lambc59e5212007-08-10 21:48:46 +00001338
Dan Gohman475871a2008-07-27 21:46:04 +00001339SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001340 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001342 unsigned Opc, MOpc;
1343 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001344 DebugLoc dl = Node->getDebugLoc();
1345
Evan Chengf597dc72006-02-10 22:24:32 +00001346#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001347 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001348 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001349 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001350 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001351#endif
1352
Dan Gohmane8be6c62008-07-17 19:10:17 +00001353 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001354#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001355 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001356 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001357 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001358 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001359#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001360 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001361 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001362
Evan Cheng0114e942006-01-06 20:36:21 +00001363 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001364 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001365 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001366 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001367
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001368 case X86ISD::ATOMOR64_DAG:
1369 return SelectAtomic64(Node, X86::ATOMOR6432);
1370 case X86ISD::ATOMXOR64_DAG:
1371 return SelectAtomic64(Node, X86::ATOMXOR6432);
1372 case X86ISD::ATOMADD64_DAG:
1373 return SelectAtomic64(Node, X86::ATOMADD6432);
1374 case X86ISD::ATOMSUB64_DAG:
1375 return SelectAtomic64(Node, X86::ATOMSUB6432);
1376 case X86ISD::ATOMNAND64_DAG:
1377 return SelectAtomic64(Node, X86::ATOMNAND6432);
1378 case X86ISD::ATOMAND64_DAG:
1379 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001380 case X86ISD::ATOMSWAP64_DAG:
1381 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001382
Dan Gohman525178c2007-10-08 18:33:35 +00001383 case ISD::SMUL_LOHI:
1384 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001385 SDValue N0 = Node->getOperand(0);
1386 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001387
Dan Gohman525178c2007-10-08 18:33:35 +00001388 bool isSigned = Opcode == ISD::SMUL_LOHI;
1389 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001390 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001391 default: assert(0 && "Unsupported VT!");
1392 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1393 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1394 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001395 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001396 }
1397 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001398 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001399 default: assert(0 && "Unsupported VT!");
1400 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1401 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1402 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001403 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001404 }
1405
1406 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001407 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001408 default: assert(0 && "Unsupported VT!");
1409 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1410 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1411 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001412 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001413 }
1414
Rafael Espindola094fad32009-04-08 21:14:34 +00001415 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1416 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001417 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001418 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001419 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001420 if (foldedLoad)
1421 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001422 }
1423
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001425 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001426
1427 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001428 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1429 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001430 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001431 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001432 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001433 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001434 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001435 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001436 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001437 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001438 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001439 }
1440
Dan Gohman525178c2007-10-08 18:33:35 +00001441 // Copy the low half of the result, if it is needed.
1442 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001443 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001444 LoReg, NVT, InFlag);
1445 InFlag = Result.getValue(2);
1446 ReplaceUses(N.getValue(0), Result);
1447#ifndef NDEBUG
1448 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001449 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001450 DOUT << "\n";
1451#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001452 }
Dan Gohman525178c2007-10-08 18:33:35 +00001453 // Copy the high half of the result, if it is needed.
1454 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001455 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001456 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1457 // Prevent use of AH in a REX instruction by referencing AX instead.
1458 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001459 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001460 X86::AX, MVT::i16, InFlag);
1461 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001462 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1463 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001464 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001465 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001466 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001467 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001468 MVT::i8, Result, SRIdx), 0);
1469 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001470 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001471 HiReg, NVT, InFlag);
1472 InFlag = Result.getValue(2);
1473 }
1474 ReplaceUses(N.getValue(1), Result);
1475#ifndef NDEBUG
1476 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001477 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001478 DOUT << "\n";
1479#endif
1480 }
Evan Cheng34167212006-02-09 00:37:58 +00001481
Evan Chengf597dc72006-02-10 22:24:32 +00001482#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001483 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001484#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001485
Evan Cheng64a752f2006-08-11 09:08:15 +00001486 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001487 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001488
Dan Gohman525178c2007-10-08 18:33:35 +00001489 case ISD::SDIVREM:
1490 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue N0 = Node->getOperand(0);
1492 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001493
1494 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001495 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001496 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001497 default: assert(0 && "Unsupported VT!");
1498 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1499 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1500 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001501 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001502 }
1503 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001504 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001505 default: assert(0 && "Unsupported VT!");
1506 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1507 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1508 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001509 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001510 }
1511
1512 unsigned LoReg, HiReg;
1513 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001515 default: assert(0 && "Unsupported VT!");
1516 case MVT::i8:
1517 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001518 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001519 SExtOpcode = X86::CBW;
1520 break;
1521 case MVT::i16:
1522 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001523 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001524 SExtOpcode = X86::CWD;
1525 break;
1526 case MVT::i32:
1527 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001528 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001529 SExtOpcode = X86::CDQ;
1530 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001531 case MVT::i64:
1532 LoReg = X86::RAX; HiReg = X86::RDX;
1533 ClrOpcode = X86::MOV64r0;
1534 SExtOpcode = X86::CQO;
1535 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001536 }
1537
Rafael Espindola094fad32009-04-08 21:14:34 +00001538 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1539 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001540 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001541
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001543 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001544 // Special case for div8, just use a move with zero extension to AX to
1545 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001546 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1547 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1548 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001549 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001550 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001551 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001552 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001553 Chain = Move.getValue(1);
1554 ReplaceUses(N0.getValue(1), Chain);
1555 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001556 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001557 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001558 Chain = CurDAG->getEntryNode();
1559 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001560 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001561 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001562 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001563 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001564 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001565 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001566 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001567 // Sign extend the low part into the high part.
1568 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001569 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001570 } else {
1571 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001572 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1573 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001574 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001575 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001576 }
Evan Cheng948f3432006-01-06 23:19:29 +00001577 }
1578
1579 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001580 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1581 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001582 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001583 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001584 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001585 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001586 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001587 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001588 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001589 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001590 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001591 }
1592
Dan Gohmana37c9f72007-09-25 18:23:27 +00001593 // Copy the division (low) result, if it is needed.
1594 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001595 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001596 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001597 InFlag = Result.getValue(2);
1598 ReplaceUses(N.getValue(0), Result);
1599#ifndef NDEBUG
1600 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001602 DOUT << "\n";
1603#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001604 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001605 // Copy the remainder (high) result, if it is needed.
1606 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001608 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1609 // Prevent use of AH in a REX instruction by referencing AX instead.
1610 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001611 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001612 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001613 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001614 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1615 Result,
1616 CurDAG->getTargetConstant(8, MVT::i8)),
1617 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001618 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001619 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001620 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001621 MVT::i8, Result, SRIdx), 0);
1622 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001623 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001624 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001625 InFlag = Result.getValue(2);
1626 }
1627 ReplaceUses(N.getValue(1), Result);
1628#ifndef NDEBUG
1629 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001630 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001631 DOUT << "\n";
1632#endif
1633 }
Evan Chengf597dc72006-02-10 22:24:32 +00001634
1635#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001636 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001637#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001638
1639 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001640 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001641
Christopher Lambc59e5212007-08-10 21:48:46 +00001642 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001643 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001644 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1645 SDValue N0 = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001646
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001647 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1648 unsigned Opc = 0;
1649 switch (NVT.getSimpleVT()) {
1650 default: assert(0 && "Unknown sign_extend_inreg!");
1651 case MVT::i16:
1652 Opc = X86::MOVSX16rr8;
1653 break;
1654 case MVT::i32:
1655 Opc = X86::MOVSX32rr8;
1656 break;
1657 }
1658
Dale Johannesend8392542009-02-03 21:48:12 +00001659 SDNode *ResNode = CurDAG->getTargetNode(Opc, dl, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001660
1661#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001662 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001663 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001664 DOUT << "\n";
1665 DOUT << std::string(Indent-2, ' ') << "=> ";
1666 DEBUG(ResNode->dump(CurDAG));
1667 DOUT << "\n";
1668 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001669#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001670 return ResNode;
1671 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001672 break;
1673 }
1674
1675 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001676 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1677 SDValue Input = Node->getOperand(0);
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001678 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001679
Evan Cheng403be7e2006-05-08 08:01:26 +00001680#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001681 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001682 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001683 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001684 Indent -= 2;
1685#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001686 return ResNode;
1687 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001688 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001689 }
Evan Cheng851bc042008-06-17 02:01:22 +00001690
1691 case ISD::DECLARE: {
1692 // Handle DECLARE nodes here because the second operand may have been
1693 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001694 SDValue Chain = Node->getOperand(0);
1695 SDValue N1 = Node->getOperand(1);
1696 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001697 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001698
1699 // FIXME: We need to handle this for VLAs.
1700 if (!FINode) {
1701 ReplaceUses(N.getValue(0), Chain);
1702 return NULL;
1703 }
1704
Evan Chengfab83872008-06-18 02:48:27 +00001705 if (N2.getOpcode() == ISD::ADD &&
1706 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1707 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001708
1709 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1710 // somehow, just ignore it.
1711 if (N2.getOpcode() != X86ISD::Wrapper) {
1712 ReplaceUses(N.getValue(0), Chain);
1713 return NULL;
1714 }
Evan Chengf2accb52009-01-10 03:33:22 +00001715 GlobalAddressSDNode *GVNode =
1716 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001717 if (GVNode == 0) {
1718 ReplaceUses(N.getValue(0), Chain);
1719 return NULL;
1720 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001721 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1722 TLI.getPointerTy());
1723 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1724 TLI.getPointerTy());
1725 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001726 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001727 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001728 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001729 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001730 }
1731
Evan Cheng9ade2182006-08-26 05:34:46 +00001732 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001733
Evan Chengf597dc72006-02-10 22:24:32 +00001734#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001735 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001736 if (ResNode == NULL || ResNode == N.getNode())
1737 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001738 else
1739 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001740 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001741 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001742#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001743
1744 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001745}
1746
Chris Lattnerc0bad572006-06-08 18:03:49 +00001747bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001748SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001749 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001750 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001751 switch (ConstraintCode) {
1752 case 'o': // offsetable ??
1753 case 'v': // not offsetable ??
1754 default: return true;
1755 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001756 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001757 return true;
1758 break;
1759 }
1760
Evan Cheng04699902006-08-26 01:05:16 +00001761 OutOps.push_back(Op0);
1762 OutOps.push_back(Op1);
1763 OutOps.push_back(Op2);
1764 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001765 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001766 return false;
1767}
1768
Chris Lattnerc961eea2005-11-16 01:54:32 +00001769/// createX86ISelDag - This pass converts a legalized DAG into a
1770/// X86-specific DAG, ready for instruction scheduling.
1771///
Evan Chenge50794a2006-08-29 18:28:33 +00001772FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1773 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001774}