blob: 4bc604b6e46ae4b2d02023c956158671a2fcbe3b [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
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000071 int32_t Disp;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000072 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000074 const char *ES;
75 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000076 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000077
78 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000079 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
80 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000082
83 bool hasSymbolicDisplacement() const {
84 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
85 }
86
Dale Johannesen50dd1d02008-08-11 23:46:25 +000087 void dump() {
88 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000089 cerr << "Base.Reg ";
90 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
91 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000092 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
93 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000094 cerr << "IndexReg ";
95 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
96 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000097 cerr << " Disp " << Disp << "\n";
98 cerr << "GV "; if (GV) GV->dump();
99 else cerr << "nul";
100 cerr << " CP "; if (CP) CP->dump();
101 else cerr << "nul";
102 cerr << "\n";
103 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
104 cerr << " JT" << JT << " Align" << Align << "\n";
105 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000106 };
107}
108
109namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000110 //===--------------------------------------------------------------------===//
111 /// ISel - X86 specific code to select X86 machine instructions for
112 /// SelectionDAG operations.
113 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000114 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 /// TM - Keep a reference to X86TargetMachine.
116 ///
117 X86TargetMachine &TM;
118
Chris Lattnerc961eea2005-11-16 01:54:32 +0000119 /// X86Lowering - This object fully describes how to lower LLVM code to an
120 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000121 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000122
123 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
124 /// make the right decision when generating code for different targets.
125 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000126
Evan Chengdb8d56b2008-06-30 20:45:06 +0000127 /// CurBB - Current BB being isel'd.
128 ///
129 MachineBasicBlock *CurBB;
130
Evan Chengb7a75a52008-09-26 23:41:32 +0000131 /// OptForSize - If true, selector should try to optimize for code size
132 /// instead of performance.
133 bool OptForSize;
134
Chris Lattnerc961eea2005-11-16 01:54:32 +0000135 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000136 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Dan Gohman79ce2762009-01-15 19:20:50 +0000137 : SelectionDAGISel(tm, fast),
Dan Gohman38217fe2008-10-03 16:17:33 +0000138 TM(tm), X86Lowering(*TM.getTargetLowering()),
Evan Chengb7a75a52008-09-26 23:41:32 +0000139 Subtarget(&TM.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000140 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000141
142 virtual const char *getPassName() const {
143 return "X86 DAG->DAG Instruction Selection";
144 }
145
Evan Chengdb8d56b2008-06-30 20:45:06 +0000146 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000147 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000148 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000149
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000150 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
151
Evan Cheng884c70c2008-11-27 00:49:46 +0000152 virtual
153 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000154
Chris Lattnerc961eea2005-11-16 01:54:32 +0000155// Include the pieces autogenerated from the target description.
156#include "X86GenDAGISel.inc"
157
158 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000160 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000161
Dan Gohman475871a2008-07-27 21:46:04 +0000162 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000163 bool isRoot = true, unsigned Depth = 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000164 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000165 bool isRoot, unsigned Depth);
Dan Gohman475871a2008-07-27 21:46:04 +0000166 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
167 SDValue &Scale, SDValue &Index, SDValue &Disp);
168 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
169 SDValue &Scale, SDValue &Index, SDValue &Disp);
170 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
171 SDValue N, SDValue &Base, SDValue &Scale,
172 SDValue &Index, SDValue &Disp,
173 SDValue &InChain, SDValue &OutChain);
174 bool TryFoldLoad(SDValue P, SDValue N,
175 SDValue &Base, SDValue &Scale,
176 SDValue &Index, SDValue &Disp);
Dan Gohmanf350b272008-08-23 02:25:05 +0000177 void PreprocessForRMW();
178 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000179
Chris Lattnerc0bad572006-06-08 18:03:49 +0000180 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
181 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000182 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000183 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000184 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000185
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000186 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
187
Dan Gohman475871a2008-07-27 21:46:04 +0000188 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
189 SDValue &Scale, SDValue &Index,
190 SDValue &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000191 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
193 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000194 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000195 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000196 // These are 32-bit even in 64-bit mode since RIP relative offset
197 // is 32-bit.
198 if (AM.GV)
199 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
200 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000201 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
202 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000204 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 else if (AM.JT != -1)
206 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
207 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000208 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000209 }
210
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000211 /// getI8Imm - Return a target constant with the specified value, of type
212 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000213 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000214 return CurDAG->getTargetConstant(Imm, MVT::i8);
215 }
216
Chris Lattnerc961eea2005-11-16 01:54:32 +0000217 /// getI16Imm - Return a target constant with the specified value, of type
218 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000219 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000220 return CurDAG->getTargetConstant(Imm, MVT::i16);
221 }
222
223 /// getI32Imm - Return a target constant with the specified value, of type
224 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000226 return CurDAG->getTargetConstant(Imm, MVT::i32);
227 }
Evan Chengf597dc72006-02-10 22:24:32 +0000228
Dan Gohman8b746962008-09-23 18:22:58 +0000229 /// getGlobalBaseReg - Return an SDNode that returns the value of
230 /// the global base register. Output instructions required to
231 /// initialize the global base register, if necessary.
232 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000233 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000234
Dan Gohman0bfa1bf2008-08-20 21:27:32 +0000235 /// getTruncateTo8Bit - return an SDNode that implements a subreg based
236 /// truncate of the specified operand to i8. This can be done with tablegen,
237 /// except that this code uses MVT::Flag in a tricky way that happens to
238 /// improve scheduling in some cases.
239 SDNode *getTruncateTo8Bit(SDValue N0);
Christopher Lambc59e5212007-08-10 21:48:46 +0000240
Evan Cheng23addc02006-02-10 22:46:26 +0000241#ifndef NDEBUG
242 unsigned Indent;
243#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000244 };
245}
246
Gabor Greif93c53e52008-08-31 15:37:04 +0000247/// findFlagUse - Return use of MVT::Flag value produced by the specified
248/// SDNode.
Evan Chengcdda25d2008-04-25 08:22:20 +0000249///
Evan Chenga275ecb2006-10-10 01:46:56 +0000250static SDNode *findFlagUse(SDNode *N) {
251 unsigned FlagResNo = N->getNumValues()-1;
252 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohmane8ecf482009-01-27 02:37:43 +0000253 SDUse &Use = I.getUse();
254 if (Use.getResNo() == FlagResNo)
255 return Use.getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000256 }
257 return NULL;
258}
259
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000260/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
261/// This function recursively traverses up the operand chain, ignoring
262/// certain nodes.
263static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
264 SDNode *Root,
Evan Chengcdda25d2008-04-25 08:22:20 +0000265 SmallPtrSet<SDNode*, 16> &Visited) {
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000266 if (Use->getNodeId() < Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000267 !Visited.insert(Use))
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000268 return false;
269
270 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000271 SDNode *N = Use->getOperand(i).getNode();
Evan Cheng27e1fe92006-10-14 08:33:25 +0000272 if (N == Def) {
Dan Gohman682d5a82008-09-17 01:39:10 +0000273 if (Use == ImmedUse || Use == Root)
Evan Cheng419ace92008-04-25 08:55:28 +0000274 continue; // We are not looking for immediate use.
Dan Gohman682d5a82008-09-17 01:39:10 +0000275 assert(N != Root);
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000276 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000277 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000278
279 // Traverse up the operand chain.
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000280 if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
281 return true;
Evan Chengf4b4c412006-08-08 00:31:00 +0000282 }
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000283 return false;
Evan Chengf4b4c412006-08-08 00:31:00 +0000284}
285
Evan Cheng27e1fe92006-10-14 08:33:25 +0000286/// isNonImmUse - Start searching from Root up the DAG to check is Def can
287/// be reached. Return true if that's the case. However, ignore direct uses
288/// by ImmedUse (which would be U in the example illustrated in
Evan Cheng884c70c2008-11-27 00:49:46 +0000289/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
290/// case).
Evan Cheng27e1fe92006-10-14 08:33:25 +0000291/// FIXME: to be really generic, we should allow direct use by any node
292/// that is being folded. But realisticly since we only fold loads which
293/// have one non-chain use, we only need to watch out for load/op/store
294/// and load/op/cmp case where the root (store / cmp) may reach the load via
295/// its chain operand.
Dan Gohman682d5a82008-09-17 01:39:10 +0000296static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000297 SmallPtrSet<SDNode*, 16> Visited;
Dan Gohmanc03e9a12009-01-27 19:04:30 +0000298 return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000299}
300
301
Evan Cheng884c70c2008-11-27 00:49:46 +0000302bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
303 SDNode *Root) const {
Dan Gohmanea9587b2008-08-13 19:55:00 +0000304 if (Fast) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000305
Evan Cheng884c70c2008-11-27 00:49:46 +0000306 if (U == Root)
307 switch (U->getOpcode()) {
308 default: break;
309 case ISD::ADD:
310 case ISD::ADDC:
311 case ISD::ADDE:
312 case ISD::AND:
313 case ISD::OR:
314 case ISD::XOR: {
315 // If the other operand is a 8-bit immediate we should fold the immediate
316 // instead. This reduces code size.
317 // e.g.
318 // movl 4(%esp), %eax
319 // addl $4, %eax
320 // vs.
321 // movl $4, %eax
322 // addl 4(%esp), %eax
323 // The former is 2 bytes shorter. In case where the increment is 1, then
324 // the saving can be 4 bytes (by using incl %eax).
Dan Gohman9a49d312009-03-14 02:07:16 +0000325 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1)))
326 if (Imm->getAPIntValue().isSignedIntN(8))
327 return false;
Evan Cheng884c70c2008-11-27 00:49:46 +0000328 }
329 }
330
Dan Gohman682d5a82008-09-17 01:39:10 +0000331 // If Root use can somehow reach N through a path that that doesn't contain
332 // U then folding N would create a cycle. e.g. In the following
333 // diagram, Root can reach N through X. If N is folded into into Root, then
334 // X is both a predecessor and a successor of U.
Evan Chenga8df1b42006-07-27 16:44:36 +0000335 //
Dan Gohman682d5a82008-09-17 01:39:10 +0000336 // [N*] //
337 // ^ ^ //
338 // / \ //
339 // [U*] [X]? //
340 // ^ ^ //
341 // \ / //
342 // \ / //
343 // [Root*] //
344 //
345 // * indicates nodes to be folded together.
346 //
347 // If Root produces a flag, then it gets (even more) interesting. Since it
348 // will be "glued" together with its flag use in the scheduler, we need to
349 // check if it might reach N.
350 //
351 // [N*] //
352 // ^ ^ //
353 // / \ //
354 // [U*] [X]? //
355 // ^ ^ //
356 // \ \ //
357 // \ | //
358 // [Root*] | //
359 // ^ | //
360 // f | //
361 // | / //
362 // [Y] / //
363 // ^ / //
364 // f / //
365 // | / //
366 // [FU] //
367 //
368 // If FU (flag use) indirectly reaches N (the load), and Root folds N
369 // (call it Fold), then X is a predecessor of FU and a successor of
370 // Fold. But since Fold and FU are flagged together, this will create
371 // a cycle in the scheduling graph.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000372
Duncan Sands83ec4b62008-06-06 12:08:01 +0000373 MVT VT = Root->getValueType(Root->getNumValues()-1);
Dan Gohman682d5a82008-09-17 01:39:10 +0000374 while (VT == MVT::Flag) {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000375 SDNode *FU = findFlagUse(Root);
376 if (FU == NULL)
377 break;
Dan Gohman682d5a82008-09-17 01:39:10 +0000378 Root = FU;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000379 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000380 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000381
Dan Gohman682d5a82008-09-17 01:39:10 +0000382 return !isNonImmUse(Root, N, U);
Evan Chenga8df1b42006-07-27 16:44:36 +0000383}
384
Evan Cheng70e674e2006-08-28 20:10:17 +0000385/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
386/// and move load below the TokenFactor. Replace store's chain operand with
387/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000388static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000389 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000390 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000391 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
392 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000393 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000394 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000395 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000396 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
397 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
398 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
399 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000400}
401
Evan Chengcd0baf22008-05-23 21:23:16 +0000402/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
403///
Dan Gohman475871a2008-07-27 21:46:04 +0000404static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
405 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000406 if (N.getOpcode() == ISD::BIT_CONVERT)
407 N = N.getOperand(0);
408
409 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
410 if (!LD || LD->isVolatile())
411 return false;
412 if (LD->getAddressingMode() != ISD::UNINDEXED)
413 return false;
414
415 ISD::LoadExtType ExtType = LD->getExtensionType();
416 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
417 return false;
418
419 if (N.hasOneUse() &&
420 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000421 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000422 Load = N;
423 return true;
424 }
425 return false;
426}
427
Evan Chengab6c3bb2008-08-25 21:27:18 +0000428/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
429/// operand and move load below the call's chain operand.
430static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000431 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000432 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000433 SDValue Chain = CallSeqStart.getOperand(0);
434 if (Chain.getNode() == Load.getNode())
435 Ops.push_back(Load.getOperand(0));
436 else {
437 assert(Chain.getOpcode() == ISD::TokenFactor &&
438 "Unexpected CallSeqStart chain operand");
439 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
440 if (Chain.getOperand(i).getNode() == Load.getNode())
441 Ops.push_back(Load.getOperand(0));
442 else
443 Ops.push_back(Chain.getOperand(i));
444 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000445 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
446 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000447 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);
Dale Johannesend8392542009-02-03 21:48:12 +0000663 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000664
665 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000666 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000667 N->getOperand(0),
668 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000669 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000670 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000671
672 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
673 // extload we created. This will cause general havok on the dag because
674 // anything below the conversion could be folded into other existing nodes.
675 // To avoid invalidating 'I', back it up to the convert node.
676 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000677 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000678
679 // Now that we did that, the node is dead. Increment the iterator to the
680 // next node to process, then delete N.
681 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000682 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000683 }
684}
685
Chris Lattnerc961eea2005-11-16 01:54:32 +0000686/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
687/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000688void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000689 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000690 const Function *F = CurDAG->getMachineFunction().getFunction();
691 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000692
Evan Chengdb8d56b2008-06-30 20:45:06 +0000693 DEBUG(BB->dump());
Dan Gohmanea9587b2008-08-13 19:55:00 +0000694 if (!Fast)
Dan Gohmanf350b272008-08-23 02:25:05 +0000695 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000696
697 // FIXME: This should only happen when not -fast.
Dan Gohmanf350b272008-08-23 02:25:05 +0000698 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000699
Chris Lattnerc961eea2005-11-16 01:54:32 +0000700 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000701#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000702 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000703 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000704#endif
David Greene8ad4c002008-10-27 21:56:29 +0000705 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000706#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000707 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000708#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000709
Dan Gohmanf350b272008-08-23 02:25:05 +0000710 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000711}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000712
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000713/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
714/// the main function.
715void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
716 MachineFrameInfo *MFI) {
717 const TargetInstrInfo *TII = TM.getInstrInfo();
718 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000719 BuildMI(BB, DebugLoc::getUnknownLoc(),
720 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000721}
722
723void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
724 // If this is main, emit special code for main.
725 MachineBasicBlock *BB = MF.begin();
726 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
727 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
728}
729
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000730/// MatchAddress - Add the specified node to the specified addressing mode,
731/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000732/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000733bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000734 bool isRoot, unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000735 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000736 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000737 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000738 // Limit recursion.
739 if (Depth > 5)
740 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000741
Evan Cheng25ab6902006-09-08 06:48:29 +0000742 // RIP relative addressing: %rip + 32-bit displacement!
743 if (AM.isRIPRel) {
744 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000745 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000746 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000747 AM.Disp += Val;
748 return false;
749 }
750 }
751 return true;
752 }
753
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000754 switch (N.getOpcode()) {
755 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000756 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000757 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000758 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000759 AM.Disp += Val;
760 return false;
761 }
762 break;
763 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000764
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000765 case X86ISD::Wrapper: {
Dan Gohman6520e202008-10-18 02:06:02 +0000766 DOUT << "Wrapper: 64bit " << is64Bit;
767 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Evan Cheng0085a282006-11-30 21:55:46 +0000768 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000769 // Also, base and index reg must be 0 in order to use rip as base.
770 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000771 AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng0085a282006-11-30 21:55:46 +0000772 break;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000773 if (AM.hasSymbolicDisplacement())
Evan Cheng28b514392006-12-05 19:50:18 +0000774 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000775 // If value is available in a register both base and index components have
776 // been picked, we can't fit the result available in the register in the
777 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000778 {
Dan Gohman475871a2008-07-27 21:46:04 +0000779 SDValue N0 = N.getOperand(0);
Evan Cheng28b514392006-12-05 19:50:18 +0000780 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000781 uint64_t Offset = G->getOffset();
782 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000783 GlobalValue *GV = G->getGlobal();
784 AM.GV = GV;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000785 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000786 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
787 return false;
788 }
Evan Cheng28b514392006-12-05 19:50:18 +0000789 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000790 uint64_t Offset = CP->getOffset();
791 if (!is64Bit || isInt32(AM.Disp + Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +0000792 AM.CP = CP->getConstVal();
793 AM.Align = CP->getAlignment();
Dan Gohman27cae7b2008-11-11 15:52:29 +0000794 AM.Disp += Offset;
Dan Gohman6520e202008-10-18 02:06:02 +0000795 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
796 return false;
797 }
Bill Wendling056292f2008-09-16 21:48:12 +0000798 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000799 AM.ES = S->getSymbol();
Dan Gohman97135e12008-09-26 19:15:30 +0000800 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000801 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000802 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000803 AM.JT = J->getIndex();
Dan Gohman97135e12008-09-26 19:15:30 +0000804 AM.isRIPRel = TM.symbolicAddressesAreRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000805 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000806 }
807 }
808 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000809 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000810
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000811 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000812 if (AM.BaseType == X86ISelAddressMode::RegBase
813 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000814 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
815 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
816 return false;
817 }
818 break;
Evan Chengec693f72005-12-08 02:01:35 +0000819
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000820 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000821 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000822 break;
823
Gabor Greif93c53e52008-08-31 15:37:04 +0000824 if (ConstantSDNode
825 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000826 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000827 if (Val == 1 || Val == 2 || Val == 3) {
828 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000829 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000830
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000831 // Okay, we know that we have a scale by now. However, if the scaled
832 // value is an add of something and a constant, we can fold the
833 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000834 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
835 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
836 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000837 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000838 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000839 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000840 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000841 AM.Disp = Disp;
842 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000843 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000844 } else {
845 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000846 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000847 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000848 }
849 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000850 }
Evan Chengec693f72005-12-08 02:01:35 +0000851
Dan Gohman83688052007-10-22 20:22:24 +0000852 case ISD::SMUL_LOHI:
853 case ISD::UMUL_LOHI:
854 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000855 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000856 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000857 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000858 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000859 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000860 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000861 AM.Base.Reg.getNode() == 0 &&
862 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000863 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000864 if (ConstantSDNode
865 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000866 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
867 CN->getZExtValue() == 9) {
868 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869
Gabor Greifba36cb52008-08-28 21:40:38 +0000870 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000871 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000872
873 // Okay, we know that we have a scale by now. However, if the scaled
874 // value is an add of something and a constant, we can fold the
875 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000876 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
877 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
878 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000879 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000880 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000881 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000882 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000883 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000884 AM.Disp = Disp;
885 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000886 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000887 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000888 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000889 }
890
891 AM.IndexReg = AM.Base.Reg = Reg;
892 return false;
893 }
Chris Lattner62412262007-02-04 20:18:17 +0000894 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000895 break;
896
Evan Cheng8e278262009-01-17 07:09:27 +0000897 case ISD::ADD: {
898 X86ISelAddressMode Backup = AM;
899 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) &&
900 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1))
901 return false;
902 AM = Backup;
903 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) &&
904 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1))
905 return false;
906 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000907
908 // If we couldn't fold both operands into the address at the same time,
909 // see if we can just put each operand into a register and fold at least
910 // the add.
911 if (AM.BaseType == X86ISelAddressMode::RegBase &&
912 !AM.Base.Reg.getNode() &&
913 !AM.IndexReg.getNode() &&
914 !AM.isRIPRel) {
915 AM.Base.Reg = N.getNode()->getOperand(0);
916 AM.IndexReg = N.getNode()->getOperand(1);
917 AM.Scale = 1;
918 return false;
919 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000920 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000921 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000922
Chris Lattner62412262007-02-04 20:18:17 +0000923 case ISD::OR:
924 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000925 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
926 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000927 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000928 // Start with the LHS as an addr mode.
929 if (!MatchAddress(N.getOperand(0), AM, false) &&
930 // Address could not have picked a GV address for the displacement.
931 AM.GV == NULL &&
932 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000933 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000934 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000935 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000936 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000937 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000938 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000939 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000940 }
941 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000942
943 case ISD::AND: {
944 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
945 // allows us to fold the shift into this addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000946 SDValue Shift = N.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000947 if (Shift.getOpcode() != ISD::SHL) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000948
Evan Cheng1314b002007-12-13 00:43:27 +0000949 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000951
952 // Not when RIP is used as the base.
953 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000954
955 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
956 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
957 if (!C1 || !C2) break;
958
959 // Not likely to be profitable if either the AND or SHIFT node has more
960 // than one use (unless all uses are for address computation). Besides,
961 // isel mechanism requires their node ids to be reused.
962 if (!N.hasOneUse() || !Shift.hasOneUse())
963 break;
964
965 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000966 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +0000967 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
968 break;
969
970 // Get the new AND mask, this folds to a constant.
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000971 SDValue X = Shift.getOperand(0);
Dale Johannesend8392542009-02-03 21:48:12 +0000972 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +0000973 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +0000974 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
975 NewANDMask);
976 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +0000977 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000978
979 // Insert the new nodes into the topological ordering.
980 if (C1->getNodeId() > X.getNode()->getNodeId()) {
981 CurDAG->RepositionNode(X.getNode(), C1);
982 C1->setNodeId(X.getNode()->getNodeId());
983 }
984 if (NewANDMask.getNode()->getNodeId() == -1 ||
985 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
986 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
987 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
988 }
989 if (NewAND.getNode()->getNodeId() == -1 ||
990 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
991 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
992 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
993 }
994 if (NewSHIFT.getNode()->getNodeId() == -1 ||
995 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
996 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
997 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
998 }
999
Dan Gohman7b8e9642008-10-13 20:52:04 +00001000 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001001
1002 AM.Scale = 1 << ShiftCst;
1003 AM.IndexReg = NewAND;
1004 return false;
1005 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001006 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001007
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001008 return MatchAddressBase(N, AM, isRoot, Depth);
1009}
1010
1011/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1012/// specified addressing mode without any further recursion.
Dan Gohman475871a2008-07-27 21:46:04 +00001013bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM,
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001014 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001015 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001017 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001018 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001019 AM.IndexReg = N;
1020 AM.Scale = 1;
1021 return false;
1022 }
1023
1024 // Otherwise, we cannot select it.
1025 return true;
1026 }
1027
1028 // Default, generate it as a register.
1029 AM.BaseType = X86ISelAddressMode::RegBase;
1030 AM.Base.Reg = N;
1031 return false;
1032}
1033
Evan Chengec693f72005-12-08 02:01:35 +00001034/// SelectAddr - returns true if it is able pattern match an addressing mode.
1035/// It returns the operands which make up the maximal addressing mode it can
1036/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001037bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1038 SDValue &Scale, SDValue &Index,
1039 SDValue &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +00001040 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001041 bool Done = false;
1042 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1043 unsigned Opcode = N.getOpcode();
1044 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1045 Opcode != X86ISD::Wrapper) {
1046 // If we are able to fold N into addressing mode, then we'll allow it even
1047 // if N has multiple uses. In general, addressing computation is used as
1048 // addresses by all of its uses. But watch out for CopyToReg uses, that
1049 // means the address computation is liveout. It will be computed by a LEA
1050 // so we want to avoid computing the address twice.
1051 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1052 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1053 if (UI->getOpcode() == ISD::CopyToReg) {
1054 MatchAddressBase(N, AM, true, 0);
1055 Done = true;
1056 break;
1057 }
1058 }
1059 }
1060 }
1061
1062 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001063 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001064
Duncan Sands83ec4b62008-06-06 12:08:01 +00001065 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001066 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001067 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001068 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001069 }
Evan Cheng8700e142006-01-11 06:09:51 +00001070
Gabor Greifba36cb52008-08-28 21:40:38 +00001071 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001072 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001073
1074 getAddressOperands(AM, Base, Scale, Index, Disp);
1075 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001076}
1077
Chris Lattner3a7cd952006-10-07 21:55:32 +00001078/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1079/// match a load whose top elements are either undef or zeros. The load flavor
1080/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001081bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1082 SDValue N, SDValue &Base,
1083 SDValue &Scale, SDValue &Index,
1084 SDValue &Disp, SDValue &InChain,
1085 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001086 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001087 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001088 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001089 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001090 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001091 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001092 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +00001093 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001094 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001095 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001096 return true;
1097 }
1098 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001099
1100 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001101 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001102 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001103 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001104 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001105 N.getOperand(0).getNode()->hasOneUse() &&
1106 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001107 N.getOperand(0).getOperand(0).hasOneUse()) {
1108 // Okay, this is a zero extending load. Fold it.
1109 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1110 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1111 return false;
1112 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001113 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001114 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001115 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001116 return false;
1117}
1118
1119
Evan Cheng51a9ed92006-02-25 10:09:08 +00001120/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1121/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001122bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1123 SDValue &Base, SDValue &Scale,
1124 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001125 X86ISelAddressMode AM;
1126 if (MatchAddress(N, AM))
1127 return false;
1128
Duncan Sands83ec4b62008-06-06 12:08:01 +00001129 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001130 unsigned Complexity = 0;
1131 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001132 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001133 Complexity = 1;
1134 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001135 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001136 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1137 Complexity = 4;
1138
Gabor Greifba36cb52008-08-28 21:40:38 +00001139 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001140 Complexity++;
1141 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001142 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001143
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001144 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1145 // a simple shift.
1146 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001147 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001148
1149 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1150 // to a LEA. This is determined with some expermentation but is by no means
1151 // optimal (especially for code size consideration). LEA is nice because of
1152 // its three-address nature. Tweak the cost function again when we can run
1153 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001154 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001155 // For X86-64, we should always use lea to materialize RIP relative
1156 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001157 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001158 Complexity = 4;
1159 else
1160 Complexity += 2;
1161 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001162
Gabor Greifba36cb52008-08-28 21:40:38 +00001163 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001164 Complexity++;
1165
1166 if (Complexity > 2) {
1167 getAddressOperands(AM, Base, Scale, Index, Disp);
1168 return true;
1169 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001170 return false;
1171}
1172
Dan Gohman475871a2008-07-27 21:46:04 +00001173bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1174 SDValue &Base, SDValue &Scale,
1175 SDValue &Index, SDValue &Disp) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001176 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001177 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001178 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Evan Cheng0d538262006-11-08 20:34:28 +00001179 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001180 return false;
1181}
1182
Dan Gohman8b746962008-09-23 18:22:58 +00001183/// getGlobalBaseReg - Return an SDNode that returns the value of
1184/// the global base register. Output instructions required to
1185/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001186///
Evan Cheng9ade2182006-08-26 05:34:46 +00001187SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001188 MachineFunction *MF = CurBB->getParent();
1189 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001190 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001191}
1192
Evan Chengb245d922006-05-20 01:36:52 +00001193static SDNode *FindCallStartFromCall(SDNode *Node) {
1194 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1195 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1196 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001197 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001198}
1199
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001200/// getTruncateTo8Bit - return an SDNode that implements a subreg based
1201/// truncate of the specified operand to i8. This can be done with tablegen,
1202/// except that this code uses MVT::Flag in a tricky way that happens to
1203/// improve scheduling in some cases.
1204SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) {
1205 assert(!Subtarget->is64Bit() &&
1206 "getTruncateTo8Bit is only needed on x86-32!");
1207 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001208 DebugLoc dl = N0.getDebugLoc();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001209
1210 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1211 unsigned Opc;
1212 MVT N0VT = N0.getValueType();
1213 switch (N0VT.getSimpleVT()) {
1214 default: assert(0 && "Unknown truncate!");
1215 case MVT::i16:
1216 Opc = X86::MOV16to16_;
1217 break;
1218 case MVT::i32:
1219 Opc = X86::MOV32to32_;
1220 break;
1221 }
1222
1223 // The use of MVT::Flag here is not strictly accurate, but it helps
1224 // scheduling in some cases.
Dale Johannesend8392542009-02-03 21:48:12 +00001225 N0 = SDValue(CurDAG->getTargetNode(Opc, dl, N0VT, MVT::Flag, N0), 0);
1226 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001227 MVT::i8, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001228}
1229
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001230SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1231 SDValue Chain = Node->getOperand(0);
1232 SDValue In1 = Node->getOperand(1);
1233 SDValue In2L = Node->getOperand(2);
1234 SDValue In2H = Node->getOperand(3);
1235 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
1236 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3))
1237 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001238 SDValue LSI = Node->getOperand(4); // MemOperand
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001239 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain };
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001240 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1241 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001242 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001243}
Christopher Lambc59e5212007-08-10 21:48:46 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001246 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001248 unsigned Opc, MOpc;
1249 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001250 DebugLoc dl = Node->getDebugLoc();
1251
Evan Chengf597dc72006-02-10 22:24:32 +00001252#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001253 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001254 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001255 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001256 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001257#endif
1258
Dan Gohmane8be6c62008-07-17 19:10:17 +00001259 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001260#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001261 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001262 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001263 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001264 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001265#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001266 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001267 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001268
Evan Cheng0114e942006-01-06 20:36:21 +00001269 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001270 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001271 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001272 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001273
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001274 case X86ISD::ATOMOR64_DAG:
1275 return SelectAtomic64(Node, X86::ATOMOR6432);
1276 case X86ISD::ATOMXOR64_DAG:
1277 return SelectAtomic64(Node, X86::ATOMXOR6432);
1278 case X86ISD::ATOMADD64_DAG:
1279 return SelectAtomic64(Node, X86::ATOMADD6432);
1280 case X86ISD::ATOMSUB64_DAG:
1281 return SelectAtomic64(Node, X86::ATOMSUB6432);
1282 case X86ISD::ATOMNAND64_DAG:
1283 return SelectAtomic64(Node, X86::ATOMNAND6432);
1284 case X86ISD::ATOMAND64_DAG:
1285 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001286 case X86ISD::ATOMSWAP64_DAG:
1287 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001288
Dan Gohman525178c2007-10-08 18:33:35 +00001289 case ISD::SMUL_LOHI:
1290 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001291 SDValue N0 = Node->getOperand(0);
1292 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001293
Dan Gohman525178c2007-10-08 18:33:35 +00001294 bool isSigned = Opcode == ISD::SMUL_LOHI;
1295 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001296 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001297 default: assert(0 && "Unsupported VT!");
1298 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1299 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1300 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001301 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001302 }
1303 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001305 default: assert(0 && "Unsupported VT!");
1306 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1307 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1308 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001309 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001310 }
1311
1312 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001313 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001314 default: assert(0 && "Unsupported VT!");
1315 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1316 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1317 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001318 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001319 }
1320
Dan Gohman475871a2008-07-27 21:46:04 +00001321 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001322 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001323 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001324 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001325 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001326 if (foldedLoad)
1327 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001328 }
1329
Dale Johannesendd64c412009-02-04 00:33:20 +00001330 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001331 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001332
1333 if (foldedLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001335 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001336 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001337 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001338 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001339 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001340 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001341 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001342 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001343 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001344 }
1345
Dan Gohman525178c2007-10-08 18:33:35 +00001346 // Copy the low half of the result, if it is needed.
1347 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001348 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001349 LoReg, NVT, InFlag);
1350 InFlag = Result.getValue(2);
1351 ReplaceUses(N.getValue(0), Result);
1352#ifndef NDEBUG
1353 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001354 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001355 DOUT << "\n";
1356#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001357 }
Dan Gohman525178c2007-10-08 18:33:35 +00001358 // Copy the high half of the result, if it is needed.
1359 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001361 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1362 // Prevent use of AH in a REX instruction by referencing AX instead.
1363 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001364 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001365 X86::AX, MVT::i16, InFlag);
1366 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001367 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1368 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001369 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001370 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesend8392542009-02-03 21:48:12 +00001372 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001373 MVT::i8, Result, SRIdx), 0);
1374 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001375 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001376 HiReg, NVT, InFlag);
1377 InFlag = Result.getValue(2);
1378 }
1379 ReplaceUses(N.getValue(1), Result);
1380#ifndef NDEBUG
1381 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001382 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001383 DOUT << "\n";
1384#endif
1385 }
Evan Cheng34167212006-02-09 00:37:58 +00001386
Evan Chengf597dc72006-02-10 22:24:32 +00001387#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001388 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001389#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001390
Evan Cheng64a752f2006-08-11 09:08:15 +00001391 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001392 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001393
Dan Gohman525178c2007-10-08 18:33:35 +00001394 case ISD::SDIVREM:
1395 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SDValue N0 = Node->getOperand(0);
1397 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001398
1399 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001400 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001401 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001402 default: assert(0 && "Unsupported VT!");
1403 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1404 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1405 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001406 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001407 }
1408 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001409 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001410 default: assert(0 && "Unsupported VT!");
1411 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1412 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1413 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001414 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001415 }
1416
1417 unsigned LoReg, HiReg;
1418 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001419 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001420 default: assert(0 && "Unsupported VT!");
1421 case MVT::i8:
1422 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001423 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001424 SExtOpcode = X86::CBW;
1425 break;
1426 case MVT::i16:
1427 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001428 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001429 SExtOpcode = X86::CWD;
1430 break;
1431 case MVT::i32:
1432 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001433 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001434 SExtOpcode = X86::CDQ;
1435 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001436 case MVT::i64:
1437 LoReg = X86::RAX; HiReg = X86::RDX;
1438 ClrOpcode = X86::MOV64r0;
1439 SExtOpcode = X86::CQO;
1440 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001441 }
1442
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Tmp0, Tmp1, Tmp2, Tmp3;
Dan Gohman525178c2007-10-08 18:33:35 +00001444 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001445 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001446
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001448 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001449 // Special case for div8, just use a move with zero extension to AX to
1450 // clear the upper 8 bits (AH).
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
Evan Chengb1409ce2006-11-17 22:10:14 +00001452 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001453 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001454 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001455 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001456 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001457 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001458 Chain = Move.getValue(1);
1459 ReplaceUses(N0.getValue(1), Chain);
1460 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001461 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001462 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001463 Chain = CurDAG->getEntryNode();
1464 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001465 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001466 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001467 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001468 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001469 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001470 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001471 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001472 // Sign extend the low part into the high part.
1473 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001474 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001475 } else {
1476 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001477 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1478 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001479 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001480 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001481 }
Evan Cheng948f3432006-01-06 23:19:29 +00001482 }
1483
1484 if (foldedLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00001485 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001486 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001487 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001488 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001489 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001490 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001491 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001492 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001493 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001494 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001495 }
1496
Dan Gohmana37c9f72007-09-25 18:23:27 +00001497 // Copy the division (low) result, if it is needed.
1498 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001499 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001500 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001501 InFlag = Result.getValue(2);
1502 ReplaceUses(N.getValue(0), Result);
1503#ifndef NDEBUG
1504 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001505 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001506 DOUT << "\n";
1507#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001508 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001509 // Copy the remainder (high) result, if it is needed.
1510 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001511 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001512 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1513 // Prevent use of AH in a REX instruction by referencing AX instead.
1514 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001516 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001517 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001518 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1519 Result,
1520 CurDAG->getTargetConstant(8, MVT::i8)),
1521 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001522 // Then truncate it down to i8.
Dan Gohman475871a2008-07-27 21:46:04 +00001523 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
Dale Johannesend8392542009-02-03 21:48:12 +00001524 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001525 MVT::i8, Result, SRIdx), 0);
1526 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001527 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001528 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001529 InFlag = Result.getValue(2);
1530 }
1531 ReplaceUses(N.getValue(1), Result);
1532#ifndef NDEBUG
1533 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001534 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001535 DOUT << "\n";
1536#endif
1537 }
Evan Chengf597dc72006-02-10 22:24:32 +00001538
1539#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001540 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001541#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001542
1543 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001544 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001545
Christopher Lambc59e5212007-08-10 21:48:46 +00001546 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001547 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001548 if (SVT == MVT::i8 && !Subtarget->is64Bit()) {
1549 SDValue N0 = Node->getOperand(0);
Christopher Lambc59e5212007-08-10 21:48:46 +00001550
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001551 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0);
1552 unsigned Opc = 0;
1553 switch (NVT.getSimpleVT()) {
1554 default: assert(0 && "Unknown sign_extend_inreg!");
1555 case MVT::i16:
1556 Opc = X86::MOVSX16rr8;
1557 break;
1558 case MVT::i32:
1559 Opc = X86::MOVSX32rr8;
1560 break;
1561 }
1562
Dale Johannesend8392542009-02-03 21:48:12 +00001563 SDNode *ResNode = CurDAG->getTargetNode(Opc, dl, NVT, TruncOp);
Christopher Lambc59e5212007-08-10 21:48:46 +00001564
1565#ifndef NDEBUG
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001566 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001567 DEBUG(TruncOp.getNode()->dump(CurDAG));
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001568 DOUT << "\n";
1569 DOUT << std::string(Indent-2, ' ') << "=> ";
1570 DEBUG(ResNode->dump(CurDAG));
1571 DOUT << "\n";
1572 Indent -= 2;
Christopher Lambc59e5212007-08-10 21:48:46 +00001573#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001574 return ResNode;
1575 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001576 break;
1577 }
1578
1579 case ISD::TRUNCATE: {
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001580 if (NVT == MVT::i8 && !Subtarget->is64Bit()) {
1581 SDValue Input = Node->getOperand(0);
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001582 SDNode *ResNode = getTruncateTo8Bit(Input);
Christopher Lambc59e5212007-08-10 21:48:46 +00001583
Evan Cheng403be7e2006-05-08 08:01:26 +00001584#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001585 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001586 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001587 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001588 Indent -= 2;
1589#endif
Dan Gohman0bfa1bf2008-08-20 21:27:32 +00001590 return ResNode;
1591 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001592 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001593 }
Evan Cheng851bc042008-06-17 02:01:22 +00001594
1595 case ISD::DECLARE: {
1596 // Handle DECLARE nodes here because the second operand may have been
1597 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001598 SDValue Chain = Node->getOperand(0);
1599 SDValue N1 = Node->getOperand(1);
1600 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001601 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001602
1603 // FIXME: We need to handle this for VLAs.
1604 if (!FINode) {
1605 ReplaceUses(N.getValue(0), Chain);
1606 return NULL;
1607 }
1608
Evan Chengfab83872008-06-18 02:48:27 +00001609 if (N2.getOpcode() == ISD::ADD &&
1610 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1611 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001612
1613 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1614 // somehow, just ignore it.
1615 if (N2.getOpcode() != X86ISD::Wrapper) {
1616 ReplaceUses(N.getValue(0), Chain);
1617 return NULL;
1618 }
Evan Chengf2accb52009-01-10 03:33:22 +00001619 GlobalAddressSDNode *GVNode =
1620 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001621 if (GVNode == 0) {
1622 ReplaceUses(N.getValue(0), Chain);
1623 return NULL;
1624 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001625 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1626 TLI.getPointerTy());
1627 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1628 TLI.getPointerTy());
1629 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001630 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001631 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001632 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001633 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001634 }
1635
Evan Cheng9ade2182006-08-26 05:34:46 +00001636 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001637
Evan Chengf597dc72006-02-10 22:24:32 +00001638#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001639 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001640 if (ResNode == NULL || ResNode == N.getNode())
1641 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001642 else
1643 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001644 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001645 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001646#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001647
1648 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001649}
1650
Chris Lattnerc0bad572006-06-08 18:03:49 +00001651bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001652SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001653 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +00001654 SDValue Op0, Op1, Op2, Op3;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001655 switch (ConstraintCode) {
1656 case 'o': // offsetable ??
1657 case 'v': // not offsetable ??
1658 default: return true;
1659 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001660 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001661 return true;
1662 break;
1663 }
1664
Evan Cheng04699902006-08-26 01:05:16 +00001665 OutOps.push_back(Op0);
1666 OutOps.push_back(Op1);
1667 OutOps.push_back(Op2);
1668 OutOps.push_back(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001669 return false;
1670}
1671
Chris Lattnerc961eea2005-11-16 01:54:32 +00001672/// createX86ISelDag - This pass converts a legalized DAG into a
1673/// X86-specific DAG, ready for instruction scheduling.
1674///
Evan Chenge50794a2006-08-29 18:28:33 +00001675FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1676 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001677}