blob: 1b9572cc41266fe55a1f9801df72373fbe2eea07 [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);
Dan Gohman475871a2008-07-27 21:46:04 +0000165 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000166 unsigned Depth = 0);
167 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000168 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000169 SDValue &Scale, SDValue &Index, SDValue &Disp,
170 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000171 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
172 SDValue &Scale, SDValue &Index, SDValue &Disp);
173 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
174 SDValue N, SDValue &Base, SDValue &Scale,
175 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000176 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDValue &InChain, SDValue &OutChain);
178 bool TryFoldLoad(SDValue P, SDValue N,
179 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000180 SDValue &Index, SDValue &Disp,
181 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000182 void PreprocessForRMW();
183 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000184
Chris Lattnerc0bad572006-06-08 18:03:49 +0000185 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
186 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000187 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000188 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000189 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000190
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000191 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
192
Dan Gohman475871a2008-07-27 21:46:04 +0000193 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
194 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000195 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000196 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000197 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
198 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000199 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000200 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 // These are 32-bit even in 64-bit mode since RIP relative offset
202 // is 32-bit.
203 if (AM.GV)
204 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
205 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000206 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
207 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000209 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000210 else if (AM.JT != -1)
211 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
212 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000213 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000214
215 if (AM.Segment.getNode())
216 Segment = AM.Segment;
217 else
218 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000219 }
220
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000221 /// getI8Imm - Return a target constant with the specified value, of type
222 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000223 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000224 return CurDAG->getTargetConstant(Imm, MVT::i8);
225 }
226
Chris Lattnerc961eea2005-11-16 01:54:32 +0000227 /// getI16Imm - Return a target constant with the specified value, of type
228 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000229 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000230 return CurDAG->getTargetConstant(Imm, MVT::i16);
231 }
232
233 /// getI32Imm - Return a target constant with the specified value, of type
234 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000235 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000236 return CurDAG->getTargetConstant(Imm, MVT::i32);
237 }
Evan Chengf597dc72006-02-10 22:24:32 +0000238
Dan Gohman8b746962008-09-23 18:22:58 +0000239 /// getGlobalBaseReg - Return an SDNode that returns the value of
240 /// the global base register. Output instructions required to
241 /// initialize the global base register, if necessary.
242 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000243 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000244
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000245 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
246 /// truncate of the specified operand to i8. This can be done with tablegen,
247 /// except that this code uses MVT::Flag in a tricky way that happens to
248 /// improve scheduling in some cases.
249 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000250
Evan Cheng23addc02006-02-10 22:46:26 +0000251#ifndef NDEBUG
252 unsigned Indent;
253#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000254 };
255}
256
Gabor Greif93c53e52008-08-31 15:37:04 +0000257/// findFlagUse - Return use of MVT::Flag value produced by the specified
258/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000259///
Evan Chenga275ecb2006-10-10 01:46:56 +0000260static SDNode *findFlagUse(SDNode *N) {
261 unsigned FlagResNo = N->getNumValues()-1;
262 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohmane8ecf482009-01-27 02:37:43 +0000263 SDUse &Use = I.getUse();
264 if (Use.getResNo() == FlagResNo)
265 return Use.getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000266 }
267 return NULL;
268}
269
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000270/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
271/// This function recursively traverses up the operand chain, ignoring
272/// certain nodes.
273static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
274 SDNode *Root,
Evan Chengcdda25d2008-04-25 08:22:20 +0000275 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000276 if (Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000277 !Visited.insert(Use))
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000278 return false;
279
280 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000281 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000282 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000283 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000284 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000285 assert(N != Root);
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000286 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000287 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000288
289 // Traverse up the operand chain.
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000290 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
291 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000292 }
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000293 return false;
Evan Chengf4b4c412006-08-08 00:31:00 +0000294}
295
Evan Cheng27e1fe92006-10-14 08:33:25 +0000296/// isNonImmUse - Start searching from Root up the DAG to check is Def can
297/// be reached. Return true if that's the case. However, ignore direct uses
298/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng884c70c2008-11-27 00:49:46 +0000299/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
300/// case).
Evan Cheng27e1fe92006-10-14 08:33:25 +0000301/// FIXME: to be really generic, we should allow direct use by any node
302/// that is being folded. But realisticly since we only fold loads which
303/// have one non-chain use, we only need to watch out for load/op/store
304/// and load/op/cmp case where the root (store / cmp) may reach the load via
305/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000306static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000307 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000308 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000309}
310
311
Evan Cheng884c70c2008-11-27 00:49:46 +0000312bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
313 SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000314 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000315
Evan Cheng884c70c2008-11-27 00:49:46 +0000316 if (U == Root)
317 switch (U->getOpcode()) {
318 default: break;
319 case ISD::ADD:
320 case ISD::ADDC:
321 case ISD::ADDE:
322 case ISD::AND:
323 case ISD::OR:
324 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000325 SDValue Op1 = U->getOperand(1);
326
Evan Cheng884c70c2008-11-27 00:49:46 +0000327 // If the other operand is a 8-bit immediate we should fold the immediate
328 // instead. This reduces code size.
329 // e.g.
330 // movl 4(%esp), %eax
331 // addl $4, %eax
332 // vs.
333 // movl $4, %eax
334 // addl 4(%esp), %eax
335 // The former is 2 bytes shorter. In case where the increment is 1, then
336 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000337 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000338 if (Imm->getAPIntValue().isSignedIntN(8))
339 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000340
341 // If the other operand is a TLS address, we should fold it instead.
342 // This produces
343 // movl %gs:0, %eax
344 // leal i@NTPOFF(%eax), %eax
345 // instead of
346 // movl $i@NTPOFF, %eax
347 // addl %gs:0, %eax
348 // if the block also has an access to a second TLS address this will save
349 // a load.
350 // FIXME: This is probably also true for non TLS addresses.
351 if (Op1.getOpcode() == X86ISD::Wrapper) {
352 SDValue Val = Op1.getOperand(0);
353 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
354 return false;
355 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000356 }
357 }
358
Dan Gohman682d5a82008-09-17 01:39:10 +0000359 // If Root use can somehow reach N through a path that that doesn't contain
360 // U then folding N would create a cycle. e.g. In the following
361 // diagram, Root can reach N through X. If N is folded into into Root, then
362 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000363 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000364 // [N*] //
365 // ^ ^ //
366 // / \ //
367 // [U*] [X]? //
368 // ^ ^ //
369 // \ / //
370 // \ / //
371 // [Root*] //
372 //
373 // * indicates nodes to be folded together.
374 //
375 // If Root produces a flag, then it gets (even more) interesting. Since it
376 // will be "glued" together with its flag use in the scheduler, we need to
377 // check if it might reach N.
378 //
379 // [N*] //
380 // ^ ^ //
381 // / \ //
382 // [U*] [X]? //
383 // ^ ^ //
384 // \ \ //
385 // \ | //
386 // [Root*] | //
387 // ^ | //
388 // f | //
389 // | / //
390 // [Y] / //
391 // ^ / //
392 // f / //
393 // | / //
394 // [FU] //
395 //
396 // If FU (flag use) indirectly reaches N (the load), and Root folds N
397 // (call it Fold), then X is a predecessor of FU and a successor of
398 // Fold. But since Fold and FU are flagged together, this will create
399 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000400
Duncan Sands83ec4b62008-06-06 12:08:01 +0000401 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000402 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000403 SDNode *FU = findFlagUse(Root);
404 if (FU == NULL)
405 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000406 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000407 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000408 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000409
Dan Gohman682d5a82008-09-17 01:39:10 +0000410 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000411}
412
Evan Cheng70e674e2006-08-28 20:10:17 +0000413/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
414/// and move load below the TokenFactor. Replace store's chain operand with
415/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000416static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000417 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000419 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
420 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000421 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000422 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000423 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000424 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
425 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
426 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
427 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000428}
429
Evan Chengcd0baf22008-05-23 21:23:16 +0000430/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
431///
Dan Gohman475871a2008-07-27 21:46:04 +0000432static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
433 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000434 if (N.getOpcode() == ISD::BIT_CONVERT)
435 N = N.getOperand(0);
436
437 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
438 if (!LD || LD->isVolatile())
439 return false;
440 if (LD->getAddressingMode() != ISD::UNINDEXED)
441 return false;
442
443 ISD::LoadExtType ExtType = LD->getExtensionType();
444 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
445 return false;
446
447 if (N.hasOneUse() &&
448 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000449 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000450 Load = N;
451 return true;
452 }
453 return false;
454}
455
Evan Chengab6c3bb2008-08-25 21:27:18 +0000456/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
457/// operand and move load below the call's chain operand.
458static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000459 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000460 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000461 SDValue Chain = CallSeqStart.getOperand(0);
462 if (Chain.getNode() == Load.getNode())
463 Ops.push_back(Load.getOperand(0));
464 else {
465 assert(Chain.getOpcode() == ISD::TokenFactor &&
466 "Unexpected CallSeqStart chain operand");
467 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
468 if (Chain.getOperand(i).getNode() == Load.getNode())
469 Ops.push_back(Load.getOperand(0));
470 else
471 Ops.push_back(Chain.getOperand(i));
472 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000473 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
474 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000475 Ops.clear();
476 Ops.push_back(NewChain);
477 }
478 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
479 Ops.push_back(CallSeqStart.getOperand(i));
480 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000481 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
482 Load.getOperand(1), Load.getOperand(2));
483 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000484 Ops.push_back(SDValue(Load.getNode(), 1));
485 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000486 Ops.push_back(Call.getOperand(i));
487 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
488}
489
490/// isCalleeLoad - Return true if call address is a load and it can be
491/// moved below CALLSEQ_START and the chains leading up to the call.
492/// Return the CALLSEQ_START by reference as a second output.
493static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000494 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000495 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000497 if (!LD ||
498 LD->isVolatile() ||
499 LD->getAddressingMode() != ISD::UNINDEXED ||
500 LD->getExtensionType() != ISD::NON_EXTLOAD)
501 return false;
502
503 // Now let's find the callseq_start.
504 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
505 if (!Chain.hasOneUse())
506 return false;
507 Chain = Chain.getOperand(0);
508 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000509
510 if (Chain.getOperand(0).getNode() == Callee.getNode())
511 return true;
512 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
513 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
514 return true;
515 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000516}
517
518
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000519/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
520/// This is only run if not in -fast mode (aka -O0).
521/// This allows the instruction selector to pick more read-modify-write
522/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000523///
524/// [Load chain]
525/// ^
526/// |
527/// [Load]
528/// ^ ^
529/// | |
530/// / \-
531/// / |
532/// [TokenFactor] [Op]
533/// ^ ^
534/// | |
535/// \ /
536/// \ /
537/// [Store]
538///
539/// The fact the store's chain operand != load's chain will prevent the
540/// (store (op (load))) instruction from being selected. We can transform it to:
541///
542/// [Load chain]
543/// ^
544/// |
545/// [TokenFactor]
546/// ^
547/// |
548/// [Load]
549/// ^ ^
550/// | |
551/// | \-
552/// | |
553/// | [Op]
554/// | ^
555/// | |
556/// \ /
557/// \ /
558/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000559void X86DAGToDAGISel::PreprocessForRMW() {
560 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
561 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000562 if (I->getOpcode() == X86ISD::CALL) {
563 /// Also try moving call address load from outside callseq_start to just
564 /// before the call to allow it to be folded.
565 ///
566 /// [Load chain]
567 /// ^
568 /// |
569 /// [Load]
570 /// ^ ^
571 /// | |
572 /// / \--
573 /// / |
574 ///[CALLSEQ_START] |
575 /// ^ |
576 /// | |
577 /// [LOAD/C2Reg] |
578 /// | |
579 /// \ /
580 /// \ /
581 /// [CALL]
582 SDValue Chain = I->getOperand(0);
583 SDValue Load = I->getOperand(1);
584 if (!isCalleeLoad(Load, Chain))
585 continue;
586 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
587 ++NumLoadMoved;
588 continue;
589 }
590
Evan Cheng8b2794a2006-10-13 21:14:26 +0000591 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000592 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000593 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000594
Gabor Greifba36cb52008-08-28 21:40:38 +0000595 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000596 continue;
597
Dan Gohman475871a2008-07-27 21:46:04 +0000598 SDValue N1 = I->getOperand(1);
599 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000600 if ((N1.getValueType().isFloatingPoint() &&
601 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000602 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000603 continue;
604
605 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000606 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000607 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000608 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000609 case ISD::ADD:
610 case ISD::MUL:
611 case ISD::AND:
612 case ISD::OR:
613 case ISD::XOR:
614 case ISD::ADDC:
615 case ISD::ADDE:
616 case ISD::VECTOR_SHUFFLE: {
617 SDValue N10 = N1.getOperand(0);
618 SDValue N11 = N1.getOperand(1);
619 RModW = isRMWLoad(N10, Chain, N2, Load);
620 if (!RModW)
621 RModW = isRMWLoad(N11, Chain, N2, Load);
622 break;
623 }
624 case ISD::SUB:
625 case ISD::SHL:
626 case ISD::SRA:
627 case ISD::SRL:
628 case ISD::ROTL:
629 case ISD::ROTR:
630 case ISD::SUBC:
631 case ISD::SUBE:
632 case X86ISD::SHLD:
633 case X86ISD::SHRD: {
634 SDValue N10 = N1.getOperand(0);
635 RModW = isRMWLoad(N10, Chain, N2, Load);
636 break;
637 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000638 }
639
Evan Cheng82a35b32006-08-29 06:44:17 +0000640 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000641 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000642 ++NumLoadMoved;
643 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000644 }
645}
646
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000647
648/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
649/// nodes that target the FP stack to be store and load to the stack. This is a
650/// gross hack. We would like to simply mark these as being illegal, but when
651/// we do that, legalize produces these when it expands calls, then expands
652/// these in the same legalize pass. We would like dag combine to be able to
653/// hack on these between the call expansion and the node legalization. As such
654/// this pass basically does "really late" legalization of these inline with the
655/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000656void X86DAGToDAGISel::PreprocessForFPConvert() {
657 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
658 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000659 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
660 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
661 continue;
662
663 // If the source and destination are SSE registers, then this is a legal
664 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000665 MVT SrcVT = N->getOperand(0).getValueType();
666 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000667 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
668 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
669 if (SrcIsSSE && DstIsSSE)
670 continue;
671
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000672 if (!SrcIsSSE && !DstIsSSE) {
673 // If this is an FPStack extension, it is a noop.
674 if (N->getOpcode() == ISD::FP_EXTEND)
675 continue;
676 // If this is a value-preserving FPStack truncation, it is a noop.
677 if (N->getConstantOperandVal(1))
678 continue;
679 }
680
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000681 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
682 // FPStack has extload and truncstore. SSE can fold direct loads into other
683 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000684 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000685 if (N->getOpcode() == ISD::FP_ROUND)
686 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
687 else
688 MemVT = SrcIsSSE ? SrcVT : DstVT;
689
Dan Gohmanf350b272008-08-23 02:25:05 +0000690 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000691 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000692
693 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000694 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000695 N->getOperand(0),
696 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000697 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000698 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000699
700 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
701 // extload we created. This will cause general havok on the dag because
702 // anything below the conversion could be folded into other existing nodes.
703 // To avoid invalidating 'I', back it up to the convert node.
704 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000705 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000706
707 // Now that we did that, the node is dead. Increment the iterator to the
708 // next node to process, then delete N.
709 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000710 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000711 }
712}
713
Chris Lattnerc961eea2005-11-16 01:54:32 +0000714/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
715/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000716void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000717 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000718 const Function *F = CurDAG->getMachineFunction().getFunction();
719 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000720
Evan Chengdb8d56b2008-06-30 20:45:06 +0000721 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000722 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000723 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000724
725 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000726 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000727
Chris Lattnerc961eea2005-11-16 01:54:32 +0000728 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000729#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000730 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000731 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000732#endif
David Greene8ad4c002008-10-27 21:56:29 +0000733 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000734#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000735 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000736#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000737
Dan Gohmanf350b272008-08-23 02:25:05 +0000738 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000739}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000740
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000741/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
742/// the main function.
743void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
744 MachineFrameInfo *MFI) {
745 const TargetInstrInfo *TII = TM.getInstrInfo();
746 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000747 BuildMI(BB, DebugLoc::getUnknownLoc(),
748 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000749}
750
751void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
752 // If this is main, emit special code for main.
753 MachineBasicBlock *BB = MF.begin();
754 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
755 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
756}
757
Rafael Espindola094fad32009-04-08 21:14:34 +0000758
759bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
760 X86ISelAddressMode &AM) {
761 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
762 SDValue Segment = N.getOperand(0);
763
764 if (AM.Segment.getNode() == 0) {
765 AM.Segment = Segment;
766 return false;
767 }
768
769 return true;
770}
771
772bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
773 // This optimization is valid because the GNU TLS model defines that
774 // gs:0 (or fs:0 on X86-64) contains its own address.
775 // For more information see http://people.redhat.com/drepper/tls.pdf
776
777 SDValue Address = N.getOperand(1);
778 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
779 !MatchSegmentBaseAddress (Address, AM))
780 return false;
781
782 return true;
783}
784
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000785/// MatchAddress - Add the specified node to the specified addressing mode,
786/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000787/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000788bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000789 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000790 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000791 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000792 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000793 // Limit recursion.
794 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000795 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000796
Evan Cheng25ab6902006-09-08 06:48:29 +0000797 // RIP relative addressing: %rip + 32-bit displacement!
798 if (AM.isRIPRel) {
799 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000800 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000801 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000802 AM.Disp += Val;
803 return false;
804 }
805 }
806 return true;
807 }
808
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000809 switch (N.getOpcode()) {
810 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000811 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000812 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000813 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000814 AM.Disp += Val;
815 return false;
816 }
817 break;
818 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000819
Rafael Espindola094fad32009-04-08 21:14:34 +0000820 case X86ISD::SegmentBaseAddress:
821 if (!MatchSegmentBaseAddress(N, AM))
822 return false;
823 break;
824
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000825 case X86ISD::Wrapper: {
Dan Gohman6520e202008-10-18 02:06:02 +0000826 DOUT << "Wrapper: 64bit " << is64Bit;
827 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Evan Cheng0085a282006-11-30 21:55:46 +0000828 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000829 // Also, base and index reg must be 0 in order to use rip as base.
830 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000832 break;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000833 if (AM.hasSymbolicDisplacement())
Evan Cheng28b514392006-12-05 19:50:18 +0000834 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000835 // If value is available in a register both base and index components have
836 // been picked, we can't fit the result available in the register in the
837 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000838 {
Dan Gohman475871a2008-07-27 21:46:04 +0000839 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000840 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000841 uint64_t Offset = G->getOffset();
842 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000843 GlobalValue *GV = G->getGlobal();
844 AM.GV = GV;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000845 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000846 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
847 return false;
848 }
Evan Cheng28b514392006-12-05 19:50:18 +0000849 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000850 uint64_t Offset = CP->getOffset();
851 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000852 AM.CP = CP->getConstVal();
853 AM.Align = CP->getAlignment();
Dan Gohman27cae7b2008-11-11 15:52:29 +0000854 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000855 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
856 return false;
857 }
Bill Wendling056292f2008-09-16 21:48:12 +0000858 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000859 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000860 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000861 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000862 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000863 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000864 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000865 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000866 }
867 }
868 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000869 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000870
Rafael Espindola094fad32009-04-08 21:14:34 +0000871 case ISD::LOAD:
872 if (!MatchLoad(N, AM))
873 return false;
874 break;
875
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000876 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000877 if (AM.BaseType == X86ISelAddressMode::RegBase
878 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000879 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
880 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
881 return false;
882 }
883 break;
Evan Chengec693f72005-12-08 02:01:35 +0000884
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000885 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000886 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000887 break;
888
Gabor Greif93c53e52008-08-31 15:37:04 +0000889 if (ConstantSDNode
890 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000891 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000892 if (Val == 1 || Val == 2 || Val == 3) {
893 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000894 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000895
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000896 // Okay, we know that we have a scale by now. However, if the scaled
897 // value is an add of something and a constant, we can fold the
898 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000899 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
900 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
901 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000902 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000903 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000904 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000905 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000906 AM.Disp = Disp;
907 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000908 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000909 } else {
910 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000911 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000912 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000913 }
914 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000915 }
Evan Chengec693f72005-12-08 02:01:35 +0000916
Dan Gohman83688052007-10-22 20:22:24 +0000917 case ISD::SMUL_LOHI:
918 case ISD::UMUL_LOHI:
919 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000920 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000921 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000922 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000923 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000924 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000925 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000926 AM.Base.Reg.getNode() == 0 &&
927 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000928 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000929 if (ConstantSDNode
930 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000931 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
932 CN->getZExtValue() == 9) {
933 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000934
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000937
938 // Okay, we know that we have a scale by now. However, if the scaled
939 // value is an add of something and a constant, we can fold the
940 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
942 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
943 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000944 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000945 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000946 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000947 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000948 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000949 AM.Disp = Disp;
950 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000951 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000952 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000953 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000954 }
955
956 AM.IndexReg = AM.Base.Reg = Reg;
957 return false;
958 }
Chris Lattner62412262007-02-04 20:18:17 +0000959 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000960 break;
961
Evan Cheng8e278262009-01-17 07:09:27 +0000962 case ISD::ADD: {
963 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +0000964 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
965 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000966 return false;
967 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +0000968 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
969 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000970 return false;
971 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000972
973 // If we couldn't fold both operands into the address at the same time,
974 // see if we can just put each operand into a register and fold at least
975 // the add.
976 if (AM.BaseType == X86ISelAddressMode::RegBase &&
977 !AM.Base.Reg.getNode() &&
978 !AM.IndexReg.getNode() &&
979 !AM.isRIPRel) {
980 AM.Base.Reg = N.getNode()->getOperand(0);
981 AM.IndexReg = N.getNode()->getOperand(1);
982 AM.Scale = 1;
983 return false;
984 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000985 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000986 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000987
Chris Lattner62412262007-02-04 20:18:17 +0000988 case ISD::OR:
989 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000990 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
991 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000992 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000993 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +0000994 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000995 // Address could not have picked a GV address for the displacement.
996 AM.GV == NULL &&
997 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000998 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000999 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001000 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001001 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001002 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001003 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001004 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001005 }
1006 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001007
1008 case ISD::AND: {
1009 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1010 // allows us to fold the shift into this addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +00001011 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001012 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001013
Evan Cheng1314b002007-12-13 00:43:27 +00001014 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001015 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001016
1017 // Not when RIP is used as the base.
1018 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +00001019
1020 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1021 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1022 if (!C1 || !C2) break;
1023
1024 // Not likely to be profitable if either the AND or SHIFT node has more
1025 // than one use (unless all uses are for address computation). Besides,
1026 // isel mechanism requires their node ids to be reused.
1027 if (!N.hasOneUse() || !Shift.hasOneUse())
1028 break;
1029
1030 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001031 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001032 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1033 break;
1034
1035 // Get the new AND mask, this folds to a constant.
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001036 SDValue X = Shift.getOperand(0);
Dale Johannesend8392542009-02-03 21:48:12 +00001037 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001038 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001039 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1040 NewANDMask);
1041 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001042 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001043
1044 // Insert the new nodes into the topological ordering.
1045 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1046 CurDAG->RepositionNode(X.getNode(), C1);
1047 C1->setNodeId(X.getNode()->getNodeId());
1048 }
1049 if (NewANDMask.getNode()->getNodeId() == -1 ||
1050 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1051 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1052 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1053 }
1054 if (NewAND.getNode()->getNodeId() == -1 ||
1055 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1056 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1057 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1058 }
1059 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1060 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1061 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1062 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1063 }
1064
Dan Gohman7b8e9642008-10-13 20:52:04 +00001065 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001066
1067 AM.Scale = 1 << ShiftCst;
1068 AM.IndexReg = NewAND;
1069 return false;
1070 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001071 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001072
Rafael Espindola523249f2009-03-31 16:16:57 +00001073 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001074}
1075
1076/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1077/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001078bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001079 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001081 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001083 AM.IndexReg = N;
1084 AM.Scale = 1;
1085 return false;
1086 }
1087
1088 // Otherwise, we cannot select it.
1089 return true;
1090 }
1091
1092 // Default, generate it as a register.
1093 AM.BaseType = X86ISelAddressMode::RegBase;
1094 AM.Base.Reg = N;
1095 return false;
1096}
1097
Evan Chengec693f72005-12-08 02:01:35 +00001098/// SelectAddr - returns true if it is able pattern match an addressing mode.
1099/// It returns the operands which make up the maximal addressing mode it can
1100/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001101bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1102 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001103 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001104 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001105 bool Done = false;
1106 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1107 unsigned Opcode = N.getOpcode();
1108 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1109 Opcode != X86ISD::Wrapper) {
1110 // If we are able to fold N into addressing mode, then we'll allow it even
1111 // if N has multiple uses. In general, addressing computation is used as
1112 // addresses by all of its uses. But watch out for CopyToReg uses, that
1113 // means the address computation is liveout. It will be computed by a LEA
1114 // so we want to avoid computing the address twice.
1115 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1116 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1117 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001118 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001119 Done = true;
1120 break;
1121 }
1122 }
1123 }
1124 }
1125
1126 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001127 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001128
Duncan Sands83ec4b62008-06-06 12:08:01 +00001129 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001130 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001132 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001133 }
Evan Cheng8700e142006-01-11 06:09:51 +00001134
Gabor Greifba36cb52008-08-28 21:40:38 +00001135 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001136 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001137
Rafael Espindola094fad32009-04-08 21:14:34 +00001138 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001139 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001140}
1141
Chris Lattner3a7cd952006-10-07 21:55:32 +00001142/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1143/// match a load whose top elements are either undef or zeros. The load flavor
1144/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001145bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1146 SDValue N, SDValue &Base,
1147 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001148 SDValue &Disp, SDValue &Segment,
1149 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001150 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001151 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001152 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001153 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001154 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001155 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001156 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001157 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001158 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001159 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001160 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001161 return true;
1162 }
1163 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001164
1165 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001166 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001167 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001168 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001169 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001170 N.getOperand(0).getNode()->hasOneUse() &&
1171 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001172 N.getOperand(0).getOperand(0).hasOneUse()) {
1173 // Okay, this is a zero extending load. Fold it.
1174 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001175 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001176 return false;
1177 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001178 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001179 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001180 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001181 return false;
1182}
1183
1184
Evan Cheng51a9ed92006-02-25 10:09:08 +00001185/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1186/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001187bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1188 SDValue &Base, SDValue &Scale,
1189 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001190 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001191
1192 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1193 // segments.
1194 SDValue Copy = AM.Segment;
1195 SDValue T = CurDAG->getRegister(0, MVT::i32);
1196 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001197 if (MatchAddress(N, AM))
1198 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001199 assert (T == AM.Segment);
1200 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001201
Duncan Sands83ec4b62008-06-06 12:08:01 +00001202 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001203 unsigned Complexity = 0;
1204 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001205 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001206 Complexity = 1;
1207 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001208 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001209 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1210 Complexity = 4;
1211
Gabor Greifba36cb52008-08-28 21:40:38 +00001212 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001213 Complexity++;
1214 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001215 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001216
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001217 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1218 // a simple shift.
1219 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001220 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001221
1222 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1223 // to a LEA. This is determined with some expermentation but is by no means
1224 // optimal (especially for code size consideration). LEA is nice because of
1225 // its three-address nature. Tweak the cost function again when we can run
1226 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001227 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001228 // For X86-64, we should always use lea to materialize RIP relative
1229 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001230 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001231 Complexity = 4;
1232 else
1233 Complexity += 2;
1234 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235
Gabor Greifba36cb52008-08-28 21:40:38 +00001236 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001237 Complexity++;
1238
1239 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001240 SDValue Segment;
1241 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001242 return true;
1243 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001244 return false;
1245}
1246
Dan Gohman475871a2008-07-27 21:46:04 +00001247bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1248 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001249 SDValue &Index, SDValue &Disp,
1250 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001251 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001252 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001253 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001254 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001255 return false;
1256}
1257
Dan Gohman8b746962008-09-23 18:22:58 +00001258/// getGlobalBaseReg - Return an SDNode that returns the value of
1259/// the global base register. Output instructions required to
1260/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001261///
Evan Cheng9ade2182006-08-26 05:34:46 +00001262SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001263 MachineFunction *MF = CurBB->getParent();
1264 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001265 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001266}
1267
Evan Chengb245d922006-05-20 01:36:52 +00001268static SDNode *FindCallStartFromCall(SDNode *Node) {
1269 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1270 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1271 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001272 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001273}
1274
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001275/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1276/// truncate of the specified operand to i8. This can be done with tablegen,
1277/// except that this code uses MVT::Flag in a tricky way that happens to
1278/// improve scheduling in some cases.
1279SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1280 assert(!Subtarget->is64Bit() &&
1281 "getTruncateTo8Bit is only needed on x86-32!");
1282 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001283 DebugLoc dl = N0.getDebugLoc();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001284
1285 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1286 unsigned Opc;
1287 MVT N0VT = N0.getValueType();
1288 switch (N0VT.getSimpleVT()) {
1289 default: assert(0 && "Unknown truncate!");
1290 case MVT::i16:
1291 Opc = X86::MOV16to16_;
1292 break;
1293 case MVT::i32:
1294 Opc = X86::MOV32to32_;
1295 break;
1296 }
1297
1298 // The use of MVT::Flag here is not strictly accurate, but it helps
1299 // scheduling in some cases.
Dale Johannesend8392542009-02-03 21:48:12 +00001300 N0 = SDValue(CurDAG->getTargetNode(Opc, dl, N0VT, MVT::Flag, N0), 0);
1301 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001302 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001303}
1304
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001305SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1306 SDValue Chain = Node->getOperand(0);
1307 SDValue In1 = Node->getOperand(1);
1308 SDValue In2L = Node->getOperand(2);
1309 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001310 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1311 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001312 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001313 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001314 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001315 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1316 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001317 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001318}
Christopher Lambc59e5212007-08-10 21:48:46 +00001319
Dan Gohman475871a2008-07-27 21:46:04 +00001320SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001322 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001323 unsigned Opc, MOpc;
1324 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001325 DebugLoc dl = Node->getDebugLoc();
1326
Evan Chengf597dc72006-02-10 22:24:32 +00001327#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001328 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001329 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001330 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001331 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001332#endif
1333
Dan Gohmane8be6c62008-07-17 19:10:17 +00001334 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001335#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001336 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001337 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001338 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001339 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001340#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001341 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001342 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001343
Evan Cheng0114e942006-01-06 20:36:21 +00001344 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001345 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001346 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001347 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001348
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001349 case X86ISD::ATOMOR64_DAG:
1350 return SelectAtomic64(Node, X86::ATOMOR6432);
1351 case X86ISD::ATOMXOR64_DAG:
1352 return SelectAtomic64(Node, X86::ATOMXOR6432);
1353 case X86ISD::ATOMADD64_DAG:
1354 return SelectAtomic64(Node, X86::ATOMADD6432);
1355 case X86ISD::ATOMSUB64_DAG:
1356 return SelectAtomic64(Node, X86::ATOMSUB6432);
1357 case X86ISD::ATOMNAND64_DAG:
1358 return SelectAtomic64(Node, X86::ATOMNAND6432);
1359 case X86ISD::ATOMAND64_DAG:
1360 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001361 case X86ISD::ATOMSWAP64_DAG:
1362 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001363
Dan Gohman525178c2007-10-08 18:33:35 +00001364 case ISD::SMUL_LOHI:
1365 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue N0 = Node->getOperand(0);
1367 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001368
Dan Gohman525178c2007-10-08 18:33:35 +00001369 bool isSigned = Opcode == ISD::SMUL_LOHI;
1370 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001371 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001372 default: assert(0 && "Unsupported VT!");
1373 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1374 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1375 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001376 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001377 }
1378 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001379 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001380 default: assert(0 && "Unsupported VT!");
1381 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1382 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1383 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001384 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001385 }
1386
1387 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001388 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001389 default: assert(0 && "Unsupported VT!");
1390 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1391 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1392 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001393 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001394 }
1395
Rafael Espindola094fad32009-04-08 21:14:34 +00001396 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1397 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001398 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001399 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001400 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001401 if (foldedLoad)
1402 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001403 }
1404
Dale Johannesendd64c412009-02-04 00:33:20 +00001405 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001406 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001407
1408 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001409 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1410 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001411 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001412 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001413 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001414 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001415 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001416 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001417 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001418 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001419 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001420 }
1421
Dan Gohman525178c2007-10-08 18:33:35 +00001422 // Copy the low half of the result, if it is needed.
1423 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001425 LoReg, NVT, InFlag);
1426 InFlag = Result.getValue(2);
1427 ReplaceUses(N.getValue(0), Result);
1428#ifndef NDEBUG
1429 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001430 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001431 DOUT << "\n";
1432#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001433 }
Dan Gohman525178c2007-10-08 18:33:35 +00001434 // Copy the high half of the result, if it is needed.
1435 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001437 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1438 // Prevent use of AH in a REX instruction by referencing AX instead.
1439 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001440 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001441 X86::AX, MVT::i16, InFlag);
1442 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001443 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1444 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001445 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001446 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesend8392542009-02-03 21:48:12 +00001448 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001449 MVT::i8, Result, SRIdx), 0);
1450 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001451 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001452 HiReg, NVT, InFlag);
1453 InFlag = Result.getValue(2);
1454 }
1455 ReplaceUses(N.getValue(1), Result);
1456#ifndef NDEBUG
1457 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001459 DOUT << "\n";
1460#endif
1461 }
Evan Cheng34167212006-02-09 00:37:58 +00001462
Evan Chengf597dc72006-02-10 22:24:32 +00001463#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001464 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001465#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001466
Evan Cheng64a752f2006-08-11 09:08:15 +00001467 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001468 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001469
Dan Gohman525178c2007-10-08 18:33:35 +00001470 case ISD::SDIVREM:
1471 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001472 SDValue N0 = Node->getOperand(0);
1473 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001474
1475 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001476 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001477 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001478 default: assert(0 && "Unsupported VT!");
1479 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1480 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1481 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001482 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001483 }
1484 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001485 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001486 default: assert(0 && "Unsupported VT!");
1487 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1488 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1489 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001490 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001491 }
1492
1493 unsigned LoReg, HiReg;
1494 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001495 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001496 default: assert(0 && "Unsupported VT!");
1497 case MVT::i8:
1498 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001499 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001500 SExtOpcode = X86::CBW;
1501 break;
1502 case MVT::i16:
1503 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001504 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001505 SExtOpcode = X86::CWD;
1506 break;
1507 case MVT::i32:
1508 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001509 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001510 SExtOpcode = X86::CDQ;
1511 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001512 case MVT::i64:
1513 LoReg = X86::RAX; HiReg = X86::RDX;
1514 ClrOpcode = X86::MOV64r0;
1515 SExtOpcode = X86::CQO;
1516 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001517 }
1518
Rafael Espindola094fad32009-04-08 21:14:34 +00001519 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1520 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001521 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001522
Dan Gohman475871a2008-07-27 21:46:04 +00001523 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001524 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001525 // Special case for div8, just use a move with zero extension to AX to
1526 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001527 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1528 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1529 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001530 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001531 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001532 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001533 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001534 Chain = Move.getValue(1);
1535 ReplaceUses(N0.getValue(1), Chain);
1536 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001537 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001538 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001539 Chain = CurDAG->getEntryNode();
1540 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001541 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001542 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001543 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001544 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001545 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001546 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001547 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001548 // Sign extend the low part into the high part.
1549 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001550 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001551 } else {
1552 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001553 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1554 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001555 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001556 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001557 }
Evan Cheng948f3432006-01-06 23:19:29 +00001558 }
1559
1560 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001561 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1562 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001563 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001564 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001565 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001566 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001567 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001568 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001569 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001570 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001571 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001572 }
1573
Dan Gohmana37c9f72007-09-25 18:23:27 +00001574 // Copy the division (low) result, if it is needed.
1575 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001576 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001577 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001578 InFlag = Result.getValue(2);
1579 ReplaceUses(N.getValue(0), Result);
1580#ifndef NDEBUG
1581 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001582 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001583 DOUT << "\n";
1584#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001585 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001586 // Copy the remainder (high) result, if it is needed.
1587 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001588 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001589 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1590 // Prevent use of AH in a REX instruction by referencing AX instead.
1591 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001592 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001593 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001594 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001595 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1596 Result,
1597 CurDAG->getTargetConstant(8, MVT::i8)),
1598 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001599 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001600 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesend8392542009-02-03 21:48:12 +00001601 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001602 MVT::i8, Result, SRIdx), 0);
1603 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001604 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001605 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001606 InFlag = Result.getValue(2);
1607 }
1608 ReplaceUses(N.getValue(1), Result);
1609#ifndef NDEBUG
1610 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001611 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001612 DOUT << "\n";
1613#endif
1614 }
Evan Chengf597dc72006-02-10 22:24:32 +00001615
1616#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001617 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001618#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001619
1620 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001621 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001622
Christopher Lambc59e5212007-08-10 21:48:46 +00001623 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001624 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001625 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1626 SDValue N0 = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001627
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001628 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1629 unsigned Opc = 0;
1630 switch (NVT.getSimpleVT()) {
1631 default: assert(0 && "Unknown sign_extend_inreg!");
1632 case MVT::i16:
1633 Opc = X86::MOVSX16rr8;
1634 break;
1635 case MVT::i32:
1636 Opc = X86::MOVSX32rr8;
1637 break;
1638 }
1639
Dale Johannesend8392542009-02-03 21:48:12 +00001640 SDNode *ResNode = CurDAG->getTargetNode(Opc, dl, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001641
1642#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001643 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001644 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001645 DOUT << "\n";
1646 DOUT << std::string(Indent-2, ' ') << "=> ";
1647 DEBUG(ResNode->dump(CurDAG));
1648 DOUT << "\n";
1649 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001650#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001651 return ResNode;
1652 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001653 break;
1654 }
1655
1656 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001657 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1658 SDValue Input = Node->getOperand(0);
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001659 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001660
Evan Cheng403be7e2006-05-08 08:01:26 +00001661#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001662 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001663 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001664 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001665 Indent -= 2;
1666#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001667 return ResNode;
1668 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001669 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001670 }
Evan Cheng851bc042008-06-17 02:01:22 +00001671
1672 case ISD::DECLARE: {
1673 // Handle DECLARE nodes here because the second operand may have been
1674 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001675 SDValue Chain = Node->getOperand(0);
1676 SDValue N1 = Node->getOperand(1);
1677 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001678 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001679
1680 // FIXME: We need to handle this for VLAs.
1681 if (!FINode) {
1682 ReplaceUses(N.getValue(0), Chain);
1683 return NULL;
1684 }
1685
Evan Chengfab83872008-06-18 02:48:27 +00001686 if (N2.getOpcode() == ISD::ADD &&
1687 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1688 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001689
1690 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1691 // somehow, just ignore it.
1692 if (N2.getOpcode() != X86ISD::Wrapper) {
1693 ReplaceUses(N.getValue(0), Chain);
1694 return NULL;
1695 }
Evan Chengf2accb52009-01-10 03:33:22 +00001696 GlobalAddressSDNode *GVNode =
1697 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001698 if (GVNode == 0) {
1699 ReplaceUses(N.getValue(0), Chain);
1700 return NULL;
1701 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001702 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1703 TLI.getPointerTy());
1704 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1705 TLI.getPointerTy());
1706 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001707 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001708 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001709 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001710 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001711 }
1712
Evan Cheng9ade2182006-08-26 05:34:46 +00001713 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001714
Evan Chengf597dc72006-02-10 22:24:32 +00001715#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001716 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001717 if (ResNode == NULL || ResNode == N.getNode())
1718 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001719 else
1720 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001721 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001722 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001723#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001724
1725 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001726}
1727
Chris Lattnerc0bad572006-06-08 18:03:49 +00001728bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001729SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001730 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001731 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001732 switch (ConstraintCode) {
1733 case 'o': // offsetable ??
1734 case 'v': // not offsetable ??
1735 default: return true;
1736 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001737 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001738 return true;
1739 break;
1740 }
1741
Evan Cheng04699902006-08-26 01:05:16 +00001742 OutOps.push_back(Op0);
1743 OutOps.push_back(Op1);
1744 OutOps.push_back(Op2);
1745 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001746 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001747 return false;
1748}
1749
Chris Lattnerc961eea2005-11-16 01:54:32 +00001750/// createX86ISelDag - This pass converts a legalized DAG into a
1751/// X86-specific DAG, ready for instruction scheduling.
1752///
Evan Chenge50794a2006-08-29 18:28:33 +00001753FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1754 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001755}