blob: 1c0ed7e6327cebd309ad701c1faa3bd8ef4be059 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000038#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Chris Lattnerc961eea2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000061 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 int FrameIndex;
63 } Base;
64
65 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000067 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000068 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000071 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000072 const char *ES;
73 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000075 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000076
77 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000078 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000079 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000080 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000082
83 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000084 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085 }
Chris Lattner18c59872009-06-27 04:16:01 +000086
87 bool hasBaseOrIndexReg() const {
88 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
89 }
90
91 /// isRIPRelative - Return true if this addressing mode is already RIP
92 /// relative.
93 bool isRIPRelative() const {
94 if (BaseType != RegBase) return false;
95 if (RegisterSDNode *RegNode =
96 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
97 return RegNode->getReg() == X86::RIP;
98 return false;
99 }
100
101 void setBaseReg(SDValue Reg) {
102 BaseType = RegBase;
103 Base.Reg = Reg;
104 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000105
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000106 void dump() {
David Greened7f4f242010-01-05 01:29:08 +0000107 dbgs() << "X86ISelAddressMode " << this << '\n';
108 dbgs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000109 if (Base.Reg.getNode() != 0)
110 Base.Reg.getNode()->dump();
111 else
David Greened7f4f242010-01-05 01:29:08 +0000112 dbgs() << "nul";
113 dbgs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000114 << " Scale" << Scale << '\n'
115 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000116 if (IndexReg.getNode() != 0)
117 IndexReg.getNode()->dump();
118 else
David Greened7f4f242010-01-05 01:29:08 +0000119 dbgs() << "nul";
120 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000121 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000122 if (GV)
123 GV->dump();
124 else
David Greened7f4f242010-01-05 01:29:08 +0000125 dbgs() << "nul";
126 dbgs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000127 if (CP)
128 CP->dump();
129 else
David Greened7f4f242010-01-05 01:29:08 +0000130 dbgs() << "nul";
131 dbgs() << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000132 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000133 if (ES)
David Greened7f4f242010-01-05 01:29:08 +0000134 dbgs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000135 else
David Greened7f4f242010-01-05 01:29:08 +0000136 dbgs() << "nul";
137 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000138 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000139 };
140}
141
142namespace {
Evan Chengf3caa522010-03-17 23:58:35 +0000143 class X86ISelListener : public SelectionDAG::DAGUpdateListener {
144 SmallSet<SDNode*, 4> Deletes;
145 public:
146 explicit X86ISelListener() {}
147 virtual void NodeDeleted(SDNode *N, SDNode *E) {
148 Deletes.insert(N);
149 }
150 virtual void NodeUpdated(SDNode *N) {
151 // Ignore updates.
152 }
153 bool IsDeleted(SDNode *N) {
154 return Deletes.count(N);
155 }
156 };
157
Chris Lattnerc961eea2005-11-16 01:54:32 +0000158 //===--------------------------------------------------------------------===//
159 /// ISel - X86 specific code to select X86 machine instructions for
160 /// SelectionDAG operations.
161 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000162 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163 /// X86Lowering - This object fully describes how to lower LLVM code to an
164 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000165 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000166
167 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
168 /// make the right decision when generating code for different targets.
169 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000170
Evan Chengb7a75a52008-09-26 23:41:32 +0000171 /// OptForSize - If true, selector should try to optimize for code size
172 /// instead of performance.
173 bool OptForSize;
174
Chris Lattnerc961eea2005-11-16 01:54:32 +0000175 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000176 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000177 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000178 X86Lowering(*tm.getTargetLowering()),
179 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000180 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000181
182 virtual const char *getPassName() const {
183 return "X86 DAG->DAG Instruction Selection";
184 }
185
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000186 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
187
Evan Cheng014bf212010-02-15 19:41:07 +0000188 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
189
Chris Lattner7c306da2010-03-02 06:34:30 +0000190 virtual void PreprocessISelDAG();
191
Chris Lattnerc961eea2005-11-16 01:54:32 +0000192// Include the pieces autogenerated from the target description.
193#include "X86GenDAGISel.inc"
194
195 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000196 SDNode *Select(SDNode *N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000197 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000198 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000199
Rafael Espindola094fad32009-04-08 21:14:34 +0000200 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
201 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000202 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000203 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
204 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000205 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000206 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000207 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000208 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000209 SDValue &Scale, SDValue &Index, SDValue &Disp,
210 SDValue &Segment);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000211 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000213 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000214 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnere60f7b42010-03-01 22:51:11 +0000215 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattner92d3ada2010-02-16 22:35:06 +0000216 SDValue &Base, SDValue &Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000218 SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +0000219 SDValue &NodeWithChain);
220
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000221 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000223 SDValue &Index, SDValue &Disp,
224 SDValue &Segment);
Chris Lattner7c306da2010-03-02 06:34:30 +0000225
Chris Lattnerc0bad572006-06-08 18:03:49 +0000226 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
227 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000228 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000229 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000230 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000231
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000232 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
233
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
235 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000236 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000237 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
239 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000240 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000241 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 // These are 32-bit even in 64-bit mode since RIP relative offset
243 // is 32-bit.
244 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000246 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000249 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000252 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000254 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000255 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
256 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000259
260 if (AM.Segment.getNode())
261 Segment = AM.Segment;
262 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000264 }
265
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000266 /// getI8Imm - Return a target constant with the specified value, of type
267 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000268 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000270 }
271
Chris Lattnerc961eea2005-11-16 01:54:32 +0000272 /// getI16Imm - Return a target constant with the specified value, of type
273 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000274 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000276 }
277
278 /// getI32Imm - Return a target constant with the specified value, of type
279 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000280 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000282 }
Evan Chengf597dc72006-02-10 22:24:32 +0000283
Dan Gohman8b746962008-09-23 18:22:58 +0000284 /// getGlobalBaseReg - Return an SDNode that returns the value of
285 /// the global base register. Output instructions required to
286 /// initialize the global base register, if necessary.
287 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000288 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000289
Dan Gohmanc5534622009-06-03 20:20:00 +0000290 /// getTargetMachine - Return a reference to the TargetMachine, casted
291 /// to the target-specific type.
292 const X86TargetMachine &getTargetMachine() {
293 return static_cast<const X86TargetMachine &>(TM);
294 }
295
296 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
297 /// to the target-specific type.
298 const X86InstrInfo *getInstrInfo() {
299 return getTargetMachine().getInstrInfo();
300 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000301 };
302}
303
Evan Chengf4b4c412006-08-08 00:31:00 +0000304
Evan Cheng014bf212010-02-15 19:41:07 +0000305bool
306X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000307 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000308
Evan Cheng014bf212010-02-15 19:41:07 +0000309 if (!N.hasOneUse())
310 return false;
311
312 if (N.getOpcode() != ISD::LOAD)
313 return true;
314
315 // If N is a load, do additional profitability checks.
316 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000317 switch (U->getOpcode()) {
318 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000319 case X86ISD::ADD:
320 case X86ISD::SUB:
321 case X86ISD::AND:
322 case X86ISD::XOR:
323 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000324 case ISD::ADD:
325 case ISD::ADDC:
326 case ISD::ADDE:
327 case ISD::AND:
328 case ISD::OR:
329 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000330 SDValue Op1 = U->getOperand(1);
331
Evan Cheng884c70c2008-11-27 00:49:46 +0000332 // If the other operand is a 8-bit immediate we should fold the immediate
333 // instead. This reduces code size.
334 // e.g.
335 // movl 4(%esp), %eax
336 // addl $4, %eax
337 // vs.
338 // movl $4, %eax
339 // addl 4(%esp), %eax
340 // The former is 2 bytes shorter. In case where the increment is 1, then
341 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000342 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000343 if (Imm->getAPIntValue().isSignedIntN(8))
344 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000345
346 // If the other operand is a TLS address, we should fold it instead.
347 // This produces
348 // movl %gs:0, %eax
349 // leal i@NTPOFF(%eax), %eax
350 // instead of
351 // movl $i@NTPOFF, %eax
352 // addl %gs:0, %eax
353 // if the block also has an access to a second TLS address this will save
354 // a load.
355 // FIXME: This is probably also true for non TLS addresses.
356 if (Op1.getOpcode() == X86ISD::Wrapper) {
357 SDValue Val = Op1.getOperand(0);
358 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
359 return false;
360 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000361 }
362 }
Evan Cheng014bf212010-02-15 19:41:07 +0000363 }
364
365 return true;
366}
367
Evan Chengf48ef032010-03-14 03:48:46 +0000368/// MoveBelowCallOrigChain - Replace the original chain operand of the call with
369/// load's chain operand and move load below the call's chain operand.
370static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
371 SDValue Call, SDValue OrigChain) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000372 SmallVector<SDValue, 8> Ops;
Evan Chengf48ef032010-03-14 03:48:46 +0000373 SDValue Chain = OrigChain.getOperand(0);
Evan Cheng5b2e5892009-01-26 18:43:34 +0000374 if (Chain.getNode() == Load.getNode())
375 Ops.push_back(Load.getOperand(0));
376 else {
377 assert(Chain.getOpcode() == ISD::TokenFactor &&
Evan Chengf48ef032010-03-14 03:48:46 +0000378 "Unexpected chain operand");
Evan Cheng5b2e5892009-01-26 18:43:34 +0000379 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
380 if (Chain.getOperand(i).getNode() == Load.getNode())
381 Ops.push_back(Load.getOperand(0));
382 else
383 Ops.push_back(Chain.getOperand(i));
384 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000385 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000387 Ops.clear();
388 Ops.push_back(NewChain);
389 }
Evan Chengf48ef032010-03-14 03:48:46 +0000390 for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i)
391 Ops.push_back(OrigChain.getOperand(i));
392 CurDAG->UpdateNodeOperands(OrigChain, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000393 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
394 Load.getOperand(1), Load.getOperand(2));
395 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000396 Ops.push_back(SDValue(Load.getNode(), 1));
397 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000398 Ops.push_back(Call.getOperand(i));
399 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
400}
401
402/// isCalleeLoad - Return true if call address is a load and it can be
403/// moved below CALLSEQ_START and the chains leading up to the call.
404/// Return the CALLSEQ_START by reference as a second output.
Evan Chengf48ef032010-03-14 03:48:46 +0000405/// In the case of a tail call, there isn't a callseq node between the call
406/// chain and the load.
407static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000408 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000410 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000411 if (!LD ||
412 LD->isVolatile() ||
413 LD->getAddressingMode() != ISD::UNINDEXED ||
414 LD->getExtensionType() != ISD::NON_EXTLOAD)
415 return false;
416
417 // Now let's find the callseq_start.
Evan Chengf48ef032010-03-14 03:48:46 +0000418 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000419 if (!Chain.hasOneUse())
420 return false;
421 Chain = Chain.getOperand(0);
422 }
Evan Chengf48ef032010-03-14 03:48:46 +0000423
424 if (!Chain.getNumOperands())
425 return false;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000426 if (Chain.getOperand(0).getNode() == Callee.getNode())
427 return true;
428 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000429 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
430 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000431 return true;
432 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000433}
434
Chris Lattnerfb444af2010-03-02 23:12:51 +0000435void X86DAGToDAGISel::PreprocessISelDAG() {
Chris Lattner97d85342010-03-04 01:43:43 +0000436 // OptForSize is used in pattern predicates that isel is matching.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000437 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
438
Dan Gohmanf350b272008-08-23 02:25:05 +0000439 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
440 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000441 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000442
Evan Chengf48ef032010-03-14 03:48:46 +0000443 if (OptLevel != CodeGenOpt::None &&
444 (N->getOpcode() == X86ISD::CALL ||
445 N->getOpcode() == X86ISD::TC_RETURN)) {
Chris Lattnerfb444af2010-03-02 23:12:51 +0000446 /// Also try moving call address load from outside callseq_start to just
447 /// before the call to allow it to be folded.
448 ///
449 /// [Load chain]
450 /// ^
451 /// |
452 /// [Load]
453 /// ^ ^
454 /// | |
455 /// / \--
456 /// / |
457 ///[CALLSEQ_START] |
458 /// ^ |
459 /// | |
460 /// [LOAD/C2Reg] |
461 /// | |
462 /// \ /
463 /// \ /
464 /// [CALL]
Evan Chengf48ef032010-03-14 03:48:46 +0000465 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
Chris Lattnerfb444af2010-03-02 23:12:51 +0000466 SDValue Chain = N->getOperand(0);
467 SDValue Load = N->getOperand(1);
Evan Chengf48ef032010-03-14 03:48:46 +0000468 if (!isCalleeLoad(Load, Chain, HasCallSeq))
Chris Lattnerfb444af2010-03-02 23:12:51 +0000469 continue;
Evan Chengf48ef032010-03-14 03:48:46 +0000470 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
Chris Lattnerfb444af2010-03-02 23:12:51 +0000471 ++NumLoadMoved;
472 continue;
473 }
474
475 // Lower fpround and fpextend nodes that target the FP stack to be store and
476 // load to the stack. This is a gross hack. We would like to simply mark
477 // these as being illegal, but when we do that, legalize produces these when
478 // it expands calls, then expands these in the same legalize pass. We would
479 // like dag combine to be able to hack on these between the call expansion
480 // and the node legalization. As such this pass basically does "really
481 // late" legalization of these inline with the X86 isel pass.
482 // FIXME: This should only happen when not compiled with -O0.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000483 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
484 continue;
485
486 // If the source and destination are SSE registers, then this is a legal
487 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000488 EVT SrcVT = N->getOperand(0).getValueType();
489 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000490 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
491 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
492 if (SrcIsSSE && DstIsSSE)
493 continue;
494
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000495 if (!SrcIsSSE && !DstIsSSE) {
496 // If this is an FPStack extension, it is a noop.
497 if (N->getOpcode() == ISD::FP_EXTEND)
498 continue;
499 // If this is a value-preserving FPStack truncation, it is a noop.
500 if (N->getConstantOperandVal(1))
501 continue;
502 }
503
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000504 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
505 // FPStack has extload and truncstore. SSE can fold direct loads into other
506 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000507 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000508 if (N->getOpcode() == ISD::FP_ROUND)
509 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
510 else
511 MemVT = SrcIsSSE ? SrcVT : DstVT;
512
Dan Gohmanf350b272008-08-23 02:25:05 +0000513 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000514 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000515
516 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000517 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000518 N->getOperand(0),
David Greenedb8d9892010-02-15 16:57:43 +0000519 MemTmp, NULL, 0, MemVT,
520 false, false, 0);
Dale Johannesend8392542009-02-03 21:48:12 +0000521 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greenedb8d9892010-02-15 16:57:43 +0000522 NULL, 0, MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000523
524 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
525 // extload we created. This will cause general havok on the dag because
526 // anything below the conversion could be folded into other existing nodes.
527 // To avoid invalidating 'I', back it up to the convert node.
528 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000529 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000530
531 // Now that we did that, the node is dead. Increment the iterator to the
532 // next node to process, then delete N.
533 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000534 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000535 }
536}
537
Chris Lattnerc961eea2005-11-16 01:54:32 +0000538
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000539/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
540/// the main function.
541void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
542 MachineFrameInfo *MFI) {
543 const TargetInstrInfo *TII = TM.getInstrInfo();
544 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000545 BuildMI(BB, DebugLoc::getUnknownLoc(),
546 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000547}
548
549void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
550 // If this is main, emit special code for main.
551 MachineBasicBlock *BB = MF.begin();
552 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
553 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
554}
555
Rafael Espindola094fad32009-04-08 21:14:34 +0000556
557bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
558 X86ISelAddressMode &AM) {
559 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
560 SDValue Segment = N.getOperand(0);
561
562 if (AM.Segment.getNode() == 0) {
563 AM.Segment = Segment;
564 return false;
565 }
566
567 return true;
568}
569
570bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
571 // This optimization is valid because the GNU TLS model defines that
572 // gs:0 (or fs:0 on X86-64) contains its own address.
573 // For more information see http://people.redhat.com/drepper/tls.pdf
574
575 SDValue Address = N.getOperand(1);
576 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
577 !MatchSegmentBaseAddress (Address, AM))
578 return false;
579
580 return true;
581}
582
Chris Lattner18c59872009-06-27 04:16:01 +0000583/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
584/// into an addressing mode. These wrap things that will resolve down into a
585/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000586/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000587bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000588 // If the addressing mode already has a symbol as the displacement, we can
589 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000590 if (AM.hasSymbolicDisplacement())
591 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000592
593 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000594 CodeModel::Model M = TM.getCodeModel();
595
Chris Lattner18c59872009-06-27 04:16:01 +0000596 // Handle X86-64 rip-relative addresses. We check this before checking direct
597 // folding because RIP is preferable to non-RIP accesses.
598 if (Subtarget->is64Bit() &&
599 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
600 // they cannot be folded into immediate fields.
601 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000602 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000603 // Base and index reg must be 0 in order to use %rip as base and lowering
604 // must allow RIP.
605 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000606 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
607 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000608 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000609 AM.GV = G->getGlobal();
610 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000611 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000612 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
613 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000614 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000615 AM.CP = CP->getConstVal();
616 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000617 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000618 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000619 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
620 AM.ES = S->getSymbol();
621 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000622 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000623 AM.JT = J->getIndex();
624 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000625 } else {
626 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000627 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000628 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000629
Chris Lattner18c59872009-06-27 04:16:01 +0000630 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000632 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000633 }
634
635 // Handle the case when globals fit in our immediate field: This is true for
636 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
637 // mode, this results in a non-RIP-relative computation.
638 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000639 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000640 TM.getRelocationModel() == Reloc::Static)) {
641 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
642 AM.GV = G->getGlobal();
643 AM.Disp += G->getOffset();
644 AM.SymbolFlags = G->getTargetFlags();
645 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
646 AM.CP = CP->getConstVal();
647 AM.Align = CP->getAlignment();
648 AM.Disp += CP->getOffset();
649 AM.SymbolFlags = CP->getTargetFlags();
650 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
651 AM.ES = S->getSymbol();
652 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000653 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000654 AM.JT = J->getIndex();
655 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000656 } else {
657 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000658 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000659 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000660 return false;
661 }
662
663 return true;
664}
665
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000666/// MatchAddress - Add the specified node to the specified addressing mode,
667/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000668/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000669bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
Evan Chengf3caa522010-03-17 23:58:35 +0000670 X86ISelListener DeadNodes;
671 if (MatchAddressRecursively(N, AM, DeadNodes, 0))
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000672 return true;
673
674 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
675 // a smaller encoding and avoids a scaled-index.
676 if (AM.Scale == 2 &&
677 AM.BaseType == X86ISelAddressMode::RegBase &&
678 AM.Base.Reg.getNode() == 0) {
679 AM.Base.Reg = AM.IndexReg;
680 AM.Scale = 1;
681 }
682
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000683 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
684 // because it has a smaller encoding.
685 // TODO: Which other code models can use this?
686 if (TM.getCodeModel() == CodeModel::Small &&
687 Subtarget->is64Bit() &&
688 AM.Scale == 1 &&
689 AM.BaseType == X86ISelAddressMode::RegBase &&
690 AM.Base.Reg.getNode() == 0 &&
691 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000692 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000693 AM.hasSymbolicDisplacement())
694 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
695
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000696 return false;
697}
698
699bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000700 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000701 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000702 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000703 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000704 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000705 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000706 AM.dump();
707 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000708 // Limit recursion.
709 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000710 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000711
712 CodeModel::Model M = TM.getCodeModel();
713
Chris Lattner18c59872009-06-27 04:16:01 +0000714 // If this is already a %rip relative address, we can only merge immediates
715 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000716 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000717 if (AM.isRIPRelative()) {
718 // FIXME: JumpTable and ExternalSymbol address currently don't like
719 // displacements. It isn't very important, but this should be fixed for
720 // consistency.
721 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000722
Chris Lattner18c59872009-06-27 04:16:01 +0000723 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
724 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000725 if (X86::isOffsetSuitableForCodeModel(Val, M,
726 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000727 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000728 return false;
729 }
730 }
731 return true;
732 }
733
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000734 switch (N.getOpcode()) {
735 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000736 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000737 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000738 if (!is64Bit ||
739 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
740 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000741 AM.Disp += Val;
742 return false;
743 }
744 break;
745 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000746
Rafael Espindola094fad32009-04-08 21:14:34 +0000747 case X86ISD::SegmentBaseAddress:
748 if (!MatchSegmentBaseAddress(N, AM))
749 return false;
750 break;
751
Rafael Espindola49a168d2009-04-12 21:55:03 +0000752 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000753 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000754 if (!MatchWrapper(N, AM))
755 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000756 break;
757
Rafael Espindola094fad32009-04-08 21:14:34 +0000758 case ISD::LOAD:
759 if (!MatchLoad(N, AM))
760 return false;
761 break;
762
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000763 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000764 if (AM.BaseType == X86ISelAddressMode::RegBase
765 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000766 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
767 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
768 return false;
769 }
770 break;
Evan Chengec693f72005-12-08 02:01:35 +0000771
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000772 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000773 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000774 break;
775
Gabor Greif93c53e52008-08-31 15:37:04 +0000776 if (ConstantSDNode
777 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000778 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000779 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
780 // that the base operand remains free for further matching. If
781 // the base doesn't end up getting used, a post-processing step
782 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000783 if (Val == 1 || Val == 2 || Val == 3) {
784 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000785 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000786
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000787 // Okay, we know that we have a scale by now. However, if the scaled
788 // value is an add of something and a constant, we can fold the
789 // constant into the disp field here.
Dan Gohmana10756e2010-01-21 02:09:26 +0000790 if (ShVal.getNode()->getOpcode() == ISD::ADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000791 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
792 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000793 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000794 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000795 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000796 if (!is64Bit ||
797 X86::isOffsetSuitableForCodeModel(Disp, M,
798 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000799 AM.Disp = Disp;
800 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000801 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000802 } else {
803 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000804 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000805 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000806 }
807 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000808 }
Evan Chengec693f72005-12-08 02:01:35 +0000809
Dan Gohman83688052007-10-22 20:22:24 +0000810 case ISD::SMUL_LOHI:
811 case ISD::UMUL_LOHI:
812 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000813 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000814 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000815 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000816 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000817 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000818 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000819 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000820 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000821 if (ConstantSDNode
822 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000823 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
824 CN->getZExtValue() == 9) {
825 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000826
Gabor Greifba36cb52008-08-28 21:40:38 +0000827 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000828 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000829
830 // Okay, we know that we have a scale by now. However, if the scaled
831 // value is an add of something and a constant, we can fold the
832 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000833 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
834 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
835 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000836 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000837 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000838 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000839 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000840 if (!is64Bit ||
841 X86::isOffsetSuitableForCodeModel(Disp, M,
842 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000843 AM.Disp = Disp;
844 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000845 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000846 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000847 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000848 }
849
850 AM.IndexReg = AM.Base.Reg = Reg;
851 return false;
852 }
Chris Lattner62412262007-02-04 20:18:17 +0000853 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000854 break;
855
Dan Gohman3cd90a12009-05-11 18:02:53 +0000856 case ISD::SUB: {
857 // Given A-B, if A can be completely folded into the address and
858 // the index field with the index field unused, use -B as the index.
859 // This is a win if a has multiple parts that can be folded into
860 // the address. Also, this saves a mov if the base register has
861 // other uses, since it avoids a two-address sub instruction, however
862 // it costs an additional mov if the index register has other uses.
863
864 // Test if the LHS of the sub can be folded.
865 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000866 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM,
867 DeadNodes, Depth+1) ||
868 // If it is successful but the recursive update causes N to be deleted,
869 // then it's not safe to continue.
870 DeadNodes.IsDeleted(N.getNode())) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000871 AM = Backup;
872 break;
873 }
874 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000875 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000876 AM = Backup;
877 break;
878 }
Evan Chengf3caa522010-03-17 23:58:35 +0000879
Dan Gohman3cd90a12009-05-11 18:02:53 +0000880 int Cost = 0;
881 SDValue RHS = N.getNode()->getOperand(1);
882 // If the RHS involves a register with multiple uses, this
883 // transformation incurs an extra mov, due to the neg instruction
884 // clobbering its operand.
885 if (!RHS.getNode()->hasOneUse() ||
886 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
887 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
888 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
889 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +0000891 ++Cost;
892 // If the base is a register with multiple uses, this
893 // transformation may save a mov.
894 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
895 AM.Base.Reg.getNode() &&
896 !AM.Base.Reg.getNode()->hasOneUse()) ||
897 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
898 --Cost;
899 // If the folded LHS was interesting, this transformation saves
900 // address arithmetic.
901 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
902 ((AM.Disp != 0) && (Backup.Disp == 0)) +
903 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
904 --Cost;
905 // If it doesn't look like it may be an overall win, don't do it.
906 if (Cost >= 0) {
907 AM = Backup;
908 break;
909 }
910
911 // Ok, the transformation is legal and appears profitable. Go for it.
912 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
913 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
914 AM.IndexReg = Neg;
915 AM.Scale = 1;
916
917 // Insert the new nodes into the topological ordering.
918 if (Zero.getNode()->getNodeId() == -1 ||
919 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
920 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
921 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
922 }
923 if (Neg.getNode()->getNodeId() == -1 ||
924 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
925 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
926 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
927 }
928 return false;
929 }
930
Evan Cheng8e278262009-01-17 07:09:27 +0000931 case ISD::ADD: {
932 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000933 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
934 DeadNodes, Depth+1)) {
935 if (DeadNodes.IsDeleted(N.getNode()))
936 // If it is successful but the recursive update causes N to be deleted,
937 // then it's not safe to continue.
938 return true;
939 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
940 DeadNodes, Depth+1))
941 // If it is successful but the recursive update causes N to be deleted,
942 // then it's not safe to continue.
943 return DeadNodes.IsDeleted(N.getNode());
944 }
945
946 // Try again after commuting the operands.
Evan Cheng8e278262009-01-17 07:09:27 +0000947 AM = Backup;
Evan Chengf3caa522010-03-17 23:58:35 +0000948 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
949 DeadNodes, Depth+1)) {
950 if (DeadNodes.IsDeleted(N.getNode()))
951 // If it is successful but the recursive update causes N to be deleted,
952 // then it's not safe to continue.
953 return true;
954 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
955 DeadNodes, Depth+1))
956 // If it is successful but the recursive update causes N to be deleted,
957 // then it's not safe to continue.
958 return DeadNodes.IsDeleted(N.getNode());
959 }
Evan Cheng8e278262009-01-17 07:09:27 +0000960 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000961
962 // If we couldn't fold both operands into the address at the same time,
963 // see if we can just put each operand into a register and fold at least
964 // the add.
965 if (AM.BaseType == X86ISelAddressMode::RegBase &&
966 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +0000967 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +0000968 AM.Base.Reg = N.getNode()->getOperand(0);
969 AM.IndexReg = N.getNode()->getOperand(1);
970 AM.Scale = 1;
971 return false;
972 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000973 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000974 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000975
Chris Lattner62412262007-02-04 20:18:17 +0000976 case ISD::OR:
977 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000978 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
979 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000980 uint64_t Offset = CN->getSExtValue();
Evan Chengf3caa522010-03-17 23:58:35 +0000981
982 // Check to see if the LHS & C is zero.
983 if (!CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue()))
984 break;
985
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000986 // Start with the LHS as an addr mode.
Evan Chengf3caa522010-03-17 23:58:35 +0000987 if (!MatchAddressRecursively(N.getOperand(0), AM, DeadNodes, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000988 // Address could not have picked a GV address for the displacement.
989 AM.GV == NULL &&
990 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000991 (!is64Bit ||
992 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
Evan Chengf3caa522010-03-17 23:58:35 +0000993 AM.hasSymbolicDisplacement()))) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000994 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000995 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000996 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000997 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000998 }
999 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001000
1001 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001002 // Perform some heroic transforms on an and of a constant-count shift
1003 // with a constant to enable use of the scaled offset field.
1004
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001006 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001007
Evan Cheng1314b002007-12-13 00:43:27 +00001008 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001009 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001010
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001011 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001012 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1013 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1014 if (!C1 || !C2) break;
1015
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001016 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1017 // allows us to convert the shift and and into an h-register extract and
1018 // a scaled index.
1019 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1020 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001021 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001022 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001023 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001024 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1025 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1026 X, Eight);
1027 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1028 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001029 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001030 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1031 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001032
1033 // Insert the new nodes into the topological ordering.
1034 if (Eight.getNode()->getNodeId() == -1 ||
1035 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1036 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1037 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1038 }
1039 if (Mask.getNode()->getNodeId() == -1 ||
1040 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1041 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1042 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1043 }
1044 if (Srl.getNode()->getNodeId() == -1 ||
1045 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1046 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1047 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1048 }
1049 if (And.getNode()->getNodeId() == -1 ||
1050 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1051 CurDAG->RepositionNode(N.getNode(), And.getNode());
1052 And.getNode()->setNodeId(N.getNode()->getNodeId());
1053 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001054 if (ShlCount.getNode()->getNodeId() == -1 ||
1055 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1056 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1057 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1058 }
1059 if (Shl.getNode()->getNodeId() == -1 ||
1060 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1061 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1062 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1063 }
Evan Chengf3caa522010-03-17 23:58:35 +00001064 CurDAG->ReplaceAllUsesWith(N, Shl, &DeadNodes);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001065 AM.IndexReg = And;
1066 AM.Scale = (1 << ScaleLog);
1067 return false;
1068 }
1069 }
1070
1071 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1072 // allows us to fold the shift into this addressing mode.
1073 if (Shift.getOpcode() != ISD::SHL) break;
1074
Evan Cheng1314b002007-12-13 00:43:27 +00001075 // Not likely to be profitable if either the AND or SHIFT node has more
1076 // than one use (unless all uses are for address computation). Besides,
1077 // isel mechanism requires their node ids to be reused.
1078 if (!N.hasOneUse() || !Shift.hasOneUse())
1079 break;
1080
1081 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001082 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001083 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1084 break;
1085
1086 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001087 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001088 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001089 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1090 NewANDMask);
1091 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001092 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001093
1094 // Insert the new nodes into the topological ordering.
1095 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1096 CurDAG->RepositionNode(X.getNode(), C1);
1097 C1->setNodeId(X.getNode()->getNodeId());
1098 }
1099 if (NewANDMask.getNode()->getNodeId() == -1 ||
1100 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1101 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1102 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1103 }
1104 if (NewAND.getNode()->getNodeId() == -1 ||
1105 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1106 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1107 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1108 }
1109 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1110 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1111 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1112 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1113 }
1114
Evan Chengf3caa522010-03-17 23:58:35 +00001115 CurDAG->ReplaceAllUsesWith(N, NewSHIFT, &DeadNodes);
Evan Cheng1314b002007-12-13 00:43:27 +00001116
1117 AM.Scale = 1 << ShiftCst;
1118 AM.IndexReg = NewAND;
1119 return false;
1120 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001121 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001122
Rafael Espindola523249f2009-03-31 16:16:57 +00001123 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001124}
1125
1126/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1127/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001128bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001129 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001130 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001131 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001132 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001133 AM.IndexReg = N;
1134 AM.Scale = 1;
1135 return false;
1136 }
1137
1138 // Otherwise, we cannot select it.
1139 return true;
1140 }
1141
1142 // Default, generate it as a register.
1143 AM.BaseType = X86ISelAddressMode::RegBase;
1144 AM.Base.Reg = N;
1145 return false;
1146}
1147
Evan Chengec693f72005-12-08 02:01:35 +00001148/// SelectAddr - returns true if it is able pattern match an addressing mode.
1149/// It returns the operands which make up the maximal addressing mode it can
1150/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001151bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001152 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001153 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001154 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001155 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001156 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001157
Owen Andersone50ed302009-08-10 22:56:29 +00001158 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001159 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001160 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001161 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001162 }
Evan Cheng8700e142006-01-11 06:09:51 +00001163
Gabor Greifba36cb52008-08-28 21:40:38 +00001164 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001165 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001166
Rafael Espindola094fad32009-04-08 21:14:34 +00001167 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001168 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001169}
1170
Chris Lattner3a7cd952006-10-07 21:55:32 +00001171/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1172/// match a load whose top elements are either undef or zeros. The load flavor
1173/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001174///
1175/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001176/// PatternChainNode: this is the matched node that has a chain input and
1177/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001178bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001179 SDValue N, SDValue &Base,
1180 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001181 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001182 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001183 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001184 PatternNodeWithChain = N.getOperand(0);
1185 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1186 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001187 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1188 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001189 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001190 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001191 return false;
1192 return true;
1193 }
1194 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001195
1196 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001197 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001198 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001199 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001200 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001201 N.getOperand(0).getNode()->hasOneUse() &&
1202 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001203 N.getOperand(0).getOperand(0).hasOneUse() &&
1204 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1205 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001206 // Okay, this is a zero extending load. Fold it.
1207 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001208 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001209 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001210 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001211 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001212 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001213 return false;
1214}
1215
1216
Evan Cheng51a9ed92006-02-25 10:09:08 +00001217/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1218/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001219bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001220 SDValue &Base, SDValue &Scale,
1221 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001222 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001223
1224 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1225 // segments.
1226 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001228 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001229 if (MatchAddress(N, AM))
1230 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001231 assert (T == AM.Segment);
1232 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001233
Owen Andersone50ed302009-08-10 22:56:29 +00001234 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235 unsigned Complexity = 0;
1236 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001237 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001238 Complexity = 1;
1239 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001240 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001241 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1242 Complexity = 4;
1243
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001245 Complexity++;
1246 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001247 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001248
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001249 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1250 // a simple shift.
1251 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001252 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001253
1254 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1255 // to a LEA. This is determined with some expermentation but is by no means
1256 // optimal (especially for code size consideration). LEA is nice because of
1257 // its three-address nature. Tweak the cost function again when we can run
1258 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001259 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001260 // For X86-64, we should always use lea to materialize RIP relative
1261 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001262 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001263 Complexity = 4;
1264 else
1265 Complexity += 2;
1266 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001267
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001269 Complexity++;
1270
Chris Lattner25142782009-07-11 22:50:33 +00001271 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001272 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001273 return false;
1274
1275 SDValue Segment;
1276 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1277 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001278}
1279
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001280/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001281bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001282 SDValue &Scale, SDValue &Index,
1283 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001284 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1285 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1286
1287 X86ISelAddressMode AM;
1288 AM.GV = GA->getGlobal();
1289 AM.Disp += GA->getOffset();
1290 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001291 AM.SymbolFlags = GA->getTargetFlags();
1292
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001294 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001296 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001298 }
1299
1300 SDValue Segment;
1301 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1302 return true;
1303}
1304
1305
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001306bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001308 SDValue &Index, SDValue &Disp,
1309 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001310 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1311 !IsProfitableToFold(N, P, P) ||
1312 !IsLegalToFold(N, P, P))
1313 return false;
1314
1315 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001316}
1317
Dan Gohman8b746962008-09-23 18:22:58 +00001318/// getGlobalBaseReg - Return an SDNode that returns the value of
1319/// the global base register. Output instructions required to
1320/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001321///
Evan Cheng9ade2182006-08-26 05:34:46 +00001322SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001323 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001325}
1326
Evan Chengb245d922006-05-20 01:36:52 +00001327static SDNode *FindCallStartFromCall(SDNode *Node) {
1328 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001330 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001332}
1333
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001334SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1335 SDValue Chain = Node->getOperand(0);
1336 SDValue In1 = Node->getOperand(1);
1337 SDValue In2L = Node->getOperand(2);
1338 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001339 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001340 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001341 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001342 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1343 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1344 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1345 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1346 MVT::i32, MVT::i32, MVT::Other, Ops,
1347 array_lengthof(Ops));
1348 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1349 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001350}
Christopher Lambc59e5212007-08-10 21:48:46 +00001351
Owen Andersone50ed302009-08-10 22:56:29 +00001352SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001353 if (Node->hasAnyUseOfValue(0))
1354 return 0;
1355
1356 // Optimize common patterns for __sync_add_and_fetch and
1357 // __sync_sub_and_fetch where the result is not used. This allows us
1358 // to use "lock" version of add, sub, inc, dec instructions.
1359 // FIXME: Do not use special instructions but instead add the "lock"
1360 // prefix to the target node somehow. The extra information will then be
1361 // transferred to machine instruction and it denotes the prefix.
1362 SDValue Chain = Node->getOperand(0);
1363 SDValue Ptr = Node->getOperand(1);
1364 SDValue Val = Node->getOperand(2);
1365 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001366 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001367 return 0;
1368
1369 bool isInc = false, isDec = false, isSub = false, isCN = false;
1370 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1371 if (CN) {
1372 isCN = true;
1373 int64_t CNVal = CN->getSExtValue();
1374 if (CNVal == 1)
1375 isInc = true;
1376 else if (CNVal == -1)
1377 isDec = true;
1378 else if (CNVal >= 0)
1379 Val = CurDAG->getTargetConstant(CNVal, NVT);
1380 else {
1381 isSub = true;
1382 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1383 }
1384 } else if (Val.hasOneUse() &&
1385 Val.getOpcode() == ISD::SUB &&
1386 X86::isZeroNode(Val.getOperand(0))) {
1387 isSub = true;
1388 Val = Val.getOperand(1);
1389 }
1390
1391 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001393 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001395 if (isInc)
1396 Opc = X86::LOCK_INC8m;
1397 else if (isDec)
1398 Opc = X86::LOCK_DEC8m;
1399 else if (isSub) {
1400 if (isCN)
1401 Opc = X86::LOCK_SUB8mi;
1402 else
1403 Opc = X86::LOCK_SUB8mr;
1404 } else {
1405 if (isCN)
1406 Opc = X86::LOCK_ADD8mi;
1407 else
1408 Opc = X86::LOCK_ADD8mr;
1409 }
1410 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001412 if (isInc)
1413 Opc = X86::LOCK_INC16m;
1414 else if (isDec)
1415 Opc = X86::LOCK_DEC16m;
1416 else if (isSub) {
1417 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001418 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001419 Opc = X86::LOCK_SUB16mi8;
1420 else
1421 Opc = X86::LOCK_SUB16mi;
1422 } else
1423 Opc = X86::LOCK_SUB16mr;
1424 } else {
1425 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001426 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001427 Opc = X86::LOCK_ADD16mi8;
1428 else
1429 Opc = X86::LOCK_ADD16mi;
1430 } else
1431 Opc = X86::LOCK_ADD16mr;
1432 }
1433 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001435 if (isInc)
1436 Opc = X86::LOCK_INC32m;
1437 else if (isDec)
1438 Opc = X86::LOCK_DEC32m;
1439 else if (isSub) {
1440 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001441 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001442 Opc = X86::LOCK_SUB32mi8;
1443 else
1444 Opc = X86::LOCK_SUB32mi;
1445 } else
1446 Opc = X86::LOCK_SUB32mr;
1447 } else {
1448 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001449 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001450 Opc = X86::LOCK_ADD32mi8;
1451 else
1452 Opc = X86::LOCK_ADD32mi;
1453 } else
1454 Opc = X86::LOCK_ADD32mr;
1455 }
1456 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001458 if (isInc)
1459 Opc = X86::LOCK_INC64m;
1460 else if (isDec)
1461 Opc = X86::LOCK_DEC64m;
1462 else if (isSub) {
1463 Opc = X86::LOCK_SUB64mr;
1464 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001465 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001466 Opc = X86::LOCK_SUB64mi8;
1467 else if (Predicate_i64immSExt32(Val.getNode()))
1468 Opc = X86::LOCK_SUB64mi32;
1469 }
1470 } else {
1471 Opc = X86::LOCK_ADD64mr;
1472 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001473 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001474 Opc = X86::LOCK_ADD64mi8;
1475 else if (Predicate_i64immSExt32(Val.getNode()))
1476 Opc = X86::LOCK_ADD64mi32;
1477 }
1478 }
1479 break;
1480 }
1481
1482 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001483 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001484 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001485 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1486 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001487 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001488 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1489 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1490 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001491 SDValue RetVals[] = { Undef, Ret };
1492 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1493 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001494 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1495 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1496 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001497 SDValue RetVals[] = { Undef, Ret };
1498 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1499 }
1500}
1501
Dan Gohman11596ed2009-10-09 20:35:19 +00001502/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1503/// any uses which require the SF or OF bits to be accurate.
1504static bool HasNoSignedComparisonUses(SDNode *N) {
1505 // Examine each user of the node.
1506 for (SDNode::use_iterator UI = N->use_begin(),
1507 UE = N->use_end(); UI != UE; ++UI) {
1508 // Only examine CopyToReg uses.
1509 if (UI->getOpcode() != ISD::CopyToReg)
1510 return false;
1511 // Only examine CopyToReg uses that copy to EFLAGS.
1512 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1513 X86::EFLAGS)
1514 return false;
1515 // Examine each user of the CopyToReg use.
1516 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1517 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1518 // Only examine the Flag result.
1519 if (FlagUI.getUse().getResNo() != 1) continue;
1520 // Anything unusual: assume conservatively.
1521 if (!FlagUI->isMachineOpcode()) return false;
1522 // Examine the opcode of the user.
1523 switch (FlagUI->getMachineOpcode()) {
1524 // These comparisons don't treat the most significant bit specially.
1525 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1526 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1527 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1528 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001529 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1530 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001531 case X86::CMOVA16rr: case X86::CMOVA16rm:
1532 case X86::CMOVA32rr: case X86::CMOVA32rm:
1533 case X86::CMOVA64rr: case X86::CMOVA64rm:
1534 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1535 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1536 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1537 case X86::CMOVB16rr: case X86::CMOVB16rm:
1538 case X86::CMOVB32rr: case X86::CMOVB32rm:
1539 case X86::CMOVB64rr: case X86::CMOVB64rm:
1540 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1541 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1542 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1543 case X86::CMOVE16rr: case X86::CMOVE16rm:
1544 case X86::CMOVE32rr: case X86::CMOVE32rm:
1545 case X86::CMOVE64rr: case X86::CMOVE64rm:
1546 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1547 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1548 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1549 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1550 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1551 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1552 case X86::CMOVP16rr: case X86::CMOVP16rm:
1553 case X86::CMOVP32rr: case X86::CMOVP32rm:
1554 case X86::CMOVP64rr: case X86::CMOVP64rm:
1555 continue;
1556 // Anything else: assume conservatively.
1557 default: return false;
1558 }
1559 }
1560 }
1561 return true;
1562}
1563
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001564SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001565 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001566 unsigned Opc, MOpc;
1567 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001568 DebugLoc dl = Node->getDebugLoc();
1569
Chris Lattner7c306da2010-03-02 06:34:30 +00001570 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001571
Dan Gohmane8be6c62008-07-17 19:10:17 +00001572 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001573 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001574 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001575 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001576
Evan Cheng0114e942006-01-06 20:36:21 +00001577 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001578 default: break;
1579 case X86ISD::GlobalBaseReg:
1580 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001581
Dan Gohman72677342009-08-02 16:10:52 +00001582 case X86ISD::ATOMOR64_DAG:
1583 return SelectAtomic64(Node, X86::ATOMOR6432);
1584 case X86ISD::ATOMXOR64_DAG:
1585 return SelectAtomic64(Node, X86::ATOMXOR6432);
1586 case X86ISD::ATOMADD64_DAG:
1587 return SelectAtomic64(Node, X86::ATOMADD6432);
1588 case X86ISD::ATOMSUB64_DAG:
1589 return SelectAtomic64(Node, X86::ATOMSUB6432);
1590 case X86ISD::ATOMNAND64_DAG:
1591 return SelectAtomic64(Node, X86::ATOMNAND6432);
1592 case X86ISD::ATOMAND64_DAG:
1593 return SelectAtomic64(Node, X86::ATOMAND6432);
1594 case X86ISD::ATOMSWAP64_DAG:
1595 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001596
Dan Gohman72677342009-08-02 16:10:52 +00001597 case ISD::ATOMIC_LOAD_ADD: {
1598 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1599 if (RetVal)
1600 return RetVal;
1601 break;
1602 }
1603
1604 case ISD::SMUL_LOHI:
1605 case ISD::UMUL_LOHI: {
1606 SDValue N0 = Node->getOperand(0);
1607 SDValue N1 = Node->getOperand(1);
1608
1609 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001610 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001611 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001612 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1614 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1615 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1616 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001617 }
Bill Wendling12321672009-08-07 21:33:25 +00001618 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001620 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1622 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1623 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1624 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001625 }
Bill Wendling12321672009-08-07 21:33:25 +00001626 }
Dan Gohman72677342009-08-02 16:10:52 +00001627
1628 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001630 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1632 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1633 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1634 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001635 }
1636
1637 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001638 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001639 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001640 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001641 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001642 if (foldedLoad)
1643 std::swap(N0, N1);
1644 }
1645
1646 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1647 N0, SDValue()).getValue(1);
1648
1649 if (foldedLoad) {
1650 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1651 InFlag };
1652 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001653 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1654 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001655 InFlag = SDValue(CNode, 1);
1656 // Update the chain.
1657 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1658 } else {
1659 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001660 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001661 }
1662
1663 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001664 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001665 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1666 LoReg, NVT, InFlag);
1667 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001668 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001669 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001670 }
1671 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001672 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001673 SDValue Result;
1674 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1675 // Prevent use of AH in a REX instruction by referencing AX instead.
1676 // Shift it down 8 bits.
1677 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001679 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001680 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1681 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001683 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001684 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1685 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001686 } else {
1687 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1688 HiReg, NVT, InFlag);
1689 InFlag = Result.getValue(2);
1690 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001691 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001692 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001693 }
1694
Dan Gohman72677342009-08-02 16:10:52 +00001695 return NULL;
1696 }
1697
1698 case ISD::SDIVREM:
1699 case ISD::UDIVREM: {
1700 SDValue N0 = Node->getOperand(0);
1701 SDValue N1 = Node->getOperand(1);
1702
1703 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001704 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001706 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1708 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1709 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1710 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001711 }
Bill Wendling12321672009-08-07 21:33:25 +00001712 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001714 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1716 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1717 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1718 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001719 }
Bill Wendling12321672009-08-07 21:33:25 +00001720 }
Dan Gohman72677342009-08-02 16:10:52 +00001721
Chris Lattner9e323832009-12-23 01:45:04 +00001722 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001723 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001725 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001727 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001728 ClrOpcode = 0;
1729 SExtOpcode = X86::CBW;
1730 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001731 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001732 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001733 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001734 SExtOpcode = X86::CWD;
1735 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001736 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001737 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001738 ClrOpcode = X86::MOV32r0;
1739 SExtOpcode = X86::CDQ;
1740 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001741 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001742 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001743 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001744 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001745 break;
1746 }
1747
Dan Gohman72677342009-08-02 16:10:52 +00001748 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001749 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001750 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001751
Dan Gohman72677342009-08-02 16:10:52 +00001752 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001754 // Special case for div8, just use a move with zero extension to AX to
1755 // clear the upper 8 bits (AH).
1756 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001757 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001758 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1759 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001760 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1761 MVT::Other, Ops,
1762 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001763 Chain = Move.getValue(1);
1764 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001765 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001766 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001767 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001768 Chain = CurDAG->getEntryNode();
1769 }
1770 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1771 InFlag = Chain.getValue(1);
1772 } else {
1773 InFlag =
1774 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1775 LoReg, N0, SDValue()).getValue(1);
1776 if (isSigned && !signBitIsZero) {
1777 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001778 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001779 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001780 } else {
1781 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001782 SDValue ClrNode =
1783 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001784 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001785 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001786 }
Evan Cheng948f3432006-01-06 23:19:29 +00001787 }
Dan Gohman525178c2007-10-08 18:33:35 +00001788
Dan Gohman72677342009-08-02 16:10:52 +00001789 if (foldedLoad) {
1790 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1791 InFlag };
1792 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001793 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1794 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001795 InFlag = SDValue(CNode, 1);
1796 // Update the chain.
1797 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1798 } else {
1799 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001800 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001801 }
Evan Cheng948f3432006-01-06 23:19:29 +00001802
Dan Gohman72677342009-08-02 16:10:52 +00001803 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001804 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001805 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1806 LoReg, NVT, InFlag);
1807 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001808 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001809 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001810 }
1811 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001812 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001813 SDValue Result;
1814 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1815 // Prevent use of AH in a REX instruction by referencing AX instead.
1816 // Shift it down 8 bits.
1817 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001819 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001820 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001821 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001823 0);
1824 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001825 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1826 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001827 } else {
1828 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1829 HiReg, NVT, InFlag);
1830 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001831 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001832 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001833 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001834 }
Dan Gohman72677342009-08-02 16:10:52 +00001835 return NULL;
1836 }
1837
Dan Gohman6a402dc2009-08-19 18:16:17 +00001838 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001839 SDValue N0 = Node->getOperand(0);
1840 SDValue N1 = Node->getOperand(1);
1841
1842 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1843 // use a smaller encoding.
1844 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1845 N0.getValueType() != MVT::i8 &&
1846 X86::isZeroNode(N1)) {
1847 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1848 if (!C) break;
1849
1850 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00001851 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
1852 (!(C->getZExtValue() & 0x80) ||
1853 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001854 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1855 SDValue Reg = N0.getNode()->getOperand(0);
1856
1857 // On x86-32, only the ABCD registers have 8-bit subregisters.
1858 if (!Subtarget->is64Bit()) {
1859 TargetRegisterClass *TRC = 0;
1860 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1861 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1862 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1863 default: llvm_unreachable("Unsupported TEST operand type!");
1864 }
1865 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001866 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1867 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001868 }
1869
1870 // Extract the l-register.
1871 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1872 MVT::i8, Reg);
1873
1874 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00001875 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001876 }
1877
1878 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00001879 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
1880 (!(C->getZExtValue() & 0x8000) ||
1881 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001882 // Shift the immediate right by 8 bits.
1883 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1884 MVT::i8);
1885 SDValue Reg = N0.getNode()->getOperand(0);
1886
1887 // Put the value in an ABCD register.
1888 TargetRegisterClass *TRC = 0;
1889 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1890 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
1891 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1892 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1893 default: llvm_unreachable("Unsupported TEST operand type!");
1894 }
1895 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001896 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1897 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001898
1899 // Extract the h-register.
1900 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
1901 MVT::i8, Reg);
1902
1903 // Emit a testb. No special NOREX tricks are needed since there's
1904 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00001905 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
1906 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001907 }
1908
1909 // For example, "testl %eax, $32776" to "testw %ax, $32776".
1910 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001911 N0.getValueType() != MVT::i16 &&
1912 (!(C->getZExtValue() & 0x8000) ||
1913 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001914 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
1915 SDValue Reg = N0.getNode()->getOperand(0);
1916
1917 // Extract the 16-bit subregister.
1918 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
1919 MVT::i16, Reg);
1920
1921 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00001922 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001923 }
1924
1925 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
1926 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001927 N0.getValueType() == MVT::i64 &&
1928 (!(C->getZExtValue() & 0x80000000) ||
1929 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001930 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
1931 SDValue Reg = N0.getNode()->getOperand(0);
1932
1933 // Extract the 32-bit subregister.
1934 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
1935 MVT::i32, Reg);
1936
1937 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00001938 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001939 }
1940 }
1941 break;
1942 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001943 }
1944
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001945 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00001946
Chris Lattner7c306da2010-03-02 06:34:30 +00001947 DEBUG(dbgs() << "=> ";
1948 if (ResNode == NULL || ResNode == Node)
1949 Node->dump(CurDAG);
1950 else
1951 ResNode->dump(CurDAG);
1952 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001953
1954 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001955}
1956
Chris Lattnerc0bad572006-06-08 18:03:49 +00001957bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001958SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001959 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001960 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001961 switch (ConstraintCode) {
1962 case 'o': // offsetable ??
1963 case 'v': // not offsetable ??
1964 default: return true;
1965 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001966 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001967 return true;
1968 break;
1969 }
1970
Evan Cheng04699902006-08-26 01:05:16 +00001971 OutOps.push_back(Op0);
1972 OutOps.push_back(Op1);
1973 OutOps.push_back(Op2);
1974 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001975 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001976 return false;
1977}
1978
Chris Lattnerc961eea2005-11-16 01:54:32 +00001979/// createX86ISelDag - This pass converts a legalized DAG into a
1980/// X86-specific DAG, ready for instruction scheduling.
1981///
Bill Wendling98a366d2009-04-29 23:29:43 +00001982FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1983 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001984 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001985}