blob: 21837d53d4331fb32aa22f0aade565555f0abb24 [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
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000237 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
238 /// truncate of the specified operand to i8. This can be done with tablegen,
239 /// except that this code uses MVT::Flag in a tricky way that happens to
240 /// improve scheduling in some cases.
241 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000242
Evan Cheng23addc02006-02-10 22:46:26 +0000243#ifndef NDEBUG
244 unsigned Indent;
245#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000246 };
247}
248
Evan Chengcdda25d2008-04-25 08:22:20 +0000249/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
250///
Evan Chenga275ecb2006-10-10 01:46:56 +0000251static SDNode *findFlagUse(SDNode *N) {
252 unsigned FlagResNo = N->getNumValues()-1;
253 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +0000254 SDNode *User = *I;
Evan Chenga275ecb2006-10-10 01:46:56 +0000255 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000256 SDValue Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000257 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000258 return User;
259 }
260 }
261 return NULL;
262}
263
Evan Chengcdda25d2008-04-25 08:22:20 +0000264/// findNonImmUse - Return true by reference in "found" if "Use" is an
265/// non-immediate use of "Def". This function recursively traversing
266/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000267static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
268 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000269 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000270 if (found ||
271 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000272 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000273 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000274
Evan Cheng27e1fe92006-10-14 08:33:25 +0000275 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000276 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000277 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000278 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000279 if (N == Def) {
280 if (Use == ImmedUse)
Evan Cheng419ace92008-04-25 08:55:28 +0000281 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000282 if (Use == Root) {
Evan Cheng419ace92008-04-25 08:55:28 +0000283 // Must be a chain reading node where it is possible to reach its own
284 // chain operand through a path started from another operand.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000285 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000286 Use->getOpcode() == X86ISD::CMP ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000287 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
288 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000289 continue;
290 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000291 found = true;
292 break;
293 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000294
295 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000296 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000297 }
298}
299
Evan Cheng27e1fe92006-10-14 08:33:25 +0000300/// isNonImmUse - Start searching from Root up the DAG to check is Def can
301/// be reached. Return true if that's the case. However, ignore direct uses
302/// by ImmedUse (which would be U in the example illustrated in
303/// CanBeFoldedBy) and by Root (which can happen in the store case).
304/// FIXME: to be really generic, we should allow direct use by any node
305/// that is being folded. But realisticly since we only fold loads which
306/// have one non-chain use, we only need to watch out for load/op/store
307/// and load/op/cmp case where the root (store / cmp) may reach the load via
308/// its chain operand.
309static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
310 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000311 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000312 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000313 findNonImmUse(Root, Def, ImmedUse, Root, Skip, 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
Evan Chenga8df1b42006-07-27 16:44:36 +0000321 // If U use can somehow reach N through another path then U can't fold N or
322 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000323 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000324 // a successor of U.
325 //
326 // [ N ]
327 // ^ ^
328 // | |
329 // / \---
330 // / [X]
331 // | ^
332 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000333
334 if (isNonImmUse(Root, N, U))
335 return false;
336
337 // If U produces a flag, then it gets (even more) interesting. Since it
338 // would have been "glued" together with its flag use, we need to check if
339 // it might reach N:
340 //
341 // [ N ]
342 // ^ ^
343 // | |
344 // [U] \--
345 // ^ [TF]
346 // | ^
347 // | |
348 // \ /
349 // [FU]
350 //
351 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
352 // NU), then TF is a predecessor of FU and a successor of NU. But since
353 // NU and FU are flagged together, this effectively creates a cycle.
354 bool HasFlagUse = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000355 MVT VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000356 while ((VT == MVT::Flag && !Root->use_empty())) {
357 SDNode *FU = findFlagUse(Root);
358 if (FU == NULL)
359 break;
360 else {
361 Root = FU;
362 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000363 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000364 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000365 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000366
367 if (HasFlagUse)
368 return !isNonImmUse(Root, N, Root, U);
369 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000370}
371
Evan Cheng70e674e2006-08-28 20:10:17 +0000372/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
373/// and move load below the TokenFactor. Replace store's chain operand with
374/// load's chain result.
Dan Gohman475871a2008-07-27 21:46:04 +0000375static void MoveBelowTokenFactor(SelectionDAG &DAG, SDValue Load,
376 SDValue Store, SDValue TF) {
377 std::vector<SDValue> Ops;
Evan Cheng70e674e2006-08-28 20:10:17 +0000378 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
379 if (Load.Val == TF.Val->getOperand(i).Val)
380 Ops.push_back(Load.Val->getOperand(0));
381 else
382 Ops.push_back(TF.Val->getOperand(i));
383 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
384 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
385 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
386 Store.getOperand(2), Store.getOperand(3));
387}
388
Evan Chengcd0baf22008-05-23 21:23:16 +0000389/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
390///
Dan Gohman475871a2008-07-27 21:46:04 +0000391static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
392 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000393 if (N.getOpcode() == ISD::BIT_CONVERT)
394 N = N.getOperand(0);
395
396 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
397 if (!LD || LD->isVolatile())
398 return false;
399 if (LD->getAddressingMode() != ISD::UNINDEXED)
400 return false;
401
402 ISD::LoadExtType ExtType = LD->getExtensionType();
403 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
404 return false;
405
406 if (N.hasOneUse() &&
407 N.getOperand(1) == Address &&
408 N.Val->isOperandOf(Chain.Val)) {
409 Load = N;
410 return true;
411 }
412 return false;
413}
414
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000415/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
416/// This is only run if not in -fast mode (aka -O0).
417/// This allows the instruction selector to pick more read-modify-write
418/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000419///
420/// [Load chain]
421/// ^
422/// |
423/// [Load]
424/// ^ ^
425/// | |
426/// / \-
427/// / |
428/// [TokenFactor] [Op]
429/// ^ ^
430/// | |
431/// \ /
432/// \ /
433/// [Store]
434///
435/// The fact the store's chain operand != load's chain will prevent the
436/// (store (op (load))) instruction from being selected. We can transform it to:
437///
438/// [Load chain]
439/// ^
440/// |
441/// [TokenFactor]
442/// ^
443/// |
444/// [Load]
445/// ^ ^
446/// | |
447/// | \-
448/// | |
449/// | [Op]
450/// | ^
451/// | |
452/// \ /
453/// \ /
454/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000455void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000456 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
457 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000458 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000459 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000460 SDValue Chain = I->getOperand(0);
Evan Cheng70e674e2006-08-28 20:10:17 +0000461 if (Chain.Val->getOpcode() != ISD::TokenFactor)
462 continue;
463
Dan Gohman475871a2008-07-27 21:46:04 +0000464 SDValue N1 = I->getOperand(1);
465 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000466 if ((N1.getValueType().isFloatingPoint() &&
467 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000468 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000469 continue;
470
471 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000472 SDValue Load;
Evan Cheng70e674e2006-08-28 20:10:17 +0000473 unsigned Opcode = N1.Val->getOpcode();
474 switch (Opcode) {
475 case ISD::ADD:
476 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000477 case ISD::AND:
478 case ISD::OR:
479 case ISD::XOR:
480 case ISD::ADDC:
Evan Chengcd0baf22008-05-23 21:23:16 +0000481 case ISD::ADDE:
482 case ISD::VECTOR_SHUFFLE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000483 SDValue N10 = N1.getOperand(0);
484 SDValue N11 = N1.getOperand(1);
Evan Chengcd0baf22008-05-23 21:23:16 +0000485 RModW = isRMWLoad(N10, Chain, N2, Load);
486 if (!RModW)
487 RModW = isRMWLoad(N11, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000488 break;
489 }
490 case ISD::SUB:
491 case ISD::SHL:
492 case ISD::SRA:
493 case ISD::SRL:
494 case ISD::ROTL:
495 case ISD::ROTR:
496 case ISD::SUBC:
497 case ISD::SUBE:
498 case X86ISD::SHLD:
499 case X86ISD::SHRD: {
Dan Gohman475871a2008-07-27 21:46:04 +0000500 SDValue N10 = N1.getOperand(0);
Evan Chengcd0baf22008-05-23 21:23:16 +0000501 RModW = isRMWLoad(N10, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000502 break;
503 }
504 }
505
Evan Cheng82a35b32006-08-29 06:44:17 +0000506 if (RModW) {
Dan Gohman475871a2008-07-27 21:46:04 +0000507 MoveBelowTokenFactor(DAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000508 ++NumLoadMoved;
509 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000510 }
511}
512
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000513
514/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
515/// nodes that target the FP stack to be store and load to the stack. This is a
516/// gross hack. We would like to simply mark these as being illegal, but when
517/// we do that, legalize produces these when it expands calls, then expands
518/// these in the same legalize pass. We would like dag combine to be able to
519/// hack on these between the call expansion and the node legalization. As such
520/// this pass basically does "really late" legalization of these inline with the
521/// X86 isel pass.
522void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
523 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
524 E = DAG.allnodes_end(); I != E; ) {
525 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
526 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
527 continue;
528
529 // If the source and destination are SSE registers, then this is a legal
530 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000531 MVT SrcVT = N->getOperand(0).getValueType();
532 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000533 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
534 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
535 if (SrcIsSSE && DstIsSSE)
536 continue;
537
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000538 if (!SrcIsSSE && !DstIsSSE) {
539 // If this is an FPStack extension, it is a noop.
540 if (N->getOpcode() == ISD::FP_EXTEND)
541 continue;
542 // If this is a value-preserving FPStack truncation, it is a noop.
543 if (N->getConstantOperandVal(1))
544 continue;
545 }
546
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000547 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
548 // FPStack has extload and truncstore. SSE can fold direct loads into other
549 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000550 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000551 if (N->getOpcode() == ISD::FP_ROUND)
552 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
553 else
554 MemVT = SrcIsSSE ? SrcVT : DstVT;
555
Dan Gohman475871a2008-07-27 21:46:04 +0000556 SDValue MemTmp = DAG.CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000557
558 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000560 MemTmp, NULL, 0, MemVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000561 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000562 NULL, 0, MemVT);
563
564 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
565 // extload we created. This will cause general havok on the dag because
566 // anything below the conversion could be folded into other existing nodes.
567 // To avoid invalidating 'I', back it up to the convert node.
568 --I;
Dan Gohman475871a2008-07-27 21:46:04 +0000569 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000570
571 // Now that we did that, the node is dead. Increment the iterator to the
572 // next node to process, then delete N.
573 ++I;
574 DAG.DeleteNode(N);
575 }
576}
577
Chris Lattnerc961eea2005-11-16 01:54:32 +0000578/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
579/// when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000580void X86DAGToDAGISel::InstructionSelect(SelectionDAG &DAG) {
581 CurBB = BB; // BB can change as result of isel.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000582
Evan Chengdb8d56b2008-06-30 20:45:06 +0000583 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000584 if (!Fast)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000585 PreprocessForRMW(DAG);
586
587 // FIXME: This should only happen when not -fast.
588 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000589
Chris Lattnerc961eea2005-11-16 01:54:32 +0000590 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000591#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000592 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000593 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000594#endif
Dan Gohmanad3460c2008-08-21 16:36:34 +0000595 SelectRoot();
Evan Chengf597dc72006-02-10 22:24:32 +0000596#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000597 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000598#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000599
Chris Lattnerc961eea2005-11-16 01:54:32 +0000600 DAG.RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000601}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000602
Dan Gohman462dc7f2008-07-21 20:00:07 +0000603void X86DAGToDAGISel::InstructionSelectPostProcessing() {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000604 // If we are emitting FP stack code, scan the basic block to determine if this
605 // block defines any FP values. If so, put an FP_REG_KILL instruction before
606 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000607
Dale Johannesen48d1e452007-09-24 22:52:39 +0000608 // Note that FP stack instructions are used in all modes for long double,
609 // so we always need to do this check.
610 // Also note that it's possible for an FP stack register to be live across
611 // an instruction that produces multiple basic blocks (SSE CMOV) so we
612 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000613
614 // Scan all of the machine instructions in these MBBs, checking for FP
615 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000616 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000617 MachineFunction::iterator EndMBB = BB; ++EndMBB;
618 for (; MBBI != EndMBB; ++MBBI) {
619 MachineBasicBlock *MBB = MBBI;
620
621 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
622 // before the return.
623 if (!MBB->empty()) {
624 MachineBasicBlock::iterator EndI = MBB->end();
625 --EndI;
626 if (EndI->getDesc().isReturn())
627 continue;
628 }
629
Dale Johannesen48d1e452007-09-24 22:52:39 +0000630 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000631 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000632 !ContainsFPCode && I != E; ++I) {
633 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
634 const TargetRegisterClass *clas;
635 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
636 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000637 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000638 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000639 X86::RFP32RegisterClass ||
640 clas == X86::RFP64RegisterClass ||
641 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000642 ContainsFPCode = true;
643 break;
644 }
645 }
646 }
647 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000648 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
649 // a copy of the input value in this block. In SSE mode, we only care about
650 // 80-bit values.
651 if (!ContainsFPCode) {
652 // Final check, check LLVM BB's that are successors to the LLVM BB
653 // corresponding to BB for FP PHI nodes.
654 const BasicBlock *LLVMBB = BB->getBasicBlock();
655 const PHINode *PN;
656 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
657 !ContainsFPCode && SI != E; ++SI) {
658 for (BasicBlock::const_iterator II = SI->begin();
659 (PN = dyn_cast<PHINode>(II)); ++II) {
660 if (PN->getType()==Type::X86_FP80Ty ||
661 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
662 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
663 ContainsFPCode = true;
664 break;
665 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000666 }
667 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000668 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000669 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
670 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000671 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000672 TM.getInstrInfo()->get(X86::FP_REG_KILL));
673 ++NumFPKill;
674 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000675 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000676}
677
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000678/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
679/// the main function.
680void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
681 MachineFrameInfo *MFI) {
682 const TargetInstrInfo *TII = TM.getInstrInfo();
683 if (Subtarget->isTargetCygMing())
684 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
685}
686
687void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
688 // If this is main, emit special code for main.
689 MachineBasicBlock *BB = MF.begin();
690 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
691 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
692}
693
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000694/// MatchAddress - Add the specified node to the specified addressing mode,
695/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000696/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000697bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000698 bool isRoot, unsigned Depth) {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000699DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000700 // Limit recursion.
701 if (Depth > 5)
702 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000703
Evan Cheng25ab6902006-09-08 06:48:29 +0000704 // RIP relative addressing: %rip + 32-bit displacement!
705 if (AM.isRIPRel) {
706 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000707 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000708 if (isInt32(AM.Disp + Val)) {
709 AM.Disp += Val;
710 return false;
711 }
712 }
713 return true;
714 }
715
Evan Cheng2ef88a02006-08-07 22:28:20 +0000716 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000717 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000718
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000719 switch (N.getOpcode()) {
720 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000721 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000722 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000723 if (isInt32(AM.Disp + Val)) {
724 AM.Disp += Val;
725 return false;
726 }
727 break;
728 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000729
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000730 case X86ISD::Wrapper: {
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000731DOUT << "Wrapper: 64bit " << Subtarget->is64Bit();
732DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
733DOUT << "AlreadySelected " << AlreadySelected << "\n";
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000734 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000735 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000736 // Also, base and index reg must be 0 in order to use rip as base.
737 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
738 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000739 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000740 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
741 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000742 // If value is available in a register both base and index components have
743 // been picked, we can't fit the result available in the register in the
744 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000745 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000746 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000747 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
748 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000749 AM.GV = GV;
750 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000751 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
752 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000753 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000754 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000755 AM.CP = CP->getConstVal();
756 AM.Align = CP->getAlignment();
757 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000758 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
759 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000760 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000761 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000762 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000763 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
764 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000765 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000766 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000767 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000768 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
769 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000770 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000771 }
772 }
773 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000774 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000775
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000776 case ISD::FrameIndex:
777 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
778 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
779 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
780 return false;
781 }
782 break;
Evan Chengec693f72005-12-08 02:01:35 +0000783
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000784 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000785 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000786 break;
787
788 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
789 unsigned Val = CN->getValue();
790 if (Val == 1 || Val == 2 || Val == 3) {
791 AM.Scale = 1 << Val;
Dan Gohman475871a2008-07-27 21:46:04 +0000792 SDValue ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000793
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000794 // Okay, we know that we have a scale by now. However, if the scaled
795 // value is an add of something and a constant, we can fold the
796 // constant into the disp field here.
797 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
798 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
799 AM.IndexReg = ShVal.Val->getOperand(0);
800 ConstantSDNode *AddVal =
801 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
802 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
803 if (isInt32(Disp))
804 AM.Disp = Disp;
805 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000806 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000807 } else {
808 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000809 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000810 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000811 }
812 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000813 }
Evan Chengec693f72005-12-08 02:01:35 +0000814
Dan Gohman83688052007-10-22 20:22:24 +0000815 case ISD::SMUL_LOHI:
816 case ISD::UMUL_LOHI:
817 // A mul_lohi where we need the low part can be folded as a plain multiply.
818 if (N.ResNo != 0) break;
819 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000820 case ISD::MUL:
821 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000822 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000823 AM.BaseType == X86ISelAddressMode::RegBase &&
824 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000825 AM.IndexReg.Val == 0 &&
826 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000827 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
828 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
829 AM.Scale = unsigned(CN->getValue())-1;
830
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue MulVal = N.Val->getOperand(0);
832 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000833
834 // Okay, we know that we have a scale by now. However, if the scaled
835 // value is an add of something and a constant, we can fold the
836 // constant into the disp field here.
837 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
838 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
839 Reg = MulVal.Val->getOperand(0);
840 ConstantSDNode *AddVal =
841 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000842 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
843 if (isInt32(Disp))
844 AM.Disp = Disp;
845 else
846 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000847 } else {
848 Reg = N.Val->getOperand(0);
849 }
850
851 AM.IndexReg = AM.Base.Reg = Reg;
852 return false;
853 }
Chris Lattner62412262007-02-04 20:18:17 +0000854 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000855 break;
856
Chris Lattner62412262007-02-04 20:18:17 +0000857 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000858 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000859 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000860 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
861 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000862 return false;
863 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000864 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
865 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000866 return false;
867 AM = Backup;
868 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000870
Chris Lattner62412262007-02-04 20:18:17 +0000871 case ISD::OR:
872 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000873 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000874
875 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
876 X86ISelAddressMode Backup = AM;
877 // Start with the LHS as an addr mode.
878 if (!MatchAddress(N.getOperand(0), AM, false) &&
879 // Address could not have picked a GV address for the displacement.
880 AM.GV == NULL &&
881 // On x86-64, the resultant disp must fit in 32-bits.
882 isInt32(AM.Disp + CN->getSignExtended()) &&
883 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000884 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000885 AM.Disp += CN->getValue();
886 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000887 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000888 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000889 }
890 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000891
892 case ISD::AND: {
893 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
894 // allows us to fold the shift into this addressing mode.
895 if (AlreadySelected) break;
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000897 if (Shift.getOpcode() != ISD::SHL) break;
898
899 // Scale must not be used already.
900 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000901
902 // Not when RIP is used as the base.
903 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000904
905 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
906 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
907 if (!C1 || !C2) break;
908
909 // Not likely to be profitable if either the AND or SHIFT node has more
910 // than one use (unless all uses are for address computation). Besides,
911 // isel mechanism requires their node ids to be reused.
912 if (!N.hasOneUse() || !Shift.hasOneUse())
913 break;
914
915 // Verify that the shift amount is something we can fold.
916 unsigned ShiftCst = C1->getValue();
917 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
918 break;
919
920 // Get the new AND mask, this folds to a constant.
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
922 SDValue(C2, 0), SDValue(C1, 0));
923 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
Evan Cheng1314b002007-12-13 00:43:27 +0000924 Shift.getOperand(0), NewANDMask);
925 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
926 NewAND.Val->setNodeId(N.Val->getNodeId());
927
928 AM.Scale = 1 << ShiftCst;
929 AM.IndexReg = NewAND;
930 return false;
931 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000932 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000933
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000934 return MatchAddressBase(N, AM, isRoot, Depth);
935}
936
937/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
938/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +0000939bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000940 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000941 // Is the base register already occupied?
942 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
943 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000944 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000945 AM.IndexReg = N;
946 AM.Scale = 1;
947 return false;
948 }
949
950 // Otherwise, we cannot select it.
951 return true;
952 }
953
954 // Default, generate it as a register.
955 AM.BaseType = X86ISelAddressMode::RegBase;
956 AM.Base.Reg = N;
957 return false;
958}
959
Evan Chengec693f72005-12-08 02:01:35 +0000960/// SelectAddr - returns true if it is able pattern match an addressing mode.
961/// It returns the operands which make up the maximal addressing mode it can
962/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +0000963bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
964 SDValue &Scale, SDValue &Index,
965 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000966 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000967 if (MatchAddress(N, AM))
968 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000969
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000971 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000972 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000973 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000974 }
Evan Cheng8700e142006-01-11 06:09:51 +0000975
Evan Cheng7dd281b2006-02-05 05:25:07 +0000976 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000977 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000978
979 getAddressOperands(AM, Base, Scale, Index, Disp);
980 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000981}
982
Chris Lattner4fe4f252006-10-11 22:09:58 +0000983/// isZeroNode - Returns true if Elt is a constant zero or a floating point
984/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000985static inline bool isZeroNode(SDValue Elt) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000986 return ((isa<ConstantSDNode>(Elt) &&
987 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
988 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000989 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000990}
991
992
Chris Lattner3a7cd952006-10-07 21:55:32 +0000993/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
994/// match a load whose top elements are either undef or zeros. The load flavor
995/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000996bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
997 SDValue N, SDValue &Base,
998 SDValue &Scale, SDValue &Index,
999 SDValue &Disp, SDValue &InChain,
1000 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001001 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001002 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +00001003 if (ISD::isNON_EXTLoad(InChain.Val) &&
1004 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001005 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +00001006 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +00001007 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001008 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001009 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001010 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001011 return true;
1012 }
1013 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001014
1015 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001016 // elements. This is a vector shuffle from the zero vector.
Evan Chengd880b972008-05-09 21:53:03 +00001017 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001018 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001019 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
1020 N.getOperand(0).Val->hasOneUse() &&
1021 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
1022 N.getOperand(0).getOperand(0).hasOneUse()) {
1023 // Okay, this is a zero extending load. Fold it.
1024 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1025 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1026 return false;
1027 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001028 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001029 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001030 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001031 return false;
1032}
1033
1034
Evan Cheng51a9ed92006-02-25 10:09:08 +00001035/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1036/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001037bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1038 SDValue &Base, SDValue &Scale,
1039 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001040 X86ISelAddressMode AM;
1041 if (MatchAddress(N, AM))
1042 return false;
1043
Duncan Sands83ec4b62008-06-06 12:08:01 +00001044 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001045 unsigned Complexity = 0;
1046 if (AM.BaseType == X86ISelAddressMode::RegBase)
1047 if (AM.Base.Reg.Val)
1048 Complexity = 1;
1049 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001050 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001051 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1052 Complexity = 4;
1053
1054 if (AM.IndexReg.Val)
1055 Complexity++;
1056 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001057 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001058
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001059 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1060 // a simple shift.
1061 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001062 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001063
1064 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1065 // to a LEA. This is determined with some expermentation but is by no means
1066 // optimal (especially for code size consideration). LEA is nice because of
1067 // its three-address nature. Tweak the cost function again when we can run
1068 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001069 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1070 // For X86-64, we should always use lea to materialize RIP relative
1071 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001072 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001073 Complexity = 4;
1074 else
1075 Complexity += 2;
1076 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001077
1078 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1079 Complexity++;
1080
1081 if (Complexity > 2) {
1082 getAddressOperands(AM, Base, Scale, Index, Disp);
1083 return true;
1084 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001085 return false;
1086}
1087
Dan Gohman475871a2008-07-27 21:46:04 +00001088bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1089 SDValue &Base, SDValue &Scale,
1090 SDValue &Index, SDValue &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001091 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001092 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001093 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001094 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001095 return false;
1096}
1097
Evan Cheng7ccced62006-02-18 00:15:05 +00001098/// getGlobalBaseReg - Output the instructions required to put the
1099/// base address to use for accessing globals into a register.
1100///
Evan Cheng9ade2182006-08-26 05:34:46 +00001101SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001102 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001103 if (!GlobalBaseReg) {
1104 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001105 MachineFunction *MF = BB->getParent();
1106 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001107 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001108 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001109 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001110
Evan Chengc0f64ff2006-11-27 23:37:22 +00001111 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001112 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1113 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001114 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001115
1116 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1117 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001118 if (TM.getRelocationModel() == Reloc::PIC_ &&
1119 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001120 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001121 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1122 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001123 } else {
1124 GlobalBaseReg = PC;
1125 }
1126
Evan Cheng7ccced62006-02-18 00:15:05 +00001127 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001128 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001129}
1130
Evan Chengb245d922006-05-20 01:36:52 +00001131static SDNode *FindCallStartFromCall(SDNode *Node) {
1132 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1133 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1134 "Node doesn't have a token chain argument!");
1135 return FindCallStartFromCall(Node->getOperand(0).Val);
1136}
1137
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001138/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1139/// truncate of the specified operand to i8. This can be done with tablegen,
1140/// except that this code uses MVT::Flag in a tricky way that happens to
1141/// improve scheduling in some cases.
1142SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1143 assert(!Subtarget->is64Bit() &&
1144 "getTruncateTo8Bit is only needed on x86-32!");
1145 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1146
1147 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1148 unsigned Opc;
1149 MVT N0VT = N0.getValueType();
1150 switch (N0VT.getSimpleVT()) {
1151 default: assert(0 && "Unknown truncate!");
1152 case MVT::i16:
1153 Opc = X86::MOV16to16_;
1154 break;
1155 case MVT::i32:
1156 Opc = X86::MOV32to32_;
1157 break;
1158 }
1159
1160 // The use of MVT::Flag here is not strictly accurate, but it helps
1161 // scheduling in some cases.
1162 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1163 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1164 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001165}
1166
1167
Dan Gohman475871a2008-07-27 21:46:04 +00001168SDNode *X86DAGToDAGISel::Select(SDValue N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001169 SDNode *Node = N.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001170 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001171 unsigned Opc, MOpc;
1172 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001173
Evan Chengf597dc72006-02-10 22:24:32 +00001174#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001175 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001176 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001177 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001178 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001179#endif
1180
Dan Gohmane8be6c62008-07-17 19:10:17 +00001181 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001182#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001183 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001184 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001185 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001186 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001187#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001188 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001189 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001190
Evan Cheng0114e942006-01-06 20:36:21 +00001191 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001192 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001193 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001194 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001195
Evan Cheng51a9ed92006-02-25 10:09:08 +00001196 case ISD::ADD: {
1197 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1198 // code and is matched first so to prevent it from being turned into
1199 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001200 // In 64-bit small code size mode, use LEA to take advantage of
1201 // RIP-relative addressing.
1202 if (TM.getCodeModel() != CodeModel::Small)
1203 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001204 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SDValue N0 = N.getOperand(0);
1206 SDValue N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001207 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001208 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001209 N1.getOpcode() == ISD::Constant) {
1210 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00001211 SDValue C(0, 0);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001212 // TODO: handle ExternalSymbolSDNode.
1213 if (GlobalAddressSDNode *G =
1214 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001215 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001216 G->getOffset() + Offset);
1217 } else if (ConstantPoolSDNode *CP =
1218 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001219 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001220 CP->getAlignment(),
1221 CP->getOffset()+Offset);
1222 }
1223
Evan Cheng25ab6902006-09-08 06:48:29 +00001224 if (C.Val) {
1225 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001226 SDValue Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
Evan Cheng25ab6902006-09-08 06:48:29 +00001227 CurDAG->getRegister(0, PtrVT), C };
1228 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1229 } else
1230 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1231 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001232 }
1233
1234 // Other cases are handled by auto-generated code.
1235 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001236 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001237
Dan Gohman525178c2007-10-08 18:33:35 +00001238 case ISD::SMUL_LOHI:
1239 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001240 SDValue N0 = Node->getOperand(0);
1241 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001242
Dan Gohman525178c2007-10-08 18:33:35 +00001243 bool isSigned = Opcode == ISD::SMUL_LOHI;
1244 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001246 default: assert(0 && "Unsupported VT!");
1247 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1248 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1249 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001250 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001251 }
1252 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001253 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001254 default: assert(0 && "Unsupported VT!");
1255 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1256 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1257 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001258 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001259 }
1260
1261 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001263 default: assert(0 && "Unsupported VT!");
1264 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1265 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1266 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001267 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001268 }
1269
Dan Gohman475871a2008-07-27 21:46:04 +00001270 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001271 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001272 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001273 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001274 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001275 if (foldedLoad)
1276 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001277 }
1278
Evan Cheng04699902006-08-26 01:05:16 +00001279 AddToISelQueue(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00001280 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1281 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001282
1283 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001284 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001285 AddToISelQueue(Tmp0);
1286 AddToISelQueue(Tmp1);
1287 AddToISelQueue(Tmp2);
1288 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001289 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001290 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001291 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001292 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001293 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001294 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001295 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001296 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001297 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001299 }
1300
Dan Gohman525178c2007-10-08 18:33:35 +00001301 // Copy the low half of the result, if it is needed.
1302 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001303 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001304 LoReg, NVT, InFlag);
1305 InFlag = Result.getValue(2);
1306 ReplaceUses(N.getValue(0), Result);
1307#ifndef NDEBUG
1308 DOUT << std::string(Indent-2, ' ') << "=> ";
1309 DEBUG(Result.Val->dump(CurDAG));
1310 DOUT << "\n";
1311#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001312 }
Dan Gohman525178c2007-10-08 18:33:35 +00001313 // Copy the high half of the result, if it is needed.
1314 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001315 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001316 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1317 // Prevent use of AH in a REX instruction by referencing AX instead.
1318 // Shift it down 8 bits.
1319 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1320 X86::AX, MVT::i16, InFlag);
1321 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001322 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohman525178c2007-10-08 18:33:35 +00001323 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1324 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001325 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1326 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001327 MVT::i8, Result, SRIdx), 0);
1328 } else {
1329 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1330 HiReg, NVT, InFlag);
1331 InFlag = Result.getValue(2);
1332 }
1333 ReplaceUses(N.getValue(1), Result);
1334#ifndef NDEBUG
1335 DOUT << std::string(Indent-2, ' ') << "=> ";
1336 DEBUG(Result.Val->dump(CurDAG));
1337 DOUT << "\n";
1338#endif
1339 }
Evan Cheng34167212006-02-09 00:37:58 +00001340
Evan Chengf597dc72006-02-10 22:24:32 +00001341#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001342 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001343#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001344
Evan Cheng64a752f2006-08-11 09:08:15 +00001345 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001346 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001347
Dan Gohman525178c2007-10-08 18:33:35 +00001348 case ISD::SDIVREM:
1349 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001350 SDValue N0 = Node->getOperand(0);
1351 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001352
1353 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001354 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001355 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001356 default: assert(0 && "Unsupported VT!");
1357 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1358 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1359 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001360 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001361 }
1362 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001363 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001364 default: assert(0 && "Unsupported VT!");
1365 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1366 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1367 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001368 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001369 }
1370
1371 unsigned LoReg, HiReg;
1372 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001373 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001374 default: assert(0 && "Unsupported VT!");
1375 case MVT::i8:
1376 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001377 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001378 SExtOpcode = X86::CBW;
1379 break;
1380 case MVT::i16:
1381 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001382 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001383 SExtOpcode = X86::CWD;
1384 break;
1385 case MVT::i32:
1386 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001387 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001388 SExtOpcode = X86::CDQ;
1389 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001390 case MVT::i64:
1391 LoReg = X86::RAX; HiReg = X86::RDX;
1392 ClrOpcode = X86::MOV64r0;
1393 SExtOpcode = X86::CQO;
1394 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001395 }
1396
Dan Gohman475871a2008-07-27 21:46:04 +00001397 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001398 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1399
Dan Gohman475871a2008-07-27 21:46:04 +00001400 SDValue InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001401 if (NVT == MVT::i8 && !isSigned) {
1402 // Special case for div8, just use a move with zero extension to AX to
1403 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001404 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001405 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001407 AddToISelQueue(N0.getOperand(0));
1408 AddToISelQueue(Tmp0);
1409 AddToISelQueue(Tmp1);
1410 AddToISelQueue(Tmp2);
1411 AddToISelQueue(Tmp3);
1412 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001413 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001414 Ops, 5), 0);
1415 Chain = Move.getValue(1);
1416 ReplaceUses(N0.getValue(1), Chain);
1417 } else {
1418 AddToISelQueue(N0);
1419 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001420 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001421 Chain = CurDAG->getEntryNode();
1422 }
Dan Gohman475871a2008-07-27 21:46:04 +00001423 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001424 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001425 } else {
1426 AddToISelQueue(N0);
1427 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001428 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001429 LoReg, N0, SDValue()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001430 if (isSigned) {
1431 // Sign extend the low part into the high part.
1432 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001433 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001434 } else {
1435 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001437 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1438 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001439 }
Evan Cheng948f3432006-01-06 23:19:29 +00001440 }
1441
1442 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001443 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001444 AddToISelQueue(Tmp0);
1445 AddToISelQueue(Tmp1);
1446 AddToISelQueue(Tmp2);
1447 AddToISelQueue(Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00001448 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001449 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001450 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001452 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001453 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001454 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001455 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001456 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001458 }
1459
Dan Gohmana37c9f72007-09-25 18:23:27 +00001460 // Copy the division (low) result, if it is needed.
1461 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001463 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001464 InFlag = Result.getValue(2);
1465 ReplaceUses(N.getValue(0), Result);
1466#ifndef NDEBUG
1467 DOUT << std::string(Indent-2, ' ') << "=> ";
1468 DEBUG(Result.Val->dump(CurDAG));
1469 DOUT << "\n";
1470#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001471 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001472 // Copy the remainder (high) result, if it is needed.
1473 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001474 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001475 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1476 // Prevent use of AH in a REX instruction by referencing AX instead.
1477 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001478 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1479 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001480 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001481 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001482 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1483 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1485 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001486 MVT::i8, Result, SRIdx), 0);
1487 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001488 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1489 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001490 InFlag = Result.getValue(2);
1491 }
1492 ReplaceUses(N.getValue(1), Result);
1493#ifndef NDEBUG
1494 DOUT << std::string(Indent-2, ' ') << "=> ";
1495 DEBUG(Result.Val->dump(CurDAG));
1496 DOUT << "\n";
1497#endif
1498 }
Evan Chengf597dc72006-02-10 22:24:32 +00001499
1500#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001501 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001502#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001503
1504 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001505 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001506
Christopher Lambc59e5212007-08-10 21:48:46 +00001507 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001508 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001509 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1510 SDValue N0 = Node->getOperand(0);
1511 AddToISelQueue(N0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001512
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001513 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1514 unsigned Opc = 0;
1515 switch (NVT.getSimpleVT()) {
1516 default: assert(0 && "Unknown sign_extend_inreg!");
1517 case MVT::i16:
1518 Opc = X86::MOVSX16rr8;
1519 break;
1520 case MVT::i32:
1521 Opc = X86::MOVSX32rr8;
1522 break;
1523 }
1524
1525 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001526
1527#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001528 DOUT << std::string(Indent-2, ' ') << "=> ";
1529 DEBUG(TruncOp.Val->dump(CurDAG));
1530 DOUT << "\n";
1531 DOUT << std::string(Indent-2, ' ') << "=> ";
1532 DEBUG(ResNode->dump(CurDAG));
1533 DOUT << "\n";
1534 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001535#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001536 return ResNode;
1537 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001538 break;
1539 }
1540
1541 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001542 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1543 SDValue Input = Node->getOperand(0);
1544 AddToISelQueue(Node->getOperand(0));
1545 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001546
Evan Cheng403be7e2006-05-08 08:01:26 +00001547#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001548 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001549 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001550 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001551 Indent -= 2;
1552#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001553 return ResNode;
1554 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001555 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001556 }
Evan Cheng851bc042008-06-17 02:01:22 +00001557
1558 case ISD::DECLARE: {
1559 // Handle DECLARE nodes here because the second operand may have been
1560 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001561 SDValue Chain = Node->getOperand(0);
1562 SDValue N1 = Node->getOperand(1);
1563 SDValue N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001564 if (!isa<FrameIndexSDNode>(N1))
1565 break;
1566 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1567 if (N2.getOpcode() == ISD::ADD &&
1568 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1569 N2 = N2.getOperand(1);
1570 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001571 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001572 GlobalValue *GV =
1573 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1575 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
Evan Cheng851bc042008-06-17 02:01:22 +00001576 AddToISelQueue(Chain);
Dan Gohman475871a2008-07-27 21:46:04 +00001577 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Evan Cheng851bc042008-06-17 02:01:22 +00001578 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1579 MVT::Other, Ops, 3);
1580 }
1581 break;
1582 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001583 }
1584
Evan Cheng9ade2182006-08-26 05:34:46 +00001585 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001586
Evan Chengf597dc72006-02-10 22:24:32 +00001587#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001588 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001589 if (ResNode == NULL || ResNode == N.Val)
1590 DEBUG(N.Val->dump(CurDAG));
1591 else
1592 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001593 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001594 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001595#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001596
1597 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001598}
1599
Chris Lattnerc0bad572006-06-08 18:03:49 +00001600bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001601SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1602 std::vector<SDValue> &OutOps, SelectionDAG &DAG){
1603 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001604 switch (ConstraintCode) {
1605 case 'o': // offsetable ??
1606 case 'v': // not offsetable ??
1607 default: return true;
1608 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001609 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001610 return true;
1611 break;
1612 }
1613
Evan Cheng04699902006-08-26 01:05:16 +00001614 OutOps.push_back(Op0);
1615 OutOps.push_back(Op1);
1616 OutOps.push_back(Op2);
1617 OutOps.push_back(Op3);
1618 AddToISelQueue(Op0);
1619 AddToISelQueue(Op1);
1620 AddToISelQueue(Op2);
1621 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001622 return false;
1623}
1624
Chris Lattnerc961eea2005-11-16 01:54:32 +00001625/// createX86ISelDag - This pass converts a legalized DAG into a
1626/// X86-specific DAG, ready for instruction scheduling.
1627///
Evan Chenge50794a2006-08-29 18:28:33 +00001628FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1629 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001630}