blob: c5a6edd87a77da0f87f7b2accf3d668f3df0068a [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Chris Lattnerc961eea2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000061 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 int FrameIndex;
63 } Base;
64
Evan Chengbe3bf422008-02-07 08:53:49 +000065 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000067 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000068 int32_t Disp;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000071 const char *ES;
72 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000076 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 }
Dale Johannesen50dd1d02008-08-11 23:46:25 +000079 void dump() {
80 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000081 cerr << "Base.Reg ";
82 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
83 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000084 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
85 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000086 cerr << "IndexReg ";
87 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
88 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000089 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 {
Evan Cheng25ab6902006-09-08 06:48:29 +0000107 /// TM - Keep a reference to X86TargetMachine.
108 ///
109 X86TargetMachine &TM;
110
Chris Lattnerc961eea2005-11-16 01:54:32 +0000111 /// X86Lowering - This object fully describes how to lower LLVM code to an
112 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000113 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000114
115 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
116 /// make the right decision when generating code for different targets.
117 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000118
Evan Chengdb8d56b2008-06-30 20:45:06 +0000119 /// CurBB - Current BB being isel'd.
120 ///
121 MachineBasicBlock *CurBB;
122
Evan Chengb7a75a52008-09-26 23:41:32 +0000123 /// OptForSize - If true, selector should try to optimize for code size
124 /// instead of performance.
125 bool OptForSize;
126
Chris Lattnerc961eea2005-11-16 01:54:32 +0000127 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000128 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman79ce2762009-01-15 19:20:50 +0000129 : SelectionDAGISel(tm, fast),
Dan Gohman38217fe2008-10-03 16:17:33 +0000130 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000131 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000132 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000133
134 virtual const char *getPassName() const {
135 return "X86 DAG->DAG Instruction Selection";
136 }
137
Evan Chengdb8d56b2008-06-30 20:45:06 +0000138 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000139 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000140 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000141
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000142 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
143
Evan Cheng884c70c2008-11-27 00:49:46 +0000144 virtual
145 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000146
Chris Lattnerc961eea2005-11-16 01:54:32 +0000147// Include the pieces autogenerated from the target description.
148#include "X86GenDAGISel.inc"
149
150 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000151 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000152 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153
Dan Gohman475871a2008-07-27 21:46:04 +0000154 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000155 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000156 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000157 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000158 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
159 SDValue &Scale, SDValue &Index, SDValue &Disp);
160 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
161 SDValue &Scale, SDValue &Index, SDValue &Disp);
162 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
163 SDValue N, SDValue &Base, SDValue &Scale,
164 SDValue &Index, SDValue &Disp,
165 SDValue &InChain, SDValue &OutChain);
166 bool TryFoldLoad(SDValue P, SDValue N,
167 SDValue &Base, SDValue &Scale,
168 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000169 void PreprocessForRMW();
170 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000171
Chris Lattnerc0bad572006-06-08 18:03:49 +0000172 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
173 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000174 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000175 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000176 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000177
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000178 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
179
Dan Gohman475871a2008-07-27 21:46:04 +0000180 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
181 SDValue &Scale, SDValue &Index,
182 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000183 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000184 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
185 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000186 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000187 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000188 // These are 32-bit even in 64-bit mode since RIP relative offset
189 // is 32-bit.
190 if (AM.GV)
191 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
192 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000193 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
194 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000195 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000196 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000197 else if (AM.JT != -1)
198 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
199 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000200 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000201 }
202
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000203 /// getI8Imm - Return a target constant with the specified value, of type
204 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000205 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000206 return CurDAG->getTargetConstant(Imm, MVT::i8);
207 }
208
Chris Lattnerc961eea2005-11-16 01:54:32 +0000209 /// getI16Imm - Return a target constant with the specified value, of type
210 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000211 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000212 return CurDAG->getTargetConstant(Imm, MVT::i16);
213 }
214
215 /// getI32Imm - Return a target constant with the specified value, of type
216 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000217 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000218 return CurDAG->getTargetConstant(Imm, MVT::i32);
219 }
Evan Chengf597dc72006-02-10 22:24:32 +0000220
Dan Gohman8b746962008-09-23 18:22:58 +0000221 /// getGlobalBaseReg - Return an SDNode that returns the value of
222 /// the global base register. Output instructions required to
223 /// initialize the global base register, if necessary.
224 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000225 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000226
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000227 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
228 /// truncate of the specified operand to i8. This can be done with tablegen,
229 /// except that this code uses MVT::Flag in a tricky way that happens to
230 /// improve scheduling in some cases.
231 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000232
Evan Cheng23addc02006-02-10 22:46:26 +0000233#ifndef NDEBUG
234 unsigned Indent;
235#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000236 };
237}
238
Gabor Greif93c53e52008-08-31 15:37:04 +0000239/// findFlagUse - Return use of MVT::Flag value produced by the specified
240/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000241///
Evan Chenga275ecb2006-10-10 01:46:56 +0000242static SDNode *findFlagUse(SDNode *N) {
243 unsigned FlagResNo = N->getNumValues()-1;
244 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohmane8ecf482009-01-27 02:37:43 +0000245 SDUse &Use = I.getUse();
246 if (Use.getResNo() == FlagResNo)
247 return Use.getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000248 }
249 return NULL;
250}
251
Evan Chengcdda25d2008-04-25 08:22:20 +0000252/// findNonImmUse - Return true by reference in "found" if "Use" is an
253/// non-immediate use of "Def". This function recursively traversing
254/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000255static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
Dan Gohman682d5a82008-09-17 01:39:10 +0000256 SDNode *Root, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000257 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000258 if (found ||
Dan Gohmanf06c8352008-09-30 18:30:35 +0000259 Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000260 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000261 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000262
Evan Cheng27e1fe92006-10-14 08:33:25 +0000263 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000264 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000265 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000266 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000267 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000268 assert(N != Root);
Evan Chengf4b4c412006-08-08 00:31:00 +0000269 found = true;
270 break;
271 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000272
273 // Traverse up the operand chain.
Dan Gohman682d5a82008-09-17 01:39:10 +0000274 findNonImmUse(N, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000275 }
276}
277
Evan Cheng27e1fe92006-10-14 08:33:25 +0000278/// isNonImmUse - Start searching from Root up the DAG to check is Def can
279/// be reached. Return true if that's the case. However, ignore direct uses
280/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng884c70c2008-11-27 00:49:46 +0000281/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
282/// case).
Evan Cheng27e1fe92006-10-14 08:33:25 +0000283/// FIXME: to be really generic, we should allow direct use by any node
284/// that is being folded. But realisticly since we only fold loads which
285/// have one non-chain use, we only need to watch out for load/op/store
286/// and load/op/cmp case where the root (store / cmp) may reach the load via
287/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000288static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000289 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000290 bool found = false;
Dan Gohman682d5a82008-09-17 01:39:10 +0000291 findNonImmUse(Root, Def, ImmedUse, Root, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000292 return found;
293}
294
295
Evan Cheng884c70c2008-11-27 00:49:46 +0000296bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
297 SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000298 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000299
Evan Cheng884c70c2008-11-27 00:49:46 +0000300 if (U == Root)
301 switch (U->getOpcode()) {
302 default: break;
303 case ISD::ADD:
304 case ISD::ADDC:
305 case ISD::ADDE:
306 case ISD::AND:
307 case ISD::OR:
308 case ISD::XOR: {
309 // If the other operand is a 8-bit immediate we should fold the immediate
310 // instead. This reduces code size.
311 // e.g.
312 // movl 4(%esp), %eax
313 // addl $4, %eax
314 // vs.
315 // movl $4, %eax
316 // addl 4(%esp), %eax
317 // The former is 2 bytes shorter. In case where the increment is 1, then
318 // the saving can be 4 bytes (by using incl %eax).
319 ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1));
320 if (Imm) {
321 if (U->getValueType(0) == MVT::i64) {
322 if ((int32_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
323 return false;
324 } else {
325 if ((int8_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue())
326 return false;
327 }
328 }
329 }
330 }
331
Dan Gohman682d5a82008-09-17 01:39:10 +0000332 // If Root use can somehow reach N through a path that that doesn't contain
333 // U then folding N would create a cycle. e.g. In the following
334 // diagram, Root can reach N through X. If N is folded into into Root, then
335 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000336 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000337 // [N*] //
338 // ^ ^ //
339 // / \ //
340 // [U*] [X]? //
341 // ^ ^ //
342 // \ / //
343 // \ / //
344 // [Root*] //
345 //
346 // * indicates nodes to be folded together.
347 //
348 // If Root produces a flag, then it gets (even more) interesting. Since it
349 // will be "glued" together with its flag use in the scheduler, we need to
350 // check if it might reach N.
351 //
352 // [N*] //
353 // ^ ^ //
354 // / \ //
355 // [U*] [X]? //
356 // ^ ^ //
357 // \ \ //
358 // \ | //
359 // [Root*] | //
360 // ^ | //
361 // f | //
362 // | / //
363 // [Y] / //
364 // ^ / //
365 // f / //
366 // | / //
367 // [FU] //
368 //
369 // If FU (flag use) indirectly reaches N (the load), and Root folds N
370 // (call it Fold), then X is a predecessor of FU and a successor of
371 // Fold. But since Fold and FU are flagged together, this will create
372 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000373
Duncan Sands83ec4b62008-06-06 12:08:01 +0000374 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000375 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000376 SDNode *FU = findFlagUse(Root);
377 if (FU == NULL)
378 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000379 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000380 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000381 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000382
Dan Gohman682d5a82008-09-17 01:39:10 +0000383 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000384}
385
Evan Cheng70e674e2006-08-28 20:10:17 +0000386/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
387/// and move load below the TokenFactor. Replace store's chain operand with
388/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000389static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000390 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000391 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000392 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
393 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000394 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000395 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000397 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
398 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
399 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
400 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000401}
402
Evan Chengcd0baf22008-05-23 21:23:16 +0000403/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
404///
Dan Gohman475871a2008-07-27 21:46:04 +0000405static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
406 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000407 if (N.getOpcode() == ISD::BIT_CONVERT)
408 N = N.getOperand(0);
409
410 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
411 if (!LD || LD->isVolatile())
412 return false;
413 if (LD->getAddressingMode() != ISD::UNINDEXED)
414 return false;
415
416 ISD::LoadExtType ExtType = LD->getExtensionType();
417 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
418 return false;
419
420 if (N.hasOneUse() &&
421 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000422 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000423 Load = N;
424 return true;
425 }
426 return false;
427}
428
Evan Chengab6c3bb2008-08-25 21:27:18 +0000429/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
430/// operand and move load below the call's chain operand.
431static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000432 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000433 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000434 SDValue Chain = CallSeqStart.getOperand(0);
435 if (Chain.getNode() == Load.getNode())
436 Ops.push_back(Load.getOperand(0));
437 else {
438 assert(Chain.getOpcode() == ISD::TokenFactor &&
439 "Unexpected CallSeqStart chain operand");
440 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
441 if (Chain.getOperand(i).getNode() == Load.getNode())
442 Ops.push_back(Load.getOperand(0));
443 else
444 Ops.push_back(Chain.getOperand(i));
445 SDValue NewChain =
446 CurDAG->getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
447 Ops.clear();
448 Ops.push_back(NewChain);
449 }
450 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
451 Ops.push_back(CallSeqStart.getOperand(i));
452 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000453 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
454 Load.getOperand(1), Load.getOperand(2));
455 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000456 Ops.push_back(SDValue(Load.getNode(), 1));
457 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000458 Ops.push_back(Call.getOperand(i));
459 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
460}
461
462/// isCalleeLoad - Return true if call address is a load and it can be
463/// moved below CALLSEQ_START and the chains leading up to the call.
464/// Return the CALLSEQ_START by reference as a second output.
465static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000466 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000467 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000468 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000469 if (!LD ||
470 LD->isVolatile() ||
471 LD->getAddressingMode() != ISD::UNINDEXED ||
472 LD->getExtensionType() != ISD::NON_EXTLOAD)
473 return false;
474
475 // Now let's find the callseq_start.
476 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
477 if (!Chain.hasOneUse())
478 return false;
479 Chain = Chain.getOperand(0);
480 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000481
482 if (Chain.getOperand(0).getNode() == Callee.getNode())
483 return true;
484 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
485 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
486 return true;
487 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000488}
489
490
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000491/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
492/// This is only run if not in -fast mode (aka -O0).
493/// This allows the instruction selector to pick more read-modify-write
494/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000495///
496/// [Load chain]
497/// ^
498/// |
499/// [Load]
500/// ^ ^
501/// | |
502/// / \-
503/// / |
504/// [TokenFactor] [Op]
505/// ^ ^
506/// | |
507/// \ /
508/// \ /
509/// [Store]
510///
511/// The fact the store's chain operand != load's chain will prevent the
512/// (store (op (load))) instruction from being selected. We can transform it to:
513///
514/// [Load chain]
515/// ^
516/// |
517/// [TokenFactor]
518/// ^
519/// |
520/// [Load]
521/// ^ ^
522/// | |
523/// | \-
524/// | |
525/// | [Op]
526/// | ^
527/// | |
528/// \ /
529/// \ /
530/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000531void X86DAGToDAGISel::PreprocessForRMW() {
532 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
533 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000534 if (I->getOpcode() == X86ISD::CALL) {
535 /// Also try moving call address load from outside callseq_start to just
536 /// before the call to allow it to be folded.
537 ///
538 /// [Load chain]
539 /// ^
540 /// |
541 /// [Load]
542 /// ^ ^
543 /// | |
544 /// / \--
545 /// / |
546 ///[CALLSEQ_START] |
547 /// ^ |
548 /// | |
549 /// [LOAD/C2Reg] |
550 /// | |
551 /// \ /
552 /// \ /
553 /// [CALL]
554 SDValue Chain = I->getOperand(0);
555 SDValue Load = I->getOperand(1);
556 if (!isCalleeLoad(Load, Chain))
557 continue;
558 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
559 ++NumLoadMoved;
560 continue;
561 }
562
Evan Cheng8b2794a2006-10-13 21:14:26 +0000563 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000564 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000565 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000566
Gabor Greifba36cb52008-08-28 21:40:38 +0000567 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000568 continue;
569
Dan Gohman475871a2008-07-27 21:46:04 +0000570 SDValue N1 = I->getOperand(1);
571 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000572 if ((N1.getValueType().isFloatingPoint() &&
573 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000574 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000575 continue;
576
577 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000578 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000579 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000580 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000581 case ISD::ADD:
582 case ISD::MUL:
583 case ISD::AND:
584 case ISD::OR:
585 case ISD::XOR:
586 case ISD::ADDC:
587 case ISD::ADDE:
588 case ISD::VECTOR_SHUFFLE: {
589 SDValue N10 = N1.getOperand(0);
590 SDValue N11 = N1.getOperand(1);
591 RModW = isRMWLoad(N10, Chain, N2, Load);
592 if (!RModW)
593 RModW = isRMWLoad(N11, Chain, N2, Load);
594 break;
595 }
596 case ISD::SUB:
597 case ISD::SHL:
598 case ISD::SRA:
599 case ISD::SRL:
600 case ISD::ROTL:
601 case ISD::ROTR:
602 case ISD::SUBC:
603 case ISD::SUBE:
604 case X86ISD::SHLD:
605 case X86ISD::SHRD: {
606 SDValue N10 = N1.getOperand(0);
607 RModW = isRMWLoad(N10, Chain, N2, Load);
608 break;
609 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000610 }
611
Evan Cheng82a35b32006-08-29 06:44:17 +0000612 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000613 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000614 ++NumLoadMoved;
615 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000616 }
617}
618
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000619
620/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
621/// nodes that target the FP stack to be store and load to the stack. This is a
622/// gross hack. We would like to simply mark these as being illegal, but when
623/// we do that, legalize produces these when it expands calls, then expands
624/// these in the same legalize pass. We would like dag combine to be able to
625/// hack on these between the call expansion and the node legalization. As such
626/// this pass basically does "really late" legalization of these inline with the
627/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000628void X86DAGToDAGISel::PreprocessForFPConvert() {
629 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
630 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000631 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
632 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
633 continue;
634
635 // If the source and destination are SSE registers, then this is a legal
636 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000637 MVT SrcVT = N->getOperand(0).getValueType();
638 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000639 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
640 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
641 if (SrcIsSSE && DstIsSSE)
642 continue;
643
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000644 if (!SrcIsSSE && !DstIsSSE) {
645 // If this is an FPStack extension, it is a noop.
646 if (N->getOpcode() == ISD::FP_EXTEND)
647 continue;
648 // If this is a value-preserving FPStack truncation, it is a noop.
649 if (N->getConstantOperandVal(1))
650 continue;
651 }
652
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000653 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
654 // FPStack has extload and truncstore. SSE can fold direct loads into other
655 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000656 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000657 if (N->getOpcode() == ISD::FP_ROUND)
658 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
659 else
660 MemVT = SrcIsSSE ? SrcVT : DstVT;
661
Dan Gohmanf350b272008-08-23 02:25:05 +0000662 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000663
664 // FIXME: optimize the case where the src/dest is a load or store?
Dan Gohmanf350b272008-08-23 02:25:05 +0000665 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(),
666 N->getOperand(0),
667 MemTmp, NULL, 0, MemVT);
668 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
669 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000670
671 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
672 // extload we created. This will cause general havok on the dag because
673 // anything below the conversion could be folded into other existing nodes.
674 // To avoid invalidating 'I', back it up to the convert node.
675 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000676 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000677
678 // Now that we did that, the node is dead. Increment the iterator to the
679 // next node to process, then delete N.
680 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000681 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000682 }
683}
684
Chris Lattnerc961eea2005-11-16 01:54:32 +0000685/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
686/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000687void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000688 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000689 const Function *F = CurDAG->getMachineFunction().getFunction();
690 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000691
Evan Chengdb8d56b2008-06-30 20:45:06 +0000692 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000693 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000694 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000695
696 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000697 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000698
Chris Lattnerc961eea2005-11-16 01:54:32 +0000699 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000700#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000701 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000702 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000703#endif
David Greene8ad4c002008-10-27 21:56:29 +0000704 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000705#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000706 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000707#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000708
Dan Gohmanf350b272008-08-23 02:25:05 +0000709 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000710}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000711
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000712/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
713/// the main function.
714void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
715 MachineFrameInfo *MFI) {
716 const TargetInstrInfo *TII = TM.getInstrInfo();
717 if (Subtarget->isTargetCygMing())
718 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
719}
720
721void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
722 // If this is main, emit special code for main.
723 MachineBasicBlock *BB = MF.begin();
724 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
725 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
726}
727
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000728/// MatchAddress - Add the specified node to the specified addressing mode,
729/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000730/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000731bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000732 bool isRoot, unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000733 bool is64Bit = Subtarget->is64Bit();
Evan Chengda43bcf2008-09-24 00:05:32 +0000734 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000735 // Limit recursion.
736 if (Depth > 5)
737 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000738
Evan Cheng25ab6902006-09-08 06:48:29 +0000739 // RIP relative addressing: %rip + 32-bit displacement!
740 if (AM.isRIPRel) {
741 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000742 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000743 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000744 AM.Disp += Val;
745 return false;
746 }
747 }
748 return true;
749 }
750
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000751 switch (N.getOpcode()) {
752 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000753 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000754 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000755 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000756 AM.Disp += Val;
757 return false;
758 }
759 break;
760 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000761
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000762 case X86ISD::Wrapper: {
Dan Gohman6520e202008-10-18 02:06:02 +0000763 DOUT << "Wrapper: 64bit " << is64Bit;
764 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Evan Cheng0085a282006-11-30 21:55:46 +0000765 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000766 // Also, base and index reg must be 0 in order to use rip as base.
767 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000768 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000769 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000770 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
771 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000772 // If value is available in a register both base and index components have
773 // been picked, we can't fit the result available in the register in the
774 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000775 {
Dan Gohman475871a2008-07-27 21:46:04 +0000776 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000777 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000778 uint64_t Offset = G->getOffset();
779 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000780 GlobalValue *GV = G->getGlobal();
781 AM.GV = GV;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000782 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000783 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
784 return false;
785 }
Evan Cheng28b514392006-12-05 19:50:18 +0000786 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000787 uint64_t Offset = CP->getOffset();
788 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000789 AM.CP = CP->getConstVal();
790 AM.Align = CP->getAlignment();
Dan Gohman27cae7b2008-11-11 15:52:29 +0000791 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000792 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
793 return false;
794 }
Bill Wendling056292f2008-09-16 21:48:12 +0000795 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000796 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000797 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000798 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000799 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000800 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000801 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000802 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000803 }
804 }
805 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000806 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000807
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000808 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000809 if (AM.BaseType == X86ISelAddressMode::RegBase
810 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000811 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
812 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
813 return false;
814 }
815 break;
Evan Chengec693f72005-12-08 02:01:35 +0000816
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000817 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000818 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000819 break;
820
Gabor Greif93c53e52008-08-31 15:37:04 +0000821 if (ConstantSDNode
822 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000823 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000824 if (Val == 1 || Val == 2 || Val == 3) {
825 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000826 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000827
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000828 // Okay, we know that we have a scale by now. However, if the scaled
829 // value is an add of something and a constant, we can fold the
830 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
832 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
833 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000834 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000836 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000837 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000838 AM.Disp = Disp;
839 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000840 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000841 } else {
842 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000843 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000844 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845 }
846 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000847 }
Evan Chengec693f72005-12-08 02:01:35 +0000848
Dan Gohman83688052007-10-22 20:22:24 +0000849 case ISD::SMUL_LOHI:
850 case ISD::UMUL_LOHI:
851 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000852 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000853 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000854 case ISD::MUL:
855 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000856 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000857 AM.Base.Reg.getNode() == 0 &&
858 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000859 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000860 if (ConstantSDNode
861 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000862 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
863 CN->getZExtValue() == 9) {
864 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865
Gabor Greifba36cb52008-08-28 21:40:38 +0000866 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000867 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000868
869 // Okay, we know that we have a scale by now. However, if the scaled
870 // value is an add of something and a constant, we can fold the
871 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000872 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
873 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
874 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000875 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000876 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000877 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000878 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000879 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000880 AM.Disp = Disp;
881 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000882 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000883 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000884 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000885 }
886
887 AM.IndexReg = AM.Base.Reg = Reg;
888 return false;
889 }
Chris Lattner62412262007-02-04 20:18:17 +0000890 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000891 break;
892
Evan Cheng8e278262009-01-17 07:09:27 +0000893 case ISD::ADD: {
894 X86ISelAddressMode Backup = AM;
895 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
896 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
897 return false;
898 AM = Backup;
899 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
900 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
901 return false;
902 AM = Backup;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000903 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000904 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000905
Chris Lattner62412262007-02-04 20:18:17 +0000906 case ISD::OR:
907 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000908 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
909 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000910 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000911 // Start with the LHS as an addr mode.
912 if (!MatchAddress(N.getOperand(0), AM, false) &&
913 // Address could not have picked a GV address for the displacement.
914 AM.GV == NULL &&
915 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000916 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000917 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000918 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000919 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000920 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000921 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000922 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000923 }
924 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000925
926 case ISD::AND: {
927 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
928 // allows us to fold the shift into this addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000929 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000930 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000931
Evan Cheng1314b002007-12-13 00:43:27 +0000932 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000933 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000934
935 // Not when RIP is used as the base.
936 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000937
938 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
939 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
940 if (!C1 || !C2) break;
941
942 // Not likely to be profitable if either the AND or SHIFT node has more
943 // than one use (unless all uses are for address computation). Besides,
944 // isel mechanism requires their node ids to be reused.
945 if (!N.hasOneUse() || !Shift.hasOneUse())
946 break;
947
948 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000949 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +0000950 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
951 break;
952
953 // Get the new AND mask, this folds to a constant.
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000954 SDValue X = Shift.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +0000956 SDValue(C2, 0), SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000957 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), X, NewANDMask);
Dan Gohman7b8e9642008-10-13 20:52:04 +0000958 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(),
959 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000960
961 // Insert the new nodes into the topological ordering.
962 if (C1->getNodeId() > X.getNode()->getNodeId()) {
963 CurDAG->RepositionNode(X.getNode(), C1);
964 C1->setNodeId(X.getNode()->getNodeId());
965 }
966 if (NewANDMask.getNode()->getNodeId() == -1 ||
967 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
968 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
969 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
970 }
971 if (NewAND.getNode()->getNodeId() == -1 ||
972 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
973 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
974 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
975 }
976 if (NewSHIFT.getNode()->getNodeId() == -1 ||
977 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
978 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
979 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
980 }
981
Dan Gohman7b8e9642008-10-13 20:52:04 +0000982 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +0000983
984 AM.Scale = 1 << ShiftCst;
985 AM.IndexReg = NewAND;
986 return false;
987 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000988 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000989
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000990 return MatchAddressBase(N, AM, isRoot, Depth);
991}
992
993/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
994/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +0000995bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000996 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000997 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +0000998 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000999 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001000 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001001 AM.IndexReg = N;
1002 AM.Scale = 1;
1003 return false;
1004 }
1005
1006 // Otherwise, we cannot select it.
1007 return true;
1008 }
1009
1010 // Default, generate it as a register.
1011 AM.BaseType = X86ISelAddressMode::RegBase;
1012 AM.Base.Reg = N;
1013 return false;
1014}
1015
Evan Chengec693f72005-12-08 02:01:35 +00001016/// SelectAddr - returns true if it is able pattern match an addressing mode.
1017/// It returns the operands which make up the maximal addressing mode it can
1018/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001019bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1020 SDValue &Scale, SDValue &Index,
1021 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001022 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +00001023 if (MatchAddress(N, AM))
1024 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001025
Duncan Sands83ec4b62008-06-06 12:08:01 +00001026 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001027 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001028 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001029 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001030 }
Evan Cheng8700e142006-01-11 06:09:51 +00001031
Gabor Greifba36cb52008-08-28 21:40:38 +00001032 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001033 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001034
1035 getAddressOperands(AM, Base, Scale, Index, Disp);
1036 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001037}
1038
Chris Lattner3a7cd952006-10-07 21:55:32 +00001039/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1040/// match a load whose top elements are either undef or zeros. The load flavor
1041/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001042bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1043 SDValue N, SDValue &Base,
1044 SDValue &Scale, SDValue &Index,
1045 SDValue &Disp, SDValue &InChain,
1046 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001047 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001048 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001049 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001050 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001051 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001052 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001053 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001054 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001055 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001056 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001057 return true;
1058 }
1059 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001060
1061 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001062 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001063 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001064 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001065 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001066 N.getOperand(0).getNode()->hasOneUse() &&
1067 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001068 N.getOperand(0).getOperand(0).hasOneUse()) {
1069 // Okay, this is a zero extending load. Fold it.
1070 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1071 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1072 return false;
1073 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001074 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001075 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001076 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001077 return false;
1078}
1079
1080
Evan Cheng51a9ed92006-02-25 10:09:08 +00001081/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1082/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001083bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1084 SDValue &Base, SDValue &Scale,
1085 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001086 X86ISelAddressMode AM;
1087 if (MatchAddress(N, AM))
1088 return false;
1089
Duncan Sands83ec4b62008-06-06 12:08:01 +00001090 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001091 unsigned Complexity = 0;
1092 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001093 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001094 Complexity = 1;
1095 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001096 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001097 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1098 Complexity = 4;
1099
Gabor Greifba36cb52008-08-28 21:40:38 +00001100 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001101 Complexity++;
1102 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001103 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001104
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001105 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1106 // a simple shift.
1107 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001108 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001109
1110 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1111 // to a LEA. This is determined with some expermentation but is by no means
1112 // optimal (especially for code size consideration). LEA is nice because of
1113 // its three-address nature. Tweak the cost function again when we can run
1114 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001115 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1116 // For X86-64, we should always use lea to materialize RIP relative
1117 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001118 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001119 Complexity = 4;
1120 else
1121 Complexity += 2;
1122 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001123
Gabor Greifba36cb52008-08-28 21:40:38 +00001124 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001125 Complexity++;
1126
1127 if (Complexity > 2) {
1128 getAddressOperands(AM, Base, Scale, Index, Disp);
1129 return true;
1130 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001131 return false;
1132}
1133
Dan Gohman475871a2008-07-27 21:46:04 +00001134bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1135 SDValue &Base, SDValue &Scale,
1136 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001137 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001138 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001139 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001140 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001141 return false;
1142}
1143
Dan Gohman8b746962008-09-23 18:22:58 +00001144/// getGlobalBaseReg - Return an SDNode that returns the value of
1145/// the global base register. Output instructions required to
1146/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001147///
Evan Cheng9ade2182006-08-26 05:34:46 +00001148SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001149 MachineFunction *MF = CurBB->getParent();
1150 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001151 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001152}
1153
Evan Chengb245d922006-05-20 01:36:52 +00001154static SDNode *FindCallStartFromCall(SDNode *Node) {
1155 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1156 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1157 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001159}
1160
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001161/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1162/// truncate of the specified operand to i8. This can be done with tablegen,
1163/// except that this code uses MVT::Flag in a tricky way that happens to
1164/// improve scheduling in some cases.
1165SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1166 assert(!Subtarget->is64Bit() &&
1167 "getTruncateTo8Bit is only needed on x86-32!");
1168 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1169
1170 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1171 unsigned Opc;
1172 MVT N0VT = N0.getValueType();
1173 switch (N0VT.getSimpleVT()) {
1174 default: assert(0 && "Unknown truncate!");
1175 case MVT::i16:
1176 Opc = X86::MOV16to16_;
1177 break;
1178 case MVT::i32:
1179 Opc = X86::MOV32to32_;
1180 break;
1181 }
1182
1183 // The use of MVT::Flag here is not strictly accurate, but it helps
1184 // scheduling in some cases.
1185 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0);
1186 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1187 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001188}
1189
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001190SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1191 SDValue Chain = Node->getOperand(0);
1192 SDValue In1 = Node->getOperand(1);
1193 SDValue In2L = Node->getOperand(2);
1194 SDValue In2H = Node->getOperand(3);
1195 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1196 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1197 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001198 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001199 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
1200 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8);
1201}
Christopher Lambc59e5212007-08-10 21:48:46 +00001202
Dan Gohman475871a2008-07-27 21:46:04 +00001203SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001204 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001205 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001206 unsigned Opc, MOpc;
1207 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001208
Evan Chengf597dc72006-02-10 22:24:32 +00001209#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001210 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001211 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001212 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001213 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001214#endif
1215
Dan Gohmane8be6c62008-07-17 19:10:17 +00001216 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001217#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001218 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001219 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001220 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001221 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001222#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001223 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001224 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001225
Evan Cheng0114e942006-01-06 20:36:21 +00001226 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001227 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001228 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001229 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001230
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001231 case X86ISD::ATOMOR64_DAG:
1232 return SelectAtomic64(Node, X86::ATOMOR6432);
1233 case X86ISD::ATOMXOR64_DAG:
1234 return SelectAtomic64(Node, X86::ATOMXOR6432);
1235 case X86ISD::ATOMADD64_DAG:
1236 return SelectAtomic64(Node, X86::ATOMADD6432);
1237 case X86ISD::ATOMSUB64_DAG:
1238 return SelectAtomic64(Node, X86::ATOMSUB6432);
1239 case X86ISD::ATOMNAND64_DAG:
1240 return SelectAtomic64(Node, X86::ATOMNAND6432);
1241 case X86ISD::ATOMAND64_DAG:
1242 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001243 case X86ISD::ATOMSWAP64_DAG:
1244 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001245
Dan Gohman525178c2007-10-08 18:33:35 +00001246 case ISD::SMUL_LOHI:
1247 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001248 SDValue N0 = Node->getOperand(0);
1249 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001250
Dan Gohman525178c2007-10-08 18:33:35 +00001251 bool isSigned = Opcode == ISD::SMUL_LOHI;
1252 if (!isSigned)
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::MUL8r; MOpc = X86::MUL8m; break;
1256 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1257 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001258 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001259 }
1260 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001261 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001262 default: assert(0 && "Unsupported VT!");
1263 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1264 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1265 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001266 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001267 }
1268
1269 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001270 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001271 default: assert(0 && "Unsupported VT!");
1272 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1273 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1274 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001275 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001276 }
1277
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001279 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001280 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001281 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001282 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001283 if (foldedLoad)
1284 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001285 }
1286
Dan Gohman475871a2008-07-27 21:46:04 +00001287 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1288 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001289
1290 if (foldedLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00001291 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001292 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001293 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001294 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001295 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001296 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001297 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001298 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001299 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001300 }
1301
Dan Gohman525178c2007-10-08 18:33:35 +00001302 // Copy the low half of the result, if it is needed.
1303 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001305 LoReg, NVT, InFlag);
1306 InFlag = Result.getValue(2);
1307 ReplaceUses(N.getValue(0), Result);
1308#ifndef NDEBUG
1309 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001310 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001311 DOUT << "\n";
1312#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001313 }
Dan Gohman525178c2007-10-08 18:33:35 +00001314 // Copy the high half of the result, if it is needed.
1315 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001316 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001317 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1318 // Prevent use of AH in a REX instruction by referencing AX instead.
1319 // Shift it down 8 bits.
1320 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1321 X86::AX, MVT::i16, InFlag);
1322 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001323 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001324 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001325 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001326 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1327 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohman525178c2007-10-08 18:33:35 +00001328 MVT::i8, Result, SRIdx), 0);
1329 } else {
1330 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1331 HiReg, NVT, InFlag);
1332 InFlag = Result.getValue(2);
1333 }
1334 ReplaceUses(N.getValue(1), Result);
1335#ifndef NDEBUG
1336 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001338 DOUT << "\n";
1339#endif
1340 }
Evan Cheng34167212006-02-09 00:37:58 +00001341
Evan Chengf597dc72006-02-10 22:24:32 +00001342#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001343 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001344#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001345
Evan Cheng64a752f2006-08-11 09:08:15 +00001346 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001347 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001348
Dan Gohman525178c2007-10-08 18:33:35 +00001349 case ISD::SDIVREM:
1350 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001351 SDValue N0 = Node->getOperand(0);
1352 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001353
1354 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001355 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001356 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001357 default: assert(0 && "Unsupported VT!");
1358 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1359 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1360 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001361 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001362 }
1363 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001364 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001365 default: assert(0 && "Unsupported VT!");
1366 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1367 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1368 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001369 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001370 }
1371
1372 unsigned LoReg, HiReg;
1373 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001374 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001375 default: assert(0 && "Unsupported VT!");
1376 case MVT::i8:
1377 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001378 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001379 SExtOpcode = X86::CBW;
1380 break;
1381 case MVT::i16:
1382 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001383 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001384 SExtOpcode = X86::CWD;
1385 break;
1386 case MVT::i32:
1387 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001388 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001389 SExtOpcode = X86::CDQ;
1390 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001391 case MVT::i64:
1392 LoReg = X86::RAX; HiReg = X86::RDX;
1393 ClrOpcode = X86::MOV64r0;
1394 SExtOpcode = X86::CQO;
1395 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001396 }
1397
Dan Gohman475871a2008-07-27 21:46:04 +00001398 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001399 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001400 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001401
Dan Gohman475871a2008-07-27 21:46:04 +00001402 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001403 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001404 // Special case for div8, just use a move with zero extension to AX to
1405 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001407 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001408 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001409 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
Evan Chengb1409ce2006-11-17 22:10:14 +00001411 Ops, 5), 0);
1412 Chain = Move.getValue(1);
1413 ReplaceUses(N0.getValue(1), Chain);
1414 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001415 Move =
Dan Gohman475871a2008-07-27 21:46:04 +00001416 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001417 Chain = CurDAG->getEntryNode();
1418 }
Dan Gohman475871a2008-07-27 21:46:04 +00001419 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001420 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001421 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001422 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001423 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Dan Gohman475871a2008-07-27 21:46:04 +00001424 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001425 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001426 // Sign extend the low part into the high part.
1427 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001428 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001429 } else {
1430 // Zero out the high part, effectively zero extending the input.
Dan Gohman475871a2008-07-27 21:46:04 +00001431 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001432 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1433 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001434 }
Evan Cheng948f3432006-01-06 23:19:29 +00001435 }
1436
1437 if (foldedLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001439 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001440 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Dan Gohman475871a2008-07-27 21:46:04 +00001441 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001442 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001443 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001444 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001445 InFlag =
Dan Gohman475871a2008-07-27 21:46:04 +00001446 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001447 }
1448
Dan Gohmana37c9f72007-09-25 18:23:27 +00001449 // Copy the division (low) result, if it is needed.
1450 if (!N.getValue(0).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohman525178c2007-10-08 18:33:35 +00001452 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001453 InFlag = Result.getValue(2);
1454 ReplaceUses(N.getValue(0), Result);
1455#ifndef NDEBUG
1456 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001457 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001458 DOUT << "\n";
1459#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001460 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001461 // Copy the remainder (high) result, if it is needed.
1462 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001463 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001464 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1465 // Prevent use of AH in a REX instruction by referencing AX instead.
1466 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001467 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1468 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001469 InFlag = Result.getValue(2);
Dan Gohman475871a2008-07-27 21:46:04 +00001470 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001471 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001472 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001473 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1474 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001475 MVT::i8, Result, SRIdx), 0);
1476 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001477 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1478 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001479 InFlag = Result.getValue(2);
1480 }
1481 ReplaceUses(N.getValue(1), Result);
1482#ifndef NDEBUG
1483 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001484 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001485 DOUT << "\n";
1486#endif
1487 }
Evan Chengf597dc72006-02-10 22:24:32 +00001488
1489#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001490 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001491#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001492
1493 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001494 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001495
Christopher Lambc59e5212007-08-10 21:48:46 +00001496 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001497 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001498 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1499 SDValue N0 = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001500
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001501 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1502 unsigned Opc = 0;
1503 switch (NVT.getSimpleVT()) {
1504 default: assert(0 && "Unknown sign_extend_inreg!");
1505 case MVT::i16:
1506 Opc = X86::MOVSX16rr8;
1507 break;
1508 case MVT::i32:
1509 Opc = X86::MOVSX32rr8;
1510 break;
1511 }
1512
1513 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001514
1515#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001516 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001517 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001518 DOUT << "\n";
1519 DOUT << std::string(Indent-2, ' ') << "=> ";
1520 DEBUG(ResNode->dump(CurDAG));
1521 DOUT << "\n";
1522 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001523#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001524 return ResNode;
1525 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001526 break;
1527 }
1528
1529 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001530 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1531 SDValue Input = Node->getOperand(0);
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001532 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001533
Evan Cheng403be7e2006-05-08 08:01:26 +00001534#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001535 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001536 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001537 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001538 Indent -= 2;
1539#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001540 return ResNode;
1541 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001542 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001543 }
Evan Cheng851bc042008-06-17 02:01:22 +00001544
1545 case ISD::DECLARE: {
1546 // Handle DECLARE nodes here because the second operand may have been
1547 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001548 SDValue Chain = Node->getOperand(0);
1549 SDValue N1 = Node->getOperand(1);
1550 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001551 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
1552 if (!FINode)
Evan Chengfab83872008-06-18 02:48:27 +00001553 break;
Evan Chengfab83872008-06-18 02:48:27 +00001554 if (N2.getOpcode() == ISD::ADD &&
1555 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1556 N2 = N2.getOperand(1);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001557 if (N2.getOpcode() != X86ISD::Wrapper)
1558 break;
Evan Chengf2accb52009-01-10 03:33:22 +00001559 GlobalAddressSDNode *GVNode =
1560 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001561 if (!GVNode)
1562 break;
1563 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1564 TLI.getPointerTy());
1565 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1566 TLI.getPointerTy());
1567 SDValue Ops[] = { Tmp1, Tmp2, Chain };
1568 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1569 MVT::Other, Ops, 3);
Evan Cheng851bc042008-06-17 02:01:22 +00001570 break;
1571 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001572 }
1573
Evan Cheng9ade2182006-08-26 05:34:46 +00001574 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001575
Evan Chengf597dc72006-02-10 22:24:32 +00001576#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001577 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001578 if (ResNode == NULL || ResNode == N.getNode())
1579 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001580 else
1581 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001582 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001583 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001584#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001585
1586 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001587}
1588
Chris Lattnerc0bad572006-06-08 18:03:49 +00001589bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001590SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001591 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001592 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001593 switch (ConstraintCode) {
1594 case 'o': // offsetable ??
1595 case 'v': // not offsetable ??
1596 default: return true;
1597 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001598 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001599 return true;
1600 break;
1601 }
1602
Evan Cheng04699902006-08-26 01:05:16 +00001603 OutOps.push_back(Op0);
1604 OutOps.push_back(Op1);
1605 OutOps.push_back(Op2);
1606 OutOps.push_back(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001607 return false;
1608}
1609
Chris Lattnerc961eea2005-11-16 01:54:32 +00001610/// createX86ISelDag - This pass converts a legalized DAG into a
1611/// X86-specific DAG, ready for instruction scheduling.
1612///
Evan Chenge50794a2006-08-29 18:28:33 +00001613FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1614 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001615}