blob: 79711dfff3113c70c67efdc95ac06a96b4b31044 [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
Daniel Dunbar95734fc2009-11-10 18:24:37 +000015// Force NDEBUG on in any optimized build on Darwin.
16//
17// FIXME: This is a huge hack, to work around ridiculously awful compile times
18// on this file with gcc-4.2 on Darwin, in Release mode.
Daniel Dunbar7eef4c72009-11-11 00:28:38 +000019#if (!defined(__llvm__) && defined(__APPLE__) && \
20 defined(__OPTIMIZE__) && !defined(NDEBUG))
Daniel Dunbar95734fc2009-11-10 18:24:37 +000021#define NDEBUG
22#endif
23
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#define DEBUG_TYPE "x86-isel"
25#include "X86.h"
26#include "X86InstrBuilder.h"
27#include "X86ISelLowering.h"
Evan Cheng0729ccf2008-01-05 00:41:47 +000028#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "X86RegisterInfo.h"
30#include "X86Subtarget.h"
31#include "X86TargetMachine.h"
32#include "llvm/GlobalValue.h"
33#include "llvm/Instructions.h"
34#include "llvm/Intrinsics.h"
35#include "llvm/Support/CFG.h"
36#include "llvm/Type.h"
37#include "llvm/CodeGen/MachineConstantPool.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineFrameInfo.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/CodeGen/SelectionDAGISel.h"
43#include "llvm/Target/TargetMachine.h"
Evan Cheng13559d62008-09-26 23:41:32 +000044#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000046#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047#include "llvm/Support/MathExtras.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000048#include "llvm/Support/raw_ostream.h"
Evan Cheng656269e2008-04-25 08:22:20 +000049#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051using namespace llvm;
52
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
54
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055//===----------------------------------------------------------------------===//
56// Pattern Matcher Implementation
57//===----------------------------------------------------------------------===//
58
59namespace {
60 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman8181bd12008-07-27 21:46:04 +000061 /// SDValue's instead of register numbers for the leaves of the matched
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 /// tree.
63 struct X86ISelAddressMode {
64 enum {
65 RegBase,
66 FrameIndexBase
67 } BaseType;
68
69 struct { // This is really a union, discriminated by BaseType!
Dan Gohman8181bd12008-07-27 21:46:04 +000070 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 int FrameIndex;
72 } Base;
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 unsigned Scale;
Dan Gohman8181bd12008-07-27 21:46:04 +000075 SDValue IndexReg;
Dan Gohman0bd76b72008-11-11 15:52:29 +000076 int32_t Disp;
Rafael Espindolabca99f72009-04-08 21:14:34 +000077 SDValue Segment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 GlobalValue *GV;
79 Constant *CP;
Chris Lattner03796ee2009-11-01 03:25:03 +000080 BlockAddress *BlockAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 const char *ES;
82 int JT;
83 unsigned Align; // CP alignment.
Chris Lattner8969a732009-06-26 05:51:45 +000084 unsigned char SymbolFlags; // X86II::MO_*
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
86 X86ISelAddressMode()
Chris Lattnerdc6fc472009-06-27 04:16:01 +000087 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner03796ee2009-11-01 03:25:03 +000088 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman814c2ea2009-08-25 17:47:44 +000089 SymbolFlags(X86II::MO_NO_FLAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 }
Dan Gohman245791b2009-02-07 00:43:41 +000091
92 bool hasSymbolicDisplacement() const {
Chris Lattner03796ee2009-11-01 03:25:03 +000093 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman245791b2009-02-07 00:43:41 +000094 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +000095
96 bool hasBaseOrIndexReg() const {
97 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
98 }
99
100 /// isRIPRelative - Return true if this addressing mode is already RIP
101 /// relative.
102 bool isRIPRelative() const {
103 if (BaseType != RegBase) return false;
104 if (RegisterSDNode *RegNode =
105 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
106 return RegNode->getReg() == X86::RIP;
107 return false;
108 }
109
110 void setBaseReg(SDValue Reg) {
111 BaseType = RegBase;
112 Base.Reg = Reg;
113 }
Dan Gohman245791b2009-02-07 00:43:41 +0000114
Dale Johannesenc501c082008-08-11 23:46:25 +0000115 void dump() {
David Greeneb592f222010-01-05 01:29:08 +0000116 dbgs() << "X86ISelAddressMode " << this << '\n';
117 dbgs() << "Base.Reg ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000118 if (Base.Reg.getNode() != 0)
119 Base.Reg.getNode()->dump();
120 else
David Greeneb592f222010-01-05 01:29:08 +0000121 dbgs() << "nul";
122 dbgs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
Benjamin Kramer7966fe82009-08-23 11:52:17 +0000123 << " Scale" << Scale << '\n'
124 << "IndexReg ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000125 if (IndexReg.getNode() != 0)
126 IndexReg.getNode()->dump();
127 else
David Greeneb592f222010-01-05 01:29:08 +0000128 dbgs() << "nul";
129 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer7966fe82009-08-23 11:52:17 +0000130 << "GV ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000131 if (GV)
132 GV->dump();
133 else
David Greeneb592f222010-01-05 01:29:08 +0000134 dbgs() << "nul";
135 dbgs() << " CP ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000136 if (CP)
137 CP->dump();
138 else
David Greeneb592f222010-01-05 01:29:08 +0000139 dbgs() << "nul";
140 dbgs() << '\n'
Benjamin Kramer7966fe82009-08-23 11:52:17 +0000141 << "ES ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000142 if (ES)
David Greeneb592f222010-01-05 01:29:08 +0000143 dbgs() << ES;
Bill Wendlingc551eff2009-08-07 21:33:25 +0000144 else
David Greeneb592f222010-01-05 01:29:08 +0000145 dbgs() << "nul";
146 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesenc501c082008-08-11 23:46:25 +0000147 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 };
149}
150
151namespace {
152 //===--------------------------------------------------------------------===//
153 /// ISel - X86 specific code to select X86 machine instructions for
154 /// SelectionDAG operations.
155 ///
Nick Lewycky492d06e2009-10-25 06:33:48 +0000156 class X86DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 /// X86Lowering - This object fully describes how to lower LLVM code to an
158 /// X86-specific SelectionDAG.
Dan Gohmanf2b29572008-10-03 16:55:19 +0000159 X86TargetLowering &X86Lowering;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
161 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
162 /// make the right decision when generating code for different targets.
163 const X86Subtarget *Subtarget;
164
Evan Cheng13559d62008-09-26 23:41:32 +0000165 /// OptForSize - If true, selector should try to optimize for code size
166 /// instead of performance.
167 bool OptForSize;
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 public:
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000170 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000171 : SelectionDAGISel(tm, OptLevel),
Dan Gohman4261ed32009-06-03 20:20:00 +0000172 X86Lowering(*tm.getTargetLowering()),
173 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel93698d92008-10-01 23:18:38 +0000174 OptForSize(false) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 virtual const char *getPassName() const {
177 return "X86 DAG->DAG Instruction Selection";
178 }
179
Evan Cheng34fd4f32008-06-30 20:45:06 +0000180 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000182 virtual void InstructionSelect();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000183
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000184 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
185
Evan Chengf80681e2010-02-15 19:41:07 +0000186 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
187
188 virtual bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190// Include the pieces autogenerated from the target description.
191#include "X86GenDAGISel.inc"
192
193 private:
Dan Gohman5f082a72010-01-05 01:24:18 +0000194 SDNode *Select(SDNode *N);
Dale Johannesenf160d802008-10-02 18:53:47 +0000195 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersonac9de032009-08-10 22:56:29 +0000196 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
Rafael Espindolabca99f72009-04-08 21:14:34 +0000198 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
199 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola84218fe2009-04-12 21:55:03 +0000200 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman3dffbbf2009-07-22 23:26:55 +0000201 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
202 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
203 unsigned Depth);
Rafael Espindola515c13e2009-03-31 16:16:57 +0000204 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman5f082a72010-01-05 01:24:18 +0000205 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000206 SDValue &Scale, SDValue &Index, SDValue &Disp,
207 SDValue &Segment);
Dan Gohman5f082a72010-01-05 01:24:18 +0000208 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman5f082a72010-01-05 01:24:18 +0000210 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattnerf1940742009-06-20 20:38:48 +0000211 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnerb4d19fc2010-02-21 03:17:59 +0000212 bool SelectScalarSSELoadXXX(SDNode *Root, SDValue N,
Chris Lattnerecbbf492010-02-16 22:35:06 +0000213 SDValue &Base, SDValue &Scale,
Dan Gohman8181bd12008-07-27 21:46:04 +0000214 SDValue &Index, SDValue &Disp,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000215 SDValue &Segment,
Chris Lattnerb4d19fc2010-02-21 03:17:59 +0000216 SDValue &NodeWithChain);
217
218 // FIXME: Remove this hacky wrapper.
219 bool SelectScalarSSELoad(SDNode *Root, SDValue N, SDValue &Base,
220 SDValue &Scale, SDValue &Index,
221 SDValue &Disp, SDValue &Segment,
Chris Lattner52b513d2010-02-17 06:07:47 +0000222 SDValue &PatternChainResult,
Chris Lattnerb4d19fc2010-02-21 03:17:59 +0000223 SDValue &PatternInputChain) {
224 SDValue Tmp;
225 if (!SelectScalarSSELoadXXX(Root, N, Base, Scale, Index, Disp, Segment,
226 Tmp))
227 return false;
228 PatternInputChain = Tmp.getOperand(0);
229 PatternChainResult = Tmp.getValue(1);
230 return true;
231 }
Dan Gohman5f082a72010-01-05 01:24:18 +0000232 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000233 SDValue &Base, SDValue &Scale,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000234 SDValue &Index, SDValue &Disp,
235 SDValue &Segment);
Dan Gohman14a66442008-08-23 02:25:05 +0000236 void PreprocessForRMW();
237 void PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238
239 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
240 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000241 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000243 std::vector<SDValue> &OutOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000245 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
246
Dan Gohman8181bd12008-07-27 21:46:04 +0000247 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
248 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +0000249 SDValue &Disp, SDValue &Segment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
251 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
252 AM.Base.Reg;
253 Scale = getI8Imm(AM.Scale);
254 Index = AM.IndexReg;
255 // These are 32-bit even in 64-bit mode since RIP relative offset
256 // is 32-bit.
257 if (AM.GV)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000258 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattner8969a732009-06-26 05:51:45 +0000259 AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 else if (AM.CP)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000261 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattner8969a732009-06-26 05:51:45 +0000262 AM.Align, AM.Disp, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 else if (AM.ES)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000264 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 else if (AM.JT != -1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000266 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner03796ee2009-11-01 03:25:03 +0000267 else if (AM.BlockAddr)
Dan Gohman885793b2009-11-20 23:18:13 +0000268 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
269 true, AM.SymbolFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000271 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000272
273 if (AM.Segment.getNode())
274 Segment = AM.Segment;
275 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000276 Segment = CurDAG->getRegister(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
278
279 /// getI8Imm - Return a target constant with the specified value, of type
280 /// i8.
Dan Gohman8181bd12008-07-27 21:46:04 +0000281 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000282 return CurDAG->getTargetConstant(Imm, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 }
284
285 /// getI16Imm - Return a target constant with the specified value, of type
286 /// i16.
Dan Gohman8181bd12008-07-27 21:46:04 +0000287 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000288 return CurDAG->getTargetConstant(Imm, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 }
290
291 /// getI32Imm - Return a target constant with the specified value, of type
292 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +0000293 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000294 return CurDAG->getTargetConstant(Imm, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 }
296
Dan Gohmanb60482f2008-09-23 18:22:58 +0000297 /// getGlobalBaseReg - Return an SDNode that returns the value of
298 /// the global base register. Output instructions required to
299 /// initialize the global base register, if necessary.
300 ///
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 SDNode *getGlobalBaseReg();
302
Dan Gohman4261ed32009-06-03 20:20:00 +0000303 /// getTargetMachine - Return a reference to the TargetMachine, casted
304 /// to the target-specific type.
305 const X86TargetMachine &getTargetMachine() {
306 return static_cast<const X86TargetMachine &>(TM);
307 }
308
309 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
310 /// to the target-specific type.
311 const X86InstrInfo *getInstrInfo() {
312 return getTargetMachine().getInstrInfo();
313 }
314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315#ifndef NDEBUG
316 unsigned Indent;
317#endif
318 };
319}
320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
Evan Chengf80681e2010-02-15 19:41:07 +0000322bool
323X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000324 if (OptLevel == CodeGenOpt::None) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
Evan Chengf80681e2010-02-15 19:41:07 +0000326 if (!N.hasOneUse())
327 return false;
328
329 if (N.getOpcode() != ISD::LOAD)
330 return true;
331
332 // If N is a load, do additional profitability checks.
333 if (U == Root) {
Evan Cheng5a424552008-11-27 00:49:46 +0000334 switch (U->getOpcode()) {
335 default: break;
Dan Gohmanf23790a2010-01-04 20:51:50 +0000336 case X86ISD::ADD:
337 case X86ISD::SUB:
338 case X86ISD::AND:
339 case X86ISD::XOR:
340 case X86ISD::OR:
Evan Cheng5a424552008-11-27 00:49:46 +0000341 case ISD::ADD:
342 case ISD::ADDC:
343 case ISD::ADDE:
344 case ISD::AND:
345 case ISD::OR:
346 case ISD::XOR: {
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000347 SDValue Op1 = U->getOperand(1);
348
Evan Cheng5a424552008-11-27 00:49:46 +0000349 // If the other operand is a 8-bit immediate we should fold the immediate
350 // instead. This reduces code size.
351 // e.g.
352 // movl 4(%esp), %eax
353 // addl $4, %eax
354 // vs.
355 // movl $4, %eax
356 // addl 4(%esp), %eax
357 // The former is 2 bytes shorter. In case where the increment is 1, then
358 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000359 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman01126892009-03-14 02:07:16 +0000360 if (Imm->getAPIntValue().isSignedIntN(8))
361 return false;
Rafael Espindola7682b9c2009-04-10 10:09:34 +0000362
363 // If the other operand is a TLS address, we should fold it instead.
364 // This produces
365 // movl %gs:0, %eax
366 // leal i@NTPOFF(%eax), %eax
367 // instead of
368 // movl $i@NTPOFF, %eax
369 // addl %gs:0, %eax
370 // if the block also has an access to a second TLS address this will save
371 // a load.
372 // FIXME: This is probably also true for non TLS addresses.
373 if (Op1.getOpcode() == X86ISD::Wrapper) {
374 SDValue Val = Op1.getOperand(0);
375 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
376 return false;
377 }
Evan Cheng5a424552008-11-27 00:49:46 +0000378 }
379 }
Evan Chengf80681e2010-02-15 19:41:07 +0000380 }
381
382 return true;
383}
384
385
386bool X86DAGToDAGISel::IsLegalToFold(SDValue N, SDNode *U, SDNode *Root) const {
387 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng5a424552008-11-27 00:49:46 +0000388
Anton Korobeynikovda76d322009-05-08 18:51:58 +0000389 // Proceed to 'generic' cycle finder code
Evan Chengf80681e2010-02-15 19:41:07 +0000390 return SelectionDAGISel::IsLegalToFold(N, U, Root);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391}
392
393/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
394/// and move load below the TokenFactor. Replace store's chain operand with
395/// load's chain result.
Dan Gohman14a66442008-08-23 02:25:05 +0000396static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman8181bd12008-07-27 21:46:04 +0000397 SDValue Store, SDValue TF) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000398 SmallVector<SDValue, 4> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +0000399 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
400 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000401 Ops.push_back(Load.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 else
Evan Cheng98cfaf82008-08-25 21:27:18 +0000403 Ops.push_back(TF.getOperand(i));
Dan Gohmanabe8a472009-08-06 09:22:57 +0000404 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
405 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
406 Load.getOperand(1),
407 Load.getOperand(2));
408 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohman14a66442008-08-23 02:25:05 +0000409 Store.getOperand(2), Store.getOperand(3));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410}
411
Nate Begeman63715c72009-09-16 03:20:46 +0000412/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
413/// chain produced by the load must only be used by the store's chain operand,
414/// otherwise this may produce a cycle in the DAG.
Evan Cheng2b2a7012008-05-23 21:23:16 +0000415///
Dan Gohman8181bd12008-07-27 21:46:04 +0000416static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
417 SDValue &Load) {
David Greene6a5bbde2010-01-15 23:23:41 +0000418 if (N.getOpcode() == ISD::BIT_CONVERT) {
419 if (!N.hasOneUse())
420 return false;
Evan Cheng2b2a7012008-05-23 21:23:16 +0000421 N = N.getOperand(0);
David Greene6a5bbde2010-01-15 23:23:41 +0000422 }
Evan Cheng2b2a7012008-05-23 21:23:16 +0000423
424 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
425 if (!LD || LD->isVolatile())
426 return false;
427 if (LD->getAddressingMode() != ISD::UNINDEXED)
428 return false;
429
430 ISD::LoadExtType ExtType = LD->getExtensionType();
431 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
432 return false;
433
434 if (N.hasOneUse() &&
Nate Begeman63715c72009-09-16 03:20:46 +0000435 LD->hasNUsesOfValue(1, 1) &&
Evan Cheng2b2a7012008-05-23 21:23:16 +0000436 N.getOperand(1) == Address &&
Nate Begeman63715c72009-09-16 03:20:46 +0000437 LD->isOperandOf(Chain.getNode())) {
Evan Cheng2b2a7012008-05-23 21:23:16 +0000438 Load = N;
439 return true;
440 }
441 return false;
442}
443
Evan Cheng98cfaf82008-08-25 21:27:18 +0000444/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
445/// operand and move load below the call's chain operand.
446static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
evanchengcd6d72b2009-01-26 18:43:34 +0000447 SDValue Call, SDValue CallSeqStart) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000448 SmallVector<SDValue, 8> Ops;
evanchengcd6d72b2009-01-26 18:43:34 +0000449 SDValue Chain = CallSeqStart.getOperand(0);
450 if (Chain.getNode() == Load.getNode())
451 Ops.push_back(Load.getOperand(0));
452 else {
453 assert(Chain.getOpcode() == ISD::TokenFactor &&
454 "Unexpected CallSeqStart chain operand");
455 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
456 if (Chain.getOperand(i).getNode() == Load.getNode())
457 Ops.push_back(Load.getOperand(0));
458 else
459 Ops.push_back(Chain.getOperand(i));
460 SDValue NewChain =
Dale Johannesen913ba762009-02-06 01:31:28 +0000461 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000462 MVT::Other, &Ops[0], Ops.size());
evanchengcd6d72b2009-01-26 18:43:34 +0000463 Ops.clear();
464 Ops.push_back(NewChain);
465 }
466 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
467 Ops.push_back(CallSeqStart.getOperand(i));
468 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000469 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
470 Load.getOperand(1), Load.getOperand(2));
471 Ops.clear();
Gabor Greif1c80d112008-08-28 21:40:38 +0000472 Ops.push_back(SDValue(Load.getNode(), 1));
473 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Cheng98cfaf82008-08-25 21:27:18 +0000474 Ops.push_back(Call.getOperand(i));
475 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
476}
477
478/// isCalleeLoad - Return true if call address is a load and it can be
479/// moved below CALLSEQ_START and the chains leading up to the call.
480/// Return the CALLSEQ_START by reference as a second output.
481static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000482 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Cheng98cfaf82008-08-25 21:27:18 +0000483 return false;
Gabor Greif1c80d112008-08-28 21:40:38 +0000484 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Cheng98cfaf82008-08-25 21:27:18 +0000485 if (!LD ||
486 LD->isVolatile() ||
487 LD->getAddressingMode() != ISD::UNINDEXED ||
488 LD->getExtensionType() != ISD::NON_EXTLOAD)
489 return false;
490
491 // Now let's find the callseq_start.
492 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
493 if (!Chain.hasOneUse())
494 return false;
495 Chain = Chain.getOperand(0);
496 }
evanchengcd6d72b2009-01-26 18:43:34 +0000497
498 if (Chain.getOperand(0).getNode() == Callee.getNode())
499 return true;
500 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohmancbe5a492009-09-15 01:22:01 +0000501 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
502 Callee.getValue(1).hasOneUse())
evanchengcd6d72b2009-01-26 18:43:34 +0000503 return true;
504 return false;
Evan Cheng98cfaf82008-08-25 21:27:18 +0000505}
506
507
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000508/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000509/// This is only run if not in -O0 mode.
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000510/// This allows the instruction selector to pick more read-modify-write
511/// instructions. This is a common case:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512///
513/// [Load chain]
514/// ^
515/// |
516/// [Load]
517/// ^ ^
518/// | |
519/// / \-
520/// / |
521/// [TokenFactor] [Op]
522/// ^ ^
523/// | |
524/// \ /
525/// \ /
526/// [Store]
527///
528/// The fact the store's chain operand != load's chain will prevent the
529/// (store (op (load))) instruction from being selected. We can transform it to:
530///
531/// [Load chain]
532/// ^
533/// |
534/// [TokenFactor]
535/// ^
536/// |
537/// [Load]
538/// ^ ^
539/// | |
540/// | \-
541/// | |
542/// | [Op]
543/// | ^
544/// | |
545/// \ /
546/// \ /
547/// [Store]
Dan Gohman14a66442008-08-23 02:25:05 +0000548void X86DAGToDAGISel::PreprocessForRMW() {
549 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
550 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000551 if (I->getOpcode() == X86ISD::CALL) {
552 /// Also try moving call address load from outside callseq_start to just
553 /// before the call to allow it to be folded.
554 ///
555 /// [Load chain]
556 /// ^
557 /// |
558 /// [Load]
559 /// ^ ^
560 /// | |
561 /// / \--
562 /// / |
563 ///[CALLSEQ_START] |
564 /// ^ |
565 /// | |
566 /// [LOAD/C2Reg] |
567 /// | |
568 /// \ /
569 /// \ /
570 /// [CALL]
571 SDValue Chain = I->getOperand(0);
572 SDValue Load = I->getOperand(1);
573 if (!isCalleeLoad(Load, Chain))
574 continue;
575 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
576 ++NumLoadMoved;
577 continue;
578 }
579
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 if (!ISD::isNON_TRUNCStore(I))
581 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +0000582 SDValue Chain = I->getOperand(0);
Evan Cheng98cfaf82008-08-25 21:27:18 +0000583
Gabor Greif1c80d112008-08-28 21:40:38 +0000584 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 continue;
586
Dan Gohman8181bd12008-07-27 21:46:04 +0000587 SDValue N1 = I->getOperand(1);
588 SDValue N2 = I->getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +0000589 if ((N1.getValueType().isFloatingPoint() &&
590 !N1.getValueType().isVector()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 !N1.hasOneUse())
592 continue;
593
594 bool RModW = false;
Dan Gohman8181bd12008-07-27 21:46:04 +0000595 SDValue Load;
Gabor Greif1c80d112008-08-28 21:40:38 +0000596 unsigned Opcode = N1.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 switch (Opcode) {
Evan Cheng98cfaf82008-08-25 21:27:18 +0000598 case ISD::ADD:
599 case ISD::MUL:
600 case ISD::AND:
601 case ISD::OR:
602 case ISD::XOR:
603 case ISD::ADDC:
604 case ISD::ADDE:
605 case ISD::VECTOR_SHUFFLE: {
606 SDValue N10 = N1.getOperand(0);
607 SDValue N11 = N1.getOperand(1);
608 RModW = isRMWLoad(N10, Chain, N2, Load);
609 if (!RModW)
610 RModW = isRMWLoad(N11, Chain, N2, Load);
611 break;
612 }
613 case ISD::SUB:
614 case ISD::SHL:
615 case ISD::SRA:
616 case ISD::SRL:
617 case ISD::ROTL:
618 case ISD::ROTR:
619 case ISD::SUBC:
620 case ISD::SUBE:
621 case X86ISD::SHLD:
622 case X86ISD::SHRD: {
623 SDValue N10 = N1.getOperand(0);
624 RModW = isRMWLoad(N10, Chain, N2, Load);
625 break;
626 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 }
628
629 if (RModW) {
Dan Gohman14a66442008-08-23 02:25:05 +0000630 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 ++NumLoadMoved;
David Greenec25757c2010-01-20 20:13:31 +0000632 checkForCycles(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 }
634 }
635}
636
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000637
638/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
639/// nodes that target the FP stack to be store and load to the stack. This is a
640/// gross hack. We would like to simply mark these as being illegal, but when
641/// we do that, legalize produces these when it expands calls, then expands
642/// these in the same legalize pass. We would like dag combine to be able to
643/// hack on these between the call expansion and the node legalization. As such
644/// this pass basically does "really late" legalization of these inline with the
645/// X86 isel pass.
Dan Gohman14a66442008-08-23 02:25:05 +0000646void X86DAGToDAGISel::PreprocessForFPConvert() {
647 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
648 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000649 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
650 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
651 continue;
652
653 // If the source and destination are SSE registers, then this is a legal
654 // conversion that should not be lowered.
Owen Andersonac9de032009-08-10 22:56:29 +0000655 EVT SrcVT = N->getOperand(0).getValueType();
656 EVT DstVT = N->getValueType(0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000657 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
658 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
659 if (SrcIsSSE && DstIsSSE)
660 continue;
661
Chris Lattner5d294e52008-03-09 07:05:32 +0000662 if (!SrcIsSSE && !DstIsSSE) {
663 // If this is an FPStack extension, it is a noop.
664 if (N->getOpcode() == ISD::FP_EXTEND)
665 continue;
666 // If this is a value-preserving FPStack truncation, it is a noop.
667 if (N->getConstantOperandVal(1))
668 continue;
669 }
670
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000671 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
672 // FPStack has extload and truncstore. SSE can fold direct loads into other
673 // operations. Based on this, decide what we want to do.
Owen Andersonac9de032009-08-10 22:56:29 +0000674 EVT MemVT;
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000675 if (N->getOpcode() == ISD::FP_ROUND)
676 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
677 else
678 MemVT = SrcIsSSE ? SrcVT : DstVT;
679
Dan Gohman14a66442008-08-23 02:25:05 +0000680 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000681 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000682
683 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000684 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohman14a66442008-08-23 02:25:05 +0000685 N->getOperand(0),
David Greene1d878002010-02-15 16:57:43 +0000686 MemTmp, NULL, 0, MemVT,
687 false, false, 0);
Dale Johannesen59b5a5c2009-02-03 21:48:12 +0000688 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greene1d878002010-02-15 16:57:43 +0000689 NULL, 0, MemVT, false, false, 0);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000690
691 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
692 // extload we created. This will cause general havok on the dag because
693 // anything below the conversion could be folded into other existing nodes.
694 // To avoid invalidating 'I', back it up to the convert node.
695 --I;
Dan Gohman14a66442008-08-23 02:25:05 +0000696 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000697
698 // Now that we did that, the node is dead. Increment the iterator to the
699 // next node to process, then delete N.
700 ++I;
Dan Gohman14a66442008-08-23 02:25:05 +0000701 CurDAG->DeleteNode(N);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000702 }
703}
704
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
706/// when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000707void X86DAGToDAGISel::InstructionSelect() {
Dan Gohmanaf435de2009-08-01 03:42:59 +0000708 const Function *F = MF->getFunction();
Devang Patel78eba022008-10-06 18:03:39 +0000709 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000711 if (OptLevel != CodeGenOpt::None)
Dan Gohman14a66442008-08-23 02:25:05 +0000712 PreprocessForRMW();
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000713
Bill Wendling58ed5d22009-04-29 00:15:41 +0000714 // FIXME: This should only happen when not compiled with -O0.
Dan Gohman14a66442008-08-23 02:25:05 +0000715 PreprocessForFPConvert();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 // Codegen the basic block.
718#ifndef NDEBUG
David Greeneb592f222010-01-05 01:29:08 +0000719 DEBUG(dbgs() << "===== Instruction selection begins:\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 Indent = 0;
721#endif
David Greene932618b2008-10-27 21:56:29 +0000722 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723#ifndef NDEBUG
David Greeneb592f222010-01-05 01:29:08 +0000724 DEBUG(dbgs() << "===== Instruction selection ends:\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725#endif
726
Dan Gohman14a66442008-08-23 02:25:05 +0000727 CurDAG->RemoveDeadNodes();
Evan Cheng34fd4f32008-06-30 20:45:06 +0000728}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000730/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
731/// the main function.
732void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
733 MachineFrameInfo *MFI) {
734 const TargetInstrInfo *TII = TM.getInstrInfo();
735 if (Subtarget->isTargetCygMing())
Dale Johannesen960bfbd2009-02-13 02:33:27 +0000736 BuildMI(BB, DebugLoc::getUnknownLoc(),
737 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov34ef31e2007-09-25 21:52:30 +0000738}
739
740void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
741 // If this is main, emit special code for main.
742 MachineBasicBlock *BB = MF.begin();
743 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
744 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
745}
746
Rafael Espindolabca99f72009-04-08 21:14:34 +0000747
748bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
749 X86ISelAddressMode &AM) {
750 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
751 SDValue Segment = N.getOperand(0);
752
753 if (AM.Segment.getNode() == 0) {
754 AM.Segment = Segment;
755 return false;
756 }
757
758 return true;
759}
760
761bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
762 // This optimization is valid because the GNU TLS model defines that
763 // gs:0 (or fs:0 on X86-64) contains its own address.
764 // For more information see http://people.redhat.com/drepper/tls.pdf
765
766 SDValue Address = N.getOperand(1);
767 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
768 !MatchSegmentBaseAddress (Address, AM))
769 return false;
770
771 return true;
772}
773
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000774/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
775/// into an addressing mode. These wrap things that will resolve down into a
776/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000777/// returns false.
Rafael Espindola84218fe2009-04-12 21:55:03 +0000778bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000779 // If the addressing mode already has a symbol as the displacement, we can
780 // never match another symbol.
Rafael Espindola84218fe2009-04-12 21:55:03 +0000781 if (AM.hasSymbolicDisplacement())
782 return true;
Rafael Espindola84218fe2009-04-12 21:55:03 +0000783
784 SDValue N0 = N.getOperand(0);
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000785 CodeModel::Model M = TM.getCodeModel();
786
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000787 // Handle X86-64 rip-relative addresses. We check this before checking direct
788 // folding because RIP is preferable to non-RIP accesses.
789 if (Subtarget->is64Bit() &&
790 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
791 // they cannot be folded into immediate fields.
792 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov20076742009-08-21 15:41:56 +0000793 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000794 // Base and index reg must be 0 in order to use %rip as base and lowering
795 // must allow RIP.
796 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000797 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
798 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000799 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000800 AM.GV = G->getGlobal();
801 AM.Disp = Offset;
Chris Lattner8969a732009-06-26 05:51:45 +0000802 AM.SymbolFlags = G->getTargetFlags();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000803 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
804 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000805 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola84218fe2009-04-12 21:55:03 +0000806 AM.CP = CP->getConstVal();
807 AM.Align = CP->getAlignment();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000808 AM.Disp = Offset;
Chris Lattner998c2052009-06-26 05:56:49 +0000809 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000810 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
811 AM.ES = S->getSymbol();
812 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner03796ee2009-11-01 03:25:03 +0000813 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000814 AM.JT = J->getIndex();
815 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner03796ee2009-11-01 03:25:03 +0000816 } else {
817 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman885793b2009-11-20 23:18:13 +0000818 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola84218fe2009-04-12 21:55:03 +0000819 }
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000820
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000821 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000822 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola84218fe2009-04-12 21:55:03 +0000823 return false;
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000824 }
825
826 // Handle the case when globals fit in our immediate field: This is true for
827 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
828 // mode, this results in a non-RIP-relative computation.
829 if (!Subtarget->is64Bit() ||
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000830 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000831 TM.getRelocationModel() == Reloc::Static)) {
832 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
833 AM.GV = G->getGlobal();
834 AM.Disp += G->getOffset();
835 AM.SymbolFlags = G->getTargetFlags();
836 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
837 AM.CP = CP->getConstVal();
838 AM.Align = CP->getAlignment();
839 AM.Disp += CP->getOffset();
840 AM.SymbolFlags = CP->getTargetFlags();
841 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
842 AM.ES = S->getSymbol();
843 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner03796ee2009-11-01 03:25:03 +0000844 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000845 AM.JT = J->getIndex();
846 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner03796ee2009-11-01 03:25:03 +0000847 } else {
848 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman885793b2009-11-20 23:18:13 +0000849 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000850 }
Rafael Espindola84218fe2009-04-12 21:55:03 +0000851 return false;
852 }
853
854 return true;
855}
856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857/// MatchAddress - Add the specified node to the specified addressing mode,
858/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner7f06edd2007-12-08 07:22:58 +0000859/// addressing mode.
Dan Gohman3dffbbf2009-07-22 23:26:55 +0000860bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
861 if (MatchAddressRecursively(N, AM, 0))
862 return true;
863
864 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
865 // a smaller encoding and avoids a scaled-index.
866 if (AM.Scale == 2 &&
867 AM.BaseType == X86ISelAddressMode::RegBase &&
868 AM.Base.Reg.getNode() == 0) {
869 AM.Base.Reg = AM.IndexReg;
870 AM.Scale = 1;
871 }
872
Dan Gohman728fd182009-08-20 18:23:44 +0000873 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
874 // because it has a smaller encoding.
875 // TODO: Which other code models can use this?
876 if (TM.getCodeModel() == CodeModel::Small &&
877 Subtarget->is64Bit() &&
878 AM.Scale == 1 &&
879 AM.BaseType == X86ISelAddressMode::RegBase &&
880 AM.Base.Reg.getNode() == 0 &&
881 AM.IndexReg.getNode() == 0 &&
Dan Gohman814c2ea2009-08-25 17:47:44 +0000882 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohman728fd182009-08-20 18:23:44 +0000883 AM.hasSymbolicDisplacement())
884 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
885
Dan Gohman3dffbbf2009-07-22 23:26:55 +0000886 return false;
887}
888
889bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
890 unsigned Depth) {
Dan Gohman36322c72008-10-18 02:06:02 +0000891 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +0000892 DebugLoc dl = N.getDebugLoc();
Bill Wendlingc551eff2009-08-07 21:33:25 +0000893 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +0000894 dbgs() << "MatchAddress: ";
Bill Wendlingc551eff2009-08-07 21:33:25 +0000895 AM.dump();
896 });
Dan Gohmana60c1b32007-08-13 20:03:06 +0000897 // Limit recursion.
898 if (Depth > 5)
Rafael Espindola515c13e2009-03-31 16:16:57 +0000899 return MatchAddressBase(N, AM);
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000900
901 CodeModel::Model M = TM.getCodeModel();
902
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000903 // If this is already a %rip relative address, we can only merge immediates
904 // into it. Instead of handling this in every case, we handle it here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000906 if (AM.isRIPRelative()) {
907 // FIXME: JumpTable and ExternalSymbol address currently don't like
908 // displacements. It isn't very important, but this should be fixed for
909 // consistency.
910 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000911
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000912 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
913 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000914 if (X86::isOffsetSuitableForCodeModel(Val, M,
915 AM.hasSymbolicDisplacement())) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000916 AM.Disp = Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 return false;
918 }
919 }
920 return true;
921 }
922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 switch (N.getOpcode()) {
924 default: break;
925 case ISD::Constant: {
Dan Gohman0bd76b72008-11-11 15:52:29 +0000926 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000927 if (!is64Bit ||
928 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
929 AM.hasSymbolicDisplacement())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 AM.Disp += Val;
931 return false;
932 }
933 break;
934 }
935
Rafael Espindolabca99f72009-04-08 21:14:34 +0000936 case X86ISD::SegmentBaseAddress:
937 if (!MatchSegmentBaseAddress(N, AM))
938 return false;
939 break;
940
Rafael Espindola84218fe2009-04-12 21:55:03 +0000941 case X86ISD::Wrapper:
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000942 case X86ISD::WrapperRIP:
Rafael Espindola84218fe2009-04-12 21:55:03 +0000943 if (!MatchWrapper(N, AM))
944 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946
Rafael Espindolabca99f72009-04-08 21:14:34 +0000947 case ISD::LOAD:
948 if (!MatchLoad(N, AM))
949 return false;
950 break;
951
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 case ISD::FrameIndex:
Gabor Greife9f7f582008-08-31 15:37:04 +0000953 if (AM.BaseType == X86ISelAddressMode::RegBase
954 && AM.Base.Reg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
956 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
957 return false;
958 }
959 break;
960
961 case ISD::SHL:
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000962 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner7f06edd2007-12-08 07:22:58 +0000963 break;
964
Gabor Greife9f7f582008-08-31 15:37:04 +0000965 if (ConstantSDNode
966 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000967 unsigned Val = CN->getZExtValue();
Dan Gohman3dffbbf2009-07-22 23:26:55 +0000968 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
969 // that the base operand remains free for further matching. If
970 // the base doesn't end up getting used, a post-processing step
971 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner7f06edd2007-12-08 07:22:58 +0000972 if (Val == 1 || Val == 2 || Val == 3) {
973 AM.Scale = 1 << Val;
Gabor Greif1c80d112008-08-28 21:40:38 +0000974 SDValue ShVal = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975
Chris Lattner7f06edd2007-12-08 07:22:58 +0000976 // Okay, we know that we have a scale by now. However, if the scaled
977 // value is an add of something and a constant, we can fold the
978 // constant into the disp field here.
Dan Gohmand7140f12010-01-21 02:09:26 +0000979 if (ShVal.getNode()->getOpcode() == ISD::ADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000980 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
981 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner7f06edd2007-12-08 07:22:58 +0000982 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +0000983 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +0000984 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovc283e152009-08-05 23:01:26 +0000985 if (!is64Bit ||
986 X86::isOffsetSuitableForCodeModel(Disp, M,
987 AM.hasSymbolicDisplacement()))
Chris Lattner7f06edd2007-12-08 07:22:58 +0000988 AM.Disp = Disp;
989 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 AM.IndexReg = ShVal;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000991 } else {
992 AM.IndexReg = ShVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 }
Chris Lattner7f06edd2007-12-08 07:22:58 +0000994 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 }
996 break;
Chris Lattner7f06edd2007-12-08 07:22:58 +0000997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998
Dan Gohman35b99222007-10-22 20:22:24 +0000999 case ISD::SMUL_LOHI:
1000 case ISD::UMUL_LOHI:
1001 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif46bf5472008-08-26 22:36:50 +00001002 if (N.getResNo() != 0) break;
Dan Gohman35b99222007-10-22 20:22:24 +00001003 // FALL THROUGH
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 case ISD::MUL:
Evan Chengc3495762009-03-30 21:36:47 +00001005 case X86ISD::MUL_IMM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmancc3df852008-11-05 04:14:16 +00001007 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001008 AM.Base.Reg.getNode() == 0 &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001009 AM.IndexReg.getNode() == 0) {
Gabor Greife9f7f582008-08-31 15:37:04 +00001010 if (ConstantSDNode
1011 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001012 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
1013 CN->getZExtValue() == 9) {
1014 AM.Scale = unsigned(CN->getZExtValue())-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015
Gabor Greif1c80d112008-08-28 21:40:38 +00001016 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001017 SDValue Reg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018
1019 // Okay, we know that we have a scale by now. However, if the scaled
1020 // value is an add of something and a constant, we can fold the
1021 // constant into the disp field here.
Gabor Greif1c80d112008-08-28 21:40:38 +00001022 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
1023 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
1024 Reg = MulVal.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 ConstantSDNode *AddVal =
Gabor Greif1c80d112008-08-28 21:40:38 +00001026 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng2ed6f342009-01-17 07:09:27 +00001027 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001028 CN->getZExtValue();
Anton Korobeynikovc283e152009-08-05 23:01:26 +00001029 if (!is64Bit ||
1030 X86::isOffsetSuitableForCodeModel(Disp, M,
1031 AM.hasSymbolicDisplacement()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 AM.Disp = Disp;
1033 else
Gabor Greif1c80d112008-08-28 21:40:38 +00001034 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 } else {
Gabor Greif1c80d112008-08-28 21:40:38 +00001036 Reg = N.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 }
1038
1039 AM.IndexReg = AM.Base.Reg = Reg;
1040 return false;
1041 }
1042 }
1043 break;
1044
Dan Gohman946223f2009-05-11 18:02:53 +00001045 case ISD::SUB: {
1046 // Given A-B, if A can be completely folded into the address and
1047 // the index field with the index field unused, use -B as the index.
1048 // This is a win if a has multiple parts that can be folded into
1049 // the address. Also, this saves a mov if the base register has
1050 // other uses, since it avoids a two-address sub instruction, however
1051 // it costs an additional mov if the index register has other uses.
1052
1053 // Test if the LHS of the sub can be folded.
1054 X86ISelAddressMode Backup = AM;
Dan Gohman3dffbbf2009-07-22 23:26:55 +00001055 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman946223f2009-05-11 18:02:53 +00001056 AM = Backup;
1057 break;
1058 }
1059 // Test if the index field is free for use.
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001060 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman946223f2009-05-11 18:02:53 +00001061 AM = Backup;
1062 break;
1063 }
1064 int Cost = 0;
1065 SDValue RHS = N.getNode()->getOperand(1);
1066 // If the RHS involves a register with multiple uses, this
1067 // transformation incurs an extra mov, due to the neg instruction
1068 // clobbering its operand.
1069 if (!RHS.getNode()->hasOneUse() ||
1070 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1071 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1072 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1073 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001074 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman946223f2009-05-11 18:02:53 +00001075 ++Cost;
1076 // If the base is a register with multiple uses, this
1077 // transformation may save a mov.
1078 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1079 AM.Base.Reg.getNode() &&
1080 !AM.Base.Reg.getNode()->hasOneUse()) ||
1081 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1082 --Cost;
1083 // If the folded LHS was interesting, this transformation saves
1084 // address arithmetic.
1085 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1086 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1087 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1088 --Cost;
1089 // If it doesn't look like it may be an overall win, don't do it.
1090 if (Cost >= 0) {
1091 AM = Backup;
1092 break;
1093 }
1094
1095 // Ok, the transformation is legal and appears profitable. Go for it.
1096 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1097 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1098 AM.IndexReg = Neg;
1099 AM.Scale = 1;
1100
1101 // Insert the new nodes into the topological ordering.
1102 if (Zero.getNode()->getNodeId() == -1 ||
1103 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1104 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1105 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1106 }
1107 if (Neg.getNode()->getNodeId() == -1 ||
1108 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1109 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1110 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1111 }
1112 return false;
1113 }
1114
Evan Cheng2ed6f342009-01-17 07:09:27 +00001115 case ISD::ADD: {
1116 X86ISelAddressMode Backup = AM;
Dan Gohman3dffbbf2009-07-22 23:26:55 +00001117 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1118 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng2ed6f342009-01-17 07:09:27 +00001119 return false;
1120 AM = Backup;
Dan Gohman3dffbbf2009-07-22 23:26:55 +00001121 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1122 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng2ed6f342009-01-17 07:09:27 +00001123 return false;
1124 AM = Backup;
Dan Gohman3ae92482009-03-13 02:25:09 +00001125
1126 // If we couldn't fold both operands into the address at the same time,
1127 // see if we can just put each operand into a register and fold at least
1128 // the add.
1129 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1130 !AM.Base.Reg.getNode() &&
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001131 !AM.IndexReg.getNode()) {
Dan Gohman3ae92482009-03-13 02:25:09 +00001132 AM.Base.Reg = N.getNode()->getOperand(0);
1133 AM.IndexReg = N.getNode()->getOperand(1);
1134 AM.Scale = 1;
1135 return false;
1136 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 break;
Evan Cheng2ed6f342009-01-17 07:09:27 +00001138 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139
1140 case ISD::OR:
1141 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner7f06edd2007-12-08 07:22:58 +00001142 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1143 X86ISelAddressMode Backup = AM;
Dan Gohman0bd76b72008-11-11 15:52:29 +00001144 uint64_t Offset = CN->getSExtValue();
Chris Lattner7f06edd2007-12-08 07:22:58 +00001145 // Start with the LHS as an addr mode.
Dan Gohman3dffbbf2009-07-22 23:26:55 +00001146 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +00001147 // Address could not have picked a GV address for the displacement.
1148 AM.GV == NULL &&
1149 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00001150 (!is64Bit ||
1151 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1152 AM.hasSymbolicDisplacement())) &&
Chris Lattner7f06edd2007-12-08 07:22:58 +00001153 // Check to see if the LHS & C is zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00001154 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman0bd76b72008-11-11 15:52:29 +00001155 AM.Disp += Offset;
Chris Lattner7f06edd2007-12-08 07:22:58 +00001156 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 }
Chris Lattner7f06edd2007-12-08 07:22:58 +00001158 AM = Backup;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 }
1160 break;
Evan Chengf2abee72007-12-13 00:43:27 +00001161
1162 case ISD::AND: {
Dan Gohman744d4622009-04-13 16:09:41 +00001163 // Perform some heroic transforms on an and of a constant-count shift
1164 // with a constant to enable use of the scaled offset field.
1165
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SDValue Shift = N.getOperand(0);
Dan Gohman744d4622009-04-13 16:09:41 +00001167 if (Shift.getNumOperands() != 2) break;
Dan Gohmancc3df852008-11-05 04:14:16 +00001168
Evan Chengf2abee72007-12-13 00:43:27 +00001169 // Scale must not be used already.
Gabor Greif1c80d112008-08-28 21:40:38 +00001170 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Cheng3b5a1272008-02-07 08:53:49 +00001171
Dan Gohman744d4622009-04-13 16:09:41 +00001172 SDValue X = Shift.getOperand(0);
Evan Chengf2abee72007-12-13 00:43:27 +00001173 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1174 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1175 if (!C1 || !C2) break;
1176
Dan Gohman744d4622009-04-13 16:09:41 +00001177 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1178 // allows us to convert the shift and and into an h-register extract and
1179 // a scaled index.
1180 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1181 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindolaa4464662009-04-16 12:34:53 +00001182 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman744d4622009-04-13 16:09:41 +00001183 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001184 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman744d4622009-04-13 16:09:41 +00001185 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1186 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1187 X, Eight);
1188 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1189 Srl, Mask);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001190 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman3dc69fd2009-04-14 22:45:05 +00001191 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1192 And, ShlCount);
Dan Gohman744d4622009-04-13 16:09:41 +00001193
1194 // Insert the new nodes into the topological ordering.
1195 if (Eight.getNode()->getNodeId() == -1 ||
1196 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1197 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1198 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1199 }
1200 if (Mask.getNode()->getNodeId() == -1 ||
1201 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1202 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1203 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1204 }
1205 if (Srl.getNode()->getNodeId() == -1 ||
1206 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1207 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1208 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1209 }
1210 if (And.getNode()->getNodeId() == -1 ||
1211 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1212 CurDAG->RepositionNode(N.getNode(), And.getNode());
1213 And.getNode()->setNodeId(N.getNode()->getNodeId());
1214 }
Dan Gohman3dc69fd2009-04-14 22:45:05 +00001215 if (ShlCount.getNode()->getNodeId() == -1 ||
1216 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1217 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1218 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1219 }
1220 if (Shl.getNode()->getNodeId() == -1 ||
1221 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1222 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1223 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1224 }
1225 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman744d4622009-04-13 16:09:41 +00001226 AM.IndexReg = And;
1227 AM.Scale = (1 << ScaleLog);
1228 return false;
1229 }
1230 }
1231
1232 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1233 // allows us to fold the shift into this addressing mode.
1234 if (Shift.getOpcode() != ISD::SHL) break;
1235
Evan Chengf2abee72007-12-13 00:43:27 +00001236 // Not likely to be profitable if either the AND or SHIFT node has more
1237 // than one use (unless all uses are for address computation). Besides,
1238 // isel mechanism requires their node ids to be reused.
1239 if (!N.hasOneUse() || !Shift.hasOneUse())
1240 break;
1241
1242 // Verify that the shift amount is something we can fold.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001243 unsigned ShiftCst = C1->getZExtValue();
Evan Chengf2abee72007-12-13 00:43:27 +00001244 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1245 break;
1246
1247 // Get the new AND mask, this folds to a constant.
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001248 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng07d091a2008-10-14 17:15:39 +00001249 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001250 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1251 NewANDMask);
1252 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman3666f472008-10-13 20:52:04 +00001253 NewAND, SDValue(C1, 0));
Dan Gohmancc3df852008-11-05 04:14:16 +00001254
1255 // Insert the new nodes into the topological ordering.
1256 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1257 CurDAG->RepositionNode(X.getNode(), C1);
1258 C1->setNodeId(X.getNode()->getNodeId());
1259 }
1260 if (NewANDMask.getNode()->getNodeId() == -1 ||
1261 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1262 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1263 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1264 }
1265 if (NewAND.getNode()->getNodeId() == -1 ||
1266 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1267 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1268 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1269 }
1270 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1271 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1272 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1273 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1274 }
1275
Dan Gohman3666f472008-10-13 20:52:04 +00001276 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Chengf2abee72007-12-13 00:43:27 +00001277
1278 AM.Scale = 1 << ShiftCst;
1279 AM.IndexReg = NewAND;
1280 return false;
1281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 }
1283
Rafael Espindola515c13e2009-03-31 16:16:57 +00001284 return MatchAddressBase(N, AM);
Dan Gohmana60c1b32007-08-13 20:03:06 +00001285}
1286
1287/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1288/// specified addressing mode without any further recursion.
Rafael Espindola515c13e2009-03-31 16:16:57 +00001289bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 // Is the base register already occupied?
Gabor Greif1c80d112008-08-28 21:40:38 +00001291 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 // If so, check to see if the scale index register is set.
Chris Lattnerdc6fc472009-06-27 04:16:01 +00001293 if (AM.IndexReg.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 AM.IndexReg = N;
1295 AM.Scale = 1;
1296 return false;
1297 }
1298
1299 // Otherwise, we cannot select it.
1300 return true;
1301 }
1302
1303 // Default, generate it as a register.
1304 AM.BaseType = X86ISelAddressMode::RegBase;
1305 AM.Base.Reg = N;
1306 return false;
1307}
1308
1309/// SelectAddr - returns true if it is able pattern match an addressing mode.
1310/// It returns the operands which make up the maximal addressing mode it can
1311/// match by reference.
Dan Gohman5f082a72010-01-05 01:24:18 +00001312bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman8181bd12008-07-27 21:46:04 +00001313 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001314 SDValue &Disp, SDValue &Segment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 X86ISelAddressMode AM;
Evan Chengd22f6c92009-12-18 01:59:21 +00001316 if (MatchAddress(N, AM))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 return false;
1318
Owen Andersonac9de032009-08-10 22:56:29 +00001319 EVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001321 if (!AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 AM.Base.Reg = CurDAG->getRegister(0, VT);
1323 }
1324
Gabor Greif1c80d112008-08-28 21:40:38 +00001325 if (!AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 AM.IndexReg = CurDAG->getRegister(0, VT);
1327
Rafael Espindolabca99f72009-04-08 21:14:34 +00001328 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329 return true;
1330}
1331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1333/// match a load whose top elements are either undef or zeros. The load flavor
1334/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner52b513d2010-02-17 06:07:47 +00001335///
1336/// We also return:
Chris Lattnerb4d19fc2010-02-21 03:17:59 +00001337/// PatternChainNode: this is the matched node that has a chain input and
1338/// output.
1339bool X86DAGToDAGISel::SelectScalarSSELoadXXX(SDNode *Root,
Dan Gohman8181bd12008-07-27 21:46:04 +00001340 SDValue N, SDValue &Base,
1341 SDValue &Scale, SDValue &Index,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001342 SDValue &Disp, SDValue &Segment,
Chris Lattnerb4d19fc2010-02-21 03:17:59 +00001343 SDValue &PatternNodeWithChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnerb4d19fc2010-02-21 03:17:59 +00001345 PatternNodeWithChain = N.getOperand(0);
1346 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1347 PatternNodeWithChain.hasOneUse() &&
1348 IsProfitableToFold(N.getOperand(0), PatternNodeWithChain.getNode(),
1349 Root) &&
1350 IsLegalToFold(N.getOperand(0), PatternNodeWithChain.getNode(), Root)) {
1351 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattnerecbbf492010-02-16 22:35:06 +00001352 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 return true;
1355 }
1356 }
1357
1358 // Also handle the case where we explicitly require zeros in the top
1359 // elements. This is a vector shuffle from the zero vector.
Gabor Greif1c80d112008-08-28 21:40:38 +00001360 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattnere6aa3862007-11-25 00:24:49 +00001361 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng40ee6e52008-05-08 00:57:18 +00001362 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001363 N.getOperand(0).getNode()->hasOneUse() &&
1364 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattnerecbbf492010-02-16 22:35:06 +00001365 N.getOperand(0).getOperand(0).hasOneUse() &&
1366 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1367 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00001368 // Okay, this is a zero extending load. Fold it.
1369 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattnerecbbf492010-02-16 22:35:06 +00001370 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng40ee6e52008-05-08 00:57:18 +00001371 return false;
Chris Lattnerb4d19fc2010-02-21 03:17:59 +00001372 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng40ee6e52008-05-08 00:57:18 +00001373 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 }
1375 return false;
1376}
1377
1378
1379/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1380/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman5f082a72010-01-05 01:24:18 +00001381bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman8181bd12008-07-27 21:46:04 +00001382 SDValue &Base, SDValue &Scale,
1383 SDValue &Index, SDValue &Disp) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 X86ISelAddressMode AM;
Rafael Espindola7682b9c2009-04-10 10:09:34 +00001385
1386 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1387 // segments.
1388 SDValue Copy = AM.Segment;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001389 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindola7682b9c2009-04-10 10:09:34 +00001390 AM.Segment = T;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 if (MatchAddress(N, AM))
1392 return false;
Rafael Espindola7682b9c2009-04-10 10:09:34 +00001393 assert (T == AM.Segment);
1394 AM.Segment = Copy;
Rafael Espindolabca99f72009-04-08 21:14:34 +00001395
Owen Andersonac9de032009-08-10 22:56:29 +00001396 EVT VT = N.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 unsigned Complexity = 0;
1398 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greif1c80d112008-08-28 21:40:38 +00001399 if (AM.Base.Reg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400 Complexity = 1;
1401 else
1402 AM.Base.Reg = CurDAG->getRegister(0, VT);
1403 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1404 Complexity = 4;
1405
Gabor Greif1c80d112008-08-28 21:40:38 +00001406 if (AM.IndexReg.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407 Complexity++;
1408 else
1409 AM.IndexReg = CurDAG->getRegister(0, VT);
1410
1411 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1412 // a simple shift.
1413 if (AM.Scale > 1)
1414 Complexity++;
1415
1416 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1417 // to a LEA. This is determined with some expermentation but is by no means
1418 // optimal (especially for code size consideration). LEA is nice because of
1419 // its three-address nature. Tweak the cost function again when we can run
1420 // convertToThreeAddress() at register allocation time.
Dan Gohman245791b2009-02-07 00:43:41 +00001421 if (AM.hasSymbolicDisplacement()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 // For X86-64, we should always use lea to materialize RIP relative
1423 // addresses.
1424 if (Subtarget->is64Bit())
1425 Complexity = 4;
1426 else
1427 Complexity += 2;
1428 }
1429
Gabor Greif1c80d112008-08-28 21:40:38 +00001430 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 Complexity++;
1432
Chris Lattner0d2dad62009-07-11 22:50:33 +00001433 // If it isn't worth using an LEA, reject it.
Chris Lattner57a80252009-07-11 23:07:30 +00001434 if (Complexity <= 2)
Chris Lattner0d2dad62009-07-11 22:50:33 +00001435 return false;
1436
1437 SDValue Segment;
1438 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1439 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440}
1441
Chris Lattnerf1940742009-06-20 20:38:48 +00001442/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohman5f082a72010-01-05 01:24:18 +00001443bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattnerf1940742009-06-20 20:38:48 +00001444 SDValue &Scale, SDValue &Index,
1445 SDValue &Disp) {
Chris Lattnerf1940742009-06-20 20:38:48 +00001446 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1447 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1448
1449 X86ISelAddressMode AM;
1450 AM.GV = GA->getGlobal();
1451 AM.Disp += GA->getOffset();
1452 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerafab6592009-06-26 21:18:37 +00001453 AM.SymbolFlags = GA->getTargetFlags();
1454
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001455 if (N.getValueType() == MVT::i32) {
Chris Lattnerf1940742009-06-20 20:38:48 +00001456 AM.Scale = 1;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001457 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattnerf1940742009-06-20 20:38:48 +00001458 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001459 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattnerf1940742009-06-20 20:38:48 +00001460 }
1461
1462 SDValue Segment;
1463 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1464 return true;
1465}
1466
1467
Dan Gohman5f082a72010-01-05 01:24:18 +00001468bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman8181bd12008-07-27 21:46:04 +00001469 SDValue &Base, SDValue &Scale,
Rafael Espindolabca99f72009-04-08 21:14:34 +00001470 SDValue &Index, SDValue &Disp,
1471 SDValue &Segment) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001472 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Chengf80681e2010-02-15 19:41:07 +00001473 IsProfitableToFold(N, P, P) &&
1474 IsLegalToFold(N, P, P))
Rafael Espindolabca99f72009-04-08 21:14:34 +00001475 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 return false;
1477}
1478
Dan Gohmanb60482f2008-09-23 18:22:58 +00001479/// getGlobalBaseReg - Return an SDNode that returns the value of
1480/// the global base register. Output instructions required to
1481/// initialize the global base register, if necessary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482///
1483SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman4261ed32009-06-03 20:20:00 +00001484 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greif1c80d112008-08-28 21:40:38 +00001485 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486}
1487
1488static SDNode *FindCallStartFromCall(SDNode *Node) {
1489 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001490 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 "Node doesn't have a token chain argument!");
Gabor Greif1c80d112008-08-28 21:40:38 +00001492 return FindCallStartFromCall(Node->getOperand(0).getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493}
1494
Dale Johannesenf160d802008-10-02 18:53:47 +00001495SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1496 SDValue Chain = Node->getOperand(0);
1497 SDValue In1 = Node->getOperand(1);
1498 SDValue In2L = Node->getOperand(2);
1499 SDValue In2H = Node->getOperand(3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00001500 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohman5f082a72010-01-05 01:24:18 +00001501 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesenf160d802008-10-02 18:53:47 +00001502 return NULL;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001503 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1504 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1505 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1506 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1507 MVT::i32, MVT::i32, MVT::Other, Ops,
1508 array_lengthof(Ops));
1509 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1510 return ResNode;
Dale Johannesenf160d802008-10-02 18:53:47 +00001511}
Christopher Lamb0a7c8662007-08-10 21:48:46 +00001512
Owen Andersonac9de032009-08-10 22:56:29 +00001513SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Chengb723fb52009-07-30 08:33:02 +00001514 if (Node->hasAnyUseOfValue(0))
1515 return 0;
1516
1517 // Optimize common patterns for __sync_add_and_fetch and
1518 // __sync_sub_and_fetch where the result is not used. This allows us
1519 // to use "lock" version of add, sub, inc, dec instructions.
1520 // FIXME: Do not use special instructions but instead add the "lock"
1521 // prefix to the target node somehow. The extra information will then be
1522 // transferred to machine instruction and it denotes the prefix.
1523 SDValue Chain = Node->getOperand(0);
1524 SDValue Ptr = Node->getOperand(1);
1525 SDValue Val = Node->getOperand(2);
1526 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohman5f082a72010-01-05 01:24:18 +00001527 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Chengb723fb52009-07-30 08:33:02 +00001528 return 0;
1529
1530 bool isInc = false, isDec = false, isSub = false, isCN = false;
1531 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1532 if (CN) {
1533 isCN = true;
1534 int64_t CNVal = CN->getSExtValue();
1535 if (CNVal == 1)
1536 isInc = true;
1537 else if (CNVal == -1)
1538 isDec = true;
1539 else if (CNVal >= 0)
1540 Val = CurDAG->getTargetConstant(CNVal, NVT);
1541 else {
1542 isSub = true;
1543 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1544 }
1545 } else if (Val.hasOneUse() &&
1546 Val.getOpcode() == ISD::SUB &&
1547 X86::isZeroNode(Val.getOperand(0))) {
1548 isSub = true;
1549 Val = Val.getOperand(1);
1550 }
1551
1552 unsigned Opc = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001553 switch (NVT.getSimpleVT().SimpleTy) {
Evan Chengb723fb52009-07-30 08:33:02 +00001554 default: return 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001555 case MVT::i8:
Evan Chengb723fb52009-07-30 08:33:02 +00001556 if (isInc)
1557 Opc = X86::LOCK_INC8m;
1558 else if (isDec)
1559 Opc = X86::LOCK_DEC8m;
1560 else if (isSub) {
1561 if (isCN)
1562 Opc = X86::LOCK_SUB8mi;
1563 else
1564 Opc = X86::LOCK_SUB8mr;
1565 } else {
1566 if (isCN)
1567 Opc = X86::LOCK_ADD8mi;
1568 else
1569 Opc = X86::LOCK_ADD8mr;
1570 }
1571 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001572 case MVT::i16:
Evan Chengb723fb52009-07-30 08:33:02 +00001573 if (isInc)
1574 Opc = X86::LOCK_INC16m;
1575 else if (isDec)
1576 Opc = X86::LOCK_DEC16m;
1577 else if (isSub) {
1578 if (isCN) {
1579 if (Predicate_i16immSExt8(Val.getNode()))
1580 Opc = X86::LOCK_SUB16mi8;
1581 else
1582 Opc = X86::LOCK_SUB16mi;
1583 } else
1584 Opc = X86::LOCK_SUB16mr;
1585 } else {
1586 if (isCN) {
1587 if (Predicate_i16immSExt8(Val.getNode()))
1588 Opc = X86::LOCK_ADD16mi8;
1589 else
1590 Opc = X86::LOCK_ADD16mi;
1591 } else
1592 Opc = X86::LOCK_ADD16mr;
1593 }
1594 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001595 case MVT::i32:
Evan Chengb723fb52009-07-30 08:33:02 +00001596 if (isInc)
1597 Opc = X86::LOCK_INC32m;
1598 else if (isDec)
1599 Opc = X86::LOCK_DEC32m;
1600 else if (isSub) {
1601 if (isCN) {
1602 if (Predicate_i32immSExt8(Val.getNode()))
1603 Opc = X86::LOCK_SUB32mi8;
1604 else
1605 Opc = X86::LOCK_SUB32mi;
1606 } else
1607 Opc = X86::LOCK_SUB32mr;
1608 } else {
1609 if (isCN) {
1610 if (Predicate_i32immSExt8(Val.getNode()))
1611 Opc = X86::LOCK_ADD32mi8;
1612 else
1613 Opc = X86::LOCK_ADD32mi;
1614 } else
1615 Opc = X86::LOCK_ADD32mr;
1616 }
1617 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001618 case MVT::i64:
Evan Chengb723fb52009-07-30 08:33:02 +00001619 if (isInc)
1620 Opc = X86::LOCK_INC64m;
1621 else if (isDec)
1622 Opc = X86::LOCK_DEC64m;
1623 else if (isSub) {
1624 Opc = X86::LOCK_SUB64mr;
1625 if (isCN) {
1626 if (Predicate_i64immSExt8(Val.getNode()))
1627 Opc = X86::LOCK_SUB64mi8;
1628 else if (Predicate_i64immSExt32(Val.getNode()))
1629 Opc = X86::LOCK_SUB64mi32;
1630 }
1631 } else {
1632 Opc = X86::LOCK_ADD64mr;
1633 if (isCN) {
1634 if (Predicate_i64immSExt8(Val.getNode()))
1635 Opc = X86::LOCK_ADD64mi8;
1636 else if (Predicate_i64immSExt32(Val.getNode()))
1637 Opc = X86::LOCK_ADD64mi32;
1638 }
1639 }
1640 break;
1641 }
1642
1643 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4052b292010-02-09 19:54:29 +00001644 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman61fda0d2009-09-25 18:54:59 +00001645 dl, NVT), 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001646 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1647 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Chengb723fb52009-07-30 08:33:02 +00001648 if (isInc || isDec) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001649 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1650 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1651 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Chengb723fb52009-07-30 08:33:02 +00001652 SDValue RetVals[] = { Undef, Ret };
1653 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1654 } else {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001655 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1656 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1657 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Chengb723fb52009-07-30 08:33:02 +00001658 SDValue RetVals[] = { Undef, Ret };
1659 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1660 }
1661}
1662
Dan Gohmanf9a38e92009-10-09 20:35:19 +00001663/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1664/// any uses which require the SF or OF bits to be accurate.
1665static bool HasNoSignedComparisonUses(SDNode *N) {
1666 // Examine each user of the node.
1667 for (SDNode::use_iterator UI = N->use_begin(),
1668 UE = N->use_end(); UI != UE; ++UI) {
1669 // Only examine CopyToReg uses.
1670 if (UI->getOpcode() != ISD::CopyToReg)
1671 return false;
1672 // Only examine CopyToReg uses that copy to EFLAGS.
1673 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1674 X86::EFLAGS)
1675 return false;
1676 // Examine each user of the CopyToReg use.
1677 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1678 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1679 // Only examine the Flag result.
1680 if (FlagUI.getUse().getResNo() != 1) continue;
1681 // Anything unusual: assume conservatively.
1682 if (!FlagUI->isMachineOpcode()) return false;
1683 // Examine the opcode of the user.
1684 switch (FlagUI->getMachineOpcode()) {
1685 // These comparisons don't treat the most significant bit specially.
1686 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1687 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1688 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1689 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerb112c022010-02-11 19:25:55 +00001690 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1691 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohmanf9a38e92009-10-09 20:35:19 +00001692 case X86::CMOVA16rr: case X86::CMOVA16rm:
1693 case X86::CMOVA32rr: case X86::CMOVA32rm:
1694 case X86::CMOVA64rr: case X86::CMOVA64rm:
1695 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1696 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1697 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1698 case X86::CMOVB16rr: case X86::CMOVB16rm:
1699 case X86::CMOVB32rr: case X86::CMOVB32rm:
1700 case X86::CMOVB64rr: case X86::CMOVB64rm:
1701 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1702 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1703 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1704 case X86::CMOVE16rr: case X86::CMOVE16rm:
1705 case X86::CMOVE32rr: case X86::CMOVE32rm:
1706 case X86::CMOVE64rr: case X86::CMOVE64rm:
1707 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1708 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1709 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1710 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1711 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1712 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1713 case X86::CMOVP16rr: case X86::CMOVP16rm:
1714 case X86::CMOVP32rr: case X86::CMOVP32rm:
1715 case X86::CMOVP64rr: case X86::CMOVP64rm:
1716 continue;
1717 // Anything else: assume conservatively.
1718 default: return false;
1719 }
1720 }
1721 }
1722 return true;
1723}
1724
Dan Gohman5f082a72010-01-05 01:24:18 +00001725SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersonac9de032009-08-10 22:56:29 +00001726 EVT NVT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 unsigned Opc, MOpc;
1728 unsigned Opcode = Node->getOpcode();
Dale Johannesen59b5a5c2009-02-03 21:48:12 +00001729 DebugLoc dl = Node->getDebugLoc();
1730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00001732 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00001733 dbgs() << std::string(Indent, ' ') << "Selecting: ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00001734 Node->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00001735 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00001736 });
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 Indent += 2;
1738#endif
1739
Dan Gohmanbd68c792008-07-17 19:10:17 +00001740 if (Node->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00001742 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00001743 dbgs() << std::string(Indent-2, ' ') << "== ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00001744 Node->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00001745 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00001746 });
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 Indent -= 2;
1748#endif
1749 return NULL; // Already selected.
1750 }
1751
1752 switch (Opcode) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001753 default: break;
1754 case X86ISD::GlobalBaseReg:
1755 return getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756
Dan Gohman020c39b2009-08-02 16:10:52 +00001757 case X86ISD::ATOMOR64_DAG:
1758 return SelectAtomic64(Node, X86::ATOMOR6432);
1759 case X86ISD::ATOMXOR64_DAG:
1760 return SelectAtomic64(Node, X86::ATOMXOR6432);
1761 case X86ISD::ATOMADD64_DAG:
1762 return SelectAtomic64(Node, X86::ATOMADD6432);
1763 case X86ISD::ATOMSUB64_DAG:
1764 return SelectAtomic64(Node, X86::ATOMSUB6432);
1765 case X86ISD::ATOMNAND64_DAG:
1766 return SelectAtomic64(Node, X86::ATOMNAND6432);
1767 case X86ISD::ATOMAND64_DAG:
1768 return SelectAtomic64(Node, X86::ATOMAND6432);
1769 case X86ISD::ATOMSWAP64_DAG:
1770 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesenf160d802008-10-02 18:53:47 +00001771
Dan Gohman020c39b2009-08-02 16:10:52 +00001772 case ISD::ATOMIC_LOAD_ADD: {
1773 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1774 if (RetVal)
1775 return RetVal;
1776 break;
1777 }
1778
1779 case ISD::SMUL_LOHI:
1780 case ISD::UMUL_LOHI: {
1781 SDValue N0 = Node->getOperand(0);
1782 SDValue N1 = Node->getOperand(1);
1783
1784 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendlingc551eff2009-08-07 21:33:25 +00001785 if (!isSigned) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001786 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001787 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001788 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1789 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1790 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1791 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman020c39b2009-08-02 16:10:52 +00001792 }
Bill Wendlingc551eff2009-08-07 21:33:25 +00001793 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001794 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001795 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001796 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1797 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1798 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1799 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman020c39b2009-08-02 16:10:52 +00001800 }
Bill Wendlingc551eff2009-08-07 21:33:25 +00001801 }
Dan Gohman020c39b2009-08-02 16:10:52 +00001802
1803 unsigned LoReg, HiReg;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001804 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001805 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001806 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1807 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1808 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1809 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman020c39b2009-08-02 16:10:52 +00001810 }
1811
1812 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohman5f082a72010-01-05 01:24:18 +00001813 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendlingc551eff2009-08-07 21:33:25 +00001814 // Multiply is commmutative.
Dan Gohman020c39b2009-08-02 16:10:52 +00001815 if (!foldedLoad) {
Dan Gohman5f082a72010-01-05 01:24:18 +00001816 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman020c39b2009-08-02 16:10:52 +00001817 if (foldedLoad)
1818 std::swap(N0, N1);
1819 }
1820
1821 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1822 N0, SDValue()).getValue(1);
1823
1824 if (foldedLoad) {
1825 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1826 InFlag };
1827 SDNode *CNode =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001828 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1829 array_lengthof(Ops));
Dan Gohman020c39b2009-08-02 16:10:52 +00001830 InFlag = SDValue(CNode, 1);
1831 // Update the chain.
1832 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1833 } else {
1834 InFlag =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001835 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001836 }
1837
1838 // Copy the low half of the result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +00001839 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001840 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1841 LoReg, NVT, InFlag);
1842 InFlag = Result.getValue(2);
Dan Gohman5f082a72010-01-05 01:24:18 +00001843 ReplaceUses(SDValue(Node, 0), Result);
Dan Gohman020c39b2009-08-02 16:10:52 +00001844#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00001845 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00001846 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00001847 Result.getNode()->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00001848 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00001849 });
Dan Gohman020c39b2009-08-02 16:10:52 +00001850#endif
1851 }
1852 // Copy the high half of the result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +00001853 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001854 SDValue Result;
1855 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1856 // Prevent use of AH in a REX instruction by referencing AX instead.
1857 // Shift it down 8 bits.
1858 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001859 X86::AX, MVT::i16, InFlag);
Dan Gohman020c39b2009-08-02 16:10:52 +00001860 InFlag = Result.getValue(2);
Dan Gohman61fda0d2009-09-25 18:54:59 +00001861 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1862 Result,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001863 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001864 // Then truncate it down to i8.
Dan Gohman7ffdd432009-08-19 18:16:17 +00001865 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1866 MVT::i8, Result);
Dan Gohman020c39b2009-08-02 16:10:52 +00001867 } else {
1868 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1869 HiReg, NVT, InFlag);
1870 InFlag = Result.getValue(2);
1871 }
Dan Gohman5f082a72010-01-05 01:24:18 +00001872 ReplaceUses(SDValue(Node, 1), Result);
Dan Gohman020c39b2009-08-02 16:10:52 +00001873#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00001874 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00001875 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00001876 Result.getNode()->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00001877 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00001878 });
Dan Gohman020c39b2009-08-02 16:10:52 +00001879#endif
1880 }
1881
1882#ifndef NDEBUG
1883 Indent -= 2;
1884#endif
1885
1886 return NULL;
1887 }
1888
1889 case ISD::SDIVREM:
1890 case ISD::UDIVREM: {
1891 SDValue N0 = Node->getOperand(0);
1892 SDValue N1 = Node->getOperand(1);
1893
1894 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendlingc551eff2009-08-07 21:33:25 +00001895 if (!isSigned) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001896 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001897 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001898 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1899 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1900 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1901 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman020c39b2009-08-02 16:10:52 +00001902 }
Bill Wendlingc551eff2009-08-07 21:33:25 +00001903 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001904 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001905 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001906 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1907 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1908 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1909 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman020c39b2009-08-02 16:10:52 +00001910 }
Bill Wendlingc551eff2009-08-07 21:33:25 +00001911 }
Dan Gohman020c39b2009-08-02 16:10:52 +00001912
Chris Lattner789328d2009-12-23 01:45:04 +00001913 unsigned LoReg, HiReg, ClrReg;
Dan Gohman020c39b2009-08-02 16:10:52 +00001914 unsigned ClrOpcode, SExtOpcode;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001915 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001916 default: llvm_unreachable("Unsupported VT!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001917 case MVT::i8:
Chris Lattner789328d2009-12-23 01:45:04 +00001918 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman020c39b2009-08-02 16:10:52 +00001919 ClrOpcode = 0;
1920 SExtOpcode = X86::CBW;
1921 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001922 case MVT::i16:
Dan Gohman020c39b2009-08-02 16:10:52 +00001923 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00001924 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman020c39b2009-08-02 16:10:52 +00001925 SExtOpcode = X86::CWD;
1926 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001927 case MVT::i32:
Chris Lattner789328d2009-12-23 01:45:04 +00001928 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman020c39b2009-08-02 16:10:52 +00001929 ClrOpcode = X86::MOV32r0;
1930 SExtOpcode = X86::CDQ;
1931 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001932 case MVT::i64:
Chris Lattner789328d2009-12-23 01:45:04 +00001933 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00001934 ClrOpcode = X86::MOV64r0;
Dan Gohman020c39b2009-08-02 16:10:52 +00001935 SExtOpcode = X86::CQO;
Evan Chengb723fb52009-07-30 08:33:02 +00001936 break;
1937 }
1938
Dan Gohman020c39b2009-08-02 16:10:52 +00001939 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohman5f082a72010-01-05 01:24:18 +00001940 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman020c39b2009-08-02 16:10:52 +00001941 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman5a199552007-10-08 18:33:35 +00001942
Dan Gohman020c39b2009-08-02 16:10:52 +00001943 SDValue InFlag;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001944 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001945 // Special case for div8, just use a move with zero extension to AX to
1946 // clear the upper 8 bits (AH).
1947 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohman5f082a72010-01-05 01:24:18 +00001948 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001949 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1950 Move =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001951 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1952 MVT::Other, Ops,
1953 array_lengthof(Ops)), 0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001954 Chain = Move.getValue(1);
1955 ReplaceUses(N0.getValue(1), Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001956 } else {
Dan Gohman020c39b2009-08-02 16:10:52 +00001957 Move =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001958 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001959 Chain = CurDAG->getEntryNode();
1960 }
1961 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1962 InFlag = Chain.getValue(1);
1963 } else {
1964 InFlag =
1965 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1966 LoReg, N0, SDValue()).getValue(1);
1967 if (isSigned && !signBitIsZero) {
1968 // Sign extend the low part into the high part.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 InFlag =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001970 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001971 } else {
1972 // Zero out the high part, effectively zero extending the input.
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00001973 SDValue ClrNode =
1974 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner789328d2009-12-23 01:45:04 +00001975 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman020c39b2009-08-02 16:10:52 +00001976 ClrNode, InFlag).getValue(1);
Dan Gohman5a199552007-10-08 18:33:35 +00001977 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001978 }
Dan Gohman5a199552007-10-08 18:33:35 +00001979
Dan Gohman020c39b2009-08-02 16:10:52 +00001980 if (foldedLoad) {
1981 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1982 InFlag };
1983 SDNode *CNode =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001984 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1985 array_lengthof(Ops));
Dan Gohman020c39b2009-08-02 16:10:52 +00001986 InFlag = SDValue(CNode, 1);
1987 // Update the chain.
1988 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1989 } else {
1990 InFlag =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001991 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman020c39b2009-08-02 16:10:52 +00001992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993
Dan Gohman020c39b2009-08-02 16:10:52 +00001994 // Copy the division (low) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +00001995 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman020c39b2009-08-02 16:10:52 +00001996 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1997 LoReg, NVT, InFlag);
1998 InFlag = Result.getValue(2);
Dan Gohman5f082a72010-01-05 01:24:18 +00001999 ReplaceUses(SDValue(Node, 0), Result);
Dan Gohman020c39b2009-08-02 16:10:52 +00002000#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00002001 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00002002 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00002003 Result.getNode()->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00002004 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00002005 });
Dan Gohman020c39b2009-08-02 16:10:52 +00002006#endif
2007 }
2008 // Copy the remainder (high) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +00002009 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman020c39b2009-08-02 16:10:52 +00002010 SDValue Result;
2011 if (HiReg == X86::AH && Subtarget->is64Bit()) {
2012 // Prevent use of AH in a REX instruction by referencing AX instead.
2013 // Shift it down 8 bits.
2014 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002015 X86::AX, MVT::i16, InFlag);
Dan Gohman242a5ba2007-09-25 18:23:27 +00002016 InFlag = Result.getValue(2);
Dan Gohman61fda0d2009-09-25 18:54:59 +00002017 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman020c39b2009-08-02 16:10:52 +00002018 Result,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002019 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman020c39b2009-08-02 16:10:52 +00002020 0);
2021 // Then truncate it down to i8.
Dan Gohman7ffdd432009-08-19 18:16:17 +00002022 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2023 MVT::i8, Result);
Dan Gohman020c39b2009-08-02 16:10:52 +00002024 } else {
2025 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2026 HiReg, NVT, InFlag);
2027 InFlag = Result.getValue(2);
Evan Cheng6f0f0dd2007-08-09 21:59:35 +00002028 }
Dan Gohman5f082a72010-01-05 01:24:18 +00002029 ReplaceUses(SDValue(Node, 1), Result);
Dan Gohman242a5ba2007-09-25 18:23:27 +00002030#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00002031 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00002032 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendlingc551eff2009-08-07 21:33:25 +00002033 Result.getNode()->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00002034 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00002035 });
Dan Gohman242a5ba2007-09-25 18:23:27 +00002036#endif
Dan Gohman020c39b2009-08-02 16:10:52 +00002037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038
2039#ifndef NDEBUG
Dan Gohman020c39b2009-08-02 16:10:52 +00002040 Indent -= 2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041#endif
2042
Dan Gohman020c39b2009-08-02 16:10:52 +00002043 return NULL;
2044 }
2045
Dan Gohman7ffdd432009-08-19 18:16:17 +00002046 case X86ISD::CMP: {
Dan Gohman7ffdd432009-08-19 18:16:17 +00002047 SDValue N0 = Node->getOperand(0);
2048 SDValue N1 = Node->getOperand(1);
2049
2050 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2051 // use a smaller encoding.
2052 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2053 N0.getValueType() != MVT::i8 &&
2054 X86::isZeroNode(N1)) {
2055 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2056 if (!C) break;
2057
2058 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohmanf9a38e92009-10-09 20:35:19 +00002059 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2060 (!(C->getZExtValue() & 0x80) ||
2061 HasNoSignedComparisonUses(Node))) {
Dan Gohman7ffdd432009-08-19 18:16:17 +00002062 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2063 SDValue Reg = N0.getNode()->getOperand(0);
2064
2065 // On x86-32, only the ABCD registers have 8-bit subregisters.
2066 if (!Subtarget->is64Bit()) {
2067 TargetRegisterClass *TRC = 0;
2068 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2069 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2070 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2071 default: llvm_unreachable("Unsupported TEST operand type!");
2072 }
2073 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman61fda0d2009-09-25 18:54:59 +00002074 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2075 Reg.getValueType(), Reg, RC), 0);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002076 }
2077
2078 // Extract the l-register.
2079 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2080 MVT::i8, Reg);
2081
2082 // Emit a testb.
Dan Gohman61fda0d2009-09-25 18:54:59 +00002083 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002084 }
2085
2086 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohmanf9a38e92009-10-09 20:35:19 +00002087 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2088 (!(C->getZExtValue() & 0x8000) ||
2089 HasNoSignedComparisonUses(Node))) {
Dan Gohman7ffdd432009-08-19 18:16:17 +00002090 // Shift the immediate right by 8 bits.
2091 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2092 MVT::i8);
2093 SDValue Reg = N0.getNode()->getOperand(0);
2094
2095 // Put the value in an ABCD register.
2096 TargetRegisterClass *TRC = 0;
2097 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2098 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2099 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2100 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2101 default: llvm_unreachable("Unsupported TEST operand type!");
2102 }
2103 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman61fda0d2009-09-25 18:54:59 +00002104 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2105 Reg.getValueType(), Reg, RC), 0);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002106
2107 // Extract the h-register.
2108 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2109 MVT::i8, Reg);
2110
2111 // Emit a testb. No special NOREX tricks are needed since there's
2112 // only one GPR operand!
Dan Gohman61fda0d2009-09-25 18:54:59 +00002113 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2114 Subreg, ShiftedImm);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002115 }
2116
2117 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2118 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohmanf9a38e92009-10-09 20:35:19 +00002119 N0.getValueType() != MVT::i16 &&
2120 (!(C->getZExtValue() & 0x8000) ||
2121 HasNoSignedComparisonUses(Node))) {
Dan Gohman7ffdd432009-08-19 18:16:17 +00002122 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2123 SDValue Reg = N0.getNode()->getOperand(0);
2124
2125 // Extract the 16-bit subregister.
2126 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2127 MVT::i16, Reg);
2128
2129 // Emit a testw.
Dan Gohman61fda0d2009-09-25 18:54:59 +00002130 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002131 }
2132
2133 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2134 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohmanf9a38e92009-10-09 20:35:19 +00002135 N0.getValueType() == MVT::i64 &&
2136 (!(C->getZExtValue() & 0x80000000) ||
2137 HasNoSignedComparisonUses(Node))) {
Dan Gohman7ffdd432009-08-19 18:16:17 +00002138 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2139 SDValue Reg = N0.getNode()->getOperand(0);
2140
2141 // Extract the 32-bit subregister.
2142 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2143 MVT::i32, Reg);
2144
2145 // Emit a testl.
Dan Gohman61fda0d2009-09-25 18:54:59 +00002146 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman7ffdd432009-08-19 18:16:17 +00002147 }
2148 }
2149 break;
2150 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 }
2152
Dan Gohman5f082a72010-01-05 01:24:18 +00002153 SDNode *ResNode = SelectCode(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154
2155#ifndef NDEBUG
Bill Wendlingc551eff2009-08-07 21:33:25 +00002156 DEBUG({
David Greeneb592f222010-01-05 01:29:08 +00002157 dbgs() << std::string(Indent-2, ' ') << "=> ";
Dan Gohman5f082a72010-01-05 01:24:18 +00002158 if (ResNode == NULL || ResNode == Node)
2159 Node->dump(CurDAG);
Bill Wendlingc551eff2009-08-07 21:33:25 +00002160 else
2161 ResNode->dump(CurDAG);
David Greeneb592f222010-01-05 01:29:08 +00002162 dbgs() << '\n';
Bill Wendlingc551eff2009-08-07 21:33:25 +00002163 });
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 Indent -= 2;
2165#endif
2166
2167 return ResNode;
2168}
2169
2170bool X86DAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +00002171SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +00002172 std::vector<SDValue> &OutOps) {
Rafael Espindolabca99f72009-04-08 21:14:34 +00002173 SDValue Op0, Op1, Op2, Op3, Op4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002174 switch (ConstraintCode) {
2175 case 'o': // offsetable ??
2176 case 'v': // not offsetable ??
2177 default: return true;
2178 case 'm': // memory
Dan Gohman5f082a72010-01-05 01:24:18 +00002179 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 return true;
2181 break;
2182 }
2183
2184 OutOps.push_back(Op0);
2185 OutOps.push_back(Op1);
2186 OutOps.push_back(Op2);
2187 OutOps.push_back(Op3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00002188 OutOps.push_back(Op4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 return false;
2190}
2191
2192/// createX86ISelDag - This pass converts a legalized DAG into a
2193/// X86-specific DAG, ready for instruction scheduling.
2194///
Bill Wendling5ed22ac2009-04-29 23:29:43 +00002195FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2196 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendling58ed5d22009-04-29 00:15:41 +00002197 return new X86DAGToDAGISel(TM, OptLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198}