blob: 4bc604b6e46ae4b2d02023c956158671a2fcbe3b [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
Evan Cheng8655a532009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
54 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
60 FrameIndexBase
61 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000064 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 int FrameIndex;
66 } Base;
67
Evan Cheng3b5a1272008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000071 int32_t Disp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 GlobalValue *GV;
73 Constant *CP;
74 const char *ES;
75 int JT;
76 unsigned Align; // CP alignment.
77
78 X86ISelAddressMode()
79 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
80 GV(0), CP(0), ES(0), JT(-1), Align(0) {
81 }
Dan Gohman245791b2009-02-07 00:43:41 +000082
83 bool hasSymbolicDisplacement() const {
84 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
85 }
86
Dale Johannesenc501c082008-08-11 23:46:25 +000087 void dump() {
88 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000089 cerr << "Base.Reg ";
90 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
91 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000092 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
93 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000094 cerr << "IndexReg ";
95 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
96 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000097 cerr << " Disp " << Disp << "\n";
98 cerr << "GV "; if (GV) GV->dump();
99 else cerr << "nul";
100 cerr << " CP "; if (CP) CP->dump();
101 else cerr << "nul";
102 cerr << "\n";
103 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
104 cerr << " JT" << JT << " Align" << Align << "\n";
105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 };
107}
108
109namespace {
110 //===--------------------------------------------------------------------===//
111 /// ISel - X86 specific code to select X86 machine instructions for
112 /// SelectionDAG operations.
113 ///
114 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 /// TM - Keep a reference to X86TargetMachine.
116 ///
117 X86TargetMachine &TM;
118
119 /// X86Lowering - This object fully describes how to lower LLVM code to an
120 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000121 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
124 /// make the right decision when generating code for different targets.
125 const X86Subtarget *Subtarget;
126
Evan Cheng34fd4f32008-06-30 20:45:06 +0000127 /// CurBB - Current BB being isel'd.
128 ///
129 MachineBasicBlock *CurBB;
130
Evan Cheng13559d62008-09-26 23:41:32 +0000131 /// OptForSize - If true, selector should try to optimize for code size
132 /// instead of performance.
133 bool OptForSize;
134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 public:
136 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000137 : SelectionDAGISel(tm, fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000138 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000139 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000140 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 virtual const char *getPassName() const {
143 return "X86 DAG->DAG Instruction Selection";
144 }
145
Evan Cheng34fd4f32008-06-30 20:45:06 +0000146 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000148 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000149
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000150 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
151
Evan Cheng5a424552008-11-27 00:49:46 +0000152 virtual
153 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
155// Include the pieces autogenerated from the target description.
156#include "X86GenDAGISel.inc"
157
158 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000160 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000165 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000166 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
167 SDValue &Scale, SDValue &Index, SDValue &Disp);
168 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
169 SDValue &Scale, SDValue &Index, SDValue &Disp);
170 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
171 SDValue N, SDValue &Base, SDValue &Scale,
172 SDValue &Index, SDValue &Disp,
173 SDValue &InChain, SDValue &OutChain);
174 bool TryFoldLoad(SDValue P, SDValue N,
175 SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000177 void PreprocessForRMW();
178 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
180 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
181 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000182 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000184 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000186 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
187
Dan Gohman8181bd12008-07-27 21:46:04 +0000188 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
189 SDValue &Scale, SDValue &Index,
190 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
192 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
193 AM.Base.Reg;
194 Scale = getI8Imm(AM.Scale);
195 Index = AM.IndexReg;
196 // These are 32-bit even in 64-bit mode since RIP relative offset
197 // is 32-bit.
198 if (AM.GV)
199 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
200 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000201 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
202 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000204 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 else if (AM.JT != -1)
206 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
207 else
Dan Gohman0bd76b72008-11-11 15:52:29 +0000208 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 }
210
211 /// getI8Imm - Return a target constant with the specified value, of type
212 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000213 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 return CurDAG->getTargetConstant(Imm, MVT::i8);
215 }
216
217 /// getI16Imm - Return a target constant with the specified value, of type
218 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000219 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 return CurDAG->getTargetConstant(Imm, MVT::i16);
221 }
222
223 /// getI32Imm - Return a target constant with the specified value, of type
224 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000225 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return CurDAG->getTargetConstant(Imm, MVT::i32);
227 }
228
Dan Gohmanb60482f2008-09-23 18:22:58 +0000229 /// getGlobalBaseReg - Return an SDNode that returns the value of
230 /// the global base register. Output instructions required to
231 /// initialize the global base register, if necessary.
232 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 SDNode *getGlobalBaseReg();
234
Dan Gohmandd612bb2008-08-20 21:27:32 +0000235 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
236 /// truncate of the specified operand to i8. This can be done with tablegen,
237 /// except that this code uses MVT::Flag in a tricky way that happens to
238 /// improve scheduling in some cases.
239 SDNode *getTruncateTo8Bit(SDValue N0);
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
Gabor Greife9f7f582008-08-31 15:37:04 +0000247/// findFlagUse - Return use of MVT::Flag value produced by the specified
248/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000249///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250static SDNode *findFlagUse(SDNode *N) {
251 unsigned FlagResNo = N->getNumValues()-1;
252 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman13f24a72009-01-27 02:37:43 +0000253 SDUse &Use = I.getUse();
254 if (Use.getResNo() == FlagResNo)
255 return Use.getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257 return NULL;
258}
259
djg4b210952009-01-27 19:04:30 +0000260/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
261/// This function recursively traverses up the operand chain, ignoring
262/// certain nodes.
263static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
264 SDNode *Root,
Evan Cheng656269e2008-04-25 08:22:20 +0000265 SmallPtrSet<SDNode*, 16> &Visited) {
djg4b210952009-01-27 19:04:30 +0000266 if (Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000267 !Visited.insert(Use))
djg4b210952009-01-27 19:04:30 +0000268 return false;
269
270 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000271 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000273 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000274 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000275 assert(N != Root);
djg4b210952009-01-27 19:04:30 +0000276 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
Evan Cheng656269e2008-04-25 08:22:20 +0000278
279 // Traverse up the operand chain.
djg4b210952009-01-27 19:04:30 +0000280 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
281 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 }
djg4b210952009-01-27 19:04:30 +0000283 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284}
285
286/// isNonImmUse - Start searching from Root up the DAG to check is Def can
287/// be reached. Return true if that's the case. However, ignore direct uses
288/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng5a424552008-11-27 00:49:46 +0000289/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
290/// case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291/// FIXME: to be really generic, we should allow direct use by any node
292/// that is being folded. But realisticly since we only fold loads which
293/// have one non-chain use, we only need to watch out for load/op/store
294/// and load/op/cmp case where the root (store / cmp) may reach the load via
295/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000296static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000297 SmallPtrSet<SDNode*, 16> Visited;
djg4b210952009-01-27 19:04:30 +0000298 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299}
300
301
Evan Cheng5a424552008-11-27 00:49:46 +0000302bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
303 SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000304 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305
Evan Cheng5a424552008-11-27 00:49:46 +0000306 if (U == Root)
307 switch (U->getOpcode()) {
308 default: break;
309 case ISD::ADD:
310 case ISD::ADDC:
311 case ISD::ADDE:
312 case ISD::AND:
313 case ISD::OR:
314 case ISD::XOR: {
315 // If the other operand is a 8-bit immediate we should fold the immediate
316 // instead. This reduces code size.
317 // e.g.
318 // movl 4(%esp), %eax
319 // addl $4, %eax
320 // vs.
321 // movl $4, %eax
322 // addl 4(%esp), %eax
323 // The former is 2 bytes shorter. In case where the increment is 1, then
324 // the saving can be 4 bytes (by using incl %eax).
Dan Gohman01126892009-03-14 02:07:16 +0000325 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1)))
326 if (Imm->getAPIntValue().isSignedIntN(8))
327 return false;
Evan Cheng5a424552008-11-27 00:49:46 +0000328 }
329 }
330
Dan Gohman602d44a2008-09-17 01:39:10 +0000331 // If Root use can somehow reach N through a path that that doesn't contain
332 // U then folding N would create a cycle. e.g. In the following
333 // diagram, Root can reach N through X. If N is folded into into Root, then
334 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000336 // [N*] //
337 // ^ ^ //
338 // / \ //
339 // [U*] [X]? //
340 // ^ ^ //
341 // \ / //
342 // \ / //
343 // [Root*] //
344 //
345 // * indicates nodes to be folded together.
346 //
347 // If Root produces a flag, then it gets (even more) interesting. Since it
348 // will be "glued" together with its flag use in the scheduler, we need to
349 // check if it might reach N.
350 //
351 // [N*] //
352 // ^ ^ //
353 // / \ //
354 // [U*] [X]? //
355 // ^ ^ //
356 // \ \ //
357 // \ | //
358 // [Root*] | //
359 // ^ | //
360 // f | //
361 // | / //
362 // [Y] / //
363 // ^ / //
364 // f / //
365 // | / //
366 // [FU] //
367 //
368 // If FU (flag use) indirectly reaches N (the load), and Root folds N
369 // (call it Fold), then X is a predecessor of FU and a successor of
370 // Fold. But since Fold and FU are flagged together, this will create
371 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372
Duncan Sands92c43912008-06-06 12:08:01 +0000373 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000374 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 SDNode *FU = findFlagUse(Root);
376 if (FU == NULL)
377 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000378 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 VT = Root->getValueType(Root->getNumValues()-1);
380 }
381
Dan Gohman602d44a2008-09-17 01:39:10 +0000382 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383}
384
385/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
386/// and move load below the TokenFactor. Replace store's chain operand with
387/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000388static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000389 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000390 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000391 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
392 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000393 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000395 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000396 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
397 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
398 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
399 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400}
401
Evan Cheng2b2a7012008-05-23 21:23:16 +0000402/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
403///
Dan Gohman8181bd12008-07-27 21:46:04 +0000404static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
405 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000406 if (N.getOpcode() == ISD::BIT_CONVERT)
407 N = N.getOperand(0);
408
409 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
410 if (!LD || LD->isVolatile())
411 return false;
412 if (LD->getAddressingMode() != ISD::UNINDEXED)
413 return false;
414
415 ISD::LoadExtType ExtType = LD->getExtensionType();
416 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
417 return false;
418
419 if (N.hasOneUse() &&
420 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000421 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000422 Load = N;
423 return true;
424 }
425 return false;
426}
427
Evan Cheng98cfaf82008-08-25 21:27:18 +0000428/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
429/// operand and move load below the call's chain operand.
430static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
evanchengcd6d72b2009-01-26 18:43:34 +0000431 SDValue Call, SDValue CallSeqStart) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000432 SmallVector<SDValue, 8> Ops;
evanchengcd6d72b2009-01-26 18:43:34 +0000433 SDValue Chain = CallSeqStart.getOperand(0);
434 if (Chain.getNode() == Load.getNode())
435 Ops.push_back(Load.getOperand(0));
436 else {
437 assert(Chain.getOpcode() == ISD::TokenFactor &&
438 "Unexpected CallSeqStart chain operand");
439 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
440 if (Chain.getOperand(i).getNode() == Load.getNode())
441 Ops.push_back(Load.getOperand(0));
442 else
443 Ops.push_back(Chain.getOperand(i));
444 SDValue NewChain =
Dale Johannesen913ba762009-02-06 01:31:28 +0000445 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
446 MVT::Other, &Ops[0], Ops.size());
evanchengcd6d72b2009-01-26 18:43:34 +0000447 Ops.clear();
448 Ops.push_back(NewChain);
449 }
450 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
451 Ops.push_back(CallSeqStart.getOperand(i));
452 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000453 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
454 Load.getOperand(1), Load.getOperand(2));
455 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000456 Ops.push_back(SDValue(Load.getNode(), 1));
457 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000458 Ops.push_back(Call.getOperand(i));
459 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
460}
461
462/// isCalleeLoad - Return true if call address is a load and it can be
463/// moved below CALLSEQ_START and the chains leading up to the call.
464/// Return the CALLSEQ_START by reference as a second output.
465static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000466 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000467 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000468 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000469 if (!LD ||
470 LD->isVolatile() ||
471 LD->getAddressingMode() != ISD::UNINDEXED ||
472 LD->getExtensionType() != ISD::NON_EXTLOAD)
473 return false;
474
475 // Now let's find the callseq_start.
476 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
477 if (!Chain.hasOneUse())
478 return false;
479 Chain = Chain.getOperand(0);
480 }
evanchengcd6d72b2009-01-26 18:43:34 +0000481
482 if (Chain.getOperand(0).getNode() == Callee.getNode())
483 return true;
484 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
485 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
486 return true;
487 return false;
Evan Cheng98cfaf82008-08-25 21:27:18 +0000488}
489
490
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000491/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
492/// This is only run if not in -fast mode (aka -O0).
493/// This allows the instruction selector to pick more read-modify-write
494/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495///
496/// [Load chain]
497/// ^
498/// |
499/// [Load]
500/// ^ ^
501/// | |
502/// / \-
503/// / |
504/// [TokenFactor] [Op]
505/// ^ ^
506/// | |
507/// \ /
508/// \ /
509/// [Store]
510///
511/// The fact the store's chain operand != load's chain will prevent the
512/// (store (op (load))) instruction from being selected. We can transform it to:
513///
514/// [Load chain]
515/// ^
516/// |
517/// [TokenFactor]
518/// ^
519/// |
520/// [Load]
521/// ^ ^
522/// | |
523/// | \-
524/// | |
525/// | [Op]
526/// | ^
527/// | |
528/// \ /
529/// \ /
530/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000531void X86DAGToDAGISel::PreprocessForRMW() {
532 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
533 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000534 if (I->getOpcode() == X86ISD::CALL) {
535 /// Also try moving call address load from outside callseq_start to just
536 /// before the call to allow it to be folded.
537 ///
538 /// [Load chain]
539 /// ^
540 /// |
541 /// [Load]
542 /// ^ ^
543 /// | |
544 /// / \--
545 /// / |
546 ///[CALLSEQ_START] |
547 /// ^ |
548 /// | |
549 /// [LOAD/C2Reg] |
550 /// | |
551 /// \ /
552 /// \ /
553 /// [CALL]
554 SDValue Chain = I->getOperand(0);
555 SDValue Load = I->getOperand(1);
556 if (!isCalleeLoad(Load, Chain))
557 continue;
558 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
559 ++NumLoadMoved;
560 continue;
561 }
562
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 if (!ISD::isNON_TRUNCStore(I))
564 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000565 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000566
Gabor Greif1c80d112008-08-28 21:40:38 +0000567 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 continue;
569
Dan Gohman8181bd12008-07-27 21:46:04 +0000570 SDValue N1 = I->getOperand(1);
571 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000572 if ((N1.getValueType().isFloatingPoint() &&
573 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 !N1.hasOneUse())
575 continue;
576
577 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000578 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000579 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000581 case ISD::ADD:
582 case ISD::MUL:
583 case ISD::AND:
584 case ISD::OR:
585 case ISD::XOR:
586 case ISD::ADDC:
587 case ISD::ADDE:
588 case ISD::VECTOR_SHUFFLE: {
589 SDValue N10 = N1.getOperand(0);
590 SDValue N11 = N1.getOperand(1);
591 RModW = isRMWLoad(N10, Chain, N2, Load);
592 if (!RModW)
593 RModW = isRMWLoad(N11, Chain, N2, Load);
594 break;
595 }
596 case ISD::SUB:
597 case ISD::SHL:
598 case ISD::SRA:
599 case ISD::SRL:
600 case ISD::ROTL:
601 case ISD::ROTR:
602 case ISD::SUBC:
603 case ISD::SUBE:
604 case X86ISD::SHLD:
605 case X86ISD::SHRD: {
606 SDValue N10 = N1.getOperand(0);
607 RModW = isRMWLoad(N10, Chain, N2, Load);
608 break;
609 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
611
612 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000613 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 ++NumLoadMoved;
615 }
616 }
617}
618
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000619
620/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
621/// nodes that target the FP stack to be store and load to the stack. This is a
622/// gross hack. We would like to simply mark these as being illegal, but when
623/// we do that, legalize produces these when it expands calls, then expands
624/// these in the same legalize pass. We would like dag combine to be able to
625/// hack on these between the call expansion and the node legalization. As such
626/// this pass basically does "really late" legalization of these inline with the
627/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000628void X86DAGToDAGISel::PreprocessForFPConvert() {
629 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
630 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000631 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
632 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
633 continue;
634
635 // If the source and destination are SSE registers, then this is a legal
636 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000637 MVT SrcVT = N->getOperand(0).getValueType();
638 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000639 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
640 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
641 if (SrcIsSSE && DstIsSSE)
642 continue;
643
Chris Lattner5d294e52008-03-09 07:05:32 +0000644 if (!SrcIsSSE && !DstIsSSE) {
645 // If this is an FPStack extension, it is a noop.
646 if (N->getOpcode() == ISD::FP_EXTEND)
647 continue;
648 // If this is a value-preserving FPStack truncation, it is a noop.
649 if (N->getConstantOperandVal(1))
650 continue;
651 }
652
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000653 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
654 // FPStack has extload and truncstore. SSE can fold direct loads into other
655 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000656 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000657 if (N->getOpcode() == ISD::FP_ROUND)
658 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
659 else
660 MemVT = SrcIsSSE ? SrcVT : DstVT;
661
Dan Gohman14a66442008-08-23 02:25:05 +0000662 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000663 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000664
665 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000666 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohman14a66442008-08-23 02:25:05 +0000667 N->getOperand(0),
668 MemTmp, NULL, 0, MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000669 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohman14a66442008-08-23 02:25:05 +0000670 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000671
672 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
673 // extload we created. This will cause general havok on the dag because
674 // anything below the conversion could be folded into other existing nodes.
675 // To avoid invalidating 'I', back it up to the convert node.
676 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000677 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000678
679 // Now that we did that, the node is dead. Increment the iterator to the
680 // next node to process, then delete N.
681 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000682 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000683 }
684}
685
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
687/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000688void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000689 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000690 const Function *F = CurDAG->getMachineFunction().getFunction();
691 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692
Evan Cheng34fd4f32008-06-30 20:45:06 +0000693 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000694 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000695 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000696
697 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000698 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699
700 // Codegen the basic block.
701#ifndef NDEBUG
702 DOUT << "===== Instruction selection begins:\n";
703 Indent = 0;
704#endif
David Greene932618b2008-10-27 21:56:29 +0000705 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706#ifndef NDEBUG
707 DOUT << "===== Instruction selection ends:\n";
708#endif
709
Dan Gohman14a66442008-08-23 02:25:05 +0000710 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000711}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000713/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
714/// the main function.
715void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
716 MachineFrameInfo *MFI) {
717 const TargetInstrInfo *TII = TM.getInstrInfo();
718 if (Subtarget->isTargetCygMing())
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000719 BuildMI(BB, DebugLoc::getUnknownLoc(),
720 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000721}
722
723void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
724 // If this is main, emit special code for main.
725 MachineBasicBlock *BB = MF.begin();
726 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
727 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
728}
729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730/// MatchAddress - Add the specified node to the specified addressing mode,
731/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000732/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000733bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 bool isRoot, unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000735 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000736 DebugLoc dl = N.getDebugLoc();
Evan Cheng7f250d62008-09-24 00:05:32 +0000737 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000738 // Limit recursion.
739 if (Depth > 5)
740 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741
742 // RIP relative addressing: %rip + 32-bit displacement!
743 if (AM.isRIPRel) {
744 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000745 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000746 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 AM.Disp += Val;
748 return false;
749 }
750 }
751 return true;
752 }
753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 switch (N.getOpcode()) {
755 default: break;
756 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000757 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000758 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 AM.Disp += Val;
760 return false;
761 }
762 break;
763 }
764
765 case X86ISD::Wrapper: {
Dan Gohman36322c72008-10-18 02:06:02 +0000766 DOUT << "Wrapper: 64bit " << is64Bit;
767 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000769 // Also, base and index reg must be 0 in order to use rip as base.
770 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000771 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 break;
Dan Gohman245791b2009-02-07 00:43:41 +0000773 if (AM.hasSymbolicDisplacement())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 break;
775 // If value is available in a register both base and index components have
776 // been picked, we can't fit the result available in the register in the
777 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohmancc3df852008-11-05 04:14:16 +0000778 {
Dan Gohman8181bd12008-07-27 21:46:04 +0000779 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000781 uint64_t Offset = G->getOffset();
782 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000783 GlobalValue *GV = G->getGlobal();
784 AM.GV = GV;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000785 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000786 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
787 return false;
788 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000790 uint64_t Offset = CP->getOffset();
791 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000792 AM.CP = CP->getConstVal();
793 AM.Align = CP->getAlignment();
Dan Gohman0bd76b72008-11-11 15:52:29 +0000794 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000795 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
796 return false;
797 }
Bill Wendlingfef06052008-09-16 21:48:12 +0000798 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000799 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000800 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000801 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000803 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000804 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000805 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 }
807 }
808 break;
809 }
810
811 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000812 if (AM.BaseType == X86ISelAddressMode::RegBase
813 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
815 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
816 return false;
817 }
818 break;
819
820 case ISD::SHL:
Dan Gohmancc3df852008-11-05 04:14:16 +0000821 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000822 break;
823
Gabor Greife9f7f582008-08-31 15:37:04 +0000824 if (ConstantSDNode
825 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000826 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000827 if (Val == 1 || Val == 2 || Val == 3) {
828 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000829 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830
Chris Lattner7f06edd2007-12-08 07:22:58 +0000831 // Okay, we know that we have a scale by now. However, if the scaled
832 // value is an add of something and a constant, we can fold the
833 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000834 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
835 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
836 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000837 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000838 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000839 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000840 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000841 AM.Disp = Disp;
842 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000844 } else {
845 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000847 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 }
849 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000850 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851
Dan Gohman35b99222007-10-22 20:22:24 +0000852 case ISD::SMUL_LOHI:
853 case ISD::UMUL_LOHI:
854 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000855 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000856 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 case ISD::MUL:
Evan Chengc3495762009-03-30 21:36:47 +0000858 case X86ISD::MUL_IMM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000860 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000861 AM.Base.Reg.getNode() == 0 &&
862 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000863 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000864 if (ConstantSDNode
865 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000866 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
867 CN->getZExtValue() == 9) {
868 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000871 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872
873 // Okay, we know that we have a scale by now. However, if the scaled
874 // value is an add of something and a constant, we can fold the
875 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000876 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
877 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
878 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000880 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000881 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000882 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000883 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 AM.Disp = Disp;
885 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000886 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000888 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 }
890
891 AM.IndexReg = AM.Base.Reg = Reg;
892 return false;
893 }
894 }
895 break;
896
Evan Cheng2ed6f342009-01-17 07:09:27 +0000897 case ISD::ADD: {
898 X86ISelAddressMode Backup = AM;
899 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
900 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
901 return false;
902 AM = Backup;
903 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
904 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
905 return false;
906 AM = Backup;
Dan Gohman3ae92482009-03-13 02:25:09 +0000907
908 // If we couldn't fold both operands into the address at the same time,
909 // see if we can just put each operand into a register and fold at least
910 // the add.
911 if (AM.BaseType == X86ISelAddressMode::RegBase &&
912 !AM.Base.Reg.getNode() &&
913 !AM.IndexReg.getNode() &&
914 !AM.isRIPRel) {
915 AM.Base.Reg = N.getNode()->getOperand(0);
916 AM.IndexReg = N.getNode()->getOperand(1);
917 AM.Scale = 1;
918 return false;
919 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 break;
Evan Cheng2ed6f342009-01-17 07:09:27 +0000921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922
923 case ISD::OR:
924 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000925 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
926 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000927 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000928 // Start with the LHS as an addr mode.
929 if (!MatchAddress(N.getOperand(0), AM, false) &&
930 // Address could not have picked a GV address for the displacement.
931 AM.GV == NULL &&
932 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman0bd76b72008-11-11 15:52:29 +0000933 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000934 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000935 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000936 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000937 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000939 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 }
941 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000942
943 case ISD::AND: {
944 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
945 // allows us to fold the shift into this addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000946 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000947 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohmancc3df852008-11-05 04:14:16 +0000948
Evan Chengf2abee72007-12-13 00:43:27 +0000949 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000950 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000951
952 // Not when RIP is used as the base.
953 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000954
955 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
956 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
957 if (!C1 || !C2) break;
958
959 // Not likely to be profitable if either the AND or SHIFT node has more
960 // than one use (unless all uses are for address computation). Besides,
961 // isel mechanism requires their node ids to be reused.
962 if (!N.hasOneUse() || !Shift.hasOneUse())
963 break;
964
965 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000966 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000967 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
968 break;
969
970 // Get the new AND mask, this folds to a constant.
Dan Gohmancc3df852008-11-05 04:14:16 +0000971 SDValue X = Shift.getOperand(0);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000972 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +0000973 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000974 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
975 NewANDMask);
976 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman3666f472008-10-13 20:52:04 +0000977 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000978
979 // Insert the new nodes into the topological ordering.
980 if (C1->getNodeId() > X.getNode()->getNodeId()) {
981 CurDAG->RepositionNode(X.getNode(), C1);
982 C1->setNodeId(X.getNode()->getNodeId());
983 }
984 if (NewANDMask.getNode()->getNodeId() == -1 ||
985 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
986 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
987 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
988 }
989 if (NewAND.getNode()->getNodeId() == -1 ||
990 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
991 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
992 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
993 }
994 if (NewSHIFT.getNode()->getNodeId() == -1 ||
995 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
996 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
997 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
998 }
999
Dan Gohman3666f472008-10-13 20:52:04 +00001000 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +00001001
1002 AM.Scale = 1 << ShiftCst;
1003 AM.IndexReg = NewAND;
1004 return false;
1005 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 }
1007
Dan Gohmana60c1b32007-08-13 20:03:06 +00001008 return MatchAddressBase(N, AM, isRoot, Depth);
1009}
1010
1011/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1012/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +00001013bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +00001014 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +00001018 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 AM.IndexReg = N;
1020 AM.Scale = 1;
1021 return false;
1022 }
1023
1024 // Otherwise, we cannot select it.
1025 return true;
1026 }
1027
1028 // Default, generate it as a register.
1029 AM.BaseType = X86ISelAddressMode::RegBase;
1030 AM.Base.Reg = N;
1031 return false;
1032}
1033
1034/// SelectAddr - returns true if it is able pattern match an addressing mode.
1035/// It returns the operands which make up the maximal addressing mode it can
1036/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001037bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1038 SDValue &Scale, SDValue &Index,
1039 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 X86ISelAddressMode AM;
Evan Cheng8655a532009-03-31 01:13:53 +00001041 bool Done = false;
1042 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1043 unsigned Opcode = N.getOpcode();
1044 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1045 Opcode != X86ISD::Wrapper) {
1046 // If we are able to fold N into addressing mode, then we'll allow it even
1047 // if N has multiple uses. In general, addressing computation is used as
1048 // addresses by all of its uses. But watch out for CopyToReg uses, that
1049 // means the address computation is liveout. It will be computed by a LEA
1050 // so we want to avoid computing the address twice.
1051 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1052 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1053 if (UI->getOpcode() == ISD::CopyToReg) {
1054 MatchAddressBase(N, AM, true, 0);
1055 Done = true;
1056 break;
1057 }
1058 }
1059 }
1060 }
1061
1062 if (!Done && MatchAddress(N, AM))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 return false;
1064
Duncan Sands92c43912008-06-06 12:08:01 +00001065 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001067 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 AM.Base.Reg = CurDAG->getRegister(0, VT);
1069 }
1070
Gabor Greif1c80d112008-08-28 21:40:38 +00001071 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 AM.IndexReg = CurDAG->getRegister(0, VT);
1073
1074 getAddressOperands(AM, Base, Scale, Index, Disp);
1075 return true;
1076}
1077
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1079/// match a load whose top elements are either undef or zeros. The load flavor
1080/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001081bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1082 SDValue N, SDValue &Base,
1083 SDValue &Scale, SDValue &Index,
1084 SDValue &Disp, SDValue &InChain,
1085 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1087 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001088 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 InChain.getValue(0).hasOneUse() &&
1090 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001091 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1093 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1094 return false;
1095 OutChain = LD->getChain();
1096 return true;
1097 }
1098 }
1099
1100 // Also handle the case where we explicitly require zeros in the top
1101 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001102 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001103 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001104 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001105 N.getOperand(0).getNode()->hasOneUse() &&
1106 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001107 N.getOperand(0).getOperand(0).hasOneUse()) {
1108 // Okay, this is a zero extending load. Fold it.
1109 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1110 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1111 return false;
1112 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001113 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001114 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 }
1116 return false;
1117}
1118
1119
1120/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1121/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001122bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1123 SDValue &Base, SDValue &Scale,
1124 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 X86ISelAddressMode AM;
1126 if (MatchAddress(N, AM))
1127 return false;
1128
Duncan Sands92c43912008-06-06 12:08:01 +00001129 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 unsigned Complexity = 0;
1131 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001132 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001133 Complexity = 1;
1134 else
1135 AM.Base.Reg = CurDAG->getRegister(0, VT);
1136 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1137 Complexity = 4;
1138
Gabor Greif1c80d112008-08-28 21:40:38 +00001139 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 Complexity++;
1141 else
1142 AM.IndexReg = CurDAG->getRegister(0, VT);
1143
1144 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1145 // a simple shift.
1146 if (AM.Scale > 1)
1147 Complexity++;
1148
1149 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1150 // to a LEA. This is determined with some expermentation but is by no means
1151 // optimal (especially for code size consideration). LEA is nice because of
1152 // its three-address nature. Tweak the cost function again when we can run
1153 // convertToThreeAddress() at register allocation time.
Dan Gohman245791b2009-02-07 00:43:41 +00001154 if (AM.hasSymbolicDisplacement()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155 // For X86-64, we should always use lea to materialize RIP relative
1156 // addresses.
1157 if (Subtarget->is64Bit())
1158 Complexity = 4;
1159 else
1160 Complexity += 2;
1161 }
1162
Gabor Greif1c80d112008-08-28 21:40:38 +00001163 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 Complexity++;
1165
1166 if (Complexity > 2) {
1167 getAddressOperands(AM, Base, Scale, Index, Disp);
1168 return true;
1169 }
1170 return false;
1171}
1172
Dan Gohman8181bd12008-07-27 21:46:04 +00001173bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1174 SDValue &Base, SDValue &Scale,
1175 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001176 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001178 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1180 return false;
1181}
1182
Dan Gohmanb60482f2008-09-23 18:22:58 +00001183/// getGlobalBaseReg - Return an SDNode that returns the value of
1184/// the global base register. Output instructions required to
1185/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186///
1187SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001188 MachineFunction *MF = CurBB->getParent();
1189 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001190 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191}
1192
1193static SDNode *FindCallStartFromCall(SDNode *Node) {
1194 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1195 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1196 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001197 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198}
1199
Dan Gohmandd612bb2008-08-20 21:27:32 +00001200/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1201/// truncate of the specified operand to i8. This can be done with tablegen,
1202/// except that this code uses MVT::Flag in a tricky way that happens to
1203/// improve scheduling in some cases.
1204SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1205 assert(!Subtarget->is64Bit() &&
1206 "getTruncateTo8Bit is only needed on x86-32!");
1207 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00001208 DebugLoc dl = N0.getDebugLoc();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001209
1210 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1211 unsigned Opc;
1212 MVT N0VT = N0.getValueType();
1213 switch (N0VT.getSimpleVT()) {
1214 default: assert(0 && "Unknown truncate!");
1215 case MVT::i16:
1216 Opc = X86::MOV16to16_;
1217 break;
1218 case MVT::i32:
1219 Opc = X86::MOV32to32_;
1220 break;
1221 }
1222
1223 // The use of MVT::Flag here is not strictly accurate, but it helps
1224 // scheduling in some cases.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001225 N0 = SDValue(CurDAG->getTargetNode(Opc, dl, N0VT, MVT::Flag, N0), 0);
1226 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmandd612bb2008-08-20 21:27:32 +00001227 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001228}
1229
Dale Johannesenf160d802008-10-02 18:53:47 +00001230SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1231 SDValue Chain = Node->getOperand(0);
1232 SDValue In1 = Node->getOperand(1);
1233 SDValue In2L = Node->getOperand(2);
1234 SDValue In2H = Node->getOperand(3);
1235 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1236 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1237 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001238 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001239 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001240 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1241 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001242 array_lengthof(Ops));
Dale Johannesenf160d802008-10-02 18:53:47 +00001243}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001244
Dan Gohman8181bd12008-07-27 21:46:04 +00001245SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001246 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001247 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 unsigned Opc, MOpc;
1249 unsigned Opcode = Node->getOpcode();
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001250 DebugLoc dl = Node->getDebugLoc();
1251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252#ifndef NDEBUG
1253 DOUT << std::string(Indent, ' ') << "Selecting: ";
1254 DEBUG(Node->dump(CurDAG));
1255 DOUT << "\n";
1256 Indent += 2;
1257#endif
1258
Dan Gohmanbd68c792008-07-17 19:10:17 +00001259 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260#ifndef NDEBUG
1261 DOUT << std::string(Indent-2, ' ') << "== ";
1262 DEBUG(Node->dump(CurDAG));
1263 DOUT << "\n";
1264 Indent -= 2;
1265#endif
1266 return NULL; // Already selected.
1267 }
1268
1269 switch (Opcode) {
1270 default: break;
1271 case X86ISD::GlobalBaseReg:
1272 return getGlobalBaseReg();
1273
Dale Johannesenf160d802008-10-02 18:53:47 +00001274 case X86ISD::ATOMOR64_DAG:
1275 return SelectAtomic64(Node, X86::ATOMOR6432);
1276 case X86ISD::ATOMXOR64_DAG:
1277 return SelectAtomic64(Node, X86::ATOMXOR6432);
1278 case X86ISD::ATOMADD64_DAG:
1279 return SelectAtomic64(Node, X86::ATOMADD6432);
1280 case X86ISD::ATOMSUB64_DAG:
1281 return SelectAtomic64(Node, X86::ATOMSUB6432);
1282 case X86ISD::ATOMNAND64_DAG:
1283 return SelectAtomic64(Node, X86::ATOMNAND6432);
1284 case X86ISD::ATOMAND64_DAG:
1285 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001286 case X86ISD::ATOMSWAP64_DAG:
1287 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001288
Dan Gohman5a199552007-10-08 18:33:35 +00001289 case ISD::SMUL_LOHI:
1290 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001291 SDValue N0 = Node->getOperand(0);
1292 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001293
Dan Gohman5a199552007-10-08 18:33:35 +00001294 bool isSigned = Opcode == ISD::SMUL_LOHI;
1295 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001296 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297 default: assert(0 && "Unsupported VT!");
1298 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1299 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1300 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1301 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1302 }
1303 else
Duncan Sands92c43912008-06-06 12:08:01 +00001304 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 default: assert(0 && "Unsupported VT!");
1306 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1307 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1308 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1309 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1310 }
1311
1312 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001313 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 default: assert(0 && "Unsupported VT!");
1315 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1316 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1317 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1318 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1319 }
1320
Dan Gohman8181bd12008-07-27 21:46:04 +00001321 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001322 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001323 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 if (!foldedLoad) {
1325 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001326 if (foldedLoad)
1327 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 }
1329
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001330 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman8181bd12008-07-27 21:46:04 +00001331 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332
1333 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001334 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 SDNode *CNode =
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001336 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001337 array_lengthof(Ops));
Dan Gohman8181bd12008-07-27 21:46:04 +00001338 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001339 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001340 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001343 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 }
1345
Dan Gohman5a199552007-10-08 18:33:35 +00001346 // Copy the low half of the result, if it is needed.
1347 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001348 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001349 LoReg, NVT, InFlag);
1350 InFlag = Result.getValue(2);
1351 ReplaceUses(N.getValue(0), Result);
1352#ifndef NDEBUG
1353 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001354 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001355 DOUT << "\n";
1356#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001357 }
Dan Gohman5a199552007-10-08 18:33:35 +00001358 // Copy the high half of the result, if it is needed.
1359 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001360 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001361 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1362 // Prevent use of AH in a REX instruction by referencing AX instead.
1363 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001364 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001365 X86::AX, MVT::i16, InFlag);
1366 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001367 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1368 Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001369 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001370 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001371 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001372 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001373 MVT::i8, Result, SRIdx), 0);
1374 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001375 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001376 HiReg, NVT, InFlag);
1377 InFlag = Result.getValue(2);
1378 }
1379 ReplaceUses(N.getValue(1), Result);
1380#ifndef NDEBUG
1381 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001382 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001383 DOUT << "\n";
1384#endif
1385 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386
1387#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 Indent -= 2;
1389#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 return NULL;
1392 }
1393
Dan Gohman5a199552007-10-08 18:33:35 +00001394 case ISD::SDIVREM:
1395 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001396 SDValue N0 = Node->getOperand(0);
1397 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001398
1399 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001401 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402 default: assert(0 && "Unsupported VT!");
1403 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1404 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1405 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1406 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1407 }
1408 else
Duncan Sands92c43912008-06-06 12:08:01 +00001409 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 default: assert(0 && "Unsupported VT!");
1411 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1412 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1413 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1414 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1415 }
1416
1417 unsigned LoReg, HiReg;
1418 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001419 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 default: assert(0 && "Unsupported VT!");
1421 case MVT::i8:
1422 LoReg = X86::AL; HiReg = X86::AH;
1423 ClrOpcode = 0;
1424 SExtOpcode = X86::CBW;
1425 break;
1426 case MVT::i16:
1427 LoReg = X86::AX; HiReg = X86::DX;
1428 ClrOpcode = X86::MOV16r0;
1429 SExtOpcode = X86::CWD;
1430 break;
1431 case MVT::i32:
1432 LoReg = X86::EAX; HiReg = X86::EDX;
1433 ClrOpcode = X86::MOV32r0;
1434 SExtOpcode = X86::CDQ;
1435 break;
1436 case MVT::i64:
1437 LoReg = X86::RAX; HiReg = X86::RDX;
1438 ClrOpcode = X86::MOV64r0;
1439 SExtOpcode = X86::CQO;
1440 break;
1441 }
1442
Dan Gohman8181bd12008-07-27 21:46:04 +00001443 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001444 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001445 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001446
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 SDValue InFlag;
Dan Gohman7bbd9202009-01-21 14:50:16 +00001448 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 // Special case for div8, just use a move with zero extension to AX to
1450 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001451 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001453 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001455 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001456 MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001457 array_lengthof(Ops)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 Chain = Move.getValue(1);
1459 ReplaceUses(N0.getValue(1), Chain);
1460 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001462 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 Chain = CurDAG->getEntryNode();
1464 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001465 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 InFlag = Chain.getValue(1);
1467 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 InFlag =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001469 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00001470 LoReg, N0, SDValue()).getValue(1);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001471 if (isSigned && !signBitIsZero) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 // Sign extend the low part into the high part.
1473 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001474 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 } else {
1476 // Zero out the high part, effectively zero extending the input.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001477 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1478 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001479 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman5a199552007-10-08 18:33:35 +00001480 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 }
1482 }
1483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001485 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486 SDNode *CNode =
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001487 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001488 array_lengthof(Ops));
Dan Gohman8181bd12008-07-27 21:46:04 +00001489 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001490 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001491 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001494 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 }
1496
Dan Gohman242a5ba2007-09-25 18:23:27 +00001497 // Copy the division (low) result, if it is needed.
1498 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001499 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001500 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001501 InFlag = Result.getValue(2);
1502 ReplaceUses(N.getValue(0), Result);
1503#ifndef NDEBUG
1504 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001505 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001506 DOUT << "\n";
1507#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001508 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001509 // Copy the remainder (high) result, if it is needed.
1510 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001511 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001512 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1513 // Prevent use of AH in a REX instruction by referencing AX instead.
1514 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001515 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001516 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001517 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001518 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1519 Result,
1520 CurDAG->getTargetConstant(8, MVT::i8)),
1521 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001522 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001523 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001524 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001525 MVT::i8, Result, SRIdx), 0);
1526 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001527 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001528 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001529 InFlag = Result.getValue(2);
1530 }
1531 ReplaceUses(N.getValue(1), Result);
1532#ifndef NDEBUG
1533 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001534 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001535 DOUT << "\n";
1536#endif
1537 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538
1539#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 Indent -= 2;
1541#endif
1542
1543 return NULL;
1544 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001545
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001546 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001547 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001548 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1549 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001550
Dan Gohmandd612bb2008-08-20 21:27:32 +00001551 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1552 unsigned Opc = 0;
1553 switch (NVT.getSimpleVT()) {
1554 default: assert(0 && "Unknown sign_extend_inreg!");
1555 case MVT::i16:
1556 Opc = X86::MOVSX16rr8;
1557 break;
1558 case MVT::i32:
1559 Opc = X86::MOVSX32rr8;
1560 break;
1561 }
1562
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001563 SDNode *ResNode = CurDAG->getTargetNode(Opc, dl, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001564
1565#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001566 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001567 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001568 DOUT << "\n";
1569 DOUT << std::string(Indent-2, ' ') << "=> ";
1570 DEBUG(ResNode->dump(CurDAG));
1571 DOUT << "\n";
1572 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001573#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001574 return ResNode;
1575 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001576 break;
1577 }
1578
1579 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001580 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1581 SDValue Input = Node->getOperand(0);
Dan Gohmandd612bb2008-08-20 21:27:32 +00001582 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584#ifndef NDEBUG
1585 DOUT << std::string(Indent-2, ' ') << "=> ";
1586 DEBUG(ResNode->dump(CurDAG));
1587 DOUT << "\n";
1588 Indent -= 2;
1589#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001590 return ResNode;
1591 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 break;
1593 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001594
1595 case ISD::DECLARE: {
1596 // Handle DECLARE nodes here because the second operand may have been
1597 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001598 SDValue Chain = Node->getOperand(0);
1599 SDValue N1 = Node->getOperand(1);
1600 SDValue N2 = Node->getOperand(2);
Evan Cheng417bc002008-12-10 21:49:05 +00001601 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001602
1603 // FIXME: We need to handle this for VLAs.
1604 if (!FINode) {
1605 ReplaceUses(N.getValue(0), Chain);
1606 return NULL;
1607 }
1608
Evan Cheng651e1442008-06-18 02:48:27 +00001609 if (N2.getOpcode() == ISD::ADD &&
1610 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1611 N2 = N2.getOperand(1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001612
1613 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1614 // somehow, just ignore it.
1615 if (N2.getOpcode() != X86ISD::Wrapper) {
1616 ReplaceUses(N.getValue(0), Chain);
1617 return NULL;
1618 }
Evan Chengf3ecd1a2009-01-10 03:33:22 +00001619 GlobalAddressSDNode *GVNode =
1620 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattneref6a8192009-02-12 17:33:11 +00001621 if (GVNode == 0) {
1622 ReplaceUses(N.getValue(0), Chain);
1623 return NULL;
1624 }
Evan Cheng417bc002008-12-10 21:49:05 +00001625 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1626 TLI.getPointerTy());
1627 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1628 TLI.getPointerTy());
1629 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001630 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001631 MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001632 array_lengthof(Ops));
Evan Chengd4cebcd2008-06-17 02:01:22 +00001633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 }
1635
1636 SDNode *ResNode = SelectCode(N);
1637
1638#ifndef NDEBUG
1639 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001640 if (ResNode == NULL || ResNode == N.getNode())
1641 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001642 else
1643 DEBUG(ResNode->dump(CurDAG));
1644 DOUT << "\n";
1645 Indent -= 2;
1646#endif
1647
1648 return ResNode;
1649}
1650
1651bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001652SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001653 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001654 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 switch (ConstraintCode) {
1656 case 'o': // offsetable ??
1657 case 'v': // not offsetable ??
1658 default: return true;
1659 case 'm': // memory
1660 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1661 return true;
1662 break;
1663 }
1664
1665 OutOps.push_back(Op0);
1666 OutOps.push_back(Op1);
1667 OutOps.push_back(Op2);
1668 OutOps.push_back(Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669 return false;
1670}
1671
1672/// createX86ISelDag - This pass converts a legalized DAG into a
1673/// X86-specific DAG, ready for instruction scheduling.
1674///
1675FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1676 return new X86DAGToDAGISel(TM, Fast);
1677}