blob: 658d939c951f5aad5551c47c01ed8adaeeb71c66 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +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
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000042#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000043#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000044using namespace llvm;
45
Chris Lattner95b2c7d2006-12-19 22:59:26 +000046STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
47STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000071 unsigned Disp;
72 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000074 const char *ES;
75 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000076 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000077
78 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000079 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
80 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 }
Dale Johannesen50dd1d02008-08-11 23:46:25 +000082 void dump() {
83 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000084 cerr << "Base.Reg ";
85 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
86 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000087 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
88 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000089 cerr << "IndexReg ";
90 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
91 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000092 cerr << " Disp " << Disp << "\n";
93 cerr << "GV "; if (GV) GV->dump();
94 else cerr << "nul";
95 cerr << " CP "; if (CP) CP->dump();
96 else cerr << "nul";
97 cerr << "\n";
98 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
99 cerr << " JT" << JT << " Align" << Align << "\n";
100 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000101 };
102}
103
104namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000105 //===--------------------------------------------------------------------===//
106 /// ISel - X86 specific code to select X86 machine instructions for
107 /// SelectionDAG operations.
108 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000109 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000110 /// ContainsFPCode - Every instruction we select that uses or defines a FP
111 /// register should set this to true.
112 bool ContainsFPCode;
113
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 /// TM - Keep a reference to X86TargetMachine.
115 ///
116 X86TargetMachine &TM;
117
Chris Lattnerc961eea2005-11-16 01:54:32 +0000118 /// X86Lowering - This object fully describes how to lower LLVM code to an
119 /// X86-specific SelectionDAG.
120 X86TargetLowering X86Lowering;
121
122 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
123 /// make the right decision when generating code for different targets.
124 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000125
Evan Chengdb8d56b2008-06-30 20:45:06 +0000126 /// CurBB - Current BB being isel'd.
127 ///
128 MachineBasicBlock *CurBB;
129
Evan Chengb7a75a52008-09-26 23:41:32 +0000130 /// OptForSize - If true, selector should try to optimize for code size
131 /// instead of performance.
132 bool OptForSize;
133
Chris Lattnerc961eea2005-11-16 01:54:32 +0000134 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000135 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng4576f6d2008-07-01 18:05:03 +0000136 : SelectionDAGISel(X86Lowering, fast),
137 ContainsFPCode(false), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000138 X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000139 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000140 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000141
142 virtual const char *getPassName() const {
143 return "X86 DAG->DAG Instruction Selection";
144 }
145
Evan Chengdb8d56b2008-06-30 20:45:06 +0000146 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000147 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000148 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000149
150 /// InstructionSelectPostProcessing - Post processing of selected and
151 /// scheduled basic blocks.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000152 virtual void InstructionSelectPostProcessing();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000154 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
155
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000156 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000157
Chris Lattnerc961eea2005-11-16 01:54:32 +0000158// Include the pieces autogenerated from the target description.
159#include "X86GenDAGISel.inc"
160
161 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000162 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000163 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000164
Dan Gohman475871a2008-07-27 21:46:04 +0000165 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000166 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000167 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000168 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000169 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
170 SDValue &Scale, SDValue &Index, SDValue &Disp);
171 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
172 SDValue &Scale, SDValue &Index, SDValue &Disp);
173 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
174 SDValue N, SDValue &Base, SDValue &Scale,
175 SDValue &Index, SDValue &Disp,
176 SDValue &InChain, SDValue &OutChain);
177 bool TryFoldLoad(SDValue P, SDValue N,
178 SDValue &Base, SDValue &Scale,
179 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000180 void PreprocessForRMW();
181 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000182
Chris Lattnerc0bad572006-06-08 18:03:49 +0000183 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
184 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000185 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000186 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000187 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000188
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000189 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
190
Dan Gohman475871a2008-07-27 21:46:04 +0000191 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
192 SDValue &Scale, SDValue &Index,
193 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000194 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000195 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
196 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000197 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000198 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000199 // These are 32-bit even in 64-bit mode since RIP relative offset
200 // is 32-bit.
201 if (AM.GV)
202 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
203 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000204 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
205 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000207 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 else if (AM.JT != -1)
209 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
210 else
211 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000212 }
213
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000214 /// getI8Imm - Return a target constant with the specified value, of type
215 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000216 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000217 return CurDAG->getTargetConstant(Imm, MVT::i8);
218 }
219
Chris Lattnerc961eea2005-11-16 01:54:32 +0000220 /// getI16Imm - Return a target constant with the specified value, of type
221 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000222 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000223 return CurDAG->getTargetConstant(Imm, MVT::i16);
224 }
225
226 /// getI32Imm - Return a target constant with the specified value, of type
227 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000228 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000229 return CurDAG->getTargetConstant(Imm, MVT::i32);
230 }
Evan Chengf597dc72006-02-10 22:24:32 +0000231
Dan Gohman8b746962008-09-23 18:22:58 +0000232 /// getGlobalBaseReg - Return an SDNode that returns the value of
233 /// the global base register. Output instructions required to
234 /// initialize the global base register, if necessary.
235 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000236 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000237
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000238 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
239 /// truncate of the specified operand to i8. This can be done with tablegen,
240 /// except that this code uses MVT::Flag in a tricky way that happens to
241 /// improve scheduling in some cases.
242 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000243
Evan Cheng23addc02006-02-10 22:46:26 +0000244#ifndef NDEBUG
245 unsigned Indent;
246#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000247 };
248}
249
Gabor Greif93c53e52008-08-31 15:37:04 +0000250/// findFlagUse - Return use of MVT::Flag value produced by the specified
251/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000252///
Evan Chenga275ecb2006-10-10 01:46:56 +0000253static SDNode *findFlagUse(SDNode *N) {
254 unsigned FlagResNo = N->getNumValues()-1;
255 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000256 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000257 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000258 SDValue Op = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +0000259 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000260 return User;
261 }
262 }
263 return NULL;
264}
265
Evan Chengcdda25d2008-04-25 08:22:20 +0000266/// findNonImmUse - Return true by reference in "found" if "Use" is an
267/// non-immediate use of "Def". This function recursively traversing
268/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000269static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman682d5a82008-09-17 01:39:10 +0000270 SDNode *Root, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000271 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000272 if (found ||
Dan Gohmanf06c8352008-09-30 18:30:35 +0000273 Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000274 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000275 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000276
Evan Cheng27e1fe92006-10-14 08:33:25 +0000277 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000278 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000279 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000280 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000281 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000282 assert(N != Root);
Evan Chengf4b4c412006-08-08 00:31:00 +0000283 found = true;
284 break;
285 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000286
287 // Traverse up the operand chain.
Dan Gohman682d5a82008-09-17 01:39:10 +0000288 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000289 }
290}
291
Evan Cheng27e1fe92006-10-14 08:33:25 +0000292/// isNonImmUse - Start searching from Root up the DAG to check is Def can
293/// be reached. Return true if that's the case. However, ignore direct uses
294/// by ImmedUse (which would be U in the example illustrated in
295/// CanBeFoldedBy) and by Root (which can happen in the store case).
296/// FIXME: to be really generic, we should allow direct use by any node
297/// that is being folded. But realisticly since we only fold loads which
298/// have one non-chain use, we only need to watch out for load/op/store
299/// and load/op/cmp case where the root (store / cmp) may reach the load via
300/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000301static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000302 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000303 bool found = false;
Dan Gohman682d5a82008-09-17 01:39:10 +0000304 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000305 return found;
306}
307
308
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000309bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000310 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000311
Dan Gohman682d5a82008-09-17 01:39:10 +0000312 // If Root use can somehow reach N through a path that that doesn't contain
313 // U then folding N would create a cycle. e.g. In the following
314 // diagram, Root can reach N through X. If N is folded into into Root, then
315 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000316 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000317 // [N*] //
318 // ^ ^ //
319 // / \ //
320 // [U*] [X]? //
321 // ^ ^ //
322 // \ / //
323 // \ / //
324 // [Root*] //
325 //
326 // * indicates nodes to be folded together.
327 //
328 // If Root produces a flag, then it gets (even more) interesting. Since it
329 // will be "glued" together with its flag use in the scheduler, we need to
330 // check if it might reach N.
331 //
332 // [N*] //
333 // ^ ^ //
334 // / \ //
335 // [U*] [X]? //
336 // ^ ^ //
337 // \ \ //
338 // \ | //
339 // [Root*] | //
340 // ^ | //
341 // f | //
342 // | / //
343 // [Y] / //
344 // ^ / //
345 // f / //
346 // | / //
347 // [FU] //
348 //
349 // If FU (flag use) indirectly reaches N (the load), and Root folds N
350 // (call it Fold), then X is a predecessor of FU and a successor of
351 // Fold. But since Fold and FU are flagged together, this will create
352 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000353
Duncan Sands83ec4b62008-06-06 12:08:01 +0000354 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000355 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000356 SDNode *FU = findFlagUse(Root);
357 if (FU == NULL)
358 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000359 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000360 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000361 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000362
Dan Gohman682d5a82008-09-17 01:39:10 +0000363 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000364}
365
Evan Cheng70e674e2006-08-28 20:10:17 +0000366/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
367/// and move load below the TokenFactor. Replace store's chain operand with
368/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000369static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000370 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000371 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000372 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
373 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000374 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000375 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000376 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000377 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
378 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
379 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
380 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000381}
382
Evan Chengcd0baf22008-05-23 21:23:16 +0000383/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
384///
Dan Gohman475871a2008-07-27 21:46:04 +0000385static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
386 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000387 if (N.getOpcode() == ISD::BIT_CONVERT)
388 N = N.getOperand(0);
389
390 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
391 if (!LD || LD->isVolatile())
392 return false;
393 if (LD->getAddressingMode() != ISD::UNINDEXED)
394 return false;
395
396 ISD::LoadExtType ExtType = LD->getExtensionType();
397 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
398 return false;
399
400 if (N.hasOneUse() &&
401 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000402 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000403 Load = N;
404 return true;
405 }
406 return false;
407}
408
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
410/// operand and move load below the call's chain operand.
411static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
412 SDValue Call, SDValue Chain) {
413 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000414 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
415 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000416 Ops.push_back(Load.getOperand(0));
417 else
418 Ops.push_back(Chain.getOperand(i));
419 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
420 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
421 Load.getOperand(1), Load.getOperand(2));
422 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000423 Ops.push_back(SDValue(Load.getNode(), 1));
424 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000425 Ops.push_back(Call.getOperand(i));
426 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
427}
428
429/// isCalleeLoad - Return true if call address is a load and it can be
430/// moved below CALLSEQ_START and the chains leading up to the call.
431/// Return the CALLSEQ_START by reference as a second output.
432static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000433 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000434 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000435 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000436 if (!LD ||
437 LD->isVolatile() ||
438 LD->getAddressingMode() != ISD::UNINDEXED ||
439 LD->getExtensionType() != ISD::NON_EXTLOAD)
440 return false;
441
442 // Now let's find the callseq_start.
443 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
444 if (!Chain.hasOneUse())
445 return false;
446 Chain = Chain.getOperand(0);
447 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000448 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Chengab6c3bb2008-08-25 21:27:18 +0000449}
450
451
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000452/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
453/// This is only run if not in -fast mode (aka -O0).
454/// This allows the instruction selector to pick more read-modify-write
455/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000456///
457/// [Load chain]
458/// ^
459/// |
460/// [Load]
461/// ^ ^
462/// | |
463/// / \-
464/// / |
465/// [TokenFactor] [Op]
466/// ^ ^
467/// | |
468/// \ /
469/// \ /
470/// [Store]
471///
472/// The fact the store's chain operand != load's chain will prevent the
473/// (store (op (load))) instruction from being selected. We can transform it to:
474///
475/// [Load chain]
476/// ^
477/// |
478/// [TokenFactor]
479/// ^
480/// |
481/// [Load]
482/// ^ ^
483/// | |
484/// | \-
485/// | |
486/// | [Op]
487/// | ^
488/// | |
489/// \ /
490/// \ /
491/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000492void X86DAGToDAGISel::PreprocessForRMW() {
493 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
494 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000495 if (I->getOpcode() == X86ISD::CALL) {
496 /// Also try moving call address load from outside callseq_start to just
497 /// before the call to allow it to be folded.
498 ///
499 /// [Load chain]
500 /// ^
501 /// |
502 /// [Load]
503 /// ^ ^
504 /// | |
505 /// / \--
506 /// / |
507 ///[CALLSEQ_START] |
508 /// ^ |
509 /// | |
510 /// [LOAD/C2Reg] |
511 /// | |
512 /// \ /
513 /// \ /
514 /// [CALL]
515 SDValue Chain = I->getOperand(0);
516 SDValue Load = I->getOperand(1);
517 if (!isCalleeLoad(Load, Chain))
518 continue;
519 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
520 ++NumLoadMoved;
521 continue;
522 }
523
Evan Cheng8b2794a2006-10-13 21:14:26 +0000524 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000525 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000526 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000527
Gabor Greifba36cb52008-08-28 21:40:38 +0000528 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000529 continue;
530
Dan Gohman475871a2008-07-27 21:46:04 +0000531 SDValue N1 = I->getOperand(1);
532 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000533 if ((N1.getValueType().isFloatingPoint() &&
534 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000535 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000536 continue;
537
538 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000539 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000540 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000541 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000542 case ISD::ADD:
543 case ISD::MUL:
544 case ISD::AND:
545 case ISD::OR:
546 case ISD::XOR:
547 case ISD::ADDC:
548 case ISD::ADDE:
549 case ISD::VECTOR_SHUFFLE: {
550 SDValue N10 = N1.getOperand(0);
551 SDValue N11 = N1.getOperand(1);
552 RModW = isRMWLoad(N10, Chain, N2, Load);
553 if (!RModW)
554 RModW = isRMWLoad(N11, Chain, N2, Load);
555 break;
556 }
557 case ISD::SUB:
558 case ISD::SHL:
559 case ISD::SRA:
560 case ISD::SRL:
561 case ISD::ROTL:
562 case ISD::ROTR:
563 case ISD::SUBC:
564 case ISD::SUBE:
565 case X86ISD::SHLD:
566 case X86ISD::SHRD: {
567 SDValue N10 = N1.getOperand(0);
568 RModW = isRMWLoad(N10, Chain, N2, Load);
569 break;
570 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000571 }
572
Evan Cheng82a35b32006-08-29 06:44:17 +0000573 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000574 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000575 ++NumLoadMoved;
576 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000577 }
578}
579
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000580
581/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
582/// nodes that target the FP stack to be store and load to the stack. This is a
583/// gross hack. We would like to simply mark these as being illegal, but when
584/// we do that, legalize produces these when it expands calls, then expands
585/// these in the same legalize pass. We would like dag combine to be able to
586/// hack on these between the call expansion and the node legalization. As such
587/// this pass basically does "really late" legalization of these inline with the
588/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000589void X86DAGToDAGISel::PreprocessForFPConvert() {
590 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
591 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000592 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
593 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
594 continue;
595
596 // If the source and destination are SSE registers, then this is a legal
597 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000598 MVT SrcVT = N->getOperand(0).getValueType();
599 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000600 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
601 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
602 if (SrcIsSSE && DstIsSSE)
603 continue;
604
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000605 if (!SrcIsSSE && !DstIsSSE) {
606 // If this is an FPStack extension, it is a noop.
607 if (N->getOpcode() == ISD::FP_EXTEND)
608 continue;
609 // If this is a value-preserving FPStack truncation, it is a noop.
610 if (N->getConstantOperandVal(1))
611 continue;
612 }
613
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000614 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
615 // FPStack has extload and truncstore. SSE can fold direct loads into other
616 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000617 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000618 if (N->getOpcode() == ISD::FP_ROUND)
619 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
620 else
621 MemVT = SrcIsSSE ? SrcVT : DstVT;
622
Dan Gohmanf350b272008-08-23 02:25:05 +0000623 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000624
625 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohmanf350b272008-08-23 02:25:05 +0000626 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
627 N->getOperand(0),
628 MemTmp, NULL, 0, MemVT);
629 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
630 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000631
632 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
633 // extload we created. This will cause general havok on the dag because
634 // anything below the conversion could be folded into other existing nodes.
635 // To avoid invalidating 'I', back it up to the convert node.
636 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000637 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000638
639 // Now that we did that, the node is dead. Increment the iterator to the
640 // next node to process, then delete N.
641 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000642 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000643 }
644}
645
Chris Lattnerc961eea2005-11-16 01:54:32 +0000646/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
647/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000648void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000649 CurBB = BB; // BB can change as result of isel.
Evan Chengb7a75a52008-09-26 23:41:32 +0000650 if (!OptForSize) {
651 const Function *F = CurDAG->getMachineFunction().getFunction();
Daniel Dunbar02a4da22008-09-27 00:22:09 +0000652 OptForSize = !F->isDeclaration() &&
653 F->hasFnAttr(Attribute::OptimizeForSize);
Evan Chengb7a75a52008-09-26 23:41:32 +0000654 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000655
Evan Chengdb8d56b2008-06-30 20:45:06 +0000656 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000657 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000658 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000659
660 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000661 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000662
Chris Lattnerc961eea2005-11-16 01:54:32 +0000663 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000664#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000665 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000666 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000667#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000668 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000669#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000670 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000671#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000672
Dan Gohmanf350b272008-08-23 02:25:05 +0000673 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000674}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000675
Dan Gohman462dc7f2008-07-21 20:00:07 +0000676void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +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 Johannesencdbe4d32007-08-07 20:29:26 +0000680
Dale Johannesen48d1e452007-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 Johannesencdbe4d32007-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 Chengdb8d56b2008-06-30 20:45:06 +0000689 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-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 Johannesen48d1e452007-09-24 22:52:39 +0000703 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000704 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000705 !ContainsFPCode && I != E; ++I) {
Dan Gohmand735b802008-10-03 15:45:36 +0000706 if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000707 const TargetRegisterClass *clas;
708 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000709 if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000710 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000711 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000712 X86::RFP32RegisterClass ||
713 clas == X86::RFP64RegisterClass ||
714 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000715 ContainsFPCode = true;
716 break;
717 }
718 }
719 }
720 }
Dale Johannesen48d1e452007-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 Johannesencdbe4d32007-08-07 20:29:26 +0000739 }
740 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000741 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000742 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
743 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000744 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000745 TM.getInstrInfo()->get(X86::FP_REG_KILL));
746 ++NumFPKill;
747 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000748 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000749}
750
Anton Korobeynikov2fe12592007-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
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +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 Lattner5aaddaa2007-12-08 07:22:58 +0000769/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000770bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000771 bool isRoot, unsigned Depth) {
Evan Chengda43bcf2008-09-24 00:05:32 +0000772 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000773 // Limit recursion.
774 if (Depth > 5)
775 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000776
Evan Cheng25ab6902006-09-08 06:48:29 +0000777 // RIP relative addressing: %rip + 32-bit displacement!
778 if (AM.isRIPRel) {
779 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000780 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000781 if (isInt32(AM.Disp + Val)) {
782 AM.Disp += Val;
783 return false;
784 }
785 }
786 return true;
787 }
788
Gabor Greifba36cb52008-08-28 21:40:38 +0000789 int id = N.getNode()->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000790 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000791
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000792 switch (N.getOpcode()) {
793 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000794 case ISD::Constant: {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000795 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000796 if (isInt32(AM.Disp + Val)) {
797 AM.Disp += Val;
798 return false;
799 }
800 break;
801 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000802
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000803 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000804DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
805DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
806DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000807 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000808 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-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 Greifba36cb52008-08-28 21:40:38 +0000811 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000812 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000813 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
814 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000815 // 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 Greifba36cb52008-08-28 21:40:38 +0000818 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000820 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
821 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000822 AM.GV = GV;
823 AM.Disp += G->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000824 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000825 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000826 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000827 AM.CP = CP->getConstVal();
828 AM.Align = CP->getAlignment();
829 AM.Disp += CP->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000830 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000831 return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000832 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000833 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000834 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000835 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000836 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000837 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000838 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000839 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000840 }
841 }
842 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000843 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000844
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000846 if (AM.BaseType == X86ISelAddressMode::RegBase
847 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000848 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
849 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
850 return false;
851 }
852 break;
Evan Chengec693f72005-12-08 02:01:35 +0000853
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000854 case ISD::SHL:
Gabor Greif93c53e52008-08-31 15:37:04 +0000855 if (AlreadySelected || AM.IndexReg.getNode() != 0
856 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000857 break;
858
Gabor Greif93c53e52008-08-31 15:37:04 +0000859 if (ConstantSDNode
860 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000861 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000862 if (Val == 1 || Val == 2 || Val == 3) {
863 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000864 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000866 // Okay, we know that we have a scale by now. However, if the scaled
867 // value is an add of something and a constant, we can fold the
868 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000869 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
870 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
871 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000872 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000873 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000874 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000875 if (isInt32(Disp))
876 AM.Disp = Disp;
877 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000878 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000879 } else {
880 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000881 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000882 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000883 }
884 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000885 }
Evan Chengec693f72005-12-08 02:01:35 +0000886
Dan Gohman83688052007-10-22 20:22:24 +0000887 case ISD::SMUL_LOHI:
888 case ISD::UMUL_LOHI:
889 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000890 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000891 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000892 case ISD::MUL:
893 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000894 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000895 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000896 AM.Base.Reg.getNode() == 0 &&
897 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000898 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000899 if (ConstantSDNode
900 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000901 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
902 CN->getZExtValue() == 9) {
903 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000904
Gabor Greifba36cb52008-08-28 21:40:38 +0000905 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000907
908 // Okay, we know that we have a scale by now. However, if the scaled
909 // value is an add of something and a constant, we can fold the
910 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000911 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
912 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
913 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000915 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000916 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
917 CN->getZExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000918 if (isInt32(Disp))
919 AM.Disp = Disp;
920 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000921 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000922 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000924 }
925
926 AM.IndexReg = AM.Base.Reg = Reg;
927 return false;
928 }
Chris Lattner62412262007-02-04 20:18:17 +0000929 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000930 break;
931
Chris Lattner62412262007-02-04 20:18:17 +0000932 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000933 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000934 X86ISelAddressMode Backup = AM;
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
936 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000937 return false;
938 AM = Backup;
Gabor Greifba36cb52008-08-28 21:40:38 +0000939 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
940 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000941 return false;
942 AM = Backup;
943 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000944 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000945
Chris Lattner62412262007-02-04 20:18:17 +0000946 case ISD::OR:
947 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000948 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000949
950 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
951 X86ISelAddressMode Backup = AM;
952 // Start with the LHS as an addr mode.
953 if (!MatchAddress(N.getOperand(0), AM, false) &&
954 // Address could not have picked a GV address for the displacement.
955 AM.GV == NULL &&
956 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman7810bfe2008-09-26 21:54:37 +0000957 isInt32(AM.Disp + CN->getSExtValue()) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000958 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000959 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000960 AM.Disp += CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000961 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000962 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000963 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000964 }
965 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000966
967 case ISD::AND: {
968 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
969 // allows us to fold the shift into this addressing mode.
970 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000971 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000972 if (Shift.getOpcode() != ISD::SHL) break;
973
974 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000975 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000976
977 // Not when RIP is used as the base.
978 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000979
980 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
981 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
982 if (!C1 || !C2) break;
983
984 // Not likely to be profitable if either the AND or SHIFT node has more
985 // than one use (unless all uses are for address computation). Besides,
986 // isel mechanism requires their node ids to be reused.
987 if (!N.hasOneUse() || !Shift.hasOneUse())
988 break;
989
990 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000991 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +0000992 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
993 break;
994
995 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +0000996 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
997 SDValue(C2, 0), SDValue(C1, 0));
998 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +0000999 Shift.getOperand(0), NewANDMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00001000 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
1001 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Evan Cheng1314b002007-12-13 00:43:27 +00001002
1003 AM.Scale = 1 << ShiftCst;
1004 AM.IndexReg = NewAND;
1005 return false;
1006 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001007 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001008
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001009 return MatchAddressBase(N, AM, isRoot, Depth);
1010}
1011
1012/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1013/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001014bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001015 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001016 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001017 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001018 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001019 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001020 AM.IndexReg = N;
1021 AM.Scale = 1;
1022 return false;
1023 }
1024
1025 // Otherwise, we cannot select it.
1026 return true;
1027 }
1028
1029 // Default, generate it as a register.
1030 AM.BaseType = X86ISelAddressMode::RegBase;
1031 AM.Base.Reg = N;
1032 return false;
1033}
1034
Evan Chengec693f72005-12-08 02:01:35 +00001035/// SelectAddr - returns true if it is able pattern match an addressing mode.
1036/// It returns the operands which make up the maximal addressing mode it can
1037/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001038bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1039 SDValue &Scale, SDValue &Index,
1040 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001041 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001042 if (MatchAddress(N, AM))
1043 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001044
Duncan Sands83ec4b62008-06-06 12:08:01 +00001045 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001046 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001047 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001048 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001049 }
Evan Cheng8700e142006-01-11 06:09:51 +00001050
Gabor Greifba36cb52008-08-28 21:40:38 +00001051 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001052 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001053
1054 getAddressOperands(AM, Base, Scale, Index, Disp);
1055 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001056}
1057
Chris Lattner3a7cd952006-10-07 21:55:32 +00001058/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1059/// match a load whose top elements are either undef or zeros. The load flavor
1060/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001061bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1062 SDValue N, SDValue &Base,
1063 SDValue &Scale, SDValue &Index,
1064 SDValue &Disp, SDValue &InChain,
1065 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001066 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001067 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001068 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001069 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001070 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001071 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001072 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001073 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001074 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001075 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001076 return true;
1077 }
1078 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001079
1080 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001081 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001083 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001084 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001085 N.getOperand(0).getNode()->hasOneUse() &&
1086 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001087 N.getOperand(0).getOperand(0).hasOneUse()) {
1088 // Okay, this is a zero extending load. Fold it.
1089 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1090 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1091 return false;
1092 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001093 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001094 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001095 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001096 return false;
1097}
1098
1099
Evan Cheng51a9ed92006-02-25 10:09:08 +00001100/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1101/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001102bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1103 SDValue &Base, SDValue &Scale,
1104 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001105 X86ISelAddressMode AM;
1106 if (MatchAddress(N, AM))
1107 return false;
1108
Duncan Sands83ec4b62008-06-06 12:08:01 +00001109 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001110 unsigned Complexity = 0;
1111 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001112 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001113 Complexity = 1;
1114 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001115 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001116 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1117 Complexity = 4;
1118
Gabor Greifba36cb52008-08-28 21:40:38 +00001119 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001120 Complexity++;
1121 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001122 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001123
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001124 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1125 // a simple shift.
1126 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001127 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001128
1129 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1130 // to a LEA. This is determined with some expermentation but is by no means
1131 // optimal (especially for code size consideration). LEA is nice because of
1132 // its three-address nature. Tweak the cost function again when we can run
1133 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001134 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1135 // For X86-64, we should always use lea to materialize RIP relative
1136 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001137 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001138 Complexity = 4;
1139 else
1140 Complexity += 2;
1141 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001142
Gabor Greifba36cb52008-08-28 21:40:38 +00001143 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001144 Complexity++;
1145
1146 if (Complexity > 2) {
1147 getAddressOperands(AM, Base, Scale, Index, Disp);
1148 return true;
1149 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001150 return false;
1151}
1152
Dan Gohman475871a2008-07-27 21:46:04 +00001153bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1154 SDValue &Base, SDValue &Scale,
1155 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001156 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001157 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001159 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001160 return false;
1161}
1162
Dan Gohman8b746962008-09-23 18:22:58 +00001163/// getGlobalBaseReg - Return an SDNode that returns the value of
1164/// the global base register. Output instructions required to
1165/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001166///
Evan Cheng9ade2182006-08-26 05:34:46 +00001167SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001168 MachineFunction *MF = CurBB->getParent();
1169 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001170 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001171}
1172
Evan Chengb245d922006-05-20 01:36:52 +00001173static SDNode *FindCallStartFromCall(SDNode *Node) {
1174 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1175 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1176 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001177 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001178}
1179
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001180/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1181/// truncate of the specified operand to i8. This can be done with tablegen,
1182/// except that this code uses MVT::Flag in a tricky way that happens to
1183/// improve scheduling in some cases.
1184SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1185 assert(!Subtarget->is64Bit() &&
1186 "getTruncateTo8Bit is only needed on x86-32!");
1187 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1188
1189 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1190 unsigned Opc;
1191 MVT N0VT = N0.getValueType();
1192 switch (N0VT.getSimpleVT()) {
1193 default: assert(0 && "Unknown truncate!");
1194 case MVT::i16:
1195 Opc = X86::MOV16to16_;
1196 break;
1197 case MVT::i32:
1198 Opc = X86::MOV32to32_;
1199 break;
1200 }
1201
1202 // The use of MVT::Flag here is not strictly accurate, but it helps
1203 // scheduling in some cases.
1204 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1205 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1206 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001207}
1208
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001209SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1210 SDValue Chain = Node->getOperand(0);
1211 SDValue In1 = Node->getOperand(1);
1212 SDValue In2L = Node->getOperand(2);
1213 SDValue In2H = Node->getOperand(3);
1214 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1215 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1216 return NULL;
1217 AddToISelQueue(Tmp0);
1218 AddToISelQueue(Tmp1);
1219 AddToISelQueue(Tmp2);
1220 AddToISelQueue(Tmp3);
1221 AddToISelQueue(In2L);
1222 AddToISelQueue(In2H);
1223 AddToISelQueue(Chain);
1224 SDValue LSI = CurDAG->getMemOperand(cast<MemSDNode>(In1)->getMemOperand());
1225 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1226 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1227}
Christopher Lambc59e5212007-08-10 21:48:46 +00001228
Dan Gohman475871a2008-07-27 21:46:04 +00001229SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001231 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001232 unsigned Opc, MOpc;
1233 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001234
Evan Chengf597dc72006-02-10 22:24:32 +00001235#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001236 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001237 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001238 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001239 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001240#endif
1241
Dan Gohmane8be6c62008-07-17 19:10:17 +00001242 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001243#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001244 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001245 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001246 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001247 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001248#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001249 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001250 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001251
Evan Cheng0114e942006-01-06 20:36:21 +00001252 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001253 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001254 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001255 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001256
Evan Cheng51a9ed92006-02-25 10:09:08 +00001257 case ISD::ADD: {
1258 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1259 // code and is matched first so to prevent it from being turned into
1260 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001261 // In 64-bit small code size mode, use LEA to take advantage of
1262 // RIP-relative addressing.
1263 if (TM.getCodeModel() != CodeModel::Small)
1264 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001265 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001266 SDValue N0 = N.getOperand(0);
1267 SDValue N1 = N.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (N.getNode()->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001269 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001270 N1.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001271 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001272 SDValue C(0, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001273 // TODO: handle ExternalSymbolSDNode.
Evan Cheng51a9ed92006-02-25 10:09:08 +00001274 if (GlobalAddressSDNode *G =
1275 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001276 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001277 G->getOffset() + Offset);
1278 } else if (ConstantPoolSDNode *CP =
1279 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001280 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001281 CP->getAlignment(),
1282 CP->getOffset()+Offset);
1283 }
1284
Gabor Greifba36cb52008-08-28 21:40:38 +00001285 if (C.getNode()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001286 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001287 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001288 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif93c53e52008-08-31 15:37:04 +00001289 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1290 MVT::i64, Ops, 4);
Evan Cheng25ab6902006-09-08 06:48:29 +00001291 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00001292 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Evan Cheng25ab6902006-09-08 06:48:29 +00001293 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001294 }
1295
1296 // Other cases are handled by auto-generated code.
1297 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001298 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001299
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001300 case X86ISD::ATOMOR64_DAG:
1301 return SelectAtomic64(Node, X86::ATOMOR6432);
1302 case X86ISD::ATOMXOR64_DAG:
1303 return SelectAtomic64(Node, X86::ATOMXOR6432);
1304 case X86ISD::ATOMADD64_DAG:
1305 return SelectAtomic64(Node, X86::ATOMADD6432);
1306 case X86ISD::ATOMSUB64_DAG:
1307 return SelectAtomic64(Node, X86::ATOMSUB6432);
1308 case X86ISD::ATOMNAND64_DAG:
1309 return SelectAtomic64(Node, X86::ATOMNAND6432);
1310 case X86ISD::ATOMAND64_DAG:
1311 return SelectAtomic64(Node, X86::ATOMAND6432);
1312
Dan Gohman525178c2007-10-08 18:33:35 +00001313 case ISD::SMUL_LOHI:
1314 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001315 SDValue N0 = Node->getOperand(0);
1316 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001317
Dan Gohman525178c2007-10-08 18:33:35 +00001318 bool isSigned = Opcode == ISD::SMUL_LOHI;
1319 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001320 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001321 default: assert(0 && "Unsupported VT!");
1322 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1323 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1324 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001325 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001326 }
1327 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001328 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001329 default: assert(0 && "Unsupported VT!");
1330 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1331 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1332 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001333 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001334 }
1335
1336 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001337 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001338 default: assert(0 && "Unsupported VT!");
1339 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1340 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1341 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001342 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001343 }
1344
Dan Gohman475871a2008-07-27 21:46:04 +00001345 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001346 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001347 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001348 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001349 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001350 if (foldedLoad)
1351 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001352 }
1353
Evan Cheng04699902006-08-26 01:05:16 +00001354 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1356 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001357
1358 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001359 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001360 AddToISelQueue(Tmp0);
1361 AddToISelQueue(Tmp1);
1362 AddToISelQueue(Tmp2);
1363 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001364 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001365 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001366 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001367 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001368 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001369 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001370 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001371 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001372 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001374 }
1375
Dan Gohman525178c2007-10-08 18:33:35 +00001376 // Copy the low half of the result, if it is needed.
1377 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001378 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001379 LoReg, NVT, InFlag);
1380 InFlag = Result.getValue(2);
1381 ReplaceUses(N.getValue(0), Result);
1382#ifndef NDEBUG
1383 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001384 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001385 DOUT << "\n";
1386#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001387 }
Dan Gohman525178c2007-10-08 18:33:35 +00001388 // Copy the high half of the result, if it is needed.
1389 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001390 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001391 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1392 // Prevent use of AH in a REX instruction by referencing AX instead.
1393 // Shift it down 8 bits.
1394 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1395 X86::AX, MVT::i16, InFlag);
1396 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001397 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001398 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001399 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001400 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1401 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001402 MVT::i8, Result, SRIdx), 0);
1403 } else {
1404 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1405 HiReg, NVT, InFlag);
1406 InFlag = Result.getValue(2);
1407 }
1408 ReplaceUses(N.getValue(1), Result);
1409#ifndef NDEBUG
1410 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001412 DOUT << "\n";
1413#endif
1414 }
Evan Cheng34167212006-02-09 00:37:58 +00001415
Evan Chengf597dc72006-02-10 22:24:32 +00001416#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001417 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001418#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001419
Evan Cheng64a752f2006-08-11 09:08:15 +00001420 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001421 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001422
Dan Gohman525178c2007-10-08 18:33:35 +00001423 case ISD::SDIVREM:
1424 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001425 SDValue N0 = Node->getOperand(0);
1426 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001427
1428 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001429 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001430 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001431 default: assert(0 && "Unsupported VT!");
1432 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1433 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1434 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001435 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001436 }
1437 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001438 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001439 default: assert(0 && "Unsupported VT!");
1440 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1441 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1442 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001443 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001444 }
1445
1446 unsigned LoReg, HiReg;
1447 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001448 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001449 default: assert(0 && "Unsupported VT!");
1450 case MVT::i8:
1451 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001452 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001453 SExtOpcode = X86::CBW;
1454 break;
1455 case MVT::i16:
1456 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001457 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001458 SExtOpcode = X86::CWD;
1459 break;
1460 case MVT::i32:
1461 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001462 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001463 SExtOpcode = X86::CDQ;
1464 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001465 case MVT::i64:
1466 LoReg = X86::RAX; HiReg = X86::RDX;
1467 ClrOpcode = X86::MOV64r0;
1468 SExtOpcode = X86::CQO;
1469 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001470 }
1471
Dan Gohman475871a2008-07-27 21:46:04 +00001472 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001473 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1474
Dan Gohman475871a2008-07-27 21:46:04 +00001475 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001476 if (NVT == MVT::i8 && !isSigned) {
1477 // Special case for div8, just use a move with zero extension to AX to
1478 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001479 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001480 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001481 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001482 AddToISelQueue(N0.getOperand(0));
1483 AddToISelQueue(Tmp0);
1484 AddToISelQueue(Tmp1);
1485 AddToISelQueue(Tmp2);
1486 AddToISelQueue(Tmp3);
1487 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001489 Ops, 5), 0);
1490 Chain = Move.getValue(1);
1491 ReplaceUses(N0.getValue(1), Chain);
1492 } else {
1493 AddToISelQueue(N0);
1494 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001495 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001496 Chain = CurDAG->getEntryNode();
1497 }
Dan Gohman475871a2008-07-27 21:46:04 +00001498 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001499 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001500 } else {
1501 AddToISelQueue(N0);
1502 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001503 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001504 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001505 if (isSigned) {
1506 // Sign extend the low part into the high part.
1507 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001509 } else {
1510 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001511 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001512 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1513 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001514 }
Evan Cheng948f3432006-01-06 23:19:29 +00001515 }
1516
1517 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001518 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001519 AddToISelQueue(Tmp0);
1520 AddToISelQueue(Tmp1);
1521 AddToISelQueue(Tmp2);
1522 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001523 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001524 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001525 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001526 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001527 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001528 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001529 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001530 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001531 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001532 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001533 }
1534
Dan Gohmana37c9f72007-09-25 18:23:27 +00001535 // Copy the division (low) result, if it is needed.
1536 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001537 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001538 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001539 InFlag = Result.getValue(2);
1540 ReplaceUses(N.getValue(0), Result);
1541#ifndef NDEBUG
1542 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001544 DOUT << "\n";
1545#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001546 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001547 // Copy the remainder (high) result, if it is needed.
1548 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001549 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001550 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1551 // Prevent use of AH in a REX instruction by referencing AX instead.
1552 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001553 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1554 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001555 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001556 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001557 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001558 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001559 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1560 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001561 MVT::i8, Result, SRIdx), 0);
1562 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001563 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1564 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001565 InFlag = Result.getValue(2);
1566 }
1567 ReplaceUses(N.getValue(1), Result);
1568#ifndef NDEBUG
1569 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001570 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001571 DOUT << "\n";
1572#endif
1573 }
Evan Chengf597dc72006-02-10 22:24:32 +00001574
1575#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001576 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001577#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001578
1579 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001580 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001581
Christopher Lambc59e5212007-08-10 21:48:46 +00001582 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001583 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001584 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1585 SDValue N0 = Node->getOperand(0);
1586 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001587
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001588 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1589 unsigned Opc = 0;
1590 switch (NVT.getSimpleVT()) {
1591 default: assert(0 && "Unknown sign_extend_inreg!");
1592 case MVT::i16:
1593 Opc = X86::MOVSX16rr8;
1594 break;
1595 case MVT::i32:
1596 Opc = X86::MOVSX32rr8;
1597 break;
1598 }
1599
1600 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001601
1602#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001603 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001604 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001605 DOUT << "\n";
1606 DOUT << std::string(Indent-2, ' ') << "=> ";
1607 DEBUG(ResNode->dump(CurDAG));
1608 DOUT << "\n";
1609 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001610#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001611 return ResNode;
1612 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001613 break;
1614 }
1615
1616 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001617 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1618 SDValue Input = Node->getOperand(0);
1619 AddToISelQueue(Node->getOperand(0));
1620 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001621
Evan Cheng403be7e2006-05-08 08:01:26 +00001622#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001623 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001624 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001625 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001626 Indent -= 2;
1627#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001628 return ResNode;
1629 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001630 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001631 }
Evan Cheng851bc042008-06-17 02:01:22 +00001632
1633 case ISD::DECLARE: {
1634 // Handle DECLARE nodes here because the second operand may have been
1635 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001636 SDValue Chain = Node->getOperand(0);
1637 SDValue N1 = Node->getOperand(1);
1638 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001639 if (!isa<FrameIndexSDNode>(N1))
1640 break;
1641 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1642 if (N2.getOpcode() == ISD::ADD &&
1643 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1644 N2 = N2.getOperand(1);
1645 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001646 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001647 GlobalValue *GV =
1648 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1650 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001651 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001652 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001653 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1654 MVT::Other, Ops, 3);
1655 }
1656 break;
1657 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001658 }
1659
Evan Cheng9ade2182006-08-26 05:34:46 +00001660 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001661
Evan Chengf597dc72006-02-10 22:24:32 +00001662#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001663 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001664 if (ResNode == NULL || ResNode == N.getNode())
1665 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001666 else
1667 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001668 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001669 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001670#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001671
1672 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001673}
1674
Chris Lattnerc0bad572006-06-08 18:03:49 +00001675bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001676SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001677 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001678 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001679 switch (ConstraintCode) {
1680 case 'o': // offsetable ??
1681 case 'v': // not offsetable ??
1682 default: return true;
1683 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001684 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001685 return true;
1686 break;
1687 }
1688
Evan Cheng04699902006-08-26 01:05:16 +00001689 OutOps.push_back(Op0);
1690 OutOps.push_back(Op1);
1691 OutOps.push_back(Op2);
1692 OutOps.push_back(Op3);
1693 AddToISelQueue(Op0);
1694 AddToISelQueue(Op1);
1695 AddToISelQueue(Op2);
1696 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001697 return false;
1698}
1699
Chris Lattnerc961eea2005-11-16 01:54:32 +00001700/// createX86ISelDag - This pass converts a legalized DAG into a
1701/// X86-specific DAG, ready for instruction scheduling.
1702///
Evan Chenge50794a2006-08-29 18:28:33 +00001703FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1704 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001705}