blob: b76d3e23cf62f1335196f577e5f4e8ddadaceebd [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";
83 cerr << "Base.Reg "; if (Base.Reg.Val!=0) Base.Reg.Val->dump();
84 else cerr << "nul";
85 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
86 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
87 cerr << "IndexReg "; if (IndexReg.Val!=0) IndexReg.Val->dump();
88 else cerr << "nul";
89 cerr << " Disp " << Disp << "\n";
90 cerr << "GV "; if (GV) GV->dump();
91 else cerr << "nul";
92 cerr << " CP "; if (CP) CP->dump();
93 else cerr << "nul";
94 cerr << "\n";
95 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
96 cerr << " JT" << JT << " Align" << Align << "\n";
97 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000098 };
99}
100
101namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000102 //===--------------------------------------------------------------------===//
103 /// ISel - X86 specific code to select X86 machine instructions for
104 /// SelectionDAG operations.
105 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000106 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000107 /// ContainsFPCode - Every instruction we select that uses or defines a FP
108 /// register should set this to true.
109 bool ContainsFPCode;
110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 /// TM - Keep a reference to X86TargetMachine.
112 ///
113 X86TargetMachine &TM;
114
Chris Lattnerc961eea2005-11-16 01:54:32 +0000115 /// X86Lowering - This object fully describes how to lower LLVM code to an
116 /// X86-specific SelectionDAG.
117 X86TargetLowering X86Lowering;
118
119 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
120 /// make the right decision when generating code for different targets.
121 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000122
Evan Cheng25ab6902006-09-08 06:48:29 +0000123 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
124 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000125 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000126
Evan Chengdb8d56b2008-06-30 20:45:06 +0000127 /// CurBB - Current BB being isel'd.
128 ///
129 MachineBasicBlock *CurBB;
130
Chris Lattnerc961eea2005-11-16 01:54:32 +0000131 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000132 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng4576f6d2008-07-01 18:05:03 +0000133 : SelectionDAGISel(X86Lowering, fast),
134 ContainsFPCode(false), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000135 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000136 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137
Evan Cheng7ccced62006-02-18 00:15:05 +0000138 virtual bool runOnFunction(Function &Fn) {
139 // Make sure we re-emit a set of the global base reg if necessary
140 GlobalBaseReg = 0;
141 return SelectionDAGISel::runOnFunction(Fn);
142 }
143
Chris Lattnerc961eea2005-11-16 01:54:32 +0000144 virtual const char *getPassName() const {
145 return "X86 DAG->DAG Instruction Selection";
146 }
147
Evan Chengdb8d56b2008-06-30 20:45:06 +0000148 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000149 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000150 virtual void InstructionSelect(SelectionDAG &DAG);
151
152 /// InstructionSelectPostProcessing - Post processing of selected and
153 /// scheduled basic blocks.
Dan Gohman462dc7f2008-07-21 20:00:07 +0000154 virtual void InstructionSelectPostProcessing();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000155
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000156 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
157
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000158 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000159
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160// Include the pieces autogenerated from the target description.
161#include "X86GenDAGISel.inc"
162
163 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDNode *Select(SDValue N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000165
Dan Gohman475871a2008-07-27 21:46:04 +0000166 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000167 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000168 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000169 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000170 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
171 SDValue &Scale, SDValue &Index, SDValue &Disp);
172 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
173 SDValue &Scale, SDValue &Index, SDValue &Disp);
174 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
175 SDValue N, SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp,
177 SDValue &InChain, SDValue &OutChain);
178 bool TryFoldLoad(SDValue P, SDValue N,
179 SDValue &Base, SDValue &Scale,
180 SDValue &Index, SDValue &Disp);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000181 void PreprocessForRMW(SelectionDAG &DAG);
182 void PreprocessForFPConvert(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000183
Chris Lattnerc0bad572006-06-08 18:03:49 +0000184 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
185 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000186 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000187 char ConstraintCode,
Dan Gohman475871a2008-07-27 21:46:04 +0000188 std::vector<SDValue> &OutOps,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000189 SelectionDAG &DAG);
190
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000191 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
192
Dan Gohman475871a2008-07-27 21:46:04 +0000193 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
194 SDValue &Scale, SDValue &Index,
195 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000196 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000197 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
198 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000199 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000200 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 // These are 32-bit even in 64-bit mode since RIP relative offset
202 // is 32-bit.
203 if (AM.GV)
204 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
205 else if (AM.CP)
206 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
207 else if (AM.ES)
208 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
209 else if (AM.JT != -1)
210 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
211 else
212 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000213 }
214
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000215 /// getI8Imm - Return a target constant with the specified value, of type
216 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000217 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i8);
219 }
220
Chris Lattnerc961eea2005-11-16 01:54:32 +0000221 /// getI16Imm - Return a target constant with the specified value, of type
222 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000223 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000224 return CurDAG->getTargetConstant(Imm, MVT::i16);
225 }
226
227 /// getI32Imm - Return a target constant with the specified value, of type
228 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000229 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000230 return CurDAG->getTargetConstant(Imm, MVT::i32);
231 }
Evan Chengf597dc72006-02-10 22:24:32 +0000232
Evan Cheng7ccced62006-02-18 00:15:05 +0000233 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
234 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000235 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000236
Christopher Lambc59e5212007-08-10 21:48:46 +0000237 /// getTruncate - return an SDNode that implements a subreg based truncate
238 /// of the specified operand to the the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +0000239 SDNode *getTruncate(SDValue N0, MVT VT);
Christopher Lambc59e5212007-08-10 21:48:46 +0000240
Evan Cheng23addc02006-02-10 22:46:26 +0000241#ifndef NDEBUG
242 unsigned Indent;
243#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000244 };
245}
246
Evan Chengcdda25d2008-04-25 08:22:20 +0000247/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
248///
Evan Chenga275ecb2006-10-10 01:46:56 +0000249static SDNode *findFlagUse(SDNode *N) {
250 unsigned FlagResNo = N->getNumValues()-1;
251 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000252 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000253 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000254 SDValue Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000255 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000256 return User;
257 }
258 }
259 return NULL;
260}
261
Evan Chengcdda25d2008-04-25 08:22:20 +0000262/// findNonImmUse - Return true by reference in "found" if "Use" is an
263/// non-immediate use of "Def". This function recursively traversing
264/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000265static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
266 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000267 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000268 if (found ||
269 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000270 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000271 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000272
Evan Cheng27e1fe92006-10-14 08:33:25 +0000273 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000274 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000275 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000276 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000277 if (N == Def) {
278 if (Use == ImmedUse)
Evan Cheng419ace92008-04-25 08:55:28 +0000279 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000280 if (Use == Root) {
Evan Cheng419ace92008-04-25 08:55:28 +0000281 // Must be a chain reading node where it is possible to reach its own
282 // chain operand through a path started from another operand.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000283 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000284 Use->getOpcode() == X86ISD::CMP ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000285 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
286 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000287 continue;
288 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000289 found = true;
290 break;
291 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000292
293 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000294 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000295 }
296}
297
Evan Cheng27e1fe92006-10-14 08:33:25 +0000298/// isNonImmUse - Start searching from Root up the DAG to check is Def can
299/// be reached. Return true if that's the case. However, ignore direct uses
300/// by ImmedUse (which would be U in the example illustrated in
301/// CanBeFoldedBy) and by Root (which can happen in the store case).
302/// FIXME: to be really generic, we should allow direct use by any node
303/// that is being folded. But realisticly since we only fold loads which
304/// have one non-chain use, we only need to watch out for load/op/store
305/// and load/op/cmp case where the root (store / cmp) may reach the load via
306/// its chain operand.
307static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
308 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000309 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000310 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000311 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000312 return found;
313}
314
315
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000316bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000317 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000318
Evan Chenga8df1b42006-07-27 16:44:36 +0000319 // If U use can somehow reach N through another path then U can't fold N or
320 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000321 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000322 // a successor of U.
323 //
324 // [ N ]
325 // ^ ^
326 // | |
327 // / \---
328 // / [X]
329 // | ^
330 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000331
332 if (isNonImmUse(Root, N, U))
333 return false;
334
335 // If U produces a flag, then it gets (even more) interesting. Since it
336 // would have been "glued" together with its flag use, we need to check if
337 // it might reach N:
338 //
339 // [ N ]
340 // ^ ^
341 // | |
342 // [U] \--
343 // ^ [TF]
344 // | ^
345 // | |
346 // \ /
347 // [FU]
348 //
349 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
350 // NU), then TF is a predecessor of FU and a successor of NU. But since
351 // NU and FU are flagged together, this effectively creates a cycle.
352 bool HasFlagUse = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000353 MVT VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000354 while ((VT == MVT::Flag && !Root->use_empty())) {
355 SDNode *FU = findFlagUse(Root);
356 if (FU == NULL)
357 break;
358 else {
359 Root = FU;
360 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000361 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000362 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000363 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000364
365 if (HasFlagUse)
366 return !isNonImmUse(Root, N, Root, U);
367 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000368}
369
Evan Cheng70e674e2006-08-28 20:10:17 +0000370/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
371/// and move load below the TokenFactor. Replace store's chain operand with
372/// load's chain result.
Dan Gohman475871a2008-07-27 21:46:04 +0000373static void MoveBelowTokenFactor(SelectionDAG &DAG, SDValue Load,
374 SDValue Store, SDValue TF) {
375 std::vector<SDValue> Ops;
Evan Cheng70e674e2006-08-28 20:10:17 +0000376 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
377 if (Load.Val == TF.Val->getOperand(i).Val)
378 Ops.push_back(Load.Val->getOperand(0));
379 else
380 Ops.push_back(TF.Val->getOperand(i));
381 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
382 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
383 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
384 Store.getOperand(2), Store.getOperand(3));
385}
386
Evan Chengcd0baf22008-05-23 21:23:16 +0000387/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
388///
Dan Gohman475871a2008-07-27 21:46:04 +0000389static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
390 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000391 if (N.getOpcode() == ISD::BIT_CONVERT)
392 N = N.getOperand(0);
393
394 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
395 if (!LD || LD->isVolatile())
396 return false;
397 if (LD->getAddressingMode() != ISD::UNINDEXED)
398 return false;
399
400 ISD::LoadExtType ExtType = LD->getExtensionType();
401 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
402 return false;
403
404 if (N.hasOneUse() &&
405 N.getOperand(1) == Address &&
406 N.Val->isOperandOf(Chain.Val)) {
407 Load = N;
408 return true;
409 }
410 return false;
411}
412
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000413/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
414/// This is only run if not in -fast mode (aka -O0).
415/// This allows the instruction selector to pick more read-modify-write
416/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000417///
418/// [Load chain]
419/// ^
420/// |
421/// [Load]
422/// ^ ^
423/// | |
424/// / \-
425/// / |
426/// [TokenFactor] [Op]
427/// ^ ^
428/// | |
429/// \ /
430/// \ /
431/// [Store]
432///
433/// The fact the store's chain operand != load's chain will prevent the
434/// (store (op (load))) instruction from being selected. We can transform it to:
435///
436/// [Load chain]
437/// ^
438/// |
439/// [TokenFactor]
440/// ^
441/// |
442/// [Load]
443/// ^ ^
444/// | |
445/// | \-
446/// | |
447/// | [Op]
448/// | ^
449/// | |
450/// \ /
451/// \ /
452/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000453void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000454 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
455 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000456 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000457 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000458 SDValue Chain = I->getOperand(0);
Evan Cheng70e674e2006-08-28 20:10:17 +0000459 if (Chain.Val->getOpcode() != ISD::TokenFactor)
460 continue;
461
Dan Gohman475871a2008-07-27 21:46:04 +0000462 SDValue N1 = I->getOperand(1);
463 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000464 if ((N1.getValueType().isFloatingPoint() &&
465 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000466 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000467 continue;
468
469 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000470 SDValue Load;
Evan Cheng70e674e2006-08-28 20:10:17 +0000471 unsigned Opcode = N1.Val->getOpcode();
472 switch (Opcode) {
473 case ISD::ADD:
474 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000475 case ISD::AND:
476 case ISD::OR:
477 case ISD::XOR:
478 case ISD::ADDC:
Evan Chengcd0baf22008-05-23 21:23:16 +0000479 case ISD::ADDE:
480 case ISD::VECTOR_SHUFFLE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000481 SDValue N10 = N1.getOperand(0);
482 SDValue N11 = N1.getOperand(1);
Evan Chengcd0baf22008-05-23 21:23:16 +0000483 RModW = isRMWLoad(N10, Chain, N2, Load);
484 if (!RModW)
485 RModW = isRMWLoad(N11, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000486 break;
487 }
488 case ISD::SUB:
489 case ISD::SHL:
490 case ISD::SRA:
491 case ISD::SRL:
492 case ISD::ROTL:
493 case ISD::ROTR:
494 case ISD::SUBC:
495 case ISD::SUBE:
496 case X86ISD::SHLD:
497 case X86ISD::SHRD: {
Dan Gohman475871a2008-07-27 21:46:04 +0000498 SDValue N10 = N1.getOperand(0);
Evan Chengcd0baf22008-05-23 21:23:16 +0000499 RModW = isRMWLoad(N10, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000500 break;
501 }
502 }
503
Evan Cheng82a35b32006-08-29 06:44:17 +0000504 if (RModW) {
Dan Gohman475871a2008-07-27 21:46:04 +0000505 MoveBelowTokenFactor(DAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000506 ++NumLoadMoved;
507 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000508 }
509}
510
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000511
512/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
513/// nodes that target the FP stack to be store and load to the stack. This is a
514/// gross hack. We would like to simply mark these as being illegal, but when
515/// we do that, legalize produces these when it expands calls, then expands
516/// these in the same legalize pass. We would like dag combine to be able to
517/// hack on these between the call expansion and the node legalization. As such
518/// this pass basically does "really late" legalization of these inline with the
519/// X86 isel pass.
520void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
521 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
522 E = DAG.allnodes_end(); I != E; ) {
523 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
524 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
525 continue;
526
527 // If the source and destination are SSE registers, then this is a legal
528 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000529 MVT SrcVT = N->getOperand(0).getValueType();
530 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000531 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
532 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
533 if (SrcIsSSE && DstIsSSE)
534 continue;
535
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000536 if (!SrcIsSSE && !DstIsSSE) {
537 // If this is an FPStack extension, it is a noop.
538 if (N->getOpcode() == ISD::FP_EXTEND)
539 continue;
540 // If this is a value-preserving FPStack truncation, it is a noop.
541 if (N->getConstantOperandVal(1))
542 continue;
543 }
544
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000545 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
546 // FPStack has extload and truncstore. SSE can fold direct loads into other
547 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000548 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000549 if (N->getOpcode() == ISD::FP_ROUND)
550 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
551 else
552 MemVT = SrcIsSSE ? SrcVT : DstVT;
553
Dan Gohman475871a2008-07-27 21:46:04 +0000554 SDValue MemTmp = DAG.CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000555
556 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman475871a2008-07-27 21:46:04 +0000557 SDValue Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000558 MemTmp, NULL, 0, MemVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000560 NULL, 0, MemVT);
561
562 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
563 // extload we created. This will cause general havok on the dag because
564 // anything below the conversion could be folded into other existing nodes.
565 // To avoid invalidating 'I', back it up to the convert node.
566 --I;
Dan Gohman475871a2008-07-27 21:46:04 +0000567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000568
569 // Now that we did that, the node is dead. Increment the iterator to the
570 // next node to process, then delete N.
571 ++I;
572 DAG.DeleteNode(N);
573 }
574}
575
Chris Lattnerc961eea2005-11-16 01:54:32 +0000576/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
577/// when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000578void X86DAGToDAGISel::InstructionSelect(SelectionDAG &DAG) {
579 CurBB = BB; // BB can change as result of isel.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000580
Evan Chengdb8d56b2008-06-30 20:45:06 +0000581 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000582 if (!Fast)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000583 PreprocessForRMW(DAG);
584
585 // FIXME: This should only happen when not -fast.
586 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000587
Chris Lattnerc961eea2005-11-16 01:54:32 +0000588 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000589#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000590 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000591 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000592#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000593 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000594#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000595 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000596#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000597
Chris Lattnerc961eea2005-11-16 01:54:32 +0000598 DAG.RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000599}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000600
Dan Gohman462dc7f2008-07-21 20:00:07 +0000601void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000602 // If we are emitting FP stack code, scan the basic block to determine if this
603 // block defines any FP values. If so, put an FP_REG_KILL instruction before
604 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000605
Dale Johannesen48d1e452007-09-24 22:52:39 +0000606 // Note that FP stack instructions are used in all modes for long double,
607 // so we always need to do this check.
608 // Also note that it's possible for an FP stack register to be live across
609 // an instruction that produces multiple basic blocks (SSE CMOV) so we
610 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000611
612 // Scan all of the machine instructions in these MBBs, checking for FP
613 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000614 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000615 MachineFunction::iterator EndMBB = BB; ++EndMBB;
616 for (; MBBI != EndMBB; ++MBBI) {
617 MachineBasicBlock *MBB = MBBI;
618
619 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
620 // before the return.
621 if (!MBB->empty()) {
622 MachineBasicBlock::iterator EndI = MBB->end();
623 --EndI;
624 if (EndI->getDesc().isReturn())
625 continue;
626 }
627
Dale Johannesen48d1e452007-09-24 22:52:39 +0000628 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000629 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000630 !ContainsFPCode && I != E; ++I) {
631 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
632 const TargetRegisterClass *clas;
633 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
634 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000635 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000636 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000637 X86::RFP32RegisterClass ||
638 clas == X86::RFP64RegisterClass ||
639 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000640 ContainsFPCode = true;
641 break;
642 }
643 }
644 }
645 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000646 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
647 // a copy of the input value in this block. In SSE mode, we only care about
648 // 80-bit values.
649 if (!ContainsFPCode) {
650 // Final check, check LLVM BB's that are successors to the LLVM BB
651 // corresponding to BB for FP PHI nodes.
652 const BasicBlock *LLVMBB = BB->getBasicBlock();
653 const PHINode *PN;
654 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
655 !ContainsFPCode && SI != E; ++SI) {
656 for (BasicBlock::const_iterator II = SI->begin();
657 (PN = dyn_cast<PHINode>(II)); ++II) {
658 if (PN->getType()==Type::X86_FP80Ty ||
659 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
660 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
661 ContainsFPCode = true;
662 break;
663 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000664 }
665 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000666 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000667 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
668 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000669 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000670 TM.getInstrInfo()->get(X86::FP_REG_KILL));
671 ++NumFPKill;
672 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000673 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000674}
675
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000676/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
677/// the main function.
678void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
679 MachineFrameInfo *MFI) {
680 const TargetInstrInfo *TII = TM.getInstrInfo();
681 if (Subtarget->isTargetCygMing())
682 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
683}
684
685void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
686 // If this is main, emit special code for main.
687 MachineBasicBlock *BB = MF.begin();
688 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
689 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
690}
691
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000692/// MatchAddress - Add the specified node to the specified addressing mode,
693/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000694/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000695bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000696 bool isRoot, unsigned Depth) {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000697DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000698 // Limit recursion.
699 if (Depth > 5)
700 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000701
Evan Cheng25ab6902006-09-08 06:48:29 +0000702 // RIP relative addressing: %rip + 32-bit displacement!
703 if (AM.isRIPRel) {
704 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000705 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000706 if (isInt32(AM.Disp + Val)) {
707 AM.Disp += Val;
708 return false;
709 }
710 }
711 return true;
712 }
713
Evan Cheng2ef88a02006-08-07 22:28:20 +0000714 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000715 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000716
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000717 switch (N.getOpcode()) {
718 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000719 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000720 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000721 if (isInt32(AM.Disp + Val)) {
722 AM.Disp += Val;
723 return false;
724 }
725 break;
726 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000727
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000728 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000729DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
730DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
731DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000732 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000733 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000734 // Also, base and index reg must be 0 in order to use rip as base.
735 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
736 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000737 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000738 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
739 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000740 // If value is available in a register both base and index components have
741 // been picked, we can't fit the result available in the register in the
742 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000743 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000744 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000745 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
746 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000747 AM.GV = GV;
748 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000749 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
750 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000751 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000752 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000753 AM.CP = CP->getConstVal();
754 AM.Align = CP->getAlignment();
755 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000756 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
757 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000758 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000759 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000760 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000761 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
762 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000763 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000764 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000765 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000766 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
767 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000768 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000769 }
770 }
771 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000772 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000773
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000774 case ISD::FrameIndex:
775 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
776 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
777 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
778 return false;
779 }
780 break;
Evan Chengec693f72005-12-08 02:01:35 +0000781
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000782 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000783 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000784 break;
785
786 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
787 unsigned Val = CN->getValue();
788 if (Val == 1 || Val == 2 || Val == 3) {
789 AM.Scale = 1 << Val;
Dan Gohman475871a2008-07-27 21:46:04 +0000790 SDValue ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000791
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000792 // Okay, we know that we have a scale by now. However, if the scaled
793 // value is an add of something and a constant, we can fold the
794 // constant into the disp field here.
795 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
796 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
797 AM.IndexReg = ShVal.Val->getOperand(0);
798 ConstantSDNode *AddVal =
799 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
800 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
801 if (isInt32(Disp))
802 AM.Disp = Disp;
803 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000804 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000805 } else {
806 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000807 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000808 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000809 }
810 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000811 }
Evan Chengec693f72005-12-08 02:01:35 +0000812
Dan Gohman83688052007-10-22 20:22:24 +0000813 case ISD::SMUL_LOHI:
814 case ISD::UMUL_LOHI:
815 // A mul_lohi where we need the low part can be folded as a plain multiply.
816 if (N.ResNo != 0) break;
817 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000818 case ISD::MUL:
819 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000820 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000821 AM.BaseType == X86ISelAddressMode::RegBase &&
822 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000823 AM.IndexReg.Val == 0 &&
824 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000825 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
826 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
827 AM.Scale = unsigned(CN->getValue())-1;
828
Dan Gohman475871a2008-07-27 21:46:04 +0000829 SDValue MulVal = N.Val->getOperand(0);
830 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000831
832 // Okay, we know that we have a scale by now. However, if the scaled
833 // value is an add of something and a constant, we can fold the
834 // constant into the disp field here.
835 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
836 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
837 Reg = MulVal.Val->getOperand(0);
838 ConstantSDNode *AddVal =
839 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000840 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
841 if (isInt32(Disp))
842 AM.Disp = Disp;
843 else
844 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845 } else {
846 Reg = N.Val->getOperand(0);
847 }
848
849 AM.IndexReg = AM.Base.Reg = Reg;
850 return false;
851 }
Chris Lattner62412262007-02-04 20:18:17 +0000852 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000853 break;
854
Chris Lattner62412262007-02-04 20:18:17 +0000855 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000856 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000857 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000858 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
859 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000860 return false;
861 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000862 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
863 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000864 return false;
865 AM = Backup;
866 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000867 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000868
Chris Lattner62412262007-02-04 20:18:17 +0000869 case ISD::OR:
870 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000871 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000872
873 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
874 X86ISelAddressMode Backup = AM;
875 // Start with the LHS as an addr mode.
876 if (!MatchAddress(N.getOperand(0), AM, false) &&
877 // Address could not have picked a GV address for the displacement.
878 AM.GV == NULL &&
879 // On x86-64, the resultant disp must fit in 32-bits.
880 isInt32(AM.Disp + CN->getSignExtended()) &&
881 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000882 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000883 AM.Disp += CN->getValue();
884 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000885 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000886 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000887 }
888 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000889
890 case ISD::AND: {
891 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
892 // allows us to fold the shift into this addressing mode.
893 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000894 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000895 if (Shift.getOpcode() != ISD::SHL) break;
896
897 // Scale must not be used already.
898 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000899
900 // Not when RIP is used as the base.
901 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000902
903 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
904 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
905 if (!C1 || !C2) break;
906
907 // Not likely to be profitable if either the AND or SHIFT node has more
908 // than one use (unless all uses are for address computation). Besides,
909 // isel mechanism requires their node ids to be reused.
910 if (!N.hasOneUse() || !Shift.hasOneUse())
911 break;
912
913 // Verify that the shift amount is something we can fold.
914 unsigned ShiftCst = C1->getValue();
915 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
916 break;
917
918 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
920 SDValue(C2, 0), SDValue(C1, 0));
921 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +0000922 Shift.getOperand(0), NewANDMask);
923 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
924 NewAND.Val->setNodeId(N.Val->getNodeId());
925
926 AM.Scale = 1 << ShiftCst;
927 AM.IndexReg = NewAND;
928 return false;
929 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000930 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000931
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000932 return MatchAddressBase(N, AM, isRoot, Depth);
933}
934
935/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
936/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +0000937bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000938 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000939 // Is the base register already occupied?
940 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
941 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000942 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000943 AM.IndexReg = N;
944 AM.Scale = 1;
945 return false;
946 }
947
948 // Otherwise, we cannot select it.
949 return true;
950 }
951
952 // Default, generate it as a register.
953 AM.BaseType = X86ISelAddressMode::RegBase;
954 AM.Base.Reg = N;
955 return false;
956}
957
Evan Chengec693f72005-12-08 02:01:35 +0000958/// SelectAddr - returns true if it is able pattern match an addressing mode.
959/// It returns the operands which make up the maximal addressing mode it can
960/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +0000961bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
962 SDValue &Scale, SDValue &Index,
963 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000964 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000965 if (MatchAddress(N, AM))
966 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000967
Duncan Sands83ec4b62008-06-06 12:08:01 +0000968 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000969 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000970 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000971 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000972 }
Evan Cheng8700e142006-01-11 06:09:51 +0000973
Evan Cheng7dd281b2006-02-05 05:25:07 +0000974 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000975 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000976
977 getAddressOperands(AM, Base, Scale, Index, Disp);
978 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000979}
980
Chris Lattner4fe4f252006-10-11 22:09:58 +0000981/// isZeroNode - Returns true if Elt is a constant zero or a floating point
982/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000983static inline bool isZeroNode(SDValue Elt) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000984 return ((isa<ConstantSDNode>(Elt) &&
985 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
986 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000987 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000988}
989
990
Chris Lattner3a7cd952006-10-07 21:55:32 +0000991/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
992/// match a load whose top elements are either undef or zeros. The load flavor
993/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000994bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
995 SDValue N, SDValue &Base,
996 SDValue &Scale, SDValue &Index,
997 SDValue &Disp, SDValue &InChain,
998 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000999 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001000 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +00001001 if (ISD::isNON_EXTLoad(InChain.Val) &&
1002 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001003 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +00001004 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +00001005 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001006 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001007 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001008 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001009 return true;
1010 }
1011 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001012
1013 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001014 // elements. This is a vector shuffle from the zero vector.
Evan Chengd880b972008-05-09 21:53:03 +00001015 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001016 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001017 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
1018 N.getOperand(0).Val->hasOneUse() &&
1019 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
1020 N.getOperand(0).getOperand(0).hasOneUse()) {
1021 // Okay, this is a zero extending load. Fold it.
1022 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1023 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1024 return false;
1025 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001026 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001027 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001028 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001029 return false;
1030}
1031
1032
Evan Cheng51a9ed92006-02-25 10:09:08 +00001033/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1034/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001035bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1036 SDValue &Base, SDValue &Scale,
1037 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001038 X86ISelAddressMode AM;
1039 if (MatchAddress(N, AM))
1040 return false;
1041
Duncan Sands83ec4b62008-06-06 12:08:01 +00001042 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001043 unsigned Complexity = 0;
1044 if (AM.BaseType == X86ISelAddressMode::RegBase)
1045 if (AM.Base.Reg.Val)
1046 Complexity = 1;
1047 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001048 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001049 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1050 Complexity = 4;
1051
1052 if (AM.IndexReg.Val)
1053 Complexity++;
1054 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001055 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001056
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001057 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1058 // a simple shift.
1059 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001060 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001061
1062 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1063 // to a LEA. This is determined with some expermentation but is by no means
1064 // optimal (especially for code size consideration). LEA is nice because of
1065 // its three-address nature. Tweak the cost function again when we can run
1066 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001067 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1068 // For X86-64, we should always use lea to materialize RIP relative
1069 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001070 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001071 Complexity = 4;
1072 else
1073 Complexity += 2;
1074 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001075
1076 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1077 Complexity++;
1078
1079 if (Complexity > 2) {
1080 getAddressOperands(AM, Base, Scale, Index, Disp);
1081 return true;
1082 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001083 return false;
1084}
1085
Dan Gohman475871a2008-07-27 21:46:04 +00001086bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1087 SDValue &Base, SDValue &Scale,
1088 SDValue &Index, SDValue &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001089 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001090 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001091 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001092 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001093 return false;
1094}
1095
Evan Cheng7ccced62006-02-18 00:15:05 +00001096/// getGlobalBaseReg - Output the instructions required to put the
1097/// base address to use for accessing globals into a register.
1098///
Evan Cheng9ade2182006-08-26 05:34:46 +00001099SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001100 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001101 if (!GlobalBaseReg) {
1102 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001103 MachineFunction *MF = BB->getParent();
1104 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001105 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001106 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001107 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001108
Evan Chengc0f64ff2006-11-27 23:37:22 +00001109 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001110 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1111 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001112 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001113
1114 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1115 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001116 if (TM.getRelocationModel() == Reloc::PIC_ &&
1117 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001118 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001119 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1120 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001121 } else {
1122 GlobalBaseReg = PC;
1123 }
1124
Evan Cheng7ccced62006-02-18 00:15:05 +00001125 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001126 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001127}
1128
Evan Chengb245d922006-05-20 01:36:52 +00001129static SDNode *FindCallStartFromCall(SDNode *Node) {
1130 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1131 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1132 "Node doesn't have a token chain argument!");
1133 return FindCallStartFromCall(Node->getOperand(0).Val);
1134}
1135
Dan Gohman475871a2008-07-27 21:46:04 +00001136SDNode *X86DAGToDAGISel::getTruncate(SDValue N0, MVT VT) {
1137 SDValue SRIdx;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001138 switch (VT.getSimpleVT()) {
1139 default: assert(0 && "Unknown truncate!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001140 case MVT::i8:
1141 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1142 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1143 if (!Subtarget->is64Bit()) {
1144 unsigned Opc;
Dan Gohman2fbdf0e2008-07-16 16:20:48 +00001145 MVT N0VT = N0.getValueType();
1146 switch (N0VT.getSimpleVT()) {
Christopher Lambc59e5212007-08-10 21:48:46 +00001147 default: assert(0 && "Unknown truncate!");
1148 case MVT::i16:
1149 Opc = X86::MOV16to16_;
Christopher Lambc59e5212007-08-10 21:48:46 +00001150 break;
1151 case MVT::i32:
1152 Opc = X86::MOV32to32_;
Christopher Lambc59e5212007-08-10 21:48:46 +00001153 break;
1154 }
Dan Gohman475871a2008-07-27 21:46:04 +00001155 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
Evan Cheng96aaa542007-10-12 07:55:53 +00001156 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1157 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001158 }
1159 break;
1160 case MVT::i16:
1161 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1162 break;
1163 case MVT::i32:
1164 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1165 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001166 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001167 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001168}
1169
1170
Dan Gohman475871a2008-07-27 21:46:04 +00001171SDNode *X86DAGToDAGISel::Select(SDValue N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001172 SDNode *Node = N.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001173 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001174 unsigned Opc, MOpc;
1175 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001176
Evan Chengf597dc72006-02-10 22:24:32 +00001177#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001178 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001179 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001180 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001181 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001182#endif
1183
Dan Gohmane8be6c62008-07-17 19:10:17 +00001184 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001185#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001186 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001187 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001188 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001189 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001190#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001191 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001192 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001193
Evan Cheng0114e942006-01-06 20:36:21 +00001194 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001195 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001196 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001197 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001198
Evan Cheng51a9ed92006-02-25 10:09:08 +00001199 case ISD::ADD: {
1200 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1201 // code and is matched first so to prevent it from being turned into
1202 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001203 // In 64-bit small code size mode, use LEA to take advantage of
1204 // RIP-relative addressing.
1205 if (TM.getCodeModel() != CodeModel::Small)
1206 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001207 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001208 SDValue N0 = N.getOperand(0);
1209 SDValue N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001210 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001211 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001212 N1.getOpcode() == ISD::Constant) {
1213 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001214 SDValue C(0, 0);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001215 // TODO: handle ExternalSymbolSDNode.
1216 if (GlobalAddressSDNode *G =
1217 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001218 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001219 G->getOffset() + Offset);
1220 } else if (ConstantPoolSDNode *CP =
1221 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001222 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001223 CP->getAlignment(),
1224 CP->getOffset()+Offset);
1225 }
1226
Evan Cheng25ab6902006-09-08 06:48:29 +00001227 if (C.Val) {
1228 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001230 CurDAG->getRegister(0, PtrVT), C };
1231 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1232 } else
1233 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1234 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235 }
1236
1237 // Other cases are handled by auto-generated code.
1238 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001239 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001240
Dan Gohman525178c2007-10-08 18:33:35 +00001241 case ISD::SMUL_LOHI:
1242 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001243 SDValue N0 = Node->getOperand(0);
1244 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001245
Dan Gohman525178c2007-10-08 18:33:35 +00001246 bool isSigned = Opcode == ISD::SMUL_LOHI;
1247 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001248 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001249 default: assert(0 && "Unsupported VT!");
1250 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1251 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1252 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001253 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001254 }
1255 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001256 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001257 default: assert(0 && "Unsupported VT!");
1258 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1259 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1260 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001261 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001262 }
1263
1264 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001265 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001266 default: assert(0 && "Unsupported VT!");
1267 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1268 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1269 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001270 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001271 }
1272
Dan Gohman475871a2008-07-27 21:46:04 +00001273 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001274 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001275 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001276 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001277 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001278 if (foldedLoad)
1279 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001280 }
1281
Evan Cheng04699902006-08-26 01:05:16 +00001282 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1284 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001285
1286 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001287 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001288 AddToISelQueue(Tmp0);
1289 AddToISelQueue(Tmp1);
1290 AddToISelQueue(Tmp2);
1291 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001292 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001293 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001294 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001295 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001296 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001297 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001298 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001299 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001300 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001301 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001302 }
1303
Dan Gohman525178c2007-10-08 18:33:35 +00001304 // Copy the low half of the result, if it is needed.
1305 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001306 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001307 LoReg, NVT, InFlag);
1308 InFlag = Result.getValue(2);
1309 ReplaceUses(N.getValue(0), Result);
1310#ifndef NDEBUG
1311 DOUT << std::string(Indent-2, ' ') << "=> ";
1312 DEBUG(Result.Val->dump(CurDAG));
1313 DOUT << "\n";
1314#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001315 }
Dan Gohman525178c2007-10-08 18:33:35 +00001316 // Copy the high half of the result, if it is needed.
1317 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001318 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001319 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1320 // Prevent use of AH in a REX instruction by referencing AX instead.
1321 // Shift it down 8 bits.
1322 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1323 X86::AX, MVT::i16, InFlag);
1324 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001325 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman525178c2007-10-08 18:33:35 +00001326 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1327 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001328 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1329 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001330 MVT::i8, Result, SRIdx), 0);
1331 } else {
1332 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1333 HiReg, NVT, InFlag);
1334 InFlag = Result.getValue(2);
1335 }
1336 ReplaceUses(N.getValue(1), Result);
1337#ifndef NDEBUG
1338 DOUT << std::string(Indent-2, ' ') << "=> ";
1339 DEBUG(Result.Val->dump(CurDAG));
1340 DOUT << "\n";
1341#endif
1342 }
Evan Cheng34167212006-02-09 00:37:58 +00001343
Evan Chengf597dc72006-02-10 22:24:32 +00001344#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001345 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001346#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001347
Evan Cheng64a752f2006-08-11 09:08:15 +00001348 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001349 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001350
Dan Gohman525178c2007-10-08 18:33:35 +00001351 case ISD::SDIVREM:
1352 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue N0 = Node->getOperand(0);
1354 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001355
1356 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001357 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001358 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001359 default: assert(0 && "Unsupported VT!");
1360 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1361 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1362 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001363 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001364 }
1365 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001366 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001367 default: assert(0 && "Unsupported VT!");
1368 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1369 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1370 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001371 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001372 }
1373
1374 unsigned LoReg, HiReg;
1375 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001376 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001377 default: assert(0 && "Unsupported VT!");
1378 case MVT::i8:
1379 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001380 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001381 SExtOpcode = X86::CBW;
1382 break;
1383 case MVT::i16:
1384 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001385 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001386 SExtOpcode = X86::CWD;
1387 break;
1388 case MVT::i32:
1389 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001390 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001391 SExtOpcode = X86::CDQ;
1392 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001393 case MVT::i64:
1394 LoReg = X86::RAX; HiReg = X86::RDX;
1395 ClrOpcode = X86::MOV64r0;
1396 SExtOpcode = X86::CQO;
1397 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001398 }
1399
Dan Gohman475871a2008-07-27 21:46:04 +00001400 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001401 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1402
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001404 if (NVT == MVT::i8 && !isSigned) {
1405 // Special case for div8, just use a move with zero extension to AX to
1406 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001407 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001408 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001410 AddToISelQueue(N0.getOperand(0));
1411 AddToISelQueue(Tmp0);
1412 AddToISelQueue(Tmp1);
1413 AddToISelQueue(Tmp2);
1414 AddToISelQueue(Tmp3);
1415 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001416 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001417 Ops, 5), 0);
1418 Chain = Move.getValue(1);
1419 ReplaceUses(N0.getValue(1), Chain);
1420 } else {
1421 AddToISelQueue(N0);
1422 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001423 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001424 Chain = CurDAG->getEntryNode();
1425 }
Dan Gohman475871a2008-07-27 21:46:04 +00001426 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001427 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001428 } else {
1429 AddToISelQueue(N0);
1430 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001431 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001432 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001433 if (isSigned) {
1434 // Sign extend the low part into the high part.
1435 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001437 } else {
1438 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001439 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001440 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1441 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001442 }
Evan Cheng948f3432006-01-06 23:19:29 +00001443 }
1444
1445 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001446 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001447 AddToISelQueue(Tmp0);
1448 AddToISelQueue(Tmp1);
1449 AddToISelQueue(Tmp2);
1450 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001452 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001453 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001454 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001455 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001456 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001457 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001458 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001459 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001460 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001461 }
1462
Dan Gohmana37c9f72007-09-25 18:23:27 +00001463 // Copy the division (low) result, if it is needed.
1464 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001465 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001466 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001467 InFlag = Result.getValue(2);
1468 ReplaceUses(N.getValue(0), Result);
1469#ifndef NDEBUG
1470 DOUT << std::string(Indent-2, ' ') << "=> ";
1471 DEBUG(Result.Val->dump(CurDAG));
1472 DOUT << "\n";
1473#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001474 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001475 // Copy the remainder (high) result, if it is needed.
1476 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001477 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001478 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1479 // Prevent use of AH in a REX instruction by referencing AX instead.
1480 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001481 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1482 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001483 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001484 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001485 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1486 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001487 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1488 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001489 MVT::i8, Result, SRIdx), 0);
1490 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001491 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1492 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001493 InFlag = Result.getValue(2);
1494 }
1495 ReplaceUses(N.getValue(1), Result);
1496#ifndef NDEBUG
1497 DOUT << std::string(Indent-2, ' ') << "=> ";
1498 DEBUG(Result.Val->dump(CurDAG));
1499 DOUT << "\n";
1500#endif
1501 }
Evan Chengf597dc72006-02-10 22:24:32 +00001502
1503#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001504 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001505#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001506
1507 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001508 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001509
1510 case ISD::ANY_EXTEND: {
Christopher Lambc9298232008-03-16 03:12:01 +00001511 // Check if the type extended to supports subregs.
1512 if (NVT == MVT::i8)
1513 break;
1514
Dan Gohman475871a2008-07-27 21:46:04 +00001515 SDValue N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001516 // Get the subregsiter index for the type to extend.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001517 MVT N0VT = N0.getValueType();
Christopher Lambc9298232008-03-16 03:12:01 +00001518 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1519 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1520 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1521
1522 // If we don't have a subreg Idx, let generated ISel have a try.
1523 if (Idx == 0)
1524 break;
1525
1526 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001527 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SDValue Undef =
1529 SDValue(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
1530 SDValue SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
Christopher Lambc9298232008-03-16 03:12:01 +00001531 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001532 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001533
1534#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001535 DOUT << std::string(Indent-2, ' ') << "=> ";
1536 DEBUG(ResNode->dump(CurDAG));
1537 DOUT << "\n";
1538 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001539#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001540 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001541 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001542
1543 case ISD::SIGN_EXTEND_INREG: {
Dan Gohman475871a2008-07-27 21:46:04 +00001544 SDValue N0 = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001545 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001546
Duncan Sands83ec4b62008-06-06 12:08:01 +00001547 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman475871a2008-07-27 21:46:04 +00001548 SDValue TruncOp = SDValue(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001549 unsigned Opc = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001550 switch (NVT.getSimpleVT()) {
1551 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001552 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001553 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1554 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001555 break;
1556 case MVT::i32:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001557 switch (SVT.getSimpleVT()) {
1558 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001559 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1560 case MVT::i16: Opc = X86::MOVSX32rr16; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001561 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001562 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001563 case MVT::i64:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001564 switch (SVT.getSimpleVT()) {
1565 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001566 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1567 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1568 case MVT::i32: Opc = X86::MOVSX64rr32; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001569 }
1570 break;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001571 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001572
1573 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1574
1575#ifndef NDEBUG
1576 DOUT << std::string(Indent-2, ' ') << "=> ";
1577 DEBUG(TruncOp.Val->dump(CurDAG));
1578 DOUT << "\n";
1579 DOUT << std::string(Indent-2, ' ') << "=> ";
1580 DEBUG(ResNode->dump(CurDAG));
1581 DOUT << "\n";
1582 Indent -= 2;
1583#endif
1584 return ResNode;
1585 break;
1586 }
1587
1588 case ISD::TRUNCATE: {
Dan Gohman475871a2008-07-27 21:46:04 +00001589 SDValue Input = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001590 AddToISelQueue(Node->getOperand(0));
1591 SDNode *ResNode = getTruncate(Input, NVT);
1592
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
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001599 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001600 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001601 }
Evan Cheng851bc042008-06-17 02:01:22 +00001602
1603 case ISD::DECLARE: {
1604 // Handle DECLARE nodes here because the second operand may have been
1605 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001606 SDValue Chain = Node->getOperand(0);
1607 SDValue N1 = Node->getOperand(1);
1608 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001609 if (!isa<FrameIndexSDNode>(N1))
1610 break;
1611 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1612 if (N2.getOpcode() == ISD::ADD &&
1613 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1614 N2 = N2.getOperand(1);
1615 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001616 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001617 GlobalValue *GV =
1618 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001619 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1620 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001621 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001623 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1624 MVT::Other, Ops, 3);
1625 }
1626 break;
1627 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001628 }
1629
Evan Cheng9ade2182006-08-26 05:34:46 +00001630 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001631
Evan Chengf597dc72006-02-10 22:24:32 +00001632#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001633 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001634 if (ResNode == NULL || ResNode == N.Val)
1635 DEBUG(N.Val->dump(CurDAG));
1636 else
1637 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001638 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001639 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001640#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001641
1642 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001643}
1644
Chris Lattnerc0bad572006-06-08 18:03:49 +00001645bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001646SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1647 std::vector<SDValue> &OutOps, SelectionDAG &DAG){
1648 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001649 switch (ConstraintCode) {
1650 case 'o': // offsetable ??
1651 case 'v': // not offsetable ??
1652 default: return true;
1653 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001654 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001655 return true;
1656 break;
1657 }
1658
Evan Cheng04699902006-08-26 01:05:16 +00001659 OutOps.push_back(Op0);
1660 OutOps.push_back(Op1);
1661 OutOps.push_back(Op2);
1662 OutOps.push_back(Op3);
1663 AddToISelQueue(Op0);
1664 AddToISelQueue(Op1);
1665 AddToISelQueue(Op2);
1666 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001667 return false;
1668}
1669
Chris Lattnerc961eea2005-11-16 01:54:32 +00001670/// createX86ISelDag - This pass converts a legalized DAG into a
1671/// X86-specific DAG, ready for instruction scheduling.
1672///
Evan Chenge50794a2006-08-29 18:28:33 +00001673FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1674 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001675}