blob: 137944ac88d113214f0ee301fdc4f6bba8bfc480 [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
44STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
45STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047//===----------------------------------------------------------------------===//
48// Pattern Matcher Implementation
49//===----------------------------------------------------------------------===//
50
51namespace {
52 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000053 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 /// tree.
55 struct X86ISelAddressMode {
56 enum {
57 RegBase,
58 FrameIndexBase
59 } BaseType;
60
61 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000062 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 int FrameIndex;
64 } Base;
65
Evan Cheng3b5a1272008-02-07 08:53:49 +000066 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000068 SDValue IndexReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 unsigned Disp;
70 GlobalValue *GV;
71 Constant *CP;
72 const char *ES;
73 int JT;
74 unsigned Align; // CP alignment.
75
76 X86ISelAddressMode()
77 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
79 }
Dale Johannesenc501c082008-08-11 23:46:25 +000080 void dump() {
81 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000082 cerr << "Base.Reg ";
83 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
84 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000085 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
86 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000087 cerr << "IndexReg ";
88 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
89 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000090 cerr << " Disp " << Disp << "\n";
91 cerr << "GV "; if (GV) GV->dump();
92 else cerr << "nul";
93 cerr << " CP "; if (CP) CP->dump();
94 else cerr << "nul";
95 cerr << "\n";
96 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
97 cerr << " JT" << JT << " Align" << Align << "\n";
98 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 };
100}
101
102namespace {
103 //===--------------------------------------------------------------------===//
104 /// ISel - X86 specific code to select X86 machine instructions for
105 /// SelectionDAG operations.
106 ///
107 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 /// TM - Keep a reference to X86TargetMachine.
109 ///
110 X86TargetMachine &TM;
111
112 /// X86Lowering - This object fully describes how to lower LLVM code to an
113 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000114 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
116 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
117 /// make the right decision when generating code for different targets.
118 const X86Subtarget *Subtarget;
119
Evan Cheng34fd4f32008-06-30 20:45:06 +0000120 /// CurBB - Current BB being isel'd.
121 ///
122 MachineBasicBlock *CurBB;
123
Evan Cheng13559d62008-09-26 23:41:32 +0000124 /// OptForSize - If true, selector should try to optimize for code size
125 /// instead of performance.
126 bool OptForSize;
127
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 public:
129 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohmanf2b29572008-10-03 16:55:19 +0000130 : SelectionDAGISel(*tm.getTargetLowering(), fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000131 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000132 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000133 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 virtual const char *getPassName() const {
136 return "X86 DAG->DAG Instruction Selection";
137 }
138
Evan Cheng34fd4f32008-06-30 20:45:06 +0000139 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000141 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000142
143 /// InstructionSelectPostProcessing - Post processing of selected and
144 /// scheduled basic blocks.
Dan Gohmanb552df72008-07-21 20:00:07 +0000145 virtual void InstructionSelectPostProcessing();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000147 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
148
Dan Gohmand6098272007-07-24 23:00:27 +0000149 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150
151// Include the pieces autogenerated from the target description.
152#include "X86GenDAGISel.inc"
153
154 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000155 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000156 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Dan Gohman8181bd12008-07-27 21:46:04 +0000158 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000160 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000161 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
163 SDValue &Scale, SDValue &Index, SDValue &Disp);
164 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
165 SDValue &Scale, SDValue &Index, SDValue &Disp);
166 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
167 SDValue N, SDValue &Base, SDValue &Scale,
168 SDValue &Index, SDValue &Disp,
169 SDValue &InChain, SDValue &OutChain);
170 bool TryFoldLoad(SDValue P, SDValue N,
171 SDValue &Base, SDValue &Scale,
172 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000173 void PreprocessForRMW();
174 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
176 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
177 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000178 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000180 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000182 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
183
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
185 SDValue &Scale, SDValue &Index,
186 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
188 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
189 AM.Base.Reg;
190 Scale = getI8Imm(AM.Scale);
191 Index = AM.IndexReg;
192 // These are 32-bit even in 64-bit mode since RIP relative offset
193 // is 32-bit.
194 if (AM.GV)
195 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
196 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000197 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
198 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000200 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 else if (AM.JT != -1)
202 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
203 else
204 Disp = getI32Imm(AM.Disp);
205 }
206
207 /// getI8Imm - Return a target constant with the specified value, of type
208 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 return CurDAG->getTargetConstant(Imm, MVT::i8);
211 }
212
213 /// getI16Imm - Return a target constant with the specified value, of type
214 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000215 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 return CurDAG->getTargetConstant(Imm, MVT::i16);
217 }
218
219 /// getI32Imm - Return a target constant with the specified value, of type
220 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000221 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 return CurDAG->getTargetConstant(Imm, MVT::i32);
223 }
224
Dan Gohmanb60482f2008-09-23 18:22:58 +0000225 /// getGlobalBaseReg - Return an SDNode that returns the value of
226 /// the global base register. Output instructions required to
227 /// initialize the global base register, if necessary.
228 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 SDNode *getGlobalBaseReg();
230
Dan Gohmandd612bb2008-08-20 21:27:32 +0000231 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
232 /// truncate of the specified operand to i8. This can be done with tablegen,
233 /// except that this code uses MVT::Flag in a tricky way that happens to
234 /// improve scheduling in some cases.
235 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237#ifndef NDEBUG
238 unsigned Indent;
239#endif
240 };
241}
242
Gabor Greife9f7f582008-08-31 15:37:04 +0000243/// findFlagUse - Return use of MVT::Flag value produced by the specified
244/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000245///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246static SDNode *findFlagUse(SDNode *N) {
247 unsigned FlagResNo = N->getNumValues()-1;
248 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000249 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000251 SDValue Op = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +0000252 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 return User;
254 }
255 }
256 return NULL;
257}
258
Evan Cheng656269e2008-04-25 08:22:20 +0000259/// findNonImmUse - Return true by reference in "found" if "Use" is an
260/// non-immediate use of "Def". This function recursively traversing
261/// up the operand chain ignoring certain nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman602d44a2008-09-17 01:39:10 +0000263 SDNode *Root, bool &found,
Evan Cheng656269e2008-04-25 08:22:20 +0000264 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 if (found ||
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000266 Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000267 !Visited.insert(Use))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 return;
Evan Cheng656269e2008-04-25 08:22:20 +0000269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000271 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000273 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000274 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000275 assert(N != Root);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 found = true;
277 break;
278 }
Evan Cheng656269e2008-04-25 08:22:20 +0000279
280 // Traverse up the operand chain.
Dan Gohman602d44a2008-09-17 01:39:10 +0000281 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 }
283}
284
285/// isNonImmUse - Start searching from Root up the DAG to check is Def can
286/// be reached. Return true if that's the case. However, ignore direct uses
287/// by ImmedUse (which would be U in the example illustrated in
288/// CanBeFoldedBy) and by Root (which can happen in the store case).
289/// FIXME: to be really generic, we should allow direct use by any node
290/// that is being folded. But realisticly since we only fold loads which
291/// have one non-chain use, we only need to watch out for load/op/store
292/// and load/op/cmp case where the root (store / cmp) may reach the load via
293/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000294static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000295 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 bool found = false;
Dan Gohman602d44a2008-09-17 01:39:10 +0000297 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return found;
299}
300
301
Dan Gohmand6098272007-07-24 23:00:27 +0000302bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000303 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
Dan Gohman602d44a2008-09-17 01:39:10 +0000305 // If Root use can somehow reach N through a path that that doesn't contain
306 // U then folding N would create a cycle. e.g. In the following
307 // diagram, Root can reach N through X. If N is folded into into Root, then
308 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000310 // [N*] //
311 // ^ ^ //
312 // / \ //
313 // [U*] [X]? //
314 // ^ ^ //
315 // \ / //
316 // \ / //
317 // [Root*] //
318 //
319 // * indicates nodes to be folded together.
320 //
321 // If Root produces a flag, then it gets (even more) interesting. Since it
322 // will be "glued" together with its flag use in the scheduler, we need to
323 // check if it might reach N.
324 //
325 // [N*] //
326 // ^ ^ //
327 // / \ //
328 // [U*] [X]? //
329 // ^ ^ //
330 // \ \ //
331 // \ | //
332 // [Root*] | //
333 // ^ | //
334 // f | //
335 // | / //
336 // [Y] / //
337 // ^ / //
338 // f / //
339 // | / //
340 // [FU] //
341 //
342 // If FU (flag use) indirectly reaches N (the load), and Root folds N
343 // (call it Fold), then X is a predecessor of FU and a successor of
344 // Fold. But since Fold and FU are flagged together, this will create
345 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
Duncan Sands92c43912008-06-06 12:08:01 +0000347 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000348 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 SDNode *FU = findFlagUse(Root);
350 if (FU == NULL)
351 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000352 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 VT = Root->getValueType(Root->getNumValues()-1);
354 }
355
Dan Gohman602d44a2008-09-17 01:39:10 +0000356 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357}
358
359/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
360/// and move load below the TokenFactor. Replace store's chain operand with
361/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000362static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000363 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000364 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000365 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
366 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000367 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000369 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000370 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
371 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
372 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
373 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374}
375
Evan Cheng2b2a7012008-05-23 21:23:16 +0000376/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
377///
Dan Gohman8181bd12008-07-27 21:46:04 +0000378static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
379 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000380 if (N.getOpcode() == ISD::BIT_CONVERT)
381 N = N.getOperand(0);
382
383 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
384 if (!LD || LD->isVolatile())
385 return false;
386 if (LD->getAddressingMode() != ISD::UNINDEXED)
387 return false;
388
389 ISD::LoadExtType ExtType = LD->getExtensionType();
390 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
391 return false;
392
393 if (N.hasOneUse() &&
394 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000395 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000396 Load = N;
397 return true;
398 }
399 return false;
400}
401
Evan Cheng98cfaf82008-08-25 21:27:18 +0000402/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
403/// operand and move load below the call's chain operand.
404static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
405 SDValue Call, SDValue Chain) {
406 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000407 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
408 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000409 Ops.push_back(Load.getOperand(0));
410 else
411 Ops.push_back(Chain.getOperand(i));
412 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
413 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
414 Load.getOperand(1), Load.getOperand(2));
415 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000416 Ops.push_back(SDValue(Load.getNode(), 1));
417 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000418 Ops.push_back(Call.getOperand(i));
419 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
420}
421
422/// isCalleeLoad - Return true if call address is a load and it can be
423/// moved below CALLSEQ_START and the chains leading up to the call.
424/// Return the CALLSEQ_START by reference as a second output.
425static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000426 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000427 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000428 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000429 if (!LD ||
430 LD->isVolatile() ||
431 LD->getAddressingMode() != ISD::UNINDEXED ||
432 LD->getExtensionType() != ISD::NON_EXTLOAD)
433 return false;
434
435 // Now let's find the callseq_start.
436 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
437 if (!Chain.hasOneUse())
438 return false;
439 Chain = Chain.getOperand(0);
440 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000441 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Cheng98cfaf82008-08-25 21:27:18 +0000442}
443
444
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000445/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
446/// This is only run if not in -fast mode (aka -O0).
447/// This allows the instruction selector to pick more read-modify-write
448/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449///
450/// [Load chain]
451/// ^
452/// |
453/// [Load]
454/// ^ ^
455/// | |
456/// / \-
457/// / |
458/// [TokenFactor] [Op]
459/// ^ ^
460/// | |
461/// \ /
462/// \ /
463/// [Store]
464///
465/// The fact the store's chain operand != load's chain will prevent the
466/// (store (op (load))) instruction from being selected. We can transform it to:
467///
468/// [Load chain]
469/// ^
470/// |
471/// [TokenFactor]
472/// ^
473/// |
474/// [Load]
475/// ^ ^
476/// | |
477/// | \-
478/// | |
479/// | [Op]
480/// | ^
481/// | |
482/// \ /
483/// \ /
484/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000485void X86DAGToDAGISel::PreprocessForRMW() {
486 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
487 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000488 if (I->getOpcode() == X86ISD::CALL) {
489 /// Also try moving call address load from outside callseq_start to just
490 /// before the call to allow it to be folded.
491 ///
492 /// [Load chain]
493 /// ^
494 /// |
495 /// [Load]
496 /// ^ ^
497 /// | |
498 /// / \--
499 /// / |
500 ///[CALLSEQ_START] |
501 /// ^ |
502 /// | |
503 /// [LOAD/C2Reg] |
504 /// | |
505 /// \ /
506 /// \ /
507 /// [CALL]
508 SDValue Chain = I->getOperand(0);
509 SDValue Load = I->getOperand(1);
510 if (!isCalleeLoad(Load, Chain))
511 continue;
512 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
513 ++NumLoadMoved;
514 continue;
515 }
516
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 if (!ISD::isNON_TRUNCStore(I))
518 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000519 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000520
Gabor Greif1c80d112008-08-28 21:40:38 +0000521 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 continue;
523
Dan Gohman8181bd12008-07-27 21:46:04 +0000524 SDValue N1 = I->getOperand(1);
525 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000526 if ((N1.getValueType().isFloatingPoint() &&
527 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 !N1.hasOneUse())
529 continue;
530
531 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000532 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000533 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000535 case ISD::ADD:
536 case ISD::MUL:
537 case ISD::AND:
538 case ISD::OR:
539 case ISD::XOR:
540 case ISD::ADDC:
541 case ISD::ADDE:
542 case ISD::VECTOR_SHUFFLE: {
543 SDValue N10 = N1.getOperand(0);
544 SDValue N11 = N1.getOperand(1);
545 RModW = isRMWLoad(N10, Chain, N2, Load);
546 if (!RModW)
547 RModW = isRMWLoad(N11, Chain, N2, Load);
548 break;
549 }
550 case ISD::SUB:
551 case ISD::SHL:
552 case ISD::SRA:
553 case ISD::SRL:
554 case ISD::ROTL:
555 case ISD::ROTR:
556 case ISD::SUBC:
557 case ISD::SUBE:
558 case X86ISD::SHLD:
559 case X86ISD::SHRD: {
560 SDValue N10 = N1.getOperand(0);
561 RModW = isRMWLoad(N10, Chain, N2, Load);
562 break;
563 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 }
565
566 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000567 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 ++NumLoadMoved;
569 }
570 }
571}
572
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000573
574/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
575/// nodes that target the FP stack to be store and load to the stack. This is a
576/// gross hack. We would like to simply mark these as being illegal, but when
577/// we do that, legalize produces these when it expands calls, then expands
578/// these in the same legalize pass. We would like dag combine to be able to
579/// hack on these between the call expansion and the node legalization. As such
580/// this pass basically does "really late" legalization of these inline with the
581/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000582void X86DAGToDAGISel::PreprocessForFPConvert() {
583 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
584 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000585 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
586 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
587 continue;
588
589 // If the source and destination are SSE registers, then this is a legal
590 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000591 MVT SrcVT = N->getOperand(0).getValueType();
592 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000593 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
594 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
595 if (SrcIsSSE && DstIsSSE)
596 continue;
597
Chris Lattner5d294e52008-03-09 07:05:32 +0000598 if (!SrcIsSSE && !DstIsSSE) {
599 // If this is an FPStack extension, it is a noop.
600 if (N->getOpcode() == ISD::FP_EXTEND)
601 continue;
602 // If this is a value-preserving FPStack truncation, it is a noop.
603 if (N->getConstantOperandVal(1))
604 continue;
605 }
606
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000607 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
608 // FPStack has extload and truncstore. SSE can fold direct loads into other
609 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000610 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000611 if (N->getOpcode() == ISD::FP_ROUND)
612 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
613 else
614 MemVT = SrcIsSSE ? SrcVT : DstVT;
615
Dan Gohman14a66442008-08-23 02:25:05 +0000616 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000617
618 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman14a66442008-08-23 02:25:05 +0000619 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
620 N->getOperand(0),
621 MemTmp, NULL, 0, MemVT);
622 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
623 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000624
625 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
626 // extload we created. This will cause general havok on the dag because
627 // anything below the conversion could be folded into other existing nodes.
628 // To avoid invalidating 'I', back it up to the convert node.
629 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000630 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000631
632 // Now that we did that, the node is dead. Increment the iterator to the
633 // next node to process, then delete N.
634 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000635 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000636 }
637}
638
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
640/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000641void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000642 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000643 const Function *F = CurDAG->getMachineFunction().getFunction();
644 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645
Evan Cheng34fd4f32008-06-30 20:45:06 +0000646 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000647 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000648 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000649
650 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000651 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652
653 // Codegen the basic block.
654#ifndef NDEBUG
655 DOUT << "===== Instruction selection begins:\n";
656 Indent = 0;
657#endif
David Greene932618b2008-10-27 21:56:29 +0000658 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659#ifndef NDEBUG
660 DOUT << "===== Instruction selection ends:\n";
661#endif
662
Dan Gohman14a66442008-08-23 02:25:05 +0000663 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000664}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665
Dan Gohmanb552df72008-07-21 20:00:07 +0000666void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 // If we are emitting FP stack code, scan the basic block to determine if this
668 // block defines any FP values. If so, put an FP_REG_KILL instruction before
669 // the terminator of the block.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000670
Dale Johannesen684887e2007-09-24 22:52:39 +0000671 // Note that FP stack instructions are used in all modes for long double,
672 // so we always need to do this check.
673 // Also note that it's possible for an FP stack register to be live across
674 // an instruction that produces multiple basic blocks (SSE CMOV) so we
675 // must check all the generated basic blocks.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000676
677 // Scan all of the machine instructions in these MBBs, checking for FP
678 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Cheng34fd4f32008-06-30 20:45:06 +0000679 MachineFunction::iterator MBBI = CurBB;
Chris Lattner04d64b22008-03-10 23:34:12 +0000680 MachineFunction::iterator EndMBB = BB; ++EndMBB;
681 for (; MBBI != EndMBB; ++MBBI) {
682 MachineBasicBlock *MBB = MBBI;
683
684 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
685 // before the return.
686 if (!MBB->empty()) {
687 MachineBasicBlock::iterator EndI = MBB->end();
688 --EndI;
689 if (EndI->getDesc().isReturn())
690 continue;
691 }
692
Dale Johannesen684887e2007-09-24 22:52:39 +0000693 bool ContainsFPCode = false;
Chris Lattner04d64b22008-03-10 23:34:12 +0000694 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000695 !ContainsFPCode && I != E; ++I) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000696 if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000697 const TargetRegisterClass *clas;
698 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000699 if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
Chris Lattner04d64b22008-03-10 23:34:12 +0000700 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner1b989192007-12-31 04:13:23 +0000701 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000702 X86::RFP32RegisterClass ||
703 clas == X86::RFP64RegisterClass ||
704 clas == X86::RFP80RegisterClass)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 ContainsFPCode = true;
706 break;
707 }
708 }
709 }
710 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000711 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
712 // a copy of the input value in this block. In SSE mode, we only care about
713 // 80-bit values.
714 if (!ContainsFPCode) {
715 // Final check, check LLVM BB's that are successors to the LLVM BB
716 // corresponding to BB for FP PHI nodes.
717 const BasicBlock *LLVMBB = BB->getBasicBlock();
718 const PHINode *PN;
719 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
720 !ContainsFPCode && SI != E; ++SI) {
721 for (BasicBlock::const_iterator II = SI->begin();
722 (PN = dyn_cast<PHINode>(II)); ++II) {
723 if (PN->getType()==Type::X86_FP80Ty ||
724 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
725 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
726 ContainsFPCode = true;
727 break;
728 }
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000729 }
730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000732 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
733 if (ContainsFPCode) {
Chris Lattner04d64b22008-03-10 23:34:12 +0000734 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen684887e2007-09-24 22:52:39 +0000735 TM.getInstrInfo()->get(X86::FP_REG_KILL));
736 ++NumFPKill;
737 }
Chris Lattner04d64b22008-03-10 23:34:12 +0000738 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739}
740
Anton Korobeynikov34ef31e2007-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())
747 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
748}
749
750void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
751 // If this is main, emit special code for main.
752 MachineBasicBlock *BB = MF.begin();
753 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
754 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
755}
756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757/// MatchAddress - Add the specified node to the specified addressing mode,
758/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000759/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000760bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 bool isRoot, unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000762 bool is64Bit = Subtarget->is64Bit();
Evan Cheng7f250d62008-09-24 00:05:32 +0000763 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000764 // Limit recursion.
765 if (Depth > 5)
766 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767
768 // RIP relative addressing: %rip + 32-bit displacement!
769 if (AM.isRIPRel) {
770 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman40686732008-09-26 21:54:37 +0000771 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000772 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 AM.Disp += Val;
774 return false;
775 }
776 }
777 return true;
778 }
779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 switch (N.getOpcode()) {
781 default: break;
782 case ISD::Constant: {
Dan Gohman40686732008-09-26 21:54:37 +0000783 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000784 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 AM.Disp += Val;
786 return false;
787 }
788 break;
789 }
790
791 case X86ISD::Wrapper: {
Dan Gohman36322c72008-10-18 02:06:02 +0000792 DOUT << "Wrapper: 64bit " << is64Bit;
793 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000795 // Also, base and index reg must be 0 in order to use rip as base.
796 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000797 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 break;
799 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
800 break;
801 // If value is available in a register both base and index components have
802 // been picked, we can't fit the result available in the register in the
803 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohmancc3df852008-11-05 04:14:16 +0000804 {
Dan Gohman8181bd12008-07-27 21:46:04 +0000805 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000807 if (!is64Bit || isInt32(AM.Disp + G->getOffset())) {
808 GlobalValue *GV = G->getGlobal();
809 AM.GV = GV;
810 AM.Disp += G->getOffset();
811 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
812 return false;
813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000815 if (!is64Bit || isInt32(AM.Disp + CP->getOffset())) {
816 AM.CP = CP->getConstVal();
817 AM.Align = CP->getAlignment();
818 AM.Disp += CP->getOffset();
819 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
820 return false;
821 }
Bill Wendlingfef06052008-09-16 21:48:12 +0000822 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000823 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000824 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000825 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000827 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000828 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000829 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 }
831 }
832 break;
833 }
834
835 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000836 if (AM.BaseType == X86ISelAddressMode::RegBase
837 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
839 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
840 return false;
841 }
842 break;
843
844 case ISD::SHL:
Dan Gohmancc3df852008-11-05 04:14:16 +0000845 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000846 break;
847
Gabor Greife9f7f582008-08-31 15:37:04 +0000848 if (ConstantSDNode
849 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000850 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000851 if (Val == 1 || Val == 2 || Val == 3) {
852 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000853 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854
Chris Lattner7f06edd2007-12-08 07:22:58 +0000855 // Okay, we know that we have a scale by now. However, if the scaled
856 // value is an add of something and a constant, we can fold the
857 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000858 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
859 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
860 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000861 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000862 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000863 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000864 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000865 AM.Disp = Disp;
866 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000868 } else {
869 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000871 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 }
873 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000874 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
Dan Gohman35b99222007-10-22 20:22:24 +0000876 case ISD::SMUL_LOHI:
877 case ISD::UMUL_LOHI:
878 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000879 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000880 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 case ISD::MUL:
882 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000883 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000884 AM.Base.Reg.getNode() == 0 &&
885 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000886 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000887 if (ConstantSDNode
888 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000889 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
890 CN->getZExtValue() == 9) {
891 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892
Gabor Greif1c80d112008-08-28 21:40:38 +0000893 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000894 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895
896 // 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 Greif1c80d112008-08-28 21:40:38 +0000899 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
900 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
901 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000903 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000904 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
905 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000906 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 AM.Disp = Disp;
908 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000909 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000911 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 }
913
914 AM.IndexReg = AM.Base.Reg = Reg;
915 return false;
916 }
917 }
918 break;
919
920 case ISD::ADD:
Dan Gohmancc3df852008-11-05 04:14:16 +0000921 {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 X86ISelAddressMode Backup = AM;
Gabor Greif1c80d112008-08-28 21:40:38 +0000923 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
924 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 return false;
926 AM = Backup;
Gabor Greif1c80d112008-08-28 21:40:38 +0000927 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
928 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 return false;
930 AM = Backup;
931 }
932 break;
933
934 case ISD::OR:
935 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000936 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
937 X86ISelAddressMode Backup = AM;
938 // Start with the LHS as an addr mode.
939 if (!MatchAddress(N.getOperand(0), AM, false) &&
940 // Address could not have picked a GV address for the displacement.
941 AM.GV == NULL &&
942 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman36322c72008-10-18 02:06:02 +0000943 (!is64Bit || isInt32(AM.Disp + CN->getSExtValue())) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000944 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000945 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000946 AM.Disp += CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000947 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000949 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 }
951 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000952
953 case ISD::AND: {
954 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
955 // allows us to fold the shift into this addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000956 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000957 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohmancc3df852008-11-05 04:14:16 +0000958
Evan Chengf2abee72007-12-13 00:43:27 +0000959 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000960 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000961
962 // Not when RIP is used as the base.
963 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000964
965 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
966 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
967 if (!C1 || !C2) break;
968
969 // Not likely to be profitable if either the AND or SHIFT node has more
970 // than one use (unless all uses are for address computation). Besides,
971 // isel mechanism requires their node ids to be reused.
972 if (!N.hasOneUse() || !Shift.hasOneUse())
973 break;
974
975 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000976 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000977 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
978 break;
979
980 // Get the new AND mask, this folds to a constant.
Dan Gohmancc3df852008-11-05 04:14:16 +0000981 SDValue X = Shift.getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000982 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +0000983 SDValue(C2, 0), SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000984 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), X, NewANDMask);
Dan Gohman3666f472008-10-13 20:52:04 +0000985 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(),
986 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000987
988 // Insert the new nodes into the topological ordering.
989 if (C1->getNodeId() > X.getNode()->getNodeId()) {
990 CurDAG->RepositionNode(X.getNode(), C1);
991 C1->setNodeId(X.getNode()->getNodeId());
992 }
993 if (NewANDMask.getNode()->getNodeId() == -1 ||
994 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
995 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
996 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
997 }
998 if (NewAND.getNode()->getNodeId() == -1 ||
999 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1000 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1001 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1002 }
1003 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1004 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1005 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1006 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1007 }
1008
Dan Gohman3666f472008-10-13 20:52:04 +00001009 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +00001010
1011 AM.Scale = 1 << ShiftCst;
1012 AM.IndexReg = NewAND;
1013 return false;
1014 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 }
1016
Dan Gohmana60c1b32007-08-13 20:03:06 +00001017 return MatchAddressBase(N, AM, isRoot, Depth);
1018}
1019
1020/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1021/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +00001022bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +00001023 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001025 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +00001027 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 AM.IndexReg = N;
1029 AM.Scale = 1;
1030 return false;
1031 }
1032
1033 // Otherwise, we cannot select it.
1034 return true;
1035 }
1036
1037 // Default, generate it as a register.
1038 AM.BaseType = X86ISelAddressMode::RegBase;
1039 AM.Base.Reg = N;
1040 return false;
1041}
1042
1043/// SelectAddr - returns true if it is able pattern match an addressing mode.
1044/// It returns the operands which make up the maximal addressing mode it can
1045/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001046bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1047 SDValue &Scale, SDValue &Index,
1048 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 X86ISelAddressMode AM;
1050 if (MatchAddress(N, AM))
1051 return false;
1052
Duncan Sands92c43912008-06-06 12:08:01 +00001053 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001055 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 AM.Base.Reg = CurDAG->getRegister(0, VT);
1057 }
1058
Gabor Greif1c80d112008-08-28 21:40:38 +00001059 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 AM.IndexReg = CurDAG->getRegister(0, VT);
1061
1062 getAddressOperands(AM, Base, Scale, Index, Disp);
1063 return true;
1064}
1065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1067/// match a load whose top elements are either undef or zeros. The load flavor
1068/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001069bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1070 SDValue N, SDValue &Base,
1071 SDValue &Scale, SDValue &Index,
1072 SDValue &Disp, SDValue &InChain,
1073 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1075 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001076 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 InChain.getValue(0).hasOneUse() &&
1078 N.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001079 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1081 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1082 return false;
1083 OutChain = LD->getChain();
1084 return true;
1085 }
1086 }
1087
1088 // Also handle the case where we explicitly require zeros in the top
1089 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001090 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001091 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001092 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001093 N.getOperand(0).getNode()->hasOneUse() &&
1094 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001095 N.getOperand(0).getOperand(0).hasOneUse()) {
1096 // Okay, this is a zero extending load. Fold it.
1097 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1098 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1099 return false;
1100 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001101 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001102 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 }
1104 return false;
1105}
1106
1107
1108/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1109/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001110bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1111 SDValue &Base, SDValue &Scale,
1112 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 X86ISelAddressMode AM;
1114 if (MatchAddress(N, AM))
1115 return false;
1116
Duncan Sands92c43912008-06-06 12:08:01 +00001117 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 unsigned Complexity = 0;
1119 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001120 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 Complexity = 1;
1122 else
1123 AM.Base.Reg = CurDAG->getRegister(0, VT);
1124 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1125 Complexity = 4;
1126
Gabor Greif1c80d112008-08-28 21:40:38 +00001127 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 Complexity++;
1129 else
1130 AM.IndexReg = CurDAG->getRegister(0, VT);
1131
1132 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1133 // a simple shift.
1134 if (AM.Scale > 1)
1135 Complexity++;
1136
1137 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1138 // to a LEA. This is determined with some expermentation but is by no means
1139 // optimal (especially for code size consideration). LEA is nice because of
1140 // its three-address nature. Tweak the cost function again when we can run
1141 // convertToThreeAddress() at register allocation time.
1142 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1143 // For X86-64, we should always use lea to materialize RIP relative
1144 // addresses.
1145 if (Subtarget->is64Bit())
1146 Complexity = 4;
1147 else
1148 Complexity += 2;
1149 }
1150
Gabor Greif1c80d112008-08-28 21:40:38 +00001151 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 Complexity++;
1153
1154 if (Complexity > 2) {
1155 getAddressOperands(AM, Base, Scale, Index, Disp);
1156 return true;
1157 }
1158 return false;
1159}
1160
Dan Gohman8181bd12008-07-27 21:46:04 +00001161bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1162 SDValue &Base, SDValue &Scale,
1163 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001164 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 N.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001166 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1168 return false;
1169}
1170
Dan Gohmanb60482f2008-09-23 18:22:58 +00001171/// getGlobalBaseReg - Return an SDNode that returns the value of
1172/// the global base register. Output instructions required to
1173/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174///
1175SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001176 MachineFunction *MF = CurBB->getParent();
1177 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001178 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179}
1180
1181static SDNode *FindCallStartFromCall(SDNode *Node) {
1182 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1183 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1184 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001185 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186}
1187
Dan Gohmandd612bb2008-08-20 21:27:32 +00001188/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1189/// truncate of the specified operand to i8. This can be done with tablegen,
1190/// except that this code uses MVT::Flag in a tricky way that happens to
1191/// improve scheduling in some cases.
1192SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1193 assert(!Subtarget->is64Bit() &&
1194 "getTruncateTo8Bit is only needed on x86-32!");
1195 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1196
1197 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1198 unsigned Opc;
1199 MVT N0VT = N0.getValueType();
1200 switch (N0VT.getSimpleVT()) {
1201 default: assert(0 && "Unknown truncate!");
1202 case MVT::i16:
1203 Opc = X86::MOV16to16_;
1204 break;
1205 case MVT::i32:
1206 Opc = X86::MOV32to32_;
1207 break;
1208 }
1209
1210 // The use of MVT::Flag here is not strictly accurate, but it helps
1211 // scheduling in some cases.
1212 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1213 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1214 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001215}
1216
Dale Johannesenf160d802008-10-02 18:53:47 +00001217SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1218 SDValue Chain = Node->getOperand(0);
1219 SDValue In1 = Node->getOperand(1);
1220 SDValue In2L = Node->getOperand(2);
1221 SDValue In2H = Node->getOperand(3);
1222 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1223 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1224 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001225 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001226 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1227 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1228}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001229
Dan Gohman8181bd12008-07-27 21:46:04 +00001230SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001231 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001232 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 unsigned Opc, MOpc;
1234 unsigned Opcode = Node->getOpcode();
1235
1236#ifndef NDEBUG
1237 DOUT << std::string(Indent, ' ') << "Selecting: ";
1238 DEBUG(Node->dump(CurDAG));
1239 DOUT << "\n";
1240 Indent += 2;
1241#endif
1242
Dan Gohmanbd68c792008-07-17 19:10:17 +00001243 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244#ifndef NDEBUG
1245 DOUT << std::string(Indent-2, ' ') << "== ";
1246 DEBUG(Node->dump(CurDAG));
1247 DOUT << "\n";
1248 Indent -= 2;
1249#endif
1250 return NULL; // Already selected.
1251 }
1252
1253 switch (Opcode) {
1254 default: break;
1255 case X86ISD::GlobalBaseReg:
1256 return getGlobalBaseReg();
1257
Dale Johannesenf160d802008-10-02 18:53:47 +00001258 case X86ISD::ATOMOR64_DAG:
1259 return SelectAtomic64(Node, X86::ATOMOR6432);
1260 case X86ISD::ATOMXOR64_DAG:
1261 return SelectAtomic64(Node, X86::ATOMXOR6432);
1262 case X86ISD::ATOMADD64_DAG:
1263 return SelectAtomic64(Node, X86::ATOMADD6432);
1264 case X86ISD::ATOMSUB64_DAG:
1265 return SelectAtomic64(Node, X86::ATOMSUB6432);
1266 case X86ISD::ATOMNAND64_DAG:
1267 return SelectAtomic64(Node, X86::ATOMNAND6432);
1268 case X86ISD::ATOMAND64_DAG:
1269 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001270 case X86ISD::ATOMSWAP64_DAG:
1271 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001272
Dan Gohman5a199552007-10-08 18:33:35 +00001273 case ISD::SMUL_LOHI:
1274 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001275 SDValue N0 = Node->getOperand(0);
1276 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001277
Dan Gohman5a199552007-10-08 18:33:35 +00001278 bool isSigned = Opcode == ISD::SMUL_LOHI;
1279 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001280 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 default: assert(0 && "Unsupported VT!");
1282 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1283 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1284 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1285 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1286 }
1287 else
Duncan Sands92c43912008-06-06 12:08:01 +00001288 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 default: assert(0 && "Unsupported VT!");
1290 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1291 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1292 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1293 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1294 }
1295
1296 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001297 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 default: assert(0 && "Unsupported VT!");
1299 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1300 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1301 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1302 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1303 }
1304
Dan Gohman8181bd12008-07-27 21:46:04 +00001305 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001306 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001307 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 if (!foldedLoad) {
1309 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001310 if (foldedLoad)
1311 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 }
1313
Dan Gohman8181bd12008-07-27 21:46:04 +00001314 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1315 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316
1317 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001318 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 SDNode *CNode =
1320 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001321 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001322 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001323 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001326 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 }
1328
Dan Gohman5a199552007-10-08 18:33:35 +00001329 // Copy the low half of the result, if it is needed.
1330 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001331 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001332 LoReg, NVT, InFlag);
1333 InFlag = Result.getValue(2);
1334 ReplaceUses(N.getValue(0), Result);
1335#ifndef NDEBUG
1336 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001337 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001338 DOUT << "\n";
1339#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001340 }
Dan Gohman5a199552007-10-08 18:33:35 +00001341 // Copy the high half of the result, if it is needed.
1342 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001343 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001344 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1345 // Prevent use of AH in a REX instruction by referencing AX instead.
1346 // Shift it down 8 bits.
1347 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1348 X86::AX, MVT::i16, InFlag);
1349 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001350 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001351 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001352 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1354 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001355 MVT::i8, Result, SRIdx), 0);
1356 } else {
1357 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1358 HiReg, NVT, InFlag);
1359 InFlag = Result.getValue(2);
1360 }
1361 ReplaceUses(N.getValue(1), Result);
1362#ifndef NDEBUG
1363 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001364 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001365 DOUT << "\n";
1366#endif
1367 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368
1369#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 Indent -= 2;
1371#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001372
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 return NULL;
1374 }
1375
Dan Gohman5a199552007-10-08 18:33:35 +00001376 case ISD::SDIVREM:
1377 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001378 SDValue N0 = Node->getOperand(0);
1379 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001380
1381 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001383 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 default: assert(0 && "Unsupported VT!");
1385 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1386 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1387 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1388 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1389 }
1390 else
Duncan Sands92c43912008-06-06 12:08:01 +00001391 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 default: assert(0 && "Unsupported VT!");
1393 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1394 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1395 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1396 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1397 }
1398
1399 unsigned LoReg, HiReg;
1400 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001401 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402 default: assert(0 && "Unsupported VT!");
1403 case MVT::i8:
1404 LoReg = X86::AL; HiReg = X86::AH;
1405 ClrOpcode = 0;
1406 SExtOpcode = X86::CBW;
1407 break;
1408 case MVT::i16:
1409 LoReg = X86::AX; HiReg = X86::DX;
1410 ClrOpcode = X86::MOV16r0;
1411 SExtOpcode = X86::CWD;
1412 break;
1413 case MVT::i32:
1414 LoReg = X86::EAX; HiReg = X86::EDX;
1415 ClrOpcode = X86::MOV32r0;
1416 SExtOpcode = X86::CDQ;
1417 break;
1418 case MVT::i64:
1419 LoReg = X86::RAX; HiReg = X86::RDX;
1420 ClrOpcode = X86::MOV64r0;
1421 SExtOpcode = X86::CQO;
1422 break;
1423 }
1424
Dan Gohman8181bd12008-07-27 21:46:04 +00001425 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001426 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1427
Dan Gohman8181bd12008-07-27 21:46:04 +00001428 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 if (NVT == MVT::i8 && !isSigned) {
1430 // Special case for div8, just use a move with zero extension to AX to
1431 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001434 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 Ops, 5), 0);
1438 Chain = Move.getValue(1);
1439 ReplaceUses(N0.getValue(1), Chain);
1440 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001442 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 Chain = CurDAG->getEntryNode();
1444 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001446 InFlag = Chain.getValue(1);
1447 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001449 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001450 LoReg, N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 if (isSigned) {
1452 // Sign extend the low part into the high part.
1453 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001454 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 } else {
1456 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001458 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1459 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 }
1461 }
1462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001464 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 SDNode *CNode =
1466 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001467 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001468 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001469 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001472 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001473 }
1474
Dan Gohman242a5ba2007-09-25 18:23:27 +00001475 // Copy the division (low) result, if it is needed.
1476 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001478 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001479 InFlag = Result.getValue(2);
1480 ReplaceUses(N.getValue(0), Result);
1481#ifndef NDEBUG
1482 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001483 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001484 DOUT << "\n";
1485#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001486 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001487 // Copy the remainder (high) result, if it is needed.
1488 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001489 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001490 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1491 // Prevent use of AH in a REX instruction by referencing AX instead.
1492 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001493 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1494 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001495 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001496 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001497 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001498 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001499 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1500 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001501 MVT::i8, Result, SRIdx), 0);
1502 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001503 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1504 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001505 InFlag = Result.getValue(2);
1506 }
1507 ReplaceUses(N.getValue(1), Result);
1508#ifndef NDEBUG
1509 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001510 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001511 DOUT << "\n";
1512#endif
1513 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514
1515#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 Indent -= 2;
1517#endif
1518
1519 return NULL;
1520 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001521
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001522 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001523 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001524 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1525 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001526
Dan Gohmandd612bb2008-08-20 21:27:32 +00001527 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1528 unsigned Opc = 0;
1529 switch (NVT.getSimpleVT()) {
1530 default: assert(0 && "Unknown sign_extend_inreg!");
1531 case MVT::i16:
1532 Opc = X86::MOVSX16rr8;
1533 break;
1534 case MVT::i32:
1535 Opc = X86::MOVSX32rr8;
1536 break;
1537 }
1538
1539 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001540
1541#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001542 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001543 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001544 DOUT << "\n";
1545 DOUT << std::string(Indent-2, ' ') << "=> ";
1546 DEBUG(ResNode->dump(CurDAG));
1547 DOUT << "\n";
1548 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001549#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001550 return ResNode;
1551 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001552 break;
1553 }
1554
1555 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001556 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1557 SDValue Input = Node->getOperand(0);
Dan Gohmandd612bb2008-08-20 21:27:32 +00001558 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001559
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560#ifndef NDEBUG
1561 DOUT << std::string(Indent-2, ' ') << "=> ";
1562 DEBUG(ResNode->dump(CurDAG));
1563 DOUT << "\n";
1564 Indent -= 2;
1565#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001566 return ResNode;
1567 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 break;
1569 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001570
1571 case ISD::DECLARE: {
1572 // Handle DECLARE nodes here because the second operand may have been
1573 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001574 SDValue Chain = Node->getOperand(0);
1575 SDValue N1 = Node->getOperand(1);
1576 SDValue N2 = Node->getOperand(2);
Evan Cheng651e1442008-06-18 02:48:27 +00001577 if (!isa<FrameIndexSDNode>(N1))
1578 break;
1579 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1580 if (N2.getOpcode() == ISD::ADD &&
1581 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1582 N2 = N2.getOperand(1);
1583 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Chengd4cebcd2008-06-17 02:01:22 +00001584 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Chengd4cebcd2008-06-17 02:01:22 +00001585 GlobalValue *GV =
1586 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00001587 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1588 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Dan Gohman8181bd12008-07-27 21:46:04 +00001589 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Chengd4cebcd2008-06-17 02:01:22 +00001590 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1591 MVT::Other, Ops, 3);
1592 }
1593 break;
1594 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 }
1596
1597 SDNode *ResNode = SelectCode(N);
1598
1599#ifndef NDEBUG
1600 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001601 if (ResNode == NULL || ResNode == N.getNode())
1602 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 else
1604 DEBUG(ResNode->dump(CurDAG));
1605 DOUT << "\n";
1606 Indent -= 2;
1607#endif
1608
1609 return ResNode;
1610}
1611
1612bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001613SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001614 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001615 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616 switch (ConstraintCode) {
1617 case 'o': // offsetable ??
1618 case 'v': // not offsetable ??
1619 default: return true;
1620 case 'm': // memory
1621 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1622 return true;
1623 break;
1624 }
1625
1626 OutOps.push_back(Op0);
1627 OutOps.push_back(Op1);
1628 OutOps.push_back(Op2);
1629 OutOps.push_back(Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 return false;
1631}
1632
1633/// createX86ISelDag - This pass converts a legalized DAG into a
1634/// X86-specific DAG, ready for instruction scheduling.
1635///
1636FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1637 return new X86DAGToDAGISel(TM, Fast);
1638}