blob: 6cc044c7e8518a1928af41d0f2b0ac8efb6badc0 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000042#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000043#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000044using namespace llvm;
45
Chris Lattner95b2c7d2006-12-19 22:59:26 +000046STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
47STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000071 unsigned Disp;
72 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000074 const char *ES;
75 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000076 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000077
78 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000079 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
80 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 }
Dale Johannesen50dd1d02008-08-11 23:46:25 +000082 void dump() {
83 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000084 cerr << "Base.Reg ";
85 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
86 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000087 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
88 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000089 cerr << "IndexReg ";
90 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
91 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000092 cerr << " Disp " << Disp << "\n";
93 cerr << "GV "; if (GV) GV->dump();
94 else cerr << "nul";
95 cerr << " CP "; if (CP) CP->dump();
96 else cerr << "nul";
97 cerr << "\n";
98 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
99 cerr << " JT" << JT << " Align" << Align << "\n";
100 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000101 };
102}
103
104namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000105 //===--------------------------------------------------------------------===//
106 /// ISel - X86 specific code to select X86 machine instructions for
107 /// SelectionDAG operations.
108 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000109 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000110 /// ContainsFPCode - Every instruction we select that uses or defines a FP
111 /// register should set this to true.
112 bool ContainsFPCode;
113
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 /// TM - Keep a reference to X86TargetMachine.
115 ///
116 X86TargetMachine &TM;
117
Chris Lattnerc961eea2005-11-16 01:54:32 +0000118 /// X86Lowering - This object fully describes how to lower LLVM code to an
119 /// X86-specific SelectionDAG.
120 X86TargetLowering X86Lowering;
121
122 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
123 /// make the right decision when generating code for different targets.
124 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000125
Evan Cheng25ab6902006-09-08 06:48:29 +0000126 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
127 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000128 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000129
Evan Chengdb8d56b2008-06-30 20:45:06 +0000130 /// CurBB - Current BB being isel'd.
131 ///
132 MachineBasicBlock *CurBB;
133
Evan Chengb7a75a52008-09-26 23:41:32 +0000134 /// OptForSize - If true, selector should try to optimize for code size
135 /// instead of performance.
136 bool OptForSize;
137
Chris Lattnerc961eea2005-11-16 01:54:32 +0000138 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000139 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng4576f6d2008-07-01 18:05:03 +0000140 : SelectionDAGISel(X86Lowering, fast),
141 ContainsFPCode(false), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000142 X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000143 Subtarget(&TM.getSubtarget<X86Subtarget>()),
144 OptForSize(OptimizeForSize) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000145
Evan Cheng7ccced62006-02-18 00:15:05 +0000146 virtual bool runOnFunction(Function &Fn) {
147 // Make sure we re-emit a set of the global base reg if necessary
148 GlobalBaseReg = 0;
149 return SelectionDAGISel::runOnFunction(Fn);
150 }
151
Chris Lattnerc961eea2005-11-16 01:54:32 +0000152 virtual const char *getPassName() const {
153 return "X86 DAG->DAG Instruction Selection";
154 }
155
Evan Chengdb8d56b2008-06-30 20:45:06 +0000156 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000157 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000158 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000159
160 /// InstructionSelectPostProcessing - Post processing of selected and
161 /// scheduled basic blocks.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000162 virtual void InstructionSelectPostProcessing();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000164 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
165
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000166 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000167
Chris Lattnerc961eea2005-11-16 01:54:32 +0000168// Include the pieces autogenerated from the target description.
169#include "X86GenDAGISel.inc"
170
171 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDNode *Select(SDValue N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000173
Dan Gohman475871a2008-07-27 21:46:04 +0000174 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000175 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000176 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000177 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000178 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
179 SDValue &Scale, SDValue &Index, SDValue &Disp);
180 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
181 SDValue &Scale, SDValue &Index, SDValue &Disp);
182 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
183 SDValue N, SDValue &Base, SDValue &Scale,
184 SDValue &Index, SDValue &Disp,
185 SDValue &InChain, SDValue &OutChain);
186 bool TryFoldLoad(SDValue P, SDValue N,
187 SDValue &Base, SDValue &Scale,
188 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000189 void PreprocessForRMW();
190 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000191
Chris Lattnerc0bad572006-06-08 18:03:49 +0000192 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
193 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000194 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000195 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000196 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000197
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000198 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
199
Dan Gohman475871a2008-07-27 21:46:04 +0000200 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
201 SDValue &Scale, SDValue &Index,
202 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000203 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
205 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000206 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000207 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 // These are 32-bit even in 64-bit mode since RIP relative offset
209 // is 32-bit.
210 if (AM.GV)
211 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
212 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000213 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
214 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000216 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 else if (AM.JT != -1)
218 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
219 else
220 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000221 }
222
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000223 /// getI8Imm - Return a target constant with the specified value, of type
224 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000226 return CurDAG->getTargetConstant(Imm, MVT::i8);
227 }
228
Chris Lattnerc961eea2005-11-16 01:54:32 +0000229 /// getI16Imm - Return a target constant with the specified value, of type
230 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000231 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000232 return CurDAG->getTargetConstant(Imm, MVT::i16);
233 }
234
235 /// getI32Imm - Return a target constant with the specified value, of type
236 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000237 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000238 return CurDAG->getTargetConstant(Imm, MVT::i32);
239 }
Evan Chengf597dc72006-02-10 22:24:32 +0000240
Dan Gohman8b746962008-09-23 18:22:58 +0000241 /// getGlobalBaseReg - Return an SDNode that returns the value of
242 /// the global base register. Output instructions required to
243 /// initialize the global base register, if necessary.
244 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000245 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000246
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000247 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
248 /// truncate of the specified operand to i8. This can be done with tablegen,
249 /// except that this code uses MVT::Flag in a tricky way that happens to
250 /// improve scheduling in some cases.
251 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000252
Evan Cheng23addc02006-02-10 22:46:26 +0000253#ifndef NDEBUG
254 unsigned Indent;
255#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000256 };
257}
258
Gabor Greif93c53e52008-08-31 15:37:04 +0000259/// findFlagUse - Return use of MVT::Flag value produced by the specified
260/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000261///
Evan Chenga275ecb2006-10-10 01:46:56 +0000262static SDNode *findFlagUse(SDNode *N) {
263 unsigned FlagResNo = N->getNumValues()-1;
264 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000265 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000266 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDValue Op = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +0000268 if (Op.getNode() == N && Op.getResNo() == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000269 return User;
270 }
271 }
272 return NULL;
273}
274
Evan Chengcdda25d2008-04-25 08:22:20 +0000275/// findNonImmUse - Return true by reference in "found" if "Use" is an
276/// non-immediate use of "Def". This function recursively traversing
277/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000278static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman682d5a82008-09-17 01:39:10 +0000279 SDNode *Root, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000280 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000281 if (found ||
282 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000283 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000284 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000285
Evan Cheng27e1fe92006-10-14 08:33:25 +0000286 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000287 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000288 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000289 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000290 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000291 assert(N != Root);
Evan Chengf4b4c412006-08-08 00:31:00 +0000292 found = true;
293 break;
294 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000295
296 // Traverse up the operand chain.
Dan Gohman682d5a82008-09-17 01:39:10 +0000297 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000298 }
299}
300
Evan Cheng27e1fe92006-10-14 08:33:25 +0000301/// isNonImmUse - Start searching from Root up the DAG to check is Def can
302/// be reached. Return true if that's the case. However, ignore direct uses
303/// by ImmedUse (which would be U in the example illustrated in
304/// CanBeFoldedBy) and by Root (which can happen in the store case).
305/// FIXME: to be really generic, we should allow direct use by any node
306/// that is being folded. But realisticly since we only fold loads which
307/// have one non-chain use, we only need to watch out for load/op/store
308/// and load/op/cmp case where the root (store / cmp) may reach the load via
309/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000310static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000311 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000312 bool found = false;
Dan Gohman682d5a82008-09-17 01:39:10 +0000313 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000314 return found;
315}
316
317
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000318bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000319 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000320
Dan Gohman682d5a82008-09-17 01:39:10 +0000321 // If Root use can somehow reach N through a path that that doesn't contain
322 // U then folding N would create a cycle. e.g. In the following
323 // diagram, Root can reach N through X. If N is folded into into Root, then
324 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000325 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000326 // [N*] //
327 // ^ ^ //
328 // / \ //
329 // [U*] [X]? //
330 // ^ ^ //
331 // \ / //
332 // \ / //
333 // [Root*] //
334 //
335 // * indicates nodes to be folded together.
336 //
337 // If Root produces a flag, then it gets (even more) interesting. Since it
338 // will be "glued" together with its flag use in the scheduler, we need to
339 // check if it might reach N.
340 //
341 // [N*] //
342 // ^ ^ //
343 // / \ //
344 // [U*] [X]? //
345 // ^ ^ //
346 // \ \ //
347 // \ | //
348 // [Root*] | //
349 // ^ | //
350 // f | //
351 // | / //
352 // [Y] / //
353 // ^ / //
354 // f / //
355 // | / //
356 // [FU] //
357 //
358 // If FU (flag use) indirectly reaches N (the load), and Root folds N
359 // (call it Fold), then X is a predecessor of FU and a successor of
360 // Fold. But since Fold and FU are flagged together, this will create
361 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000362
Duncan Sands83ec4b62008-06-06 12:08:01 +0000363 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000364 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000365 SDNode *FU = findFlagUse(Root);
366 if (FU == NULL)
367 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000368 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000369 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000370 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000371
Dan Gohman682d5a82008-09-17 01:39:10 +0000372 return !isNonImmUse(Root, N, U);
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.
Evan Chengb7a75a52008-09-26 23:41:32 +0000659 if (!OptForSize) {
660 const Function *F = CurDAG->getMachineFunction().getFunction();
Daniel Dunbar02a4da22008-09-27 00:22:09 +0000661 OptForSize = !F->isDeclaration() &&
662 F->hasFnAttr(Attribute::OptimizeForSize);
Evan Chengb7a75a52008-09-26 23:41:32 +0000663 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000664
Evan Chengdb8d56b2008-06-30 20:45:06 +0000665 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000666 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000667 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000668
669 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000670 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000671
Chris Lattnerc961eea2005-11-16 01:54:32 +0000672 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000673#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000674 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000675 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000676#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000677 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000678#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000679 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000680#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000681
Dan Gohmanf350b272008-08-23 02:25:05 +0000682 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000683}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000684
Dan Gohman462dc7f2008-07-21 20:00:07 +0000685void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000686 // If we are emitting FP stack code, scan the basic block to determine if this
687 // block defines any FP values. If so, put an FP_REG_KILL instruction before
688 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000689
Dale Johannesen48d1e452007-09-24 22:52:39 +0000690 // Note that FP stack instructions are used in all modes for long double,
691 // so we always need to do this check.
692 // Also note that it's possible for an FP stack register to be live across
693 // an instruction that produces multiple basic blocks (SSE CMOV) so we
694 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000695
696 // Scan all of the machine instructions in these MBBs, checking for FP
697 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000698 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000699 MachineFunction::iterator EndMBB = BB; ++EndMBB;
700 for (; MBBI != EndMBB; ++MBBI) {
701 MachineBasicBlock *MBB = MBBI;
702
703 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
704 // before the return.
705 if (!MBB->empty()) {
706 MachineBasicBlock::iterator EndI = MBB->end();
707 --EndI;
708 if (EndI->getDesc().isReturn())
709 continue;
710 }
711
Dale Johannesen48d1e452007-09-24 22:52:39 +0000712 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000713 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000714 !ContainsFPCode && I != E; ++I) {
715 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
716 const TargetRegisterClass *clas;
717 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
718 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000719 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000720 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000721 X86::RFP32RegisterClass ||
722 clas == X86::RFP64RegisterClass ||
723 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000724 ContainsFPCode = true;
725 break;
726 }
727 }
728 }
729 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000730 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
731 // a copy of the input value in this block. In SSE mode, we only care about
732 // 80-bit values.
733 if (!ContainsFPCode) {
734 // Final check, check LLVM BB's that are successors to the LLVM BB
735 // corresponding to BB for FP PHI nodes.
736 const BasicBlock *LLVMBB = BB->getBasicBlock();
737 const PHINode *PN;
738 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
739 !ContainsFPCode && SI != E; ++SI) {
740 for (BasicBlock::const_iterator II = SI->begin();
741 (PN = dyn_cast<PHINode>(II)); ++II) {
742 if (PN->getType()==Type::X86_FP80Ty ||
743 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
744 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
745 ContainsFPCode = true;
746 break;
747 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000748 }
749 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000750 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000751 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
752 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000753 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000754 TM.getInstrInfo()->get(X86::FP_REG_KILL));
755 ++NumFPKill;
756 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000757 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000758}
759
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000760/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
761/// the main function.
762void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
763 MachineFrameInfo *MFI) {
764 const TargetInstrInfo *TII = TM.getInstrInfo();
765 if (Subtarget->isTargetCygMing())
766 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
767}
768
769void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
770 // If this is main, emit special code for main.
771 MachineBasicBlock *BB = MF.begin();
772 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
773 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
774}
775
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000776/// MatchAddress - Add the specified node to the specified addressing mode,
777/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000778/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000779bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000780 bool isRoot, unsigned Depth) {
Evan Chengda43bcf2008-09-24 00:05:32 +0000781 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000782 // Limit recursion.
783 if (Depth > 5)
784 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000785
Evan Cheng25ab6902006-09-08 06:48:29 +0000786 // RIP relative addressing: %rip + 32-bit displacement!
787 if (AM.isRIPRel) {
788 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000789 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000790 if (isInt32(AM.Disp + Val)) {
791 AM.Disp += Val;
792 return false;
793 }
794 }
795 return true;
796 }
797
Gabor Greifba36cb52008-08-28 21:40:38 +0000798 int id = N.getNode()->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000799 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000800
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000801 switch (N.getOpcode()) {
802 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000803 case ISD::Constant: {
Dan Gohman7810bfe2008-09-26 21:54:37 +0000804 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000805 if (isInt32(AM.Disp + Val)) {
806 AM.Disp += Val;
807 return false;
808 }
809 break;
810 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000811
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000812 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000813DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
814DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
815DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000816 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000817 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000818 // Also, base and index reg must be 0 in order to use rip as base.
819 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000821 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000822 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
823 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000824 // If value is available in a register both base and index components have
825 // been picked, we can't fit the result available in the register in the
826 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Gabor Greifba36cb52008-08-28 21:40:38 +0000827 if (!AlreadySelected || (AM.Base.Reg.getNode() && AM.IndexReg.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +0000828 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000829 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
830 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000831 AM.GV = GV;
832 AM.Disp += G->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000833 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000834 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000835 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000836 AM.CP = CP->getConstVal();
837 AM.Align = CP->getAlignment();
838 AM.Disp += CP->getOffset();
Dan Gohman97135e12008-09-26 19:15:30 +0000839 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000840 return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000841 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000842 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000843 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000844 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000845 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000846 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000847 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000848 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000849 }
850 }
851 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000852 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000853
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000854 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000855 if (AM.BaseType == X86ISelAddressMode::RegBase
856 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000857 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
858 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
859 return false;
860 }
861 break;
Evan Chengec693f72005-12-08 02:01:35 +0000862
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863 case ISD::SHL:
Gabor Greif93c53e52008-08-31 15:37:04 +0000864 if (AlreadySelected || AM.IndexReg.getNode() != 0
865 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000866 break;
867
Gabor Greif93c53e52008-08-31 15:37:04 +0000868 if (ConstantSDNode
869 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000870 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000871 if (Val == 1 || Val == 2 || Val == 3) {
872 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000873 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000874
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000875 // Okay, we know that we have a scale by now. However, if the scaled
876 // value is an add of something and a constant, we can fold the
877 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000878 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
879 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
880 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000881 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000882 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000883 uint64_t Disp = AM.Disp + (AddVal->getZExtValue() << Val);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000884 if (isInt32(Disp))
885 AM.Disp = Disp;
886 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000887 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000888 } else {
889 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000890 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000891 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000892 }
893 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000894 }
Evan Chengec693f72005-12-08 02:01:35 +0000895
Dan Gohman83688052007-10-22 20:22:24 +0000896 case ISD::SMUL_LOHI:
897 case ISD::UMUL_LOHI:
898 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000899 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000900 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000901 case ISD::MUL:
902 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000903 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000904 AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000905 AM.Base.Reg.getNode() == 0 &&
906 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000907 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000908 if (ConstantSDNode
909 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000910 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
911 CN->getZExtValue() == 9) {
912 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000913
Gabor Greifba36cb52008-08-28 21:40:38 +0000914 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000915 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916
917 // Okay, we know that we have a scale by now. However, if the scaled
918 // value is an add of something and a constant, we can fold the
919 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000920 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
921 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
922 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000923 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000924 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000925 uint64_t Disp = AM.Disp + AddVal->getZExtValue() *
926 CN->getZExtValue();
Evan Cheng25ab6902006-09-08 06:48:29 +0000927 if (isInt32(Disp))
928 AM.Disp = Disp;
929 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000930 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000931 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000932 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000933 }
934
935 AM.IndexReg = AM.Base.Reg = Reg;
936 return false;
937 }
Chris Lattner62412262007-02-04 20:18:17 +0000938 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000939 break;
940
Chris Lattner62412262007-02-04 20:18:17 +0000941 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000942 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000943 X86ISelAddressMode Backup = AM;
Gabor Greifba36cb52008-08-28 21:40:38 +0000944 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
945 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000946 return false;
947 AM = Backup;
Gabor Greifba36cb52008-08-28 21:40:38 +0000948 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
949 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000950 return false;
951 AM = Backup;
952 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000953 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000954
Chris Lattner62412262007-02-04 20:18:17 +0000955 case ISD::OR:
956 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000957 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000958
959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
960 X86ISelAddressMode Backup = AM;
961 // Start with the LHS as an addr mode.
962 if (!MatchAddress(N.getOperand(0), AM, false) &&
963 // Address could not have picked a GV address for the displacement.
964 AM.GV == NULL &&
965 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman7810bfe2008-09-26 21:54:37 +0000966 isInt32(AM.Disp + CN->getSExtValue()) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000967 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000968 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000969 AM.Disp += CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000970 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000971 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000972 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000973 }
974 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000975
976 case ISD::AND: {
977 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
978 // allows us to fold the shift into this addressing mode.
979 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000980 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000981 if (Shift.getOpcode() != ISD::SHL) break;
982
983 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000984 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000985
986 // Not when RIP is used as the base.
987 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000988
989 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
990 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
991 if (!C1 || !C2) break;
992
993 // Not likely to be profitable if either the AND or SHIFT node has more
994 // than one use (unless all uses are for address computation). Besides,
995 // isel mechanism requires their node ids to be reused.
996 if (!N.hasOneUse() || !Shift.hasOneUse())
997 break;
998
999 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001000 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001001 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1002 break;
1003
1004 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
1006 SDValue(C2, 0), SDValue(C1, 0));
1007 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +00001008 Shift.getOperand(0), NewANDMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00001009 NewANDMask.getNode()->setNodeId(Shift.getNode()->getNodeId());
1010 NewAND.getNode()->setNodeId(N.getNode()->getNodeId());
Evan Cheng1314b002007-12-13 00:43:27 +00001011
1012 AM.Scale = 1 << ShiftCst;
1013 AM.IndexReg = NewAND;
1014 return false;
1015 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001016 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001017
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001018 return MatchAddressBase(N, AM, isRoot, Depth);
1019}
1020
1021/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1022/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001023bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001024 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001025 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001026 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001027 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001028 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001029 AM.IndexReg = N;
1030 AM.Scale = 1;
1031 return false;
1032 }
1033
1034 // Otherwise, we cannot select it.
1035 return true;
1036 }
1037
1038 // Default, generate it as a register.
1039 AM.BaseType = X86ISelAddressMode::RegBase;
1040 AM.Base.Reg = N;
1041 return false;
1042}
1043
Evan Chengec693f72005-12-08 02:01:35 +00001044/// SelectAddr - returns true if it is able pattern match an addressing mode.
1045/// It returns the operands which make up the maximal addressing mode it can
1046/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001047bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1048 SDValue &Scale, SDValue &Index,
1049 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001050 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001051 if (MatchAddress(N, AM))
1052 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001053
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001055 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001056 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001057 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001058 }
Evan Cheng8700e142006-01-11 06:09:51 +00001059
Gabor Greifba36cb52008-08-28 21:40:38 +00001060 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001061 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001062
1063 getAddressOperands(AM, Base, Scale, Index, Disp);
1064 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001065}
1066
Chris Lattner3a7cd952006-10-07 21:55:32 +00001067/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1068/// match a load whose top elements are either undef or zeros. The load flavor
1069/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001070bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1071 SDValue N, SDValue &Base,
1072 SDValue &Scale, SDValue &Index,
1073 SDValue &Disp, SDValue &InChain,
1074 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001075 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001076 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001077 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001078 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001079 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 CanBeFoldedBy(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001081 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001082 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001083 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001084 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001085 return true;
1086 }
1087 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001088
1089 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001090 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001091 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001092 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001093 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 N.getOperand(0).getNode()->hasOneUse() &&
1095 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001096 N.getOperand(0).getOperand(0).hasOneUse()) {
1097 // Okay, this is a zero extending load. Fold it.
1098 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1099 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1100 return false;
1101 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001102 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001103 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001104 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001105 return false;
1106}
1107
1108
Evan Cheng51a9ed92006-02-25 10:09:08 +00001109/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1110/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001111bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1112 SDValue &Base, SDValue &Scale,
1113 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001114 X86ISelAddressMode AM;
1115 if (MatchAddress(N, AM))
1116 return false;
1117
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001119 unsigned Complexity = 0;
1120 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001122 Complexity = 1;
1123 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001124 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001125 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1126 Complexity = 4;
1127
Gabor Greifba36cb52008-08-28 21:40:38 +00001128 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001129 Complexity++;
1130 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001131 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001132
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001133 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1134 // a simple shift.
1135 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001136 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001137
1138 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1139 // to a LEA. This is determined with some expermentation but is by no means
1140 // optimal (especially for code size consideration). LEA is nice because of
1141 // its three-address nature. Tweak the cost function again when we can run
1142 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001143 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1144 // For X86-64, we should always use lea to materialize RIP relative
1145 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001146 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001147 Complexity = 4;
1148 else
1149 Complexity += 2;
1150 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001151
Gabor Greifba36cb52008-08-28 21:40:38 +00001152 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001153 Complexity++;
1154
1155 if (Complexity > 2) {
1156 getAddressOperands(AM, Base, Scale, Index, Disp);
1157 return true;
1158 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001159 return false;
1160}
1161
Dan Gohman475871a2008-07-27 21:46:04 +00001162bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1163 SDValue &Base, SDValue &Scale,
1164 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001165 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001166 N.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001167 CanBeFoldedBy(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001168 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001169 return false;
1170}
1171
Dan Gohman8b746962008-09-23 18:22:58 +00001172/// getGlobalBaseReg - Return an SDNode that returns the value of
1173/// the global base register. Output instructions required to
1174/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001175///
Evan Cheng9ade2182006-08-26 05:34:46 +00001176SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001177 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Dan Gohman8b746962008-09-23 18:22:58 +00001178 if (!GlobalBaseReg)
1179 GlobalBaseReg = TM.getInstrInfo()->initializeGlobalBaseReg(BB->getParent());
Gabor Greifba36cb52008-08-28 21:40:38 +00001180 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001181}
1182
Evan Chengb245d922006-05-20 01:36:52 +00001183static SDNode *FindCallStartFromCall(SDNode *Node) {
1184 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1185 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1186 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001187 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001188}
1189
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001190/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1191/// truncate of the specified operand to i8. This can be done with tablegen,
1192/// except that this code uses MVT::Flag in a tricky way that happens to
1193/// improve scheduling in some cases.
1194SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1195 assert(!Subtarget->is64Bit() &&
1196 "getTruncateTo8Bit is only needed on x86-32!");
1197 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1198
1199 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1200 unsigned Opc;
1201 MVT N0VT = N0.getValueType();
1202 switch (N0VT.getSimpleVT()) {
1203 default: assert(0 && "Unknown truncate!");
1204 case MVT::i16:
1205 Opc = X86::MOV16to16_;
1206 break;
1207 case MVT::i32:
1208 Opc = X86::MOV32to32_;
1209 break;
1210 }
1211
1212 // The use of MVT::Flag here is not strictly accurate, but it helps
1213 // scheduling in some cases.
1214 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1215 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1216 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001217}
1218
1219
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001221 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001222 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001223 unsigned Opc, MOpc;
1224 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001225
Evan Chengf597dc72006-02-10 22:24:32 +00001226#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001227 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001228 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001229 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001230 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001231#endif
1232
Dan Gohmane8be6c62008-07-17 19:10:17 +00001233 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001234#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001235 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001236 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001237 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001238 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001239#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001240 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001241 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001242
Evan Cheng0114e942006-01-06 20:36:21 +00001243 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001244 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001245 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001246 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001247
Evan Cheng51a9ed92006-02-25 10:09:08 +00001248 case ISD::ADD: {
1249 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1250 // code and is matched first so to prevent it from being turned into
1251 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001252 // In 64-bit small code size mode, use LEA to take advantage of
1253 // RIP-relative addressing.
1254 if (TM.getCodeModel() != CodeModel::Small)
1255 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001256 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SDValue N0 = N.getOperand(0);
1258 SDValue N1 = N.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 if (N.getNode()->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001260 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001261 N1.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001262 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001263 SDValue C(0, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001264 // TODO: handle ExternalSymbolSDNode.
Evan Cheng51a9ed92006-02-25 10:09:08 +00001265 if (GlobalAddressSDNode *G =
1266 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001267 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001268 G->getOffset() + Offset);
1269 } else if (ConstantPoolSDNode *CP =
1270 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001271 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001272 CP->getAlignment(),
1273 CP->getOffset()+Offset);
1274 }
1275
Gabor Greifba36cb52008-08-28 21:40:38 +00001276 if (C.getNode()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001277 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001279 CurDAG->getRegister(0, PtrVT), C };
Gabor Greif93c53e52008-08-31 15:37:04 +00001280 return CurDAG->SelectNodeTo(N.getNode(), X86::LEA64r,
1281 MVT::i64, Ops, 4);
Evan Cheng25ab6902006-09-08 06:48:29 +00001282 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00001283 return CurDAG->SelectNodeTo(N.getNode(), X86::MOV32ri, PtrVT, C);
Evan Cheng25ab6902006-09-08 06:48:29 +00001284 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001285 }
1286
1287 // Other cases are handled by auto-generated code.
1288 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001289 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001290
Dan Gohman525178c2007-10-08 18:33:35 +00001291 case ISD::SMUL_LOHI:
1292 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue N0 = Node->getOperand(0);
1294 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001295
Dan Gohman525178c2007-10-08 18:33:35 +00001296 bool isSigned = Opcode == ISD::SMUL_LOHI;
1297 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001298 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001299 default: assert(0 && "Unsupported VT!");
1300 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1301 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1302 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001303 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001304 }
1305 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001307 default: assert(0 && "Unsupported VT!");
1308 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1309 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1310 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001311 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001312 }
1313
1314 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001315 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001316 default: assert(0 && "Unsupported VT!");
1317 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1318 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1319 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001320 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001321 }
1322
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001324 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001325 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001326 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001327 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001328 if (foldedLoad)
1329 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001330 }
1331
Evan Cheng04699902006-08-26 01:05:16 +00001332 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1334 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001335
1336 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001337 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001338 AddToISelQueue(Tmp0);
1339 AddToISelQueue(Tmp1);
1340 AddToISelQueue(Tmp2);
1341 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001342 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001343 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001344 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001345 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001346 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001347 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001348 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001349 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001350 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001352 }
1353
Dan Gohman525178c2007-10-08 18:33:35 +00001354 // Copy the low half of the result, if it is needed.
1355 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001357 LoReg, NVT, InFlag);
1358 InFlag = Result.getValue(2);
1359 ReplaceUses(N.getValue(0), Result);
1360#ifndef NDEBUG
1361 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001362 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001363 DOUT << "\n";
1364#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001365 }
Dan Gohman525178c2007-10-08 18:33:35 +00001366 // Copy the high half of the result, if it is needed.
1367 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001369 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1370 // Prevent use of AH in a REX instruction by referencing AX instead.
1371 // Shift it down 8 bits.
1372 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1373 X86::AX, MVT::i16, InFlag);
1374 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001375 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001376 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001377 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001378 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1379 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001380 MVT::i8, Result, SRIdx), 0);
1381 } else {
1382 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1383 HiReg, NVT, InFlag);
1384 InFlag = Result.getValue(2);
1385 }
1386 ReplaceUses(N.getValue(1), Result);
1387#ifndef NDEBUG
1388 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001390 DOUT << "\n";
1391#endif
1392 }
Evan Cheng34167212006-02-09 00:37:58 +00001393
Evan Chengf597dc72006-02-10 22:24:32 +00001394#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001395 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001396#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001397
Evan Cheng64a752f2006-08-11 09:08:15 +00001398 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001399 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001400
Dan Gohman525178c2007-10-08 18:33:35 +00001401 case ISD::SDIVREM:
1402 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue N0 = Node->getOperand(0);
1404 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001405
1406 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001407 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001408 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001409 default: assert(0 && "Unsupported VT!");
1410 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1411 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1412 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001413 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001414 }
1415 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001416 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001417 default: assert(0 && "Unsupported VT!");
1418 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1419 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1420 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001421 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001422 }
1423
1424 unsigned LoReg, HiReg;
1425 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001426 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001427 default: assert(0 && "Unsupported VT!");
1428 case MVT::i8:
1429 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001430 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001431 SExtOpcode = X86::CBW;
1432 break;
1433 case MVT::i16:
1434 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001435 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001436 SExtOpcode = X86::CWD;
1437 break;
1438 case MVT::i32:
1439 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001440 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001441 SExtOpcode = X86::CDQ;
1442 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001443 case MVT::i64:
1444 LoReg = X86::RAX; HiReg = X86::RDX;
1445 ClrOpcode = X86::MOV64r0;
1446 SExtOpcode = X86::CQO;
1447 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001448 }
1449
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001451 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1452
Dan Gohman475871a2008-07-27 21:46:04 +00001453 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001454 if (NVT == MVT::i8 && !isSigned) {
1455 // Special case for div8, just use a move with zero extension to AX to
1456 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001458 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001460 AddToISelQueue(N0.getOperand(0));
1461 AddToISelQueue(Tmp0);
1462 AddToISelQueue(Tmp1);
1463 AddToISelQueue(Tmp2);
1464 AddToISelQueue(Tmp3);
1465 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001467 Ops, 5), 0);
1468 Chain = Move.getValue(1);
1469 ReplaceUses(N0.getValue(1), Chain);
1470 } else {
1471 AddToISelQueue(N0);
1472 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001473 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001474 Chain = CurDAG->getEntryNode();
1475 }
Dan Gohman475871a2008-07-27 21:46:04 +00001476 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001477 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001478 } else {
1479 AddToISelQueue(N0);
1480 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001481 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001482 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001483 if (isSigned) {
1484 // Sign extend the low part into the high part.
1485 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001486 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001487 } else {
1488 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001489 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001490 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1491 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001492 }
Evan Cheng948f3432006-01-06 23:19:29 +00001493 }
1494
1495 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001496 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001497 AddToISelQueue(Tmp0);
1498 AddToISelQueue(Tmp1);
1499 AddToISelQueue(Tmp2);
1500 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001501 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001502 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001503 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001504 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001505 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001506 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001507 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001508 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001509 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001510 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001511 }
1512
Dan Gohmana37c9f72007-09-25 18:23:27 +00001513 // Copy the division (low) result, if it is needed.
1514 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001515 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001516 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001517 InFlag = Result.getValue(2);
1518 ReplaceUses(N.getValue(0), Result);
1519#ifndef NDEBUG
1520 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001521 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001522 DOUT << "\n";
1523#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001524 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001525 // Copy the remainder (high) result, if it is needed.
1526 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001528 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1529 // Prevent use of AH in a REX instruction by referencing AX instead.
1530 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001531 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1532 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001533 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001534 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001535 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001536 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001537 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1538 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001539 MVT::i8, Result, SRIdx), 0);
1540 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001541 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1542 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001543 InFlag = Result.getValue(2);
1544 }
1545 ReplaceUses(N.getValue(1), Result);
1546#ifndef NDEBUG
1547 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001548 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001549 DOUT << "\n";
1550#endif
1551 }
Evan Chengf597dc72006-02-10 22:24:32 +00001552
1553#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001554 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001555#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001556
1557 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001558 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001559
Christopher Lambc59e5212007-08-10 21:48:46 +00001560 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001561 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001562 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1563 SDValue N0 = Node->getOperand(0);
1564 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001565
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001566 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1567 unsigned Opc = 0;
1568 switch (NVT.getSimpleVT()) {
1569 default: assert(0 && "Unknown sign_extend_inreg!");
1570 case MVT::i16:
1571 Opc = X86::MOVSX16rr8;
1572 break;
1573 case MVT::i32:
1574 Opc = X86::MOVSX32rr8;
1575 break;
1576 }
1577
1578 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001579
1580#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001581 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001582 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001583 DOUT << "\n";
1584 DOUT << std::string(Indent-2, ' ') << "=> ";
1585 DEBUG(ResNode->dump(CurDAG));
1586 DOUT << "\n";
1587 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001588#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001589 return ResNode;
1590 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001591 break;
1592 }
1593
1594 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001595 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1596 SDValue Input = Node->getOperand(0);
1597 AddToISelQueue(Node->getOperand(0));
1598 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001599
Evan Cheng403be7e2006-05-08 08:01:26 +00001600#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001601 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001602 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001603 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001604 Indent -= 2;
1605#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001606 return ResNode;
1607 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001608 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001609 }
Evan Cheng851bc042008-06-17 02:01:22 +00001610
1611 case ISD::DECLARE: {
1612 // Handle DECLARE nodes here because the second operand may have been
1613 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001614 SDValue Chain = Node->getOperand(0);
1615 SDValue N1 = Node->getOperand(1);
1616 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001617 if (!isa<FrameIndexSDNode>(N1))
1618 break;
1619 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1620 if (N2.getOpcode() == ISD::ADD &&
1621 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1622 N2 = N2.getOperand(1);
1623 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001624 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001625 GlobalValue *GV =
1626 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001627 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1628 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001629 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001631 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1632 MVT::Other, Ops, 3);
1633 }
1634 break;
1635 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001636 }
1637
Evan Cheng9ade2182006-08-26 05:34:46 +00001638 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001639
Evan Chengf597dc72006-02-10 22:24:32 +00001640#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001641 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001642 if (ResNode == NULL || ResNode == N.getNode())
1643 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001644 else
1645 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001646 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001647 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001648#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001649
1650 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001651}
1652
Chris Lattnerc0bad572006-06-08 18:03:49 +00001653bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001654SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001655 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001656 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001657 switch (ConstraintCode) {
1658 case 'o': // offsetable ??
1659 case 'v': // not offsetable ??
1660 default: return true;
1661 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001662 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001663 return true;
1664 break;
1665 }
1666
Evan Cheng04699902006-08-26 01:05:16 +00001667 OutOps.push_back(Op0);
1668 OutOps.push_back(Op1);
1669 OutOps.push_back(Op2);
1670 OutOps.push_back(Op3);
1671 AddToISelQueue(Op0);
1672 AddToISelQueue(Op1);
1673 AddToISelQueue(Op2);
1674 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001675 return false;
1676}
1677
Chris Lattnerc961eea2005-11-16 01:54:32 +00001678/// createX86ISelDag - This pass converts a legalized DAG into a
1679/// X86-specific DAG, ready for instruction scheduling.
1680///
Evan Chenge50794a2006-08-29 18:28:33 +00001681FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1682 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001683}