blob: 54b04ecabc69c7a91cfb7b97e71e293d3da5b84a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Cheng13559d62008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Compiler.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
51 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
57 FrameIndexBase
58 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 int FrameIndex;
63 } Base;
64
Evan Cheng3b5a1272008-02-07 08:53:49 +000065 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000067 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000068 int32_t Disp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 GlobalValue *GV;
70 Constant *CP;
71 const char *ES;
72 int JT;
73 unsigned Align; // CP alignment.
74
75 X86ISelAddressMode()
76 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
78 }
Dale Johannesenc501c082008-08-11 23:46:25 +000079 void dump() {
80 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000081 cerr << "Base.Reg ";
82 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
83 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000084 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
85 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000086 cerr << "IndexReg ";
87 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
88 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000089 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 {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 /// TM - Keep a reference to X86TargetMachine.
108 ///
109 X86TargetMachine &TM;
110
111 /// X86Lowering - This object fully describes how to lower LLVM code to an
112 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000113 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
116 /// make the right decision when generating code for different targets.
117 const X86Subtarget *Subtarget;
118
Evan Cheng34fd4f32008-06-30 20:45:06 +0000119 /// CurBB - Current BB being isel'd.
120 ///
121 MachineBasicBlock *CurBB;
122
Evan Cheng13559d62008-09-26 23:41:32 +0000123 /// OptForSize - If true, selector should try to optimize for code size
124 /// instead of performance.
125 bool OptForSize;
126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 public:
128 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000129 : SelectionDAGISel(tm, fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000130 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000131 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000132 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 virtual const char *getPassName() const {
135 return "X86 DAG->DAG Instruction Selection";
136 }
137
Evan Cheng34fd4f32008-06-30 20:45:06 +0000138 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000140 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000141
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000142 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
143
Evan Cheng5a424552008-11-27 00:49:46 +0000144 virtual
145 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147// Include the pieces autogenerated from the target description.
148#include "X86GenDAGISel.inc"
149
150 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000151 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000152 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000157 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000158 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
159 SDValue &Scale, SDValue &Index, SDValue &Disp);
160 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
161 SDValue &Scale, SDValue &Index, SDValue &Disp);
162 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
163 SDValue N, SDValue &Base, SDValue &Scale,
164 SDValue &Index, SDValue &Disp,
165 SDValue &InChain, SDValue &OutChain);
166 bool TryFoldLoad(SDValue P, SDValue N,
167 SDValue &Base, SDValue &Scale,
168 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000169 void PreprocessForRMW();
170 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
172 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
173 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000174 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000176 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000178 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
179
Dan Gohman8181bd12008-07-27 21:46:04 +0000180 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
181 SDValue &Scale, SDValue &Index,
182 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
184 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
185 AM.Base.Reg;
186 Scale = getI8Imm(AM.Scale);
187 Index = AM.IndexReg;
188 // These are 32-bit even in 64-bit mode since RIP relative offset
189 // is 32-bit.
190 if (AM.GV)
191 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
192 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000193 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
194 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000196 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 else if (AM.JT != -1)
198 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
199 else
Dan Gohman0bd76b72008-11-11 15:52:29 +0000200 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 }
202
203 /// getI8Imm - Return a target constant with the specified value, of type
204 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000205 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return CurDAG->getTargetConstant(Imm, MVT::i8);
207 }
208
209 /// getI16Imm - Return a target constant with the specified value, of type
210 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 return CurDAG->getTargetConstant(Imm, MVT::i16);
213 }
214
215 /// getI32Imm - Return a target constant with the specified value, of type
216 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000217 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i32);
219 }
220
Dan Gohmanb60482f2008-09-23 18:22:58 +0000221 /// getGlobalBaseReg - Return an SDNode that returns the value of
222 /// the global base register. Output instructions required to
223 /// initialize the global base register, if necessary.
224 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 SDNode *getGlobalBaseReg();
226
Dan Gohmandd612bb2008-08-20 21:27:32 +0000227 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
228 /// truncate of the specified operand to i8. This can be done with tablegen,
229 /// except that this code uses MVT::Flag in a tricky way that happens to
230 /// improve scheduling in some cases.
231 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233#ifndef NDEBUG
234 unsigned Indent;
235#endif
236 };
237}
238
Gabor Greife9f7f582008-08-31 15:37:04 +0000239/// findFlagUse - Return use of MVT::Flag value produced by the specified
240/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000241///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242static SDNode *findFlagUse(SDNode *N) {
243 unsigned FlagResNo = N->getNumValues()-1;
244 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman13f24a72009-01-27 02:37:43 +0000245 SDUse &Use = I.getUse();
246 if (Use.getResNo() == FlagResNo)
247 return Use.getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 }
249 return NULL;
250}
251
djg4b210952009-01-27 19:04:30 +0000252/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
253/// This function recursively traverses up the operand chain, ignoring
254/// certain nodes.
255static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
256 SDNode *Root,
Evan Cheng656269e2008-04-25 08:22:20 +0000257 SmallPtrSet<SDNode*, 16> &Visited) {
djg4b210952009-01-27 19:04:30 +0000258 if (Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000259 !Visited.insert(Use))
djg4b210952009-01-27 19:04:30 +0000260 return false;
261
262 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000263 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000265 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000266 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000267 assert(N != Root);
djg4b210952009-01-27 19:04:30 +0000268 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 }
Evan Cheng656269e2008-04-25 08:22:20 +0000270
271 // Traverse up the operand chain.
djg4b210952009-01-27 19:04:30 +0000272 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
273 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 }
djg4b210952009-01-27 19:04:30 +0000275 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276}
277
278/// isNonImmUse - Start searching from Root up the DAG to check is Def can
279/// be reached. Return true if that's the case. However, ignore direct uses
280/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng5a424552008-11-27 00:49:46 +0000281/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
282/// case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283/// FIXME: to be really generic, we should allow direct use by any node
284/// that is being folded. But realisticly since we only fold loads which
285/// have one non-chain use, we only need to watch out for load/op/store
286/// and load/op/cmp case where the root (store / cmp) may reach the load via
287/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000288static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000289 SmallPtrSet<SDNode*, 16> Visited;
djg4b210952009-01-27 19:04:30 +0000290 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291}
292
293
Evan Cheng5a424552008-11-27 00:49:46 +0000294bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
295 SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000296 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Evan Cheng5a424552008-11-27 00:49:46 +0000298 if (U == Root)
299 switch (U->getOpcode()) {
300 default: break;
301 case ISD::ADD:
302 case ISD::ADDC:
303 case ISD::ADDE:
304 case ISD::AND:
305 case ISD::OR:
306 case ISD::XOR: {
307 // If the other operand is a 8-bit immediate we should fold the immediate
308 // instead. This reduces code size.
309 // e.g.
310 // movl 4(%esp), %eax
311 // addl $4, %eax
312 // vs.
313 // movl $4, %eax
314 // addl 4(%esp), %eax
315 // The former is 2 bytes shorter. In case where the increment is 1, then
316 // the saving can be 4 bytes (by using incl %eax).
317 ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1));
318 if (Imm) {
319 if (U->getValueType(0) == MVT::i64) {
320 if ((int32_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
321 return false;
322 } else {
323 if ((int8_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
324 return false;
325 }
326 }
327 }
328 }
329
Dan Gohman602d44a2008-09-17 01:39:10 +0000330 // If Root use can somehow reach N through a path that that doesn't contain
331 // U then folding N would create a cycle. e.g. In the following
332 // diagram, Root can reach N through X. If N is folded into into Root, then
333 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000335 // [N*] //
336 // ^ ^ //
337 // / \ //
338 // [U*] [X]? //
339 // ^ ^ //
340 // \ / //
341 // \ / //
342 // [Root*] //
343 //
344 // * indicates nodes to be folded together.
345 //
346 // If Root produces a flag, then it gets (even more) interesting. Since it
347 // will be "glued" together with its flag use in the scheduler, we need to
348 // check if it might reach N.
349 //
350 // [N*] //
351 // ^ ^ //
352 // / \ //
353 // [U*] [X]? //
354 // ^ ^ //
355 // \ \ //
356 // \ | //
357 // [Root*] | //
358 // ^ | //
359 // f | //
360 // | / //
361 // [Y] / //
362 // ^ / //
363 // f / //
364 // | / //
365 // [FU] //
366 //
367 // If FU (flag use) indirectly reaches N (the load), and Root folds N
368 // (call it Fold), then X is a predecessor of FU and a successor of
369 // Fold. But since Fold and FU are flagged together, this will create
370 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371
Duncan Sands92c43912008-06-06 12:08:01 +0000372 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000373 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 SDNode *FU = findFlagUse(Root);
375 if (FU == NULL)
376 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000377 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 VT = Root->getValueType(Root->getNumValues()-1);
379 }
380
Dan Gohman602d44a2008-09-17 01:39:10 +0000381 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382}
383
384/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
385/// and move load below the TokenFactor. Replace store's chain operand with
386/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000387static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000388 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000389 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000390 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
391 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000392 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000394 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000395 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
396 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
397 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
398 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399}
400
Evan Cheng2b2a7012008-05-23 21:23:16 +0000401/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
402///
Dan Gohman8181bd12008-07-27 21:46:04 +0000403static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
404 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000405 if (N.getOpcode() == ISD::BIT_CONVERT)
406 N = N.getOperand(0);
407
408 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
409 if (!LD || LD->isVolatile())
410 return false;
411 if (LD->getAddressingMode() != ISD::UNINDEXED)
412 return false;
413
414 ISD::LoadExtType ExtType = LD->getExtensionType();
415 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
416 return false;
417
418 if (N.hasOneUse() &&
419 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000420 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000421 Load = N;
422 return true;
423 }
424 return false;
425}
426
Evan Cheng98cfaf82008-08-25 21:27:18 +0000427/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
428/// operand and move load below the call's chain operand.
429static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
evanchengcd6d72b2009-01-26 18:43:34 +0000430 SDValue Call, SDValue CallSeqStart) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000431 SmallVector<SDValue, 8> Ops;
evanchengcd6d72b2009-01-26 18:43:34 +0000432 SDValue Chain = CallSeqStart.getOperand(0);
433 if (Chain.getNode() == Load.getNode())
434 Ops.push_back(Load.getOperand(0));
435 else {
436 assert(Chain.getOpcode() == ISD::TokenFactor &&
437 "Unexpected CallSeqStart chain operand");
438 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
439 if (Chain.getOperand(i).getNode() == Load.getNode())
440 Ops.push_back(Load.getOperand(0));
441 else
442 Ops.push_back(Chain.getOperand(i));
443 SDValue NewChain =
444 CurDAG->getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
445 Ops.clear();
446 Ops.push_back(NewChain);
447 }
448 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
449 Ops.push_back(CallSeqStart.getOperand(i));
450 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000451 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
452 Load.getOperand(1), Load.getOperand(2));
453 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000454 Ops.push_back(SDValue(Load.getNode(), 1));
455 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000456 Ops.push_back(Call.getOperand(i));
457 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
458}
459
460/// isCalleeLoad - Return true if call address is a load and it can be
461/// moved below CALLSEQ_START and the chains leading up to the call.
462/// Return the CALLSEQ_START by reference as a second output.
463static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000464 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000465 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000466 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000467 if (!LD ||
468 LD->isVolatile() ||
469 LD->getAddressingMode() != ISD::UNINDEXED ||
470 LD->getExtensionType() != ISD::NON_EXTLOAD)
471 return false;
472
473 // Now let's find the callseq_start.
474 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
475 if (!Chain.hasOneUse())
476 return false;
477 Chain = Chain.getOperand(0);
478 }
evanchengcd6d72b2009-01-26 18:43:34 +0000479
480 if (Chain.getOperand(0).getNode() == Callee.getNode())
481 return true;
482 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
483 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
484 return true;
485 return false;
Evan Cheng98cfaf82008-08-25 21:27:18 +0000486}
487
488
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000489/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
490/// This is only run if not in -fast mode (aka -O0).
491/// This allows the instruction selector to pick more read-modify-write
492/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493///
494/// [Load chain]
495/// ^
496/// |
497/// [Load]
498/// ^ ^
499/// | |
500/// / \-
501/// / |
502/// [TokenFactor] [Op]
503/// ^ ^
504/// | |
505/// \ /
506/// \ /
507/// [Store]
508///
509/// The fact the store's chain operand != load's chain will prevent the
510/// (store (op (load))) instruction from being selected. We can transform it to:
511///
512/// [Load chain]
513/// ^
514/// |
515/// [TokenFactor]
516/// ^
517/// |
518/// [Load]
519/// ^ ^
520/// | |
521/// | \-
522/// | |
523/// | [Op]
524/// | ^
525/// | |
526/// \ /
527/// \ /
528/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000529void X86DAGToDAGISel::PreprocessForRMW() {
530 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
531 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000532 if (I->getOpcode() == X86ISD::CALL) {
533 /// Also try moving call address load from outside callseq_start to just
534 /// before the call to allow it to be folded.
535 ///
536 /// [Load chain]
537 /// ^
538 /// |
539 /// [Load]
540 /// ^ ^
541 /// | |
542 /// / \--
543 /// / |
544 ///[CALLSEQ_START] |
545 /// ^ |
546 /// | |
547 /// [LOAD/C2Reg] |
548 /// | |
549 /// \ /
550 /// \ /
551 /// [CALL]
552 SDValue Chain = I->getOperand(0);
553 SDValue Load = I->getOperand(1);
554 if (!isCalleeLoad(Load, Chain))
555 continue;
556 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
557 ++NumLoadMoved;
558 continue;
559 }
560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 if (!ISD::isNON_TRUNCStore(I))
562 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000563 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000564
Gabor Greif1c80d112008-08-28 21:40:38 +0000565 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 continue;
567
Dan Gohman8181bd12008-07-27 21:46:04 +0000568 SDValue N1 = I->getOperand(1);
569 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000570 if ((N1.getValueType().isFloatingPoint() &&
571 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 !N1.hasOneUse())
573 continue;
574
575 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000576 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000577 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000579 case ISD::ADD:
580 case ISD::MUL:
581 case ISD::AND:
582 case ISD::OR:
583 case ISD::XOR:
584 case ISD::ADDC:
585 case ISD::ADDE:
586 case ISD::VECTOR_SHUFFLE: {
587 SDValue N10 = N1.getOperand(0);
588 SDValue N11 = N1.getOperand(1);
589 RModW = isRMWLoad(N10, Chain, N2, Load);
590 if (!RModW)
591 RModW = isRMWLoad(N11, Chain, N2, Load);
592 break;
593 }
594 case ISD::SUB:
595 case ISD::SHL:
596 case ISD::SRA:
597 case ISD::SRL:
598 case ISD::ROTL:
599 case ISD::ROTR:
600 case ISD::SUBC:
601 case ISD::SUBE:
602 case X86ISD::SHLD:
603 case X86ISD::SHRD: {
604 SDValue N10 = N1.getOperand(0);
605 RModW = isRMWLoad(N10, Chain, N2, Load);
606 break;
607 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 }
609
610 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000611 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 ++NumLoadMoved;
613 }
614 }
615}
616
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000617
618/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
619/// nodes that target the FP stack to be store and load to the stack. This is a
620/// gross hack. We would like to simply mark these as being illegal, but when
621/// we do that, legalize produces these when it expands calls, then expands
622/// these in the same legalize pass. We would like dag combine to be able to
623/// hack on these between the call expansion and the node legalization. As such
624/// this pass basically does "really late" legalization of these inline with the
625/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000626void X86DAGToDAGISel::PreprocessForFPConvert() {
627 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
628 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000629 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
630 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
631 continue;
632
633 // If the source and destination are SSE registers, then this is a legal
634 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000635 MVT SrcVT = N->getOperand(0).getValueType();
636 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000637 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
638 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
639 if (SrcIsSSE && DstIsSSE)
640 continue;
641
Chris Lattner5d294e52008-03-09 07:05:32 +0000642 if (!SrcIsSSE && !DstIsSSE) {
643 // If this is an FPStack extension, it is a noop.
644 if (N->getOpcode() == ISD::FP_EXTEND)
645 continue;
646 // If this is a value-preserving FPStack truncation, it is a noop.
647 if (N->getConstantOperandVal(1))
648 continue;
649 }
650
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000651 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
652 // FPStack has extload and truncstore. SSE can fold direct loads into other
653 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000654 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000655 if (N->getOpcode() == ISD::FP_ROUND)
656 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
657 else
658 MemVT = SrcIsSSE ? SrcVT : DstVT;
659
Dan Gohman14a66442008-08-23 02:25:05 +0000660 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000661
662 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman14a66442008-08-23 02:25:05 +0000663 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
664 N->getOperand(0),
665 MemTmp, NULL, 0, MemVT);
666 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
667 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000668
669 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
670 // extload we created. This will cause general havok on the dag because
671 // anything below the conversion could be folded into other existing nodes.
672 // To avoid invalidating 'I', back it up to the convert node.
673 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000674 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000675
676 // Now that we did that, the node is dead. Increment the iterator to the
677 // next node to process, then delete N.
678 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000679 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000680 }
681}
682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
684/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000685void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000686 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000687 const Function *F = CurDAG->getMachineFunction().getFunction();
688 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
Evan Cheng34fd4f32008-06-30 20:45:06 +0000690 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000691 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000692 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000693
694 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000695 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696
697 // Codegen the basic block.
698#ifndef NDEBUG
699 DOUT << "===== Instruction selection begins:\n";
700 Indent = 0;
701#endif
David Greene932618b2008-10-27 21:56:29 +0000702 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703#ifndef NDEBUG
704 DOUT << "===== Instruction selection ends:\n";
705#endif
706
Dan Gohman14a66442008-08-23 02:25:05 +0000707 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000708}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000710/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
711/// the main function.
712void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
713 MachineFrameInfo *MFI) {
714 const TargetInstrInfo *TII = TM.getInstrInfo();
715 if (Subtarget->isTargetCygMing())
716 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
717}
718
719void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
720 // If this is main, emit special code for main.
721 MachineBasicBlock *BB = MF.begin();
722 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
723 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
724}
725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726/// MatchAddress - Add the specified node to the specified addressing mode,
727/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000728/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000729bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 bool isRoot, unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000731 bool is64Bit = Subtarget->is64Bit();
Evan Cheng7f250d62008-09-24 00:05:32 +0000732 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000733 // Limit recursion.
734 if (Depth > 5)
735 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736
737 // RIP relative addressing: %rip + 32-bit displacement!
738 if (AM.isRIPRel) {
739 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000740 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000741 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 AM.Disp += Val;
743 return false;
744 }
745 }
746 return true;
747 }
748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 switch (N.getOpcode()) {
750 default: break;
751 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000752 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000753 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 AM.Disp += Val;
755 return false;
756 }
757 break;
758 }
759
760 case X86ISD::Wrapper: {
Dan Gohman36322c72008-10-18 02:06:02 +0000761 DOUT << "Wrapper: 64bit " << is64Bit;
762 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000764 // Also, base and index reg must be 0 in order to use rip as base.
765 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000766 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 break;
768 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
769 break;
770 // If value is available in a register both base and index components have
771 // been picked, we can't fit the result available in the register in the
772 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohmancc3df852008-11-05 04:14:16 +0000773 {
Dan Gohman8181bd12008-07-27 21:46:04 +0000774 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000776 uint64_t Offset = G->getOffset();
777 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000778 GlobalValue *GV = G->getGlobal();
779 AM.GV = GV;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000780 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000781 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
782 return false;
783 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000785 uint64_t Offset = CP->getOffset();
786 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000787 AM.CP = CP->getConstVal();
788 AM.Align = CP->getAlignment();
Dan Gohman0bd76b72008-11-11 15:52:29 +0000789 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000790 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
791 return false;
792 }
Bill Wendlingfef06052008-09-16 21:48:12 +0000793 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000794 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000795 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000796 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000798 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000799 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000800 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 }
802 }
803 break;
804 }
805
806 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000807 if (AM.BaseType == X86ISelAddressMode::RegBase
808 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
810 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
811 return false;
812 }
813 break;
814
815 case ISD::SHL:
Dan Gohmancc3df852008-11-05 04:14:16 +0000816 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000817 break;
818
Gabor Greife9f7f582008-08-31 15:37:04 +0000819 if (ConstantSDNode
820 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000821 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000822 if (Val == 1 || Val == 2 || Val == 3) {
823 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000824 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825
Chris Lattner7f06edd2007-12-08 07:22:58 +0000826 // Okay, we know that we have a scale by now. However, if the scaled
827 // value is an add of something and a constant, we can fold the
828 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000829 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
830 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
831 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000832 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000833 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000834 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000835 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000836 AM.Disp = Disp;
837 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000839 } else {
840 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000842 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 }
844 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000845 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846
Dan Gohman35b99222007-10-22 20:22:24 +0000847 case ISD::SMUL_LOHI:
848 case ISD::UMUL_LOHI:
849 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000850 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000851 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 case ISD::MUL:
853 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000854 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000855 AM.Base.Reg.getNode() == 0 &&
856 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000857 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000858 if (ConstantSDNode
859 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000860 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
861 CN->getZExtValue() == 9) {
862 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863
Gabor Greif1c80d112008-08-28 21:40:38 +0000864 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000865 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866
867 // Okay, we know that we have a scale by now. However, if the scaled
868 // value is an add of something and a constant, we can fold the
869 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
871 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
872 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000874 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000875 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000876 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000877 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 AM.Disp = Disp;
879 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000880 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000882 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
884
885 AM.IndexReg = AM.Base.Reg = Reg;
886 return false;
887 }
888 }
889 break;
890
Evan Cheng2ed6f342009-01-17 07:09:27 +0000891 case ISD::ADD: {
892 X86ISelAddressMode Backup = AM;
893 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
894 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
895 return false;
896 AM = Backup;
897 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
898 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
899 return false;
900 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 break;
Evan Cheng2ed6f342009-01-17 07:09:27 +0000902 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903
904 case ISD::OR:
905 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000906 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
907 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000908 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000909 // Start with the LHS as an addr mode.
910 if (!MatchAddress(N.getOperand(0), AM, false) &&
911 // Address could not have picked a GV address for the displacement.
912 AM.GV == NULL &&
913 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman0bd76b72008-11-11 15:52:29 +0000914 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000915 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000916 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000917 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000918 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000920 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 }
922 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000923
924 case ISD::AND: {
925 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
926 // allows us to fold the shift into this addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000927 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000928 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohmancc3df852008-11-05 04:14:16 +0000929
Evan Chengf2abee72007-12-13 00:43:27 +0000930 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000931 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000932
933 // Not when RIP is used as the base.
934 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000935
936 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
937 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
938 if (!C1 || !C2) break;
939
940 // Not likely to be profitable if either the AND or SHIFT node has more
941 // than one use (unless all uses are for address computation). Besides,
942 // isel mechanism requires their node ids to be reused.
943 if (!N.hasOneUse() || !Shift.hasOneUse())
944 break;
945
946 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000947 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000948 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
949 break;
950
951 // Get the new AND mask, this folds to a constant.
Dan Gohmancc3df852008-11-05 04:14:16 +0000952 SDValue X = Shift.getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +0000954 SDValue(C2, 0), SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000955 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), X, NewANDMask);
Dan Gohman3666f472008-10-13 20:52:04 +0000956 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(),
957 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000958
959 // Insert the new nodes into the topological ordering.
960 if (C1->getNodeId() > X.getNode()->getNodeId()) {
961 CurDAG->RepositionNode(X.getNode(), C1);
962 C1->setNodeId(X.getNode()->getNodeId());
963 }
964 if (NewANDMask.getNode()->getNodeId() == -1 ||
965 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
966 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
967 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
968 }
969 if (NewAND.getNode()->getNodeId() == -1 ||
970 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
971 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
972 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
973 }
974 if (NewSHIFT.getNode()->getNodeId() == -1 ||
975 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
976 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
977 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
978 }
979
Dan Gohman3666f472008-10-13 20:52:04 +0000980 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +0000981
982 AM.Scale = 1 << ShiftCst;
983 AM.IndexReg = NewAND;
984 return false;
985 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 }
987
Dan Gohmana60c1b32007-08-13 20:03:06 +0000988 return MatchAddressBase(N, AM, isRoot, Depth);
989}
990
991/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
992/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000993bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000994 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +0000996 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +0000998 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 AM.IndexReg = N;
1000 AM.Scale = 1;
1001 return false;
1002 }
1003
1004 // Otherwise, we cannot select it.
1005 return true;
1006 }
1007
1008 // Default, generate it as a register.
1009 AM.BaseType = X86ISelAddressMode::RegBase;
1010 AM.Base.Reg = N;
1011 return false;
1012}
1013
1014/// SelectAddr - returns true if it is able pattern match an addressing mode.
1015/// It returns the operands which make up the maximal addressing mode it can
1016/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001017bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1018 SDValue &Scale, SDValue &Index,
1019 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 X86ISelAddressMode AM;
1021 if (MatchAddress(N, AM))
1022 return false;
1023
Duncan Sands92c43912008-06-06 12:08:01 +00001024 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001026 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 AM.Base.Reg = CurDAG->getRegister(0, VT);
1028 }
1029
Gabor Greif1c80d112008-08-28 21:40:38 +00001030 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 AM.IndexReg = CurDAG->getRegister(0, VT);
1032
1033 getAddressOperands(AM, Base, Scale, Index, Disp);
1034 return true;
1035}
1036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1038/// match a load whose top elements are either undef or zeros. The load flavor
1039/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001040bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1041 SDValue N, SDValue &Base,
1042 SDValue &Scale, SDValue &Index,
1043 SDValue &Disp, SDValue &InChain,
1044 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1046 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001047 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 InChain.getValue(0).hasOneUse() &&
1049 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001050 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1052 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1053 return false;
1054 OutChain = LD->getChain();
1055 return true;
1056 }
1057 }
1058
1059 // Also handle the case where we explicitly require zeros in the top
1060 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001061 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001062 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001063 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001064 N.getOperand(0).getNode()->hasOneUse() &&
1065 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001066 N.getOperand(0).getOperand(0).hasOneUse()) {
1067 // Okay, this is a zero extending load. Fold it.
1068 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1069 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1070 return false;
1071 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001072 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001073 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 }
1075 return false;
1076}
1077
1078
1079/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1080/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001081bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1082 SDValue &Base, SDValue &Scale,
1083 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 X86ISelAddressMode AM;
1085 if (MatchAddress(N, AM))
1086 return false;
1087
Duncan Sands92c43912008-06-06 12:08:01 +00001088 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 unsigned Complexity = 0;
1090 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001091 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 Complexity = 1;
1093 else
1094 AM.Base.Reg = CurDAG->getRegister(0, VT);
1095 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1096 Complexity = 4;
1097
Gabor Greif1c80d112008-08-28 21:40:38 +00001098 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 Complexity++;
1100 else
1101 AM.IndexReg = CurDAG->getRegister(0, VT);
1102
1103 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1104 // a simple shift.
1105 if (AM.Scale > 1)
1106 Complexity++;
1107
1108 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1109 // to a LEA. This is determined with some expermentation but is by no means
1110 // optimal (especially for code size consideration). LEA is nice because of
1111 // its three-address nature. Tweak the cost function again when we can run
1112 // convertToThreeAddress() at register allocation time.
1113 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1114 // For X86-64, we should always use lea to materialize RIP relative
1115 // addresses.
1116 if (Subtarget->is64Bit())
1117 Complexity = 4;
1118 else
1119 Complexity += 2;
1120 }
1121
Gabor Greif1c80d112008-08-28 21:40:38 +00001122 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 Complexity++;
1124
1125 if (Complexity > 2) {
1126 getAddressOperands(AM, Base, Scale, Index, Disp);
1127 return true;
1128 }
1129 return false;
1130}
1131
Dan Gohman8181bd12008-07-27 21:46:04 +00001132bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1133 SDValue &Base, SDValue &Scale,
1134 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001135 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001137 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1139 return false;
1140}
1141
Dan Gohmanb60482f2008-09-23 18:22:58 +00001142/// getGlobalBaseReg - Return an SDNode that returns the value of
1143/// the global base register. Output instructions required to
1144/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145///
1146SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001147 MachineFunction *MF = CurBB->getParent();
1148 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001149 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150}
1151
1152static SDNode *FindCallStartFromCall(SDNode *Node) {
1153 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1154 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1155 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001156 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157}
1158
Dan Gohmandd612bb2008-08-20 21:27:32 +00001159/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1160/// truncate of the specified operand to i8. This can be done with tablegen,
1161/// except that this code uses MVT::Flag in a tricky way that happens to
1162/// improve scheduling in some cases.
1163SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1164 assert(!Subtarget->is64Bit() &&
1165 "getTruncateTo8Bit is only needed on x86-32!");
1166 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1167
1168 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1169 unsigned Opc;
1170 MVT N0VT = N0.getValueType();
1171 switch (N0VT.getSimpleVT()) {
1172 default: assert(0 && "Unknown truncate!");
1173 case MVT::i16:
1174 Opc = X86::MOV16to16_;
1175 break;
1176 case MVT::i32:
1177 Opc = X86::MOV32to32_;
1178 break;
1179 }
1180
1181 // The use of MVT::Flag here is not strictly accurate, but it helps
1182 // scheduling in some cases.
1183 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1184 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1185 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001186}
1187
Dale Johannesenf160d802008-10-02 18:53:47 +00001188SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1189 SDValue Chain = Node->getOperand(0);
1190 SDValue In1 = Node->getOperand(1);
1191 SDValue In2L = Node->getOperand(2);
1192 SDValue In2H = Node->getOperand(3);
1193 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1194 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1195 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001196 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001197 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1198 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1199}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001200
Dan Gohman8181bd12008-07-27 21:46:04 +00001201SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001202 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001203 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 unsigned Opc, MOpc;
1205 unsigned Opcode = Node->getOpcode();
1206
1207#ifndef NDEBUG
1208 DOUT << std::string(Indent, ' ') << "Selecting: ";
1209 DEBUG(Node->dump(CurDAG));
1210 DOUT << "\n";
1211 Indent += 2;
1212#endif
1213
Dan Gohmanbd68c792008-07-17 19:10:17 +00001214 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215#ifndef NDEBUG
1216 DOUT << std::string(Indent-2, ' ') << "== ";
1217 DEBUG(Node->dump(CurDAG));
1218 DOUT << "\n";
1219 Indent -= 2;
1220#endif
1221 return NULL; // Already selected.
1222 }
1223
1224 switch (Opcode) {
1225 default: break;
1226 case X86ISD::GlobalBaseReg:
1227 return getGlobalBaseReg();
1228
Dale Johannesenf160d802008-10-02 18:53:47 +00001229 case X86ISD::ATOMOR64_DAG:
1230 return SelectAtomic64(Node, X86::ATOMOR6432);
1231 case X86ISD::ATOMXOR64_DAG:
1232 return SelectAtomic64(Node, X86::ATOMXOR6432);
1233 case X86ISD::ATOMADD64_DAG:
1234 return SelectAtomic64(Node, X86::ATOMADD6432);
1235 case X86ISD::ATOMSUB64_DAG:
1236 return SelectAtomic64(Node, X86::ATOMSUB6432);
1237 case X86ISD::ATOMNAND64_DAG:
1238 return SelectAtomic64(Node, X86::ATOMNAND6432);
1239 case X86ISD::ATOMAND64_DAG:
1240 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001241 case X86ISD::ATOMSWAP64_DAG:
1242 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001243
Dan Gohman5a199552007-10-08 18:33:35 +00001244 case ISD::SMUL_LOHI:
1245 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001246 SDValue N0 = Node->getOperand(0);
1247 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001248
Dan Gohman5a199552007-10-08 18:33:35 +00001249 bool isSigned = Opcode == ISD::SMUL_LOHI;
1250 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001251 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 default: assert(0 && "Unsupported VT!");
1253 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1254 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1255 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1256 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1257 }
1258 else
Duncan Sands92c43912008-06-06 12:08:01 +00001259 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260 default: assert(0 && "Unsupported VT!");
1261 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1262 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1263 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1264 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1265 }
1266
1267 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001268 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 default: assert(0 && "Unsupported VT!");
1270 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1271 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1272 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1273 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1274 }
1275
Dan Gohman8181bd12008-07-27 21:46:04 +00001276 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001277 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001278 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 if (!foldedLoad) {
1280 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001281 if (foldedLoad)
1282 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 }
1284
Dan Gohman8181bd12008-07-27 21:46:04 +00001285 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1286 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287
1288 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 SDNode *CNode =
1291 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001292 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001293 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001294 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001297 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 }
1299
Dan Gohman5a199552007-10-08 18:33:35 +00001300 // Copy the low half of the result, if it is needed.
1301 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001302 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001303 LoReg, NVT, InFlag);
1304 InFlag = Result.getValue(2);
1305 ReplaceUses(N.getValue(0), Result);
1306#ifndef NDEBUG
1307 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001308 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001309 DOUT << "\n";
1310#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001311 }
Dan Gohman5a199552007-10-08 18:33:35 +00001312 // Copy the high half of the result, if it is needed.
1313 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001314 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001315 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1316 // Prevent use of AH in a REX instruction by referencing AX instead.
1317 // Shift it down 8 bits.
1318 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1319 X86::AX, MVT::i16, InFlag);
1320 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001321 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001322 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001323 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001324 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1325 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001326 MVT::i8, Result, SRIdx), 0);
1327 } else {
1328 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1329 HiReg, NVT, InFlag);
1330 InFlag = Result.getValue(2);
1331 }
1332 ReplaceUses(N.getValue(1), Result);
1333#ifndef NDEBUG
1334 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001335 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001336 DOUT << "\n";
1337#endif
1338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339
1340#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 Indent -= 2;
1342#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 return NULL;
1345 }
1346
Dan Gohman5a199552007-10-08 18:33:35 +00001347 case ISD::SDIVREM:
1348 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001349 SDValue N0 = Node->getOperand(0);
1350 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001351
1352 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001354 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 default: assert(0 && "Unsupported VT!");
1356 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1357 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1358 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1359 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1360 }
1361 else
Duncan Sands92c43912008-06-06 12:08:01 +00001362 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 default: assert(0 && "Unsupported VT!");
1364 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1365 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1366 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1367 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1368 }
1369
1370 unsigned LoReg, HiReg;
1371 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001372 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 default: assert(0 && "Unsupported VT!");
1374 case MVT::i8:
1375 LoReg = X86::AL; HiReg = X86::AH;
1376 ClrOpcode = 0;
1377 SExtOpcode = X86::CBW;
1378 break;
1379 case MVT::i16:
1380 LoReg = X86::AX; HiReg = X86::DX;
1381 ClrOpcode = X86::MOV16r0;
1382 SExtOpcode = X86::CWD;
1383 break;
1384 case MVT::i32:
1385 LoReg = X86::EAX; HiReg = X86::EDX;
1386 ClrOpcode = X86::MOV32r0;
1387 SExtOpcode = X86::CDQ;
1388 break;
1389 case MVT::i64:
1390 LoReg = X86::RAX; HiReg = X86::RDX;
1391 ClrOpcode = X86::MOV64r0;
1392 SExtOpcode = X86::CQO;
1393 break;
1394 }
1395
Dan Gohman8181bd12008-07-27 21:46:04 +00001396 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001397 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001398 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001399
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 SDValue InFlag;
Dan Gohman7bbd9202009-01-21 14:50:16 +00001401 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402 // Special case for div8, just use a move with zero extension to AX to
1403 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001404 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001406 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001408 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 Ops, 5), 0);
1410 Chain = Move.getValue(1);
1411 ReplaceUses(N0.getValue(1), Chain);
1412 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 Chain = CurDAG->getEntryNode();
1416 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001417 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418 InFlag = Chain.getValue(1);
1419 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001421 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 LoReg, N0, SDValue()).getValue(1);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001423 if (isSigned && !signBitIsZero) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 // Sign extend the low part into the high part.
1425 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 } else {
1428 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001429 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001430 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1431 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 }
1433 }
1434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 SDNode *CNode =
1438 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001439 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001440 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001441 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001444 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 }
1446
Dan Gohman242a5ba2007-09-25 18:23:27 +00001447 // Copy the division (low) result, if it is needed.
1448 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001449 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001450 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001451 InFlag = Result.getValue(2);
1452 ReplaceUses(N.getValue(0), Result);
1453#ifndef NDEBUG
1454 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001455 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001456 DOUT << "\n";
1457#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001458 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001459 // Copy the remainder (high) result, if it is needed.
1460 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001461 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001462 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1463 // Prevent use of AH in a REX instruction by referencing AX instead.
1464 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001465 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1466 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001467 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001468 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001469 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001470 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001471 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1472 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001473 MVT::i8, Result, SRIdx), 0);
1474 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001475 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1476 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001477 InFlag = Result.getValue(2);
1478 }
1479 ReplaceUses(N.getValue(1), Result);
1480#ifndef NDEBUG
1481 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001482 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001483 DOUT << "\n";
1484#endif
1485 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486
1487#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 Indent -= 2;
1489#endif
1490
1491 return NULL;
1492 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001493
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001494 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001495 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001496 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1497 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001498
Dan Gohmandd612bb2008-08-20 21:27:32 +00001499 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1500 unsigned Opc = 0;
1501 switch (NVT.getSimpleVT()) {
1502 default: assert(0 && "Unknown sign_extend_inreg!");
1503 case MVT::i16:
1504 Opc = X86::MOVSX16rr8;
1505 break;
1506 case MVT::i32:
1507 Opc = X86::MOVSX32rr8;
1508 break;
1509 }
1510
1511 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001512
1513#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001514 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001515 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001516 DOUT << "\n";
1517 DOUT << std::string(Indent-2, ' ') << "=> ";
1518 DEBUG(ResNode->dump(CurDAG));
1519 DOUT << "\n";
1520 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001521#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001522 return ResNode;
1523 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001524 break;
1525 }
1526
1527 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001528 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1529 SDValue Input = Node->getOperand(0);
Dan Gohmandd612bb2008-08-20 21:27:32 +00001530 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532#ifndef NDEBUG
1533 DOUT << std::string(Indent-2, ' ') << "=> ";
1534 DEBUG(ResNode->dump(CurDAG));
1535 DOUT << "\n";
1536 Indent -= 2;
1537#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001538 return ResNode;
1539 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 break;
1541 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001542
1543 case ISD::DECLARE: {
1544 // Handle DECLARE nodes here because the second operand may have been
1545 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001546 SDValue Chain = Node->getOperand(0);
1547 SDValue N1 = Node->getOperand(1);
1548 SDValue N2 = Node->getOperand(2);
Evan Cheng417bc002008-12-10 21:49:05 +00001549 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
1550 if (!FINode)
Evan Cheng651e1442008-06-18 02:48:27 +00001551 break;
Evan Cheng651e1442008-06-18 02:48:27 +00001552 if (N2.getOpcode() == ISD::ADD &&
1553 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1554 N2 = N2.getOperand(1);
Evan Cheng417bc002008-12-10 21:49:05 +00001555 if (N2.getOpcode() != X86ISD::Wrapper)
1556 break;
Evan Chengf3ecd1a2009-01-10 03:33:22 +00001557 GlobalAddressSDNode *GVNode =
1558 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Evan Cheng417bc002008-12-10 21:49:05 +00001559 if (!GVNode)
1560 break;
1561 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1562 TLI.getPointerTy());
1563 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1564 TLI.getPointerTy());
1565 SDValue Ops[] = { Tmp1, Tmp2, Chain };
1566 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1567 MVT::Other, Ops, 3);
Evan Chengd4cebcd2008-06-17 02:01:22 +00001568 break;
1569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 }
1571
1572 SDNode *ResNode = SelectCode(N);
1573
1574#ifndef NDEBUG
1575 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001576 if (ResNode == NULL || ResNode == N.getNode())
1577 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578 else
1579 DEBUG(ResNode->dump(CurDAG));
1580 DOUT << "\n";
1581 Indent -= 2;
1582#endif
1583
1584 return ResNode;
1585}
1586
1587bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001588SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001589 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001590 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 switch (ConstraintCode) {
1592 case 'o': // offsetable ??
1593 case 'v': // not offsetable ??
1594 default: return true;
1595 case 'm': // memory
1596 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1597 return true;
1598 break;
1599 }
1600
1601 OutOps.push_back(Op0);
1602 OutOps.push_back(Op1);
1603 OutOps.push_back(Op2);
1604 OutOps.push_back(Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 return false;
1606}
1607
1608/// createX86ISelDag - This pass converts a legalized DAG into a
1609/// X86-specific DAG, ready for instruction scheduling.
1610///
1611FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1612 return new X86DAGToDAGISel(TM, Fast);
1613}