blob: 1336177de249f07fb3d3c8c8a778f81e54235f9c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
15#define DEBUG_TYPE "x86-isel"
16#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
22#include "X86TargetMachine.h"
23#include "llvm/GlobalValue.h"
24#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
26#include "llvm/Support/CFG.h"
27#include "llvm/Type.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Cheng13559d62008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Compiler.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesenc501c082008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Cheng656269e2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
Evan Cheng8655a532009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
54 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
60 FrameIndexBase
61 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000064 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 int FrameIndex;
66 } Base;
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000070 int32_t Disp;
Rafael Espindolabca99f72009-04-08 21:14:34 +000071 SDValue Segment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 GlobalValue *GV;
73 Constant *CP;
74 const char *ES;
75 int JT;
76 unsigned Align; // CP alignment.
Chris Lattner8969a732009-06-26 05:51:45 +000077 unsigned char SymbolFlags; // X86II::MO_*
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 X86ISelAddressMode()
Chris Lattnerdc6fc472009-06-27 04:16:01 +000080 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner8969a732009-06-26 05:51:45 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 }
Dan Gohman245791b2009-02-07 00:43:41 +000083
84 bool hasSymbolicDisplacement() const {
85 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
86 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +000087
88 bool hasBaseOrIndexReg() const {
89 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
90 }
91
92 /// isRIPRelative - Return true if this addressing mode is already RIP
93 /// relative.
94 bool isRIPRelative() const {
95 if (BaseType != RegBase) return false;
96 if (RegisterSDNode *RegNode =
97 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
98 return RegNode->getReg() == X86::RIP;
99 return false;
100 }
101
102 void setBaseReg(SDValue Reg) {
103 BaseType = RegBase;
104 Base.Reg = Reg;
105 }
Dan Gohman245791b2009-02-07 00:43:41 +0000106
Dale Johannesenc501c082008-08-11 23:46:25 +0000107 void dump() {
108 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +0000109 cerr << "Base.Reg ";
110 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
111 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +0000112 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000113 cerr << " Scale" << Scale << "\n";
Gabor Greife9f7f582008-08-31 15:37:04 +0000114 cerr << "IndexReg ";
115 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
116 else cerr << "nul";
Dale Johannesenc501c082008-08-11 23:46:25 +0000117 cerr << " Disp " << Disp << "\n";
118 cerr << "GV "; if (GV) GV->dump();
119 else cerr << "nul";
120 cerr << " CP "; if (CP) CP->dump();
121 else cerr << "nul";
122 cerr << "\n";
123 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
124 cerr << " JT" << JT << " Align" << Align << "\n";
125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 };
127}
128
129namespace {
130 //===--------------------------------------------------------------------===//
131 /// ISel - X86 specific code to select X86 machine instructions for
132 /// SelectionDAG operations.
133 ///
134 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 /// X86Lowering - This object fully describes how to lower LLVM code to an
136 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000137 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
140 /// make the right decision when generating code for different targets.
141 const X86Subtarget *Subtarget;
142
Evan Cheng34fd4f32008-06-30 20:45:06 +0000143 /// CurBB - Current BB being isel'd.
144 ///
145 MachineBasicBlock *CurBB;
146
Evan Cheng13559d62008-09-26 23:41:32 +0000147 /// OptForSize - If true, selector should try to optimize for code size
148 /// instead of performance.
149 bool OptForSize;
150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 public:
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000152 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000153 : SelectionDAGISel(tm, OptLevel),
Dan Gohman4261ed32009-06-03 20:20:00 +0000154 X86Lowering(*tm.getTargetLowering()),
155 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000156 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 virtual const char *getPassName() const {
159 return "X86 DAG->DAG Instruction Selection";
160 }
161
Evan Cheng34fd4f32008-06-30 20:45:06 +0000162 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000164 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000165
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000166 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
167
Evan Cheng5a424552008-11-27 00:49:46 +0000168 virtual
169 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170
171// Include the pieces autogenerated from the target description.
172#include "X86GenDAGISel.inc"
173
174 private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000175 SDNode *Select(SDValue N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000176 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Rafael Espindolabca99f72009-04-08 21:14:34 +0000178 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
179 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola84218fe2009-04-12 21:55:03 +0000180 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman8181bd12008-07-27 21:46:04 +0000181 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola515c13e2009-03-31 16:16:57 +0000182 unsigned Depth = 0);
183 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000185 SDValue &Scale, SDValue &Index, SDValue &Disp,
186 SDValue &Segment);
Dan Gohman8181bd12008-07-27 21:46:04 +0000187 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
188 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnerf1940742009-06-20 20:38:48 +0000189 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
190 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman8181bd12008-07-27 21:46:04 +0000191 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
192 SDValue N, SDValue &Base, SDValue &Scale,
193 SDValue &Index, SDValue &Disp,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000194 SDValue &Segment,
Dan Gohman8181bd12008-07-27 21:46:04 +0000195 SDValue &InChain, SDValue &OutChain);
196 bool TryFoldLoad(SDValue P, SDValue N,
197 SDValue &Base, SDValue &Scale,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000198 SDValue &Index, SDValue &Disp,
199 SDValue &Segment);
Dan Gohman14a66442008-08-23 02:25:05 +0000200 void PreprocessForRMW();
201 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
203 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
204 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000205 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000207 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000209 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
210
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
212 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000213 SDValue &Disp, SDValue &Segment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
215 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
216 AM.Base.Reg;
217 Scale = getI8Imm(AM.Scale);
218 Index = AM.IndexReg;
219 // These are 32-bit even in 64-bit mode since RIP relative offset
220 // is 32-bit.
221 if (AM.GV)
Chris Lattner8969a732009-06-26 05:51:45 +0000222 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
223 AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 else if (AM.CP)
Gabor Greife9f7f582008-08-31 15:37:04 +0000225 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattner8969a732009-06-26 05:51:45 +0000226 AM.Align, AM.Disp, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 else if (AM.ES)
Chris Lattner8969a732009-06-26 05:51:45 +0000228 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 else if (AM.JT != -1)
Chris Lattner8969a732009-06-26 05:51:45 +0000230 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 else
Dan Gohman0bd76b72008-11-11 15:52:29 +0000232 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000233
234 if (AM.Segment.getNode())
235 Segment = AM.Segment;
236 else
237 Segment = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 }
239
240 /// getI8Imm - Return a target constant with the specified value, of type
241 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 inline SDValue getI8Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 return CurDAG->getTargetConstant(Imm, MVT::i8);
244 }
245
246 /// getI16Imm - Return a target constant with the specified value, of type
247 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000248 inline SDValue getI16Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 return CurDAG->getTargetConstant(Imm, MVT::i16);
250 }
251
252 /// getI32Imm - Return a target constant with the specified value, of type
253 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000254 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 return CurDAG->getTargetConstant(Imm, MVT::i32);
256 }
257
Dan Gohmanb60482f2008-09-23 18:22:58 +0000258 /// getGlobalBaseReg - Return an SDNode that returns the value of
259 /// the global base register. Output instructions required to
260 /// initialize the global base register, if necessary.
261 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 SDNode *getGlobalBaseReg();
263
Dan Gohman4261ed32009-06-03 20:20:00 +0000264 /// getTargetMachine - Return a reference to the TargetMachine, casted
265 /// to the target-specific type.
266 const X86TargetMachine &getTargetMachine() {
267 return static_cast<const X86TargetMachine &>(TM);
268 }
269
270 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
271 /// to the target-specific type.
272 const X86InstrInfo *getInstrInfo() {
273 return getTargetMachine().getInstrInfo();
274 }
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276#ifndef NDEBUG
277 unsigned Indent;
278#endif
279 };
280}
281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
Evan Cheng5a424552008-11-27 00:49:46 +0000283bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
284 SDNode *Root) const {
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000285 if (OptLevel == CodeGenOpt::None) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
Evan Cheng5a424552008-11-27 00:49:46 +0000287 if (U == Root)
288 switch (U->getOpcode()) {
289 default: break;
290 case ISD::ADD:
291 case ISD::ADDC:
292 case ISD::ADDE:
293 case ISD::AND:
294 case ISD::OR:
295 case ISD::XOR: {
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000296 SDValue Op1 = U->getOperand(1);
297
Evan Cheng5a424552008-11-27 00:49:46 +0000298 // If the other operand is a 8-bit immediate we should fold the immediate
299 // instead. This reduces code size.
300 // e.g.
301 // movl 4(%esp), %eax
302 // addl $4, %eax
303 // vs.
304 // movl $4, %eax
305 // addl 4(%esp), %eax
306 // The former is 2 bytes shorter. In case where the increment is 1, then
307 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000308 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman01126892009-03-14 02:07:16 +0000309 if (Imm->getAPIntValue().isSignedIntN(8))
310 return false;
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000311
312 // If the other operand is a TLS address, we should fold it instead.
313 // This produces
314 // movl %gs:0, %eax
315 // leal i@NTPOFF(%eax), %eax
316 // instead of
317 // movl $i@NTPOFF, %eax
318 // addl %gs:0, %eax
319 // if the block also has an access to a second TLS address this will save
320 // a load.
321 // FIXME: This is probably also true for non TLS addresses.
322 if (Op1.getOpcode() == X86ISD::Wrapper) {
323 SDValue Val = Op1.getOperand(0);
324 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
325 return false;
326 }
Evan Cheng5a424552008-11-27 00:49:46 +0000327 }
328 }
329
Anton Korobeynikovda76d322009-05-08 18:51:58 +0000330 // Proceed to 'generic' cycle finder code
331 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332}
333
334/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
335/// and move load below the TokenFactor. Replace store's chain operand with
336/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000337static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000338 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000339 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000340 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
341 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000342 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000344 Ops.push_back(TF.getOperand(i));
Dan Gohman14a66442008-08-23 02:25:05 +0000345 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
346 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
347 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
348 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349}
350
Evan Cheng2b2a7012008-05-23 21:23:16 +0000351/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
352///
Dan Gohman8181bd12008-07-27 21:46:04 +0000353static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
354 SDValue &Load) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000355 if (N.getOpcode() == ISD::BIT_CONVERT)
356 N = N.getOperand(0);
357
358 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
359 if (!LD || LD->isVolatile())
360 return false;
361 if (LD->getAddressingMode() != ISD::UNINDEXED)
362 return false;
363
364 ISD::LoadExtType ExtType = LD->getExtensionType();
365 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
366 return false;
367
368 if (N.hasOneUse() &&
369 N.getOperand(1) == Address &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000370 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000371 Load = N;
372 return true;
373 }
374 return false;
375}
376
Evan Cheng98cfaf82008-08-25 21:27:18 +0000377/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
378/// operand and move load below the call's chain operand.
379static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
evanchengcd6d72b2009-01-26 18:43:34 +0000380 SDValue Call, SDValue CallSeqStart) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000381 SmallVector<SDValue, 8> Ops;
evanchengcd6d72b2009-01-26 18:43:34 +0000382 SDValue Chain = CallSeqStart.getOperand(0);
383 if (Chain.getNode() == Load.getNode())
384 Ops.push_back(Load.getOperand(0));
385 else {
386 assert(Chain.getOpcode() == ISD::TokenFactor &&
387 "Unexpected CallSeqStart chain operand");
388 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
389 if (Chain.getOperand(i).getNode() == Load.getNode())
390 Ops.push_back(Load.getOperand(0));
391 else
392 Ops.push_back(Chain.getOperand(i));
393 SDValue NewChain =
Dale Johannesen913ba762009-02-06 01:31:28 +0000394 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
395 MVT::Other, &Ops[0], Ops.size());
evanchengcd6d72b2009-01-26 18:43:34 +0000396 Ops.clear();
397 Ops.push_back(NewChain);
398 }
399 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
400 Ops.push_back(CallSeqStart.getOperand(i));
401 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000402 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
403 Load.getOperand(1), Load.getOperand(2));
404 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000405 Ops.push_back(SDValue(Load.getNode(), 1));
406 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000407 Ops.push_back(Call.getOperand(i));
408 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
409}
410
411/// isCalleeLoad - Return true if call address is a load and it can be
412/// moved below CALLSEQ_START and the chains leading up to the call.
413/// Return the CALLSEQ_START by reference as a second output.
414static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000415 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000416 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000417 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000418 if (!LD ||
419 LD->isVolatile() ||
420 LD->getAddressingMode() != ISD::UNINDEXED ||
421 LD->getExtensionType() != ISD::NON_EXTLOAD)
422 return false;
423
424 // Now let's find the callseq_start.
425 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
426 if (!Chain.hasOneUse())
427 return false;
428 Chain = Chain.getOperand(0);
429 }
evanchengcd6d72b2009-01-26 18:43:34 +0000430
431 if (Chain.getOperand(0).getNode() == Callee.getNode())
432 return true;
433 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
434 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
435 return true;
436 return false;
Evan Cheng98cfaf82008-08-25 21:27:18 +0000437}
438
439
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000440/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000441/// This is only run if not in -O0 mode.
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000442/// This allows the instruction selector to pick more read-modify-write
443/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444///
445/// [Load chain]
446/// ^
447/// |
448/// [Load]
449/// ^ ^
450/// | |
451/// / \-
452/// / |
453/// [TokenFactor] [Op]
454/// ^ ^
455/// | |
456/// \ /
457/// \ /
458/// [Store]
459///
460/// The fact the store's chain operand != load's chain will prevent the
461/// (store (op (load))) instruction from being selected. We can transform it to:
462///
463/// [Load chain]
464/// ^
465/// |
466/// [TokenFactor]
467/// ^
468/// |
469/// [Load]
470/// ^ ^
471/// | |
472/// | \-
473/// | |
474/// | [Op]
475/// | ^
476/// | |
477/// \ /
478/// \ /
479/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000480void X86DAGToDAGISel::PreprocessForRMW() {
481 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
482 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000483 if (I->getOpcode() == X86ISD::CALL) {
484 /// Also try moving call address load from outside callseq_start to just
485 /// before the call to allow it to be folded.
486 ///
487 /// [Load chain]
488 /// ^
489 /// |
490 /// [Load]
491 /// ^ ^
492 /// | |
493 /// / \--
494 /// / |
495 ///[CALLSEQ_START] |
496 /// ^ |
497 /// | |
498 /// [LOAD/C2Reg] |
499 /// | |
500 /// \ /
501 /// \ /
502 /// [CALL]
503 SDValue Chain = I->getOperand(0);
504 SDValue Load = I->getOperand(1);
505 if (!isCalleeLoad(Load, Chain))
506 continue;
507 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
508 ++NumLoadMoved;
509 continue;
510 }
511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 if (!ISD::isNON_TRUNCStore(I))
513 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000514 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000515
Gabor Greif1c80d112008-08-28 21:40:38 +0000516 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 continue;
518
Dan Gohman8181bd12008-07-27 21:46:04 +0000519 SDValue N1 = I->getOperand(1);
520 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000521 if ((N1.getValueType().isFloatingPoint() &&
522 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 !N1.hasOneUse())
524 continue;
525
526 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000527 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000528 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000530 case ISD::ADD:
531 case ISD::MUL:
532 case ISD::AND:
533 case ISD::OR:
534 case ISD::XOR:
535 case ISD::ADDC:
536 case ISD::ADDE:
537 case ISD::VECTOR_SHUFFLE: {
538 SDValue N10 = N1.getOperand(0);
539 SDValue N11 = N1.getOperand(1);
540 RModW = isRMWLoad(N10, Chain, N2, Load);
541 if (!RModW)
542 RModW = isRMWLoad(N11, Chain, N2, Load);
543 break;
544 }
545 case ISD::SUB:
546 case ISD::SHL:
547 case ISD::SRA:
548 case ISD::SRL:
549 case ISD::ROTL:
550 case ISD::ROTR:
551 case ISD::SUBC:
552 case ISD::SUBE:
553 case X86ISD::SHLD:
554 case X86ISD::SHRD: {
555 SDValue N10 = N1.getOperand(0);
556 RModW = isRMWLoad(N10, Chain, N2, Load);
557 break;
558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 }
560
561 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000562 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 ++NumLoadMoved;
564 }
565 }
566}
567
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000568
569/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
570/// nodes that target the FP stack to be store and load to the stack. This is a
571/// gross hack. We would like to simply mark these as being illegal, but when
572/// we do that, legalize produces these when it expands calls, then expands
573/// these in the same legalize pass. We would like dag combine to be able to
574/// hack on these between the call expansion and the node legalization. As such
575/// this pass basically does "really late" legalization of these inline with the
576/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000577void X86DAGToDAGISel::PreprocessForFPConvert() {
578 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
579 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000580 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
581 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
582 continue;
583
584 // If the source and destination are SSE registers, then this is a legal
585 // conversion that should not be lowered.
Duncan Sands92c43912008-06-06 12:08:01 +0000586 MVT SrcVT = N->getOperand(0).getValueType();
587 MVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000588 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
589 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
590 if (SrcIsSSE && DstIsSSE)
591 continue;
592
Chris Lattner5d294e52008-03-09 07:05:32 +0000593 if (!SrcIsSSE && !DstIsSSE) {
594 // If this is an FPStack extension, it is a noop.
595 if (N->getOpcode() == ISD::FP_EXTEND)
596 continue;
597 // If this is a value-preserving FPStack truncation, it is a noop.
598 if (N->getConstantOperandVal(1))
599 continue;
600 }
601
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000602 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
603 // FPStack has extload and truncstore. SSE can fold direct loads into other
604 // operations. Based on this, decide what we want to do.
Duncan Sands92c43912008-06-06 12:08:01 +0000605 MVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000606 if (N->getOpcode() == ISD::FP_ROUND)
607 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
608 else
609 MemVT = SrcIsSSE ? SrcVT : DstVT;
610
Dan Gohman14a66442008-08-23 02:25:05 +0000611 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000612 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000613
614 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000615 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohman14a66442008-08-23 02:25:05 +0000616 N->getOperand(0),
617 MemTmp, NULL, 0, MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000618 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohman14a66442008-08-23 02:25:05 +0000619 NULL, 0, MemVT);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000620
621 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
622 // extload we created. This will cause general havok on the dag because
623 // anything below the conversion could be folded into other existing nodes.
624 // To avoid invalidating 'I', back it up to the convert node.
625 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000626 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000627
628 // Now that we did that, the node is dead. Increment the iterator to the
629 // next node to process, then delete N.
630 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000631 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000632 }
633}
634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
636/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000637void X86DAGToDAGISel::InstructionSelect() {
Evan Cheng34fd4f32008-06-30 20:45:06 +0000638 CurBB = BB; // BB can change as result of isel.
Devang Patel78eba022008-10-06 18:03:39 +0000639 const Function *F = CurDAG->getMachineFunction().getFunction();
640 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641
Evan Cheng34fd4f32008-06-30 20:45:06 +0000642 DEBUG(BB->dump());
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000643 if (OptLevel != CodeGenOpt::None)
Dan Gohman14a66442008-08-23 02:25:05 +0000644 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000645
Bill Wendling58ed5d22009-04-29 00:15:41 +0000646 // FIXME: This should only happen when not compiled with -O0.
Dan Gohman14a66442008-08-23 02:25:05 +0000647 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648
649 // Codegen the basic block.
650#ifndef NDEBUG
651 DOUT << "===== Instruction selection begins:\n";
652 Indent = 0;
653#endif
David Greene932618b2008-10-27 21:56:29 +0000654 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655#ifndef NDEBUG
656 DOUT << "===== Instruction selection ends:\n";
657#endif
658
Dan Gohman14a66442008-08-23 02:25:05 +0000659 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000660}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000662/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
663/// the main function.
664void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
665 MachineFrameInfo *MFI) {
666 const TargetInstrInfo *TII = TM.getInstrInfo();
667 if (Subtarget->isTargetCygMing())
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000668 BuildMI(BB, DebugLoc::getUnknownLoc(),
669 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000670}
671
672void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
673 // If this is main, emit special code for main.
674 MachineBasicBlock *BB = MF.begin();
675 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
676 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
677}
678
Rafael Espindolabca99f72009-04-08 21:14:34 +0000679
680bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
681 X86ISelAddressMode &AM) {
682 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
683 SDValue Segment = N.getOperand(0);
684
685 if (AM.Segment.getNode() == 0) {
686 AM.Segment = Segment;
687 return false;
688 }
689
690 return true;
691}
692
693bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
694 // This optimization is valid because the GNU TLS model defines that
695 // gs:0 (or fs:0 on X86-64) contains its own address.
696 // For more information see http://people.redhat.com/drepper/tls.pdf
697
698 SDValue Address = N.getOperand(1);
699 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
700 !MatchSegmentBaseAddress (Address, AM))
701 return false;
702
703 return true;
704}
705
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000706/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
707/// into an addressing mode. These wrap things that will resolve down into a
708/// symbol reference. If no match is possible, this returns true, otherwise it
709/// returns false.
Rafael Espindola84218fe2009-04-12 21:55:03 +0000710bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000711 // If the addressing mode already has a symbol as the displacement, we can
712 // never match another symbol.
Rafael Espindola84218fe2009-04-12 21:55:03 +0000713 if (AM.hasSymbolicDisplacement())
714 return true;
Rafael Espindola84218fe2009-04-12 21:55:03 +0000715
716 SDValue N0 = N.getOperand(0);
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000717
718 // Handle X86-64 rip-relative addresses. We check this before checking direct
719 // folding because RIP is preferable to non-RIP accesses.
720 if (Subtarget->is64Bit() &&
721 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
722 // they cannot be folded into immediate fields.
723 // FIXME: This can be improved for kernel and other models?
724 TM.getCodeModel() == CodeModel::Small &&
725
726 // Base and index reg must be 0 in order to use %rip as base and lowering
727 // must allow RIP.
728 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
729
730 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
731 int64_t Offset = AM.Disp + G->getOffset();
732 if (!isInt32(Offset)) return true;
733 AM.GV = G->getGlobal();
734 AM.Disp = Offset;
Chris Lattner8969a732009-06-26 05:51:45 +0000735 AM.SymbolFlags = G->getTargetFlags();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000736 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
737 int64_t Offset = AM.Disp + CP->getOffset();
738 if (!isInt32(Offset)) return true;
Rafael Espindola84218fe2009-04-12 21:55:03 +0000739 AM.CP = CP->getConstVal();
740 AM.Align = CP->getAlignment();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000741 AM.Disp = Offset;
Chris Lattner998c2052009-06-26 05:56:49 +0000742 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000743 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
744 AM.ES = S->getSymbol();
745 AM.SymbolFlags = S->getTargetFlags();
746 } else {
747 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
748 AM.JT = J->getIndex();
749 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola84218fe2009-04-12 21:55:03 +0000750 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000751
752 if (N.getOpcode() == X86ISD::WrapperRIP)
753 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola84218fe2009-04-12 21:55:03 +0000754 return false;
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000755 }
756
757 // Handle the case when globals fit in our immediate field: This is true for
758 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
759 // mode, this results in a non-RIP-relative computation.
760 if (!Subtarget->is64Bit() ||
761 (TM.getCodeModel() == CodeModel::Small &&
762 TM.getRelocationModel() == Reloc::Static)) {
763 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
764 AM.GV = G->getGlobal();
765 AM.Disp += G->getOffset();
766 AM.SymbolFlags = G->getTargetFlags();
767 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
768 AM.CP = CP->getConstVal();
769 AM.Align = CP->getAlignment();
770 AM.Disp += CP->getOffset();
771 AM.SymbolFlags = CP->getTargetFlags();
772 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
773 AM.ES = S->getSymbol();
774 AM.SymbolFlags = S->getTargetFlags();
775 } else {
776 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
777 AM.JT = J->getIndex();
778 AM.SymbolFlags = J->getTargetFlags();
779 }
Rafael Espindola84218fe2009-04-12 21:55:03 +0000780 return false;
781 }
782
783 return true;
784}
785
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786/// MatchAddress - Add the specified node to the specified addressing mode,
787/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000788/// addressing mode.
Dan Gohman8181bd12008-07-27 21:46:04 +0000789bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola515c13e2009-03-31 16:16:57 +0000790 unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000791 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000792 DebugLoc dl = N.getDebugLoc();
Evan Cheng7f250d62008-09-24 00:05:32 +0000793 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmana60c1b32007-08-13 20:03:06 +0000794 // Limit recursion.
795 if (Depth > 5)
Rafael Espindola515c13e2009-03-31 16:16:57 +0000796 return MatchAddressBase(N, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000798 // If this is already a %rip relative address, we can only merge immediates
799 // into it. Instead of handling this in every case, we handle it here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000801 if (AM.isRIPRelative()) {
802 // FIXME: JumpTable and ExternalSymbol address currently don't like
803 // displacements. It isn't very important, but this should be fixed for
804 // consistency.
805 if (!AM.ES && AM.JT != -1) return true;
806
807 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
808 int64_t Val = AM.Disp + Cst->getSExtValue();
809 if (isInt32(Val)) {
810 AM.Disp = Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 return false;
812 }
813 }
814 return true;
815 }
816
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 switch (N.getOpcode()) {
818 default: break;
819 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000820 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000821 if (!is64Bit || isInt32(AM.Disp + Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 AM.Disp += Val;
823 return false;
824 }
825 break;
826 }
827
Rafael Espindolabca99f72009-04-08 21:14:34 +0000828 case X86ISD::SegmentBaseAddress:
829 if (!MatchSegmentBaseAddress(N, AM))
830 return false;
831 break;
832
Rafael Espindola84218fe2009-04-12 21:55:03 +0000833 case X86ISD::Wrapper:
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000834 case X86ISD::WrapperRIP:
Rafael Espindola84218fe2009-04-12 21:55:03 +0000835 if (!MatchWrapper(N, AM))
836 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838
Rafael Espindolabca99f72009-04-08 21:14:34 +0000839 case ISD::LOAD:
840 if (!MatchLoad(N, AM))
841 return false;
842 break;
843
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000845 if (AM.BaseType == X86ISelAddressMode::RegBase
846 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
848 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
849 return false;
850 }
851 break;
852
853 case ISD::SHL:
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000854 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000855 break;
856
Gabor Greife9f7f582008-08-31 15:37:04 +0000857 if (ConstantSDNode
858 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000859 unsigned Val = CN->getZExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +0000860 if (Val == 1 || Val == 2 || Val == 3) {
861 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000862 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863
Chris Lattner7f06edd2007-12-08 07:22:58 +0000864 // Okay, we know that we have a scale by now. However, if the scaled
865 // value is an add of something and a constant, we can fold the
866 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000867 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
868 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
869 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000870 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000871 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000872 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman36322c72008-10-18 02:06:02 +0000873 if (!is64Bit || isInt32(Disp))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000874 AM.Disp = Disp;
875 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000877 } else {
878 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000880 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 }
882 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000883 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884
Dan Gohman35b99222007-10-22 20:22:24 +0000885 case ISD::SMUL_LOHI:
886 case ISD::UMUL_LOHI:
887 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +0000888 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +0000889 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 case ISD::MUL:
Evan Chengc3495762009-03-30 21:36:47 +0000891 case X86ISD::MUL_IMM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +0000893 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000894 AM.Base.Reg.getNode() == 0 &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000895 AM.IndexReg.getNode() == 0) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000896 if (ConstantSDNode
897 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000898 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
899 CN->getZExtValue() == 9) {
900 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901
Gabor Greif1c80d112008-08-28 21:40:38 +0000902 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000903 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904
905 // Okay, we know that we have a scale by now. However, if the scaled
906 // value is an add of something and a constant, we can fold the
907 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +0000908 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
909 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
910 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000912 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000913 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000914 CN->getZExtValue();
Dan Gohman36322c72008-10-18 02:06:02 +0000915 if (!is64Bit || isInt32(Disp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 AM.Disp = Disp;
917 else
Gabor Greif1c80d112008-08-28 21:40:38 +0000918 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +0000920 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 }
922
923 AM.IndexReg = AM.Base.Reg = Reg;
924 return false;
925 }
926 }
927 break;
928
Dan Gohman946223f2009-05-11 18:02:53 +0000929 case ISD::SUB: {
930 // Given A-B, if A can be completely folded into the address and
931 // the index field with the index field unused, use -B as the index.
932 // This is a win if a has multiple parts that can be folded into
933 // the address. Also, this saves a mov if the base register has
934 // other uses, since it avoids a two-address sub instruction, however
935 // it costs an additional mov if the index register has other uses.
936
937 // Test if the LHS of the sub can be folded.
938 X86ISelAddressMode Backup = AM;
939 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
940 AM = Backup;
941 break;
942 }
943 // Test if the index field is free for use.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000944 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman946223f2009-05-11 18:02:53 +0000945 AM = Backup;
946 break;
947 }
948 int Cost = 0;
949 SDValue RHS = N.getNode()->getOperand(1);
950 // If the RHS involves a register with multiple uses, this
951 // transformation incurs an extra mov, due to the neg instruction
952 // clobbering its operand.
953 if (!RHS.getNode()->hasOneUse() ||
954 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
955 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
956 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
957 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
958 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
959 ++Cost;
960 // If the base is a register with multiple uses, this
961 // transformation may save a mov.
962 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
963 AM.Base.Reg.getNode() &&
964 !AM.Base.Reg.getNode()->hasOneUse()) ||
965 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
966 --Cost;
967 // If the folded LHS was interesting, this transformation saves
968 // address arithmetic.
969 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
970 ((AM.Disp != 0) && (Backup.Disp == 0)) +
971 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
972 --Cost;
973 // If it doesn't look like it may be an overall win, don't do it.
974 if (Cost >= 0) {
975 AM = Backup;
976 break;
977 }
978
979 // Ok, the transformation is legal and appears profitable. Go for it.
980 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
981 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
982 AM.IndexReg = Neg;
983 AM.Scale = 1;
984
985 // Insert the new nodes into the topological ordering.
986 if (Zero.getNode()->getNodeId() == -1 ||
987 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
988 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
989 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
990 }
991 if (Neg.getNode()->getNodeId() == -1 ||
992 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
993 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
994 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
995 }
996 return false;
997 }
998
Evan Cheng2ed6f342009-01-17 07:09:27 +0000999 case ISD::ADD: {
1000 X86ISelAddressMode Backup = AM;
Rafael Espindola515c13e2009-03-31 16:16:57 +00001001 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
1002 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng2ed6f342009-01-17 07:09:27 +00001003 return false;
1004 AM = Backup;
Rafael Espindola515c13e2009-03-31 16:16:57 +00001005 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
1006 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng2ed6f342009-01-17 07:09:27 +00001007 return false;
1008 AM = Backup;
Dan Gohman3ae92482009-03-13 02:25:09 +00001009
1010 // If we couldn't fold both operands into the address at the same time,
1011 // see if we can just put each operand into a register and fold at least
1012 // the add.
1013 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1014 !AM.Base.Reg.getNode() &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001015 !AM.IndexReg.getNode()) {
Dan Gohman3ae92482009-03-13 02:25:09 +00001016 AM.Base.Reg = N.getNode()->getOperand(0);
1017 AM.IndexReg = N.getNode()->getOperand(1);
1018 AM.Scale = 1;
1019 return false;
1020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 break;
Evan Cheng2ed6f342009-01-17 07:09:27 +00001022 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023
1024 case ISD::OR:
1025 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +00001026 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1027 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +00001028 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +00001029 // Start with the LHS as an addr mode.
Rafael Espindola515c13e2009-03-31 16:16:57 +00001030 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +00001031 // Address could not have picked a GV address for the displacement.
1032 AM.GV == NULL &&
1033 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman0bd76b72008-11-11 15:52:29 +00001034 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +00001035 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00001036 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +00001037 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +00001038 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 }
Chris Lattner7f06edd2007-12-08 07:22:58 +00001040 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 }
1042 break;
Evan Chengf2abee72007-12-13 00:43:27 +00001043
1044 case ISD::AND: {
Dan Gohman744d4622009-04-13 16:09:41 +00001045 // Perform some heroic transforms on an and of a constant-count shift
1046 // with a constant to enable use of the scaled offset field.
1047
Dan Gohman8181bd12008-07-27 21:46:04 +00001048 SDValue Shift = N.getOperand(0);
Dan Gohman744d4622009-04-13 16:09:41 +00001049 if (Shift.getNumOperands() != 2) break;
Dan Gohmancc3df852008-11-05 04:14:16 +00001050
Evan Chengf2abee72007-12-13 00:43:27 +00001051 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +00001052 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +00001053
Dan Gohman744d4622009-04-13 16:09:41 +00001054 SDValue X = Shift.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +00001055 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1056 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1057 if (!C1 || !C2) break;
1058
Dan Gohman744d4622009-04-13 16:09:41 +00001059 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1060 // allows us to convert the shift and and into an h-register extract and
1061 // a scaled index.
1062 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1063 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindolaa4464662009-04-16 12:34:53 +00001064 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman744d4622009-04-13 16:09:41 +00001065 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1066 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1067 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1068 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1069 X, Eight);
1070 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1071 Srl, Mask);
Dan Gohman3dc69fd2009-04-14 22:45:05 +00001072 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1073 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1074 And, ShlCount);
Dan Gohman744d4622009-04-13 16:09:41 +00001075
1076 // Insert the new nodes into the topological ordering.
1077 if (Eight.getNode()->getNodeId() == -1 ||
1078 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1079 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1080 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1081 }
1082 if (Mask.getNode()->getNodeId() == -1 ||
1083 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1084 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1085 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1086 }
1087 if (Srl.getNode()->getNodeId() == -1 ||
1088 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1089 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1090 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1091 }
1092 if (And.getNode()->getNodeId() == -1 ||
1093 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1094 CurDAG->RepositionNode(N.getNode(), And.getNode());
1095 And.getNode()->setNodeId(N.getNode()->getNodeId());
1096 }
Dan Gohman3dc69fd2009-04-14 22:45:05 +00001097 if (ShlCount.getNode()->getNodeId() == -1 ||
1098 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1099 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1100 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1101 }
1102 if (Shl.getNode()->getNodeId() == -1 ||
1103 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1104 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1105 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1106 }
1107 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman744d4622009-04-13 16:09:41 +00001108 AM.IndexReg = And;
1109 AM.Scale = (1 << ScaleLog);
1110 return false;
1111 }
1112 }
1113
1114 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1115 // allows us to fold the shift into this addressing mode.
1116 if (Shift.getOpcode() != ISD::SHL) break;
1117
Evan Chengf2abee72007-12-13 00:43:27 +00001118 // Not likely to be profitable if either the AND or SHIFT node has more
1119 // than one use (unless all uses are for address computation). Besides,
1120 // isel mechanism requires their node ids to be reused.
1121 if (!N.hasOneUse() || !Shift.hasOneUse())
1122 break;
1123
1124 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001125 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +00001126 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1127 break;
1128
1129 // Get the new AND mask, this folds to a constant.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001130 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +00001131 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001132 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1133 NewANDMask);
1134 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman3666f472008-10-13 20:52:04 +00001135 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +00001136
1137 // Insert the new nodes into the topological ordering.
1138 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1139 CurDAG->RepositionNode(X.getNode(), C1);
1140 C1->setNodeId(X.getNode()->getNodeId());
1141 }
1142 if (NewANDMask.getNode()->getNodeId() == -1 ||
1143 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1144 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1145 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1146 }
1147 if (NewAND.getNode()->getNodeId() == -1 ||
1148 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1149 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1150 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1151 }
1152 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1153 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1154 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1155 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1156 }
1157
Dan Gohman3666f472008-10-13 20:52:04 +00001158 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +00001159
1160 AM.Scale = 1 << ShiftCst;
1161 AM.IndexReg = NewAND;
1162 return false;
1163 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 }
1165
Rafael Espindola515c13e2009-03-31 16:16:57 +00001166 return MatchAddressBase(N, AM);
Dan Gohmana60c1b32007-08-13 20:03:06 +00001167}
1168
1169/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1170/// specified addressing mode without any further recursion.
Rafael Espindola515c13e2009-03-31 16:16:57 +00001171bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001173 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 // If so, check to see if the scale index register is set.
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001175 if (AM.IndexReg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 AM.IndexReg = N;
1177 AM.Scale = 1;
1178 return false;
1179 }
1180
1181 // Otherwise, we cannot select it.
1182 return true;
1183 }
1184
1185 // Default, generate it as a register.
1186 AM.BaseType = X86ISelAddressMode::RegBase;
1187 AM.Base.Reg = N;
1188 return false;
1189}
1190
1191/// SelectAddr - returns true if it is able pattern match an addressing mode.
1192/// It returns the operands which make up the maximal addressing mode it can
1193/// match by reference.
Dan Gohman8181bd12008-07-27 21:46:04 +00001194bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1195 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001196 SDValue &Disp, SDValue &Segment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 X86ISelAddressMode AM;
Evan Cheng8655a532009-03-31 01:13:53 +00001198 bool Done = false;
1199 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1200 unsigned Opcode = N.getOpcode();
1201 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001202 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng8655a532009-03-31 01:13:53 +00001203 // If we are able to fold N into addressing mode, then we'll allow it even
1204 // if N has multiple uses. In general, addressing computation is used as
1205 // addresses by all of its uses. But watch out for CopyToReg uses, that
1206 // means the address computation is liveout. It will be computed by a LEA
1207 // so we want to avoid computing the address twice.
1208 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1209 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1210 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola515c13e2009-03-31 16:16:57 +00001211 MatchAddressBase(N, AM);
Evan Cheng8655a532009-03-31 01:13:53 +00001212 Done = true;
1213 break;
1214 }
1215 }
1216 }
1217 }
1218
1219 if (!Done && MatchAddress(N, AM))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 return false;
1221
Duncan Sands92c43912008-06-06 12:08:01 +00001222 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001224 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 AM.Base.Reg = CurDAG->getRegister(0, VT);
1226 }
1227
Gabor Greif1c80d112008-08-28 21:40:38 +00001228 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 AM.IndexReg = CurDAG->getRegister(0, VT);
1230
Rafael Espindolabca99f72009-04-08 21:14:34 +00001231 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 return true;
1233}
1234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001235/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1236/// match a load whose top elements are either undef or zeros. The load flavor
1237/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00001238bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1239 SDValue N, SDValue &Base,
1240 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001241 SDValue &Disp, SDValue &Segment,
1242 SDValue &InChain,
Dan Gohman8181bd12008-07-27 21:46:04 +00001243 SDValue &OutChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1245 InChain = N.getOperand(0).getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001246 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 InChain.getValue(0).hasOneUse() &&
1248 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001249 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001251 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 return false;
1253 OutChain = LD->getChain();
1254 return true;
1255 }
1256 }
1257
1258 // Also handle the case where we explicitly require zeros in the top
1259 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001260 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001261 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001262 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001263 N.getOperand(0).getNode()->hasOneUse() &&
1264 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00001265 N.getOperand(0).getOperand(0).hasOneUse()) {
1266 // Okay, this is a zero extending load. Fold it.
1267 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindolabca99f72009-04-08 21:14:34 +00001268 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng40ee6e52008-05-08 00:57:18 +00001269 return false;
1270 OutChain = LD->getChain();
Dan Gohman8181bd12008-07-27 21:46:04 +00001271 InChain = SDValue(LD, 1);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001272 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 }
1274 return false;
1275}
1276
1277
1278/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1279/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman8181bd12008-07-27 21:46:04 +00001280bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1281 SDValue &Base, SDValue &Scale,
1282 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 X86ISelAddressMode AM;
Rafael Espindola7682b9c2009-04-10 10:09:34 +00001284
1285 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1286 // segments.
1287 SDValue Copy = AM.Segment;
1288 SDValue T = CurDAG->getRegister(0, MVT::i32);
1289 AM.Segment = T;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 if (MatchAddress(N, AM))
1291 return false;
Rafael Espindola7682b9c2009-04-10 10:09:34 +00001292 assert (T == AM.Segment);
1293 AM.Segment = Copy;
Rafael Espindolabca99f72009-04-08 21:14:34 +00001294
Duncan Sands92c43912008-06-06 12:08:01 +00001295 MVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 unsigned Complexity = 0;
1297 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001298 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 Complexity = 1;
1300 else
1301 AM.Base.Reg = CurDAG->getRegister(0, VT);
1302 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1303 Complexity = 4;
1304
Gabor Greif1c80d112008-08-28 21:40:38 +00001305 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 Complexity++;
1307 else
1308 AM.IndexReg = CurDAG->getRegister(0, VT);
1309
1310 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1311 // a simple shift.
1312 if (AM.Scale > 1)
1313 Complexity++;
1314
1315 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1316 // to a LEA. This is determined with some expermentation but is by no means
1317 // optimal (especially for code size consideration). LEA is nice because of
1318 // its three-address nature. Tweak the cost function again when we can run
1319 // convertToThreeAddress() at register allocation time.
Dan Gohman245791b2009-02-07 00:43:41 +00001320 if (AM.hasSymbolicDisplacement()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 // For X86-64, we should always use lea to materialize RIP relative
1322 // addresses.
1323 if (Subtarget->is64Bit())
1324 Complexity = 4;
1325 else
1326 Complexity += 2;
1327 }
1328
Gabor Greif1c80d112008-08-28 21:40:38 +00001329 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 Complexity++;
1331
1332 if (Complexity > 2) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00001333 SDValue Segment;
1334 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 return true;
1336 }
1337 return false;
1338}
1339
Chris Lattnerf1940742009-06-20 20:38:48 +00001340/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1341bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1342 SDValue &Scale, SDValue &Index,
1343 SDValue &Disp) {
1344 assert(Op.getOpcode() == X86ISD::TLSADDR);
1345 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1346 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1347
1348 X86ISelAddressMode AM;
1349 AM.GV = GA->getGlobal();
1350 AM.Disp += GA->getOffset();
1351 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerafab6592009-06-26 21:18:37 +00001352 AM.SymbolFlags = GA->getTargetFlags();
1353
Chris Lattnerf1940742009-06-20 20:38:48 +00001354 if (N.getValueType() == MVT::i32) {
1355 AM.Scale = 1;
1356 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1357 } else {
1358 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1359 }
1360
1361 SDValue Segment;
1362 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1363 return true;
1364}
1365
1366
Dan Gohman8181bd12008-07-27 21:46:04 +00001367bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1368 SDValue &Base, SDValue &Scale,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001369 SDValue &Index, SDValue &Disp,
1370 SDValue &Segment) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001371 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 N.hasOneUse() &&
Evan Cheng5a424552008-11-27 00:49:46 +00001373 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindolabca99f72009-04-08 21:14:34 +00001374 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 return false;
1376}
1377
Dan Gohmanb60482f2008-09-23 18:22:58 +00001378/// getGlobalBaseReg - Return an SDNode that returns the value of
1379/// the global base register. Output instructions required to
1380/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381///
1382SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman882ab732008-09-30 00:58:23 +00001383 MachineFunction *MF = CurBB->getParent();
Dan Gohman4261ed32009-06-03 20:20:00 +00001384 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001385 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386}
1387
1388static SDNode *FindCallStartFromCall(SDNode *Node) {
1389 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1390 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1391 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001392 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393}
1394
Dale Johannesenf160d802008-10-02 18:53:47 +00001395SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1396 SDValue Chain = Node->getOperand(0);
1397 SDValue In1 = Node->getOperand(1);
1398 SDValue In2L = Node->getOperand(2);
1399 SDValue In2H = Node->getOperand(3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001400 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1401 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesenf160d802008-10-02 18:53:47 +00001402 return NULL;
Dale Johannesen44eb5372008-10-03 19:41:08 +00001403 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindolabca99f72009-04-08 21:14:34 +00001404 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001405 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1406 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001407 array_lengthof(Ops));
Dale Johannesenf160d802008-10-02 18:53:47 +00001408}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001409
Dan Gohman8181bd12008-07-27 21:46:04 +00001410SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001411 SDNode *Node = N.getNode();
Duncan Sands92c43912008-06-06 12:08:01 +00001412 MVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 unsigned Opc, MOpc;
1414 unsigned Opcode = Node->getOpcode();
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001415 DebugLoc dl = Node->getDebugLoc();
1416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417#ifndef NDEBUG
1418 DOUT << std::string(Indent, ' ') << "Selecting: ";
1419 DEBUG(Node->dump(CurDAG));
1420 DOUT << "\n";
1421 Indent += 2;
1422#endif
1423
Dan Gohmanbd68c792008-07-17 19:10:17 +00001424 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425#ifndef NDEBUG
1426 DOUT << std::string(Indent-2, ' ') << "== ";
1427 DEBUG(Node->dump(CurDAG));
1428 DOUT << "\n";
1429 Indent -= 2;
1430#endif
1431 return NULL; // Already selected.
1432 }
1433
1434 switch (Opcode) {
1435 default: break;
1436 case X86ISD::GlobalBaseReg:
1437 return getGlobalBaseReg();
1438
Dale Johannesenf160d802008-10-02 18:53:47 +00001439 case X86ISD::ATOMOR64_DAG:
1440 return SelectAtomic64(Node, X86::ATOMOR6432);
1441 case X86ISD::ATOMXOR64_DAG:
1442 return SelectAtomic64(Node, X86::ATOMXOR6432);
1443 case X86ISD::ATOMADD64_DAG:
1444 return SelectAtomic64(Node, X86::ATOMADD6432);
1445 case X86ISD::ATOMSUB64_DAG:
1446 return SelectAtomic64(Node, X86::ATOMSUB6432);
1447 case X86ISD::ATOMNAND64_DAG:
1448 return SelectAtomic64(Node, X86::ATOMNAND6432);
1449 case X86ISD::ATOMAND64_DAG:
1450 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00001451 case X86ISD::ATOMSWAP64_DAG:
1452 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001453
Dan Gohman5a199552007-10-08 18:33:35 +00001454 case ISD::SMUL_LOHI:
1455 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001456 SDValue N0 = Node->getOperand(0);
1457 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001458
Dan Gohman5a199552007-10-08 18:33:35 +00001459 bool isSigned = Opcode == ISD::SMUL_LOHI;
1460 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001461 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 default: assert(0 && "Unsupported VT!");
1463 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1464 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1465 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1466 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1467 }
1468 else
Duncan Sands92c43912008-06-06 12:08:01 +00001469 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 default: assert(0 && "Unsupported VT!");
1471 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1472 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1473 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1474 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1475 }
1476
1477 unsigned LoReg, HiReg;
Duncan Sands92c43912008-06-06 12:08:01 +00001478 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 default: assert(0 && "Unsupported VT!");
1480 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1481 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1482 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1483 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1484 }
1485
Rafael Espindolabca99f72009-04-08 21:14:34 +00001486 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1487 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman5a199552007-10-08 18:33:35 +00001488 // multiplty is commmutative
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 if (!foldedLoad) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00001490 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng508fe8b2007-08-02 05:48:35 +00001491 if (foldedLoad)
1492 std::swap(N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 }
1494
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001495 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman8181bd12008-07-27 21:46:04 +00001496 N0, SDValue()).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497
1498 if (foldedLoad) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00001499 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1500 InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 SDNode *CNode =
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001502 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001503 array_lengthof(Ops));
Dan Gohman8181bd12008-07-27 21:46:04 +00001504 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001505 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001506 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001509 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 }
1511
Dan Gohman5a199552007-10-08 18:33:35 +00001512 // Copy the low half of the result, if it is needed.
1513 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001514 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001515 LoReg, NVT, InFlag);
1516 InFlag = Result.getValue(2);
1517 ReplaceUses(N.getValue(0), Result);
1518#ifndef NDEBUG
1519 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001520 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001521 DOUT << "\n";
1522#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001523 }
Dan Gohman5a199552007-10-08 18:33:35 +00001524 // Copy the high half of the result, if it is needed.
1525 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001526 SDValue Result;
Dan Gohman5a199552007-10-08 18:33:35 +00001527 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1528 // Prevent use of AH in a REX instruction by referencing AX instead.
1529 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001530 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001531 X86::AX, MVT::i16, InFlag);
1532 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001533 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1534 Result,
Gabor Greife9f7f582008-08-31 15:37:04 +00001535 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman5a199552007-10-08 18:33:35 +00001536 // Then truncate it down to i8.
Dan Gohman436c7622009-04-13 15:14:03 +00001537 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001538 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001539 MVT::i8, Result, SRIdx), 0);
1540 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001541 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001542 HiReg, NVT, InFlag);
1543 InFlag = Result.getValue(2);
1544 }
1545 ReplaceUses(N.getValue(1), Result);
1546#ifndef NDEBUG
1547 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001548 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman5a199552007-10-08 18:33:35 +00001549 DOUT << "\n";
1550#endif
1551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552
1553#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 Indent -= 2;
1555#endif
Dan Gohman5a199552007-10-08 18:33:35 +00001556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 return NULL;
1558 }
1559
Dan Gohman5a199552007-10-08 18:33:35 +00001560 case ISD::SDIVREM:
1561 case ISD::UDIVREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001562 SDValue N0 = Node->getOperand(0);
1563 SDValue N1 = Node->getOperand(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001564
1565 bool isSigned = Opcode == ISD::SDIVREM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 if (!isSigned)
Duncan Sands92c43912008-06-06 12:08:01 +00001567 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 default: assert(0 && "Unsupported VT!");
1569 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1570 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1571 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1572 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1573 }
1574 else
Duncan Sands92c43912008-06-06 12:08:01 +00001575 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 default: assert(0 && "Unsupported VT!");
1577 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1578 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1579 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1580 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1581 }
1582
1583 unsigned LoReg, HiReg;
1584 unsigned ClrOpcode, SExtOpcode;
Duncan Sands92c43912008-06-06 12:08:01 +00001585 switch (NVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 default: assert(0 && "Unsupported VT!");
1587 case MVT::i8:
1588 LoReg = X86::AL; HiReg = X86::AH;
1589 ClrOpcode = 0;
1590 SExtOpcode = X86::CBW;
1591 break;
1592 case MVT::i16:
1593 LoReg = X86::AX; HiReg = X86::DX;
1594 ClrOpcode = X86::MOV16r0;
1595 SExtOpcode = X86::CWD;
1596 break;
1597 case MVT::i32:
1598 LoReg = X86::EAX; HiReg = X86::EDX;
1599 ClrOpcode = X86::MOV32r0;
1600 SExtOpcode = X86::CDQ;
1601 break;
1602 case MVT::i64:
1603 LoReg = X86::RAX; HiReg = X86::RDX;
1604 ClrOpcode = X86::MOV64r0;
1605 SExtOpcode = X86::CQO;
1606 break;
1607 }
1608
Rafael Espindolabca99f72009-04-08 21:14:34 +00001609 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1610 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001611 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001612
Dan Gohman8181bd12008-07-27 21:46:04 +00001613 SDValue InFlag;
Dan Gohman7bbd9202009-01-21 14:50:16 +00001614 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 // Special case for div8, just use a move with zero extension to AX to
1616 // clear the upper 8 bits (AH).
Rafael Espindolabca99f72009-04-08 21:14:34 +00001617 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1618 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1619 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001621 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001622 MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001623 array_lengthof(Ops)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624 Chain = Move.getValue(1);
1625 ReplaceUses(N0.getValue(1), Chain);
1626 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 Move =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001628 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629 Chain = CurDAG->getEntryNode();
1630 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001631 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632 InFlag = Chain.getValue(1);
1633 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 InFlag =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001635 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00001636 LoReg, N0, SDValue()).getValue(1);
Dan Gohman7bbd9202009-01-21 14:50:16 +00001637 if (isSigned && !signBitIsZero) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 // Sign extend the low part into the high part.
1639 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001640 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641 } else {
1642 // Zero out the high part, effectively zero extending the input.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001643 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1644 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001645 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman5a199552007-10-08 18:33:35 +00001646 ClrNode, InFlag).getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 }
1648 }
1649
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 if (foldedLoad) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00001651 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1652 InFlag };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 SDNode *CNode =
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001654 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001655 array_lengthof(Ops));
Dan Gohman8181bd12008-07-27 21:46:04 +00001656 InFlag = SDValue(CNode, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00001657 // Update the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00001658 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 InFlag =
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001661 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 }
1663
Dan Gohman242a5ba2007-09-25 18:23:27 +00001664 // Copy the division (low) result, if it is needed.
1665 if (!N.getValue(0).use_empty()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001666 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001667 LoReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001668 InFlag = Result.getValue(2);
1669 ReplaceUses(N.getValue(0), Result);
1670#ifndef NDEBUG
1671 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001672 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001673 DOUT << "\n";
1674#endif
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00001675 }
Dan Gohman242a5ba2007-09-25 18:23:27 +00001676 // Copy the remainder (high) result, if it is needed.
1677 if (!N.getValue(1).use_empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001678 SDValue Result;
Dan Gohman242a5ba2007-09-25 18:23:27 +00001679 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1680 // Prevent use of AH in a REX instruction by referencing AX instead.
1681 // Shift it down 8 bits.
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001682 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001683 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001684 InFlag = Result.getValue(2);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001685 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1686 Result,
1687 CurDAG->getTargetConstant(8, MVT::i8)),
1688 0);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001689 // Then truncate it down to i8.
Dan Gohman436c7622009-04-13 15:14:03 +00001690 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001691 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman242a5ba2007-09-25 18:23:27 +00001692 MVT::i8, Result, SRIdx), 0);
1693 } else {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001694 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman5a199552007-10-08 18:33:35 +00001695 HiReg, NVT, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00001696 InFlag = Result.getValue(2);
1697 }
1698 ReplaceUses(N.getValue(1), Result);
1699#ifndef NDEBUG
1700 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001701 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman242a5ba2007-09-25 18:23:27 +00001702 DOUT << "\n";
1703#endif
1704 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705
1706#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 Indent -= 2;
1708#endif
1709
1710 return NULL;
1711 }
Christopher Lamb422213d2007-08-10 22:22:41 +00001712
Evan Chengd4cebcd2008-06-17 02:01:22 +00001713 case ISD::DECLARE: {
1714 // Handle DECLARE nodes here because the second operand may have been
1715 // wrapped in X86ISD::Wrapper.
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SDValue Chain = Node->getOperand(0);
1717 SDValue N1 = Node->getOperand(1);
1718 SDValue N2 = Node->getOperand(2);
Evan Cheng417bc002008-12-10 21:49:05 +00001719 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001720
1721 // FIXME: We need to handle this for VLAs.
1722 if (!FINode) {
1723 ReplaceUses(N.getValue(0), Chain);
1724 return NULL;
1725 }
1726
Evan Cheng651e1442008-06-18 02:48:27 +00001727 if (N2.getOpcode() == ISD::ADD &&
1728 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1729 N2 = N2.getOperand(1);
Chris Lattneref6a8192009-02-12 17:33:11 +00001730
1731 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1732 // somehow, just ignore it.
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001733 if (N2.getOpcode() != X86ISD::Wrapper &&
1734 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattneref6a8192009-02-12 17:33:11 +00001735 ReplaceUses(N.getValue(0), Chain);
1736 return NULL;
1737 }
Evan Chengf3ecd1a2009-01-10 03:33:22 +00001738 GlobalAddressSDNode *GVNode =
1739 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattneref6a8192009-02-12 17:33:11 +00001740 if (GVNode == 0) {
1741 ReplaceUses(N.getValue(0), Chain);
1742 return NULL;
1743 }
Evan Cheng417bc002008-12-10 21:49:05 +00001744 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1745 TLI.getPointerTy());
1746 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1747 TLI.getPointerTy());
1748 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001749 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolabc7f4ac2009-03-27 15:45:05 +00001750 MVT::Other, Ops,
Rafael Espindolad9040a12009-03-28 19:02:18 +00001751 array_lengthof(Ops));
Evan Chengd4cebcd2008-06-17 02:01:22 +00001752 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001753 }
1754
1755 SDNode *ResNode = SelectCode(N);
1756
1757#ifndef NDEBUG
1758 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greif1c80d112008-08-28 21:40:38 +00001759 if (ResNode == NULL || ResNode == N.getNode())
1760 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 else
1762 DEBUG(ResNode->dump(CurDAG));
1763 DOUT << "\n";
1764 Indent -= 2;
1765#endif
1766
1767 return ResNode;
1768}
1769
1770bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00001771SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00001772 std::vector<SDValue> &OutOps) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00001773 SDValue Op0, Op1, Op2, Op3, Op4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 switch (ConstraintCode) {
1775 case 'o': // offsetable ??
1776 case 'v': // not offsetable ??
1777 default: return true;
1778 case 'm': // memory
Rafael Espindolabca99f72009-04-08 21:14:34 +00001779 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 return true;
1781 break;
1782 }
1783
1784 OutOps.push_back(Op0);
1785 OutOps.push_back(Op1);
1786 OutOps.push_back(Op2);
1787 OutOps.push_back(Op3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001788 OutOps.push_back(Op4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 return false;
1790}
1791
1792/// createX86ISelDag - This pass converts a legalized DAG into a
1793/// X86-specific DAG, ready for instruction scheduling.
1794///
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001795FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1796 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendling58ed5d22009-04-29 00:15:41 +00001797 return new X86DAGToDAGISel(TM, OptLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001798}