blob: 0f64383b0b72939ec1bccedc8ecd4cd9be6c80c1 [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 Cheng0475ab52008-01-05 00:41:47 +000018#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000022#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000023#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000025#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000033#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000038#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000039#include "llvm/ADT/Statistic.h"
40using namespace llvm;
41
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
43
Chris Lattnerc961eea2005-11-16 01:54:32 +000044//===----------------------------------------------------------------------===//
45// Pattern Matcher Implementation
46//===----------------------------------------------------------------------===//
47
48namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000049 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000050 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// tree.
52 struct X86ISelAddressMode {
53 enum {
54 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000055 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 } BaseType;
57
Dan Gohmanffce6f12010-04-29 23:30:41 +000058 // This is really a union, discriminated by BaseType!
59 SDValue Base_Reg;
60 int Base_FrameIndex;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061
62 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000063 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000064 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000065 SDValue Segment;
Dan Gohman46510a72010-04-15 01:51:59 +000066 const GlobalValue *GV;
67 const Constant *CP;
68 const BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000069 const char *ES;
70 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000071 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000072 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073
74 X86ISelAddressMode()
Dan Gohmanffce6f12010-04-29 23:30:41 +000075 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000076 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000077 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000079
80 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000081 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000082 }
Chris Lattner18c59872009-06-27 04:16:01 +000083
84 bool hasBaseOrIndexReg() const {
Dan Gohmanffce6f12010-04-29 23:30:41 +000085 return IndexReg.getNode() != 0 || Base_Reg.getNode() != 0;
Chris Lattner18c59872009-06-27 04:16:01 +000086 }
87
88 /// isRIPRelative - Return true if this addressing mode is already RIP
89 /// relative.
90 bool isRIPRelative() const {
91 if (BaseType != RegBase) return false;
92 if (RegisterSDNode *RegNode =
Dan Gohmanffce6f12010-04-29 23:30:41 +000093 dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode()))
Chris Lattner18c59872009-06-27 04:16:01 +000094 return RegNode->getReg() == X86::RIP;
95 return false;
96 }
97
98 void setBaseReg(SDValue Reg) {
99 BaseType = RegBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +0000100 Base_Reg = Reg;
Chris Lattner18c59872009-06-27 04:16:01 +0000101 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000102
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000103 void dump() {
David Greened7f4f242010-01-05 01:29:08 +0000104 dbgs() << "X86ISelAddressMode " << this << '\n';
Dan Gohmanffce6f12010-04-29 23:30:41 +0000105 dbgs() << "Base_Reg ";
106 if (Base_Reg.getNode() != 0)
107 Base_Reg.getNode()->dump();
Bill Wendling12321672009-08-07 21:33:25 +0000108 else
David Greened7f4f242010-01-05 01:29:08 +0000109 dbgs() << "nul";
Dan Gohmanffce6f12010-04-29 23:30:41 +0000110 dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000111 << " Scale" << Scale << '\n'
112 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000113 if (IndexReg.getNode() != 0)
114 IndexReg.getNode()->dump();
115 else
David Greened7f4f242010-01-05 01:29:08 +0000116 dbgs() << "nul";
117 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000118 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000119 if (GV)
120 GV->dump();
121 else
David Greened7f4f242010-01-05 01:29:08 +0000122 dbgs() << "nul";
123 dbgs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000124 if (CP)
125 CP->dump();
126 else
David Greened7f4f242010-01-05 01:29:08 +0000127 dbgs() << "nul";
128 dbgs() << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000129 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000130 if (ES)
David Greened7f4f242010-01-05 01:29:08 +0000131 dbgs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000132 else
David Greened7f4f242010-01-05 01:29:08 +0000133 dbgs() << "nul";
134 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000135 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000136 };
137}
138
139namespace {
Evan Chengf3caa522010-03-17 23:58:35 +0000140 class X86ISelListener : public SelectionDAG::DAGUpdateListener {
141 SmallSet<SDNode*, 4> Deletes;
142 public:
143 explicit X86ISelListener() {}
144 virtual void NodeDeleted(SDNode *N, SDNode *E) {
145 Deletes.insert(N);
146 }
147 virtual void NodeUpdated(SDNode *N) {
148 // Ignore updates.
149 }
150 bool IsDeleted(SDNode *N) {
151 return Deletes.count(N);
152 }
153 };
154
Chris Lattnerc961eea2005-11-16 01:54:32 +0000155 //===--------------------------------------------------------------------===//
156 /// ISel - X86 specific code to select X86 machine instructions for
157 /// SelectionDAG operations.
158 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000159 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160 /// X86Lowering - This object fully describes how to lower LLVM code to an
161 /// X86-specific SelectionDAG.
Dan Gohmand858e902010-04-17 15:26:15 +0000162 const X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163
164 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
165 /// make the right decision when generating code for different targets.
166 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000167
Evan Chengb7a75a52008-09-26 23:41:32 +0000168 /// OptForSize - If true, selector should try to optimize for code size
169 /// instead of performance.
170 bool OptForSize;
171
Chris Lattnerc961eea2005-11-16 01:54:32 +0000172 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000173 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000174 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000175 X86Lowering(*tm.getTargetLowering()),
176 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000177 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000178
179 virtual const char *getPassName() const {
180 return "X86 DAG->DAG Instruction Selection";
181 }
182
Dan Gohman64652652010-04-14 20:17:22 +0000183 virtual void EmitFunctionEntryCode();
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000184
Evan Cheng014bf212010-02-15 19:41:07 +0000185 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
186
Chris Lattner7c306da2010-03-02 06:34:30 +0000187 virtual void PreprocessISelDAG();
188
Chris Lattnerc961eea2005-11-16 01:54:32 +0000189// Include the pieces autogenerated from the target description.
190#include "X86GenDAGISel.inc"
191
192 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000193 SDNode *Select(SDNode *N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000194 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000195 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000196
Rafael Espindola094fad32009-04-08 21:14:34 +0000197 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
198 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000199 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000200 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
201 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000202 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000203 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000204 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000205 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000206 SDValue &Scale, SDValue &Index, SDValue &Disp,
207 SDValue &Segment);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000208 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000210 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000211 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnere60f7b42010-03-01 22:51:11 +0000212 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattner92d3ada2010-02-16 22:35:06 +0000213 SDValue &Base, SDValue &Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000214 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000215 SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +0000216 SDValue &NodeWithChain);
217
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000218 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000220 SDValue &Index, SDValue &Disp,
221 SDValue &Segment);
Chris Lattner7c306da2010-03-02 06:34:30 +0000222
Chris Lattnerc0bad572006-06-08 18:03:49 +0000223 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
224 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000226 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000227 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000228
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000229 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
230
Dan Gohman475871a2008-07-27 21:46:04 +0000231 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
232 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000233 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000234 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Dan Gohmanffce6f12010-04-29 23:30:41 +0000235 CurDAG->getTargetFrameIndex(AM.Base_FrameIndex, TLI.getPointerTy()) :
236 AM.Base_Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000237 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000238 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000239 // These are 32-bit even in 64-bit mode since RIP relative offset
240 // is 32-bit.
241 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000243 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000246 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000251 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000252 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
253 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000256
257 if (AM.Segment.getNode())
258 Segment = AM.Segment;
259 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000261 }
262
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000263 /// getI8Imm - Return a target constant with the specified value, of type
264 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000265 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000267 }
268
Chris Lattnerc961eea2005-11-16 01:54:32 +0000269 /// getI16Imm - Return a target constant with the specified value, of type
270 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000271 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000273 }
274
275 /// getI32Imm - Return a target constant with the specified value, of type
276 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000279 }
Evan Chengf597dc72006-02-10 22:24:32 +0000280
Dan Gohman8b746962008-09-23 18:22:58 +0000281 /// getGlobalBaseReg - Return an SDNode that returns the value of
282 /// the global base register. Output instructions required to
283 /// initialize the global base register, if necessary.
284 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000285 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000286
Dan Gohmanc5534622009-06-03 20:20:00 +0000287 /// getTargetMachine - Return a reference to the TargetMachine, casted
288 /// to the target-specific type.
289 const X86TargetMachine &getTargetMachine() {
290 return static_cast<const X86TargetMachine &>(TM);
291 }
292
293 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
294 /// to the target-specific type.
295 const X86InstrInfo *getInstrInfo() {
296 return getTargetMachine().getInstrInfo();
297 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000298 };
299}
300
Evan Chengf4b4c412006-08-08 00:31:00 +0000301
Evan Cheng014bf212010-02-15 19:41:07 +0000302bool
303X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000304 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000305
Evan Cheng014bf212010-02-15 19:41:07 +0000306 if (!N.hasOneUse())
307 return false;
308
309 if (N.getOpcode() != ISD::LOAD)
310 return true;
311
312 // If N is a load, do additional profitability checks.
313 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000314 switch (U->getOpcode()) {
315 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000316 case X86ISD::ADD:
317 case X86ISD::SUB:
318 case X86ISD::AND:
319 case X86ISD::XOR:
320 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000321 case ISD::ADD:
322 case ISD::ADDC:
323 case ISD::ADDE:
324 case ISD::AND:
325 case ISD::OR:
326 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000327 SDValue Op1 = U->getOperand(1);
328
Evan Cheng884c70c2008-11-27 00:49:46 +0000329 // If the other operand is a 8-bit immediate we should fold the immediate
330 // instead. This reduces code size.
331 // e.g.
332 // movl 4(%esp), %eax
333 // addl $4, %eax
334 // vs.
335 // movl $4, %eax
336 // addl 4(%esp), %eax
337 // The former is 2 bytes shorter. In case where the increment is 1, then
338 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000339 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000340 if (Imm->getAPIntValue().isSignedIntN(8))
341 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000342
343 // If the other operand is a TLS address, we should fold it instead.
344 // This produces
345 // movl %gs:0, %eax
346 // leal i@NTPOFF(%eax), %eax
347 // instead of
348 // movl $i@NTPOFF, %eax
349 // addl %gs:0, %eax
350 // if the block also has an access to a second TLS address this will save
351 // a load.
352 // FIXME: This is probably also true for non TLS addresses.
353 if (Op1.getOpcode() == X86ISD::Wrapper) {
354 SDValue Val = Op1.getOperand(0);
355 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
356 return false;
357 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000358 }
359 }
Evan Cheng014bf212010-02-15 19:41:07 +0000360 }
361
362 return true;
363}
364
Evan Chengf48ef032010-03-14 03:48:46 +0000365/// MoveBelowCallOrigChain - Replace the original chain operand of the call with
366/// load's chain operand and move load below the call's chain operand.
367static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
368 SDValue Call, SDValue OrigChain) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000369 SmallVector<SDValue, 8> Ops;
Evan Chengf48ef032010-03-14 03:48:46 +0000370 SDValue Chain = OrigChain.getOperand(0);
Evan Cheng5b2e5892009-01-26 18:43:34 +0000371 if (Chain.getNode() == Load.getNode())
372 Ops.push_back(Load.getOperand(0));
373 else {
374 assert(Chain.getOpcode() == ISD::TokenFactor &&
Evan Chengf48ef032010-03-14 03:48:46 +0000375 "Unexpected chain operand");
Evan Cheng5b2e5892009-01-26 18:43:34 +0000376 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
377 if (Chain.getOperand(i).getNode() == Load.getNode())
378 Ops.push_back(Load.getOperand(0));
379 else
380 Ops.push_back(Chain.getOperand(i));
381 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000382 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000384 Ops.clear();
385 Ops.push_back(NewChain);
386 }
Evan Chengf48ef032010-03-14 03:48:46 +0000387 for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i)
388 Ops.push_back(OrigChain.getOperand(i));
389 CurDAG->UpdateNodeOperands(OrigChain, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000390 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
391 Load.getOperand(1), Load.getOperand(2));
392 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000393 Ops.push_back(SDValue(Load.getNode(), 1));
394 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000395 Ops.push_back(Call.getOperand(i));
396 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
397}
398
399/// isCalleeLoad - Return true if call address is a load and it can be
400/// moved below CALLSEQ_START and the chains leading up to the call.
401/// Return the CALLSEQ_START by reference as a second output.
Evan Chengf48ef032010-03-14 03:48:46 +0000402/// In the case of a tail call, there isn't a callseq node between the call
403/// chain and the load.
404static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000405 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000406 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000407 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000408 if (!LD ||
409 LD->isVolatile() ||
410 LD->getAddressingMode() != ISD::UNINDEXED ||
411 LD->getExtensionType() != ISD::NON_EXTLOAD)
412 return false;
413
414 // Now let's find the callseq_start.
Evan Chengf48ef032010-03-14 03:48:46 +0000415 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000416 if (!Chain.hasOneUse())
417 return false;
418 Chain = Chain.getOperand(0);
419 }
Evan Chengf48ef032010-03-14 03:48:46 +0000420
421 if (!Chain.getNumOperands())
422 return false;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000423 if (Chain.getOperand(0).getNode() == Callee.getNode())
424 return true;
425 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000426 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
427 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000428 return true;
429 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000430}
431
Chris Lattnerfb444af2010-03-02 23:12:51 +0000432void X86DAGToDAGISel::PreprocessISelDAG() {
Chris Lattner97d85342010-03-04 01:43:43 +0000433 // OptForSize is used in pattern predicates that isel is matching.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000434 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
435
Dan Gohmanf350b272008-08-23 02:25:05 +0000436 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
437 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000438 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000439
Evan Chengf48ef032010-03-14 03:48:46 +0000440 if (OptLevel != CodeGenOpt::None &&
441 (N->getOpcode() == X86ISD::CALL ||
442 N->getOpcode() == X86ISD::TC_RETURN)) {
Chris Lattnerfb444af2010-03-02 23:12:51 +0000443 /// Also try moving call address load from outside callseq_start to just
444 /// before the call to allow it to be folded.
445 ///
446 /// [Load chain]
447 /// ^
448 /// |
449 /// [Load]
450 /// ^ ^
451 /// | |
452 /// / \--
453 /// / |
454 ///[CALLSEQ_START] |
455 /// ^ |
456 /// | |
457 /// [LOAD/C2Reg] |
458 /// | |
459 /// \ /
460 /// \ /
461 /// [CALL]
Evan Chengf48ef032010-03-14 03:48:46 +0000462 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
Chris Lattnerfb444af2010-03-02 23:12:51 +0000463 SDValue Chain = N->getOperand(0);
464 SDValue Load = N->getOperand(1);
Evan Chengf48ef032010-03-14 03:48:46 +0000465 if (!isCalleeLoad(Load, Chain, HasCallSeq))
Chris Lattnerfb444af2010-03-02 23:12:51 +0000466 continue;
Evan Chengf48ef032010-03-14 03:48:46 +0000467 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
Chris Lattnerfb444af2010-03-02 23:12:51 +0000468 ++NumLoadMoved;
469 continue;
470 }
471
472 // Lower fpround and fpextend nodes that target the FP stack to be store and
473 // load to the stack. This is a gross hack. We would like to simply mark
474 // these as being illegal, but when we do that, legalize produces these when
475 // it expands calls, then expands these in the same legalize pass. We would
476 // like dag combine to be able to hack on these between the call expansion
477 // and the node legalization. As such this pass basically does "really
478 // late" legalization of these inline with the X86 isel pass.
479 // FIXME: This should only happen when not compiled with -O0.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000480 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
481 continue;
482
483 // If the source and destination are SSE registers, then this is a legal
484 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000485 EVT SrcVT = N->getOperand(0).getValueType();
486 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000487 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
488 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
489 if (SrcIsSSE && DstIsSSE)
490 continue;
491
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000492 if (!SrcIsSSE && !DstIsSSE) {
493 // If this is an FPStack extension, it is a noop.
494 if (N->getOpcode() == ISD::FP_EXTEND)
495 continue;
496 // If this is a value-preserving FPStack truncation, it is a noop.
497 if (N->getConstantOperandVal(1))
498 continue;
499 }
500
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000501 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
502 // FPStack has extload and truncstore. SSE can fold direct loads into other
503 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000504 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000505 if (N->getOpcode() == ISD::FP_ROUND)
506 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
507 else
508 MemVT = SrcIsSSE ? SrcVT : DstVT;
509
Dan Gohmanf350b272008-08-23 02:25:05 +0000510 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000511 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000512
513 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000514 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000515 N->getOperand(0),
David Greenedb8d9892010-02-15 16:57:43 +0000516 MemTmp, NULL, 0, MemVT,
517 false, false, 0);
Dale Johannesend8392542009-02-03 21:48:12 +0000518 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greenedb8d9892010-02-15 16:57:43 +0000519 NULL, 0, MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000520
521 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
522 // extload we created. This will cause general havok on the dag because
523 // anything below the conversion could be folded into other existing nodes.
524 // To avoid invalidating 'I', back it up to the convert node.
525 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000526 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000527
528 // Now that we did that, the node is dead. Increment the iterator to the
529 // next node to process, then delete N.
530 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000531 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000532 }
533}
534
Chris Lattnerc961eea2005-11-16 01:54:32 +0000535
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000536/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
537/// the main function.
538void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
539 MachineFrameInfo *MFI) {
540 const TargetInstrInfo *TII = TM.getInstrInfo();
541 if (Subtarget->isTargetCygMing())
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000542 BuildMI(BB, DebugLoc(),
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000543 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000544}
545
Dan Gohman64652652010-04-14 20:17:22 +0000546void X86DAGToDAGISel::EmitFunctionEntryCode() {
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000547 // If this is main, emit special code for main.
Dan Gohman64652652010-04-14 20:17:22 +0000548 if (const Function *Fn = MF->getFunction())
549 if (Fn->hasExternalLinkage() && Fn->getName() == "main")
550 EmitSpecialCodeForMain(MF->begin(), MF->getFrameInfo());
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000551}
552
Rafael Espindola094fad32009-04-08 21:14:34 +0000553
554bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
555 X86ISelAddressMode &AM) {
556 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
557 SDValue Segment = N.getOperand(0);
558
559 if (AM.Segment.getNode() == 0) {
560 AM.Segment = Segment;
561 return false;
562 }
563
564 return true;
565}
566
567bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
568 // This optimization is valid because the GNU TLS model defines that
569 // gs:0 (or fs:0 on X86-64) contains its own address.
570 // For more information see http://people.redhat.com/drepper/tls.pdf
571
572 SDValue Address = N.getOperand(1);
573 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
574 !MatchSegmentBaseAddress (Address, AM))
575 return false;
576
577 return true;
578}
579
Chris Lattner18c59872009-06-27 04:16:01 +0000580/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
581/// into an addressing mode. These wrap things that will resolve down into a
582/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000583/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000584bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000585 // If the addressing mode already has a symbol as the displacement, we can
586 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000587 if (AM.hasSymbolicDisplacement())
588 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000589
590 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000591 CodeModel::Model M = TM.getCodeModel();
592
Chris Lattner18c59872009-06-27 04:16:01 +0000593 // Handle X86-64 rip-relative addresses. We check this before checking direct
594 // folding because RIP is preferable to non-RIP accesses.
595 if (Subtarget->is64Bit() &&
596 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
597 // they cannot be folded into immediate fields.
598 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000599 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000600 // Base and index reg must be 0 in order to use %rip as base and lowering
601 // must allow RIP.
602 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000603 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
604 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000605 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000606 AM.GV = G->getGlobal();
607 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000608 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000609 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
610 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000611 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000612 AM.CP = CP->getConstVal();
613 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000614 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000615 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000616 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
617 AM.ES = S->getSymbol();
618 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000619 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000620 AM.JT = J->getIndex();
621 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000622 } else {
623 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000624 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000625 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000626
Chris Lattner18c59872009-06-27 04:16:01 +0000627 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000629 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000630 }
631
632 // Handle the case when globals fit in our immediate field: This is true for
633 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
634 // mode, this results in a non-RIP-relative computation.
635 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000636 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000637 TM.getRelocationModel() == Reloc::Static)) {
638 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
639 AM.GV = G->getGlobal();
640 AM.Disp += G->getOffset();
641 AM.SymbolFlags = G->getTargetFlags();
642 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
643 AM.CP = CP->getConstVal();
644 AM.Align = CP->getAlignment();
645 AM.Disp += CP->getOffset();
646 AM.SymbolFlags = CP->getTargetFlags();
647 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
648 AM.ES = S->getSymbol();
649 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000650 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000651 AM.JT = J->getIndex();
652 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000653 } else {
654 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000655 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000656 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000657 return false;
658 }
659
660 return true;
661}
662
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000663/// MatchAddress - Add the specified node to the specified addressing mode,
664/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000665/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000666bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
Evan Chengf3caa522010-03-17 23:58:35 +0000667 X86ISelListener DeadNodes;
668 if (MatchAddressRecursively(N, AM, DeadNodes, 0))
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000669 return true;
670
671 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
672 // a smaller encoding and avoids a scaled-index.
673 if (AM.Scale == 2 &&
674 AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000675 AM.Base_Reg.getNode() == 0) {
676 AM.Base_Reg = AM.IndexReg;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000677 AM.Scale = 1;
678 }
679
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000680 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
681 // because it has a smaller encoding.
682 // TODO: Which other code models can use this?
683 if (TM.getCodeModel() == CodeModel::Small &&
684 Subtarget->is64Bit() &&
685 AM.Scale == 1 &&
686 AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000687 AM.Base_Reg.getNode() == 0 &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000688 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000689 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000690 AM.hasSymbolicDisplacement())
Dan Gohmanffce6f12010-04-29 23:30:41 +0000691 AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000692
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000693 return false;
694}
695
Chris Lattnerd6139422010-04-20 23:18:40 +0000696/// isLogicallyAddWithConstant - Return true if this node is semantically an
697/// add of a value with a constantint.
698static bool isLogicallyAddWithConstant(SDValue V, SelectionDAG *CurDAG) {
699 // Check for (add x, Cst)
700 if (V->getOpcode() == ISD::ADD)
701 return isa<ConstantSDNode>(V->getOperand(1));
702
703 // Check for (or x, Cst), where Cst & x == 0.
704 if (V->getOpcode() != ISD::OR ||
705 !isa<ConstantSDNode>(V->getOperand(1)))
706 return false;
707
708 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
709 ConstantSDNode *CN = cast<ConstantSDNode>(V->getOperand(1));
710
711 // Check to see if the LHS & C is zero.
712 return CurDAG->MaskedValueIsZero(V->getOperand(0), CN->getAPIntValue());
713}
714
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000715bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000716 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000717 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000718 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000719 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000720 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000721 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000722 AM.dump();
723 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000724 // Limit recursion.
725 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000726 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000727
728 CodeModel::Model M = TM.getCodeModel();
729
Chris Lattner18c59872009-06-27 04:16:01 +0000730 // If this is already a %rip relative address, we can only merge immediates
731 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000732 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000733 if (AM.isRIPRelative()) {
734 // FIXME: JumpTable and ExternalSymbol address currently don't like
735 // displacements. It isn't very important, but this should be fixed for
736 // consistency.
737 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000738
Chris Lattner18c59872009-06-27 04:16:01 +0000739 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
740 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000741 if (X86::isOffsetSuitableForCodeModel(Val, M,
742 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000743 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000744 return false;
745 }
746 }
747 return true;
748 }
749
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000750 switch (N.getOpcode()) {
751 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000752 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000753 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000754 if (!is64Bit ||
755 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
756 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000757 AM.Disp += Val;
758 return false;
759 }
760 break;
761 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000762
Rafael Espindola094fad32009-04-08 21:14:34 +0000763 case X86ISD::SegmentBaseAddress:
764 if (!MatchSegmentBaseAddress(N, AM))
765 return false;
766 break;
767
Rafael Espindola49a168d2009-04-12 21:55:03 +0000768 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000769 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000770 if (!MatchWrapper(N, AM))
771 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000772 break;
773
Rafael Espindola094fad32009-04-08 21:14:34 +0000774 case ISD::LOAD:
775 if (!MatchLoad(N, AM))
776 return false;
777 break;
778
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000779 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000780 if (AM.BaseType == X86ISelAddressMode::RegBase
Dan Gohmanffce6f12010-04-29 23:30:41 +0000781 && AM.Base_Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000782 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +0000783 AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000784 return false;
785 }
786 break;
Evan Chengec693f72005-12-08 02:01:35 +0000787
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000788 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000789 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000790 break;
791
Gabor Greif93c53e52008-08-31 15:37:04 +0000792 if (ConstantSDNode
793 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000794 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000795 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
796 // that the base operand remains free for further matching. If
797 // the base doesn't end up getting used, a post-processing step
798 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000799 if (Val == 1 || Val == 2 || Val == 3) {
800 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000801 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000802
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000803 // Okay, we know that we have a scale by now. However, if the scaled
804 // value is an add of something and a constant, we can fold the
805 // constant into the disp field here.
Chris Lattnerd6139422010-04-20 23:18:40 +0000806 if (isLogicallyAddWithConstant(ShVal, CurDAG)) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000807 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000808 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000810 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000811 if (!is64Bit ||
812 X86::isOffsetSuitableForCodeModel(Disp, M,
813 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000814 AM.Disp = Disp;
815 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000816 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000817 } else {
818 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000819 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000820 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000821 }
822 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000823 }
Evan Chengec693f72005-12-08 02:01:35 +0000824
Dan Gohman83688052007-10-22 20:22:24 +0000825 case ISD::SMUL_LOHI:
826 case ISD::UMUL_LOHI:
827 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000828 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000829 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000830 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000831 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000832 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000833 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000834 AM.Base_Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000835 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000836 if (ConstantSDNode
837 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000838 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
839 CN->getZExtValue() == 9) {
840 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000841
Gabor Greifba36cb52008-08-28 21:40:38 +0000842 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000843 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844
845 // Okay, we know that we have a scale by now. However, if the scaled
846 // value is an add of something and a constant, we can fold the
847 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000848 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
849 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
850 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000851 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000852 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000853 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000854 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000855 if (!is64Bit ||
856 X86::isOffsetSuitableForCodeModel(Disp, M,
857 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000858 AM.Disp = Disp;
859 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000860 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000861 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863 }
864
Dan Gohmanffce6f12010-04-29 23:30:41 +0000865 AM.IndexReg = AM.Base_Reg = Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000866 return false;
867 }
Chris Lattner62412262007-02-04 20:18:17 +0000868 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869 break;
870
Dan Gohman3cd90a12009-05-11 18:02:53 +0000871 case ISD::SUB: {
872 // Given A-B, if A can be completely folded into the address and
873 // the index field with the index field unused, use -B as the index.
874 // This is a win if a has multiple parts that can be folded into
875 // the address. Also, this saves a mov if the base register has
876 // other uses, since it avoids a two-address sub instruction, however
877 // it costs an additional mov if the index register has other uses.
878
879 // Test if the LHS of the sub can be folded.
880 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000881 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM,
882 DeadNodes, Depth+1) ||
883 // If it is successful but the recursive update causes N to be deleted,
884 // then it's not safe to continue.
885 DeadNodes.IsDeleted(N.getNode())) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000886 AM = Backup;
887 break;
888 }
889 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000890 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000891 AM = Backup;
892 break;
893 }
Evan Chengf3caa522010-03-17 23:58:35 +0000894
Dan Gohman3cd90a12009-05-11 18:02:53 +0000895 int Cost = 0;
896 SDValue RHS = N.getNode()->getOperand(1);
897 // If the RHS involves a register with multiple uses, this
898 // transformation incurs an extra mov, due to the neg instruction
899 // clobbering its operand.
900 if (!RHS.getNode()->hasOneUse() ||
901 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
902 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
903 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
904 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +0000906 ++Cost;
907 // If the base is a register with multiple uses, this
908 // transformation may save a mov.
909 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000910 AM.Base_Reg.getNode() &&
911 !AM.Base_Reg.getNode()->hasOneUse()) ||
Dan Gohman3cd90a12009-05-11 18:02:53 +0000912 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
913 --Cost;
914 // If the folded LHS was interesting, this transformation saves
915 // address arithmetic.
916 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
917 ((AM.Disp != 0) && (Backup.Disp == 0)) +
918 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
919 --Cost;
920 // If it doesn't look like it may be an overall win, don't do it.
921 if (Cost >= 0) {
922 AM = Backup;
923 break;
924 }
925
926 // Ok, the transformation is legal and appears profitable. Go for it.
927 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
928 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
929 AM.IndexReg = Neg;
930 AM.Scale = 1;
931
932 // Insert the new nodes into the topological ordering.
933 if (Zero.getNode()->getNodeId() == -1 ||
934 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
935 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
936 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
937 }
938 if (Neg.getNode()->getNodeId() == -1 ||
939 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
940 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
941 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
942 }
943 return false;
944 }
945
Evan Cheng8e278262009-01-17 07:09:27 +0000946 case ISD::ADD: {
947 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000948 if (!MatchAddressRecursively(N.getNode()->getOperand(0), 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(1), 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 }
960
961 // Try again after commuting the operands.
Evan Cheng8e278262009-01-17 07:09:27 +0000962 AM = Backup;
Evan Chengf3caa522010-03-17 23:58:35 +0000963 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
964 DeadNodes, Depth+1)) {
965 if (DeadNodes.IsDeleted(N.getNode()))
966 // If it is successful but the recursive update causes N to be deleted,
967 // then it's not safe to continue.
968 return true;
969 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
970 DeadNodes, Depth+1))
971 // If it is successful but the recursive update causes N to be deleted,
972 // then it's not safe to continue.
973 return DeadNodes.IsDeleted(N.getNode());
974 }
Evan Cheng8e278262009-01-17 07:09:27 +0000975 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000976
977 // If we couldn't fold both operands into the address at the same time,
978 // see if we can just put each operand into a register and fold at least
979 // the add.
980 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000981 !AM.Base_Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +0000982 !AM.IndexReg.getNode()) {
Dan Gohmanffce6f12010-04-29 23:30:41 +0000983 AM.Base_Reg = N.getNode()->getOperand(0);
Dan Gohman77502c92009-03-13 02:25:09 +0000984 AM.IndexReg = N.getNode()->getOperand(1);
985 AM.Scale = 1;
986 return false;
987 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000988 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000989 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000990
Chris Lattner62412262007-02-04 20:18:17 +0000991 case ISD::OR:
992 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattnerd6139422010-04-20 23:18:40 +0000993 if (isLogicallyAddWithConstant(N, CurDAG)) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000994 X86ISelAddressMode Backup = AM;
Chris Lattnerd6139422010-04-20 23:18:40 +0000995 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
Dan Gohman27cae7b2008-11-11 15:52:29 +0000996 uint64_t Offset = CN->getSExtValue();
Evan Chengf3caa522010-03-17 23:58:35 +0000997
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000998 // Start with the LHS as an addr mode.
Evan Chengf3caa522010-03-17 23:58:35 +0000999 if (!MatchAddressRecursively(N.getOperand(0), AM, DeadNodes, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001000 // Address could not have picked a GV address for the displacement.
1001 AM.GV == NULL &&
1002 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001003 (!is64Bit ||
1004 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
Evan Chengf3caa522010-03-17 23:58:35 +00001005 AM.hasSymbolicDisplacement()))) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001006 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001007 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001008 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001009 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001010 }
1011 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001012
1013 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001014 // Perform some heroic transforms on an and of a constant-count shift
1015 // with a constant to enable use of the scaled offset field.
1016
Dan Gohman475871a2008-07-27 21:46:04 +00001017 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001018 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001019
Evan Cheng1314b002007-12-13 00:43:27 +00001020 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001022
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001023 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001024 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1025 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1026 if (!C1 || !C2) break;
1027
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001028 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1029 // allows us to convert the shift and and into an h-register extract and
1030 // a scaled index.
1031 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1032 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001033 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001034 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001036 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1037 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1038 X, Eight);
1039 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1040 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001042 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1043 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001044
1045 // Insert the new nodes into the topological ordering.
1046 if (Eight.getNode()->getNodeId() == -1 ||
1047 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1048 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1049 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1050 }
1051 if (Mask.getNode()->getNodeId() == -1 ||
1052 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1053 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1054 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1055 }
1056 if (Srl.getNode()->getNodeId() == -1 ||
1057 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1058 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1059 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1060 }
1061 if (And.getNode()->getNodeId() == -1 ||
1062 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1063 CurDAG->RepositionNode(N.getNode(), And.getNode());
1064 And.getNode()->setNodeId(N.getNode()->getNodeId());
1065 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001066 if (ShlCount.getNode()->getNodeId() == -1 ||
1067 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1068 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1069 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1070 }
1071 if (Shl.getNode()->getNodeId() == -1 ||
1072 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1073 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1074 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1075 }
Evan Chengf3caa522010-03-17 23:58:35 +00001076 CurDAG->ReplaceAllUsesWith(N, Shl, &DeadNodes);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001077 AM.IndexReg = And;
1078 AM.Scale = (1 << ScaleLog);
1079 return false;
1080 }
1081 }
1082
1083 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1084 // allows us to fold the shift into this addressing mode.
1085 if (Shift.getOpcode() != ISD::SHL) break;
1086
Evan Cheng1314b002007-12-13 00:43:27 +00001087 // Not likely to be profitable if either the AND or SHIFT node has more
1088 // than one use (unless all uses are for address computation). Besides,
1089 // isel mechanism requires their node ids to be reused.
1090 if (!N.hasOneUse() || !Shift.hasOneUse())
1091 break;
1092
1093 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001094 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001095 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1096 break;
1097
1098 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001099 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001100 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001101 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1102 NewANDMask);
1103 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001104 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001105
1106 // Insert the new nodes into the topological ordering.
1107 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1108 CurDAG->RepositionNode(X.getNode(), C1);
1109 C1->setNodeId(X.getNode()->getNodeId());
1110 }
1111 if (NewANDMask.getNode()->getNodeId() == -1 ||
1112 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1113 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1114 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1115 }
1116 if (NewAND.getNode()->getNodeId() == -1 ||
1117 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1118 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1119 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1120 }
1121 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1122 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1123 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1124 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1125 }
1126
Evan Chengf3caa522010-03-17 23:58:35 +00001127 CurDAG->ReplaceAllUsesWith(N, NewSHIFT, &DeadNodes);
Evan Cheng1314b002007-12-13 00:43:27 +00001128
1129 AM.Scale = 1 << ShiftCst;
1130 AM.IndexReg = NewAND;
1131 return false;
1132 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001133 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001134
Rafael Espindola523249f2009-03-31 16:16:57 +00001135 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001136}
1137
1138/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1139/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001140bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001141 // Is the base register already occupied?
Dan Gohmanffce6f12010-04-29 23:30:41 +00001142 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001143 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001144 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001145 AM.IndexReg = N;
1146 AM.Scale = 1;
1147 return false;
1148 }
1149
1150 // Otherwise, we cannot select it.
1151 return true;
1152 }
1153
1154 // Default, generate it as a register.
1155 AM.BaseType = X86ISelAddressMode::RegBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +00001156 AM.Base_Reg = N;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001157 return false;
1158}
1159
Evan Chengec693f72005-12-08 02:01:35 +00001160/// SelectAddr - returns true if it is able pattern match an addressing mode.
1161/// It returns the operands which make up the maximal addressing mode it can
1162/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001163bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001164 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001165 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001166 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001167 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001168 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001169
Owen Andersone50ed302009-08-10 22:56:29 +00001170 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001171 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Dan Gohmanffce6f12010-04-29 23:30:41 +00001172 if (!AM.Base_Reg.getNode())
1173 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001174 }
Evan Cheng8700e142006-01-11 06:09:51 +00001175
Gabor Greifba36cb52008-08-28 21:40:38 +00001176 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001177 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001178
Rafael Espindola094fad32009-04-08 21:14:34 +00001179 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001180 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001181}
1182
Chris Lattner3a7cd952006-10-07 21:55:32 +00001183/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1184/// match a load whose top elements are either undef or zeros. The load flavor
1185/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001186///
1187/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001188/// PatternChainNode: this is the matched node that has a chain input and
1189/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001190bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001191 SDValue N, SDValue &Base,
1192 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001193 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001194 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001195 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001196 PatternNodeWithChain = N.getOperand(0);
1197 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1198 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001199 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001200 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001201 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001202 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001203 return false;
1204 return true;
1205 }
1206 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001207
1208 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001209 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001210 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001211 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001212 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001213 N.getOperand(0).getNode()->hasOneUse() &&
1214 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001215 N.getOperand(0).getOperand(0).hasOneUse() &&
1216 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001217 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001218 // Okay, this is a zero extending load. Fold it.
1219 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001220 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001221 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001222 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001223 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001224 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001225 return false;
1226}
1227
1228
Evan Cheng51a9ed92006-02-25 10:09:08 +00001229/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1230/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001231bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SDValue &Base, SDValue &Scale,
1233 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001234 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001235
1236 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1237 // segments.
1238 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001240 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001241 if (MatchAddress(N, AM))
1242 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001243 assert (T == AM.Segment);
1244 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001245
Owen Andersone50ed302009-08-10 22:56:29 +00001246 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001247 unsigned Complexity = 0;
1248 if (AM.BaseType == X86ISelAddressMode::RegBase)
Dan Gohmanffce6f12010-04-29 23:30:41 +00001249 if (AM.Base_Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001250 Complexity = 1;
1251 else
Dan Gohmanffce6f12010-04-29 23:30:41 +00001252 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001253 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1254 Complexity = 4;
1255
Gabor Greifba36cb52008-08-28 21:40:38 +00001256 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001257 Complexity++;
1258 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001259 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001260
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001261 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1262 // a simple shift.
1263 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001264 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001265
1266 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1267 // to a LEA. This is determined with some expermentation but is by no means
1268 // optimal (especially for code size consideration). LEA is nice because of
1269 // its three-address nature. Tweak the cost function again when we can run
1270 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001271 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001272 // For X86-64, we should always use lea to materialize RIP relative
1273 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001274 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001275 Complexity = 4;
1276 else
1277 Complexity += 2;
1278 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001279
Dan Gohmanffce6f12010-04-29 23:30:41 +00001280 if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001281 Complexity++;
1282
Chris Lattner25142782009-07-11 22:50:33 +00001283 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001284 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001285 return false;
1286
1287 SDValue Segment;
1288 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1289 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001290}
1291
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001292/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001293bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001294 SDValue &Scale, SDValue &Index,
1295 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001296 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1297 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1298
1299 X86ISelAddressMode AM;
1300 AM.GV = GA->getGlobal();
1301 AM.Disp += GA->getOffset();
Dan Gohmanffce6f12010-04-29 23:30:41 +00001302 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001303 AM.SymbolFlags = GA->getTargetFlags();
1304
Owen Anderson825b72b2009-08-11 20:47:22 +00001305 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001306 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001308 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001310 }
1311
1312 SDValue Segment;
1313 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1314 return true;
1315}
1316
1317
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001318bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001319 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001320 SDValue &Index, SDValue &Disp,
1321 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001322 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1323 !IsProfitableToFold(N, P, P) ||
Dan Gohmand858e902010-04-17 15:26:15 +00001324 !IsLegalToFold(N, P, P, OptLevel))
Chris Lattnerd1b73822010-03-02 22:20:06 +00001325 return false;
1326
1327 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001328}
1329
Dan Gohman8b746962008-09-23 18:22:58 +00001330/// getGlobalBaseReg - Return an SDNode that returns the value of
1331/// the global base register. Output instructions required to
1332/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001333///
Evan Cheng9ade2182006-08-26 05:34:46 +00001334SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001335 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001337}
1338
Evan Chengb245d922006-05-20 01:36:52 +00001339static SDNode *FindCallStartFromCall(SDNode *Node) {
1340 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001342 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001343 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001344}
1345
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001346SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1347 SDValue Chain = Node->getOperand(0);
1348 SDValue In1 = Node->getOperand(1);
1349 SDValue In2L = Node->getOperand(2);
1350 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001351 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001352 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001353 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001354 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1355 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1356 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1357 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1358 MVT::i32, MVT::i32, MVT::Other, Ops,
1359 array_lengthof(Ops));
1360 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1361 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001362}
Christopher Lambc59e5212007-08-10 21:48:46 +00001363
Owen Andersone50ed302009-08-10 22:56:29 +00001364SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001365 if (Node->hasAnyUseOfValue(0))
1366 return 0;
1367
1368 // Optimize common patterns for __sync_add_and_fetch and
1369 // __sync_sub_and_fetch where the result is not used. This allows us
1370 // to use "lock" version of add, sub, inc, dec instructions.
1371 // FIXME: Do not use special instructions but instead add the "lock"
1372 // prefix to the target node somehow. The extra information will then be
1373 // transferred to machine instruction and it denotes the prefix.
1374 SDValue Chain = Node->getOperand(0);
1375 SDValue Ptr = Node->getOperand(1);
1376 SDValue Val = Node->getOperand(2);
1377 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001378 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001379 return 0;
1380
1381 bool isInc = false, isDec = false, isSub = false, isCN = false;
1382 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1383 if (CN) {
1384 isCN = true;
1385 int64_t CNVal = CN->getSExtValue();
1386 if (CNVal == 1)
1387 isInc = true;
1388 else if (CNVal == -1)
1389 isDec = true;
1390 else if (CNVal >= 0)
1391 Val = CurDAG->getTargetConstant(CNVal, NVT);
1392 else {
1393 isSub = true;
1394 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1395 }
1396 } else if (Val.hasOneUse() &&
1397 Val.getOpcode() == ISD::SUB &&
1398 X86::isZeroNode(Val.getOperand(0))) {
1399 isSub = true;
1400 Val = Val.getOperand(1);
1401 }
1402
1403 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001405 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001407 if (isInc)
1408 Opc = X86::LOCK_INC8m;
1409 else if (isDec)
1410 Opc = X86::LOCK_DEC8m;
1411 else if (isSub) {
1412 if (isCN)
1413 Opc = X86::LOCK_SUB8mi;
1414 else
1415 Opc = X86::LOCK_SUB8mr;
1416 } else {
1417 if (isCN)
1418 Opc = X86::LOCK_ADD8mi;
1419 else
1420 Opc = X86::LOCK_ADD8mr;
1421 }
1422 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001424 if (isInc)
1425 Opc = X86::LOCK_INC16m;
1426 else if (isDec)
1427 Opc = X86::LOCK_DEC16m;
1428 else if (isSub) {
1429 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001430 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001431 Opc = X86::LOCK_SUB16mi8;
1432 else
1433 Opc = X86::LOCK_SUB16mi;
1434 } else
1435 Opc = X86::LOCK_SUB16mr;
1436 } else {
1437 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001438 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001439 Opc = X86::LOCK_ADD16mi8;
1440 else
1441 Opc = X86::LOCK_ADD16mi;
1442 } else
1443 Opc = X86::LOCK_ADD16mr;
1444 }
1445 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001447 if (isInc)
1448 Opc = X86::LOCK_INC32m;
1449 else if (isDec)
1450 Opc = X86::LOCK_DEC32m;
1451 else if (isSub) {
1452 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001453 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001454 Opc = X86::LOCK_SUB32mi8;
1455 else
1456 Opc = X86::LOCK_SUB32mi;
1457 } else
1458 Opc = X86::LOCK_SUB32mr;
1459 } else {
1460 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001461 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001462 Opc = X86::LOCK_ADD32mi8;
1463 else
1464 Opc = X86::LOCK_ADD32mi;
1465 } else
1466 Opc = X86::LOCK_ADD32mr;
1467 }
1468 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001470 if (isInc)
1471 Opc = X86::LOCK_INC64m;
1472 else if (isDec)
1473 Opc = X86::LOCK_DEC64m;
1474 else if (isSub) {
1475 Opc = X86::LOCK_SUB64mr;
1476 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001477 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001478 Opc = X86::LOCK_SUB64mi8;
1479 else if (Predicate_i64immSExt32(Val.getNode()))
1480 Opc = X86::LOCK_SUB64mi32;
1481 }
1482 } else {
1483 Opc = X86::LOCK_ADD64mr;
1484 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001485 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001486 Opc = X86::LOCK_ADD64mi8;
1487 else if (Predicate_i64immSExt32(Val.getNode()))
1488 Opc = X86::LOCK_ADD64mi32;
1489 }
1490 }
1491 break;
1492 }
1493
1494 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001495 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001496 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001497 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1498 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001499 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001500 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1501 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1502 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001503 SDValue RetVals[] = { Undef, Ret };
1504 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1505 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001506 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1507 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1508 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001509 SDValue RetVals[] = { Undef, Ret };
1510 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1511 }
1512}
1513
Dan Gohman11596ed2009-10-09 20:35:19 +00001514/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1515/// any uses which require the SF or OF bits to be accurate.
1516static bool HasNoSignedComparisonUses(SDNode *N) {
1517 // Examine each user of the node.
1518 for (SDNode::use_iterator UI = N->use_begin(),
1519 UE = N->use_end(); UI != UE; ++UI) {
1520 // Only examine CopyToReg uses.
1521 if (UI->getOpcode() != ISD::CopyToReg)
1522 return false;
1523 // Only examine CopyToReg uses that copy to EFLAGS.
1524 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1525 X86::EFLAGS)
1526 return false;
1527 // Examine each user of the CopyToReg use.
1528 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1529 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1530 // Only examine the Flag result.
1531 if (FlagUI.getUse().getResNo() != 1) continue;
1532 // Anything unusual: assume conservatively.
1533 if (!FlagUI->isMachineOpcode()) return false;
1534 // Examine the opcode of the user.
1535 switch (FlagUI->getMachineOpcode()) {
1536 // These comparisons don't treat the most significant bit specially.
1537 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1538 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1539 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1540 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001541 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1542 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001543 case X86::CMOVA16rr: case X86::CMOVA16rm:
1544 case X86::CMOVA32rr: case X86::CMOVA32rm:
1545 case X86::CMOVA64rr: case X86::CMOVA64rm:
1546 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1547 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1548 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1549 case X86::CMOVB16rr: case X86::CMOVB16rm:
1550 case X86::CMOVB32rr: case X86::CMOVB32rm:
1551 case X86::CMOVB64rr: case X86::CMOVB64rm:
1552 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1553 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1554 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1555 case X86::CMOVE16rr: case X86::CMOVE16rm:
1556 case X86::CMOVE32rr: case X86::CMOVE32rm:
1557 case X86::CMOVE64rr: case X86::CMOVE64rm:
1558 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1559 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1560 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1561 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1562 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1563 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1564 case X86::CMOVP16rr: case X86::CMOVP16rm:
1565 case X86::CMOVP32rr: case X86::CMOVP32rm:
1566 case X86::CMOVP64rr: case X86::CMOVP64rm:
1567 continue;
1568 // Anything else: assume conservatively.
1569 default: return false;
1570 }
1571 }
1572 }
1573 return true;
1574}
1575
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001576SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001577 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001578 unsigned Opc, MOpc;
1579 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001580 DebugLoc dl = Node->getDebugLoc();
1581
Chris Lattner7c306da2010-03-02 06:34:30 +00001582 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001583
Dan Gohmane8be6c62008-07-17 19:10:17 +00001584 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001585 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001586 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001587 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001588
Evan Cheng0114e942006-01-06 20:36:21 +00001589 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001590 default: break;
1591 case X86ISD::GlobalBaseReg:
1592 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001593
Dan Gohman72677342009-08-02 16:10:52 +00001594 case X86ISD::ATOMOR64_DAG:
1595 return SelectAtomic64(Node, X86::ATOMOR6432);
1596 case X86ISD::ATOMXOR64_DAG:
1597 return SelectAtomic64(Node, X86::ATOMXOR6432);
1598 case X86ISD::ATOMADD64_DAG:
1599 return SelectAtomic64(Node, X86::ATOMADD6432);
1600 case X86ISD::ATOMSUB64_DAG:
1601 return SelectAtomic64(Node, X86::ATOMSUB6432);
1602 case X86ISD::ATOMNAND64_DAG:
1603 return SelectAtomic64(Node, X86::ATOMNAND6432);
1604 case X86ISD::ATOMAND64_DAG:
1605 return SelectAtomic64(Node, X86::ATOMAND6432);
1606 case X86ISD::ATOMSWAP64_DAG:
1607 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001608
Dan Gohman72677342009-08-02 16:10:52 +00001609 case ISD::ATOMIC_LOAD_ADD: {
1610 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1611 if (RetVal)
1612 return RetVal;
1613 break;
1614 }
1615
1616 case ISD::SMUL_LOHI:
1617 case ISD::UMUL_LOHI: {
1618 SDValue N0 = Node->getOperand(0);
1619 SDValue N1 = Node->getOperand(1);
1620
1621 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001622 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001624 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1626 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1627 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1628 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001629 }
Bill Wendling12321672009-08-07 21:33:25 +00001630 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001632 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1634 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1635 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1636 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001637 }
Bill Wendling12321672009-08-07 21:33:25 +00001638 }
Dan Gohman72677342009-08-02 16:10:52 +00001639
1640 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001642 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1644 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1645 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1646 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001647 }
1648
1649 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001650 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001651 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001652 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001653 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001654 if (foldedLoad)
1655 std::swap(N0, N1);
1656 }
1657
1658 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1659 N0, SDValue()).getValue(1);
1660
1661 if (foldedLoad) {
1662 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1663 InFlag };
1664 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001665 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1666 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001667 InFlag = SDValue(CNode, 1);
1668 // Update the chain.
1669 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1670 } else {
1671 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001672 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001673 }
1674
1675 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001676 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001677 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1678 LoReg, NVT, InFlag);
1679 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001680 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001681 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001682 }
1683 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001684 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001685 SDValue Result;
1686 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1687 // Prevent use of AH in a REX instruction by referencing AX instead.
1688 // Shift it down 8 bits.
1689 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001691 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001692 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1693 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001695 // Then truncate it down to i8.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001696 Result = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001697 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001698 } else {
1699 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1700 HiReg, NVT, InFlag);
1701 InFlag = Result.getValue(2);
1702 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001703 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001704 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001705 }
1706
Dan Gohman72677342009-08-02 16:10:52 +00001707 return NULL;
1708 }
1709
1710 case ISD::SDIVREM:
1711 case ISD::UDIVREM: {
1712 SDValue N0 = Node->getOperand(0);
1713 SDValue N1 = Node->getOperand(1);
1714
1715 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001716 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001718 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001719 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1720 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1721 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1722 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001723 }
Bill Wendling12321672009-08-07 21:33:25 +00001724 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001726 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001727 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1728 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1729 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1730 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001731 }
Bill Wendling12321672009-08-07 21:33:25 +00001732 }
Dan Gohman72677342009-08-02 16:10:52 +00001733
Chris Lattner9e323832009-12-23 01:45:04 +00001734 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001735 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001736 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001737 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001738 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001739 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001740 ClrOpcode = 0;
1741 SExtOpcode = X86::CBW;
1742 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001743 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001744 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001745 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001746 SExtOpcode = X86::CWD;
1747 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001749 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001750 ClrOpcode = X86::MOV32r0;
1751 SExtOpcode = X86::CDQ;
1752 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001754 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001755 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001756 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001757 break;
1758 }
1759
Dan Gohman72677342009-08-02 16:10:52 +00001760 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001761 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001762 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001763
Dan Gohman72677342009-08-02 16:10:52 +00001764 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001766 // Special case for div8, just use a move with zero extension to AX to
1767 // clear the upper 8 bits (AH).
1768 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001769 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001770 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1771 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001772 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1773 MVT::Other, Ops,
1774 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001775 Chain = Move.getValue(1);
1776 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001777 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001778 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001779 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001780 Chain = CurDAG->getEntryNode();
1781 }
1782 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1783 InFlag = Chain.getValue(1);
1784 } else {
1785 InFlag =
1786 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1787 LoReg, N0, SDValue()).getValue(1);
1788 if (isSigned && !signBitIsZero) {
1789 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001790 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001791 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001792 } else {
1793 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001794 SDValue ClrNode =
1795 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001796 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001797 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001798 }
Evan Cheng948f3432006-01-06 23:19:29 +00001799 }
Dan Gohman525178c2007-10-08 18:33:35 +00001800
Dan Gohman72677342009-08-02 16:10:52 +00001801 if (foldedLoad) {
1802 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1803 InFlag };
1804 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001805 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1806 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001807 InFlag = SDValue(CNode, 1);
1808 // Update the chain.
1809 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1810 } else {
1811 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001812 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001813 }
Evan Cheng948f3432006-01-06 23:19:29 +00001814
Dan Gohman72677342009-08-02 16:10:52 +00001815 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001816 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001817 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1818 LoReg, NVT, InFlag);
1819 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001820 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001821 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001822 }
1823 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001824 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001825 SDValue Result;
1826 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1827 // Prevent use of AH in a REX instruction by referencing AX instead.
1828 // Shift it down 8 bits.
1829 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001831 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001832 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001833 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001835 0);
1836 // Then truncate it down to i8.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001837 Result = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001838 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001839 } else {
1840 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1841 HiReg, NVT, InFlag);
1842 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001843 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001844 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001845 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001846 }
Dan Gohman72677342009-08-02 16:10:52 +00001847 return NULL;
1848 }
1849
Dan Gohman6a402dc2009-08-19 18:16:17 +00001850 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001851 SDValue N0 = Node->getOperand(0);
1852 SDValue N1 = Node->getOperand(1);
1853
1854 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1855 // use a smaller encoding.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00001856 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse())
1857 // Look past the truncate if CMP is the only use of it.
1858 N0 = N0.getOperand(0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001859 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1860 N0.getValueType() != MVT::i8 &&
1861 X86::isZeroNode(N1)) {
1862 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1863 if (!C) break;
1864
1865 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00001866 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
1867 (!(C->getZExtValue() & 0x80) ||
1868 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001869 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1870 SDValue Reg = N0.getNode()->getOperand(0);
1871
1872 // On x86-32, only the ABCD registers have 8-bit subregisters.
1873 if (!Subtarget->is64Bit()) {
1874 TargetRegisterClass *TRC = 0;
1875 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1876 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1877 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1878 default: llvm_unreachable("Unsupported TEST operand type!");
1879 }
1880 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001881 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1882 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001883 }
1884
1885 // Extract the l-register.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001886 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001887 MVT::i8, Reg);
1888
1889 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00001890 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001891 }
1892
1893 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00001894 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
1895 (!(C->getZExtValue() & 0x8000) ||
1896 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001897 // Shift the immediate right by 8 bits.
1898 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1899 MVT::i8);
1900 SDValue Reg = N0.getNode()->getOperand(0);
1901
1902 // Put the value in an ABCD register.
1903 TargetRegisterClass *TRC = 0;
1904 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1905 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
1906 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1907 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1908 default: llvm_unreachable("Unsupported TEST operand type!");
1909 }
1910 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001911 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1912 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001913
1914 // Extract the h-register.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001915 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001916 MVT::i8, Reg);
1917
1918 // Emit a testb. No special NOREX tricks are needed since there's
1919 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00001920 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
1921 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001922 }
1923
1924 // For example, "testl %eax, $32776" to "testw %ax, $32776".
1925 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001926 N0.getValueType() != MVT::i16 &&
1927 (!(C->getZExtValue() & 0x8000) ||
1928 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001929 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
1930 SDValue Reg = N0.getNode()->getOperand(0);
1931
1932 // Extract the 16-bit subregister.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001933 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001934 MVT::i16, Reg);
1935
1936 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00001937 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001938 }
1939
1940 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
1941 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001942 N0.getValueType() == MVT::i64 &&
1943 (!(C->getZExtValue() & 0x80000000) ||
1944 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001945 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
1946 SDValue Reg = N0.getNode()->getOperand(0);
1947
1948 // Extract the 32-bit subregister.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001949 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00001950 MVT::i32, Reg);
1951
1952 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00001953 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001954 }
1955 }
1956 break;
1957 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001958 }
1959
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001960 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00001961
Chris Lattner7c306da2010-03-02 06:34:30 +00001962 DEBUG(dbgs() << "=> ";
1963 if (ResNode == NULL || ResNode == Node)
1964 Node->dump(CurDAG);
1965 else
1966 ResNode->dump(CurDAG);
1967 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001968
1969 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001970}
1971
Chris Lattnerc0bad572006-06-08 18:03:49 +00001972bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001973SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001974 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001975 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001976 switch (ConstraintCode) {
1977 case 'o': // offsetable ??
1978 case 'v': // not offsetable ??
1979 default: return true;
1980 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001981 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001982 return true;
1983 break;
1984 }
1985
Evan Cheng04699902006-08-26 01:05:16 +00001986 OutOps.push_back(Op0);
1987 OutOps.push_back(Op1);
1988 OutOps.push_back(Op2);
1989 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001990 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001991 return false;
1992}
1993
Chris Lattnerc961eea2005-11-16 01:54:32 +00001994/// createX86ISelDag - This pass converts a legalized DAG into a
1995/// X86-specific DAG, ready for instruction scheduling.
1996///
Bill Wendling98a366d2009-04-29 23:29:43 +00001997FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1998 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001999 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002000}