blob: 1317b53a78e8c545d588c2a6e2fd2a77e5afc6e5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Cheng13559d62008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Compiler.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
51 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
57 FrameIndexBase
58 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 int FrameIndex;
63 } Base;
64
Evan Cheng3b5a1272008-02-07 08:53:49 +000065 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000067 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000068 int32_t Disp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 GlobalValue *GV;
70 Constant *CP;
71 const char *ES;
72 int JT;
73 unsigned Align; // CP alignment.
74
75 X86ISelAddressMode()
76 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
78 }
Dan Gohman245791b2009-02-07 00:43:41 +000079
80 bool hasSymbolicDisplacement() const {
81 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
82 }
83
Dale Johannesenc501c082008-08-11 23:46:25 +000084 void dump() {
85 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000086 cerr << "Base.Reg ";
87 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
88 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000089 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
90 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000091 cerr << "IndexReg ";
92 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
93 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000094 cerr << " Disp " << Disp << "\n";
95 cerr << "GV "; if (GV) GV->dump();
96 else cerr << "nul";
97 cerr << " CP "; if (CP) CP->dump();
98 else cerr << "nul";
99 cerr << "\n";
100 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
101 cerr << " JT" << JT << " Align" << Align << "\n";
102 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 };
104}
105
106namespace {
107 //===--------------------------------------------------------------------===//
108 /// ISel - X86 specific code to select X86 machine instructions for
109 /// SelectionDAG operations.
110 ///
111 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 /// TM - Keep a reference to X86TargetMachine.
113 ///
114 X86TargetMachine &TM;
115
116 /// X86Lowering - This object fully describes how to lower LLVM code to an
117 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000118 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
120 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
121 /// make the right decision when generating code for different targets.
122 const X86Subtarget *Subtarget;
123
Evan Cheng34fd4f32008-06-30 20:45:06 +0000124 /// CurBB - Current BB being isel'd.
125 ///
126 MachineBasicBlock *CurBB;
127
Evan Cheng13559d62008-09-26 23:41:32 +0000128 /// OptForSize - If true, selector should try to optimize for code size
129 /// instead of performance.
130 bool OptForSize;
131
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 public:
133 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000134 : SelectionDAGISel(tm, fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000135 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000136 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000137 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 virtual const char *getPassName() const {
140 return "X86 DAG->DAG Instruction Selection";
141 }
142
Evan Cheng34fd4f32008-06-30 20:45:06 +0000143 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000145 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000146
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000147 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
148
Evan Cheng5a424552008-11-27 00:49:46 +0000149 virtual
150 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
152// Include the pieces autogenerated from the target description.
153#include "X86GenDAGISel.inc"
154
155 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000157 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000161 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000162 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000163 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
164 SDValue &Scale, SDValue &Index, SDValue &Disp);
165 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
166 SDValue &Scale, SDValue &Index, SDValue &Disp);
167 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
168 SDValue N, SDValue &Base, SDValue &Scale,
169 SDValue &Index, SDValue &Disp,
170 SDValue &InChain, SDValue &OutChain);
171 bool TryFoldLoad(SDValue P, SDValue N,
172 SDValue &Base, SDValue &Scale,
173 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000174 void PreprocessForRMW();
175 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
177 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
178 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000179 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000181 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000183 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
184
Dan Gohman8181bd12008-07-27 21:46:04 +0000185 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
186 SDValue &Scale, SDValue &Index,
187 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
189 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
190 AM.Base.Reg;
191 Scale = getI8Imm(AM.Scale);
192 Index = AM.IndexReg;
193 // These are 32-bit even in 64-bit mode since RIP relative offset
194 // is 32-bit.
195 if (AM.GV)
196 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
197 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000198 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
199 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000201 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 else if (AM.JT != -1)
203 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
204 else
Dan Gohman0bd76b72008-11-11 15:52:29 +0000205 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 }
207
208 /// getI8Imm - Return a target constant with the specified value, of type
209 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000210 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 return CurDAG->getTargetConstant(Imm, MVT::i8);
212 }
213
214 /// getI16Imm - Return a target constant with the specified value, of type
215 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000216 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return CurDAG->getTargetConstant(Imm, MVT::i16);
218 }
219
220 /// getI32Imm - Return a target constant with the specified value, of type
221 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000222 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return CurDAG->getTargetConstant(Imm, MVT::i32);
224 }
225
Dan Gohmanb60482f2008-09-23 18:22:58 +0000226 /// getGlobalBaseReg - Return an SDNode that returns the value of
227 /// the global base register. Output instructions required to
228 /// initialize the global base register, if necessary.
229 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 SDNode *getGlobalBaseReg();
231
Dan Gohmandd612bb2008-08-20 21:27:32 +0000232 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
233 /// truncate of the specified operand to i8. This can be done with tablegen,
234 /// except that this code uses MVT::Flag in a tricky way that happens to
235 /// improve scheduling in some cases.
236 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238#ifndef NDEBUG
239 unsigned Indent;
240#endif
241 };
242}
243
Gabor Greife9f7f582008-08-31 15:37:04 +0000244/// findFlagUse - Return use of MVT::Flag value produced by the specified
245/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000246///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247static SDNode *findFlagUse(SDNode *N) {
248 unsigned FlagResNo = N->getNumValues()-1;
249 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman13f24a72009-01-27 02:37:43 +0000250 SDUse &Use = I.getUse();
251 if (Use.getResNo() == FlagResNo)
252 return Use.getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
254 return NULL;
255}
256
djg4b210952009-01-27 19:04:30 +0000257/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
258/// This function recursively traverses up the operand chain, ignoring
259/// certain nodes.
260static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
261 SDNode *Root,
Evan Cheng656269e2008-04-25 08:22:20 +0000262 SmallPtrSet<SDNode*, 16> &Visited) {
djg4b210952009-01-27 19:04:30 +0000263 if (Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000264 !Visited.insert(Use))
djg4b210952009-01-27 19:04:30 +0000265 return false;
266
267 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000268 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000270 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000271 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000272 assert(N != Root);
djg4b210952009-01-27 19:04:30 +0000273 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 }
Evan Cheng656269e2008-04-25 08:22:20 +0000275
276 // Traverse up the operand chain.
djg4b210952009-01-27 19:04:30 +0000277 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
278 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 }
djg4b210952009-01-27 19:04:30 +0000280 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281}
282
283/// isNonImmUse - Start searching from Root up the DAG to check is Def can
284/// be reached. Return true if that's the case. However, ignore direct uses
285/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng5a424552008-11-27 00:49:46 +0000286/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
287/// case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288/// FIXME: to be really generic, we should allow direct use by any node
289/// that is being folded. But realisticly since we only fold loads which
290/// have one non-chain use, we only need to watch out for load/op/store
291/// and load/op/cmp case where the root (store / cmp) may reach the load via
292/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000293static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000294 SmallPtrSet<SDNode*, 16> Visited;
djg4b210952009-01-27 19:04:30 +0000295 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296}
297
298
Evan Cheng5a424552008-11-27 00:49:46 +0000299bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
300 SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000301 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Evan Cheng5a424552008-11-27 00:49:46 +0000303 if (U == Root)
304 switch (U->getOpcode()) {
305 default: break;
306 case ISD::ADD:
307 case ISD::ADDC:
308 case ISD::ADDE:
309 case ISD::AND:
310 case ISD::OR:
311 case ISD::XOR: {
312 // If the other operand is a 8-bit immediate we should fold the immediate
313 // instead. This reduces code size.
314 // e.g.
315 // movl 4(%esp), %eax
316 // addl $4, %eax
317 // vs.
318 // movl $4, %eax
319 // addl 4(%esp), %eax
320 // The former is 2 bytes shorter. In case where the increment is 1, then
321 // the saving can be 4 bytes (by using incl %eax).
Dan Gohman01126892009-03-14 02:07:16 +0000322 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1)))
323 if (Imm->getAPIntValue().isSignedIntN(8))
324 return false;
Evan Cheng5a424552008-11-27 00:49:46 +0000325 }
326 }
327
Dan Gohman602d44a2008-09-17 01:39:10 +0000328 // If Root use can somehow reach N through a path that that doesn't contain
329 // U then folding N would create a cycle. e.g. In the following
330 // diagram, Root can reach N through X. If N is folded into into Root, then
331 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000333 // [N*] //
334 // ^ ^ //
335 // / \ //
336 // [U*] [X]? //
337 // ^ ^ //
338 // \ / //
339 // \ / //
340 // [Root*] //
341 //
342 // * indicates nodes to be folded together.
343 //
344 // If Root produces a flag, then it gets (even more) interesting. Since it
345 // will be "glued" together with its flag use in the scheduler, we need to
346 // check if it might reach N.
347 //
348 // [N*] //
349 // ^ ^ //
350 // / \ //
351 // [U*] [X]? //
352 // ^ ^ //
353 // \ \ //
354 // \ | //
355 // [Root*] | //
356 // ^ | //
357 // f | //
358 // | / //
359 // [Y] / //
360 // ^ / //
361 // f / //
362 // | / //
363 // [FU] //
364 //
365 // If FU (flag use) indirectly reaches N (the load), and Root folds N
366 // (call it Fold), then X is a predecessor of FU and a successor of
367 // Fold. But since Fold and FU are flagged together, this will create
368 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
Duncan Sands92c43912008-06-06 12:08:01 +0000370 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000371 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 SDNode *FU = findFlagUse(Root);
373 if (FU == NULL)
374 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000375 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 VT = Root->getValueType(Root->getNumValues()-1);
377 }
378
Dan Gohman602d44a2008-09-17 01:39:10 +0000379 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380}
381
382/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
383/// and move load below the TokenFactor. Replace store's chain operand with
384/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000385static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000386 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000387 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000388 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
389 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000390 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000392 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000393 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
394 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
395 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
396 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397}
398
Evan Cheng2b2a7012008-05-23 21:23:16 +0000399/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
400///
Dan Gohman8181bd12008-07-27 21:46:04 +0000401static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
402 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000403 if (N.getOpcode() == ISD::BIT_CONVERT)
404 N = N.getOperand(0);
405
406 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
407 if (!LD || LD->isVolatile())
408 return false;
409 if (LD->getAddressingMode() != ISD::UNINDEXED)
410 return false;
411
412 ISD::LoadExtType ExtType = LD->getExtensionType();
413 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
414 return false;
415
416 if (N.hasOneUse() &&
417 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000418 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000419 Load = N;
420 return true;
421 }
422 return false;
423}
424
Evan Cheng98cfaf82008-08-25 21:27:18 +0000425/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
426/// operand and move load below the call's chain operand.
427static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
evanchengcd6d72b2009-01-26 18:43:34 +0000428 SDValue Call, SDValue CallSeqStart) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000429 SmallVector<SDValue, 8> Ops;
evanchengcd6d72b2009-01-26 18:43:34 +0000430 SDValue Chain = CallSeqStart.getOperand(0);
431 if (Chain.getNode() == Load.getNode())
432 Ops.push_back(Load.getOperand(0));
433 else {
434 assert(Chain.getOpcode() == ISD::TokenFactor &&
435 "Unexpected CallSeqStart chain operand");
436 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
437 if (Chain.getOperand(i).getNode() == Load.getNode())
438 Ops.push_back(Load.getOperand(0));
439 else
440 Ops.push_back(Chain.getOperand(i));
441 SDValue NewChain =
Dale Johannesen913ba762009-02-06 01:31:28 +0000442 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
443 MVT::Other, &Ops[0], Ops.size());
evanchengcd6d72b2009-01-26 18:43:34 +0000444 Ops.clear();
445 Ops.push_back(NewChain);
446 }
447 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
448 Ops.push_back(CallSeqStart.getOperand(i));
449 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000450 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
451 Load.getOperand(1), Load.getOperand(2));
452 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000453 Ops.push_back(SDValue(Load.getNode(), 1));
454 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000455 Ops.push_back(Call.getOperand(i));
456 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
457}
458
459/// isCalleeLoad - Return true if call address is a load and it can be
460/// moved below CALLSEQ_START and the chains leading up to the call.
461/// Return the CALLSEQ_START by reference as a second output.
462static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000463 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000464 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000465 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000466 if (!LD ||
467 LD->isVolatile() ||
468 LD->getAddressingMode() != ISD::UNINDEXED ||
469 LD->getExtensionType() != ISD::NON_EXTLOAD)
470 return false;
471
472 // Now let's find the callseq_start.
473 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
474 if (!Chain.hasOneUse())
475 return false;
476 Chain = Chain.getOperand(0);
477 }
evanchengcd6d72b2009-01-26 18:43:34 +0000478
479 if (Chain.getOperand(0).getNode() == Callee.getNode())
480 return true;
481 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
482 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
483 return true;
484 return false;
Evan Cheng98cfaf82008-08-25 21:27:18 +0000485}
486
487
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000488/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
489/// This is only run if not in -fast mode (aka -O0).
490/// This allows the instruction selector to pick more read-modify-write
491/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492///
493/// [Load chain]
494/// ^
495/// |
496/// [Load]
497/// ^ ^
498/// | |
499/// / \-
500/// / |
501/// [TokenFactor] [Op]
502/// ^ ^
503/// | |
504/// \ /
505/// \ /
506/// [Store]
507///
508/// The fact the store's chain operand != load's chain will prevent the
509/// (store (op (load))) instruction from being selected. We can transform it to:
510///
511/// [Load chain]
512/// ^
513/// |
514/// [TokenFactor]
515/// ^
516/// |
517/// [Load]
518/// ^ ^
519/// | |
520/// | \-
521/// | |
522/// | [Op]
523/// | ^
524/// | |
525/// \ /
526/// \ /
527/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000528void X86DAGToDAGISel::PreprocessForRMW() {
529 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
530 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000531 if (I->getOpcode() == X86ISD::CALL) {
532 /// Also try moving call address load from outside callseq_start to just
533 /// before the call to allow it to be folded.
534 ///
535 /// [Load chain]
536 /// ^
537 /// |
538 /// [Load]
539 /// ^ ^
540 /// | |
541 /// / \--
542 /// / |
543 ///[CALLSEQ_START] |
544 /// ^ |
545 /// | |
546 /// [LOAD/C2Reg] |
547 /// | |
548 /// \ /
549 /// \ /
550 /// [CALL]
551 SDValue Chain = I->getOperand(0);
552 SDValue Load = I->getOperand(1);
553 if (!isCalleeLoad(Load, Chain))
554 continue;
555 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
556 ++NumLoadMoved;
557 continue;
558 }
559
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 if (!ISD::isNON_TRUNCStore(I))
561 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000562 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000563
Gabor Greif1c80d112008-08-28 21:40:38 +0000564 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 continue;
566
Dan Gohman8181bd12008-07-27 21:46:04 +0000567 SDValue N1 = I->getOperand(1);
568 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000569 if ((N1.getValueType().isFloatingPoint() &&
570 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 !N1.hasOneUse())
572 continue;
573
574 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000575 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000576 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000578 case ISD::ADD:
579 case ISD::MUL:
580 case ISD::AND:
581 case ISD::OR:
582 case ISD::XOR:
583 case ISD::ADDC:
584 case ISD::ADDE:
585 case ISD::VECTOR_SHUFFLE: {
586 SDValue N10 = N1.getOperand(0);
587 SDValue N11 = N1.getOperand(1);
588 RModW = isRMWLoad(N10, Chain, N2, Load);
589 if (!RModW)
590 RModW = isRMWLoad(N11, Chain, N2, Load);
591 break;
592 }
593 case ISD::SUB:
594 case ISD::SHL:
595 case ISD::SRA:
596 case ISD::SRL:
597 case ISD::ROTL:
598 case ISD::ROTR:
599 case ISD::SUBC:
600 case ISD::SUBE:
601 case X86ISD::SHLD:
602 case X86ISD::SHRD: {
603 SDValue N10 = N1.getOperand(0);
604 RModW = isRMWLoad(N10, Chain, N2, Load);
605 break;
606 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 }
608
609 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000610 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 ++NumLoadMoved;
612 }
613 }
614}
615
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000616
617/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
618/// nodes that target the FP stack to be store and load to the stack. This is a
619/// gross hack. We would like to simply mark these as being illegal, but when
620/// we do that, legalize produces these when it expands calls, then expands
621/// these in the same legalize pass. We would like dag combine to be able to
622/// hack on these between the call expansion and the node legalization. As such
623/// this pass basically does "really late" legalization of these inline with the
624/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000625void X86DAGToDAGISel::PreprocessForFPConvert() {
626 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
627 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000628 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
629 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
630 continue;
631
632 // If the source and destination are SSE registers, then this is a legal
633 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000634 MVT SrcVT = N->getOperand(0).getValueType();
635 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000636 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
637 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
638 if (SrcIsSSE && DstIsSSE)
639 continue;
640
Chris Lattner5d294e52008-03-09 07:05:32 +0000641 if (!SrcIsSSE && !DstIsSSE) {
642 // If this is an FPStack extension, it is a noop.
643 if (N->getOpcode() == ISD::FP_EXTEND)
644 continue;
645 // If this is a value-preserving FPStack truncation, it is a noop.
646 if (N->getConstantOperandVal(1))
647 continue;
648 }
649
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000650 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
651 // FPStack has extload and truncstore. SSE can fold direct loads into other
652 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000653 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000654 if (N->getOpcode() == ISD::FP_ROUND)
655 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
656 else
657 MemVT = SrcIsSSE ? SrcVT : DstVT;
658
Dan Gohman14a66442008-08-23 02:25:05 +0000659 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000660 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000661
662 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000663 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohman14a66442008-08-23 02:25:05 +0000664 N->getOperand(0),
665 MemTmp, NULL, 0, MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000666 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohman14a66442008-08-23 02:25:05 +0000667 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000668
669 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
670 // extload we created. This will cause general havok on the dag because
671 // anything below the conversion could be folded into other existing nodes.
672 // To avoid invalidating 'I', back it up to the convert node.
673 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000674 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000675
676 // Now that we did that, the node is dead. Increment the iterator to the
677 // next node to process, then delete N.
678 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000679 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000680 }
681}
682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
684/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000685void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000686 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000687 const Function *F = CurDAG->getMachineFunction().getFunction();
688 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
Evan Cheng34fd4f32008-06-30 20:45:06 +0000690 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000691 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000692 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000693
694 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000695 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696
697 // Codegen the basic block.
698#ifndef NDEBUG
699 DOUT << "===== Instruction selection begins:\n";
700 Indent = 0;
701#endif
David Greene932618b2008-10-27 21:56:29 +0000702 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703#ifndef NDEBUG
704 DOUT << "===== Instruction selection ends:\n";
705#endif
706
Dan Gohman14a66442008-08-23 02:25:05 +0000707 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000708}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000710/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
711/// the main function.
712void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
713 MachineFrameInfo *MFI) {
714 const TargetInstrInfo *TII = TM.getInstrInfo();
715 if (Subtarget->isTargetCygMing())
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000716 BuildMI(BB, DebugLoc::getUnknownLoc(),
717 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000718}
719
720void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
721 // If this is main, emit special code for main.
722 MachineBasicBlock *BB = MF.begin();
723 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
724 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
725}
726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727/// MatchAddress - Add the specified node to the specified addressing mode,
728/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000729/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000730bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 bool isRoot, unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000732 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000733 DebugLoc dl = N.getDebugLoc();
Evan Cheng7f250d62008-09-24 00:05:32 +0000734 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000735 // Limit recursion.
736 if (Depth > 5)
737 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738
739 // RIP relative addressing: %rip + 32-bit displacement!
740 if (AM.isRIPRel) {
741 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000742 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000743 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 AM.Disp += Val;
745 return false;
746 }
747 }
748 return true;
749 }
750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 switch (N.getOpcode()) {
752 default: break;
753 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000754 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000755 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 AM.Disp += Val;
757 return false;
758 }
759 break;
760 }
761
762 case X86ISD::Wrapper: {
Dan Gohman36322c72008-10-18 02:06:02 +0000763 DOUT << "Wrapper: 64bit " << is64Bit;
764 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000766 // Also, base and index reg must be 0 in order to use rip as base.
767 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000768 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 break;
Dan Gohman245791b2009-02-07 00:43:41 +0000770 if (AM.hasSymbolicDisplacement())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 break;
772 // If value is available in a register both base and index components have
773 // been picked, we can't fit the result available in the register in the
774 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohmancc3df852008-11-05 04:14:16 +0000775 {
Dan Gohman8181bd12008-07-27 21:46:04 +0000776 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000778 uint64_t Offset = G->getOffset();
779 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000780 GlobalValue *GV = G->getGlobal();
781 AM.GV = GV;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000782 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000783 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
784 return false;
785 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000787 uint64_t Offset = CP->getOffset();
788 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000789 AM.CP = CP->getConstVal();
790 AM.Align = CP->getAlignment();
Dan Gohman0bd76b72008-11-11 15:52:29 +0000791 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000792 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
793 return false;
794 }
Bill Wendlingfef06052008-09-16 21:48:12 +0000795 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000796 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000797 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000798 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000800 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000801 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000802 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 }
804 }
805 break;
806 }
807
808 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000809 if (AM.BaseType == X86ISelAddressMode::RegBase
810 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
812 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
813 return false;
814 }
815 break;
816
817 case ISD::SHL:
Dan Gohmancc3df852008-11-05 04:14:16 +0000818 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000819 break;
820
Gabor Greife9f7f582008-08-31 15:37:04 +0000821 if (ConstantSDNode
822 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000823 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000824 if (Val == 1 || Val == 2 || Val == 3) {
825 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000826 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827
Chris Lattner7f06edd2007-12-08 07:22:58 +0000828 // Okay, we know that we have a scale by now. However, if the scaled
829 // value is an add of something and a constant, we can fold the
830 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000831 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
832 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
833 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000834 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000835 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000836 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000837 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000838 AM.Disp = Disp;
839 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000841 } else {
842 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000844 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 }
846 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000847 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848
Dan Gohman35b99222007-10-22 20:22:24 +0000849 case ISD::SMUL_LOHI:
850 case ISD::UMUL_LOHI:
851 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000852 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000853 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 case ISD::MUL:
855 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000856 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000857 AM.Base.Reg.getNode() == 0 &&
858 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000859 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000860 if (ConstantSDNode
861 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000862 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
863 CN->getZExtValue() == 9) {
864 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
Gabor Greif1c80d112008-08-28 21:40:38 +0000866 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000867 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868
869 // Okay, we know that we have a scale by now. However, if the scaled
870 // value is an add of something and a constant, we can fold the
871 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000872 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
873 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
874 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000876 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000877 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000878 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000879 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 AM.Disp = Disp;
881 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000882 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000884 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 }
886
887 AM.IndexReg = AM.Base.Reg = Reg;
888 return false;
889 }
890 }
891 break;
892
Evan Cheng2ed6f342009-01-17 07:09:27 +0000893 case ISD::ADD: {
894 X86ISelAddressMode Backup = AM;
895 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
896 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
897 return false;
898 AM = Backup;
899 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
900 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
901 return false;
902 AM = Backup;
Dan Gohman3ae92482009-03-13 02:25:09 +0000903
904 // If we couldn't fold both operands into the address at the same time,
905 // see if we can just put each operand into a register and fold at least
906 // the add.
907 if (AM.BaseType == X86ISelAddressMode::RegBase &&
908 !AM.Base.Reg.getNode() &&
909 !AM.IndexReg.getNode() &&
910 !AM.isRIPRel) {
911 AM.Base.Reg = N.getNode()->getOperand(0);
912 AM.IndexReg = N.getNode()->getOperand(1);
913 AM.Scale = 1;
914 return false;
915 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 break;
Evan Cheng2ed6f342009-01-17 07:09:27 +0000917 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918
919 case ISD::OR:
920 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000921 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
922 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000923 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000924 // Start with the LHS as an addr mode.
925 if (!MatchAddress(N.getOperand(0), AM, false) &&
926 // Address could not have picked a GV address for the displacement.
927 AM.GV == NULL &&
928 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman0bd76b72008-11-11 15:52:29 +0000929 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000930 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000931 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000932 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000933 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000935 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 }
937 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000938
939 case ISD::AND: {
940 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
941 // allows us to fold the shift into this addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000942 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000943 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohmancc3df852008-11-05 04:14:16 +0000944
Evan Chengf2abee72007-12-13 00:43:27 +0000945 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000946 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000947
948 // Not when RIP is used as the base.
949 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000950
951 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
952 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
953 if (!C1 || !C2) break;
954
955 // Not likely to be profitable if either the AND or SHIFT node has more
956 // than one use (unless all uses are for address computation). Besides,
957 // isel mechanism requires their node ids to be reused.
958 if (!N.hasOneUse() || !Shift.hasOneUse())
959 break;
960
961 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000962 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000963 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
964 break;
965
966 // Get the new AND mask, this folds to a constant.
Dan Gohmancc3df852008-11-05 04:14:16 +0000967 SDValue X = Shift.getOperand(0);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000968 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +0000969 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000970 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
971 NewANDMask);
972 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman3666f472008-10-13 20:52:04 +0000973 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000974
975 // Insert the new nodes into the topological ordering.
976 if (C1->getNodeId() > X.getNode()->getNodeId()) {
977 CurDAG->RepositionNode(X.getNode(), C1);
978 C1->setNodeId(X.getNode()->getNodeId());
979 }
980 if (NewANDMask.getNode()->getNodeId() == -1 ||
981 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
982 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
983 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
984 }
985 if (NewAND.getNode()->getNodeId() == -1 ||
986 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
987 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
988 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
989 }
990 if (NewSHIFT.getNode()->getNodeId() == -1 ||
991 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
992 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
993 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
994 }
995
Dan Gohman3666f472008-10-13 20:52:04 +0000996 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +0000997
998 AM.Scale = 1 << ShiftCst;
999 AM.IndexReg = NewAND;
1000 return false;
1001 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 }
1003
Dan Gohmana60c1b32007-08-13 20:03:06 +00001004 return MatchAddressBase(N, AM, isRoot, Depth);
1005}
1006
1007/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1008/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +00001009bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +00001010 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001012 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +00001014 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 AM.IndexReg = N;
1016 AM.Scale = 1;
1017 return false;
1018 }
1019
1020 // Otherwise, we cannot select it.
1021 return true;
1022 }
1023
1024 // Default, generate it as a register.
1025 AM.BaseType = X86ISelAddressMode::RegBase;
1026 AM.Base.Reg = N;
1027 return false;
1028}
1029
1030/// SelectAddr - returns true if it is able pattern match an addressing mode.
1031/// It returns the operands which make up the maximal addressing mode it can
1032/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001033bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1034 SDValue &Scale, SDValue &Index,
1035 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 X86ISelAddressMode AM;
1037 if (MatchAddress(N, AM))
1038 return false;
1039
Duncan Sands92c43912008-06-06 12:08:01 +00001040 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001042 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 AM.Base.Reg = CurDAG->getRegister(0, VT);
1044 }
1045
Gabor Greif1c80d112008-08-28 21:40:38 +00001046 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 AM.IndexReg = CurDAG->getRegister(0, VT);
1048
1049 getAddressOperands(AM, Base, Scale, Index, Disp);
1050 return true;
1051}
1052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1054/// match a load whose top elements are either undef or zeros. The load flavor
1055/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001056bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1057 SDValue N, SDValue &Base,
1058 SDValue &Scale, SDValue &Index,
1059 SDValue &Disp, SDValue &InChain,
1060 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1062 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001063 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 InChain.getValue(0).hasOneUse() &&
1065 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001066 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1068 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1069 return false;
1070 OutChain = LD->getChain();
1071 return true;
1072 }
1073 }
1074
1075 // Also handle the case where we explicitly require zeros in the top
1076 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001077 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001078 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001079 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001080 N.getOperand(0).getNode()->hasOneUse() &&
1081 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001082 N.getOperand(0).getOperand(0).hasOneUse()) {
1083 // Okay, this is a zero extending load. Fold it.
1084 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1085 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1086 return false;
1087 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001088 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001089 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 }
1091 return false;
1092}
1093
1094
1095/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1096/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001097bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1098 SDValue &Base, SDValue &Scale,
1099 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 X86ISelAddressMode AM;
1101 if (MatchAddress(N, AM))
1102 return false;
1103
Duncan Sands92c43912008-06-06 12:08:01 +00001104 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 unsigned Complexity = 0;
1106 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001107 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 Complexity = 1;
1109 else
1110 AM.Base.Reg = CurDAG->getRegister(0, VT);
1111 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1112 Complexity = 4;
1113
Gabor Greif1c80d112008-08-28 21:40:38 +00001114 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 Complexity++;
1116 else
1117 AM.IndexReg = CurDAG->getRegister(0, VT);
1118
1119 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1120 // a simple shift.
1121 if (AM.Scale > 1)
1122 Complexity++;
1123
1124 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1125 // to a LEA. This is determined with some expermentation but is by no means
1126 // optimal (especially for code size consideration). LEA is nice because of
1127 // its three-address nature. Tweak the cost function again when we can run
1128 // convertToThreeAddress() at register allocation time.
Dan Gohman245791b2009-02-07 00:43:41 +00001129 if (AM.hasSymbolicDisplacement()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 // For X86-64, we should always use lea to materialize RIP relative
1131 // addresses.
1132 if (Subtarget->is64Bit())
1133 Complexity = 4;
1134 else
1135 Complexity += 2;
1136 }
1137
Gabor Greif1c80d112008-08-28 21:40:38 +00001138 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 Complexity++;
1140
1141 if (Complexity > 2) {
1142 getAddressOperands(AM, Base, Scale, Index, Disp);
1143 return true;
1144 }
1145 return false;
1146}
1147
Dan Gohman8181bd12008-07-27 21:46:04 +00001148bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1149 SDValue &Base, SDValue &Scale,
1150 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001151 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001153 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1155 return false;
1156}
1157
Dan Gohmanb60482f2008-09-23 18:22:58 +00001158/// getGlobalBaseReg - Return an SDNode that returns the value of
1159/// the global base register. Output instructions required to
1160/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161///
1162SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001163 MachineFunction *MF = CurBB->getParent();
1164 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001165 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166}
1167
1168static SDNode *FindCallStartFromCall(SDNode *Node) {
1169 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1170 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1171 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001172 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173}
1174
Dan Gohmandd612bb2008-08-20 21:27:32 +00001175/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1176/// truncate of the specified operand to i8. This can be done with tablegen,
1177/// except that this code uses MVT::Flag in a tricky way that happens to
1178/// improve scheduling in some cases.
1179SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1180 assert(!Subtarget->is64Bit() &&
1181 "getTruncateTo8Bit is only needed on x86-32!");
1182 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00001183 DebugLoc dl = N0.getDebugLoc();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001184
1185 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1186 unsigned Opc;
1187 MVT N0VT = N0.getValueType();
1188 switch (N0VT.getSimpleVT()) {
1189 default: assert(0 && "Unknown truncate!");
1190 case MVT::i16:
1191 Opc = X86::MOV16to16_;
1192 break;
1193 case MVT::i32:
1194 Opc = X86::MOV32to32_;
1195 break;
1196 }
1197
1198 // The use of MVT::Flag here is not strictly accurate, but it helps
1199 // scheduling in some cases.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001200 N0 = SDValue(CurDAG->getTargetNode(Opc, dl, N0VT, MVT::Flag, N0), 0);
1201 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmandd612bb2008-08-20 21:27:32 +00001202 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001203}
1204
Dale Johannesenf160d802008-10-02 18:53:47 +00001205SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1206 SDValue Chain = Node->getOperand(0);
1207 SDValue In1 = Node->getOperand(1);
1208 SDValue In2L = Node->getOperand(2);
1209 SDValue In2H = Node->getOperand(3);
1210 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1211 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1212 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001213 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001214 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001215 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1216 MVT::i32, MVT::i32, MVT::Other, Ops, 8);
Dale Johannesenf160d802008-10-02 18:53:47 +00001217}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001218
Dan Gohman8181bd12008-07-27 21:46:04 +00001219SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001220 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001221 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 unsigned Opc, MOpc;
1223 unsigned Opcode = Node->getOpcode();
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001224 DebugLoc dl = Node->getDebugLoc();
1225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226#ifndef NDEBUG
1227 DOUT << std::string(Indent, ' ') << "Selecting: ";
1228 DEBUG(Node->dump(CurDAG));
1229 DOUT << "\n";
1230 Indent += 2;
1231#endif
1232
Dan Gohmanbd68c792008-07-17 19:10:17 +00001233 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234#ifndef NDEBUG
1235 DOUT << std::string(Indent-2, ' ') << "== ";
1236 DEBUG(Node->dump(CurDAG));
1237 DOUT << "\n";
1238 Indent -= 2;
1239#endif
1240 return NULL; // Already selected.
1241 }
1242
1243 switch (Opcode) {
1244 default: break;
1245 case X86ISD::GlobalBaseReg:
1246 return getGlobalBaseReg();
1247
Dale Johannesenf160d802008-10-02 18:53:47 +00001248 case X86ISD::ATOMOR64_DAG:
1249 return SelectAtomic64(Node, X86::ATOMOR6432);
1250 case X86ISD::ATOMXOR64_DAG:
1251 return SelectAtomic64(Node, X86::ATOMXOR6432);
1252 case X86ISD::ATOMADD64_DAG:
1253 return SelectAtomic64(Node, X86::ATOMADD6432);
1254 case X86ISD::ATOMSUB64_DAG:
1255 return SelectAtomic64(Node, X86::ATOMSUB6432);
1256 case X86ISD::ATOMNAND64_DAG:
1257 return SelectAtomic64(Node, X86::ATOMNAND6432);
1258 case X86ISD::ATOMAND64_DAG:
1259 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001260 case X86ISD::ATOMSWAP64_DAG:
1261 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001262
Dan Gohman5a199552007-10-08 18:33:35 +00001263 case ISD::SMUL_LOHI:
1264 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001265 SDValue N0 = Node->getOperand(0);
1266 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001267
Dan Gohman5a199552007-10-08 18:33:35 +00001268 bool isSigned = Opcode == ISD::SMUL_LOHI;
1269 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001270 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 default: assert(0 && "Unsupported VT!");
1272 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1273 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1274 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1275 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1276 }
1277 else
Duncan Sands92c43912008-06-06 12:08:01 +00001278 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 default: assert(0 && "Unsupported VT!");
1280 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1281 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1282 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1283 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1284 }
1285
1286 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001287 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 default: assert(0 && "Unsupported VT!");
1289 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1290 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1291 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1292 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1293 }
1294
Dan Gohman8181bd12008-07-27 21:46:04 +00001295 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001296 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001297 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 if (!foldedLoad) {
1299 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001300 if (foldedLoad)
1301 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 }
1303
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001304 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman8181bd12008-07-27 21:46:04 +00001305 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306
1307 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001308 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309 SDNode *CNode =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001310 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001311 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001312 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001313 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001316 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 }
1318
Dan Gohman5a199552007-10-08 18:33:35 +00001319 // Copy the low half of the result, if it is needed.
1320 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001321 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001322 LoReg, NVT, InFlag);
1323 InFlag = Result.getValue(2);
1324 ReplaceUses(N.getValue(0), Result);
1325#ifndef NDEBUG
1326 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001327 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001328 DOUT << "\n";
1329#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001330 }
Dan Gohman5a199552007-10-08 18:33:35 +00001331 // Copy the high half of the result, if it is needed.
1332 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001333 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001334 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1335 // Prevent use of AH in a REX instruction by referencing AX instead.
1336 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001337 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001338 X86::AX, MVT::i16, InFlag);
1339 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001340 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1341 Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001342 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001343 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001344 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001345 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001346 MVT::i8, Result, SRIdx), 0);
1347 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001348 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001349 HiReg, NVT, InFlag);
1350 InFlag = Result.getValue(2);
1351 }
1352 ReplaceUses(N.getValue(1), Result);
1353#ifndef NDEBUG
1354 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001355 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001356 DOUT << "\n";
1357#endif
1358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359
1360#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 Indent -= 2;
1362#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001363
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364 return NULL;
1365 }
1366
Dan Gohman5a199552007-10-08 18:33:35 +00001367 case ISD::SDIVREM:
1368 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001369 SDValue N0 = Node->getOperand(0);
1370 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001371
1372 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001374 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 default: assert(0 && "Unsupported VT!");
1376 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1377 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1378 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1379 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1380 }
1381 else
Duncan Sands92c43912008-06-06 12:08:01 +00001382 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 default: assert(0 && "Unsupported VT!");
1384 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1385 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1386 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1387 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1388 }
1389
1390 unsigned LoReg, HiReg;
1391 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001392 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 default: assert(0 && "Unsupported VT!");
1394 case MVT::i8:
1395 LoReg = X86::AL; HiReg = X86::AH;
1396 ClrOpcode = 0;
1397 SExtOpcode = X86::CBW;
1398 break;
1399 case MVT::i16:
1400 LoReg = X86::AX; HiReg = X86::DX;
1401 ClrOpcode = X86::MOV16r0;
1402 SExtOpcode = X86::CWD;
1403 break;
1404 case MVT::i32:
1405 LoReg = X86::EAX; HiReg = X86::EDX;
1406 ClrOpcode = X86::MOV32r0;
1407 SExtOpcode = X86::CDQ;
1408 break;
1409 case MVT::i64:
1410 LoReg = X86::RAX; HiReg = X86::RDX;
1411 ClrOpcode = X86::MOV64r0;
1412 SExtOpcode = X86::CQO;
1413 break;
1414 }
1415
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001417 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001418 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001419
Dan Gohman8181bd12008-07-27 21:46:04 +00001420 SDValue InFlag;
Dan Gohman7bbd9202009-01-21 14:50:16 +00001421 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 // Special case for div8, just use a move with zero extension to AX to
1423 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001424 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001428 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
1429 MVT::Other, Ops, 5), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 Chain = Move.getValue(1);
1431 ReplaceUses(N0.getValue(1), Chain);
1432 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001434 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 Chain = CurDAG->getEntryNode();
1436 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001437 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 InFlag = Chain.getValue(1);
1439 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 InFlag =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001441 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00001442 LoReg, N0, SDValue()).getValue(1);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001443 if (isSigned && !signBitIsZero) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 // Sign extend the low part into the high part.
1445 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001446 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 } else {
1448 // Zero out the high part, effectively zero extending the input.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001449 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1450 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001451 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman5a199552007-10-08 18:33:35 +00001452 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 }
1454 }
1455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 SDNode *CNode =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001459 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001461 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001462 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001465 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 }
1467
Dan Gohman242a5ba2007-09-25 18:23:27 +00001468 // Copy the division (low) result, if it is needed.
1469 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001470 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001471 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001472 InFlag = Result.getValue(2);
1473 ReplaceUses(N.getValue(0), Result);
1474#ifndef NDEBUG
1475 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001476 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001477 DOUT << "\n";
1478#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001479 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001480 // Copy the remainder (high) result, if it is needed.
1481 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001482 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001483 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1484 // Prevent use of AH in a REX instruction by referencing AX instead.
1485 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001486 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001487 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001488 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001489 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1490 Result,
1491 CurDAG->getTargetConstant(8, MVT::i8)),
1492 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001493 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001494 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001495 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001496 MVT::i8, Result, SRIdx), 0);
1497 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001498 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001499 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001500 InFlag = Result.getValue(2);
1501 }
1502 ReplaceUses(N.getValue(1), Result);
1503#ifndef NDEBUG
1504 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001505 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001506 DOUT << "\n";
1507#endif
1508 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509
1510#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 Indent -= 2;
1512#endif
1513
1514 return NULL;
1515 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001516
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001517 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001518 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001519 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1520 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001521
Dan Gohmandd612bb2008-08-20 21:27:32 +00001522 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1523 unsigned Opc = 0;
1524 switch (NVT.getSimpleVT()) {
1525 default: assert(0 && "Unknown sign_extend_inreg!");
1526 case MVT::i16:
1527 Opc = X86::MOVSX16rr8;
1528 break;
1529 case MVT::i32:
1530 Opc = X86::MOVSX32rr8;
1531 break;
1532 }
1533
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001534 SDNode *ResNode = CurDAG->getTargetNode(Opc, dl, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001535
1536#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001537 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001538 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001539 DOUT << "\n";
1540 DOUT << std::string(Indent-2, ' ') << "=> ";
1541 DEBUG(ResNode->dump(CurDAG));
1542 DOUT << "\n";
1543 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001544#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001545 return ResNode;
1546 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001547 break;
1548 }
1549
1550 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001551 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1552 SDValue Input = Node->getOperand(0);
Dan Gohmandd612bb2008-08-20 21:27:32 +00001553 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555#ifndef NDEBUG
1556 DOUT << std::string(Indent-2, ' ') << "=> ";
1557 DEBUG(ResNode->dump(CurDAG));
1558 DOUT << "\n";
1559 Indent -= 2;
1560#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001561 return ResNode;
1562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 break;
1564 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001565
1566 case ISD::DECLARE: {
1567 // Handle DECLARE nodes here because the second operand may have been
1568 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001569 SDValue Chain = Node->getOperand(0);
1570 SDValue N1 = Node->getOperand(1);
1571 SDValue N2 = Node->getOperand(2);
Evan Cheng417bc002008-12-10 21:49:05 +00001572 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001573
1574 // FIXME: We need to handle this for VLAs.
1575 if (!FINode) {
1576 ReplaceUses(N.getValue(0), Chain);
1577 return NULL;
1578 }
1579
Evan Cheng651e1442008-06-18 02:48:27 +00001580 if (N2.getOpcode() == ISD::ADD &&
1581 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1582 N2 = N2.getOperand(1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001583
1584 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1585 // somehow, just ignore it.
1586 if (N2.getOpcode() != X86ISD::Wrapper) {
1587 ReplaceUses(N.getValue(0), Chain);
1588 return NULL;
1589 }
Evan Chengf3ecd1a2009-01-10 03:33:22 +00001590 GlobalAddressSDNode *GVNode =
1591 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattneref6a8192009-02-12 17:33:11 +00001592 if (GVNode == 0) {
1593 ReplaceUses(N.getValue(0), Chain);
1594 return NULL;
1595 }
Evan Cheng417bc002008-12-10 21:49:05 +00001596 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1597 TLI.getPointerTy());
1598 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1599 TLI.getPointerTy());
1600 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001601 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Evan Cheng417bc002008-12-10 21:49:05 +00001602 MVT::Other, Ops, 3);
Evan Chengd4cebcd2008-06-17 02:01:22 +00001603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604 }
1605
1606 SDNode *ResNode = SelectCode(N);
1607
1608#ifndef NDEBUG
1609 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001610 if (ResNode == NULL || ResNode == N.getNode())
1611 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 else
1613 DEBUG(ResNode->dump(CurDAG));
1614 DOUT << "\n";
1615 Indent -= 2;
1616#endif
1617
1618 return ResNode;
1619}
1620
1621bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001622SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001623 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001624 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 switch (ConstraintCode) {
1626 case 'o': // offsetable ??
1627 case 'v': // not offsetable ??
1628 default: return true;
1629 case 'm': // memory
1630 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1631 return true;
1632 break;
1633 }
1634
1635 OutOps.push_back(Op0);
1636 OutOps.push_back(Op1);
1637 OutOps.push_back(Op2);
1638 OutOps.push_back(Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 return false;
1640}
1641
1642/// createX86ISelDag - This pass converts a legalized DAG into a
1643/// X86-specific DAG, ready for instruction scheduling.
1644///
1645FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1646 return new X86DAGToDAGISel(TM, Fast);
1647}