blob: 7834f6e1fdaf1b22e681e73bd828a24b85d10bbd [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"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000038#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000039#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/Statistic.h"
41#include <queue>
42#include <set>
43using namespace llvm;
44
45STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
46STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048//===----------------------------------------------------------------------===//
49// Pattern Matcher Implementation
50//===----------------------------------------------------------------------===//
51
52namespace {
53 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000054 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 /// tree.
56 struct X86ISelAddressMode {
57 enum {
58 RegBase,
59 FrameIndexBase
60 } BaseType;
61
62 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000063 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 int FrameIndex;
65 } Base;
66
Evan Cheng3b5a1272008-02-07 08:53:49 +000067 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 unsigned Disp;
71 GlobalValue *GV;
72 Constant *CP;
73 const char *ES;
74 int JT;
75 unsigned Align; // CP alignment.
76
77 X86ISelAddressMode()
78 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
79 GV(0), CP(0), ES(0), JT(-1), Align(0) {
80 }
Dale Johannesenc501c082008-08-11 23:46:25 +000081 void dump() {
82 cerr << "X86ISelAddressMode " << this << "\n";
83 cerr << "Base.Reg "; if (Base.Reg.Val!=0) Base.Reg.Val->dump();
84 else cerr << "nul";
85 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
86 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
87 cerr << "IndexReg "; if (IndexReg.Val!=0) IndexReg.Val->dump();
88 else cerr << "nul";
89 cerr << " Disp " << Disp << "\n";
90 cerr << "GV "; if (GV) GV->dump();
91 else cerr << "nul";
92 cerr << " CP "; if (CP) CP->dump();
93 else cerr << "nul";
94 cerr << "\n";
95 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
96 cerr << " JT" << JT << " Align" << Align << "\n";
97 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 };
99}
100
101namespace {
102 //===--------------------------------------------------------------------===//
103 /// ISel - X86 specific code to select X86 machine instructions for
104 /// SelectionDAG operations.
105 ///
106 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
107 /// ContainsFPCode - Every instruction we select that uses or defines a FP
108 /// register should set this to true.
109 bool ContainsFPCode;
110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 /// TM - Keep a reference to X86TargetMachine.
112 ///
113 X86TargetMachine &TM;
114
115 /// X86Lowering - This object fully describes how to lower LLVM code to an
116 /// X86-specific SelectionDAG.
117 X86TargetLowering X86Lowering;
118
119 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
120 /// make the right decision when generating code for different targets.
121 const X86Subtarget *Subtarget;
122
123 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
124 /// base register.
125 unsigned GlobalBaseReg;
126
Evan Cheng34fd4f32008-06-30 20:45:06 +0000127 /// CurBB - Current BB being isel'd.
128 ///
129 MachineBasicBlock *CurBB;
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 public:
132 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng9b77cae2008-07-01 18:05:03 +0000133 : SelectionDAGISel(X86Lowering, fast),
134 ContainsFPCode(false), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 X86Lowering(*TM.getTargetLowering()),
136 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
137
138 virtual bool runOnFunction(Function &Fn) {
139 // Make sure we re-emit a set of the global base reg if necessary
140 GlobalBaseReg = 0;
141 return SelectionDAGISel::runOnFunction(Fn);
142 }
143
144 virtual const char *getPassName() const {
145 return "X86 DAG->DAG Instruction Selection";
146 }
147
Evan Cheng34fd4f32008-06-30 20:45:06 +0000148 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Evan Cheng34fd4f32008-06-30 20:45:06 +0000150 virtual void InstructionSelect(SelectionDAG &DAG);
151
152 /// InstructionSelectPostProcessing - Post processing of selected and
153 /// scheduled basic blocks.
Dan Gohmanb552df72008-07-21 20:00:07 +0000154 virtual void InstructionSelectPostProcessing();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000156 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
157
Dan Gohmand6098272007-07-24 23:00:27 +0000158 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
160// Include the pieces autogenerated from the target description.
161#include "X86GenDAGISel.inc"
162
163 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 SDNode *Select(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Dan Gohman8181bd12008-07-27 21:46:04 +0000166 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000169 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000170 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
171 SDValue &Scale, SDValue &Index, SDValue &Disp);
172 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
175 SDValue N, SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp,
177 SDValue &InChain, SDValue &OutChain);
178 bool TryFoldLoad(SDValue P, SDValue N,
179 SDValue &Base, SDValue &Scale,
180 SDValue &Index, SDValue &Disp);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000181 void PreprocessForRMW(SelectionDAG &DAG);
182 void PreprocessForFPConvert(SelectionDAG &DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
184 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
185 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000186 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 char ConstraintCode,
Dan Gohman8181bd12008-07-27 21:46:04 +0000188 std::vector<SDValue> &OutOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 SelectionDAG &DAG);
190
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000191 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
192
Dan Gohman8181bd12008-07-27 21:46:04 +0000193 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
194 SDValue &Scale, SDValue &Index,
195 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
197 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
198 AM.Base.Reg;
199 Scale = getI8Imm(AM.Scale);
200 Index = AM.IndexReg;
201 // These are 32-bit even in 64-bit mode since RIP relative offset
202 // is 32-bit.
203 if (AM.GV)
204 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
205 else if (AM.CP)
206 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
207 else if (AM.ES)
208 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
209 else if (AM.JT != -1)
210 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
211 else
212 Disp = getI32Imm(AM.Disp);
213 }
214
215 /// getI8Imm - Return a target constant with the specified value, of type
216 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000217 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i8);
219 }
220
221 /// getI16Imm - Return a target constant with the specified value, of type
222 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000223 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return CurDAG->getTargetConstant(Imm, MVT::i16);
225 }
226
227 /// getI32Imm - Return a target constant with the specified value, of type
228 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000229 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return CurDAG->getTargetConstant(Imm, MVT::i32);
231 }
232
233 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
234 /// base register. Return the virtual register that holds this value.
235 SDNode *getGlobalBaseReg();
236
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000237 /// getTruncate - return an SDNode that implements a subreg based truncate
238 /// of the specified operand to the the specified value type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000239 SDNode *getTruncate(SDValue N0, MVT VT);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241#ifndef NDEBUG
242 unsigned Indent;
243#endif
244 };
245}
246
Evan Cheng656269e2008-04-25 08:22:20 +0000247/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
248///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249static SDNode *findFlagUse(SDNode *N) {
250 unsigned FlagResNo = N->getNumValues()-1;
251 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000252 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000254 SDValue Op = User->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Op.Val == N && Op.ResNo == FlagResNo)
256 return User;
257 }
258 }
259 return NULL;
260}
261
Evan Cheng656269e2008-04-25 08:22:20 +0000262/// findNonImmUse - Return true by reference in "found" if "Use" is an
263/// non-immediate use of "Def". This function recursively traversing
264/// up the operand chain ignoring certain nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
266 SDNode *Root, SDNode *Skip, bool &found,
Evan Cheng656269e2008-04-25 08:22:20 +0000267 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (found ||
269 Use->getNodeId() > Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000270 !Visited.insert(Use))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 return;
Evan Cheng656269e2008-04-25 08:22:20 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
274 SDNode *N = Use->getOperand(i).Val;
275 if (N == Skip)
276 continue;
277 if (N == Def) {
278 if (Use == ImmedUse)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000279 continue; // We are not looking for immediate use.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 if (Use == Root) {
Evan Cheng9ea310c2008-04-25 08:55:28 +0000281 // Must be a chain reading node where it is possible to reach its own
282 // chain operand through a path started from another operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattnercfbb2722008-04-25 05:13:01 +0000284 Use->getOpcode() == X86ISD::CMP ||
Chris Lattnercfbb2722008-04-25 05:13:01 +0000285 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
286 Use->getOpcode() == ISD::INTRINSIC_VOID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 continue;
288 }
289 found = true;
290 break;
291 }
Evan Cheng656269e2008-04-25 08:22:20 +0000292
293 // Traverse up the operand chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
295 }
296}
297
298/// isNonImmUse - Start searching from Root up the DAG to check is Def can
299/// be reached. Return true if that's the case. However, ignore direct uses
300/// by ImmedUse (which would be U in the example illustrated in
301/// CanBeFoldedBy) and by Root (which can happen in the store case).
302/// FIXME: to be really generic, we should allow direct use by any node
303/// that is being folded. But realisticly since we only fold loads which
304/// have one non-chain use, we only need to watch out for load/op/store
305/// and load/op/cmp case where the root (store / cmp) may reach the load via
306/// its chain operand.
307static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
308 SDNode *Skip = NULL) {
Evan Cheng656269e2008-04-25 08:22:20 +0000309 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 bool found = false;
311 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
312 return found;
313}
314
315
Dan Gohmand6098272007-07-24 23:00:27 +0000316bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 if (FastISel) return false;
318
319 // If U use can somehow reach N through another path then U can't fold N or
320 // it will create a cycle. e.g. In the following diagram, U can reach N
321 // through X. If N is folded into into U, then X is both a predecessor and
322 // a successor of U.
323 //
324 // [ N ]
325 // ^ ^
326 // | |
327 // / \---
328 // / [X]
329 // | ^
330 // [U]--------|
331
332 if (isNonImmUse(Root, N, U))
333 return false;
334
335 // If U produces a flag, then it gets (even more) interesting. Since it
336 // would have been "glued" together with its flag use, we need to check if
337 // it might reach N:
338 //
339 // [ N ]
340 // ^ ^
341 // | |
342 // [U] \--
343 // ^ [TF]
344 // | ^
345 // | |
346 // \ /
347 // [FU]
348 //
349 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
350 // NU), then TF is a predecessor of FU and a successor of NU. But since
351 // NU and FU are flagged together, this effectively creates a cycle.
352 bool HasFlagUse = false;
Duncan Sands92c43912008-06-06 12:08:01 +0000353 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 while ((VT == MVT::Flag && !Root->use_empty())) {
355 SDNode *FU = findFlagUse(Root);
356 if (FU == NULL)
357 break;
358 else {
359 Root = FU;
360 HasFlagUse = true;
361 }
362 VT = Root->getValueType(Root->getNumValues()-1);
363 }
364
365 if (HasFlagUse)
366 return !isNonImmUse(Root, N, Root, U);
367 return true;
368}
369
370/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
371/// and move load below the TokenFactor. Replace store's chain operand with
372/// load's chain result.
Dan Gohman8181bd12008-07-27 21:46:04 +0000373static void MoveBelowTokenFactor(SelectionDAG &DAG, SDValue Load,
374 SDValue Store, SDValue TF) {
375 std::vector<SDValue> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
377 if (Load.Val == TF.Val->getOperand(i).Val)
378 Ops.push_back(Load.Val->getOperand(0));
379 else
380 Ops.push_back(TF.Val->getOperand(i));
381 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
382 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
383 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
384 Store.getOperand(2), Store.getOperand(3));
385}
386
Evan Cheng2b2a7012008-05-23 21:23:16 +0000387/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
388///
Dan Gohman8181bd12008-07-27 21:46:04 +0000389static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
390 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000391 if (N.getOpcode() == ISD::BIT_CONVERT)
392 N = N.getOperand(0);
393
394 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
395 if (!LD || LD->isVolatile())
396 return false;
397 if (LD->getAddressingMode() != ISD::UNINDEXED)
398 return false;
399
400 ISD::LoadExtType ExtType = LD->getExtensionType();
401 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
402 return false;
403
404 if (N.hasOneUse() &&
405 N.getOperand(1) == Address &&
406 N.Val->isOperandOf(Chain.Val)) {
407 Load = N;
408 return true;
409 }
410 return false;
411}
412
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000413/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
414/// This is only run if not in -fast mode (aka -O0).
415/// This allows the instruction selector to pick more read-modify-write
416/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417///
418/// [Load chain]
419/// ^
420/// |
421/// [Load]
422/// ^ ^
423/// | |
424/// / \-
425/// / |
426/// [TokenFactor] [Op]
427/// ^ ^
428/// | |
429/// \ /
430/// \ /
431/// [Store]
432///
433/// The fact the store's chain operand != load's chain will prevent the
434/// (store (op (load))) instruction from being selected. We can transform it to:
435///
436/// [Load chain]
437/// ^
438/// |
439/// [TokenFactor]
440/// ^
441/// |
442/// [Load]
443/// ^ ^
444/// | |
445/// | \-
446/// | |
447/// | [Op]
448/// | ^
449/// | |
450/// \ /
451/// \ /
452/// [Store]
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000453void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
455 E = DAG.allnodes_end(); I != E; ++I) {
456 if (!ISD::isNON_TRUNCStore(I))
457 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000458 SDValue Chain = I->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 if (Chain.Val->getOpcode() != ISD::TokenFactor)
460 continue;
461
Dan Gohman8181bd12008-07-27 21:46:04 +0000462 SDValue N1 = I->getOperand(1);
463 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000464 if ((N1.getValueType().isFloatingPoint() &&
465 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 !N1.hasOneUse())
467 continue;
468
469 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000470 SDValue Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 unsigned Opcode = N1.Val->getOpcode();
472 switch (Opcode) {
473 case ISD::ADD:
474 case ISD::MUL:
475 case ISD::AND:
476 case ISD::OR:
477 case ISD::XOR:
478 case ISD::ADDC:
Evan Cheng2b2a7012008-05-23 21:23:16 +0000479 case ISD::ADDE:
480 case ISD::VECTOR_SHUFFLE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000481 SDValue N10 = N1.getOperand(0);
482 SDValue N11 = N1.getOperand(1);
Evan Cheng2b2a7012008-05-23 21:23:16 +0000483 RModW = isRMWLoad(N10, Chain, N2, Load);
484 if (!RModW)
485 RModW = isRMWLoad(N11, Chain, N2, Load);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 break;
487 }
488 case ISD::SUB:
489 case ISD::SHL:
490 case ISD::SRA:
491 case ISD::SRL:
492 case ISD::ROTL:
493 case ISD::ROTR:
494 case ISD::SUBC:
495 case ISD::SUBE:
496 case X86ISD::SHLD:
497 case X86ISD::SHRD: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000498 SDValue N10 = N1.getOperand(0);
Evan Cheng2b2a7012008-05-23 21:23:16 +0000499 RModW = isRMWLoad(N10, Chain, N2, Load);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 break;
501 }
502 }
503
504 if (RModW) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000505 MoveBelowTokenFactor(DAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 ++NumLoadMoved;
507 }
508 }
509}
510
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000511
512/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
513/// nodes that target the FP stack to be store and load to the stack. This is a
514/// gross hack. We would like to simply mark these as being illegal, but when
515/// we do that, legalize produces these when it expands calls, then expands
516/// these in the same legalize pass. We would like dag combine to be able to
517/// hack on these between the call expansion and the node legalization. As such
518/// this pass basically does "really late" legalization of these inline with the
519/// X86 isel pass.
520void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
521 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
522 E = DAG.allnodes_end(); I != E; ) {
523 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
524 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
525 continue;
526
527 // If the source and destination are SSE registers, then this is a legal
528 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000529 MVT SrcVT = N->getOperand(0).getValueType();
530 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000531 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
532 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
533 if (SrcIsSSE && DstIsSSE)
534 continue;
535
Chris Lattner5d294e52008-03-09 07:05:32 +0000536 if (!SrcIsSSE && !DstIsSSE) {
537 // If this is an FPStack extension, it is a noop.
538 if (N->getOpcode() == ISD::FP_EXTEND)
539 continue;
540 // If this is a value-preserving FPStack truncation, it is a noop.
541 if (N->getConstantOperandVal(1))
542 continue;
543 }
544
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000545 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
546 // FPStack has extload and truncstore. SSE can fold direct loads into other
547 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000548 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000549 if (N->getOpcode() == ISD::FP_ROUND)
550 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
551 else
552 MemVT = SrcIsSSE ? SrcVT : DstVT;
553
Dan Gohman8181bd12008-07-27 21:46:04 +0000554 SDValue MemTmp = DAG.CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000555
556 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman8181bd12008-07-27 21:46:04 +0000557 SDValue Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000558 MemTmp, NULL, 0, MemVT);
Dan Gohman8181bd12008-07-27 21:46:04 +0000559 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000560 NULL, 0, MemVT);
561
562 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
563 // extload we created. This will cause general havok on the dag because
564 // anything below the conversion could be folded into other existing nodes.
565 // To avoid invalidating 'I', back it up to the convert node.
566 --I;
Dan Gohman8181bd12008-07-27 21:46:04 +0000567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000568
569 // Now that we did that, the node is dead. Increment the iterator to the
570 // next node to process, then delete N.
571 ++I;
572 DAG.DeleteNode(N);
573 }
574}
575
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
577/// when it has created a SelectionDAG for us to codegen.
Evan Cheng34fd4f32008-06-30 20:45:06 +0000578void X86DAGToDAGISel::InstructionSelect(SelectionDAG &DAG) {
579 CurBB = BB; // BB can change as result of isel.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580
Evan Cheng34fd4f32008-06-30 20:45:06 +0000581 DEBUG(BB->dump());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 if (!FastISel)
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000583 PreprocessForRMW(DAG);
584
585 // FIXME: This should only happen when not -fast.
586 PreprocessForFPConvert(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587
588 // Codegen the basic block.
589#ifndef NDEBUG
590 DOUT << "===== Instruction selection begins:\n";
591 Indent = 0;
592#endif
593 DAG.setRoot(SelectRoot(DAG.getRoot()));
594#ifndef NDEBUG
595 DOUT << "===== Instruction selection ends:\n";
596#endif
597
598 DAG.RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000599}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600
Dan Gohmanb552df72008-07-21 20:00:07 +0000601void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 // If we are emitting FP stack code, scan the basic block to determine if this
603 // block defines any FP values. If so, put an FP_REG_KILL instruction before
604 // the terminator of the block.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000605
Dale Johannesen684887e2007-09-24 22:52:39 +0000606 // Note that FP stack instructions are used in all modes for long double,
607 // so we always need to do this check.
608 // Also note that it's possible for an FP stack register to be live across
609 // an instruction that produces multiple basic blocks (SSE CMOV) so we
610 // must check all the generated basic blocks.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000611
612 // Scan all of the machine instructions in these MBBs, checking for FP
613 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Cheng34fd4f32008-06-30 20:45:06 +0000614 MachineFunction::iterator MBBI = CurBB;
Chris Lattner04d64b22008-03-10 23:34:12 +0000615 MachineFunction::iterator EndMBB = BB; ++EndMBB;
616 for (; MBBI != EndMBB; ++MBBI) {
617 MachineBasicBlock *MBB = MBBI;
618
619 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
620 // before the return.
621 if (!MBB->empty()) {
622 MachineBasicBlock::iterator EndI = MBB->end();
623 --EndI;
624 if (EndI->getDesc().isReturn())
625 continue;
626 }
627
Dale Johannesen684887e2007-09-24 22:52:39 +0000628 bool ContainsFPCode = false;
Chris Lattner04d64b22008-03-10 23:34:12 +0000629 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000630 !ContainsFPCode && I != E; ++I) {
631 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
632 const TargetRegisterClass *clas;
633 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
634 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner04d64b22008-03-10 23:34:12 +0000635 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner1b989192007-12-31 04:13:23 +0000636 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000637 X86::RFP32RegisterClass ||
638 clas == X86::RFP64RegisterClass ||
639 clas == X86::RFP80RegisterClass)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 ContainsFPCode = true;
641 break;
642 }
643 }
644 }
645 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000646 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
647 // a copy of the input value in this block. In SSE mode, we only care about
648 // 80-bit values.
649 if (!ContainsFPCode) {
650 // Final check, check LLVM BB's that are successors to the LLVM BB
651 // corresponding to BB for FP PHI nodes.
652 const BasicBlock *LLVMBB = BB->getBasicBlock();
653 const PHINode *PN;
654 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
655 !ContainsFPCode && SI != E; ++SI) {
656 for (BasicBlock::const_iterator II = SI->begin();
657 (PN = dyn_cast<PHINode>(II)); ++II) {
658 if (PN->getType()==Type::X86_FP80Ty ||
659 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
660 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
661 ContainsFPCode = true;
662 break;
663 }
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000664 }
665 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000667 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
668 if (ContainsFPCode) {
Chris Lattner04d64b22008-03-10 23:34:12 +0000669 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen684887e2007-09-24 22:52:39 +0000670 TM.getInstrInfo()->get(X86::FP_REG_KILL));
671 ++NumFPKill;
672 }
Chris Lattner04d64b22008-03-10 23:34:12 +0000673 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674}
675
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000676/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
677/// the main function.
678void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
679 MachineFrameInfo *MFI) {
680 const TargetInstrInfo *TII = TM.getInstrInfo();
681 if (Subtarget->isTargetCygMing())
682 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
683}
684
685void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
686 // If this is main, emit special code for main.
687 MachineBasicBlock *BB = MF.begin();
688 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
689 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
690}
691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692/// MatchAddress - Add the specified node to the specified addressing mode,
693/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000694/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000695bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 bool isRoot, unsigned Depth) {
Dale Johannesenc501c082008-08-11 23:46:25 +0000697DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000698 // Limit recursion.
699 if (Depth > 5)
700 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
702 // RIP relative addressing: %rip + 32-bit displacement!
703 if (AM.isRIPRel) {
704 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
705 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
706 if (isInt32(AM.Disp + Val)) {
707 AM.Disp += Val;
708 return false;
709 }
710 }
711 return true;
712 }
713
714 int id = N.Val->getNodeId();
Evan Chengf2abee72007-12-13 00:43:27 +0000715 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 switch (N.getOpcode()) {
718 default: break;
719 case ISD::Constant: {
720 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
721 if (isInt32(AM.Disp + Val)) {
722 AM.Disp += Val;
723 return false;
724 }
725 break;
726 }
727
728 case X86ISD::Wrapper: {
Dale Johannesenc501c082008-08-11 23:46:25 +0000729DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
730DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
731DOUT << "AlreadySelected " << AlreadySelected << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 bool is64Bit = Subtarget->is64Bit();
733 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000734 // Also, base and index reg must be 0 in order to use rip as base.
735 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
736 AM.Base.Reg.Val || AM.IndexReg.Val))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 break;
738 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
739 break;
740 // If value is available in a register both base and index components have
741 // been picked, we can't fit the result available in the register in the
742 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Chengf2abee72007-12-13 00:43:27 +0000743 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000744 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
746 GlobalValue *GV = G->getGlobal();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000747 AM.GV = GV;
748 AM.Disp += G->getOffset();
Evan Chenga54e14f2008-02-12 19:20:46 +0000749 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
750 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000751 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000753 AM.CP = CP->getConstVal();
754 AM.Align = CP->getAlignment();
755 AM.Disp += CP->getOffset();
Evan Chenga54e14f2008-02-12 19:20:46 +0000756 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
757 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000758 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000760 AM.ES = S->getSymbol();
Evan Chenga54e14f2008-02-12 19:20:46 +0000761 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
762 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000763 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000765 AM.JT = J->getIndex();
Evan Chenga54e14f2008-02-12 19:20:46 +0000766 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
767 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000768 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 }
770 }
771 break;
772 }
773
774 case ISD::FrameIndex:
775 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
776 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
777 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
778 return false;
779 }
780 break;
781
782 case ISD::SHL:
Evan Cheng3b5a1272008-02-07 08:53:49 +0000783 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000784 break;
785
786 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
787 unsigned Val = CN->getValue();
788 if (Val == 1 || Val == 2 || Val == 3) {
789 AM.Scale = 1 << Val;
Dan Gohman8181bd12008-07-27 21:46:04 +0000790 SDValue ShVal = N.Val->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791
Chris Lattner7f06edd2007-12-08 07:22:58 +0000792 // Okay, we know that we have a scale by now. However, if the scaled
793 // value is an add of something and a constant, we can fold the
794 // constant into the disp field here.
795 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
796 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
797 AM.IndexReg = ShVal.Val->getOperand(0);
798 ConstantSDNode *AddVal =
799 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
800 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
801 if (isInt32(Disp))
802 AM.Disp = Disp;
803 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000805 } else {
806 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000808 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 }
810 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000811 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812
Dan Gohman35b99222007-10-22 20:22:24 +0000813 case ISD::SMUL_LOHI:
814 case ISD::UMUL_LOHI:
815 // A mul_lohi where we need the low part can be folded as a plain multiply.
816 if (N.ResNo != 0) break;
817 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 case ISD::MUL:
819 // X*[3,5,9] -> X+X*[2,4,8]
Evan Chengf2abee72007-12-13 00:43:27 +0000820 if (!AlreadySelected &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 AM.BaseType == X86ISelAddressMode::RegBase &&
822 AM.Base.Reg.Val == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000823 AM.IndexReg.Val == 0 &&
824 !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
826 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
827 AM.Scale = unsigned(CN->getValue())-1;
828
Dan Gohman8181bd12008-07-27 21:46:04 +0000829 SDValue MulVal = N.Val->getOperand(0);
830 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831
832 // Okay, we know that we have a scale by now. However, if the scaled
833 // value is an add of something and a constant, we can fold the
834 // constant into the disp field here.
835 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
836 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
837 Reg = MulVal.Val->getOperand(0);
838 ConstantSDNode *AddVal =
839 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
840 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
841 if (isInt32(Disp))
842 AM.Disp = Disp;
843 else
844 Reg = N.Val->getOperand(0);
845 } else {
846 Reg = N.Val->getOperand(0);
847 }
848
849 AM.IndexReg = AM.Base.Reg = Reg;
850 return false;
851 }
852 }
853 break;
854
855 case ISD::ADD:
Evan Chengf2abee72007-12-13 00:43:27 +0000856 if (!AlreadySelected) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 X86ISelAddressMode Backup = AM;
858 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
859 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
860 return false;
861 AM = Backup;
862 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
863 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
864 return false;
865 AM = Backup;
866 }
867 break;
868
869 case ISD::OR:
870 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Chengf2abee72007-12-13 00:43:27 +0000871 if (AlreadySelected) break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000872
873 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
874 X86ISelAddressMode Backup = AM;
875 // Start with the LHS as an addr mode.
876 if (!MatchAddress(N.getOperand(0), AM, false) &&
877 // Address could not have picked a GV address for the displacement.
878 AM.GV == NULL &&
879 // On x86-64, the resultant disp must fit in 32-bits.
880 isInt32(AM.Disp + CN->getSignExtended()) &&
881 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000882 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner7f06edd2007-12-08 07:22:58 +0000883 AM.Disp += CN->getValue();
884 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000886 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 }
888 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000889
890 case ISD::AND: {
891 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
892 // allows us to fold the shift into this addressing mode.
893 if (AlreadySelected) break;
Dan Gohman8181bd12008-07-27 21:46:04 +0000894 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000895 if (Shift.getOpcode() != ISD::SHL) break;
896
897 // Scale must not be used already.
898 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000899
900 // Not when RIP is used as the base.
901 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000902
903 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
904 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
905 if (!C1 || !C2) break;
906
907 // Not likely to be profitable if either the AND or SHIFT node has more
908 // than one use (unless all uses are for address computation). Besides,
909 // isel mechanism requires their node ids to be reused.
910 if (!N.hasOneUse() || !Shift.hasOneUse())
911 break;
912
913 // Verify that the shift amount is something we can fold.
914 unsigned ShiftCst = C1->getValue();
915 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
916 break;
917
918 // Get the new AND mask, this folds to a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +0000919 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
920 SDValue(C2, 0), SDValue(C1, 0));
921 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Chengf2abee72007-12-13 00:43:27 +0000922 Shift.getOperand(0), NewANDMask);
923 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
924 NewAND.Val->setNodeId(N.Val->getNodeId());
925
926 AM.Scale = 1 << ShiftCst;
927 AM.IndexReg = NewAND;
928 return false;
929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 }
931
Dan Gohmana60c1b32007-08-13 20:03:06 +0000932 return MatchAddressBase(N, AM, isRoot, Depth);
933}
934
935/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
936/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000937bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000938 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939 // Is the base register already occupied?
940 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
941 // If so, check to see if the scale index register is set.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000942 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 AM.IndexReg = N;
944 AM.Scale = 1;
945 return false;
946 }
947
948 // Otherwise, we cannot select it.
949 return true;
950 }
951
952 // Default, generate it as a register.
953 AM.BaseType = X86ISelAddressMode::RegBase;
954 AM.Base.Reg = N;
955 return false;
956}
957
958/// SelectAddr - returns true if it is able pattern match an addressing mode.
959/// It returns the operands which make up the maximal addressing mode it can
960/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +0000961bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
962 SDValue &Scale, SDValue &Index,
963 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 X86ISelAddressMode AM;
965 if (MatchAddress(N, AM))
966 return false;
967
Duncan Sands92c43912008-06-06 12:08:01 +0000968 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 if (AM.BaseType == X86ISelAddressMode::RegBase) {
970 if (!AM.Base.Reg.Val)
971 AM.Base.Reg = CurDAG->getRegister(0, VT);
972 }
973
974 if (!AM.IndexReg.Val)
975 AM.IndexReg = CurDAG->getRegister(0, VT);
976
977 getAddressOperands(AM, Base, Scale, Index, Disp);
978 return true;
979}
980
981/// isZeroNode - Returns true if Elt is a constant zero or a floating point
982/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +0000983static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 return ((isa<ConstantSDNode>(Elt) &&
985 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
986 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +0000987 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988}
989
990
991/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
992/// match a load whose top elements are either undef or zeros. The load flavor
993/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +0000994bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
995 SDValue N, SDValue &Base,
996 SDValue &Scale, SDValue &Index,
997 SDValue &Disp, SDValue &InChain,
998 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1000 InChain = N.getOperand(0).getValue(1);
1001 if (ISD::isNON_EXTLoad(InChain.Val) &&
1002 InChain.getValue(0).hasOneUse() &&
1003 N.hasOneUse() &&
1004 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
1005 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1006 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1007 return false;
1008 OutChain = LD->getChain();
1009 return true;
1010 }
1011 }
1012
1013 // Also handle the case where we explicitly require zeros in the top
1014 // elements. This is a vector shuffle from the zero vector.
Evan Chenge9b9c672008-05-09 21:53:03 +00001015 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.Val->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001016 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001017 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
1018 N.getOperand(0).Val->hasOneUse() &&
1019 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
1020 N.getOperand(0).getOperand(0).hasOneUse()) {
1021 // Okay, this is a zero extending load. Fold it.
1022 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1023 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1024 return false;
1025 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001026 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001027 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 }
1029 return false;
1030}
1031
1032
1033/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1034/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001035bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1036 SDValue &Base, SDValue &Scale,
1037 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 X86ISelAddressMode AM;
1039 if (MatchAddress(N, AM))
1040 return false;
1041
Duncan Sands92c43912008-06-06 12:08:01 +00001042 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 unsigned Complexity = 0;
1044 if (AM.BaseType == X86ISelAddressMode::RegBase)
1045 if (AM.Base.Reg.Val)
1046 Complexity = 1;
1047 else
1048 AM.Base.Reg = CurDAG->getRegister(0, VT);
1049 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1050 Complexity = 4;
1051
1052 if (AM.IndexReg.Val)
1053 Complexity++;
1054 else
1055 AM.IndexReg = CurDAG->getRegister(0, VT);
1056
1057 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1058 // a simple shift.
1059 if (AM.Scale > 1)
1060 Complexity++;
1061
1062 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1063 // to a LEA. This is determined with some expermentation but is by no means
1064 // optimal (especially for code size consideration). LEA is nice because of
1065 // its three-address nature. Tweak the cost function again when we can run
1066 // convertToThreeAddress() at register allocation time.
1067 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1068 // For X86-64, we should always use lea to materialize RIP relative
1069 // addresses.
1070 if (Subtarget->is64Bit())
1071 Complexity = 4;
1072 else
1073 Complexity += 2;
1074 }
1075
1076 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1077 Complexity++;
1078
1079 if (Complexity > 2) {
1080 getAddressOperands(AM, Base, Scale, Index, Disp);
1081 return true;
1082 }
1083 return false;
1084}
1085
Dan Gohman8181bd12008-07-27 21:46:04 +00001086bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1087 SDValue &Base, SDValue &Scale,
1088 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 if (ISD::isNON_EXTLoad(N.Val) &&
1090 N.hasOneUse() &&
1091 CanBeFoldedBy(N.Val, P.Val, P.Val))
1092 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1093 return false;
1094}
1095
1096/// getGlobalBaseReg - Output the instructions required to put the
1097/// base address to use for accessing globals into a register.
1098///
1099SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1100 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
1101 if (!GlobalBaseReg) {
1102 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0729ccf2008-01-05 00:41:47 +00001103 MachineFunction *MF = BB->getParent();
1104 MachineBasicBlock &FirstMBB = MF->front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0729ccf2008-01-05 00:41:47 +00001106 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner1b989192007-12-31 04:13:23 +00001107 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108
1109 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Cheng34f93712007-12-22 02:26:46 +00001110 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1111 // only used in JIT code emission as displacement to pc.
Evan Cheng0729ccf2008-01-05 00:41:47 +00001112 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113
1114 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1115 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1116 if (TM.getRelocationModel() == Reloc::PIC_ &&
1117 Subtarget->isPICStyleGOT()) {
Chris Lattner1b989192007-12-31 04:13:23 +00001118 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0729ccf2008-01-05 00:41:47 +00001119 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1120 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 } else {
1122 GlobalBaseReg = PC;
1123 }
1124
1125 }
1126 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
1127}
1128
1129static SDNode *FindCallStartFromCall(SDNode *Node) {
1130 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1131 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1132 "Node doesn't have a token chain argument!");
1133 return FindCallStartFromCall(Node->getOperand(0).Val);
1134}
1135
Dan Gohman8181bd12008-07-27 21:46:04 +00001136SDNode *X86DAGToDAGISel::getTruncate(SDValue N0, MVT VT) {
1137 SDValue SRIdx;
Duncan Sands92c43912008-06-06 12:08:01 +00001138 switch (VT.getSimpleVT()) {
1139 default: assert(0 && "Unknown truncate!");
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001140 case MVT::i8:
1141 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1142 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1143 if (!Subtarget->is64Bit()) {
1144 unsigned Opc;
Dan Gohmand5a14852008-07-16 16:20:48 +00001145 MVT N0VT = N0.getValueType();
1146 switch (N0VT.getSimpleVT()) {
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001147 default: assert(0 && "Unknown truncate!");
1148 case MVT::i16:
1149 Opc = X86::MOV16to16_;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001150 break;
1151 case MVT::i32:
1152 Opc = X86::MOV32to32_;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001153 break;
1154 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001155 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
Evan Chenge1f39552007-10-12 07:55:53 +00001156 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1157 VT, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001158 }
1159 break;
1160 case MVT::i16:
1161 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1162 break;
1163 case MVT::i32:
1164 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1165 break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001166 }
Evan Chenge1f39552007-10-12 07:55:53 +00001167 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001168}
1169
1170
Dan Gohman8181bd12008-07-27 21:46:04 +00001171SDNode *X86DAGToDAGISel::Select(SDValue N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 SDNode *Node = N.Val;
Duncan Sands92c43912008-06-06 12:08:01 +00001173 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 unsigned Opc, MOpc;
1175 unsigned Opcode = Node->getOpcode();
1176
1177#ifndef NDEBUG
1178 DOUT << std::string(Indent, ' ') << "Selecting: ";
1179 DEBUG(Node->dump(CurDAG));
1180 DOUT << "\n";
1181 Indent += 2;
1182#endif
1183
Dan Gohmanbd68c792008-07-17 19:10:17 +00001184 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185#ifndef NDEBUG
1186 DOUT << std::string(Indent-2, ' ') << "== ";
1187 DEBUG(Node->dump(CurDAG));
1188 DOUT << "\n";
1189 Indent -= 2;
1190#endif
1191 return NULL; // Already selected.
1192 }
1193
1194 switch (Opcode) {
1195 default: break;
1196 case X86ISD::GlobalBaseReg:
1197 return getGlobalBaseReg();
1198
1199 case ISD::ADD: {
1200 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1201 // code and is matched first so to prevent it from being turned into
1202 // LEA32r X+c.
Evan Cheng17e39d62008-01-08 02:06:11 +00001203 // In 64-bit small code size mode, use LEA to take advantage of
1204 // RIP-relative addressing.
1205 if (TM.getCodeModel() != CodeModel::Small)
1206 break;
Duncan Sands92c43912008-06-06 12:08:01 +00001207 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001208 SDValue N0 = N.getOperand(0);
1209 SDValue N1 = N.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 if (N.Val->getValueType(0) == PtrVT &&
1211 N0.getOpcode() == X86ISD::Wrapper &&
1212 N1.getOpcode() == ISD::Constant) {
1213 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00001214 SDValue C(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215 // TODO: handle ExternalSymbolSDNode.
1216 if (GlobalAddressSDNode *G =
1217 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1218 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1219 G->getOffset() + Offset);
1220 } else if (ConstantPoolSDNode *CP =
1221 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1222 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1223 CP->getAlignment(),
1224 CP->getOffset()+Offset);
1225 }
1226
1227 if (C.Val) {
1228 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001229 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001230 CurDAG->getRegister(0, PtrVT), C };
1231 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1232 } else
1233 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1234 }
1235 }
1236
1237 // Other cases are handled by auto-generated code.
1238 break;
1239 }
1240
Dan Gohman5a199552007-10-08 18:33:35 +00001241 case ISD::SMUL_LOHI:
1242 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001243 SDValue N0 = Node->getOperand(0);
1244 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001245
Dan Gohman5a199552007-10-08 18:33:35 +00001246 bool isSigned = Opcode == ISD::SMUL_LOHI;
1247 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001248 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 default: assert(0 && "Unsupported VT!");
1250 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1251 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1252 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1253 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1254 }
1255 else
Duncan Sands92c43912008-06-06 12:08:01 +00001256 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257 default: assert(0 && "Unsupported VT!");
1258 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1259 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1260 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1261 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1262 }
1263
1264 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001265 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 default: assert(0 && "Unsupported VT!");
1267 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1268 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1269 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1270 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1271 }
1272
Dan Gohman8181bd12008-07-27 21:46:04 +00001273 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001274 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001275 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 if (!foldedLoad) {
1277 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001278 if (foldedLoad)
1279 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 }
1281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 AddToISelQueue(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001283 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1284 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285
1286 if (foldedLoad) {
Dan Gohman5a199552007-10-08 18:33:35 +00001287 AddToISelQueue(N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 AddToISelQueue(Tmp0);
1289 AddToISelQueue(Tmp1);
1290 AddToISelQueue(Tmp2);
1291 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001292 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 SDNode *CNode =
1294 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001295 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001296 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001297 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 } else {
1299 AddToISelQueue(N1);
1300 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001301 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 }
1303
Dan Gohman5a199552007-10-08 18:33:35 +00001304 // Copy the low half of the result, if it is needed.
1305 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001306 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001307 LoReg, NVT, InFlag);
1308 InFlag = Result.getValue(2);
1309 ReplaceUses(N.getValue(0), Result);
1310#ifndef NDEBUG
1311 DOUT << std::string(Indent-2, ' ') << "=> ";
1312 DEBUG(Result.Val->dump(CurDAG));
1313 DOUT << "\n";
1314#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001315 }
Dan Gohman5a199552007-10-08 18:33:35 +00001316 // Copy the high half of the result, if it is needed.
1317 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001318 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001319 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1320 // Prevent use of AH in a REX instruction by referencing AX instead.
1321 // Shift it down 8 bits.
1322 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1323 X86::AX, MVT::i16, InFlag);
1324 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001325 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman5a199552007-10-08 18:33:35 +00001326 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1327 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001328 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1329 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001330 MVT::i8, Result, SRIdx), 0);
1331 } else {
1332 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1333 HiReg, NVT, InFlag);
1334 InFlag = Result.getValue(2);
1335 }
1336 ReplaceUses(N.getValue(1), Result);
1337#ifndef NDEBUG
1338 DOUT << std::string(Indent-2, ' ') << "=> ";
1339 DEBUG(Result.Val->dump(CurDAG));
1340 DOUT << "\n";
1341#endif
1342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343
1344#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345 Indent -= 2;
1346#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 return NULL;
1349 }
1350
Dan Gohman5a199552007-10-08 18:33:35 +00001351 case ISD::SDIVREM:
1352 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue N0 = Node->getOperand(0);
1354 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001355
1356 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001358 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 default: assert(0 && "Unsupported VT!");
1360 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1361 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1362 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1363 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1364 }
1365 else
Duncan Sands92c43912008-06-06 12:08:01 +00001366 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 default: assert(0 && "Unsupported VT!");
1368 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1369 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1370 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1371 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1372 }
1373
1374 unsigned LoReg, HiReg;
1375 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001376 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 default: assert(0 && "Unsupported VT!");
1378 case MVT::i8:
1379 LoReg = X86::AL; HiReg = X86::AH;
1380 ClrOpcode = 0;
1381 SExtOpcode = X86::CBW;
1382 break;
1383 case MVT::i16:
1384 LoReg = X86::AX; HiReg = X86::DX;
1385 ClrOpcode = X86::MOV16r0;
1386 SExtOpcode = X86::CWD;
1387 break;
1388 case MVT::i32:
1389 LoReg = X86::EAX; HiReg = X86::EDX;
1390 ClrOpcode = X86::MOV32r0;
1391 SExtOpcode = X86::CDQ;
1392 break;
1393 case MVT::i64:
1394 LoReg = X86::RAX; HiReg = X86::RDX;
1395 ClrOpcode = X86::MOV64r0;
1396 SExtOpcode = X86::CQO;
1397 break;
1398 }
1399
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001401 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1402
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 if (NVT == MVT::i8 && !isSigned) {
1405 // Special case for div8, just use a move with zero extension to AX to
1406 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001407 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001409 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 AddToISelQueue(N0.getOperand(0));
1411 AddToISelQueue(Tmp0);
1412 AddToISelQueue(Tmp1);
1413 AddToISelQueue(Tmp2);
1414 AddToISelQueue(Tmp3);
1415 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 Ops, 5), 0);
1418 Chain = Move.getValue(1);
1419 ReplaceUses(N0.getValue(1), Chain);
1420 } else {
1421 AddToISelQueue(N0);
1422 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001423 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 Chain = CurDAG->getEntryNode();
1425 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 InFlag = Chain.getValue(1);
1428 } else {
1429 AddToISelQueue(N0);
1430 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001431 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 LoReg, N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 if (isSigned) {
1434 // Sign extend the low part into the high part.
1435 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 } else {
1438 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001439 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001440 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1441 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 }
1443 }
1444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 if (foldedLoad) {
1446 AddToISelQueue(N1.getOperand(0));
1447 AddToISelQueue(Tmp0);
1448 AddToISelQueue(Tmp1);
1449 AddToISelQueue(Tmp2);
1450 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001451 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 SDNode *CNode =
1453 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001454 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001455 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001456 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 } else {
1458 AddToISelQueue(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 }
1462
Dan Gohman242a5ba2007-09-25 18:23:27 +00001463 // Copy the division (low) result, if it is needed.
1464 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001465 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001466 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001467 InFlag = Result.getValue(2);
1468 ReplaceUses(N.getValue(0), Result);
1469#ifndef NDEBUG
1470 DOUT << std::string(Indent-2, ' ') << "=> ";
1471 DEBUG(Result.Val->dump(CurDAG));
1472 DOUT << "\n";
1473#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001474 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001475 // Copy the remainder (high) result, if it is needed.
1476 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001478 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1479 // Prevent use of AH in a REX instruction by referencing AX instead.
1480 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001481 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1482 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001483 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001484 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001485 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1486 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001487 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1488 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001489 MVT::i8, Result, SRIdx), 0);
1490 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001491 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1492 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001493 InFlag = Result.getValue(2);
1494 }
1495 ReplaceUses(N.getValue(1), Result);
1496#ifndef NDEBUG
1497 DOUT << std::string(Indent-2, ' ') << "=> ";
1498 DEBUG(Result.Val->dump(CurDAG));
1499 DOUT << "\n";
1500#endif
1501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
1503#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 Indent -= 2;
1505#endif
1506
1507 return NULL;
1508 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001509
1510 case ISD::ANY_EXTEND: {
Christopher Lamb76d72da2008-03-16 03:12:01 +00001511 // Check if the type extended to supports subregs.
1512 if (NVT == MVT::i8)
1513 break;
1514
Dan Gohman8181bd12008-07-27 21:46:04 +00001515 SDValue N0 = Node->getOperand(0);
Christopher Lamb76d72da2008-03-16 03:12:01 +00001516 // Get the subregsiter index for the type to extend.
Duncan Sands92c43912008-06-06 12:08:01 +00001517 MVT N0VT = N0.getValueType();
Christopher Lamb76d72da2008-03-16 03:12:01 +00001518 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1519 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1520 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1521
1522 // If we don't have a subreg Idx, let generated ISel have a try.
1523 if (Idx == 0)
1524 break;
1525
1526 // If we have an index, generate an insert_subreg into undef.
Christopher Lamb422213d2007-08-10 22:22:41 +00001527 AddToISelQueue(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001528 SDValue Undef =
1529 SDValue(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
1530 SDValue SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
Christopher Lamb76d72da2008-03-16 03:12:01 +00001531 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng55a2dd02008-04-03 07:45:18 +00001532 NVT, Undef, N0, SRIdx);
Christopher Lamb422213d2007-08-10 22:22:41 +00001533
1534#ifndef NDEBUG
Christopher Lamb76d72da2008-03-16 03:12:01 +00001535 DOUT << std::string(Indent-2, ' ') << "=> ";
1536 DEBUG(ResNode->dump(CurDAG));
1537 DOUT << "\n";
1538 Indent -= 2;
Christopher Lamb422213d2007-08-10 22:22:41 +00001539#endif
Christopher Lamb76d72da2008-03-16 03:12:01 +00001540 return ResNode;
Christopher Lamb422213d2007-08-10 22:22:41 +00001541 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001542
1543 case ISD::SIGN_EXTEND_INREG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001544 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001545 AddToISelQueue(N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546
Duncan Sands92c43912008-06-06 12:08:01 +00001547 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00001548 SDValue TruncOp = SDValue(getTruncate(N0, SVT), 0);
Bill Wendling79bb1a22007-11-01 08:51:44 +00001549 unsigned Opc = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00001550 switch (NVT.getSimpleVT()) {
1551 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb444336c2007-07-29 01:24:57 +00001552 case MVT::i16:
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001553 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1554 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb444336c2007-07-29 01:24:57 +00001555 break;
1556 case MVT::i32:
Duncan Sands92c43912008-06-06 12:08:01 +00001557 switch (SVT.getSimpleVT()) {
1558 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001559 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1560 case MVT::i16: Opc = X86::MOVSX32rr16; break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001561 }
Christopher Lamb444336c2007-07-29 01:24:57 +00001562 break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001563 case MVT::i64:
Duncan Sands92c43912008-06-06 12:08:01 +00001564 switch (SVT.getSimpleVT()) {
1565 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001566 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1567 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1568 case MVT::i32: Opc = X86::MOVSX64rr32; break;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001569 }
1570 break;
Christopher Lamb444336c2007-07-29 01:24:57 +00001571 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001572
1573 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1574
1575#ifndef NDEBUG
1576 DOUT << std::string(Indent-2, ' ') << "=> ";
1577 DEBUG(TruncOp.Val->dump(CurDAG));
1578 DOUT << "\n";
1579 DOUT << std::string(Indent-2, ' ') << "=> ";
1580 DEBUG(ResNode->dump(CurDAG));
1581 DOUT << "\n";
1582 Indent -= 2;
1583#endif
1584 return ResNode;
1585 break;
1586 }
1587
1588 case ISD::TRUNCATE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001589 SDValue Input = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001590 AddToISelQueue(Node->getOperand(0));
1591 SDNode *ResNode = getTruncate(Input, NVT);
1592
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593#ifndef NDEBUG
1594 DOUT << std::string(Indent-2, ' ') << "=> ";
1595 DEBUG(ResNode->dump(CurDAG));
1596 DOUT << "\n";
1597 Indent -= 2;
1598#endif
Christopher Lamb444336c2007-07-29 01:24:57 +00001599 return ResNode;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 break;
1601 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001602
1603 case ISD::DECLARE: {
1604 // Handle DECLARE nodes here because the second operand may have been
1605 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001606 SDValue Chain = Node->getOperand(0);
1607 SDValue N1 = Node->getOperand(1);
1608 SDValue N2 = Node->getOperand(2);
Evan Cheng651e1442008-06-18 02:48:27 +00001609 if (!isa<FrameIndexSDNode>(N1))
1610 break;
1611 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1612 if (N2.getOpcode() == ISD::ADD &&
1613 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1614 N2 = N2.getOperand(1);
1615 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Chengd4cebcd2008-06-17 02:01:22 +00001616 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Chengd4cebcd2008-06-17 02:01:22 +00001617 GlobalValue *GV =
1618 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00001619 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1620 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Chengd4cebcd2008-06-17 02:01:22 +00001621 AddToISelQueue(Chain);
Dan Gohman8181bd12008-07-27 21:46:04 +00001622 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Chengd4cebcd2008-06-17 02:01:22 +00001623 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1624 MVT::Other, Ops, 3);
1625 }
1626 break;
1627 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 }
1629
1630 SDNode *ResNode = SelectCode(N);
1631
1632#ifndef NDEBUG
1633 DOUT << std::string(Indent-2, ' ') << "=> ";
1634 if (ResNode == NULL || ResNode == N.Val)
1635 DEBUG(N.Val->dump(CurDAG));
1636 else
1637 DEBUG(ResNode->dump(CurDAG));
1638 DOUT << "\n";
1639 Indent -= 2;
1640#endif
1641
1642 return ResNode;
1643}
1644
1645bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001646SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1647 std::vector<SDValue> &OutOps, SelectionDAG &DAG){
1648 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 switch (ConstraintCode) {
1650 case 'o': // offsetable ??
1651 case 'v': // not offsetable ??
1652 default: return true;
1653 case 'm': // memory
1654 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1655 return true;
1656 break;
1657 }
1658
1659 OutOps.push_back(Op0);
1660 OutOps.push_back(Op1);
1661 OutOps.push_back(Op2);
1662 OutOps.push_back(Op3);
1663 AddToISelQueue(Op0);
1664 AddToISelQueue(Op1);
1665 AddToISelQueue(Op2);
1666 AddToISelQueue(Op3);
1667 return false;
1668}
1669
1670/// createX86ISelDag - This pass converts a legalized DAG into a
1671/// X86-specific DAG, ready for instruction scheduling.
1672///
1673FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1674 return new X86DAGToDAGISel(TM, Fast);
1675}