blob: 5c49e528c5fe195211a2c51c5686dcc38645e962 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000038#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000039#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/Statistic.h"
41#include <queue>
42#include <set>
43using namespace llvm;
44
45STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
46STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048//===----------------------------------------------------------------------===//
49// Pattern Matcher Implementation
50//===----------------------------------------------------------------------===//
51
52namespace {
53 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000054 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 /// tree.
56 struct X86ISelAddressMode {
57 enum {
58 RegBase,
59 FrameIndexBase
60 } BaseType;
61
62 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000063 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 int FrameIndex;
65 } Base;
66
Evan Cheng3b5a1272008-02-07 08:53:49 +000067 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 unsigned Disp;
71 GlobalValue *GV;
72 Constant *CP;
73 const char *ES;
74 int JT;
75 unsigned Align; // CP alignment.
76
77 X86ISelAddressMode()
78 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
79 GV(0), CP(0), ES(0), JT(-1), Align(0) {
80 }
Dale Johannesenc501c082008-08-11 23:46:25 +000081 void dump() {
82 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif1c80d112008-08-28 21:40:38 +000083 cerr << "Base.Reg "; if (Base.Reg.getNode()!=0) Base.Reg.getNode()->dump();
Dale Johannesenc501c082008-08-11 23:46:25 +000084 else cerr << "nul";
85 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
86 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif1c80d112008-08-28 21:40:38 +000087 cerr << "IndexReg "; if (IndexReg.getNode()!=0) IndexReg.getNode()->dump();
Dale Johannesenc501c082008-08-11 23:46:25 +000088 else cerr << "nul";
89 cerr << " Disp " << Disp << "\n";
90 cerr << "GV "; if (GV) GV->dump();
91 else cerr << "nul";
92 cerr << " CP "; if (CP) CP->dump();
93 else cerr << "nul";
94 cerr << "\n";
95 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
96 cerr << " JT" << JT << " Align" << Align << "\n";
97 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 };
99}
100
101namespace {
102 //===--------------------------------------------------------------------===//
103 /// ISel - X86 specific code to select X86 machine instructions for
104 /// SelectionDAG operations.
105 ///
106 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
107 /// ContainsFPCode - Every instruction we select that uses or defines a FP
108 /// register should set this to true.
109 bool ContainsFPCode;
110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 /// TM - Keep a reference to X86TargetMachine.
112 ///
113 X86TargetMachine &TM;
114
115 /// X86Lowering - This object fully describes how to lower LLVM code to an
116 /// X86-specific SelectionDAG.
117 X86TargetLowering X86Lowering;
118
119 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
120 /// make the right decision when generating code for different targets.
121 const X86Subtarget *Subtarget;
122
123 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
124 /// base register.
125 unsigned GlobalBaseReg;
126
Evan Cheng34fd4f32008-06-30 20:45:06 +0000127 /// CurBB - Current BB being isel'd.
128 ///
129 MachineBasicBlock *CurBB;
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 public:
132 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng9b77cae2008-07-01 18:05:03 +0000133 : SelectionDAGISel(X86Lowering, fast),
134 ContainsFPCode(false), TM(tm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 X86Lowering(*TM.getTargetLowering()),
136 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
137
138 virtual bool runOnFunction(Function &Fn) {
139 // Make sure we re-emit a set of the global base reg if necessary
140 GlobalBaseReg = 0;
141 return SelectionDAGISel::runOnFunction(Fn);
142 }
143
144 virtual const char *getPassName() const {
145 return "X86 DAG->DAG Instruction Selection";
146 }
147
Evan Cheng34fd4f32008-06-30 20:45:06 +0000148 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000150 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000151
152 /// InstructionSelectPostProcessing - Post processing of selected and
153 /// scheduled basic blocks.
Dan Gohmanb552df72008-07-21 20:00:07 +0000154 virtual void InstructionSelectPostProcessing();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000156 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
157
Dan Gohmand6098272007-07-24 23:00:27 +0000158 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
160// Include the pieces autogenerated from the target description.
161#include "X86GenDAGISel.inc"
162
163 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 SDNode *Select(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Dan Gohman8181bd12008-07-27 21:46:04 +0000166 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000169 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000170 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
171 SDValue &Scale, SDValue &Index, SDValue &Disp);
172 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
175 SDValue N, SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp,
177 SDValue &InChain, SDValue &OutChain);
178 bool TryFoldLoad(SDValue P, SDValue N,
179 SDValue &Base, SDValue &Scale,
180 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000181 void PreprocessForRMW();
182 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
184 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
185 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000186 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000188 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000190 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
191
Dan Gohman8181bd12008-07-27 21:46:04 +0000192 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
193 SDValue &Scale, SDValue &Index,
194 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
196 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
197 AM.Base.Reg;
198 Scale = getI8Imm(AM.Scale);
199 Index = AM.IndexReg;
200 // These are 32-bit even in 64-bit mode since RIP relative offset
201 // is 32-bit.
202 if (AM.GV)
203 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
204 else if (AM.CP)
205 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
206 else if (AM.ES)
207 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
208 else if (AM.JT != -1)
209 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
210 else
211 Disp = getI32Imm(AM.Disp);
212 }
213
214 /// getI8Imm - Return a target constant with the specified value, of type
215 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000216 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return CurDAG->getTargetConstant(Imm, MVT::i8);
218 }
219
220 /// getI16Imm - Return a target constant with the specified value, of type
221 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000222 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return CurDAG->getTargetConstant(Imm, MVT::i16);
224 }
225
226 /// getI32Imm - Return a target constant with the specified value, of type
227 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000228 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return CurDAG->getTargetConstant(Imm, MVT::i32);
230 }
231
232 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
233 /// base register. Return the virtual register that holds this value.
234 SDNode *getGlobalBaseReg();
235
Dan Gohmandd612bb2008-08-20 21:27:32 +0000236 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
237 /// truncate of the specified operand to i8. This can be done with tablegen,
238 /// except that this code uses MVT::Flag in a tricky way that happens to
239 /// improve scheduling in some cases.
240 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242#ifndef NDEBUG
243 unsigned Indent;
244#endif
245 };
246}
247
Evan Cheng656269e2008-04-25 08:22:20 +0000248/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
249///
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 Gohman0c97f1d2008-07-27 20:43:25 +0000253 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000255 SDValue Op = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +0000256 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 return User;
258 }
259 }
260 return NULL;
261}
262
Evan Cheng656269e2008-04-25 08:22:20 +0000263/// findNonImmUse - Return true by reference in "found" if "Use" is an
264/// non-immediate use of "Def". This function recursively traversing
265/// up the operand chain ignoring certain nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
267 SDNode *Root, SDNode *Skip, bool &found,
Evan Cheng656269e2008-04-25 08:22:20 +0000268 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 if (found ||
270 Use->getNodeId() > Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000271 !Visited.insert(Use))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 return;
Evan Cheng656269e2008-04-25 08:22:20 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000275 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 if (N == Skip)
277 continue;
278 if (N == Def) {
279 if (Use == ImmedUse)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000280 continue; // We are not looking for immediate use.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 if (Use == Root) {
Evan Cheng9ea310c2008-04-25 08:55:28 +0000282 // Must be a chain reading node where it is possible to reach its own
283 // chain operand through a path started from another operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattnercfbb2722008-04-25 05:13:01 +0000285 Use->getOpcode() == X86ISD::CMP ||
Chris Lattnercfbb2722008-04-25 05:13:01 +0000286 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
287 Use->getOpcode() == ISD::INTRINSIC_VOID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 continue;
289 }
290 found = true;
291 break;
292 }
Evan Cheng656269e2008-04-25 08:22:20 +0000293
294 // Traverse up the operand chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
296 }
297}
298
299/// isNonImmUse - Start searching from Root up the DAG to check is Def can
300/// be reached. Return true if that's the case. However, ignore direct uses
301/// by ImmedUse (which would be U in the example illustrated in
302/// CanBeFoldedBy) and by Root (which can happen in the store case).
303/// FIXME: to be really generic, we should allow direct use by any node
304/// that is being folded. But realisticly since we only fold loads which
305/// have one non-chain use, we only need to watch out for load/op/store
306/// and load/op/cmp case where the root (store / cmp) may reach the load via
307/// its chain operand.
308static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
309 SDNode *Skip = NULL) {
Evan Cheng656269e2008-04-25 08:22:20 +0000310 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 bool found = false;
312 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
313 return found;
314}
315
316
Dan Gohmand6098272007-07-24 23:00:27 +0000317bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000318 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 // If U use can somehow reach N through another path then U can't fold N or
321 // it will create a cycle. e.g. In the following diagram, U can reach N
322 // through X. If N is folded into into U, then X is both a predecessor and
323 // a successor of U.
324 //
325 // [ N ]
326 // ^ ^
327 // | |
328 // / \---
329 // / [X]
330 // | ^
331 // [U]--------|
332
333 if (isNonImmUse(Root, N, U))
334 return false;
335
336 // If U produces a flag, then it gets (even more) interesting. Since it
337 // would have been "glued" together with its flag use, we need to check if
338 // it might reach N:
339 //
340 // [ N ]
341 // ^ ^
342 // | |
343 // [U] \--
344 // ^ [TF]
345 // | ^
346 // | |
347 // \ /
348 // [FU]
349 //
350 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
351 // NU), then TF is a predecessor of FU and a successor of NU. But since
352 // NU and FU are flagged together, this effectively creates a cycle.
353 bool HasFlagUse = false;
Duncan Sands92c43912008-06-06 12:08:01 +0000354 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 while ((VT == MVT::Flag && !Root->use_empty())) {
356 SDNode *FU = findFlagUse(Root);
357 if (FU == NULL)
358 break;
359 else {
360 Root = FU;
361 HasFlagUse = true;
362 }
363 VT = Root->getValueType(Root->getNumValues()-1);
364 }
365
366 if (HasFlagUse)
367 return !isNonImmUse(Root, N, Root, U);
368 return true;
369}
370
371/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
372/// and move load below the TokenFactor. Replace store's chain operand with
373/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000374static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000375 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000376 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000377 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
378 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000379 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000381 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000382 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
383 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
384 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
385 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386}
387
Evan Cheng2b2a7012008-05-23 21:23:16 +0000388/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
389///
Dan Gohman8181bd12008-07-27 21:46:04 +0000390static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
391 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000392 if (N.getOpcode() == ISD::BIT_CONVERT)
393 N = N.getOperand(0);
394
395 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
396 if (!LD || LD->isVolatile())
397 return false;
398 if (LD->getAddressingMode() != ISD::UNINDEXED)
399 return false;
400
401 ISD::LoadExtType ExtType = LD->getExtensionType();
402 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
403 return false;
404
405 if (N.hasOneUse() &&
406 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000407 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000408 Load = N;
409 return true;
410 }
411 return false;
412}
413
Evan Cheng98cfaf82008-08-25 21:27:18 +0000414/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
415/// operand and move load below the call's chain operand.
416static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
417 SDValue Call, SDValue Chain) {
418 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000419 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
420 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000421 Ops.push_back(Load.getOperand(0));
422 else
423 Ops.push_back(Chain.getOperand(i));
424 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
425 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
426 Load.getOperand(1), Load.getOperand(2));
427 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000428 Ops.push_back(SDValue(Load.getNode(), 1));
429 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000430 Ops.push_back(Call.getOperand(i));
431 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
432}
433
434/// isCalleeLoad - Return true if call address is a load and it can be
435/// moved below CALLSEQ_START and the chains leading up to the call.
436/// Return the CALLSEQ_START by reference as a second output.
437static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000438 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000439 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000440 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000441 if (!LD ||
442 LD->isVolatile() ||
443 LD->getAddressingMode() != ISD::UNINDEXED ||
444 LD->getExtensionType() != ISD::NON_EXTLOAD)
445 return false;
446
447 // Now let's find the callseq_start.
448 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
449 if (!Chain.hasOneUse())
450 return false;
451 Chain = Chain.getOperand(0);
452 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000453 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Cheng98cfaf82008-08-25 21:27:18 +0000454}
455
456
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000457/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
458/// This is only run if not in -fast mode (aka -O0).
459/// This allows the instruction selector to pick more read-modify-write
460/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461///
462/// [Load chain]
463/// ^
464/// |
465/// [Load]
466/// ^ ^
467/// | |
468/// / \-
469/// / |
470/// [TokenFactor] [Op]
471/// ^ ^
472/// | |
473/// \ /
474/// \ /
475/// [Store]
476///
477/// The fact the store's chain operand != load's chain will prevent the
478/// (store (op (load))) instruction from being selected. We can transform it to:
479///
480/// [Load chain]
481/// ^
482/// |
483/// [TokenFactor]
484/// ^
485/// |
486/// [Load]
487/// ^ ^
488/// | |
489/// | \-
490/// | |
491/// | [Op]
492/// | ^
493/// | |
494/// \ /
495/// \ /
496/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000497void X86DAGToDAGISel::PreprocessForRMW() {
498 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
499 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000500 if (I->getOpcode() == X86ISD::CALL) {
501 /// Also try moving call address load from outside callseq_start to just
502 /// before the call to allow it to be folded.
503 ///
504 /// [Load chain]
505 /// ^
506 /// |
507 /// [Load]
508 /// ^ ^
509 /// | |
510 /// / \--
511 /// / |
512 ///[CALLSEQ_START] |
513 /// ^ |
514 /// | |
515 /// [LOAD/C2Reg] |
516 /// | |
517 /// \ /
518 /// \ /
519 /// [CALL]
520 SDValue Chain = I->getOperand(0);
521 SDValue Load = I->getOperand(1);
522 if (!isCalleeLoad(Load, Chain))
523 continue;
524 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
525 ++NumLoadMoved;
526 continue;
527 }
528
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 if (!ISD::isNON_TRUNCStore(I))
530 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000531 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000532
Gabor Greif1c80d112008-08-28 21:40:38 +0000533 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 continue;
535
Dan Gohman8181bd12008-07-27 21:46:04 +0000536 SDValue N1 = I->getOperand(1);
537 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000538 if ((N1.getValueType().isFloatingPoint() &&
539 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 !N1.hasOneUse())
541 continue;
542
543 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000544 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000545 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000547 case ISD::ADD:
548 case ISD::MUL:
549 case ISD::AND:
550 case ISD::OR:
551 case ISD::XOR:
552 case ISD::ADDC:
553 case ISD::ADDE:
554 case ISD::VECTOR_SHUFFLE: {
555 SDValue N10 = N1.getOperand(0);
556 SDValue N11 = N1.getOperand(1);
557 RModW = isRMWLoad(N10, Chain, N2, Load);
558 if (!RModW)
559 RModW = isRMWLoad(N11, Chain, N2, Load);
560 break;
561 }
562 case ISD::SUB:
563 case ISD::SHL:
564 case ISD::SRA:
565 case ISD::SRL:
566 case ISD::ROTL:
567 case ISD::ROTR:
568 case ISD::SUBC:
569 case ISD::SUBE:
570 case X86ISD::SHLD:
571 case X86ISD::SHRD: {
572 SDValue N10 = N1.getOperand(0);
573 RModW = isRMWLoad(N10, Chain, N2, Load);
574 break;
575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 }
577
578 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000579 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 ++NumLoadMoved;
581 }
582 }
583}
584
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000585
586/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
587/// nodes that target the FP stack to be store and load to the stack. This is a
588/// gross hack. We would like to simply mark these as being illegal, but when
589/// we do that, legalize produces these when it expands calls, then expands
590/// these in the same legalize pass. We would like dag combine to be able to
591/// hack on these between the call expansion and the node legalization. As such
592/// this pass basically does "really late" legalization of these inline with the
593/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000594void X86DAGToDAGISel::PreprocessForFPConvert() {
595 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
596 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000597 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
598 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
599 continue;
600
601 // If the source and destination are SSE registers, then this is a legal
602 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000603 MVT SrcVT = N->getOperand(0).getValueType();
604 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000605 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
606 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
607 if (SrcIsSSE && DstIsSSE)
608 continue;
609
Chris Lattner5d294e52008-03-09 07:05:32 +0000610 if (!SrcIsSSE && !DstIsSSE) {
611 // If this is an FPStack extension, it is a noop.
612 if (N->getOpcode() == ISD::FP_EXTEND)
613 continue;
614 // If this is a value-preserving FPStack truncation, it is a noop.
615 if (N->getConstantOperandVal(1))
616 continue;
617 }
618
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000619 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
620 // FPStack has extload and truncstore. SSE can fold direct loads into other
621 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000622 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000623 if (N->getOpcode() == ISD::FP_ROUND)
624 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
625 else
626 MemVT = SrcIsSSE ? SrcVT : DstVT;
627
Dan Gohman14a66442008-08-23 02:25:05 +0000628 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000629
630 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman14a66442008-08-23 02:25:05 +0000631 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
632 N->getOperand(0),
633 MemTmp, NULL, 0, MemVT);
634 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
635 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000636
637 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
638 // extload we created. This will cause general havok on the dag because
639 // anything below the conversion could be folded into other existing nodes.
640 // To avoid invalidating 'I', back it up to the convert node.
641 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000642 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000643
644 // Now that we did that, the node is dead. Increment the iterator to the
645 // next node to process, then delete N.
646 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000647 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000648 }
649}
650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
652/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000653void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000654 CurBB = BB; // BB can change as result of isel.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655
Evan Cheng34fd4f32008-06-30 20:45:06 +0000656 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000657 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000658 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000659
660 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000661 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662
663 // Codegen the basic block.
664#ifndef NDEBUG
665 DOUT << "===== Instruction selection begins:\n";
666 Indent = 0;
667#endif
Dan Gohmanbd3f8822008-08-21 16:36:34 +0000668 SelectRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669#ifndef NDEBUG
670 DOUT << "===== Instruction selection ends:\n";
671#endif
672
Dan Gohman14a66442008-08-23 02:25:05 +0000673 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000674}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
Dan Gohmanb552df72008-07-21 20:00:07 +0000676void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 // If we are emitting FP stack code, scan the basic block to determine if this
678 // block defines any FP values. If so, put an FP_REG_KILL instruction before
679 // the terminator of the block.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000680
Dale Johannesen684887e2007-09-24 22:52:39 +0000681 // Note that FP stack instructions are used in all modes for long double,
682 // so we always need to do this check.
683 // Also note that it's possible for an FP stack register to be live across
684 // an instruction that produces multiple basic blocks (SSE CMOV) so we
685 // must check all the generated basic blocks.
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000686
687 // Scan all of the machine instructions in these MBBs, checking for FP
688 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Cheng34fd4f32008-06-30 20:45:06 +0000689 MachineFunction::iterator MBBI = CurBB;
Chris Lattner04d64b22008-03-10 23:34:12 +0000690 MachineFunction::iterator EndMBB = BB; ++EndMBB;
691 for (; MBBI != EndMBB; ++MBBI) {
692 MachineBasicBlock *MBB = MBBI;
693
694 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
695 // before the return.
696 if (!MBB->empty()) {
697 MachineBasicBlock::iterator EndI = MBB->end();
698 --EndI;
699 if (EndI->getDesc().isReturn())
700 continue;
701 }
702
Dale Johannesen684887e2007-09-24 22:52:39 +0000703 bool ContainsFPCode = false;
Chris Lattner04d64b22008-03-10 23:34:12 +0000704 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000705 !ContainsFPCode && I != E; ++I) {
706 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
707 const TargetRegisterClass *clas;
708 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
709 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner04d64b22008-03-10 23:34:12 +0000710 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner1b989192007-12-31 04:13:23 +0000711 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000712 X86::RFP32RegisterClass ||
713 clas == X86::RFP64RegisterClass ||
714 clas == X86::RFP80RegisterClass)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 ContainsFPCode = true;
716 break;
717 }
718 }
719 }
720 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000721 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
722 // a copy of the input value in this block. In SSE mode, we only care about
723 // 80-bit values.
724 if (!ContainsFPCode) {
725 // Final check, check LLVM BB's that are successors to the LLVM BB
726 // corresponding to BB for FP PHI nodes.
727 const BasicBlock *LLVMBB = BB->getBasicBlock();
728 const PHINode *PN;
729 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
730 !ContainsFPCode && SI != E; ++SI) {
731 for (BasicBlock::const_iterator II = SI->begin();
732 (PN = dyn_cast<PHINode>(II)); ++II) {
733 if (PN->getType()==Type::X86_FP80Ty ||
734 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
735 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
736 ContainsFPCode = true;
737 break;
738 }
Dale Johannesenc428e0f2007-08-07 20:29:26 +0000739 }
740 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 }
Dale Johannesen684887e2007-09-24 22:52:39 +0000742 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
743 if (ContainsFPCode) {
Chris Lattner04d64b22008-03-10 23:34:12 +0000744 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen684887e2007-09-24 22:52:39 +0000745 TM.getInstrInfo()->get(X86::FP_REG_KILL));
746 ++NumFPKill;
747 }
Chris Lattner04d64b22008-03-10 23:34:12 +0000748 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749}
750
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000751/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
752/// the main function.
753void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
754 MachineFrameInfo *MFI) {
755 const TargetInstrInfo *TII = TM.getInstrInfo();
756 if (Subtarget->isTargetCygMing())
757 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
758}
759
760void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
761 // If this is main, emit special code for main.
762 MachineBasicBlock *BB = MF.begin();
763 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
764 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
765}
766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767/// MatchAddress - Add the specified node to the specified addressing mode,
768/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000769/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000770bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 bool isRoot, unsigned Depth) {
Dale Johannesenc501c082008-08-11 23:46:25 +0000772DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000773 // Limit recursion.
774 if (Depth > 5)
775 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776
777 // RIP relative addressing: %rip + 32-bit displacement!
778 if (AM.isRIPRel) {
779 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
780 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
781 if (isInt32(AM.Disp + Val)) {
782 AM.Disp += Val;
783 return false;
784 }
785 }
786 return true;
787 }
788
Gabor Greif1c80d112008-08-28 21:40:38 +0000789 int id = N.getNode()->getNodeId();
Evan Chengf2abee72007-12-13 00:43:27 +0000790 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791
792 switch (N.getOpcode()) {
793 default: break;
794 case ISD::Constant: {
795 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
796 if (isInt32(AM.Disp + Val)) {
797 AM.Disp += Val;
798 return false;
799 }
800 break;
801 }
802
803 case X86ISD::Wrapper: {
Dale Johannesenc501c082008-08-11 23:46:25 +0000804DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
805DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
806DOUT << "AlreadySelected " << AlreadySelected << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 bool is64Bit = Subtarget->is64Bit();
808 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000809 // Also, base and index reg must be 0 in order to use rip as base.
810 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000811 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 break;
813 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
814 break;
815 // If value is available in a register both base and index components have
816 // been picked, we can't fit the result available in the register in the
817 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greif1c80d112008-08-28 21:40:38 +0000818 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000819 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
821 GlobalValue *GV = G->getGlobal();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000822 AM.GV = GV;
823 AM.Disp += G->getOffset();
Evan Chenga54e14f2008-02-12 19:20:46 +0000824 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
825 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000826 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000828 AM.CP = CP->getConstVal();
829 AM.Align = CP->getAlignment();
830 AM.Disp += CP->getOffset();
Evan Chenga54e14f2008-02-12 19:20:46 +0000831 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
832 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000833 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000835 AM.ES = S->getSymbol();
Evan Chenga54e14f2008-02-12 19:20:46 +0000836 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
837 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000838 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000840 AM.JT = J->getIndex();
Evan Chenga54e14f2008-02-12 19:20:46 +0000841 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
842 Subtarget->isPICStyleRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000843 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 }
845 }
846 break;
847 }
848
849 case ISD::FrameIndex:
Gabor Greif1c80d112008-08-28 21:40:38 +0000850 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
852 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
853 return false;
854 }
855 break;
856
857 case ISD::SHL:
Gabor Greif1c80d112008-08-28 21:40:38 +0000858 if (AlreadySelected || AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000859 break;
860
Gabor Greif1c80d112008-08-28 21:40:38 +0000861 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Chris Lattner7f06edd2007-12-08 07:22:58 +0000862 unsigned Val = CN->getValue();
863 if (Val == 1 || Val == 2 || Val == 3) {
864 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000865 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866
Chris Lattner7f06edd2007-12-08 07:22:58 +0000867 // 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 (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
871 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
872 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000873 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000874 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Chris Lattner7f06edd2007-12-08 07:22:58 +0000875 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
876 if (isInt32(Disp))
877 AM.Disp = Disp;
878 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000880 } else {
881 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000883 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 }
885 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000886 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887
Dan Gohman35b99222007-10-22 20:22:24 +0000888 case ISD::SMUL_LOHI:
889 case ISD::UMUL_LOHI:
890 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000891 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000892 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 case ISD::MUL:
894 // X*[3,5,9] -> X+X*[2,4,8]
Evan Chengf2abee72007-12-13 00:43:27 +0000895 if (!AlreadySelected &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000897 AM.Base.Reg.getNode() == 0 &&
898 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000899 !AM.isRIPRel) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000900 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
902 AM.Scale = unsigned(CN->getValue())-1;
903
Gabor Greif1c80d112008-08-28 21:40:38 +0000904 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000905 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906
907 // Okay, we know that we have a scale by now. However, if the scaled
908 // value is an add of something and a constant, we can fold the
909 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000910 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
911 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
912 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000914 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
916 if (isInt32(Disp))
917 AM.Disp = Disp;
918 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000919 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000921 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 }
923
924 AM.IndexReg = AM.Base.Reg = Reg;
925 return false;
926 }
927 }
928 break;
929
930 case ISD::ADD:
Evan Chengf2abee72007-12-13 00:43:27 +0000931 if (!AlreadySelected) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 X86ISelAddressMode Backup = AM;
Gabor Greif1c80d112008-08-28 21:40:38 +0000933 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
934 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 return false;
936 AM = Backup;
Gabor Greif1c80d112008-08-28 21:40:38 +0000937 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
938 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939 return false;
940 AM = Backup;
941 }
942 break;
943
944 case ISD::OR:
945 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Chengf2abee72007-12-13 00:43:27 +0000946 if (AlreadySelected) break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000947
948 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
949 X86ISelAddressMode Backup = AM;
950 // Start with the LHS as an addr mode.
951 if (!MatchAddress(N.getOperand(0), AM, false) &&
952 // Address could not have picked a GV address for the displacement.
953 AM.GV == NULL &&
954 // On x86-64, the resultant disp must fit in 32-bits.
955 isInt32(AM.Disp + CN->getSignExtended()) &&
956 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000957 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner7f06edd2007-12-08 07:22:58 +0000958 AM.Disp += CN->getValue();
959 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000961 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 }
963 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000964
965 case ISD::AND: {
966 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
967 // allows us to fold the shift into this addressing mode.
968 if (AlreadySelected) break;
Dan Gohman8181bd12008-07-27 21:46:04 +0000969 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000970 if (Shift.getOpcode() != ISD::SHL) break;
971
972 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000973 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000974
975 // Not when RIP is used as the base.
976 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000977
978 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
979 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
980 if (!C1 || !C2) break;
981
982 // Not likely to be profitable if either the AND or SHIFT node has more
983 // than one use (unless all uses are for address computation). Besides,
984 // isel mechanism requires their node ids to be reused.
985 if (!N.hasOneUse() || !Shift.hasOneUse())
986 break;
987
988 // Verify that the shift amount is something we can fold.
989 unsigned ShiftCst = C1->getValue();
990 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
991 break;
992
993 // Get the new AND mask, this folds to a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +0000994 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
995 SDValue(C2, 0), SDValue(C1, 0));
996 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Chengf2abee72007-12-13 00:43:27 +0000997 Shift.getOperand(0), NewANDMask);
Gabor Greif1c80d112008-08-28 21:40:38 +0000998 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
999 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Evan Chengf2abee72007-12-13 00:43:27 +00001000
1001 AM.Scale = 1 << ShiftCst;
1002 AM.IndexReg = NewAND;
1003 return false;
1004 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 }
1006
Dan Gohmana60c1b32007-08-13 20:03:06 +00001007 return MatchAddressBase(N, AM, isRoot, Depth);
1008}
1009
1010/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1011/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +00001012bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +00001013 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001015 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +00001017 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 AM.IndexReg = N;
1019 AM.Scale = 1;
1020 return false;
1021 }
1022
1023 // Otherwise, we cannot select it.
1024 return true;
1025 }
1026
1027 // Default, generate it as a register.
1028 AM.BaseType = X86ISelAddressMode::RegBase;
1029 AM.Base.Reg = N;
1030 return false;
1031}
1032
1033/// SelectAddr - returns true if it is able pattern match an addressing mode.
1034/// It returns the operands which make up the maximal addressing mode it can
1035/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001036bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1037 SDValue &Scale, SDValue &Index,
1038 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 X86ISelAddressMode AM;
1040 if (MatchAddress(N, AM))
1041 return false;
1042
Duncan Sands92c43912008-06-06 12:08:01 +00001043 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001045 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 AM.Base.Reg = CurDAG->getRegister(0, VT);
1047 }
1048
Gabor Greif1c80d112008-08-28 21:40:38 +00001049 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 AM.IndexReg = CurDAG->getRegister(0, VT);
1051
1052 getAddressOperands(AM, Base, Scale, Index, Disp);
1053 return true;
1054}
1055
1056/// isZeroNode - Returns true if Elt is a constant zero or a floating point
1057/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00001058static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 return ((isa<ConstantSDNode>(Elt) &&
1060 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
1061 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00001062 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063}
1064
1065
1066/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1067/// match a load whose top elements are either undef or zeros. The load flavor
1068/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001069bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1070 SDValue N, SDValue &Base,
1071 SDValue &Scale, SDValue &Index,
1072 SDValue &Disp, SDValue &InChain,
1073 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1075 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001076 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 InChain.getValue(0).hasOneUse() &&
1078 N.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001079 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1081 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1082 return false;
1083 OutChain = LD->getChain();
1084 return true;
1085 }
1086 }
1087
1088 // Also handle the case where we explicitly require zeros in the top
1089 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001090 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001091 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001092 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001093 N.getOperand(0).getNode()->hasOneUse() &&
1094 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001095 N.getOperand(0).getOperand(0).hasOneUse()) {
1096 // Okay, this is a zero extending load. Fold it.
1097 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1098 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1099 return false;
1100 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001101 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001102 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 }
1104 return false;
1105}
1106
1107
1108/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1109/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001110bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1111 SDValue &Base, SDValue &Scale,
1112 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 X86ISelAddressMode AM;
1114 if (MatchAddress(N, AM))
1115 return false;
1116
Duncan Sands92c43912008-06-06 12:08:01 +00001117 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 unsigned Complexity = 0;
1119 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001120 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 Complexity = 1;
1122 else
1123 AM.Base.Reg = CurDAG->getRegister(0, VT);
1124 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1125 Complexity = 4;
1126
Gabor Greif1c80d112008-08-28 21:40:38 +00001127 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 Complexity++;
1129 else
1130 AM.IndexReg = CurDAG->getRegister(0, VT);
1131
1132 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1133 // a simple shift.
1134 if (AM.Scale > 1)
1135 Complexity++;
1136
1137 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1138 // to a LEA. This is determined with some expermentation but is by no means
1139 // optimal (especially for code size consideration). LEA is nice because of
1140 // its three-address nature. Tweak the cost function again when we can run
1141 // convertToThreeAddress() at register allocation time.
1142 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1143 // For X86-64, we should always use lea to materialize RIP relative
1144 // addresses.
1145 if (Subtarget->is64Bit())
1146 Complexity = 4;
1147 else
1148 Complexity += 2;
1149 }
1150
Gabor Greif1c80d112008-08-28 21:40:38 +00001151 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 Complexity++;
1153
1154 if (Complexity > 2) {
1155 getAddressOperands(AM, Base, Scale, Index, Disp);
1156 return true;
1157 }
1158 return false;
1159}
1160
Dan Gohman8181bd12008-07-27 21:46:04 +00001161bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1162 SDValue &Base, SDValue &Scale,
1163 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001164 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 N.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001166 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1168 return false;
1169}
1170
1171/// getGlobalBaseReg - Output the instructions required to put the
1172/// base address to use for accessing globals into a register.
1173///
1174SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1175 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
1176 if (!GlobalBaseReg) {
1177 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0729ccf2008-01-05 00:41:47 +00001178 MachineFunction *MF = BB->getParent();
1179 MachineBasicBlock &FirstMBB = MF->front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0729ccf2008-01-05 00:41:47 +00001181 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner1b989192007-12-31 04:13:23 +00001182 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001183
1184 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Cheng34f93712007-12-22 02:26:46 +00001185 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1186 // only used in JIT code emission as displacement to pc.
Evan Cheng0729ccf2008-01-05 00:41:47 +00001187 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188
1189 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1190 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
1191 if (TM.getRelocationModel() == Reloc::PIC_ &&
1192 Subtarget->isPICStyleGOT()) {
Chris Lattner1b989192007-12-31 04:13:23 +00001193 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0729ccf2008-01-05 00:41:47 +00001194 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1195 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 } else {
1197 GlobalBaseReg = PC;
1198 }
1199
1200 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001201 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202}
1203
1204static SDNode *FindCallStartFromCall(SDNode *Node) {
1205 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1206 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1207 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001208 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209}
1210
Dan Gohmandd612bb2008-08-20 21:27:32 +00001211/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1212/// truncate of the specified operand to i8. This can be done with tablegen,
1213/// except that this code uses MVT::Flag in a tricky way that happens to
1214/// improve scheduling in some cases.
1215SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1216 assert(!Subtarget->is64Bit() &&
1217 "getTruncateTo8Bit is only needed on x86-32!");
1218 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1219
1220 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1221 unsigned Opc;
1222 MVT N0VT = N0.getValueType();
1223 switch (N0VT.getSimpleVT()) {
1224 default: assert(0 && "Unknown truncate!");
1225 case MVT::i16:
1226 Opc = X86::MOV16to16_;
1227 break;
1228 case MVT::i32:
1229 Opc = X86::MOV32to32_;
1230 break;
1231 }
1232
1233 // The use of MVT::Flag here is not strictly accurate, but it helps
1234 // scheduling in some cases.
1235 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1236 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1237 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001238}
1239
1240
Dan Gohman8181bd12008-07-27 21:46:04 +00001241SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001242 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001243 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 unsigned Opc, MOpc;
1245 unsigned Opcode = Node->getOpcode();
1246
1247#ifndef NDEBUG
1248 DOUT << std::string(Indent, ' ') << "Selecting: ";
1249 DEBUG(Node->dump(CurDAG));
1250 DOUT << "\n";
1251 Indent += 2;
1252#endif
1253
Dan Gohmanbd68c792008-07-17 19:10:17 +00001254 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255#ifndef NDEBUG
1256 DOUT << std::string(Indent-2, ' ') << "== ";
1257 DEBUG(Node->dump(CurDAG));
1258 DOUT << "\n";
1259 Indent -= 2;
1260#endif
1261 return NULL; // Already selected.
1262 }
1263
1264 switch (Opcode) {
1265 default: break;
1266 case X86ISD::GlobalBaseReg:
1267 return getGlobalBaseReg();
1268
1269 case ISD::ADD: {
1270 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1271 // code and is matched first so to prevent it from being turned into
1272 // LEA32r X+c.
Evan Cheng17e39d62008-01-08 02:06:11 +00001273 // In 64-bit small code size mode, use LEA to take advantage of
1274 // RIP-relative addressing.
1275 if (TM.getCodeModel() != CodeModel::Small)
1276 break;
Duncan Sands92c43912008-06-06 12:08:01 +00001277 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001278 SDValue N0 = N.getOperand(0);
1279 SDValue N1 = N.getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001280 if (N.getNode()->getValueType(0) == PtrVT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 N0.getOpcode() == X86ISD::Wrapper &&
1282 N1.getOpcode() == ISD::Constant) {
1283 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00001284 SDValue C(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285 // TODO: handle ExternalSymbolSDNode.
1286 if (GlobalAddressSDNode *G =
1287 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
1288 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
1289 G->getOffset() + Offset);
1290 } else if (ConstantPoolSDNode *CP =
1291 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
1292 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
1293 CP->getAlignment(),
1294 CP->getOffset()+Offset);
1295 }
1296
Gabor Greif1c80d112008-08-28 21:40:38 +00001297 if (C.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001299 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif1c80d112008-08-28 21:40:38 +00001301 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r, MVT::i64, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 } else
Gabor Greif1c80d112008-08-28 21:40:38 +00001303 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 }
1305 }
1306
1307 // Other cases are handled by auto-generated code.
1308 break;
1309 }
1310
Dan Gohman5a199552007-10-08 18:33:35 +00001311 case ISD::SMUL_LOHI:
1312 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001313 SDValue N0 = Node->getOperand(0);
1314 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001315
Dan Gohman5a199552007-10-08 18:33:35 +00001316 bool isSigned = Opcode == ISD::SMUL_LOHI;
1317 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001318 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 default: assert(0 && "Unsupported VT!");
1320 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1321 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1322 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1323 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1324 }
1325 else
Duncan Sands92c43912008-06-06 12:08:01 +00001326 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 default: assert(0 && "Unsupported VT!");
1328 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1329 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1330 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1331 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1332 }
1333
1334 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001335 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 default: assert(0 && "Unsupported VT!");
1337 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1338 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1339 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1340 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1341 }
1342
Dan Gohman8181bd12008-07-27 21:46:04 +00001343 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001344 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001345 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 if (!foldedLoad) {
1347 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001348 if (foldedLoad)
1349 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 }
1351
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 AddToISelQueue(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1354 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355
1356 if (foldedLoad) {
Dan Gohman5a199552007-10-08 18:33:35 +00001357 AddToISelQueue(N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 AddToISelQueue(Tmp0);
1359 AddToISelQueue(Tmp1);
1360 AddToISelQueue(Tmp2);
1361 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001362 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 SDNode *CNode =
1364 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001365 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001366 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001367 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 } else {
1369 AddToISelQueue(N1);
1370 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001371 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 }
1373
Dan Gohman5a199552007-10-08 18:33:35 +00001374 // Copy the low half of the result, if it is needed.
1375 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001376 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001377 LoReg, NVT, InFlag);
1378 InFlag = Result.getValue(2);
1379 ReplaceUses(N.getValue(0), 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
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001385 }
Dan Gohman5a199552007-10-08 18:33:35 +00001386 // Copy the high half of the result, if it is needed.
1387 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001388 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001389 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1390 // Prevent use of AH in a REX instruction by referencing AX instead.
1391 // Shift it down 8 bits.
1392 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1393 X86::AX, MVT::i16, InFlag);
1394 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001395 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman5a199552007-10-08 18:33:35 +00001396 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1397 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001398 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1399 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001400 MVT::i8, Result, SRIdx), 0);
1401 } else {
1402 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1403 HiReg, NVT, InFlag);
1404 InFlag = Result.getValue(2);
1405 }
1406 ReplaceUses(N.getValue(1), Result);
1407#ifndef NDEBUG
1408 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001409 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001410 DOUT << "\n";
1411#endif
1412 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413
1414#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 Indent -= 2;
1416#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418 return NULL;
1419 }
1420
Dan Gohman5a199552007-10-08 18:33:35 +00001421 case ISD::SDIVREM:
1422 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001423 SDValue N0 = Node->getOperand(0);
1424 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001425
1426 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001428 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 default: assert(0 && "Unsupported VT!");
1430 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1431 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1432 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1433 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1434 }
1435 else
Duncan Sands92c43912008-06-06 12:08:01 +00001436 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 default: assert(0 && "Unsupported VT!");
1438 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1439 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1440 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1441 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1442 }
1443
1444 unsigned LoReg, HiReg;
1445 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001446 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 default: assert(0 && "Unsupported VT!");
1448 case MVT::i8:
1449 LoReg = X86::AL; HiReg = X86::AH;
1450 ClrOpcode = 0;
1451 SExtOpcode = X86::CBW;
1452 break;
1453 case MVT::i16:
1454 LoReg = X86::AX; HiReg = X86::DX;
1455 ClrOpcode = X86::MOV16r0;
1456 SExtOpcode = X86::CWD;
1457 break;
1458 case MVT::i32:
1459 LoReg = X86::EAX; HiReg = X86::EDX;
1460 ClrOpcode = X86::MOV32r0;
1461 SExtOpcode = X86::CDQ;
1462 break;
1463 case MVT::i64:
1464 LoReg = X86::RAX; HiReg = X86::RDX;
1465 ClrOpcode = X86::MOV64r0;
1466 SExtOpcode = X86::CQO;
1467 break;
1468 }
1469
Dan Gohman8181bd12008-07-27 21:46:04 +00001470 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001471 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1472
Dan Gohman8181bd12008-07-27 21:46:04 +00001473 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 if (NVT == MVT::i8 && !isSigned) {
1475 // Special case for div8, just use a move with zero extension to AX to
1476 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001479 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001480 AddToISelQueue(N0.getOperand(0));
1481 AddToISelQueue(Tmp0);
1482 AddToISelQueue(Tmp1);
1483 AddToISelQueue(Tmp2);
1484 AddToISelQueue(Tmp3);
1485 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001486 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 Ops, 5), 0);
1488 Chain = Move.getValue(1);
1489 ReplaceUses(N0.getValue(1), Chain);
1490 } else {
1491 AddToISelQueue(N0);
1492 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001493 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 Chain = CurDAG->getEntryNode();
1495 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001496 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 InFlag = Chain.getValue(1);
1498 } else {
1499 AddToISelQueue(N0);
1500 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001501 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001502 LoReg, N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 if (isSigned) {
1504 // Sign extend the low part into the high part.
1505 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001506 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 } else {
1508 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001509 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001510 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1511 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 }
1513 }
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 if (foldedLoad) {
1516 AddToISelQueue(N1.getOperand(0));
1517 AddToISelQueue(Tmp0);
1518 AddToISelQueue(Tmp1);
1519 AddToISelQueue(Tmp2);
1520 AddToISelQueue(Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00001521 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 SDNode *CNode =
1523 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001524 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001525 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001526 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 } else {
1528 AddToISelQueue(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001530 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 }
1532
Dan Gohman242a5ba2007-09-25 18:23:27 +00001533 // Copy the division (low) result, if it is needed.
1534 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001535 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001536 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001537 InFlag = Result.getValue(2);
1538 ReplaceUses(N.getValue(0), Result);
1539#ifndef NDEBUG
1540 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001541 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001542 DOUT << "\n";
1543#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001544 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001545 // Copy the remainder (high) result, if it is needed.
1546 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001547 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001548 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1549 // Prevent use of AH in a REX instruction by referencing AX instead.
1550 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001551 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1552 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001553 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001554 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001555 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1556 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001557 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1558 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001559 MVT::i8, Result, SRIdx), 0);
1560 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001561 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1562 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001563 InFlag = Result.getValue(2);
1564 }
1565 ReplaceUses(N.getValue(1), Result);
1566#ifndef NDEBUG
1567 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001568 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001569 DOUT << "\n";
1570#endif
1571 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572
1573#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 Indent -= 2;
1575#endif
1576
1577 return NULL;
1578 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001579
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001580 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001581 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001582 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1583 SDValue N0 = Node->getOperand(0);
1584 AddToISelQueue(N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001585
Dan Gohmandd612bb2008-08-20 21:27:32 +00001586 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1587 unsigned Opc = 0;
1588 switch (NVT.getSimpleVT()) {
1589 default: assert(0 && "Unknown sign_extend_inreg!");
1590 case MVT::i16:
1591 Opc = X86::MOVSX16rr8;
1592 break;
1593 case MVT::i32:
1594 Opc = X86::MOVSX32rr8;
1595 break;
1596 }
1597
1598 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001599
1600#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001601 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001602 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001603 DOUT << "\n";
1604 DOUT << std::string(Indent-2, ' ') << "=> ";
1605 DEBUG(ResNode->dump(CurDAG));
1606 DOUT << "\n";
1607 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001608#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001609 return ResNode;
1610 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001611 break;
1612 }
1613
1614 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001615 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1616 SDValue Input = Node->getOperand(0);
1617 AddToISelQueue(Node->getOperand(0));
1618 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001619
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620#ifndef NDEBUG
1621 DOUT << std::string(Indent-2, ' ') << "=> ";
1622 DEBUG(ResNode->dump(CurDAG));
1623 DOUT << "\n";
1624 Indent -= 2;
1625#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001626 return ResNode;
1627 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 break;
1629 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001630
1631 case ISD::DECLARE: {
1632 // Handle DECLARE nodes here because the second operand may have been
1633 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001634 SDValue Chain = Node->getOperand(0);
1635 SDValue N1 = Node->getOperand(1);
1636 SDValue N2 = Node->getOperand(2);
Evan Cheng651e1442008-06-18 02:48:27 +00001637 if (!isa<FrameIndexSDNode>(N1))
1638 break;
1639 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1640 if (N2.getOpcode() == ISD::ADD &&
1641 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1642 N2 = N2.getOperand(1);
1643 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Chengd4cebcd2008-06-17 02:01:22 +00001644 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Chengd4cebcd2008-06-17 02:01:22 +00001645 GlobalValue *GV =
1646 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1648 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Chengd4cebcd2008-06-17 02:01:22 +00001649 AddToISelQueue(Chain);
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Chengd4cebcd2008-06-17 02:01:22 +00001651 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1652 MVT::Other, Ops, 3);
1653 }
1654 break;
1655 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656 }
1657
1658 SDNode *ResNode = SelectCode(N);
1659
1660#ifndef NDEBUG
1661 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001662 if (ResNode == NULL || ResNode == N.getNode())
1663 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 else
1665 DEBUG(ResNode->dump(CurDAG));
1666 DOUT << "\n";
1667 Indent -= 2;
1668#endif
1669
1670 return ResNode;
1671}
1672
1673bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001674SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001675 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001676 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001677 switch (ConstraintCode) {
1678 case 'o': // offsetable ??
1679 case 'v': // not offsetable ??
1680 default: return true;
1681 case 'm': // memory
1682 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1683 return true;
1684 break;
1685 }
1686
1687 OutOps.push_back(Op0);
1688 OutOps.push_back(Op1);
1689 OutOps.push_back(Op2);
1690 OutOps.push_back(Op3);
1691 AddToISelQueue(Op0);
1692 AddToISelQueue(Op1);
1693 AddToISelQueue(Op2);
1694 AddToISelQueue(Op3);
1695 return false;
1696}
1697
1698/// createX86ISelDag - This pass converts a legalized DAG into a
1699/// X86-specific DAG, ready for instruction scheduling.
1700///
1701FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1702 return new X86DAGToDAGISel(TM, Fast);
1703}