blob: a7820795a1a73ad8abbe5a7d71e4aa85bb22ddda [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"
42#include <queue>
43#include <set>
44using namespace llvm;
45
46STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
47STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
54 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
60 FrameIndexBase
61 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000064 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 int FrameIndex;
66 } Base;
67
Evan Cheng3b5a1272008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 unsigned Disp;
72 GlobalValue *GV;
73 Constant *CP;
74 const char *ES;
75 int JT;
76 unsigned Align; // CP alignment.
77
78 X86ISelAddressMode()
79 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
80 GV(0), CP(0), ES(0), JT(-1), Align(0) {
81 }
Dale Johannesenc501c082008-08-11 23:46:25 +000082 void dump() {
83 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000084 cerr << "Base.Reg ";
85 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
86 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000087 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
88 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000089 cerr << "IndexReg ";
90 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
91 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000092 cerr << " Disp " << Disp << "\n";
93 cerr << "GV "; if (GV) GV->dump();
94 else cerr << "nul";
95 cerr << " CP "; if (CP) CP->dump();
96 else cerr << "nul";
97 cerr << "\n";
98 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
99 cerr << " JT" << JT << " Align" << Align << "\n";
100 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 };
102}
103
104namespace {
105 //===--------------------------------------------------------------------===//
106 /// ISel - X86 specific code to select X86 machine instructions for
107 /// SelectionDAG operations.
108 ///
109 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 /// TM - Keep a reference to X86TargetMachine.
111 ///
112 X86TargetMachine &TM;
113
114 /// X86Lowering - This object fully describes how to lower LLVM code to an
115 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000116 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117
118 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
119 /// make the right decision when generating code for different targets.
120 const X86Subtarget *Subtarget;
121
Evan Cheng34fd4f32008-06-30 20:45:06 +0000122 /// CurBB - Current BB being isel'd.
123 ///
124 MachineBasicBlock *CurBB;
125
Evan Cheng13559d62008-09-26 23:41:32 +0000126 /// OptForSize - If true, selector should try to optimize for code size
127 /// instead of performance.
128 bool OptForSize;
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 public:
131 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohmanf2b29572008-10-03 16:55:19 +0000132 : SelectionDAGISel(*tm.getTargetLowering(), fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000133 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000134 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000135 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 virtual const char *getPassName() const {
138 return "X86 DAG->DAG Instruction Selection";
139 }
140
Evan Cheng34fd4f32008-06-30 20:45:06 +0000141 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000143 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000144
145 /// InstructionSelectPostProcessing - Post processing of selected and
146 /// scheduled basic blocks.
Dan Gohmanb552df72008-07-21 20:00:07 +0000147 virtual void InstructionSelectPostProcessing();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000149 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
150
Dan Gohmand6098272007-07-24 23:00:27 +0000151 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
153// Include the pieces autogenerated from the target description.
154#include "X86GenDAGISel.inc"
155
156 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000157 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000158 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Dan Gohman8181bd12008-07-27 21:46:04 +0000160 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000163 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
165 SDValue &Scale, SDValue &Index, SDValue &Disp);
166 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
167 SDValue &Scale, SDValue &Index, SDValue &Disp);
168 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
169 SDValue N, SDValue &Base, SDValue &Scale,
170 SDValue &Index, SDValue &Disp,
171 SDValue &InChain, SDValue &OutChain);
172 bool TryFoldLoad(SDValue P, SDValue N,
173 SDValue &Base, SDValue &Scale,
174 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000175 void PreprocessForRMW();
176 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
178 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
179 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000180 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000182 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000184 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
185
Dan Gohman8181bd12008-07-27 21:46:04 +0000186 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
187 SDValue &Scale, SDValue &Index,
188 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
190 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
191 AM.Base.Reg;
192 Scale = getI8Imm(AM.Scale);
193 Index = AM.IndexReg;
194 // These are 32-bit even in 64-bit mode since RIP relative offset
195 // is 32-bit.
196 if (AM.GV)
197 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
198 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000199 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
200 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000202 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else if (AM.JT != -1)
204 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
205 else
206 Disp = getI32Imm(AM.Disp);
207 }
208
209 /// getI8Imm - Return a target constant with the specified value, of type
210 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 return CurDAG->getTargetConstant(Imm, MVT::i8);
213 }
214
215 /// getI16Imm - Return a target constant with the specified value, of type
216 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000217 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i16);
219 }
220
221 /// getI32Imm - Return a target constant with the specified value, of type
222 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000223 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return CurDAG->getTargetConstant(Imm, MVT::i32);
225 }
226
Dan Gohmanb60482f2008-09-23 18:22:58 +0000227 /// getGlobalBaseReg - Return an SDNode that returns the value of
228 /// the global base register. Output instructions required to
229 /// initialize the global base register, if necessary.
230 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 SDNode *getGlobalBaseReg();
232
Dan Gohmandd612bb2008-08-20 21:27:32 +0000233 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
234 /// truncate of the specified operand to i8. This can be done with tablegen,
235 /// except that this code uses MVT::Flag in a tricky way that happens to
236 /// improve scheduling in some cases.
237 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239#ifndef NDEBUG
240 unsigned Indent;
241#endif
242 };
243}
244
Gabor Greife9f7f582008-08-31 15:37:04 +0000245/// findFlagUse - Return use of MVT::Flag value produced by the specified
246/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000247///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248static SDNode *findFlagUse(SDNode *N) {
249 unsigned FlagResNo = N->getNumValues()-1;
250 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000251 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000253 SDValue Op = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +0000254 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 return User;
256 }
257 }
258 return NULL;
259}
260
Evan Cheng656269e2008-04-25 08:22:20 +0000261/// findNonImmUse - Return true by reference in "found" if "Use" is an
262/// non-immediate use of "Def". This function recursively traversing
263/// up the operand chain ignoring certain nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman602d44a2008-09-17 01:39:10 +0000265 SDNode *Root, bool &found,
Evan Cheng656269e2008-04-25 08:22:20 +0000266 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 if (found ||
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000268 Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000269 !Visited.insert(Use))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 return;
Evan Cheng656269e2008-04-25 08:22:20 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000273 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000275 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000276 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000277 assert(N != Root);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 found = true;
279 break;
280 }
Evan Cheng656269e2008-04-25 08:22:20 +0000281
282 // Traverse up the operand chain.
Dan Gohman602d44a2008-09-17 01:39:10 +0000283 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 }
285}
286
287/// isNonImmUse - Start searching from Root up the DAG to check is Def can
288/// be reached. Return true if that's the case. However, ignore direct uses
289/// by ImmedUse (which would be U in the example illustrated in
290/// CanBeFoldedBy) and by Root (which can happen in the store case).
291/// FIXME: to be really generic, we should allow direct use by any node
292/// that is being folded. But realisticly since we only fold loads which
293/// have one non-chain use, we only need to watch out for load/op/store
294/// and load/op/cmp case where the root (store / cmp) may reach the load via
295/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000296static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000297 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 bool found = false;
Dan Gohman602d44a2008-09-17 01:39:10 +0000299 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 return found;
301}
302
303
Dan Gohmand6098272007-07-24 23:00:27 +0000304bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000305 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
Dan Gohman602d44a2008-09-17 01:39:10 +0000307 // If Root use can somehow reach N through a path that that doesn't contain
308 // U then folding N would create a cycle. e.g. In the following
309 // diagram, Root can reach N through X. If N is folded into into Root, then
310 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000312 // [N*] //
313 // ^ ^ //
314 // / \ //
315 // [U*] [X]? //
316 // ^ ^ //
317 // \ / //
318 // \ / //
319 // [Root*] //
320 //
321 // * indicates nodes to be folded together.
322 //
323 // If Root produces a flag, then it gets (even more) interesting. Since it
324 // will be "glued" together with its flag use in the scheduler, we need to
325 // check if it might reach N.
326 //
327 // [N*] //
328 // ^ ^ //
329 // / \ //
330 // [U*] [X]? //
331 // ^ ^ //
332 // \ \ //
333 // \ | //
334 // [Root*] | //
335 // ^ | //
336 // f | //
337 // | / //
338 // [Y] / //
339 // ^ / //
340 // f / //
341 // | / //
342 // [FU] //
343 //
344 // If FU (flag use) indirectly reaches N (the load), and Root folds N
345 // (call it Fold), then X is a predecessor of FU and a successor of
346 // Fold. But since Fold and FU are flagged together, this will create
347 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348
Duncan Sands92c43912008-06-06 12:08:01 +0000349 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000350 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 SDNode *FU = findFlagUse(Root);
352 if (FU == NULL)
353 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000354 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 VT = Root->getValueType(Root->getNumValues()-1);
356 }
357
Dan Gohman602d44a2008-09-17 01:39:10 +0000358 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359}
360
361/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
362/// and move load below the TokenFactor. Replace store's chain operand with
363/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000364static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000365 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000366 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000367 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
368 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000369 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000371 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000372 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
373 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
374 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
375 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376}
377
Evan Cheng2b2a7012008-05-23 21:23:16 +0000378/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
379///
Dan Gohman8181bd12008-07-27 21:46:04 +0000380static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
381 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000382 if (N.getOpcode() == ISD::BIT_CONVERT)
383 N = N.getOperand(0);
384
385 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
386 if (!LD || LD->isVolatile())
387 return false;
388 if (LD->getAddressingMode() != ISD::UNINDEXED)
389 return false;
390
391 ISD::LoadExtType ExtType = LD->getExtensionType();
392 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
393 return false;
394
395 if (N.hasOneUse() &&
396 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000397 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000398 Load = N;
399 return true;
400 }
401 return false;
402}
403
Evan Cheng98cfaf82008-08-25 21:27:18 +0000404/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
405/// operand and move load below the call's chain operand.
406static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
407 SDValue Call, SDValue Chain) {
408 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000409 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
410 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000411 Ops.push_back(Load.getOperand(0));
412 else
413 Ops.push_back(Chain.getOperand(i));
414 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
415 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
416 Load.getOperand(1), Load.getOperand(2));
417 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000418 Ops.push_back(SDValue(Load.getNode(), 1));
419 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000420 Ops.push_back(Call.getOperand(i));
421 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
422}
423
424/// isCalleeLoad - Return true if call address is a load and it can be
425/// moved below CALLSEQ_START and the chains leading up to the call.
426/// Return the CALLSEQ_START by reference as a second output.
427static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000428 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000429 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000430 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000431 if (!LD ||
432 LD->isVolatile() ||
433 LD->getAddressingMode() != ISD::UNINDEXED ||
434 LD->getExtensionType() != ISD::NON_EXTLOAD)
435 return false;
436
437 // Now let's find the callseq_start.
438 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
439 if (!Chain.hasOneUse())
440 return false;
441 Chain = Chain.getOperand(0);
442 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000443 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Cheng98cfaf82008-08-25 21:27:18 +0000444}
445
446
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000447/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
448/// This is only run if not in -fast mode (aka -O0).
449/// This allows the instruction selector to pick more read-modify-write
450/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451///
452/// [Load chain]
453/// ^
454/// |
455/// [Load]
456/// ^ ^
457/// | |
458/// / \-
459/// / |
460/// [TokenFactor] [Op]
461/// ^ ^
462/// | |
463/// \ /
464/// \ /
465/// [Store]
466///
467/// The fact the store's chain operand != load's chain will prevent the
468/// (store (op (load))) instruction from being selected. We can transform it to:
469///
470/// [Load chain]
471/// ^
472/// |
473/// [TokenFactor]
474/// ^
475/// |
476/// [Load]
477/// ^ ^
478/// | |
479/// | \-
480/// | |
481/// | [Op]
482/// | ^
483/// | |
484/// \ /
485/// \ /
486/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000487void X86DAGToDAGISel::PreprocessForRMW() {
488 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
489 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000490 if (I->getOpcode() == X86ISD::CALL) {
491 /// Also try moving call address load from outside callseq_start to just
492 /// before the call to allow it to be folded.
493 ///
494 /// [Load chain]
495 /// ^
496 /// |
497 /// [Load]
498 /// ^ ^
499 /// | |
500 /// / \--
501 /// / |
502 ///[CALLSEQ_START] |
503 /// ^ |
504 /// | |
505 /// [LOAD/C2Reg] |
506 /// | |
507 /// \ /
508 /// \ /
509 /// [CALL]
510 SDValue Chain = I->getOperand(0);
511 SDValue Load = I->getOperand(1);
512 if (!isCalleeLoad(Load, Chain))
513 continue;
514 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
515 ++NumLoadMoved;
516 continue;
517 }
518
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 if (!ISD::isNON_TRUNCStore(I))
520 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000521 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000522
Gabor Greif1c80d112008-08-28 21:40:38 +0000523 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 continue;
525
Dan Gohman8181bd12008-07-27 21:46:04 +0000526 SDValue N1 = I->getOperand(1);
527 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000528 if ((N1.getValueType().isFloatingPoint() &&
529 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 !N1.hasOneUse())
531 continue;
532
533 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000534 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000535 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000537 case ISD::ADD:
538 case ISD::MUL:
539 case ISD::AND:
540 case ISD::OR:
541 case ISD::XOR:
542 case ISD::ADDC:
543 case ISD::ADDE:
544 case ISD::VECTOR_SHUFFLE: {
545 SDValue N10 = N1.getOperand(0);
546 SDValue N11 = N1.getOperand(1);
547 RModW = isRMWLoad(N10, Chain, N2, Load);
548 if (!RModW)
549 RModW = isRMWLoad(N11, Chain, N2, Load);
550 break;
551 }
552 case ISD::SUB:
553 case ISD::SHL:
554 case ISD::SRA:
555 case ISD::SRL:
556 case ISD::ROTL:
557 case ISD::ROTR:
558 case ISD::SUBC:
559 case ISD::SUBE:
560 case X86ISD::SHLD:
561 case X86ISD::SHRD: {
562 SDValue N10 = N1.getOperand(0);
563 RModW = isRMWLoad(N10, Chain, N2, Load);
564 break;
565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 }
567
568 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000569 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 ++NumLoadMoved;
571 }
572 }
573}
574
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000575
576/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
577/// nodes that target the FP stack to be store and load to the stack. This is a
578/// gross hack. We would like to simply mark these as being illegal, but when
579/// we do that, legalize produces these when it expands calls, then expands
580/// these in the same legalize pass. We would like dag combine to be able to
581/// hack on these between the call expansion and the node legalization. As such
582/// this pass basically does "really late" legalization of these inline with the
583/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000584void X86DAGToDAGISel::PreprocessForFPConvert() {
585 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
586 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000587 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
588 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
589 continue;
590
591 // If the source and destination are SSE registers, then this is a legal
592 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000593 MVT SrcVT = N->getOperand(0).getValueType();
594 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000595 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
596 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
597 if (SrcIsSSE && DstIsSSE)
598 continue;
599
Chris Lattner5d294e52008-03-09 07:05:32 +0000600 if (!SrcIsSSE && !DstIsSSE) {
601 // If this is an FPStack extension, it is a noop.
602 if (N->getOpcode() == ISD::FP_EXTEND)
603 continue;
604 // If this is a value-preserving FPStack truncation, it is a noop.
605 if (N->getConstantOperandVal(1))
606 continue;
607 }
608
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000609 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
610 // FPStack has extload and truncstore. SSE can fold direct loads into other
611 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000612 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000613 if (N->getOpcode() == ISD::FP_ROUND)
614 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
615 else
616 MemVT = SrcIsSSE ? SrcVT : DstVT;
617
Dan Gohman14a66442008-08-23 02:25:05 +0000618 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000619
620 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman14a66442008-08-23 02:25:05 +0000621 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
622 N->getOperand(0),
623 MemTmp, NULL, 0, MemVT);
624 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
625 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000626
627 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
628 // extload we created. This will cause general havok on the dag because
629 // anything below the conversion could be folded into other existing nodes.
630 // To avoid invalidating 'I', back it up to the convert node.
631 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000632 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000633
634 // Now that we did that, the node is dead. Increment the iterator to the
635 // next node to process, then delete N.
636 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000637 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000638 }
639}
640
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
642/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000643void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000644 CurBB = BB; // BB can change as result of isel.
Evan Cheng13559d62008-09-26 23:41:32 +0000645 if (!OptForSize) {
646 const Function *F = CurDAG->getMachineFunction().getFunction();
Daniel Dunbar1b1e7ef2008-09-27 00:22:09 +0000647 OptForSize = !F->isDeclaration() &&
648 F->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng13559d62008-09-26 23:41:32 +0000649 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650
Evan Cheng34fd4f32008-06-30 20:45:06 +0000651 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000652 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000653 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000654
655 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000656 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657
658 // Codegen the basic block.
659#ifndef NDEBUG
660 DOUT << "===== Instruction selection begins:\n";
661 Indent = 0;
662#endif
Dan Gohmanbd3f8822008-08-21 16:36:34 +0000663 SelectRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664#ifndef NDEBUG
665 DOUT << "===== Instruction selection ends:\n";
666#endif
667
Dan Gohman14a66442008-08-23 02:25:05 +0000668 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000669}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670
Dan Gohmanb552df72008-07-21 20:00:07 +0000671void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 // If we are emitting FP stack code, scan the basic block to determine if this
673 // block defines any FP values. If so, put an FP_REG_KILL instruction before
674 // the terminator of the block.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000675
Dale Johannesen684887e2007-09-24 22:52:39 +0000676 // Note that FP stack instructions are used in all modes for long double,
677 // so we always need to do this check.
678 // Also note that it's possible for an FP stack register to be live across
679 // an instruction that produces multiple basic blocks (SSE CMOV) so we
680 // must check all the generated basic blocks.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000681
682 // Scan all of the machine instructions in these MBBs, checking for FP
683 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Cheng34fd4f32008-06-30 20:45:06 +0000684 MachineFunction::iterator MBBI = CurBB;
Chris Lattner04d64b22008-03-10 23:34:12 +0000685 MachineFunction::iterator EndMBB = BB; ++EndMBB;
686 for (; MBBI != EndMBB; ++MBBI) {
687 MachineBasicBlock *MBB = MBBI;
688
689 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
690 // before the return.
691 if (!MBB->empty()) {
692 MachineBasicBlock::iterator EndI = MBB->end();
693 --EndI;
694 if (EndI->getDesc().isReturn())
695 continue;
696 }
697
Dale Johannesen684887e2007-09-24 22:52:39 +0000698 bool ContainsFPCode = false;
Chris Lattner04d64b22008-03-10 23:34:12 +0000699 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000700 !ContainsFPCode && I != E; ++I) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000701 if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000702 const TargetRegisterClass *clas;
703 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000704 if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
Chris Lattner04d64b22008-03-10 23:34:12 +0000705 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner1b989192007-12-31 04:13:23 +0000706 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000707 X86::RFP32RegisterClass ||
708 clas == X86::RFP64RegisterClass ||
709 clas == X86::RFP80RegisterClass)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 ContainsFPCode = true;
711 break;
712 }
713 }
714 }
715 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000716 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
717 // a copy of the input value in this block. In SSE mode, we only care about
718 // 80-bit values.
719 if (!ContainsFPCode) {
720 // Final check, check LLVM BB's that are successors to the LLVM BB
721 // corresponding to BB for FP PHI nodes.
722 const BasicBlock *LLVMBB = BB->getBasicBlock();
723 const PHINode *PN;
724 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
725 !ContainsFPCode && SI != E; ++SI) {
726 for (BasicBlock::const_iterator II = SI->begin();
727 (PN = dyn_cast<PHINode>(II)); ++II) {
728 if (PN->getType()==Type::X86_FP80Ty ||
729 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
730 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
731 ContainsFPCode = true;
732 break;
733 }
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000734 }
735 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000737 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
738 if (ContainsFPCode) {
Chris Lattner04d64b22008-03-10 23:34:12 +0000739 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen684887e2007-09-24 22:52:39 +0000740 TM.getInstrInfo()->get(X86::FP_REG_KILL));
741 ++NumFPKill;
742 }
Chris Lattner04d64b22008-03-10 23:34:12 +0000743 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744}
745
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000746/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
747/// the main function.
748void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
749 MachineFrameInfo *MFI) {
750 const TargetInstrInfo *TII = TM.getInstrInfo();
751 if (Subtarget->isTargetCygMing())
752 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
753}
754
755void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
756 // If this is main, emit special code for main.
757 MachineBasicBlock *BB = MF.begin();
758 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
759 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
760}
761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762/// MatchAddress - Add the specified node to the specified addressing mode,
763/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000764/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000765bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 bool isRoot, unsigned Depth) {
Evan Cheng7f250d62008-09-24 00:05:32 +0000767 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000768 // Limit recursion.
769 if (Depth > 5)
770 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771
772 // RIP relative addressing: %rip + 32-bit displacement!
773 if (AM.isRIPRel) {
774 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman40686732008-09-26 21:54:37 +0000775 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 if (isInt32(AM.Disp + Val)) {
777 AM.Disp += Val;
778 return false;
779 }
780 }
781 return true;
782 }
783
Gabor Greif1c80d112008-08-28 21:40:38 +0000784 int id = N.getNode()->getNodeId();
Evan Chengf2abee72007-12-13 00:43:27 +0000785 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786
787 switch (N.getOpcode()) {
788 default: break;
789 case ISD::Constant: {
Dan Gohman40686732008-09-26 21:54:37 +0000790 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 if (isInt32(AM.Disp + Val)) {
792 AM.Disp += Val;
793 return false;
794 }
795 break;
796 }
797
798 case X86ISD::Wrapper: {
Dale Johannesenc501c082008-08-11 23:46:25 +0000799DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
800DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
801DOUT << "AlreadySelected " << AlreadySelected << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 bool is64Bit = Subtarget->is64Bit();
803 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000804 // Also, base and index reg must be 0 in order to use rip as base.
805 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000806 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 break;
808 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
809 break;
810 // If value is available in a register both base and index components have
811 // been picked, we can't fit the result available in the register in the
812 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greif1c80d112008-08-28 21:40:38 +0000813 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000814 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
816 GlobalValue *GV = G->getGlobal();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000817 AM.GV = GV;
818 AM.Disp += G->getOffset();
Dan Gohmanc6413362008-09-26 19:15:30 +0000819 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000820 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000822 AM.CP = CP->getConstVal();
823 AM.Align = CP->getAlignment();
824 AM.Disp += CP->getOffset();
Dan Gohmanc6413362008-09-26 19:15:30 +0000825 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000826 return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000827 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000828 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000829 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000830 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000832 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000833 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000834 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 }
836 }
837 break;
838 }
839
840 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000841 if (AM.BaseType == X86ISelAddressMode::RegBase
842 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
844 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
845 return false;
846 }
847 break;
848
849 case ISD::SHL:
Gabor Greife9f7f582008-08-31 15:37:04 +0000850 if (AlreadySelected || AM.IndexReg.getNode() != 0
851 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000852 break;
853
Gabor Greife9f7f582008-08-31 15:37:04 +0000854 if (ConstantSDNode
855 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000856 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000857 if (Val == 1 || Val == 2 || Val == 3) {
858 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000859 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860
Chris Lattner7f06edd2007-12-08 07:22:58 +0000861 // Okay, we know that we have a scale by now. However, if the scaled
862 // value is an add of something and a constant, we can fold the
863 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000864 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
865 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
866 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000867 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000868 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000869 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000870 if (isInt32(Disp))
871 AM.Disp = Disp;
872 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000874 } else {
875 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000877 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 }
879 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000880 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881
Dan Gohman35b99222007-10-22 20:22:24 +0000882 case ISD::SMUL_LOHI:
883 case ISD::UMUL_LOHI:
884 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000885 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000886 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 case ISD::MUL:
888 // X*[3,5,9] -> X+X*[2,4,8]
Evan Chengf2abee72007-12-13 00:43:27 +0000889 if (!AlreadySelected &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000891 AM.Base.Reg.getNode() == 0 &&
892 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000893 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000894 if (ConstantSDNode
895 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000896 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
897 CN->getZExtValue() == 9) {
898 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899
Gabor Greif1c80d112008-08-28 21:40:38 +0000900 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000901 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902
903 // Okay, we know that we have a scale by now. However, if the scaled
904 // value is an add of something and a constant, we can fold the
905 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000906 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
907 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
908 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000910 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000911 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
912 CN->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 if (isInt32(Disp))
914 AM.Disp = Disp;
915 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000916 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000918 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920
921 AM.IndexReg = AM.Base.Reg = Reg;
922 return false;
923 }
924 }
925 break;
926
927 case ISD::ADD:
Evan Chengf2abee72007-12-13 00:43:27 +0000928 if (!AlreadySelected) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 X86ISelAddressMode Backup = AM;
Gabor Greif1c80d112008-08-28 21:40:38 +0000930 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
931 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 return false;
933 AM = Backup;
Gabor Greif1c80d112008-08-28 21:40:38 +0000934 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
935 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 return false;
937 AM = Backup;
938 }
939 break;
940
941 case ISD::OR:
942 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Chengf2abee72007-12-13 00:43:27 +0000943 if (AlreadySelected) break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000944
945 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
946 X86ISelAddressMode Backup = AM;
947 // Start with the LHS as an addr mode.
948 if (!MatchAddress(N.getOperand(0), AM, false) &&
949 // Address could not have picked a GV address for the displacement.
950 AM.GV == NULL &&
951 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman40686732008-09-26 21:54:37 +0000952 isInt32(AM.Disp + CN->getSExtValue()) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000953 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000954 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000955 AM.Disp += CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000956 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000958 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 }
960 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000961
962 case ISD::AND: {
963 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
964 // allows us to fold the shift into this addressing mode.
965 if (AlreadySelected) break;
Dan Gohman8181bd12008-07-27 21:46:04 +0000966 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000967 if (Shift.getOpcode() != ISD::SHL) break;
968
969 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000970 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000971
972 // Not when RIP is used as the base.
973 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000974
975 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
976 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
977 if (!C1 || !C2) break;
978
979 // Not likely to be profitable if either the AND or SHIFT node has more
980 // than one use (unless all uses are for address computation). Besides,
981 // isel mechanism requires their node ids to be reused.
982 if (!N.hasOneUse() || !Shift.hasOneUse())
983 break;
984
985 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000986 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000987 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
988 break;
989
990 // Get the new AND mask, this folds to a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +0000991 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
992 SDValue(C2, 0), SDValue(C1, 0));
993 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Chengf2abee72007-12-13 00:43:27 +0000994 Shift.getOperand(0), NewANDMask);
Gabor Greif1c80d112008-08-28 21:40:38 +0000995 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
996 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
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() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001066 CanBeFoldedBy(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.
1129 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1130 // 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() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001153 CanBeFoldedBy(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
1183
1184 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1185 unsigned Opc;
1186 MVT N0VT = N0.getValueType();
1187 switch (N0VT.getSimpleVT()) {
1188 default: assert(0 && "Unknown truncate!");
1189 case MVT::i16:
1190 Opc = X86::MOV16to16_;
1191 break;
1192 case MVT::i32:
1193 Opc = X86::MOV32to32_;
1194 break;
1195 }
1196
1197 // The use of MVT::Flag here is not strictly accurate, but it helps
1198 // scheduling in some cases.
1199 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1200 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1201 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001202}
1203
Dale Johannesenf160d802008-10-02 18:53:47 +00001204SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1205 SDValue Chain = Node->getOperand(0);
1206 SDValue In1 = Node->getOperand(1);
1207 SDValue In2L = Node->getOperand(2);
1208 SDValue In2H = Node->getOperand(3);
1209 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1210 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1211 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001212 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001213 AddToISelQueue(Tmp0);
1214 AddToISelQueue(Tmp1);
1215 AddToISelQueue(Tmp2);
1216 AddToISelQueue(Tmp3);
1217 AddToISelQueue(In2L);
1218 AddToISelQueue(In2H);
Dale Johannesen44eb5372008-10-03 19:41:08 +00001219 // For now, don't select the MemOperand object, we don't know how.
Dale Johannesenf160d802008-10-02 18:53:47 +00001220 AddToISelQueue(Chain);
Dale Johannesenf160d802008-10-02 18:53:47 +00001221 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1222 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1223}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001224
Dan Gohman8181bd12008-07-27 21:46:04 +00001225SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001226 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001227 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 unsigned Opc, MOpc;
1229 unsigned Opcode = Node->getOpcode();
1230
1231#ifndef NDEBUG
1232 DOUT << std::string(Indent, ' ') << "Selecting: ";
1233 DEBUG(Node->dump(CurDAG));
1234 DOUT << "\n";
1235 Indent += 2;
1236#endif
1237
Dan Gohmanbd68c792008-07-17 19:10:17 +00001238 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239#ifndef NDEBUG
1240 DOUT << std::string(Indent-2, ' ') << "== ";
1241 DEBUG(Node->dump(CurDAG));
1242 DOUT << "\n";
1243 Indent -= 2;
1244#endif
1245 return NULL; // Already selected.
1246 }
1247
1248 switch (Opcode) {
1249 default: break;
1250 case X86ISD::GlobalBaseReg:
1251 return getGlobalBaseReg();
1252
1253 case ISD::ADD: {
1254 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1255 // code and is matched first so to prevent it from being turned into
1256 // LEA32r X+c.
Evan Cheng17e39d62008-01-08 02:06:11 +00001257 // In 64-bit small code size mode, use LEA to take advantage of
1258 // RIP-relative addressing.
1259 if (TM.getCodeModel() != CodeModel::Small)
1260 break;
Duncan Sands92c43912008-06-06 12:08:01 +00001261 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001262 SDValue N0 = N.getOperand(0);
1263 SDValue N1 = N.getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001264 if (N.getNode()->getValueType(0) == PtrVT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 N0.getOpcode() == X86ISD::Wrapper &&
1266 N1.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001267 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00001268 SDValue C(0, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001269 // TODO: handle ExternalSymbolSDNode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 if (GlobalAddressSDNode *G =
1271 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1272 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1273 G->getOffset() + Offset);
1274 } else if (ConstantPoolSDNode *CP =
1275 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1276 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1277 CP->getAlignment(),
1278 CP->getOffset()+Offset);
1279 }
1280
Gabor Greif1c80d112008-08-28 21:40:38 +00001281 if (C.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001283 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 CurDAG->getRegister(0, PtrVT), C };
Gabor Greife9f7f582008-08-31 15:37:04 +00001285 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1286 MVT::i64, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 } else
Gabor Greif1c80d112008-08-28 21:40:38 +00001288 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 }
1290 }
1291
1292 // Other cases are handled by auto-generated code.
1293 break;
1294 }
1295
Dale Johannesenf160d802008-10-02 18:53:47 +00001296 case X86ISD::ATOMOR64_DAG:
1297 return SelectAtomic64(Node, X86::ATOMOR6432);
1298 case X86ISD::ATOMXOR64_DAG:
1299 return SelectAtomic64(Node, X86::ATOMXOR6432);
1300 case X86ISD::ATOMADD64_DAG:
1301 return SelectAtomic64(Node, X86::ATOMADD6432);
1302 case X86ISD::ATOMSUB64_DAG:
1303 return SelectAtomic64(Node, X86::ATOMSUB6432);
1304 case X86ISD::ATOMNAND64_DAG:
1305 return SelectAtomic64(Node, X86::ATOMNAND6432);
1306 case X86ISD::ATOMAND64_DAG:
1307 return SelectAtomic64(Node, X86::ATOMAND6432);
1308
Dan Gohman5a199552007-10-08 18:33:35 +00001309 case ISD::SMUL_LOHI:
1310 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001311 SDValue N0 = Node->getOperand(0);
1312 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001313
Dan Gohman5a199552007-10-08 18:33:35 +00001314 bool isSigned = Opcode == ISD::SMUL_LOHI;
1315 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001316 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 default: assert(0 && "Unsupported VT!");
1318 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1319 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1320 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1321 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1322 }
1323 else
Duncan Sands92c43912008-06-06 12:08:01 +00001324 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 default: assert(0 && "Unsupported VT!");
1326 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1327 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1328 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1329 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1330 }
1331
1332 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001333 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 default: assert(0 && "Unsupported VT!");
1335 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1336 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1337 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1338 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1339 }
1340
Dan Gohman8181bd12008-07-27 21:46:04 +00001341 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001342 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001343 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 if (!foldedLoad) {
1345 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001346 if (foldedLoad)
1347 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 }
1349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 AddToISelQueue(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001351 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1352 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353
1354 if (foldedLoad) {
Dan Gohman5a199552007-10-08 18:33:35 +00001355 AddToISelQueue(N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 AddToISelQueue(Tmp0);
1357 AddToISelQueue(Tmp1);
1358 AddToISelQueue(Tmp2);
1359 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001360 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 SDNode *CNode =
1362 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001363 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001364 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001365 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 } else {
1367 AddToISelQueue(N1);
1368 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001369 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 }
1371
Dan Gohman5a199552007-10-08 18:33:35 +00001372 // Copy the low half of the result, if it is needed.
1373 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001374 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001375 LoReg, NVT, InFlag);
1376 InFlag = Result.getValue(2);
1377 ReplaceUses(N.getValue(0), Result);
1378#ifndef NDEBUG
1379 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001380 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001381 DOUT << "\n";
1382#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001383 }
Dan Gohman5a199552007-10-08 18:33:35 +00001384 // Copy the high half of the result, if it is needed.
1385 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001386 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001387 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1388 // Prevent use of AH in a REX instruction by referencing AX instead.
1389 // Shift it down 8 bits.
1390 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1391 X86::AX, MVT::i16, InFlag);
1392 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001393 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001394 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001395 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001396 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1397 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001398 MVT::i8, Result, SRIdx), 0);
1399 } else {
1400 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1401 HiReg, NVT, InFlag);
1402 InFlag = Result.getValue(2);
1403 }
1404 ReplaceUses(N.getValue(1), Result);
1405#ifndef NDEBUG
1406 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001407 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001408 DOUT << "\n";
1409#endif
1410 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411
1412#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 Indent -= 2;
1414#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 return NULL;
1417 }
1418
Dan Gohman5a199552007-10-08 18:33:35 +00001419 case ISD::SDIVREM:
1420 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001421 SDValue N0 = Node->getOperand(0);
1422 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001423
1424 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001426 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 default: assert(0 && "Unsupported VT!");
1428 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1429 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1430 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1431 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1432 }
1433 else
Duncan Sands92c43912008-06-06 12:08:01 +00001434 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 default: assert(0 && "Unsupported VT!");
1436 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1437 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1438 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1439 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1440 }
1441
1442 unsigned LoReg, HiReg;
1443 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001444 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 default: assert(0 && "Unsupported VT!");
1446 case MVT::i8:
1447 LoReg = X86::AL; HiReg = X86::AH;
1448 ClrOpcode = 0;
1449 SExtOpcode = X86::CBW;
1450 break;
1451 case MVT::i16:
1452 LoReg = X86::AX; HiReg = X86::DX;
1453 ClrOpcode = X86::MOV16r0;
1454 SExtOpcode = X86::CWD;
1455 break;
1456 case MVT::i32:
1457 LoReg = X86::EAX; HiReg = X86::EDX;
1458 ClrOpcode = X86::MOV32r0;
1459 SExtOpcode = X86::CDQ;
1460 break;
1461 case MVT::i64:
1462 LoReg = X86::RAX; HiReg = X86::RDX;
1463 ClrOpcode = X86::MOV64r0;
1464 SExtOpcode = X86::CQO;
1465 break;
1466 }
1467
Dan Gohman8181bd12008-07-27 21:46:04 +00001468 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001469 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1470
Dan Gohman8181bd12008-07-27 21:46:04 +00001471 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 if (NVT == MVT::i8 && !isSigned) {
1473 // Special case for div8, just use a move with zero extension to AX to
1474 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001475 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 AddToISelQueue(N0.getOperand(0));
1479 AddToISelQueue(Tmp0);
1480 AddToISelQueue(Tmp1);
1481 AddToISelQueue(Tmp2);
1482 AddToISelQueue(Tmp3);
1483 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001484 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 Ops, 5), 0);
1486 Chain = Move.getValue(1);
1487 ReplaceUses(N0.getValue(1), Chain);
1488 } else {
1489 AddToISelQueue(N0);
1490 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001491 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 Chain = CurDAG->getEntryNode();
1493 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001494 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 InFlag = Chain.getValue(1);
1496 } else {
1497 AddToISelQueue(N0);
1498 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001499 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001500 LoReg, N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 if (isSigned) {
1502 // Sign extend the low part into the high part.
1503 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001504 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 } else {
1506 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001507 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001508 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1509 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 }
1511 }
1512
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 if (foldedLoad) {
1514 AddToISelQueue(N1.getOperand(0));
1515 AddToISelQueue(Tmp0);
1516 AddToISelQueue(Tmp1);
1517 AddToISelQueue(Tmp2);
1518 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001519 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 SDNode *CNode =
1521 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001522 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001523 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001524 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 } else {
1526 AddToISelQueue(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001528 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 }
1530
Dan Gohman242a5ba2007-09-25 18:23:27 +00001531 // Copy the division (low) result, if it is needed.
1532 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001533 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001534 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001535 InFlag = Result.getValue(2);
1536 ReplaceUses(N.getValue(0), Result);
1537#ifndef NDEBUG
1538 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001539 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001540 DOUT << "\n";
1541#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001542 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001543 // Copy the remainder (high) result, if it is needed.
1544 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001545 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001546 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1547 // Prevent use of AH in a REX instruction by referencing AX instead.
1548 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001549 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1550 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001551 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001552 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001553 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001554 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001555 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1556 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001557 MVT::i8, Result, SRIdx), 0);
1558 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001559 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1560 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001561 InFlag = Result.getValue(2);
1562 }
1563 ReplaceUses(N.getValue(1), Result);
1564#ifndef NDEBUG
1565 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001566 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001567 DOUT << "\n";
1568#endif
1569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570
1571#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 Indent -= 2;
1573#endif
1574
1575 return NULL;
1576 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001577
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001578 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001579 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001580 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1581 SDValue N0 = Node->getOperand(0);
1582 AddToISelQueue(N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001583
Dan Gohmandd612bb2008-08-20 21:27:32 +00001584 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1585 unsigned Opc = 0;
1586 switch (NVT.getSimpleVT()) {
1587 default: assert(0 && "Unknown sign_extend_inreg!");
1588 case MVT::i16:
1589 Opc = X86::MOVSX16rr8;
1590 break;
1591 case MVT::i32:
1592 Opc = X86::MOVSX32rr8;
1593 break;
1594 }
1595
1596 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001597
1598#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001599 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001600 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001601 DOUT << "\n";
1602 DOUT << std::string(Indent-2, ' ') << "=> ";
1603 DEBUG(ResNode->dump(CurDAG));
1604 DOUT << "\n";
1605 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001606#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001607 return ResNode;
1608 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001609 break;
1610 }
1611
1612 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001613 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1614 SDValue Input = Node->getOperand(0);
1615 AddToISelQueue(Node->getOperand(0));
1616 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001617
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001618#ifndef NDEBUG
1619 DOUT << std::string(Indent-2, ' ') << "=> ";
1620 DEBUG(ResNode->dump(CurDAG));
1621 DOUT << "\n";
1622 Indent -= 2;
1623#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001624 return ResNode;
1625 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001626 break;
1627 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001628
1629 case ISD::DECLARE: {
1630 // Handle DECLARE nodes here because the second operand may have been
1631 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 SDValue Chain = Node->getOperand(0);
1633 SDValue N1 = Node->getOperand(1);
1634 SDValue N2 = Node->getOperand(2);
Evan Cheng651e1442008-06-18 02:48:27 +00001635 if (!isa<FrameIndexSDNode>(N1))
1636 break;
1637 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1638 if (N2.getOpcode() == ISD::ADD &&
1639 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1640 N2 = N2.getOperand(1);
1641 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Chengd4cebcd2008-06-17 02:01:22 +00001642 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Chengd4cebcd2008-06-17 02:01:22 +00001643 GlobalValue *GV =
1644 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00001645 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1646 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Chengd4cebcd2008-06-17 02:01:22 +00001647 AddToISelQueue(Chain);
Dan Gohman8181bd12008-07-27 21:46:04 +00001648 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Chengd4cebcd2008-06-17 02:01:22 +00001649 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1650 MVT::Other, Ops, 3);
1651 }
1652 break;
1653 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654 }
1655
1656 SDNode *ResNode = SelectCode(N);
1657
1658#ifndef NDEBUG
1659 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001660 if (ResNode == NULL || ResNode == N.getNode())
1661 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 else
1663 DEBUG(ResNode->dump(CurDAG));
1664 DOUT << "\n";
1665 Indent -= 2;
1666#endif
1667
1668 return ResNode;
1669}
1670
1671bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001672SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001673 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001674 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 switch (ConstraintCode) {
1676 case 'o': // offsetable ??
1677 case 'v': // not offsetable ??
1678 default: return true;
1679 case 'm': // memory
1680 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1681 return true;
1682 break;
1683 }
1684
1685 OutOps.push_back(Op0);
1686 OutOps.push_back(Op1);
1687 OutOps.push_back(Op2);
1688 OutOps.push_back(Op3);
1689 AddToISelQueue(Op0);
1690 AddToISelQueue(Op1);
1691 AddToISelQueue(Op2);
1692 AddToISelQueue(Op3);
1693 return false;
1694}
1695
1696/// createX86ISelDag - This pass converts a legalized DAG into a
1697/// X86-specific DAG, ready for instruction scheduling.
1698///
1699FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1700 return new X86DAGToDAGISel(TM, Fast);
1701}