blob: dedd8908da0f6fbe53e219a738946b7ef6ad3f7f [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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000035#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000038#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000040#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000041#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000042#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000043using namespace llvm;
44
Chris Lattner95b2c7d2006-12-19 22:59:26 +000045STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
46STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47
Chris Lattnerc961eea2005-11-16 01:54:32 +000048//===----------------------------------------------------------------------===//
49// Pattern Matcher Implementation
50//===----------------------------------------------------------------------===//
51
52namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000054 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000055 /// tree.
56 struct X86ISelAddressMode {
57 enum {
58 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000059 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000060 } BaseType;
61
62 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000063 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000064 int FrameIndex;
65 } Base;
66
Evan Chengbe3bf422008-02-07 08:53:49 +000067 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000068 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000069 SDValue IndexReg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000070 unsigned Disp;
71 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000072 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000073 const char *ES;
74 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000076
77 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000078 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
79 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080 }
Dale Johannesen50dd1d02008-08-11 23:46:25 +000081 void dump() {
82 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000083 cerr << "Base.Reg ";
84 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
85 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000086 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
87 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000088 cerr << "IndexReg ";
89 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
90 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000091 cerr << " Disp " << Disp << "\n";
92 cerr << "GV "; if (GV) GV->dump();
93 else cerr << "nul";
94 cerr << " CP "; if (CP) CP->dump();
95 else cerr << "nul";
96 cerr << "\n";
97 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
98 cerr << " JT" << JT << " Align" << Align << "\n";
99 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000100 };
101}
102
103namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000104 //===--------------------------------------------------------------------===//
105 /// ISel - X86 specific code to select X86 machine instructions for
106 /// SelectionDAG operations.
107 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000108 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000109 /// ContainsFPCode - Every instruction we select that uses or defines a FP
110 /// register should set this to true.
111 bool ContainsFPCode;
112
Evan Cheng25ab6902006-09-08 06:48:29 +0000113 /// TM - Keep a reference to X86TargetMachine.
114 ///
115 X86TargetMachine &TM;
116
Chris Lattnerc961eea2005-11-16 01:54:32 +0000117 /// X86Lowering - This object fully describes how to lower LLVM code to an
118 /// X86-specific SelectionDAG.
119 X86TargetLowering X86Lowering;
120
121 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
122 /// make the right decision when generating code for different targets.
123 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000124
Evan Cheng25ab6902006-09-08 06:48:29 +0000125 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
126 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000127 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000128
Evan Chengdb8d56b2008-06-30 20:45:06 +0000129 /// CurBB - Current BB being isel'd.
130 ///
131 MachineBasicBlock *CurBB;
132
Chris Lattnerc961eea2005-11-16 01:54:32 +0000133 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000134 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng4576f6d2008-07-01 18:05:03 +0000135 : SelectionDAGISel(X86Lowering, fast),
136 ContainsFPCode(false), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000137 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000138 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000139
Evan Cheng7ccced62006-02-18 00:15:05 +0000140 virtual bool runOnFunction(Function &Fn) {
141 // Make sure we re-emit a set of the global base reg if necessary
142 GlobalBaseReg = 0;
143 return SelectionDAGISel::runOnFunction(Fn);
144 }
145
Chris Lattnerc961eea2005-11-16 01:54:32 +0000146 virtual const char *getPassName() const {
147 return "X86 DAG->DAG Instruction Selection";
148 }
149
Evan Chengdb8d56b2008-06-30 20:45:06 +0000150 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000152 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000153
154 /// InstructionSelectPostProcessing - Post processing of selected and
155 /// scheduled basic blocks.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000156 virtual void InstructionSelectPostProcessing();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000157
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000158 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
159
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000160 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000161
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162// Include the pieces autogenerated from the target description.
163#include "X86GenDAGISel.inc"
164
165 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000166 SDNode *Select(SDValue N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000167
Dan Gohman475871a2008-07-27 21:46:04 +0000168 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000169 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000170 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000171 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000172 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
175 SDValue &Scale, SDValue &Index, SDValue &Disp);
176 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
177 SDValue N, SDValue &Base, SDValue &Scale,
178 SDValue &Index, SDValue &Disp,
179 SDValue &InChain, SDValue &OutChain);
180 bool TryFoldLoad(SDValue P, SDValue N,
181 SDValue &Base, SDValue &Scale,
182 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000183 void PreprocessForRMW();
184 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000185
Chris Lattnerc0bad572006-06-08 18:03:49 +0000186 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
187 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000188 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000189 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000190 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000191
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000192 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
193
Dan Gohman475871a2008-07-27 21:46:04 +0000194 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
195 SDValue &Scale, SDValue &Index,
196 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000197 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
199 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000200 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000201 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 // These are 32-bit even in 64-bit mode since RIP relative offset
203 // is 32-bit.
204 if (AM.GV)
205 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
206 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000207 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
208 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else if (AM.ES)
210 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
211 else if (AM.JT != -1)
212 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
213 else
214 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000215 }
216
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000217 /// getI8Imm - Return a target constant with the specified value, of type
218 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000219 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000220 return CurDAG->getTargetConstant(Imm, MVT::i8);
221 }
222
Chris Lattnerc961eea2005-11-16 01:54:32 +0000223 /// getI16Imm - Return a target constant with the specified value, of type
224 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000226 return CurDAG->getTargetConstant(Imm, MVT::i16);
227 }
228
229 /// getI32Imm - Return a target constant with the specified value, of type
230 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000231 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000232 return CurDAG->getTargetConstant(Imm, MVT::i32);
233 }
Evan Chengf597dc72006-02-10 22:24:32 +0000234
Evan Cheng7ccced62006-02-18 00:15:05 +0000235 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
236 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000237 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000238
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000239 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
240 /// truncate of the specified operand to i8. This can be done with tablegen,
241 /// except that this code uses MVT::Flag in a tricky way that happens to
242 /// improve scheduling in some cases.
243 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000244
Evan Cheng23addc02006-02-10 22:46:26 +0000245#ifndef NDEBUG
246 unsigned Indent;
247#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000248 };
249}
250
Gabor Greif93c53e52008-08-31 15:37:04 +0000251/// findFlagUse - Return use of MVT::Flag value produced by the specified
252/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000253///
Evan Chenga275ecb2006-10-10 01:46:56 +0000254static SDNode *findFlagUse(SDNode *N) {
255 unsigned FlagResNo = N->getNumValues()-1;
256 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000257 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000258 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000259 SDValue Op = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +0000260 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000261 return User;
262 }
263 }
264 return NULL;
265}
266
Evan Chengcdda25d2008-04-25 08:22:20 +0000267/// findNonImmUse - Return true by reference in "found" if "Use" is an
268/// non-immediate use of "Def". This function recursively traversing
269/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000270static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
271 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000272 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000273 if (found ||
274 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000275 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000276 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000277
Evan Cheng27e1fe92006-10-14 08:33:25 +0000278 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000279 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000280 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000281 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000282 if (N == Def) {
283 if (Use == ImmedUse)
Evan Cheng419ace92008-04-25 08:55:28 +0000284 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000285 if (Use == Root) {
Evan Cheng419ace92008-04-25 08:55:28 +0000286 // Must be a chain reading node where it is possible to reach its own
287 // chain operand through a path started from another operand.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000288 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000289 Use->getOpcode() == X86ISD::CMP ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000290 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
291 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000292 continue;
293 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000294 found = true;
295 break;
296 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000297
298 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000299 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000300 }
301}
302
Evan Cheng27e1fe92006-10-14 08:33:25 +0000303/// isNonImmUse - Start searching from Root up the DAG to check is Def can
304/// be reached. Return true if that's the case. However, ignore direct uses
305/// by ImmedUse (which would be U in the example illustrated in
306/// CanBeFoldedBy) and by Root (which can happen in the store case).
307/// FIXME: to be really generic, we should allow direct use by any node
308/// that is being folded. But realisticly since we only fold loads which
309/// have one non-chain use, we only need to watch out for load/op/store
310/// and load/op/cmp case where the root (store / cmp) may reach the load via
311/// its chain operand.
312static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
313 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000314 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000315 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000316 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000317 return found;
318}
319
320
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000321bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000322 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000323
Evan Chenga8df1b42006-07-27 16:44:36 +0000324 // If U use can somehow reach N through another path then U can't fold N or
325 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000326 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000327 // a successor of U.
328 //
329 // [ N ]
330 // ^ ^
331 // | |
332 // / \---
333 // / [X]
334 // | ^
335 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000336
337 if (isNonImmUse(Root, N, U))
338 return false;
339
340 // If U produces a flag, then it gets (even more) interesting. Since it
341 // would have been "glued" together with its flag use, we need to check if
342 // it might reach N:
343 //
344 // [ N ]
345 // ^ ^
346 // | |
347 // [U] \--
348 // ^ [TF]
349 // | ^
350 // | |
351 // \ /
352 // [FU]
353 //
354 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
355 // NU), then TF is a predecessor of FU and a successor of NU. But since
356 // NU and FU are flagged together, this effectively creates a cycle.
357 bool HasFlagUse = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000358 MVT VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000359 while ((VT == MVT::Flag && !Root->use_empty())) {
360 SDNode *FU = findFlagUse(Root);
361 if (FU == NULL)
362 break;
363 else {
364 Root = FU;
365 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000366 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000367 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000368 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000369
370 if (HasFlagUse)
371 return !isNonImmUse(Root, N, Root, U);
372 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000373}
374
Evan Cheng70e674e2006-08-28 20:10:17 +0000375/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
376/// and move load below the TokenFactor. Replace store's chain operand with
377/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000378static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000379 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000380 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000381 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
382 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000383 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000384 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000385 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000386 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
387 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
388 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
389 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000390}
391
Evan Chengcd0baf22008-05-23 21:23:16 +0000392/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
393///
Dan Gohman475871a2008-07-27 21:46:04 +0000394static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
395 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000396 if (N.getOpcode() == ISD::BIT_CONVERT)
397 N = N.getOperand(0);
398
399 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
400 if (!LD || LD->isVolatile())
401 return false;
402 if (LD->getAddressingMode() != ISD::UNINDEXED)
403 return false;
404
405 ISD::LoadExtType ExtType = LD->getExtensionType();
406 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
407 return false;
408
409 if (N.hasOneUse() &&
410 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000411 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000412 Load = N;
413 return true;
414 }
415 return false;
416}
417
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
419/// operand and move load below the call's chain operand.
420static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
421 SDValue Call, SDValue Chain) {
422 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000423 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
424 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000425 Ops.push_back(Load.getOperand(0));
426 else
427 Ops.push_back(Chain.getOperand(i));
428 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
429 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
430 Load.getOperand(1), Load.getOperand(2));
431 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000432 Ops.push_back(SDValue(Load.getNode(), 1));
433 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000434 Ops.push_back(Call.getOperand(i));
435 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
436}
437
438/// isCalleeLoad - Return true if call address is a load and it can be
439/// moved below CALLSEQ_START and the chains leading up to the call.
440/// Return the CALLSEQ_START by reference as a second output.
441static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000442 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000443 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000444 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000445 if (!LD ||
446 LD->isVolatile() ||
447 LD->getAddressingMode() != ISD::UNINDEXED ||
448 LD->getExtensionType() != ISD::NON_EXTLOAD)
449 return false;
450
451 // Now let's find the callseq_start.
452 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
453 if (!Chain.hasOneUse())
454 return false;
455 Chain = Chain.getOperand(0);
456 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000457 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Chengab6c3bb2008-08-25 21:27:18 +0000458}
459
460
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000461/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
462/// This is only run if not in -fast mode (aka -O0).
463/// This allows the instruction selector to pick more read-modify-write
464/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000465///
466/// [Load chain]
467/// ^
468/// |
469/// [Load]
470/// ^ ^
471/// | |
472/// / \-
473/// / |
474/// [TokenFactor] [Op]
475/// ^ ^
476/// | |
477/// \ /
478/// \ /
479/// [Store]
480///
481/// The fact the store's chain operand != load's chain will prevent the
482/// (store (op (load))) instruction from being selected. We can transform it to:
483///
484/// [Load chain]
485/// ^
486/// |
487/// [TokenFactor]
488/// ^
489/// |
490/// [Load]
491/// ^ ^
492/// | |
493/// | \-
494/// | |
495/// | [Op]
496/// | ^
497/// | |
498/// \ /
499/// \ /
500/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000501void X86DAGToDAGISel::PreprocessForRMW() {
502 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
503 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000504 if (I->getOpcode() == X86ISD::CALL) {
505 /// Also try moving call address load from outside callseq_start to just
506 /// before the call to allow it to be folded.
507 ///
508 /// [Load chain]
509 /// ^
510 /// |
511 /// [Load]
512 /// ^ ^
513 /// | |
514 /// / \--
515 /// / |
516 ///[CALLSEQ_START] |
517 /// ^ |
518 /// | |
519 /// [LOAD/C2Reg] |
520 /// | |
521 /// \ /
522 /// \ /
523 /// [CALL]
524 SDValue Chain = I->getOperand(0);
525 SDValue Load = I->getOperand(1);
526 if (!isCalleeLoad(Load, Chain))
527 continue;
528 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
529 ++NumLoadMoved;
530 continue;
531 }
532
Evan Cheng8b2794a2006-10-13 21:14:26 +0000533 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000534 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000536
Gabor Greifba36cb52008-08-28 21:40:38 +0000537 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000538 continue;
539
Dan Gohman475871a2008-07-27 21:46:04 +0000540 SDValue N1 = I->getOperand(1);
541 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000542 if ((N1.getValueType().isFloatingPoint() &&
543 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000544 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000545 continue;
546
547 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000548 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000549 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000550 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000551 case ISD::ADD:
552 case ISD::MUL:
553 case ISD::AND:
554 case ISD::OR:
555 case ISD::XOR:
556 case ISD::ADDC:
557 case ISD::ADDE:
558 case ISD::VECTOR_SHUFFLE: {
559 SDValue N10 = N1.getOperand(0);
560 SDValue N11 = N1.getOperand(1);
561 RModW = isRMWLoad(N10, Chain, N2, Load);
562 if (!RModW)
563 RModW = isRMWLoad(N11, Chain, N2, Load);
564 break;
565 }
566 case ISD::SUB:
567 case ISD::SHL:
568 case ISD::SRA:
569 case ISD::SRL:
570 case ISD::ROTL:
571 case ISD::ROTR:
572 case ISD::SUBC:
573 case ISD::SUBE:
574 case X86ISD::SHLD:
575 case X86ISD::SHRD: {
576 SDValue N10 = N1.getOperand(0);
577 RModW = isRMWLoad(N10, Chain, N2, Load);
578 break;
579 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000580 }
581
Evan Cheng82a35b32006-08-29 06:44:17 +0000582 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000583 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000584 ++NumLoadMoved;
585 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000586 }
587}
588
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000589
590/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
591/// nodes that target the FP stack to be store and load to the stack. This is a
592/// gross hack. We would like to simply mark these as being illegal, but when
593/// we do that, legalize produces these when it expands calls, then expands
594/// these in the same legalize pass. We would like dag combine to be able to
595/// hack on these between the call expansion and the node legalization. As such
596/// this pass basically does "really late" legalization of these inline with the
597/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000598void X86DAGToDAGISel::PreprocessForFPConvert() {
599 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
600 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000601 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
602 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
603 continue;
604
605 // If the source and destination are SSE registers, then this is a legal
606 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 MVT SrcVT = N->getOperand(0).getValueType();
608 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000609 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
610 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
611 if (SrcIsSSE && DstIsSSE)
612 continue;
613
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000614 if (!SrcIsSSE && !DstIsSSE) {
615 // If this is an FPStack extension, it is a noop.
616 if (N->getOpcode() == ISD::FP_EXTEND)
617 continue;
618 // If this is a value-preserving FPStack truncation, it is a noop.
619 if (N->getConstantOperandVal(1))
620 continue;
621 }
622
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000623 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
624 // FPStack has extload and truncstore. SSE can fold direct loads into other
625 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000626 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000627 if (N->getOpcode() == ISD::FP_ROUND)
628 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
629 else
630 MemVT = SrcIsSSE ? SrcVT : DstVT;
631
Dan Gohmanf350b272008-08-23 02:25:05 +0000632 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000633
634 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohmanf350b272008-08-23 02:25:05 +0000635 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
636 N->getOperand(0),
637 MemTmp, NULL, 0, MemVT);
638 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
639 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000640
641 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
642 // extload we created. This will cause general havok on the dag because
643 // anything below the conversion could be folded into other existing nodes.
644 // To avoid invalidating 'I', back it up to the convert node.
645 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000646 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000647
648 // Now that we did that, the node is dead. Increment the iterator to the
649 // next node to process, then delete N.
650 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000651 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000652 }
653}
654
Chris Lattnerc961eea2005-11-16 01:54:32 +0000655/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
656/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000657void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000658 CurBB = BB; // BB can change as result of isel.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000659
Evan Chengdb8d56b2008-06-30 20:45:06 +0000660 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000661 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000662 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000663
664 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000665 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000666
Chris Lattnerc961eea2005-11-16 01:54:32 +0000667 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000668#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000669 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000670 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000671#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000672 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000673#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000674 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000675#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000676
Dan Gohmanf350b272008-08-23 02:25:05 +0000677 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000678}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000679
Dan Gohman462dc7f2008-07-21 20:00:07 +0000680void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000681 // If we are emitting FP stack code, scan the basic block to determine if this
682 // block defines any FP values. If so, put an FP_REG_KILL instruction before
683 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000684
Dale Johannesen48d1e452007-09-24 22:52:39 +0000685 // Note that FP stack instructions are used in all modes for long double,
686 // so we always need to do this check.
687 // Also note that it's possible for an FP stack register to be live across
688 // an instruction that produces multiple basic blocks (SSE CMOV) so we
689 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000690
691 // Scan all of the machine instructions in these MBBs, checking for FP
692 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000693 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000694 MachineFunction::iterator EndMBB = BB; ++EndMBB;
695 for (; MBBI != EndMBB; ++MBBI) {
696 MachineBasicBlock *MBB = MBBI;
697
698 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
699 // before the return.
700 if (!MBB->empty()) {
701 MachineBasicBlock::iterator EndI = MBB->end();
702 --EndI;
703 if (EndI->getDesc().isReturn())
704 continue;
705 }
706
Dale Johannesen48d1e452007-09-24 22:52:39 +0000707 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000708 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000709 !ContainsFPCode && I != E; ++I) {
710 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
711 const TargetRegisterClass *clas;
712 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
713 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000714 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000715 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000716 X86::RFP32RegisterClass ||
717 clas == X86::RFP64RegisterClass ||
718 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000719 ContainsFPCode = true;
720 break;
721 }
722 }
723 }
724 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000725 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
726 // a copy of the input value in this block. In SSE mode, we only care about
727 // 80-bit values.
728 if (!ContainsFPCode) {
729 // Final check, check LLVM BB's that are successors to the LLVM BB
730 // corresponding to BB for FP PHI nodes.
731 const BasicBlock *LLVMBB = BB->getBasicBlock();
732 const PHINode *PN;
733 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
734 !ContainsFPCode && SI != E; ++SI) {
735 for (BasicBlock::const_iterator II = SI->begin();
736 (PN = dyn_cast<PHINode>(II)); ++II) {
737 if (PN->getType()==Type::X86_FP80Ty ||
738 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
739 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
740 ContainsFPCode = true;
741 break;
742 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000743 }
744 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000745 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000746 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
747 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000748 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000749 TM.getInstrInfo()->get(X86::FP_REG_KILL));
750 ++NumFPKill;
751 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000752 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000753}
754
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000755/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
756/// the main function.
757void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
758 MachineFrameInfo *MFI) {
759 const TargetInstrInfo *TII = TM.getInstrInfo();
760 if (Subtarget->isTargetCygMing())
761 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
762}
763
764void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
765 // If this is main, emit special code for main.
766 MachineBasicBlock *BB = MF.begin();
767 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
768 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
769}
770
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000771/// MatchAddress - Add the specified node to the specified addressing mode,
772/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000773/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000774bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000775 bool isRoot, unsigned Depth) {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000776DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000777 // Limit recursion.
778 if (Depth > 5)
779 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000780
Evan Cheng25ab6902006-09-08 06:48:29 +0000781 // RIP relative addressing: %rip + 32-bit displacement!
782 if (AM.isRIPRel) {
783 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000784 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000785 if (isInt32(AM.Disp + Val)) {
786 AM.Disp += Val;
787 return false;
788 }
789 }
790 return true;
791 }
792
Gabor Greifba36cb52008-08-28 21:40:38 +0000793 int id = N.getNode()->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000794 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000795
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000796 switch (N.getOpcode()) {
797 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000798 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000799 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000800 if (isInt32(AM.Disp + Val)) {
801 AM.Disp += Val;
802 return false;
803 }
804 break;
805 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000806
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000807 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000808DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
809DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
810DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000811 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000812 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000813 // Also, base and index reg must be 0 in order to use rip as base.
814 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000815 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000816 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000817 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
818 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000819 // If value is available in a register both base and index components have
820 // been picked, we can't fit the result available in the register in the
821 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greifba36cb52008-08-28 21:40:38 +0000822 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +0000823 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000824 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
825 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000826 AM.GV = GV;
827 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000828 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
829 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000830 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000831 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000832 AM.CP = CP->getConstVal();
833 AM.Align = CP->getAlignment();
834 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000835 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
836 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000837 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000838 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000839 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000840 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
841 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000842 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000843 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000844 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000845 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
846 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000847 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000848 }
849 }
850 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000851 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000852
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000853 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000854 if (AM.BaseType == X86ISelAddressMode::RegBase
855 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000856 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
857 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
858 return false;
859 }
860 break;
Evan Chengec693f72005-12-08 02:01:35 +0000861
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000862 case ISD::SHL:
Gabor Greif93c53e52008-08-31 15:37:04 +0000863 if (AlreadySelected || AM.IndexReg.getNode() != 0
864 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000865 break;
866
Gabor Greif93c53e52008-08-31 15:37:04 +0000867 if (ConstantSDNode
868 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000869 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000870 if (Val == 1 || Val == 2 || Val == 3) {
871 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000872 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000873
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000874 // Okay, we know that we have a scale by now. However, if the scaled
875 // value is an add of something and a constant, we can fold the
876 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000877 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
878 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
879 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000880 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000881 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000882 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000883 if (isInt32(Disp))
884 AM.Disp = Disp;
885 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000886 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000887 } else {
888 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000889 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000890 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000891 }
892 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000893 }
Evan Chengec693f72005-12-08 02:01:35 +0000894
Dan Gohman83688052007-10-22 20:22:24 +0000895 case ISD::SMUL_LOHI:
896 case ISD::UMUL_LOHI:
897 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000898 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000899 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000900 case ISD::MUL:
901 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000902 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000903 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000904 AM.Base.Reg.getNode() == 0 &&
905 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000906 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000907 if (ConstantSDNode
908 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000909 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
910 CN->getZExtValue() == 9) {
911 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000912
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000914 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000915
916 // Okay, we know that we have a scale by now. However, if the scaled
917 // value is an add of something and a constant, we can fold the
918 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000919 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
920 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
921 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000922 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000924 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
925 CN->getZExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000926 if (isInt32(Disp))
927 AM.Disp = Disp;
928 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000929 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000930 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000931 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000932 }
933
934 AM.IndexReg = AM.Base.Reg = Reg;
935 return false;
936 }
Chris Lattner62412262007-02-04 20:18:17 +0000937 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000938 break;
939
Chris Lattner62412262007-02-04 20:18:17 +0000940 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000941 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000942 X86ISelAddressMode Backup = AM;
Gabor Greifba36cb52008-08-28 21:40:38 +0000943 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
944 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000945 return false;
946 AM = Backup;
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
948 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000949 return false;
950 AM = Backup;
951 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000952 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000953
Chris Lattner62412262007-02-04 20:18:17 +0000954 case ISD::OR:
955 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000956 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000957
958 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
959 X86ISelAddressMode Backup = AM;
960 // Start with the LHS as an addr mode.
961 if (!MatchAddress(N.getOperand(0), AM, false) &&
962 // Address could not have picked a GV address for the displacement.
963 AM.GV == NULL &&
964 // On x86-64, the resultant disp must fit in 32-bits.
965 isInt32(AM.Disp + CN->getSignExtended()) &&
966 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000967 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000968 AM.Disp += CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000969 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000970 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000971 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000972 }
973 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000974
975 case ISD::AND: {
976 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
977 // allows us to fold the shift into this addressing mode.
978 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000980 if (Shift.getOpcode() != ISD::SHL) break;
981
982 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000983 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000984
985 // Not when RIP is used as the base.
986 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000987
988 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
989 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
990 if (!C1 || !C2) break;
991
992 // Not likely to be profitable if either the AND or SHIFT node has more
993 // than one use (unless all uses are for address computation). Besides,
994 // isel mechanism requires their node ids to be reused.
995 if (!N.hasOneUse() || !Shift.hasOneUse())
996 break;
997
998 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000999 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001000 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1001 break;
1002
1003 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00001004 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
1005 SDValue(C2, 0), SDValue(C1, 0));
1006 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +00001007 Shift.getOperand(0), NewANDMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00001008 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
1009 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Evan Cheng1314b002007-12-13 00:43:27 +00001010
1011 AM.Scale = 1 << ShiftCst;
1012 AM.IndexReg = NewAND;
1013 return false;
1014 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001015 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001016
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001017 return MatchAddressBase(N, AM, isRoot, Depth);
1018}
1019
1020/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1021/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001022bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001023 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001024 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001025 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001026 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001027 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001028 AM.IndexReg = N;
1029 AM.Scale = 1;
1030 return false;
1031 }
1032
1033 // Otherwise, we cannot select it.
1034 return true;
1035 }
1036
1037 // Default, generate it as a register.
1038 AM.BaseType = X86ISelAddressMode::RegBase;
1039 AM.Base.Reg = N;
1040 return false;
1041}
1042
Evan Chengec693f72005-12-08 02:01:35 +00001043/// SelectAddr - returns true if it is able pattern match an addressing mode.
1044/// It returns the operands which make up the maximal addressing mode it can
1045/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001046bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1047 SDValue &Scale, SDValue &Index,
1048 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001049 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001050 if (MatchAddress(N, AM))
1051 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001052
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001054 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001055 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001056 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001057 }
Evan Cheng8700e142006-01-11 06:09:51 +00001058
Gabor Greifba36cb52008-08-28 21:40:38 +00001059 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001060 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001061
1062 getAddressOperands(AM, Base, Scale, Index, Disp);
1063 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001064}
1065
Chris Lattner4fe4f252006-10-11 22:09:58 +00001066/// isZeroNode - Returns true if Elt is a constant zero or a floating point
1067/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00001068static inline bool isZeroNode(SDValue Elt) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001069 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001070 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Chris Lattner4fe4f252006-10-11 22:09:58 +00001071 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00001072 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +00001073}
1074
1075
Chris Lattner3a7cd952006-10-07 21:55:32 +00001076/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1077/// match a load whose top elements are either undef or zeros. The load flavor
1078/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001079bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1080 SDValue N, SDValue &Base,
1081 SDValue &Scale, SDValue &Index,
1082 SDValue &Disp, SDValue &InChain,
1083 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001084 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001085 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001087 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001088 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001089 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001090 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001091 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001092 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001093 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001094 return true;
1095 }
1096 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001097
1098 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001099 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001100 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001101 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001102 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001103 N.getOperand(0).getNode()->hasOneUse() &&
1104 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001105 N.getOperand(0).getOperand(0).hasOneUse()) {
1106 // Okay, this is a zero extending load. Fold it.
1107 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1108 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1109 return false;
1110 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001111 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001112 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001113 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001114 return false;
1115}
1116
1117
Evan Cheng51a9ed92006-02-25 10:09:08 +00001118/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1119/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001120bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1121 SDValue &Base, SDValue &Scale,
1122 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001123 X86ISelAddressMode AM;
1124 if (MatchAddress(N, AM))
1125 return false;
1126
Duncan Sands83ec4b62008-06-06 12:08:01 +00001127 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001128 unsigned Complexity = 0;
1129 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001130 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001131 Complexity = 1;
1132 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001133 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001134 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1135 Complexity = 4;
1136
Gabor Greifba36cb52008-08-28 21:40:38 +00001137 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001138 Complexity++;
1139 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001140 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001141
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001142 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1143 // a simple shift.
1144 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001145 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001146
1147 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1148 // to a LEA. This is determined with some expermentation but is by no means
1149 // optimal (especially for code size consideration). LEA is nice because of
1150 // its three-address nature. Tweak the cost function again when we can run
1151 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001152 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1153 // For X86-64, we should always use lea to materialize RIP relative
1154 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001155 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001156 Complexity = 4;
1157 else
1158 Complexity += 2;
1159 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001160
Gabor Greifba36cb52008-08-28 21:40:38 +00001161 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001162 Complexity++;
1163
1164 if (Complexity > 2) {
1165 getAddressOperands(AM, Base, Scale, Index, Disp);
1166 return true;
1167 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001168 return false;
1169}
1170
Dan Gohman475871a2008-07-27 21:46:04 +00001171bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1172 SDValue &Base, SDValue &Scale,
1173 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001174 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001175 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001176 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001177 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001178 return false;
1179}
1180
Evan Cheng7ccced62006-02-18 00:15:05 +00001181/// getGlobalBaseReg - Output the instructions required to put the
1182/// base address to use for accessing globals into a register.
1183///
Evan Cheng9ade2182006-08-26 05:34:46 +00001184SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001185 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001186 if (!GlobalBaseReg) {
1187 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001188 MachineFunction *MF = BB->getParent();
1189 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001190 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001191 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001192 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001193
Evan Chengc0f64ff2006-11-27 23:37:22 +00001194 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001195 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1196 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001197 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001198
1199 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1200 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001201 if (TM.getRelocationModel() == Reloc::PIC_ &&
1202 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001203 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001204 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1205 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001206 } else {
1207 GlobalBaseReg = PC;
1208 }
1209
Evan Cheng7ccced62006-02-18 00:15:05 +00001210 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001211 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001212}
1213
Evan Chengb245d922006-05-20 01:36:52 +00001214static SDNode *FindCallStartFromCall(SDNode *Node) {
1215 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1216 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1217 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001218 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001219}
1220
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001221/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1222/// truncate of the specified operand to i8. This can be done with tablegen,
1223/// except that this code uses MVT::Flag in a tricky way that happens to
1224/// improve scheduling in some cases.
1225SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1226 assert(!Subtarget->is64Bit() &&
1227 "getTruncateTo8Bit is only needed on x86-32!");
1228 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1229
1230 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1231 unsigned Opc;
1232 MVT N0VT = N0.getValueType();
1233 switch (N0VT.getSimpleVT()) {
1234 default: assert(0 && "Unknown truncate!");
1235 case MVT::i16:
1236 Opc = X86::MOV16to16_;
1237 break;
1238 case MVT::i32:
1239 Opc = X86::MOV32to32_;
1240 break;
1241 }
1242
1243 // The use of MVT::Flag here is not strictly accurate, but it helps
1244 // scheduling in some cases.
1245 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1246 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1247 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001248}
1249
1250
Dan Gohman475871a2008-07-27 21:46:04 +00001251SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001253 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001254 unsigned Opc, MOpc;
1255 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001256
Evan Chengf597dc72006-02-10 22:24:32 +00001257#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001258 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001259 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001260 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001261 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001262#endif
1263
Dan Gohmane8be6c62008-07-17 19:10:17 +00001264 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001265#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001266 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001267 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001268 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001269 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001270#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001271 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001272 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001273
Evan Cheng0114e942006-01-06 20:36:21 +00001274 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001275 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001276 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001277 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001278
Evan Cheng51a9ed92006-02-25 10:09:08 +00001279 case ISD::ADD: {
1280 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1281 // code and is matched first so to prevent it from being turned into
1282 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001283 // In 64-bit small code size mode, use LEA to take advantage of
1284 // RIP-relative addressing.
1285 if (TM.getCodeModel() != CodeModel::Small)
1286 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001287 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001288 SDValue N0 = N.getOperand(0);
1289 SDValue N1 = N.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001290 if (N.getNode()->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001291 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001292 N1.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001293 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue C(0, 0);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001295 // TODO: handle ExternalSymbolSDNode.
1296 if (GlobalAddressSDNode *G =
1297 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001298 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001299 G->getOffset() + Offset);
1300 } else if (ConstantPoolSDNode *CP =
1301 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001302 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001303 CP->getAlignment(),
1304 CP->getOffset()+Offset);
1305 }
1306
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (C.getNode()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001308 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001310 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif93c53e52008-08-31 15:37:04 +00001311 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1312 MVT::i64, Ops, 4);
Evan Cheng25ab6902006-09-08 06:48:29 +00001313 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00001314 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Evan Cheng25ab6902006-09-08 06:48:29 +00001315 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001316 }
1317
1318 // Other cases are handled by auto-generated code.
1319 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001320 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001321
Dan Gohman525178c2007-10-08 18:33:35 +00001322 case ISD::SMUL_LOHI:
1323 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001324 SDValue N0 = Node->getOperand(0);
1325 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001326
Dan Gohman525178c2007-10-08 18:33:35 +00001327 bool isSigned = Opcode == ISD::SMUL_LOHI;
1328 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001329 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001330 default: assert(0 && "Unsupported VT!");
1331 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1332 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1333 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001334 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001335 }
1336 else
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: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1340 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1341 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001342 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001343 }
1344
1345 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001346 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001347 default: assert(0 && "Unsupported VT!");
1348 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1349 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1350 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001351 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001352 }
1353
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001355 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001356 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001357 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001358 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001359 if (foldedLoad)
1360 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001361 }
1362
Evan Cheng04699902006-08-26 01:05:16 +00001363 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001364 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1365 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001366
1367 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001368 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001369 AddToISelQueue(Tmp0);
1370 AddToISelQueue(Tmp1);
1371 AddToISelQueue(Tmp2);
1372 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001374 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001375 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001376 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001377 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001378 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001379 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001380 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001381 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001382 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001383 }
1384
Dan Gohman525178c2007-10-08 18:33:35 +00001385 // Copy the low half of the result, if it is needed.
1386 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001387 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001388 LoReg, NVT, InFlag);
1389 InFlag = Result.getValue(2);
1390 ReplaceUses(N.getValue(0), Result);
1391#ifndef NDEBUG
1392 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001394 DOUT << "\n";
1395#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001396 }
Dan Gohman525178c2007-10-08 18:33:35 +00001397 // Copy the high half of the result, if it is needed.
1398 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001399 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001400 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1401 // Prevent use of AH in a REX instruction by referencing AX instead.
1402 // Shift it down 8 bits.
1403 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1404 X86::AX, MVT::i16, InFlag);
1405 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001406 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001407 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001408 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1410 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001411 MVT::i8, Result, SRIdx), 0);
1412 } else {
1413 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1414 HiReg, NVT, InFlag);
1415 InFlag = Result.getValue(2);
1416 }
1417 ReplaceUses(N.getValue(1), Result);
1418#ifndef NDEBUG
1419 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001420 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001421 DOUT << "\n";
1422#endif
1423 }
Evan Cheng34167212006-02-09 00:37:58 +00001424
Evan Chengf597dc72006-02-10 22:24:32 +00001425#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001426 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001427#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001428
Evan Cheng64a752f2006-08-11 09:08:15 +00001429 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001430 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001431
Dan Gohman525178c2007-10-08 18:33:35 +00001432 case ISD::SDIVREM:
1433 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001434 SDValue N0 = Node->getOperand(0);
1435 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001436
1437 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001438 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001439 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001440 default: assert(0 && "Unsupported VT!");
1441 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1442 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1443 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001444 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001445 }
1446 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001448 default: assert(0 && "Unsupported VT!");
1449 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1450 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1451 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001452 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001453 }
1454
1455 unsigned LoReg, HiReg;
1456 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001457 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001458 default: assert(0 && "Unsupported VT!");
1459 case MVT::i8:
1460 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001461 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001462 SExtOpcode = X86::CBW;
1463 break;
1464 case MVT::i16:
1465 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001466 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001467 SExtOpcode = X86::CWD;
1468 break;
1469 case MVT::i32:
1470 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001471 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001472 SExtOpcode = X86::CDQ;
1473 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001474 case MVT::i64:
1475 LoReg = X86::RAX; HiReg = X86::RDX;
1476 ClrOpcode = X86::MOV64r0;
1477 SExtOpcode = X86::CQO;
1478 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001479 }
1480
Dan Gohman475871a2008-07-27 21:46:04 +00001481 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001482 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1483
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001485 if (NVT == MVT::i8 && !isSigned) {
1486 // Special case for div8, just use a move with zero extension to AX to
1487 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001489 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001490 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001491 AddToISelQueue(N0.getOperand(0));
1492 AddToISelQueue(Tmp0);
1493 AddToISelQueue(Tmp1);
1494 AddToISelQueue(Tmp2);
1495 AddToISelQueue(Tmp3);
1496 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001497 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001498 Ops, 5), 0);
1499 Chain = Move.getValue(1);
1500 ReplaceUses(N0.getValue(1), Chain);
1501 } else {
1502 AddToISelQueue(N0);
1503 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001504 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001505 Chain = CurDAG->getEntryNode();
1506 }
Dan Gohman475871a2008-07-27 21:46:04 +00001507 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001508 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001509 } else {
1510 AddToISelQueue(N0);
1511 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001512 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001513 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001514 if (isSigned) {
1515 // Sign extend the low part into the high part.
1516 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001517 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001518 } else {
1519 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001520 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001521 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1522 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001523 }
Evan Cheng948f3432006-01-06 23:19:29 +00001524 }
1525
1526 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001527 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001528 AddToISelQueue(Tmp0);
1529 AddToISelQueue(Tmp1);
1530 AddToISelQueue(Tmp2);
1531 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001532 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001533 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001534 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001535 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001536 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001537 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001538 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001539 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001540 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001542 }
1543
Dan Gohmana37c9f72007-09-25 18:23:27 +00001544 // Copy the division (low) result, if it is needed.
1545 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001546 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001547 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001548 InFlag = Result.getValue(2);
1549 ReplaceUses(N.getValue(0), Result);
1550#ifndef NDEBUG
1551 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001552 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001553 DOUT << "\n";
1554#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001555 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001556 // Copy the remainder (high) result, if it is needed.
1557 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001558 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001559 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1560 // Prevent use of AH in a REX instruction by referencing AX instead.
1561 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001562 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1563 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001564 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001565 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001566 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001567 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001568 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1569 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001570 MVT::i8, Result, SRIdx), 0);
1571 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001572 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1573 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001574 InFlag = Result.getValue(2);
1575 }
1576 ReplaceUses(N.getValue(1), Result);
1577#ifndef NDEBUG
1578 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001579 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001580 DOUT << "\n";
1581#endif
1582 }
Evan Chengf597dc72006-02-10 22:24:32 +00001583
1584#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001585 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001586#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001587
1588 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001589 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001590
Christopher Lambc59e5212007-08-10 21:48:46 +00001591 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001592 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001593 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1594 SDValue N0 = Node->getOperand(0);
1595 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001596
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001597 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1598 unsigned Opc = 0;
1599 switch (NVT.getSimpleVT()) {
1600 default: assert(0 && "Unknown sign_extend_inreg!");
1601 case MVT::i16:
1602 Opc = X86::MOVSX16rr8;
1603 break;
1604 case MVT::i32:
1605 Opc = X86::MOVSX32rr8;
1606 break;
1607 }
1608
1609 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001610
1611#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001612 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001613 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001614 DOUT << "\n";
1615 DOUT << std::string(Indent-2, ' ') << "=> ";
1616 DEBUG(ResNode->dump(CurDAG));
1617 DOUT << "\n";
1618 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001619#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001620 return ResNode;
1621 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001622 break;
1623 }
1624
1625 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001626 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1627 SDValue Input = Node->getOperand(0);
1628 AddToISelQueue(Node->getOperand(0));
1629 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001630
Evan Cheng403be7e2006-05-08 08:01:26 +00001631#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001632 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001633 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001634 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001635 Indent -= 2;
1636#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001637 return ResNode;
1638 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001639 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001640 }
Evan Cheng851bc042008-06-17 02:01:22 +00001641
1642 case ISD::DECLARE: {
1643 // Handle DECLARE nodes here because the second operand may have been
1644 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001645 SDValue Chain = Node->getOperand(0);
1646 SDValue N1 = Node->getOperand(1);
1647 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001648 if (!isa<FrameIndexSDNode>(N1))
1649 break;
1650 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1651 if (N2.getOpcode() == ISD::ADD &&
1652 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1653 N2 = N2.getOperand(1);
1654 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001655 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001656 GlobalValue *GV =
1657 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001658 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1659 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001660 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001661 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001662 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1663 MVT::Other, Ops, 3);
1664 }
1665 break;
1666 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001667 }
1668
Evan Cheng9ade2182006-08-26 05:34:46 +00001669 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001670
Evan Chengf597dc72006-02-10 22:24:32 +00001671#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001672 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001673 if (ResNode == NULL || ResNode == N.getNode())
1674 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001675 else
1676 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001677 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001678 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001679#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001680
1681 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001682}
1683
Chris Lattnerc0bad572006-06-08 18:03:49 +00001684bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001685SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001686 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001687 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001688 switch (ConstraintCode) {
1689 case 'o': // offsetable ??
1690 case 'v': // not offsetable ??
1691 default: return true;
1692 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001693 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001694 return true;
1695 break;
1696 }
1697
Evan Cheng04699902006-08-26 01:05:16 +00001698 OutOps.push_back(Op0);
1699 OutOps.push_back(Op1);
1700 OutOps.push_back(Op2);
1701 OutOps.push_back(Op3);
1702 AddToISelQueue(Op0);
1703 AddToISelQueue(Op1);
1704 AddToISelQueue(Op2);
1705 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001706 return false;
1707}
1708
Chris Lattnerc961eea2005-11-16 01:54:32 +00001709/// createX86ISelDag - This pass converts a legalized DAG into a
1710/// X86-specific DAG, ready for instruction scheduling.
1711///
Evan Chenge50794a2006-08-29 18:28:33 +00001712FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1713 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001714}