blob: f9a682894c79462aee792f90bcc991fc868df362 [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)
Bill Wendling056292f2008-09-16 21:48:12 +0000210 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 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
Dan Gohman8b746962008-09-23 18:22:58 +0000235 /// getGlobalBaseReg - Return an SDNode that returns the value of
236 /// the global base register. Output instructions required to
237 /// initialize the global base register, if necessary.
238 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000239 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000240
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000241 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
242 /// truncate of the specified operand to i8. This can be done with tablegen,
243 /// except that this code uses MVT::Flag in a tricky way that happens to
244 /// improve scheduling in some cases.
245 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000246
Evan Cheng23addc02006-02-10 22:46:26 +0000247#ifndef NDEBUG
248 unsigned Indent;
249#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000250 };
251}
252
Gabor Greif93c53e52008-08-31 15:37:04 +0000253/// findFlagUse - Return use of MVT::Flag value produced by the specified
254/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000255///
Evan Chenga275ecb2006-10-10 01:46:56 +0000256static SDNode *findFlagUse(SDNode *N) {
257 unsigned FlagResNo = N->getNumValues()-1;
258 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000259 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000260 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue Op = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +0000262 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000263 return User;
264 }
265 }
266 return NULL;
267}
268
Evan Chengcdda25d2008-04-25 08:22:20 +0000269/// findNonImmUse - Return true by reference in "found" if "Use" is an
270/// non-immediate use of "Def". This function recursively traversing
271/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000272static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman682d5a82008-09-17 01:39:10 +0000273 SDNode *Root, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000274 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000275 if (found ||
276 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000277 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000278 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000279
Evan Cheng27e1fe92006-10-14 08:33:25 +0000280 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000281 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000282 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000283 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000284 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000285 assert(N != Root);
Evan Chengf4b4c412006-08-08 00:31:00 +0000286 found = true;
287 break;
288 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000289
290 // Traverse up the operand chain.
Dan Gohman682d5a82008-09-17 01:39:10 +0000291 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000292 }
293}
294
Evan Cheng27e1fe92006-10-14 08:33:25 +0000295/// isNonImmUse - Start searching from Root up the DAG to check is Def can
296/// be reached. Return true if that's the case. However, ignore direct uses
297/// by ImmedUse (which would be U in the example illustrated in
298/// CanBeFoldedBy) and by Root (which can happen in the store case).
299/// FIXME: to be really generic, we should allow direct use by any node
300/// that is being folded. But realisticly since we only fold loads which
301/// have one non-chain use, we only need to watch out for load/op/store
302/// and load/op/cmp case where the root (store / cmp) may reach the load via
303/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000304static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000305 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000306 bool found = false;
Dan Gohman682d5a82008-09-17 01:39:10 +0000307 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000308 return found;
309}
310
311
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000312bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000313 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000314
Dan Gohman682d5a82008-09-17 01:39:10 +0000315 // If Root use can somehow reach N through a path that that doesn't contain
316 // U then folding N would create a cycle. e.g. In the following
317 // diagram, Root can reach N through X. If N is folded into into Root, then
318 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000319 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000320 // [N*] //
321 // ^ ^ //
322 // / \ //
323 // [U*] [X]? //
324 // ^ ^ //
325 // \ / //
326 // \ / //
327 // [Root*] //
328 //
329 // * indicates nodes to be folded together.
330 //
331 // If Root produces a flag, then it gets (even more) interesting. Since it
332 // will be "glued" together with its flag use in the scheduler, we need to
333 // check if it might reach N.
334 //
335 // [N*] //
336 // ^ ^ //
337 // / \ //
338 // [U*] [X]? //
339 // ^ ^ //
340 // \ \ //
341 // \ | //
342 // [Root*] | //
343 // ^ | //
344 // f | //
345 // | / //
346 // [Y] / //
347 // ^ / //
348 // f / //
349 // | / //
350 // [FU] //
351 //
352 // If FU (flag use) indirectly reaches N (the load), and Root folds N
353 // (call it Fold), then X is a predecessor of FU and a successor of
354 // Fold. But since Fold and FU are flagged together, this will create
355 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000356
Duncan Sands83ec4b62008-06-06 12:08:01 +0000357 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000358 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000359 SDNode *FU = findFlagUse(Root);
360 if (FU == NULL)
361 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000362 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000363 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000364 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000365
Dan Gohman682d5a82008-09-17 01:39:10 +0000366 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000367}
368
Evan Cheng70e674e2006-08-28 20:10:17 +0000369/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
370/// and move load below the TokenFactor. Replace store's chain operand with
371/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000372static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000373 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000374 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000375 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
376 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000377 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000378 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000379 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000380 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
381 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
382 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
383 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000384}
385
Evan Chengcd0baf22008-05-23 21:23:16 +0000386/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
387///
Dan Gohman475871a2008-07-27 21:46:04 +0000388static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
389 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000390 if (N.getOpcode() == ISD::BIT_CONVERT)
391 N = N.getOperand(0);
392
393 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
394 if (!LD || LD->isVolatile())
395 return false;
396 if (LD->getAddressingMode() != ISD::UNINDEXED)
397 return false;
398
399 ISD::LoadExtType ExtType = LD->getExtensionType();
400 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
401 return false;
402
403 if (N.hasOneUse() &&
404 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000405 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000406 Load = N;
407 return true;
408 }
409 return false;
410}
411
Evan Chengab6c3bb2008-08-25 21:27:18 +0000412/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
413/// operand and move load below the call's chain operand.
414static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
415 SDValue Call, SDValue Chain) {
416 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000417 for (unsigned i = 0, e = Chain.getNode()->getNumOperands(); i != e; ++i)
418 if (Load.getNode() == Chain.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000419 Ops.push_back(Load.getOperand(0));
420 else
421 Ops.push_back(Chain.getOperand(i));
422 CurDAG->UpdateNodeOperands(Chain, &Ops[0], Ops.size());
423 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
424 Load.getOperand(1), Load.getOperand(2));
425 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000426 Ops.push_back(SDValue(Load.getNode(), 1));
427 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000428 Ops.push_back(Call.getOperand(i));
429 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
430}
431
432/// isCalleeLoad - Return true if call address is a load and it can be
433/// moved below CALLSEQ_START and the chains leading up to the call.
434/// Return the CALLSEQ_START by reference as a second output.
435static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000436 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000437 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000438 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000439 if (!LD ||
440 LD->isVolatile() ||
441 LD->getAddressingMode() != ISD::UNINDEXED ||
442 LD->getExtensionType() != ISD::NON_EXTLOAD)
443 return false;
444
445 // Now let's find the callseq_start.
446 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
447 if (!Chain.hasOneUse())
448 return false;
449 Chain = Chain.getOperand(0);
450 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000451 return Chain.getOperand(0).getNode() == Callee.getNode();
Evan Chengab6c3bb2008-08-25 21:27:18 +0000452}
453
454
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000455/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
456/// This is only run if not in -fast mode (aka -O0).
457/// This allows the instruction selector to pick more read-modify-write
458/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000459///
460/// [Load chain]
461/// ^
462/// |
463/// [Load]
464/// ^ ^
465/// | |
466/// / \-
467/// / |
468/// [TokenFactor] [Op]
469/// ^ ^
470/// | |
471/// \ /
472/// \ /
473/// [Store]
474///
475/// The fact the store's chain operand != load's chain will prevent the
476/// (store (op (load))) instruction from being selected. We can transform it to:
477///
478/// [Load chain]
479/// ^
480/// |
481/// [TokenFactor]
482/// ^
483/// |
484/// [Load]
485/// ^ ^
486/// | |
487/// | \-
488/// | |
489/// | [Op]
490/// | ^
491/// | |
492/// \ /
493/// \ /
494/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000495void X86DAGToDAGISel::PreprocessForRMW() {
496 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
497 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000498 if (I->getOpcode() == X86ISD::CALL) {
499 /// Also try moving call address load from outside callseq_start to just
500 /// before the call to allow it to be folded.
501 ///
502 /// [Load chain]
503 /// ^
504 /// |
505 /// [Load]
506 /// ^ ^
507 /// | |
508 /// / \--
509 /// / |
510 ///[CALLSEQ_START] |
511 /// ^ |
512 /// | |
513 /// [LOAD/C2Reg] |
514 /// | |
515 /// \ /
516 /// \ /
517 /// [CALL]
518 SDValue Chain = I->getOperand(0);
519 SDValue Load = I->getOperand(1);
520 if (!isCalleeLoad(Load, Chain))
521 continue;
522 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
523 ++NumLoadMoved;
524 continue;
525 }
526
Evan Cheng8b2794a2006-10-13 21:14:26 +0000527 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000528 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000529 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000530
Gabor Greifba36cb52008-08-28 21:40:38 +0000531 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000532 continue;
533
Dan Gohman475871a2008-07-27 21:46:04 +0000534 SDValue N1 = I->getOperand(1);
535 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000536 if ((N1.getValueType().isFloatingPoint() &&
537 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000538 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000539 continue;
540
541 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000542 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000543 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000544 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000545 case ISD::ADD:
546 case ISD::MUL:
547 case ISD::AND:
548 case ISD::OR:
549 case ISD::XOR:
550 case ISD::ADDC:
551 case ISD::ADDE:
552 case ISD::VECTOR_SHUFFLE: {
553 SDValue N10 = N1.getOperand(0);
554 SDValue N11 = N1.getOperand(1);
555 RModW = isRMWLoad(N10, Chain, N2, Load);
556 if (!RModW)
557 RModW = isRMWLoad(N11, Chain, N2, Load);
558 break;
559 }
560 case ISD::SUB:
561 case ISD::SHL:
562 case ISD::SRA:
563 case ISD::SRL:
564 case ISD::ROTL:
565 case ISD::ROTR:
566 case ISD::SUBC:
567 case ISD::SUBE:
568 case X86ISD::SHLD:
569 case X86ISD::SHRD: {
570 SDValue N10 = N1.getOperand(0);
571 RModW = isRMWLoad(N10, Chain, N2, Load);
572 break;
573 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000574 }
575
Evan Cheng82a35b32006-08-29 06:44:17 +0000576 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000577 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000578 ++NumLoadMoved;
579 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000580 }
581}
582
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000583
584/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
585/// nodes that target the FP stack to be store and load to the stack. This is a
586/// gross hack. We would like to simply mark these as being illegal, but when
587/// we do that, legalize produces these when it expands calls, then expands
588/// these in the same legalize pass. We would like dag combine to be able to
589/// hack on these between the call expansion and the node legalization. As such
590/// this pass basically does "really late" legalization of these inline with the
591/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000592void X86DAGToDAGISel::PreprocessForFPConvert() {
593 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
594 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000595 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
596 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
597 continue;
598
599 // If the source and destination are SSE registers, then this is a legal
600 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000601 MVT SrcVT = N->getOperand(0).getValueType();
602 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000603 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
604 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
605 if (SrcIsSSE && DstIsSSE)
606 continue;
607
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000608 if (!SrcIsSSE && !DstIsSSE) {
609 // If this is an FPStack extension, it is a noop.
610 if (N->getOpcode() == ISD::FP_EXTEND)
611 continue;
612 // If this is a value-preserving FPStack truncation, it is a noop.
613 if (N->getConstantOperandVal(1))
614 continue;
615 }
616
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000617 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
618 // FPStack has extload and truncstore. SSE can fold direct loads into other
619 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000620 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000621 if (N->getOpcode() == ISD::FP_ROUND)
622 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
623 else
624 MemVT = SrcIsSSE ? SrcVT : DstVT;
625
Dan Gohmanf350b272008-08-23 02:25:05 +0000626 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000627
628 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohmanf350b272008-08-23 02:25:05 +0000629 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
630 N->getOperand(0),
631 MemTmp, NULL, 0, MemVT);
632 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
633 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000634
635 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
636 // extload we created. This will cause general havok on the dag because
637 // anything below the conversion could be folded into other existing nodes.
638 // To avoid invalidating 'I', back it up to the convert node.
639 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000640 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000641
642 // Now that we did that, the node is dead. Increment the iterator to the
643 // next node to process, then delete N.
644 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000645 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000646 }
647}
648
Chris Lattnerc961eea2005-11-16 01:54:32 +0000649/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
650/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000651void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000652 CurBB = BB; // BB can change as result of isel.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000653
Evan Chengdb8d56b2008-06-30 20:45:06 +0000654 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000655 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000656 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000657
658 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000659 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000660
Chris Lattnerc961eea2005-11-16 01:54:32 +0000661 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000662#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000663 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000664 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000665#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000666 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000667#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000668 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000669#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000670
Dan Gohmanf350b272008-08-23 02:25:05 +0000671 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000672}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000673
Dan Gohman462dc7f2008-07-21 20:00:07 +0000674void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000675 // If we are emitting FP stack code, scan the basic block to determine if this
676 // block defines any FP values. If so, put an FP_REG_KILL instruction before
677 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000678
Dale Johannesen48d1e452007-09-24 22:52:39 +0000679 // Note that FP stack instructions are used in all modes for long double,
680 // so we always need to do this check.
681 // Also note that it's possible for an FP stack register to be live across
682 // an instruction that produces multiple basic blocks (SSE CMOV) so we
683 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000684
685 // Scan all of the machine instructions in these MBBs, checking for FP
686 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000687 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000688 MachineFunction::iterator EndMBB = BB; ++EndMBB;
689 for (; MBBI != EndMBB; ++MBBI) {
690 MachineBasicBlock *MBB = MBBI;
691
692 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
693 // before the return.
694 if (!MBB->empty()) {
695 MachineBasicBlock::iterator EndI = MBB->end();
696 --EndI;
697 if (EndI->getDesc().isReturn())
698 continue;
699 }
700
Dale Johannesen48d1e452007-09-24 22:52:39 +0000701 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000702 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000703 !ContainsFPCode && I != E; ++I) {
704 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
705 const TargetRegisterClass *clas;
706 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
707 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000708 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000709 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000710 X86::RFP32RegisterClass ||
711 clas == X86::RFP64RegisterClass ||
712 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000713 ContainsFPCode = true;
714 break;
715 }
716 }
717 }
718 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000719 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
720 // a copy of the input value in this block. In SSE mode, we only care about
721 // 80-bit values.
722 if (!ContainsFPCode) {
723 // Final check, check LLVM BB's that are successors to the LLVM BB
724 // corresponding to BB for FP PHI nodes.
725 const BasicBlock *LLVMBB = BB->getBasicBlock();
726 const PHINode *PN;
727 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
728 !ContainsFPCode && SI != E; ++SI) {
729 for (BasicBlock::const_iterator II = SI->begin();
730 (PN = dyn_cast<PHINode>(II)); ++II) {
731 if (PN->getType()==Type::X86_FP80Ty ||
732 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
733 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
734 ContainsFPCode = true;
735 break;
736 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000737 }
738 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000739 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000740 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
741 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000742 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000743 TM.getInstrInfo()->get(X86::FP_REG_KILL));
744 ++NumFPKill;
745 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000746 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000747}
748
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000749/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
750/// the main function.
751void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
752 MachineFrameInfo *MFI) {
753 const TargetInstrInfo *TII = TM.getInstrInfo();
754 if (Subtarget->isTargetCygMing())
755 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
756}
757
758void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
759 // If this is main, emit special code for main.
760 MachineBasicBlock *BB = MF.begin();
761 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
762 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
763}
764
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000765/// MatchAddress - Add the specified node to the specified addressing mode,
766/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000767/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000768bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000769 bool isRoot, unsigned Depth) {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000770DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000771 // Limit recursion.
772 if (Depth > 5)
773 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000774
Evan Cheng25ab6902006-09-08 06:48:29 +0000775 // RIP relative addressing: %rip + 32-bit displacement!
776 if (AM.isRIPRel) {
777 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000778 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000779 if (isInt32(AM.Disp + Val)) {
780 AM.Disp += Val;
781 return false;
782 }
783 }
784 return true;
785 }
786
Gabor Greifba36cb52008-08-28 21:40:38 +0000787 int id = N.getNode()->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000788 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000789
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000790 switch (N.getOpcode()) {
791 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000792 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000793 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000794 if (isInt32(AM.Disp + Val)) {
795 AM.Disp += Val;
796 return false;
797 }
798 break;
799 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000800
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000801 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000802DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
803DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
804DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000805 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000806 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000807 // Also, base and index reg must be 0 in order to use rip as base.
808 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000810 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000811 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
812 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000813 // If value is available in a register both base and index components have
814 // been picked, we can't fit the result available in the register in the
815 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greifba36cb52008-08-28 21:40:38 +0000816 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000818 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
819 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000820 AM.GV = GV;
821 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000822 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
823 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000824 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000825 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000826 AM.CP = CP->getConstVal();
827 AM.Align = CP->getAlignment();
828 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000829 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
830 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000831 return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000832 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000833 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000834 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
835 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000836 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000837 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000838 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000839 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
840 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000841 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000842 }
843 }
844 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000845 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000846
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000847 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000848 if (AM.BaseType == X86ISelAddressMode::RegBase
849 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000850 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
851 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
852 return false;
853 }
854 break;
Evan Chengec693f72005-12-08 02:01:35 +0000855
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000856 case ISD::SHL:
Gabor Greif93c53e52008-08-31 15:37:04 +0000857 if (AlreadySelected || AM.IndexReg.getNode() != 0
858 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000859 break;
860
Gabor Greif93c53e52008-08-31 15:37:04 +0000861 if (ConstantSDNode
862 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000863 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000864 if (Val == 1 || Val == 2 || Val == 3) {
865 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000866 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000867
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000868 // Okay, we know that we have a scale by now. However, if the scaled
869 // value is an add of something and a constant, we can fold the
870 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000871 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
872 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
873 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000874 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000875 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000876 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000877 if (isInt32(Disp))
878 AM.Disp = Disp;
879 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000880 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000881 } else {
882 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000883 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000884 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000885 }
886 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000887 }
Evan Chengec693f72005-12-08 02:01:35 +0000888
Dan Gohman83688052007-10-22 20:22:24 +0000889 case ISD::SMUL_LOHI:
890 case ISD::UMUL_LOHI:
891 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000892 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000893 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894 case ISD::MUL:
895 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000896 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000897 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000898 AM.Base.Reg.getNode() == 0 &&
899 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000900 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000901 if (ConstantSDNode
902 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000903 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
904 CN->getZExtValue() == 9) {
905 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000906
Gabor Greifba36cb52008-08-28 21:40:38 +0000907 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000908 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000909
910 // Okay, we know that we have a scale by now. However, if the scaled
911 // value is an add of something and a constant, we can fold the
912 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
914 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
915 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000917 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000918 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
919 CN->getZExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000920 if (isInt32(Disp))
921 AM.Disp = Disp;
922 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000924 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000926 }
927
928 AM.IndexReg = AM.Base.Reg = Reg;
929 return false;
930 }
Chris Lattner62412262007-02-04 20:18:17 +0000931 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000932 break;
933
Chris Lattner62412262007-02-04 20:18:17 +0000934 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000935 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000936 X86ISelAddressMode Backup = AM;
Gabor Greifba36cb52008-08-28 21:40:38 +0000937 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
938 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000939 return false;
940 AM = Backup;
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
942 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000943 return false;
944 AM = Backup;
945 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000946 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000947
Chris Lattner62412262007-02-04 20:18:17 +0000948 case ISD::OR:
949 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000950 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000951
952 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
953 X86ISelAddressMode Backup = AM;
954 // Start with the LHS as an addr mode.
955 if (!MatchAddress(N.getOperand(0), AM, false) &&
956 // Address could not have picked a GV address for the displacement.
957 AM.GV == NULL &&
958 // On x86-64, the resultant disp must fit in 32-bits.
959 isInt32(AM.Disp + CN->getSignExtended()) &&
960 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000961 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000962 AM.Disp += CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000963 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000964 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000965 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000966 }
967 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000968
969 case ISD::AND: {
970 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
971 // allows us to fold the shift into this addressing mode.
972 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000973 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000974 if (Shift.getOpcode() != ISD::SHL) break;
975
976 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000977 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000978
979 // Not when RIP is used as the base.
980 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000981
982 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
983 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
984 if (!C1 || !C2) break;
985
986 // Not likely to be profitable if either the AND or SHIFT node has more
987 // than one use (unless all uses are for address computation). Besides,
988 // isel mechanism requires their node ids to be reused.
989 if (!N.hasOneUse() || !Shift.hasOneUse())
990 break;
991
992 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000993 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +0000994 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
995 break;
996
997 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +0000998 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
999 SDValue(C2, 0), SDValue(C1, 0));
1000 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +00001001 Shift.getOperand(0), NewANDMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00001002 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
1003 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Evan Cheng1314b002007-12-13 00:43:27 +00001004
1005 AM.Scale = 1 << ShiftCst;
1006 AM.IndexReg = NewAND;
1007 return false;
1008 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001009 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001010
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001011 return MatchAddressBase(N, AM, isRoot, Depth);
1012}
1013
1014/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1015/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001016bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001017 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001018 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001019 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001020 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001022 AM.IndexReg = N;
1023 AM.Scale = 1;
1024 return false;
1025 }
1026
1027 // Otherwise, we cannot select it.
1028 return true;
1029 }
1030
1031 // Default, generate it as a register.
1032 AM.BaseType = X86ISelAddressMode::RegBase;
1033 AM.Base.Reg = N;
1034 return false;
1035}
1036
Evan Chengec693f72005-12-08 02:01:35 +00001037/// SelectAddr - returns true if it is able pattern match an addressing mode.
1038/// It returns the operands which make up the maximal addressing mode it can
1039/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001040bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1041 SDValue &Scale, SDValue &Index,
1042 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001043 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001044 if (MatchAddress(N, AM))
1045 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001046
Duncan Sands83ec4b62008-06-06 12:08:01 +00001047 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001048 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001049 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001050 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001051 }
Evan Cheng8700e142006-01-11 06:09:51 +00001052
Gabor Greifba36cb52008-08-28 21:40:38 +00001053 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001054 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001055
1056 getAddressOperands(AM, Base, Scale, Index, Disp);
1057 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001058}
1059
Chris Lattner3a7cd952006-10-07 21:55:32 +00001060/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1061/// match a load whose top elements are either undef or zeros. The load flavor
1062/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001063bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1064 SDValue N, SDValue &Base,
1065 SDValue &Scale, SDValue &Index,
1066 SDValue &Disp, SDValue &InChain,
1067 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001068 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001069 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001070 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001071 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001072 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001073 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001074 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001075 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001076 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001077 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001078 return true;
1079 }
1080 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001081
1082 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001083 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001085 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001086 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001087 N.getOperand(0).getNode()->hasOneUse() &&
1088 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001089 N.getOperand(0).getOperand(0).hasOneUse()) {
1090 // Okay, this is a zero extending load. Fold it.
1091 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1092 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1093 return false;
1094 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001095 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001096 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001097 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001098 return false;
1099}
1100
1101
Evan Cheng51a9ed92006-02-25 10:09:08 +00001102/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1103/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001104bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1105 SDValue &Base, SDValue &Scale,
1106 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001107 X86ISelAddressMode AM;
1108 if (MatchAddress(N, AM))
1109 return false;
1110
Duncan Sands83ec4b62008-06-06 12:08:01 +00001111 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001112 unsigned Complexity = 0;
1113 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001114 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001115 Complexity = 1;
1116 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001117 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001118 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1119 Complexity = 4;
1120
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001122 Complexity++;
1123 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001124 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001125
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001126 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1127 // a simple shift.
1128 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001129 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001130
1131 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1132 // to a LEA. This is determined with some expermentation but is by no means
1133 // optimal (especially for code size consideration). LEA is nice because of
1134 // its three-address nature. Tweak the cost function again when we can run
1135 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001136 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1137 // For X86-64, we should always use lea to materialize RIP relative
1138 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001139 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001140 Complexity = 4;
1141 else
1142 Complexity += 2;
1143 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001144
Gabor Greifba36cb52008-08-28 21:40:38 +00001145 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001146 Complexity++;
1147
1148 if (Complexity > 2) {
1149 getAddressOperands(AM, Base, Scale, Index, Disp);
1150 return true;
1151 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001152 return false;
1153}
1154
Dan Gohman475871a2008-07-27 21:46:04 +00001155bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1156 SDValue &Base, SDValue &Scale,
1157 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001159 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001160 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001161 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001162 return false;
1163}
1164
Dan Gohman8b746962008-09-23 18:22:58 +00001165/// getGlobalBaseReg - Return an SDNode that returns the value of
1166/// the global base register. Output instructions required to
1167/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001168///
Evan Cheng9ade2182006-08-26 05:34:46 +00001169SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001170 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Dan Gohman8b746962008-09-23 18:22:58 +00001171 if (!GlobalBaseReg)
1172 GlobalBaseReg = TM.getInstrInfo()->initializeGlobalBaseReg(BB->getParent());
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001174}
1175
Evan Chengb245d922006-05-20 01:36:52 +00001176static SDNode *FindCallStartFromCall(SDNode *Node) {
1177 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1178 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1179 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001180 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001181}
1182
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001183/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1184/// truncate of the specified operand to i8. This can be done with tablegen,
1185/// except that this code uses MVT::Flag in a tricky way that happens to
1186/// improve scheduling in some cases.
1187SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1188 assert(!Subtarget->is64Bit() &&
1189 "getTruncateTo8Bit is only needed on x86-32!");
1190 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1191
1192 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1193 unsigned Opc;
1194 MVT N0VT = N0.getValueType();
1195 switch (N0VT.getSimpleVT()) {
1196 default: assert(0 && "Unknown truncate!");
1197 case MVT::i16:
1198 Opc = X86::MOV16to16_;
1199 break;
1200 case MVT::i32:
1201 Opc = X86::MOV32to32_;
1202 break;
1203 }
1204
1205 // The use of MVT::Flag here is not strictly accurate, but it helps
1206 // scheduling in some cases.
1207 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1208 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1209 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001210}
1211
1212
Dan Gohman475871a2008-07-27 21:46:04 +00001213SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001215 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001216 unsigned Opc, MOpc;
1217 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001218
Evan Chengf597dc72006-02-10 22:24:32 +00001219#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001220 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001221 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001222 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001223 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001224#endif
1225
Dan Gohmane8be6c62008-07-17 19:10:17 +00001226 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001227#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001228 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001229 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001230 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001231 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001232#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001233 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001234 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001235
Evan Cheng0114e942006-01-06 20:36:21 +00001236 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001237 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001238 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001239 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001240
Evan Cheng51a9ed92006-02-25 10:09:08 +00001241 case ISD::ADD: {
1242 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1243 // code and is matched first so to prevent it from being turned into
1244 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001245 // In 64-bit small code size mode, use LEA to take advantage of
1246 // RIP-relative addressing.
1247 if (TM.getCodeModel() != CodeModel::Small)
1248 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001249 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001250 SDValue N0 = N.getOperand(0);
1251 SDValue N1 = N.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (N.getNode()->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001253 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001254 N1.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001255 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001256 SDValue C(0, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001257 // TODO: handle ExternalSymbolSDNode.
Evan Cheng51a9ed92006-02-25 10:09:08 +00001258 if (GlobalAddressSDNode *G =
1259 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001260 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001261 G->getOffset() + Offset);
1262 } else if (ConstantPoolSDNode *CP =
1263 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001264 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001265 CP->getAlignment(),
1266 CP->getOffset()+Offset);
1267 }
1268
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 if (C.getNode()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001270 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001272 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif93c53e52008-08-31 15:37:04 +00001273 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1274 MVT::i64, Ops, 4);
Evan Cheng25ab6902006-09-08 06:48:29 +00001275 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00001276 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Evan Cheng25ab6902006-09-08 06:48:29 +00001277 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001278 }
1279
1280 // Other cases are handled by auto-generated code.
1281 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001282 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001283
Dan Gohman525178c2007-10-08 18:33:35 +00001284 case ISD::SMUL_LOHI:
1285 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001286 SDValue N0 = Node->getOperand(0);
1287 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001288
Dan Gohman525178c2007-10-08 18:33:35 +00001289 bool isSigned = Opcode == ISD::SMUL_LOHI;
1290 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001291 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001292 default: assert(0 && "Unsupported VT!");
1293 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1294 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1295 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001296 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001297 }
1298 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001299 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001300 default: assert(0 && "Unsupported VT!");
1301 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1302 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1303 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001304 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001305 }
1306
1307 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001308 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001309 default: assert(0 && "Unsupported VT!");
1310 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1311 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1312 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001313 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001314 }
1315
Dan Gohman475871a2008-07-27 21:46:04 +00001316 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001317 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001318 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001319 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001320 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001321 if (foldedLoad)
1322 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001323 }
1324
Evan Cheng04699902006-08-26 01:05:16 +00001325 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001326 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1327 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001328
1329 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001330 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001331 AddToISelQueue(Tmp0);
1332 AddToISelQueue(Tmp1);
1333 AddToISelQueue(Tmp2);
1334 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001335 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001336 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001337 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001338 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001339 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001340 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001341 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001342 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001343 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001344 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001345 }
1346
Dan Gohman525178c2007-10-08 18:33:35 +00001347 // Copy the low half of the result, if it is needed.
1348 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001350 LoReg, NVT, InFlag);
1351 InFlag = Result.getValue(2);
1352 ReplaceUses(N.getValue(0), Result);
1353#ifndef NDEBUG
1354 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001355 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001356 DOUT << "\n";
1357#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001358 }
Dan Gohman525178c2007-10-08 18:33:35 +00001359 // Copy the high half of the result, if it is needed.
1360 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001361 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001362 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1363 // Prevent use of AH in a REX instruction by referencing AX instead.
1364 // Shift it down 8 bits.
1365 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1366 X86::AX, MVT::i16, InFlag);
1367 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001368 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001369 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001370 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1372 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001373 MVT::i8, Result, SRIdx), 0);
1374 } else {
1375 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1376 HiReg, NVT, InFlag);
1377 InFlag = Result.getValue(2);
1378 }
1379 ReplaceUses(N.getValue(1), Result);
1380#ifndef NDEBUG
1381 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001382 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001383 DOUT << "\n";
1384#endif
1385 }
Evan Cheng34167212006-02-09 00:37:58 +00001386
Evan Chengf597dc72006-02-10 22:24:32 +00001387#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001388 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001389#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001390
Evan Cheng64a752f2006-08-11 09:08:15 +00001391 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001392 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001393
Dan Gohman525178c2007-10-08 18:33:35 +00001394 case ISD::SDIVREM:
1395 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SDValue N0 = Node->getOperand(0);
1397 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001398
1399 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001400 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001401 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001402 default: assert(0 && "Unsupported VT!");
1403 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1404 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1405 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001406 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001407 }
1408 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001409 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001410 default: assert(0 && "Unsupported VT!");
1411 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1412 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1413 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001414 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001415 }
1416
1417 unsigned LoReg, HiReg;
1418 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001419 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001420 default: assert(0 && "Unsupported VT!");
1421 case MVT::i8:
1422 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001423 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001424 SExtOpcode = X86::CBW;
1425 break;
1426 case MVT::i16:
1427 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001428 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001429 SExtOpcode = X86::CWD;
1430 break;
1431 case MVT::i32:
1432 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001433 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001434 SExtOpcode = X86::CDQ;
1435 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001436 case MVT::i64:
1437 LoReg = X86::RAX; HiReg = X86::RDX;
1438 ClrOpcode = X86::MOV64r0;
1439 SExtOpcode = X86::CQO;
1440 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001441 }
1442
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001444 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1445
Dan Gohman475871a2008-07-27 21:46:04 +00001446 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001447 if (NVT == MVT::i8 && !isSigned) {
1448 // Special case for div8, just use a move with zero extension to AX to
1449 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001451 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001452 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001453 AddToISelQueue(N0.getOperand(0));
1454 AddToISelQueue(Tmp0);
1455 AddToISelQueue(Tmp1);
1456 AddToISelQueue(Tmp2);
1457 AddToISelQueue(Tmp3);
1458 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001460 Ops, 5), 0);
1461 Chain = Move.getValue(1);
1462 ReplaceUses(N0.getValue(1), Chain);
1463 } else {
1464 AddToISelQueue(N0);
1465 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001467 Chain = CurDAG->getEntryNode();
1468 }
Dan Gohman475871a2008-07-27 21:46:04 +00001469 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001470 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001471 } else {
1472 AddToISelQueue(N0);
1473 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001474 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001475 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001476 if (isSigned) {
1477 // Sign extend the low part into the high part.
1478 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001479 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001480 } else {
1481 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001482 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001483 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1484 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001485 }
Evan Cheng948f3432006-01-06 23:19:29 +00001486 }
1487
1488 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001489 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001490 AddToISelQueue(Tmp0);
1491 AddToISelQueue(Tmp1);
1492 AddToISelQueue(Tmp2);
1493 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001494 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001495 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001496 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001497 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001498 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001499 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001500 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001501 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001502 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001503 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001504 }
1505
Dan Gohmana37c9f72007-09-25 18:23:27 +00001506 // Copy the division (low) result, if it is needed.
1507 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001509 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001510 InFlag = Result.getValue(2);
1511 ReplaceUses(N.getValue(0), Result);
1512#ifndef NDEBUG
1513 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001514 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001515 DOUT << "\n";
1516#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001517 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001518 // Copy the remainder (high) result, if it is needed.
1519 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001520 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001521 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1522 // Prevent use of AH in a REX instruction by referencing AX instead.
1523 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001524 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1525 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001526 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001527 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001528 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001529 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001530 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1531 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001532 MVT::i8, Result, SRIdx), 0);
1533 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001534 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1535 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001536 InFlag = Result.getValue(2);
1537 }
1538 ReplaceUses(N.getValue(1), Result);
1539#ifndef NDEBUG
1540 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001542 DOUT << "\n";
1543#endif
1544 }
Evan Chengf597dc72006-02-10 22:24:32 +00001545
1546#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001547 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001548#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001549
1550 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001551 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001552
Christopher Lambc59e5212007-08-10 21:48:46 +00001553 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001554 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001555 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1556 SDValue N0 = Node->getOperand(0);
1557 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001558
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001559 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1560 unsigned Opc = 0;
1561 switch (NVT.getSimpleVT()) {
1562 default: assert(0 && "Unknown sign_extend_inreg!");
1563 case MVT::i16:
1564 Opc = X86::MOVSX16rr8;
1565 break;
1566 case MVT::i32:
1567 Opc = X86::MOVSX32rr8;
1568 break;
1569 }
1570
1571 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001572
1573#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001574 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001575 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001576 DOUT << "\n";
1577 DOUT << std::string(Indent-2, ' ') << "=> ";
1578 DEBUG(ResNode->dump(CurDAG));
1579 DOUT << "\n";
1580 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001581#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001582 return ResNode;
1583 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001584 break;
1585 }
1586
1587 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001588 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1589 SDValue Input = Node->getOperand(0);
1590 AddToISelQueue(Node->getOperand(0));
1591 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001592
Evan Cheng403be7e2006-05-08 08:01:26 +00001593#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001594 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001595 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001596 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001597 Indent -= 2;
1598#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001599 return ResNode;
1600 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001601 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001602 }
Evan Cheng851bc042008-06-17 02:01:22 +00001603
1604 case ISD::DECLARE: {
1605 // Handle DECLARE nodes here because the second operand may have been
1606 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Chain = Node->getOperand(0);
1608 SDValue N1 = Node->getOperand(1);
1609 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001610 if (!isa<FrameIndexSDNode>(N1))
1611 break;
1612 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1613 if (N2.getOpcode() == ISD::ADD &&
1614 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1615 N2 = N2.getOperand(1);
1616 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001617 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001618 GlobalValue *GV =
1619 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001620 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1621 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001622 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001623 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001624 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1625 MVT::Other, Ops, 3);
1626 }
1627 break;
1628 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001629 }
1630
Evan Cheng9ade2182006-08-26 05:34:46 +00001631 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001632
Evan Chengf597dc72006-02-10 22:24:32 +00001633#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001634 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001635 if (ResNode == NULL || ResNode == N.getNode())
1636 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001637 else
1638 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001639 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001640 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001641#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001642
1643 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001644}
1645
Chris Lattnerc0bad572006-06-08 18:03:49 +00001646bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001647SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001648 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001650 switch (ConstraintCode) {
1651 case 'o': // offsetable ??
1652 case 'v': // not offsetable ??
1653 default: return true;
1654 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001655 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001656 return true;
1657 break;
1658 }
1659
Evan Cheng04699902006-08-26 01:05:16 +00001660 OutOps.push_back(Op0);
1661 OutOps.push_back(Op1);
1662 OutOps.push_back(Op2);
1663 OutOps.push_back(Op3);
1664 AddToISelQueue(Op0);
1665 AddToISelQueue(Op1);
1666 AddToISelQueue(Op2);
1667 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001668 return false;
1669}
1670
Chris Lattnerc961eea2005-11-16 01:54:32 +00001671/// createX86ISelDag - This pass converts a legalized DAG into a
1672/// X86-specific DAG, ready for instruction scheduling.
1673///
Evan Chenge50794a2006-08-29 18:28:33 +00001674FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1675 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001676}