blob: e6c344890dcf1f40de207c85717bc5dc649ae713 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Cheng13559d62008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Compiler.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
51 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
57 FrameIndexBase
58 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 int FrameIndex;
63 } Base;
64
Evan Cheng3b5a1272008-02-07 08:53:49 +000065 bool isRIPRel; // RIP as base?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000067 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000068 int32_t Disp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 GlobalValue *GV;
70 Constant *CP;
71 const char *ES;
72 int JT;
73 unsigned Align; // CP alignment.
74
75 X86ISelAddressMode()
76 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
78 }
Dale Johannesenc501c082008-08-11 23:46:25 +000079 void dump() {
80 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000081 cerr << "Base.Reg ";
82 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
83 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000084 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
85 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +000086 cerr << "IndexReg ";
87 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
88 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +000089 cerr << " Disp " << Disp << "\n";
90 cerr << "GV "; if (GV) GV->dump();
91 else cerr << "nul";
92 cerr << " CP "; if (CP) CP->dump();
93 else cerr << "nul";
94 cerr << "\n";
95 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
96 cerr << " JT" << JT << " Align" << Align << "\n";
97 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 };
99}
100
101namespace {
102 //===--------------------------------------------------------------------===//
103 /// ISel - X86 specific code to select X86 machine instructions for
104 /// SelectionDAG operations.
105 ///
106 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 /// TM - Keep a reference to X86TargetMachine.
108 ///
109 X86TargetMachine &TM;
110
111 /// X86Lowering - This object fully describes how to lower LLVM code to an
112 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000113 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
116 /// make the right decision when generating code for different targets.
117 const X86Subtarget *Subtarget;
118
Evan Cheng34fd4f32008-06-30 20:45:06 +0000119 /// CurBB - Current BB being isel'd.
120 ///
121 MachineBasicBlock *CurBB;
122
Evan Cheng13559d62008-09-26 23:41:32 +0000123 /// OptForSize - If true, selector should try to optimize for code size
124 /// instead of performance.
125 bool OptForSize;
126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 public:
128 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohmanf2b29572008-10-03 16:55:19 +0000129 : SelectionDAGISel(*tm.getTargetLowering(), fast),
Dan Gohman61ad8642008-10-03 16:17:33 +0000130 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Cheng13559d62008-09-26 23:41:32 +0000131 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000132 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 virtual const char *getPassName() const {
135 return "X86 DAG->DAG Instruction Selection";
136 }
137
Evan Cheng34fd4f32008-06-30 20:45:06 +0000138 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000140 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000141
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000142 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
143
Evan Cheng5a424552008-11-27 00:49:46 +0000144 virtual
145 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147// Include the pieces autogenerated from the target description.
148#include "X86GenDAGISel.inc"
149
150 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000151 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000152 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 bool isRoot = true, unsigned Depth = 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000157 bool isRoot, unsigned Depth);
Dan Gohman8181bd12008-07-27 21:46:04 +0000158 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
159 SDValue &Scale, SDValue &Index, SDValue &Disp);
160 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
161 SDValue &Scale, SDValue &Index, SDValue &Disp);
162 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
163 SDValue N, SDValue &Base, SDValue &Scale,
164 SDValue &Index, SDValue &Disp,
165 SDValue &InChain, SDValue &OutChain);
166 bool TryFoldLoad(SDValue P, SDValue N,
167 SDValue &Base, SDValue &Scale,
168 SDValue &Index, SDValue &Disp);
Dan Gohman14a66442008-08-23 02:25:05 +0000169 void PreprocessForRMW();
170 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
172 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
173 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000174 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000176 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000178 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
179
Dan Gohman8181bd12008-07-27 21:46:04 +0000180 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
181 SDValue &Scale, SDValue &Index,
182 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
184 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
185 AM.Base.Reg;
186 Scale = getI8Imm(AM.Scale);
187 Index = AM.IndexReg;
188 // These are 32-bit even in 64-bit mode since RIP relative offset
189 // is 32-bit.
190 if (AM.GV)
191 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
192 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000193 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
194 AM.Align, AM.Disp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 else if (AM.ES)
Bill Wendlingfef06052008-09-16 21:48:12 +0000196 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 else if (AM.JT != -1)
198 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
199 else
Dan Gohman0bd76b72008-11-11 15:52:29 +0000200 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 }
202
203 /// getI8Imm - Return a target constant with the specified value, of type
204 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000205 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return CurDAG->getTargetConstant(Imm, MVT::i8);
207 }
208
209 /// getI16Imm - Return a target constant with the specified value, of type
210 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 return CurDAG->getTargetConstant(Imm, MVT::i16);
213 }
214
215 /// getI32Imm - Return a target constant with the specified value, of type
216 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000217 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i32);
219 }
220
Dan Gohmanb60482f2008-09-23 18:22:58 +0000221 /// getGlobalBaseReg - Return an SDNode that returns the value of
222 /// the global base register. Output instructions required to
223 /// initialize the global base register, if necessary.
224 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 SDNode *getGlobalBaseReg();
226
Dan Gohmandd612bb2008-08-20 21:27:32 +0000227 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
228 /// truncate of the specified operand to i8. This can be done with tablegen,
229 /// except that this code uses MVT::Flag in a tricky way that happens to
230 /// improve scheduling in some cases.
231 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233#ifndef NDEBUG
234 unsigned Indent;
235#endif
236 };
237}
238
Gabor Greife9f7f582008-08-31 15:37:04 +0000239/// findFlagUse - Return use of MVT::Flag value produced by the specified
240/// SDNode.
Evan Cheng656269e2008-04-25 08:22:20 +0000241///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242static SDNode *findFlagUse(SDNode *N) {
243 unsigned FlagResNo = N->getNumValues()-1;
244 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000245 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000247 SDValue Op = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +0000248 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 return User;
250 }
251 }
252 return NULL;
253}
254
Evan Cheng656269e2008-04-25 08:22:20 +0000255/// findNonImmUse - Return true by reference in "found" if "Use" is an
256/// non-immediate use of "Def". This function recursively traversing
257/// up the operand chain ignoring certain nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman602d44a2008-09-17 01:39:10 +0000259 SDNode *Root, bool &found,
Evan Cheng656269e2008-04-25 08:22:20 +0000260 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 if (found ||
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000262 Use->getNodeId() < Def->getNodeId() ||
Evan Cheng656269e2008-04-25 08:22:20 +0000263 !Visited.insert(Use))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 return;
Evan Cheng656269e2008-04-25 08:22:20 +0000265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000267 SDNode *N = Use->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (N == Def) {
Dan Gohman602d44a2008-09-17 01:39:10 +0000269 if (Use == ImmedUse || Use == Root)
Evan Cheng9ea310c2008-04-25 08:55:28 +0000270 continue; // We are not looking for immediate use.
Dan Gohman602d44a2008-09-17 01:39:10 +0000271 assert(N != Root);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 found = true;
273 break;
274 }
Evan Cheng656269e2008-04-25 08:22:20 +0000275
276 // Traverse up the operand chain.
Dan Gohman602d44a2008-09-17 01:39:10 +0000277 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 }
279}
280
281/// isNonImmUse - Start searching from Root up the DAG to check is Def can
282/// be reached. Return true if that's the case. However, ignore direct uses
283/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng5a424552008-11-27 00:49:46 +0000284/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
285/// case).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286/// FIXME: to be really generic, we should allow direct use by any node
287/// that is being folded. But realisticly since we only fold loads which
288/// have one non-chain use, we only need to watch out for load/op/store
289/// and load/op/cmp case where the root (store / cmp) may reach the load via
290/// its chain operand.
Dan Gohman602d44a2008-09-17 01:39:10 +0000291static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Cheng656269e2008-04-25 08:22:20 +0000292 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 bool found = false;
Dan Gohman602d44a2008-09-17 01:39:10 +0000294 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 return found;
296}
297
298
Evan Cheng5a424552008-11-27 00:49:46 +0000299bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
300 SDNode *Root) const {
Dan Gohmana29efcf2008-08-13 19:55:00 +0000301 if (Fast) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Evan Cheng5a424552008-11-27 00:49:46 +0000303 if (U == Root)
304 switch (U->getOpcode()) {
305 default: break;
306 case ISD::ADD:
307 case ISD::ADDC:
308 case ISD::ADDE:
309 case ISD::AND:
310 case ISD::OR:
311 case ISD::XOR: {
312 // If the other operand is a 8-bit immediate we should fold the immediate
313 // instead. This reduces code size.
314 // e.g.
315 // movl 4(%esp), %eax
316 // addl $4, %eax
317 // vs.
318 // movl $4, %eax
319 // addl 4(%esp), %eax
320 // The former is 2 bytes shorter. In case where the increment is 1, then
321 // the saving can be 4 bytes (by using incl %eax).
322 ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1));
323 if (Imm) {
324 if (U->getValueType(0) == MVT::i64) {
325 if ((int32_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
326 return false;
327 } else {
328 if ((int8_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
329 return false;
330 }
331 }
332 }
333 }
334
Dan Gohman602d44a2008-09-17 01:39:10 +0000335 // If Root use can somehow reach N through a path that that doesn't contain
336 // U then folding N would create a cycle. e.g. In the following
337 // diagram, Root can reach N through X. If N is folded into into Root, then
338 // X is both a predecessor and a successor of U.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 //
Dan Gohman602d44a2008-09-17 01:39:10 +0000340 // [N*] //
341 // ^ ^ //
342 // / \ //
343 // [U*] [X]? //
344 // ^ ^ //
345 // \ / //
346 // \ / //
347 // [Root*] //
348 //
349 // * indicates nodes to be folded together.
350 //
351 // If Root produces a flag, then it gets (even more) interesting. Since it
352 // will be "glued" together with its flag use in the scheduler, we need to
353 // check if it might reach N.
354 //
355 // [N*] //
356 // ^ ^ //
357 // / \ //
358 // [U*] [X]? //
359 // ^ ^ //
360 // \ \ //
361 // \ | //
362 // [Root*] | //
363 // ^ | //
364 // f | //
365 // | / //
366 // [Y] / //
367 // ^ / //
368 // f / //
369 // | / //
370 // [FU] //
371 //
372 // If FU (flag use) indirectly reaches N (the load), and Root folds N
373 // (call it Fold), then X is a predecessor of FU and a successor of
374 // Fold. But since Fold and FU are flagged together, this will create
375 // a cycle in the scheduling graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
Duncan Sands92c43912008-06-06 12:08:01 +0000377 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman602d44a2008-09-17 01:39:10 +0000378 while (VT == MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 SDNode *FU = findFlagUse(Root);
380 if (FU == NULL)
381 break;
Dan Gohman602d44a2008-09-17 01:39:10 +0000382 Root = FU;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 VT = Root->getValueType(Root->getNumValues()-1);
384 }
385
Dan Gohman602d44a2008-09-17 01:39:10 +0000386 return !isNonImmUse(Root, N, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387}
388
389/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
390/// and move load below the TokenFactor. Replace store's chain operand with
391/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000392static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000393 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000394 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000395 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
396 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000397 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000399 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000400 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
401 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
402 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
403 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404}
405
Evan Cheng2b2a7012008-05-23 21:23:16 +0000406/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
407///
Dan Gohman8181bd12008-07-27 21:46:04 +0000408static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
409 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000410 if (N.getOpcode() == ISD::BIT_CONVERT)
411 N = N.getOperand(0);
412
413 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
414 if (!LD || LD->isVolatile())
415 return false;
416 if (LD->getAddressingMode() != ISD::UNINDEXED)
417 return false;
418
419 ISD::LoadExtType ExtType = LD->getExtensionType();
420 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
421 return false;
422
423 if (N.hasOneUse() &&
424 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000425 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000426 Load = N;
427 return true;
428 }
429 return false;
430}
431
Evan Cheng98cfaf82008-08-25 21:27:18 +0000432/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
433/// operand and move load below the call's chain operand.
434static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
435 SDValue Call, SDValue Chain) {
436 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000437 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
438 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000439 Ops.push_back(Load.getOperand(0));
440 else
441 Ops.push_back(Chain.getOperand(i));
442 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
443 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
444 Load.getOperand(1), Load.getOperand(2));
445 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000446 Ops.push_back(SDValue(Load.getNode(), 1));
447 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000448 Ops.push_back(Call.getOperand(i));
449 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
450}
451
452/// isCalleeLoad - Return true if call address is a load and it can be
453/// moved below CALLSEQ_START and the chains leading up to the call.
454/// Return the CALLSEQ_START by reference as a second output.
455static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000456 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000457 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000458 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000459 if (!LD ||
460 LD->isVolatile() ||
461 LD->getAddressingMode() != ISD::UNINDEXED ||
462 LD->getExtensionType() != ISD::NON_EXTLOAD)
463 return false;
464
465 // Now let's find the callseq_start.
466 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
467 if (!Chain.hasOneUse())
468 return false;
469 Chain = Chain.getOperand(0);
470 }
Gabor Greif1c80d112008-08-28 21:40:38 +0000471 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Cheng98cfaf82008-08-25 21:27:18 +0000472}
473
474
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000475/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
476/// This is only run if not in -fast mode (aka -O0).
477/// This allows the instruction selector to pick more read-modify-write
478/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479///
480/// [Load chain]
481/// ^
482/// |
483/// [Load]
484/// ^ ^
485/// | |
486/// / \-
487/// / |
488/// [TokenFactor] [Op]
489/// ^ ^
490/// | |
491/// \ /
492/// \ /
493/// [Store]
494///
495/// The fact the store's chain operand != load's chain will prevent the
496/// (store (op (load))) instruction from being selected. We can transform it to:
497///
498/// [Load chain]
499/// ^
500/// |
501/// [TokenFactor]
502/// ^
503/// |
504/// [Load]
505/// ^ ^
506/// | |
507/// | \-
508/// | |
509/// | [Op]
510/// | ^
511/// | |
512/// \ /
513/// \ /
514/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000515void X86DAGToDAGISel::PreprocessForRMW() {
516 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
517 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000518 if (I->getOpcode() == X86ISD::CALL) {
519 /// Also try moving call address load from outside callseq_start to just
520 /// before the call to allow it to be folded.
521 ///
522 /// [Load chain]
523 /// ^
524 /// |
525 /// [Load]
526 /// ^ ^
527 /// | |
528 /// / \--
529 /// / |
530 ///[CALLSEQ_START] |
531 /// ^ |
532 /// | |
533 /// [LOAD/C2Reg] |
534 /// | |
535 /// \ /
536 /// \ /
537 /// [CALL]
538 SDValue Chain = I->getOperand(0);
539 SDValue Load = I->getOperand(1);
540 if (!isCalleeLoad(Load, Chain))
541 continue;
542 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
543 ++NumLoadMoved;
544 continue;
545 }
546
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 if (!ISD::isNON_TRUNCStore(I))
548 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000549 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000550
Gabor Greif1c80d112008-08-28 21:40:38 +0000551 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 continue;
553
Dan Gohman8181bd12008-07-27 21:46:04 +0000554 SDValue N1 = I->getOperand(1);
555 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000556 if ((N1.getValueType().isFloatingPoint() &&
557 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 !N1.hasOneUse())
559 continue;
560
561 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000562 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000563 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000565 case ISD::ADD:
566 case ISD::MUL:
567 case ISD::AND:
568 case ISD::OR:
569 case ISD::XOR:
570 case ISD::ADDC:
571 case ISD::ADDE:
572 case ISD::VECTOR_SHUFFLE: {
573 SDValue N10 = N1.getOperand(0);
574 SDValue N11 = N1.getOperand(1);
575 RModW = isRMWLoad(N10, Chain, N2, Load);
576 if (!RModW)
577 RModW = isRMWLoad(N11, Chain, N2, Load);
578 break;
579 }
580 case ISD::SUB:
581 case ISD::SHL:
582 case ISD::SRA:
583 case ISD::SRL:
584 case ISD::ROTL:
585 case ISD::ROTR:
586 case ISD::SUBC:
587 case ISD::SUBE:
588 case X86ISD::SHLD:
589 case X86ISD::SHRD: {
590 SDValue N10 = N1.getOperand(0);
591 RModW = isRMWLoad(N10, Chain, N2, Load);
592 break;
593 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 }
595
596 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000597 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 ++NumLoadMoved;
599 }
600 }
601}
602
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000603
604/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
605/// nodes that target the FP stack to be store and load to the stack. This is a
606/// gross hack. We would like to simply mark these as being illegal, but when
607/// we do that, legalize produces these when it expands calls, then expands
608/// these in the same legalize pass. We would like dag combine to be able to
609/// hack on these between the call expansion and the node legalization. As such
610/// this pass basically does "really late" legalization of these inline with the
611/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000612void X86DAGToDAGISel::PreprocessForFPConvert() {
613 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
614 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000615 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
616 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
617 continue;
618
619 // If the source and destination are SSE registers, then this is a legal
620 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000621 MVT SrcVT = N->getOperand(0).getValueType();
622 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000623 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
624 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
625 if (SrcIsSSE && DstIsSSE)
626 continue;
627
Chris Lattner5d294e52008-03-09 07:05:32 +0000628 if (!SrcIsSSE && !DstIsSSE) {
629 // If this is an FPStack extension, it is a noop.
630 if (N->getOpcode() == ISD::FP_EXTEND)
631 continue;
632 // If this is a value-preserving FPStack truncation, it is a noop.
633 if (N->getConstantOperandVal(1))
634 continue;
635 }
636
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000637 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
638 // FPStack has extload and truncstore. SSE can fold direct loads into other
639 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000640 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000641 if (N->getOpcode() == ISD::FP_ROUND)
642 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
643 else
644 MemVT = SrcIsSSE ? SrcVT : DstVT;
645
Dan Gohman14a66442008-08-23 02:25:05 +0000646 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000647
648 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman14a66442008-08-23 02:25:05 +0000649 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
650 N->getOperand(0),
651 MemTmp, NULL, 0, MemVT);
652 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
653 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000654
655 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
656 // extload we created. This will cause general havok on the dag because
657 // anything below the conversion could be folded into other existing nodes.
658 // To avoid invalidating 'I', back it up to the convert node.
659 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000660 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000661
662 // Now that we did that, the node is dead. Increment the iterator to the
663 // next node to process, then delete N.
664 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000665 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000666 }
667}
668
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
670/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000671void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000672 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000673 const Function *F = CurDAG->getMachineFunction().getFunction();
674 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
Evan Cheng34fd4f32008-06-30 20:45:06 +0000676 DEBUG(BB->dump());
Dan Gohmana29efcf2008-08-13 19:55:00 +0000677 if (!Fast)
Dan Gohman14a66442008-08-23 02:25:05 +0000678 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000679
680 // FIXME: This should only happen when not -fast.
Dan Gohman14a66442008-08-23 02:25:05 +0000681 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682
683 // Codegen the basic block.
684#ifndef NDEBUG
685 DOUT << "===== Instruction selection begins:\n";
686 Indent = 0;
687#endif
David Greene932618b2008-10-27 21:56:29 +0000688 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689#ifndef NDEBUG
690 DOUT << "===== Instruction selection ends:\n";
691#endif
692
Dan Gohman14a66442008-08-23 02:25:05 +0000693 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000694}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000696/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
697/// the main function.
698void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
699 MachineFrameInfo *MFI) {
700 const TargetInstrInfo *TII = TM.getInstrInfo();
701 if (Subtarget->isTargetCygMing())
702 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
703}
704
705void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
706 // If this is main, emit special code for main.
707 MachineBasicBlock *BB = MF.begin();
708 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
709 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
710}
711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712/// MatchAddress - Add the specified node to the specified addressing mode,
713/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000714/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000715bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 bool isRoot, unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000717 bool is64Bit = Subtarget->is64Bit();
Evan Cheng7f250d62008-09-24 00:05:32 +0000718 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000719 // Limit recursion.
720 if (Depth > 5)
721 return MatchAddressBase(N, AM, isRoot, Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722
723 // RIP relative addressing: %rip + 32-bit displacement!
724 if (AM.isRIPRel) {
725 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000726 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000727 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 AM.Disp += Val;
729 return false;
730 }
731 }
732 return true;
733 }
734
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 switch (N.getOpcode()) {
736 default: break;
737 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000738 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000739 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 AM.Disp += Val;
741 return false;
742 }
743 break;
744 }
745
746 case X86ISD::Wrapper: {
Dan Gohman36322c72008-10-18 02:06:02 +0000747 DOUT << "Wrapper: 64bit " << is64Bit;
748 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng3b5a1272008-02-07 08:53:49 +0000750 // Also, base and index reg must be 0 in order to use rip as base.
751 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000752 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 break;
754 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
755 break;
756 // If value is available in a register both base and index components have
757 // been picked, we can't fit the result available in the register in the
758 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohmancc3df852008-11-05 04:14:16 +0000759 {
Dan Gohman8181bd12008-07-27 21:46:04 +0000760 SDValue N0 = N.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000762 uint64_t Offset = G->getOffset();
763 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000764 GlobalValue *GV = G->getGlobal();
765 AM.GV = GV;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000766 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000767 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
768 return false;
769 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000771 uint64_t Offset = CP->getOffset();
772 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +0000773 AM.CP = CP->getConstVal();
774 AM.Align = CP->getAlignment();
Dan Gohman0bd76b72008-11-11 15:52:29 +0000775 AM.Disp += Offset;
Dan Gohman36322c72008-10-18 02:06:02 +0000776 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
777 return false;
778 }
Bill Wendlingfef06052008-09-16 21:48:12 +0000779 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000780 AM.ES = S->getSymbol();
Dan Gohmanc6413362008-09-26 19:15:30 +0000781 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000782 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng3b5a1272008-02-07 08:53:49 +0000784 AM.JT = J->getIndex();
Dan Gohmanc6413362008-09-26 19:15:30 +0000785 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Cheng3b5a1272008-02-07 08:53:49 +0000786 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 }
788 }
789 break;
790 }
791
792 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000793 if (AM.BaseType == X86ISelAddressMode::RegBase
794 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
796 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
797 return false;
798 }
799 break;
800
801 case ISD::SHL:
Dan Gohmancc3df852008-11-05 04:14:16 +0000802 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000803 break;
804
Gabor Greife9f7f582008-08-31 15:37:04 +0000805 if (ConstantSDNode
806 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000807 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000808 if (Val == 1 || Val == 2 || Val == 3) {
809 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000810 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811
Chris Lattner7f06edd2007-12-08 07:22:58 +0000812 // Okay, we know that we have a scale by now. However, if the scaled
813 // value is an add of something and a constant, we can fold the
814 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000815 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
816 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
817 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000818 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000819 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000820 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000821 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000822 AM.Disp = Disp;
823 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000825 } else {
826 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000828 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 }
830 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832
Dan Gohman35b99222007-10-22 20:22:24 +0000833 case ISD::SMUL_LOHI:
834 case ISD::UMUL_LOHI:
835 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000836 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000837 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 case ISD::MUL:
839 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000840 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000841 AM.Base.Reg.getNode() == 0 &&
842 AM.IndexReg.getNode() == 0 &&
Evan Cheng3b5a1272008-02-07 08:53:49 +0000843 !AM.isRIPRel) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000844 if (ConstantSDNode
845 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000846 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
847 CN->getZExtValue() == 9) {
848 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849
Gabor Greif1c80d112008-08-28 21:40:38 +0000850 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000851 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852
853 // Okay, we know that we have a scale by now. However, if the scaled
854 // value is an add of something and a constant, we can fold the
855 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000856 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
857 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
858 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000860 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000861 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
862 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000863 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 AM.Disp = Disp;
865 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000866 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000868 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 }
870
871 AM.IndexReg = AM.Base.Reg = Reg;
872 return false;
873 }
874 }
875 break;
876
877 case ISD::ADD:
Dan Gohmancc3df852008-11-05 04:14:16 +0000878 {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 X86ISelAddressMode Backup = AM;
Gabor Greif1c80d112008-08-28 21:40:38 +0000880 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
881 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 return false;
883 AM = Backup;
Gabor Greif1c80d112008-08-28 21:40:38 +0000884 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
885 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 return false;
887 AM = Backup;
888 }
889 break;
890
891 case ISD::OR:
892 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000893 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
894 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +0000895 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000896 // Start with the LHS as an addr mode.
897 if (!MatchAddress(N.getOperand(0), AM, false) &&
898 // Address could not have picked a GV address for the displacement.
899 AM.GV == NULL &&
900 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman0bd76b72008-11-11 15:52:29 +0000901 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +0000902 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +0000903 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000904 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000905 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000907 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 }
909 break;
Evan Chengf2abee72007-12-13 00:43:27 +0000910
911 case ISD::AND: {
912 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
913 // allows us to fold the shift into this addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000914 SDValue Shift = N.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +0000915 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohmancc3df852008-11-05 04:14:16 +0000916
Evan Chengf2abee72007-12-13 00:43:27 +0000917 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +0000918 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +0000919
920 // Not when RIP is used as the base.
921 if (AM.isRIPRel) break;
Evan Chengf2abee72007-12-13 00:43:27 +0000922
923 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
924 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
925 if (!C1 || !C2) break;
926
927 // Not likely to be profitable if either the AND or SHIFT node has more
928 // than one use (unless all uses are for address computation). Besides,
929 // isel mechanism requires their node ids to be reused.
930 if (!N.hasOneUse() || !Shift.hasOneUse())
931 break;
932
933 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000934 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +0000935 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
936 break;
937
938 // Get the new AND mask, this folds to a constant.
Dan Gohmancc3df852008-11-05 04:14:16 +0000939 SDValue X = Shift.getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000940 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +0000941 SDValue(C2, 0), SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000942 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), X, NewANDMask);
Dan Gohman3666f472008-10-13 20:52:04 +0000943 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(),
944 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +0000945
946 // Insert the new nodes into the topological ordering.
947 if (C1->getNodeId() > X.getNode()->getNodeId()) {
948 CurDAG->RepositionNode(X.getNode(), C1);
949 C1->setNodeId(X.getNode()->getNodeId());
950 }
951 if (NewANDMask.getNode()->getNodeId() == -1 ||
952 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
953 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
954 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
955 }
956 if (NewAND.getNode()->getNodeId() == -1 ||
957 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
958 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
959 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
960 }
961 if (NewSHIFT.getNode()->getNodeId() == -1 ||
962 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
963 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
964 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
965 }
966
Dan Gohman3666f472008-10-13 20:52:04 +0000967 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +0000968
969 AM.Scale = 1 << ShiftCst;
970 AM.IndexReg = NewAND;
971 return false;
972 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973 }
974
Dan Gohmana60c1b32007-08-13 20:03:06 +0000975 return MatchAddressBase(N, AM, isRoot, Depth);
976}
977
978/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
979/// specified addressing mode without any further recursion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000980bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmana60c1b32007-08-13 20:03:06 +0000981 bool isRoot, unsigned Depth) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +0000983 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 // If so, check to see if the scale index register is set.
Gabor Greif1c80d112008-08-28 21:40:38 +0000985 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 AM.IndexReg = N;
987 AM.Scale = 1;
988 return false;
989 }
990
991 // Otherwise, we cannot select it.
992 return true;
993 }
994
995 // Default, generate it as a register.
996 AM.BaseType = X86ISelAddressMode::RegBase;
997 AM.Base.Reg = N;
998 return false;
999}
1000
1001/// SelectAddr - returns true if it is able pattern match an addressing mode.
1002/// It returns the operands which make up the maximal addressing mode it can
1003/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001004bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1005 SDValue &Scale, SDValue &Index,
1006 SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 X86ISelAddressMode AM;
1008 if (MatchAddress(N, AM))
1009 return false;
1010
Duncan Sands92c43912008-06-06 12:08:01 +00001011 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001013 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 AM.Base.Reg = CurDAG->getRegister(0, VT);
1015 }
1016
Gabor Greif1c80d112008-08-28 21:40:38 +00001017 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 AM.IndexReg = CurDAG->getRegister(0, VT);
1019
1020 getAddressOperands(AM, Base, Scale, Index, Disp);
1021 return true;
1022}
1023
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1025/// match a load whose top elements are either undef or zeros. The load flavor
1026/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001027bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1028 SDValue N, SDValue &Base,
1029 SDValue &Scale, SDValue &Index,
1030 SDValue &Disp, SDValue &InChain,
1031 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1033 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001034 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 InChain.getValue(0).hasOneUse() &&
1036 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001037 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 LoadSDNode *LD = cast<LoadSDNode>(InChain);
1039 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1040 return false;
1041 OutChain = LD->getChain();
1042 return true;
1043 }
1044 }
1045
1046 // Also handle the case where we explicitly require zeros in the top
1047 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001048 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001049 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001050 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001051 N.getOperand(0).getNode()->hasOneUse() &&
1052 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001053 N.getOperand(0).getOperand(0).hasOneUse()) {
1054 // Okay, this is a zero extending load. Fold it.
1055 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1056 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1057 return false;
1058 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001059 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001060 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 }
1062 return false;
1063}
1064
1065
1066/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1067/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001068bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1069 SDValue &Base, SDValue &Scale,
1070 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 X86ISelAddressMode AM;
1072 if (MatchAddress(N, AM))
1073 return false;
1074
Duncan Sands92c43912008-06-06 12:08:01 +00001075 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 unsigned Complexity = 0;
1077 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001078 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 Complexity = 1;
1080 else
1081 AM.Base.Reg = CurDAG->getRegister(0, VT);
1082 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1083 Complexity = 4;
1084
Gabor Greif1c80d112008-08-28 21:40:38 +00001085 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 Complexity++;
1087 else
1088 AM.IndexReg = CurDAG->getRegister(0, VT);
1089
1090 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1091 // a simple shift.
1092 if (AM.Scale > 1)
1093 Complexity++;
1094
1095 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1096 // to a LEA. This is determined with some expermentation but is by no means
1097 // optimal (especially for code size consideration). LEA is nice because of
1098 // its three-address nature. Tweak the cost function again when we can run
1099 // convertToThreeAddress() at register allocation time.
1100 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1101 // For X86-64, we should always use lea to materialize RIP relative
1102 // addresses.
1103 if (Subtarget->is64Bit())
1104 Complexity = 4;
1105 else
1106 Complexity += 2;
1107 }
1108
Gabor Greif1c80d112008-08-28 21:40:38 +00001109 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 Complexity++;
1111
1112 if (Complexity > 2) {
1113 getAddressOperands(AM, Base, Scale, Index, Disp);
1114 return true;
1115 }
1116 return false;
1117}
1118
Dan Gohman8181bd12008-07-27 21:46:04 +00001119bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1120 SDValue &Base, SDValue &Scale,
1121 SDValue &Index, SDValue &Disp) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001122 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001124 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
1126 return false;
1127}
1128
Dan Gohmanb60482f2008-09-23 18:22:58 +00001129/// getGlobalBaseReg - Return an SDNode that returns the value of
1130/// the global base register. Output instructions required to
1131/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132///
1133SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001134 MachineFunction *MF = CurBB->getParent();
1135 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001136 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137}
1138
1139static SDNode *FindCallStartFromCall(SDNode *Node) {
1140 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1141 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1142 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001143 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144}
1145
Dan Gohmandd612bb2008-08-20 21:27:32 +00001146/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1147/// truncate of the specified operand to i8. This can be done with tablegen,
1148/// except that this code uses MVT::Flag in a tricky way that happens to
1149/// improve scheduling in some cases.
1150SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1151 assert(!Subtarget->is64Bit() &&
1152 "getTruncateTo8Bit is only needed on x86-32!");
1153 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1154
1155 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1156 unsigned Opc;
1157 MVT N0VT = N0.getValueType();
1158 switch (N0VT.getSimpleVT()) {
1159 default: assert(0 && "Unknown truncate!");
1160 case MVT::i16:
1161 Opc = X86::MOV16to16_;
1162 break;
1163 case MVT::i32:
1164 Opc = X86::MOV32to32_;
1165 break;
1166 }
1167
1168 // The use of MVT::Flag here is not strictly accurate, but it helps
1169 // scheduling in some cases.
1170 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1171 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1172 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001173}
1174
Dale Johannesenf160d802008-10-02 18:53:47 +00001175SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1176 SDValue Chain = Node->getOperand(0);
1177 SDValue In1 = Node->getOperand(1);
1178 SDValue In2L = Node->getOperand(2);
1179 SDValue In2H = Node->getOperand(3);
1180 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1181 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1182 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001183 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesenf160d802008-10-02 18:53:47 +00001184 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1185 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1186}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001187
Dan Gohman8181bd12008-07-27 21:46:04 +00001188SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001189 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001190 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191 unsigned Opc, MOpc;
1192 unsigned Opcode = Node->getOpcode();
1193
1194#ifndef NDEBUG
1195 DOUT << std::string(Indent, ' ') << "Selecting: ";
1196 DEBUG(Node->dump(CurDAG));
1197 DOUT << "\n";
1198 Indent += 2;
1199#endif
1200
Dan Gohmanbd68c792008-07-17 19:10:17 +00001201 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202#ifndef NDEBUG
1203 DOUT << std::string(Indent-2, ' ') << "== ";
1204 DEBUG(Node->dump(CurDAG));
1205 DOUT << "\n";
1206 Indent -= 2;
1207#endif
1208 return NULL; // Already selected.
1209 }
1210
1211 switch (Opcode) {
1212 default: break;
1213 case X86ISD::GlobalBaseReg:
1214 return getGlobalBaseReg();
1215
Dale Johannesenf160d802008-10-02 18:53:47 +00001216 case X86ISD::ATOMOR64_DAG:
1217 return SelectAtomic64(Node, X86::ATOMOR6432);
1218 case X86ISD::ATOMXOR64_DAG:
1219 return SelectAtomic64(Node, X86::ATOMXOR6432);
1220 case X86ISD::ATOMADD64_DAG:
1221 return SelectAtomic64(Node, X86::ATOMADD6432);
1222 case X86ISD::ATOMSUB64_DAG:
1223 return SelectAtomic64(Node, X86::ATOMSUB6432);
1224 case X86ISD::ATOMNAND64_DAG:
1225 return SelectAtomic64(Node, X86::ATOMNAND6432);
1226 case X86ISD::ATOMAND64_DAG:
1227 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001228 case X86ISD::ATOMSWAP64_DAG:
1229 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001230
Dan Gohman5a199552007-10-08 18:33:35 +00001231 case ISD::SMUL_LOHI:
1232 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001233 SDValue N0 = Node->getOperand(0);
1234 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001235
Dan Gohman5a199552007-10-08 18:33:35 +00001236 bool isSigned = Opcode == ISD::SMUL_LOHI;
1237 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001238 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 default: assert(0 && "Unsupported VT!");
1240 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1241 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1242 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1243 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1244 }
1245 else
Duncan Sands92c43912008-06-06 12:08:01 +00001246 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 default: assert(0 && "Unsupported VT!");
1248 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1249 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1250 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1251 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1252 }
1253
1254 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001255 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 default: assert(0 && "Unsupported VT!");
1257 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1258 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1259 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1260 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1261 }
1262
Dan Gohman8181bd12008-07-27 21:46:04 +00001263 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng508fe8b2007-08-02 05:48:35 +00001264 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman5a199552007-10-08 18:33:35 +00001265 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 if (!foldedLoad) {
1267 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001268 if (foldedLoad)
1269 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 }
1271
Dan Gohman8181bd12008-07-27 21:46:04 +00001272 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1273 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274
1275 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001276 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 SDNode *CNode =
1278 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001279 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001280 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001281 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001284 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285 }
1286
Dan Gohman5a199552007-10-08 18:33:35 +00001287 // Copy the low half of the result, if it is needed.
1288 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001290 LoReg, NVT, InFlag);
1291 InFlag = Result.getValue(2);
1292 ReplaceUses(N.getValue(0), Result);
1293#ifndef NDEBUG
1294 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001295 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001296 DOUT << "\n";
1297#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001298 }
Dan Gohman5a199552007-10-08 18:33:35 +00001299 // Copy the high half of the result, if it is needed.
1300 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001301 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001302 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1303 // Prevent use of AH in a REX instruction by referencing AX instead.
1304 // Shift it down 8 bits.
1305 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1306 X86::AX, MVT::i16, InFlag);
1307 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001308 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001309 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001310 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001311 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1312 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman5a199552007-10-08 18:33:35 +00001313 MVT::i8, Result, SRIdx), 0);
1314 } else {
1315 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1316 HiReg, NVT, InFlag);
1317 InFlag = Result.getValue(2);
1318 }
1319 ReplaceUses(N.getValue(1), Result);
1320#ifndef NDEBUG
1321 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001322 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001323 DOUT << "\n";
1324#endif
1325 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326
1327#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 Indent -= 2;
1329#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 return NULL;
1332 }
1333
Dan Gohman5a199552007-10-08 18:33:35 +00001334 case ISD::SDIVREM:
1335 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001336 SDValue N0 = Node->getOperand(0);
1337 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001338
1339 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001341 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 default: assert(0 && "Unsupported VT!");
1343 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1344 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1345 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1346 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1347 }
1348 else
Duncan Sands92c43912008-06-06 12:08:01 +00001349 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 default: assert(0 && "Unsupported VT!");
1351 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1352 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1353 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1354 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1355 }
1356
1357 unsigned LoReg, HiReg;
1358 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001359 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 default: assert(0 && "Unsupported VT!");
1361 case MVT::i8:
1362 LoReg = X86::AL; HiReg = X86::AH;
1363 ClrOpcode = 0;
1364 SExtOpcode = X86::CBW;
1365 break;
1366 case MVT::i16:
1367 LoReg = X86::AX; HiReg = X86::DX;
1368 ClrOpcode = X86::MOV16r0;
1369 SExtOpcode = X86::CWD;
1370 break;
1371 case MVT::i32:
1372 LoReg = X86::EAX; HiReg = X86::EDX;
1373 ClrOpcode = X86::MOV32r0;
1374 SExtOpcode = X86::CDQ;
1375 break;
1376 case MVT::i64:
1377 LoReg = X86::RAX; HiReg = X86::RDX;
1378 ClrOpcode = X86::MOV64r0;
1379 SExtOpcode = X86::CQO;
1380 break;
1381 }
1382
Dan Gohman8181bd12008-07-27 21:46:04 +00001383 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman5a199552007-10-08 18:33:35 +00001384 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1385
Dan Gohman8181bd12008-07-27 21:46:04 +00001386 SDValue InFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 if (NVT == MVT::i8 && !isSigned) {
1388 // Special case for div8, just use a move with zero extension to AX to
1389 // clear the upper 8 bits (AH).
Dan Gohman8181bd12008-07-27 21:46:04 +00001390 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001392 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001394 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 Ops, 5), 0);
1396 Chain = Move.getValue(1);
1397 ReplaceUses(N0.getValue(1), Chain);
1398 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399 Move =
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 Chain = CurDAG->getEntryNode();
1402 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 InFlag = Chain.getValue(1);
1405 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 InFlag =
Dan Gohman5a199552007-10-08 18:33:35 +00001407 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman8181bd12008-07-27 21:46:04 +00001408 LoReg, N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 if (isSigned) {
1410 // Sign extend the low part into the high part.
1411 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001412 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 } else {
1414 // Zero out the high part, effectively zero extending the input.
Dan Gohman8181bd12008-07-27 21:46:04 +00001415 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001416 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1417 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418 }
1419 }
1420
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421 if (foldedLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 SDNode *CNode =
1424 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman8181bd12008-07-27 21:46:04 +00001425 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001426 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001427 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 InFlag =
Dan Gohman8181bd12008-07-27 21:46:04 +00001430 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 }
1432
Dan Gohman242a5ba2007-09-25 18:23:27 +00001433 // Copy the division (low) result, if it is needed.
1434 if (!N.getValue(0).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001435 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman5a199552007-10-08 18:33:35 +00001436 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001437 InFlag = Result.getValue(2);
1438 ReplaceUses(N.getValue(0), Result);
1439#ifndef NDEBUG
1440 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001441 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001442 DOUT << "\n";
1443#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001444 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001445 // Copy the remainder (high) result, if it is needed.
1446 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001448 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1449 // Prevent use of AH in a REX instruction by referencing AX instead.
1450 // Shift it down 8 bits.
Dan Gohman5a199552007-10-08 18:33:35 +00001451 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1452 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001453 InFlag = Result.getValue(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00001454 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001455 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001456 // Then truncate it down to i8.
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1458 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001459 MVT::i8, Result, SRIdx), 0);
1460 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00001461 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1462 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001463 InFlag = Result.getValue(2);
1464 }
1465 ReplaceUses(N.getValue(1), Result);
1466#ifndef NDEBUG
1467 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001468 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001469 DOUT << "\n";
1470#endif
1471 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472
1473#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 Indent -= 2;
1475#endif
1476
1477 return NULL;
1478 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001479
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001480 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001481 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmandd612bb2008-08-20 21:27:32 +00001482 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1483 SDValue N0 = Node->getOperand(0);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001484
Dan Gohmandd612bb2008-08-20 21:27:32 +00001485 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1486 unsigned Opc = 0;
1487 switch (NVT.getSimpleVT()) {
1488 default: assert(0 && "Unknown sign_extend_inreg!");
1489 case MVT::i16:
1490 Opc = X86::MOVSX16rr8;
1491 break;
1492 case MVT::i32:
1493 Opc = X86::MOVSX32rr8;
1494 break;
1495 }
1496
1497 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001498
1499#ifndef NDEBUG
Dan Gohmandd612bb2008-08-20 21:27:32 +00001500 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001501 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohmandd612bb2008-08-20 21:27:32 +00001502 DOUT << "\n";
1503 DOUT << std::string(Indent-2, ' ') << "=> ";
1504 DEBUG(ResNode->dump(CurDAG));
1505 DOUT << "\n";
1506 Indent -= 2;
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001507#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001508 return ResNode;
1509 }
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001510 break;
1511 }
1512
1513 case ISD::TRUNCATE: {
Dan Gohmandd612bb2008-08-20 21:27:32 +00001514 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1515 SDValue Input = Node->getOperand(0);
Dan Gohmandd612bb2008-08-20 21:27:32 +00001516 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518#ifndef NDEBUG
1519 DOUT << std::string(Indent-2, ' ') << "=> ";
1520 DEBUG(ResNode->dump(CurDAG));
1521 DOUT << "\n";
1522 Indent -= 2;
1523#endif
Dan Gohmandd612bb2008-08-20 21:27:32 +00001524 return ResNode;
1525 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526 break;
1527 }
Evan Chengd4cebcd2008-06-17 02:01:22 +00001528
1529 case ISD::DECLARE: {
1530 // Handle DECLARE nodes here because the second operand may have been
1531 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001532 SDValue Chain = Node->getOperand(0);
1533 SDValue N1 = Node->getOperand(1);
1534 SDValue N2 = Node->getOperand(2);
Evan Cheng651e1442008-06-18 02:48:27 +00001535 if (!isa<FrameIndexSDNode>(N1))
1536 break;
1537 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1538 if (N2.getOpcode() == ISD::ADD &&
1539 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1540 N2 = N2.getOperand(1);
1541 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Chengd4cebcd2008-06-17 02:01:22 +00001542 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Chengd4cebcd2008-06-17 02:01:22 +00001543 GlobalValue *GV =
1544 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00001545 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1546 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Dan Gohman8181bd12008-07-27 21:46:04 +00001547 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Chengd4cebcd2008-06-17 02:01:22 +00001548 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1549 MVT::Other, Ops, 3);
1550 }
1551 break;
1552 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 }
1554
1555 SDNode *ResNode = SelectCode(N);
1556
1557#ifndef NDEBUG
1558 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001559 if (ResNode == NULL || ResNode == N.getNode())
1560 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 else
1562 DEBUG(ResNode->dump(CurDAG));
1563 DOUT << "\n";
1564 Indent -= 2;
1565#endif
1566
1567 return ResNode;
1568}
1569
1570bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001571SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001572 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001573 SDValue Op0, Op1, Op2, Op3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 switch (ConstraintCode) {
1575 case 'o': // offsetable ??
1576 case 'v': // not offsetable ??
1577 default: return true;
1578 case 'm': // memory
1579 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
1580 return true;
1581 break;
1582 }
1583
1584 OutOps.push_back(Op0);
1585 OutOps.push_back(Op1);
1586 OutOps.push_back(Op2);
1587 OutOps.push_back(Op3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 return false;
1589}
1590
1591/// createX86ISelDag - This pass converts a legalized DAG into a
1592/// X86-specific DAG, ready for instruction scheduling.
1593///
1594FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1595 return new X86DAGToDAGISel(TM, Fast);
1596}